このページでは

Qt OPC UA X.509のサポート

鍵および証明書署名要求の生成方法を説明します。

この例では、クライアントアプリケーションが独自の自己署名証明書を生成する方法、または証明書署名要求を生成する方法を示します。

RSA 鍵の生成

まず、RSA 鍵を生成します:

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

秘密鍵は、後で使用するためにファイルに保存できます:

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

証明書署名要求の生成

次に、証明書署名要求を作成します。また、証明書のサブジェクトを設定し、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);

ここで2つの選択肢があります:

1. 証明書署名要求を認証局(CA)に署名してもらう必要がある場合は、この要求データを使用する必要があります。

QByteArray certificateSigningRequestData = csr.createRequest(key);

2. 認証局がない場合は、リクエストを自己署名する必要があります。

QByteArray selfSignedCertificateData = csr.createSelfSignedCertificate(key);

サンプルプロジェクト @ 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.