QSslCertificate 类

The QSslCertificate class provides a convenient API for an X509 certificate. 更多...

头: #include <QSslCertificate>
CMake: find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)
qmake: QT += network

注意: 此类的所有函数 可重入 .

公共类型

enum class PatternSyntax { RegularExpression, Wildcard, FixedString }
enum SubjectInfo { Organization, CommonName, LocalityName, OrganizationalUnitName, CountryName, …, EmailAddress }

公共函数

QSslCertificate (QIODevice * device , QSsl::EncodingFormat format = QSsl::Pem)
QSslCertificate (const QByteArray & data = QByteArray(), QSsl::EncodingFormat format = QSsl::Pem)
QSslCertificate (const QSslCertificate & other )
~QSslCertificate ()
void clear ()
QByteArray digest (QCryptographicHash::Algorithm algorithm = QCryptographicHash::Md5) const
QDateTime effectiveDate () const
QDateTime expiryDate () const
QList<QSslCertificateExtension> extensions () const
Qt::HANDLE handle () const
bool isBlacklisted () const
bool isNull () const
bool isSelfSigned () const
QString issuerDisplayName () const
QStringList issuerInfo (QSslCertificate::SubjectInfo subject ) const
QStringList issuerInfo (const QByteArray & 属性 ) const
QList<QByteArray> issuerInfoAttributes () const
QSslKey publicKey () const
QByteArray serialNumber () const
QMultiMap<QSsl::AlternativeNameEntryType, QString> subjectAlternativeNames () const
QString subjectDisplayName () const
QStringList subjectInfo (QSslCertificate::SubjectInfo subject ) const
QStringList subjectInfo (const QByteArray & 属性 ) const
QList<QByteArray> subjectInfoAttributes () const
void swap (QSslCertificate & other )
QByteArray toDer () const
QByteArray toPem () const
QString toText () const
QByteArray version () const
bool operator!= (const QSslCertificate & other ) const
QSslCertificate & operator= (const QSslCertificate & other )
bool operator== (const QSslCertificate & other ) const

静态公共成员

QList<QSslCertificate> fromData (const QByteArray & data , QSsl::EncodingFormat format = QSsl::Pem)
QList<QSslCertificate> fromDevice (QIODevice * device , QSsl::EncodingFormat format = QSsl::Pem)
QList<QSslCertificate> fromPath (const QString & path , QSsl::EncodingFormat format = QSsl::Pem, QSslCertificate::PatternSyntax syntax = PatternSyntax::FixedString)
bool importPkcs12 (QIODevice * device , QSslKey * key , QSslCertificate * certificate , QList<QSslCertificate> * caCertificates = nullptr, const QByteArray & passPhrase = QByteArray())
QList<QSslError> verify (const QList<QSslCertificate> & certificateChain , const QString & hostName = QString())

详细描述

QSslCertificate stores an X509 certificate, and is commonly used to verify the identity and store information about the local host, a remotely connected peer, or a trusted third party Certificate Authority.

There are many ways to construct a QSslCertificate. The most common way is to call QSslSocket::peerCertificate (), which returns a QSslCertificate object, or QSslSocket::peerCertificateChain (), which returns a list of them. You can also load certificates from a DER (binary) or PEM (Base64) encoded bundle, typically stored as one or more local files, or in a Qt Resource.

可以调用 isNull () to check if your certificate is null. By default, QSslCertificate constructs a null certificate. A null certificate is invalid, but an invalid certificate is not necessarily null. If you want to reset all contents in a certificate, call clear ().

After loading a certificate, you can find information about the certificate, its subject, and its issuer, by calling one of the many accessor functions, including version (), serialNumber (), issuerInfo () 和 subjectInfo (). You can call effectiveDate () 和 expiryDate () to check when the certificate starts being effective and when it expires. The publicKey () function returns the certificate subject's public key as a QSslKey 。可以调用 issuerInfo () 或 subjectInfo () to get detailed information about the certificate issuer and its subject.

Internally, QSslCertificate is stored as an X509 structure. You can access this handle by calling handle (), but the results are likely to not be portable.

另请参阅 QSslSocket , QSslKey , QSslCipher ,和 QSslError .

成员类型文档编制

enum class QSslCertificate:: PatternSyntax

The syntax used to interpret the meaning of the pattern.

常量 描述
QSslCertificate::PatternSyntax::RegularExpression 0 A rich Perl-like pattern matching syntax.
QSslCertificate::PatternSyntax::Wildcard 1 This provides a simple pattern matching syntax similar to that used by shells (command interpreters) for "file globbing". See QRegularExpression::fromWildcard ().
QSslCertificate::PatternSyntax::FixedString 2 The pattern is a fixed string. This is equivalent to using the RegularExpression pattern on a string in which all metacharacters are escaped using escape(). This is the default.

enum QSslCertificate:: SubjectInfo

Describes keys that you can pass to QSslCertificate::issuerInfo () 或 QSslCertificate::subjectInfo () to get information about the certificate issuer or subject.

常量 描述
QSslCertificate::Organization 0 "O" The name of the organization.
QSslCertificate::CommonName 1 "CN" The common name; most often this is used to store the host name.
QSslCertificate::LocalityName 2 "L" The locality.
QSslCertificate::OrganizationalUnitName 3 "OU" The organizational unit name.
QSslCertificate::CountryName 4 "C" The country.
QSslCertificate::StateOrProvinceName 5 "ST" The state or province.
QSslCertificate::DistinguishedNameQualifier 6 The distinguished name qualifier
QSslCertificate::SerialNumber 7 The certificate's serial number
QSslCertificate::EmailAddress 8 The email address associated with the certificate

成员函数文档编制

[explicit] QSslCertificate:: QSslCertificate ( QIODevice * device , QSsl::EncodingFormat format = QSsl::Pem)

Constructs a QSslCertificate by reading format encoded data from device and using the first certificate found. You can later call isNull () to see if device contained a certificate, and if this certificate was loaded successfully.

[explicit] QSslCertificate:: QSslCertificate (const QByteArray & data = QByteArray(), QSsl::EncodingFormat format = QSsl::Pem)

Constructs a QSslCertificate by parsing the format encoded data and using the first available certificate found. You can later call isNull () to see if data contained a certificate, and if this certificate was loaded successfully.

QSslCertificate:: QSslCertificate (const QSslCertificate & other )

Constructs an identical copy of other .

QSslCertificate:: ~QSslCertificate ()

销毁 QSslCertificate .

void QSslCertificate:: clear ()

Clears the contents of this certificate, making it a null certificate.

另请参阅 isNull ().

QByteArray QSslCertificate:: digest ( QCryptographicHash::Algorithm algorithm = QCryptographicHash::Md5) const

Returns a cryptographic digest of this certificate. By default, an MD5 digest will be generated, but you can also specify a custom algorithm .

QDateTime QSslCertificate:: effectiveDate () const

Returns the date-time that the certificate becomes valid, or an empty QDateTime if this is a null certificate.

另请参阅 expiryDate ().

QDateTime QSslCertificate:: expiryDate () const

Returns the date-time that the certificate expires, or an empty QDateTime if this is a null certificate.

另请参阅 effectiveDate ().

QList < QSslCertificateExtension > QSslCertificate:: extensions () const

Returns a list containing the X509 extensions of this certificate.

[static] QList < QSslCertificate > QSslCertificate:: fromData (const QByteArray & data , QSsl::EncodingFormat format = QSsl::Pem)

Searches for and parses all certificates in data that are encoded in the specified format and returns them in a list of certificates.

另请参阅 fromDevice ().

[static] QList < QSslCertificate > QSslCertificate:: fromDevice ( QIODevice * device , QSsl::EncodingFormat format = QSsl::Pem)

Searches for and parses all certificates in device that are encoded in the specified format and returns them in a list of certificates.

另请参阅 fromData ().

[static] QList < QSslCertificate > QSslCertificate:: fromPath (const QString & path , QSsl::EncodingFormat format = QSsl::Pem, QSslCertificate::PatternSyntax syntax = PatternSyntax::FixedString)

Searches all files in the path for certificates encoded in the specified format and returns them in a list. path must be a file or a pattern matching one or more files, as specified by syntax .

范例:

const auto certs = QSslCertificate::fromPath("C:/ssl/certificate.*.pem",
                                             QSsl::Pem, QSslCertificate::Wildcard);
for (const QSslCertificate &cert : certs) {
    qDebug() << cert.issuerInfo(QSslCertificate::Organization);
}
					

另请参阅 fromData ().

Qt::HANDLE QSslCertificate:: handle () const

Returns a pointer to the native certificate handle, if there is one, else nullptr .

You can use this handle, together with the native API, to access extended information about the certificate.

警告: Use of this function has a high probability of being non-portable, and its return value may vary from platform to platform or change from minor release to minor release.

[static] bool QSslCertificate:: importPkcs12 ( QIODevice * device , QSslKey * key , QSslCertificate * certificate , QList < QSslCertificate > * caCertificates = nullptr, const QByteArray & passPhrase = QByteArray())

Imports a PKCS#12 (pfx) file from the specified device . A PKCS#12 file is a bundle that can contain a number of certificates and keys. This method reads a single key , its certificate and any associated caCertificates from the bundle. If a passPhrase is specified then this will be used to decrypt the bundle. Returns true if the PKCS#12 file was successfully loaded.

注意: The device must be open and ready to be read from.

bool QSslCertificate:: isBlacklisted () const

返回 true if this certificate is blacklisted; otherwise returns false .

另请参阅 isNull ().

bool QSslCertificate:: isNull () const

返回 true if this is a null certificate (i.e., a certificate with no contents); otherwise returns false .

默认情况下, QSslCertificate constructs a null certificate.

另请参阅 clear ().

bool QSslCertificate:: isSelfSigned () const

返回 true 若此证书自签名;否则返回 false .

A certificate is considered self-signed its issuer and subject are identical.

QString QSslCertificate:: issuerDisplayName () const

Returns a name that describes the issuer. It returns the QSslCertificate::CommonName if available, otherwise falls back to the first QSslCertificate::Organization or the first QSslCertificate::OrganizationalUnitName .

另请参阅 issuerInfo ().

QStringList QSslCertificate:: issuerInfo ( QSslCertificate::SubjectInfo subject ) const

Returns the issuer information for the subject from the certificate, or an empty list if there is no information for subject in the certificate. There can be more than one entry of each type.

另请参阅 subjectInfo ().

QStringList QSslCertificate:: issuerInfo (const QByteArray & 属性 ) const

Returns the issuer information for attribute from the certificate, or an empty list if there is no information for attribute in the certificate. There can be more than one entry for an attribute.

另请参阅 subjectInfo ().

QList < QByteArray > QSslCertificate:: issuerInfoAttributes () const

Returns a list of the attributes that have values in the issuer information of this certificate. The information associated with a given attribute can be accessed using the issuerInfo () method. Note that this list may include the OIDs for any elements that are not known by the SSL backend.

另请参阅 subjectInfo ().

QSslKey QSslCertificate:: publicKey () const

Returns the certificate subject's public key.

QByteArray QSslCertificate:: serialNumber () const

Returns the certificate's serial number string in hexadecimal format.

QMultiMap < QSsl::AlternativeNameEntryType , QString > QSslCertificate:: subjectAlternativeNames () const

Returns the list of alternative subject names for this certificate. The alternative names typically contain host names, optionally with wildcards, that are valid for this certificate.

These names are tested against the connected peer's host name, if either the subject information for CommonName doesn't define a valid host name, or the subject info name doesn't match the peer's host name.

另请参阅 subjectInfo ().

QString QSslCertificate:: subjectDisplayName () const

Returns a name that describes the subject. It returns the QSslCertificate::CommonName if available, otherwise falls back to the first QSslCertificate::Organization or the first QSslCertificate::OrganizationalUnitName .

另请参阅 subjectInfo ().

QStringList QSslCertificate:: subjectInfo ( QSslCertificate::SubjectInfo subject ) const

Returns the information for the subject , or an empty list if there is no information for subject in the certificate. There can be more than one entry of each type.

另请参阅 issuerInfo ().

QStringList QSslCertificate:: subjectInfo (const QByteArray & 属性 ) const

Returns the subject information for attribute , or an empty list if there is no information for attribute in the certificate. There can be more than one entry for an attribute.

另请参阅 issuerInfo ().

QList < QByteArray > QSslCertificate:: subjectInfoAttributes () const

Returns a list of the attributes that have values in the subject information of this certificate. The information associated with a given attribute can be accessed using the subjectInfo () method. Note that this list may include the OIDs for any elements that are not known by the SSL backend.

另请参阅 subjectInfo ().

void QSslCertificate:: swap ( QSslCertificate & other )

交换此证书实例与 other 。此函数非常快且从不失败。

QByteArray QSslCertificate:: toDer () const

Returns this certificate converted to a DER (binary) encoded representation.

QByteArray QSslCertificate:: toPem () const

Returns this certificate converted to a PEM (Base64) encoded representation.

QString QSslCertificate:: toText () const

Returns this certificate converted to a human-readable text representation.

[static] QList < QSslError > QSslCertificate:: verify (const QList < QSslCertificate > & certificateChain , const QString & hostName = QString())

Verifies a certificate chain. The chain to be verified is passed in the certificateChain parameter. The first certificate in the list should be the leaf certificate of the chain to be verified. If hostName is specified then the certificate is also checked to see if it is valid for the specified host name.

Note that the root (CA) certificate should not be included in the list to be verified, this will be looked up automatically using the CA list specified in the default QSslConfiguration , and, in addition, if possible, CA certificates loaded on demand on Unix and Windows.

QByteArray QSslCertificate:: version () const

返回证书的版本字符串。

bool QSslCertificate:: operator!= (const QSslCertificate & other ) const

返回 true 若此证书不同于 other ;否则返回 false .

QSslCertificate &QSslCertificate:: operator= (const QSslCertificate & other )

Copies the contents of other into this certificate, making the two certificates identical.

bool QSslCertificate:: operator== (const QSslCertificate & other ) const

返回 true 若此证书如同 other ;否则返回 false .