Инициализация защищенного сервера в OPCUA с использованием Open62541C++

Программы на C++. Форум разработчиков
Anonymous
Инициализация защищенного сервера в OPCUA с использованием Open62541

Сообщение Anonymous »

Я пытаюсь создать минимальный сервер, используя сертификаты ACS и AES256.
Код и сертификаты
Я создал сертификаты с помощью OpenSSL .



< th style="text-align: left;">Шаг
Команда
Результат




Закрытый ключ сервера
openssl genpkey -algorithm RSA -out opcua_server.key -aes256
Зашифрованный ключ


Запрос на подпись сервера
openssl req -new -key opcua_server.key -out request.csr
< td style="text-align: right;">Запрос CSR


Сертификат сервера с URI
openssl req -new -key decrypted_opcua_server.key -x509 -days 365 -out certificate.pem -subj "/C= IT/ST=Campania/L=Roma/O=Senatus Romanum/CN=Opcua Legionis" -addext "subjectAltName=URI:urn:OPCUASecureServerTest"
Сертификат PEM


Декапсулировать в DER
openssl x509 -in certificate.pem -outform der -out certificate.der
Сертификат DER


Отображение данных сертификата
< td style="text-align: left;">openssl x509 -in certificate.der -inform der -text -noout
Нет
Нет



Затем я пытаюсь настроить сервер.Утилиты

Код: Выделить всё

UA_StatusCode myInsecurePasswordCallback(UA_ServerConfig *sc,
UA_ByteString *bytestringPassword) {
char* sec = (char*)"insecure_password";
*bytestringPassword = UA_STRING_ALLOC(sec);
return UA_STATUSCODE_GOOD;
}

void printApplicationUriFromLoadedCertificate(UA_ServerConfig *config) {
UA_Boolean none = false;
printf("Verifying certificates on endpoints...\n");
// Loop through the existing security policies and print certificate info
for(size_t i = 0; i < config->securityPoliciesSize; i++) {
UA_EndpointDescription *endpoint = &config->endpoints[0];
UA_ByteString *certificate = &endpoint->serverCertificate;
printf("*");
if(certificate->length > 0) {
printf("\nLoaded Certificate Application URI: %.*s\n", (int)certificate->length, certificate->data);
none = true;
}
}
if (!none) printf("\nNo certificate loaded.\n");
}

// Custom permissions and roles functions
UA_UInt32 custom_getUserPermissions(const UA_AccessControl *ac, void *sessionContext, const UA_NodeId *nodeId, UA_UInt32 permissions) {
return permissions;
}

static UA_Boolean
allowAddNode(UA_Server *server, UA_AccessControl *ac,
const UA_NodeId *sessionId, void *sessionContext,
const UA_AddNodesItem *item) {
printf("Called allowAddNode\n");
return UA_TRUE;
}

static UA_Boolean
allowAddReference(UA_Server *server, UA_AccessControl *ac,
const UA_NodeId *sessionId, void *sessionContext,
const UA_AddReferencesItem *item) {
printf("Called allowAddReference\n");
return UA_TRUE;
}

static UA_Boolean
allowDeleteNode(UA_Server *server, UA_AccessControl *ac,
const UA_NodeId *sessionId, void *sessionContext,
const UA_DeleteNodesItem *item) {
printf("Called allowDeleteNode\n");
return UA_FALSE;  // Do not allow deletion from client
}

static UA_Boolean
allowDeleteReference(UA_Server *server, UA_AccessControl *ac,
const UA_NodeId *sessionId, void *sessionContext,
const UA_DeleteReferencesItem *item) {
printf("Called allowDeleteReference\n");
return UA_TRUE;
}

static UA_UsernamePasswordLogin userNamePW[2] = {
{UA_STRING_STATIC("station-client"), UA_STRING_STATIC("operator1")},
{UA_STRING_STATIC("admin-client"), UA_STRING_STATIC("superSecurePassword1!")}
};
Сервер

Код: Выделить всё

int main() {
signal(SIGINT, stopHandler);
signal(SIGTERM, stopHandler);

UA_String applicationUri =  UA_String_fromChars("urn:open62541.OPCUASecureServerTest");

// Hardcoded paths to the certificate and private key files
const char* certificatePath =  "./modules/opcua/src/.server_security/certificate.der";
const char* privateKeyPath =  "./modules/opcua/src/.server_security/privatekey.der";

// Load server certificates
const UA_ByteString certificate = loadFile(certificatePath);
const UA_ByteString privateKey = loadFile(privateKeyPath);

// Create the server
UA_Server *server = UA_Server_new();
UA_ServerConfig *config = UA_Server_getConfig(server);

// Output the Application URI for debugging
std::cout accessControl.allowAddNode = allowAddNode;
config->accessControl.allowAddReference = allowAddReference;
config->accessControl.allowDeleteNode = allowDeleteNode;
config->accessControl.allowDeleteReference = allowDeleteReference;
UA_ServerConfig_addSecurityPolicyBasic256Sha256(config, &certificate, &privateKey);

// Output the Application URI for debugging
std::cout 

Подробнее здесь: [url]https://stackoverflow.com/questions/79030584/initialization-of-a-secure-server-in-opcua-using-open62541[/url]

Вернуться в «C++»