In questa pagina

Qt OPC UA Supporto X509

Mostra come generare chiavi e richieste di firma del certificato.

Questo esempio illustra come le applicazioni client possano generare il proprio certificato autofirmato o generare una richiesta di firma del certificato.

Generazione della chiave RSA

Per prima cosa, viene generata una chiave RSA:

QOpcUaKeyPair key;
key.generateRsaKey(QOpcUaKeyPair::RsaKeyStrength::Bits2048);

La chiave privata può essere salvata in un file per un utilizzo successivo:

QByteArray keyData = key.privateKeyToByteArray(QOpcUaKeyPair::Cipher::Unencrypted, QString());

QFile keyFile(u"privateKey.pem"_s);
if (!keyFile.open(QFile::WriteOnly))
    return EXIT_FAILURE;

keyFile.write(keyData);
keyFile.close();

Generazione di richieste di firma certificato

Successivamente, viene creata una richiesta di firma del certificato. È inoltre necessario impostare il soggetto del certificato e aggiungere tutte le estensioni necessarie per OPC UA.

QOpcUaX509CertificateSigningRequest csr;

// Set the subject of the certificate
QOpcUaX509DistinguishedName dn;
dn.setEntry(QOpcUaX509DistinguishedName::Type::CommonName, u"QtOpcUaViewer"_s);
dn.setEntry(QOpcUaX509DistinguishedName::Type::CountryName, u"DE"_s);
dn.setEntry(QOpcUaX509DistinguishedName::Type::LocalityName, u"Berlin"_s);
dn.setEntry(QOpcUaX509DistinguishedName::Type::StateOrProvinceName, u"Berlin"_s);
dn.setEntry(QOpcUaX509DistinguishedName::Type::OrganizationName, u"The Qt Company"_s);
csr.setSubject(dn);

Ora ci sono due opzioni:

1. Se è necessario far firmare la richiesta di firma del certificato da un’autorità di certificazione, è necessario utilizzare i dati della richiesta.

QByteArray certificateSigningRequestData = csr.createRequest(key);

2. Se non è presente un'autorità di certificazione, è necessario autofirmare la richiesta.

QByteArray selfSignedCertificateData = csr.createSelfSignedCertificate(key);

Progetto di esempio su code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.