Qt OPC UA X509 サポート

鍵と証明書署名要求の生成方法を示す。

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

RSA 鍵の生成

最初に、RSA 鍵が生成されます:

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

秘密鍵はファイルに保存しておくことができる:

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

QFile keyFile(u"privateKey.pem"_s);
keyFile.open(QFile::WriteOnly);
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.証明書署名要求が認証局によって署名される必要がある場合、要求データを使用する必要があります。

QByteArray certificateSigningRequestData = csr.createRequest(key);

2.認証局がない場合は、自己署名する。

QByteArray selfSignedCertificateData = csr.createSelfSignedCertificate(key);

ファイル

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