En esta página

Qt OPC UA Soporte X509

Muestra cómo generar claves y solicitudes de firma de certificado.

Este ejemplo muestra cómo las aplicaciones cliente pueden generar su propio certificado autofirmado o generar una solicitud de firma de certificado.

Generación de una clave RSA

En primer lugar, se genera una clave RSA:

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

La clave privada puede guardarse en un archivo para su uso posterior:

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();

Generación de solicitudes de firma de certificado

A continuación, se crea una solicitud de firma de certificado. También es necesario establecer el asunto del certificado y añadir todas las extensiones necesarias para 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);

Ahora hay dos opciones:

1. Cuando necesites que una autoridad de certificación firme tu solicitud de firma de certificado, tienes que utilizar los datos de la solicitud.

QByteArray certificateSigningRequestData = csr.createRequest(key);

2. Cuando no hay ninguna autoridad de certificación, tiene que autofirmar la solicitud.

QByteArray selfSignedCertificateData = csr.createSelfSignedCertificate(key);

Ficheros:

© 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.