TlsCertificate#
Added in version 2.28.
Superclasses: Object
A certificate used for TLS authentication and encryption.
This can represent either a certificate only (eg, the certificate
received by a client from a server), or the combination of
a certificate and a private key (which is needed when acting as a
TlsServerConnection).
Constructors#
- class TlsCertificate
- classmethod new_from_file(file: str) TlsCertificate#
Creates a
TlsCertificatefrom the data infile.As of 2.72, if the filename ends in
.p12or.pfxthe data is loaded bynew_from_pkcs12()otherwise it is loaded bynew_from_pem(). See those functions for exact details.If
filecannot be read or parsed, the function will returnNoneand seterror.Added in version 2.28.
- Parameters:
file – file containing a certificate to import
- classmethod new_from_file_with_password(file: str, password: str) TlsCertificate#
Creates a
TlsCertificatefrom the data infile.If
filecannot be read or parsed, the function will returnNoneand seterror.Any unknown file types will error with
NOT_SUPPORTED. Currently only.p12and.pfxfiles are supported. Seenew_from_pkcs12()for more details.Added in version 2.72.
- Parameters:
file – file containing a certificate to import
password – password for PKCS
12files
- classmethod new_from_files(cert_file: str, key_file: str) TlsCertificate#
Creates a
TlsCertificatefrom the PEM-encoded data incert_fileandkey_file. The returned certificate will be the first certificate found incert_file. As of GLib 2.44, ifcert_filecontains more certificates it will try to load a certificate chain. All certificates will be verified in the order found (top-level certificate should be the last one in the file) and theTlsCertificate:issuer property of each certificate will be set accordingly if the verification succeeds. If any certificate in the chain cannot be verified, the first certificate in the file will still be returned.If either file cannot be read or parsed, the function will return
Noneand seterror. Otherwise, this behaves likenew_from_pem().Added in version 2.28.
- Parameters:
cert_file – file containing one or more PEM-encoded certificates to import
key_file – file containing a PEM-encoded private key to import
- classmethod new_from_pem(data: str, length: int) TlsCertificate#
Creates a
TlsCertificatefrom the PEM-encoded data indata. Ifdataincludes both a certificate and a private key, then the returned certificate will include the private key data as well. (See theTlsCertificate:private-key-pem property for information about supported formats.)The returned certificate will be the first certificate found in
data. As of GLib 2.44, ifdatacontains more certificates it will try to load a certificate chain. All certificates will be verified in the order found (top-level certificate should be the last one in the file) and theTlsCertificate:issuer property of each certificate will be set accordingly if the verification succeeds. If any certificate in the chain cannot be verified, the first certificate in the file will still be returned.Added in version 2.28.
- Parameters:
data – PEM-encoded certificate data
length – the length of
data, or -1 if it’s 0-terminated.
- classmethod new_from_pkcs11_uris(pkcs11_uri: str, private_key_pkcs11_uri: str | None = None) TlsCertificate#
Creates a
TlsCertificatefrom a PKCS #11 URI.An example
pkcs11_uriwould bepkcs11:model=Model;manufacturer=Manufacture;serial=1;token=My%20Client%20Certificate;id=%01Where the token’s layout is:
Object 0: URL: pkcs11:model=Model;manufacturer=Manufacture;serial=1;token=My%20Client%20Certificate;id=%01;object=private%20key;type=private Type: Private key (RSA-2048) ID: 01 Object 1: URL: pkcs11:model=Model;manufacturer=Manufacture;serial=1;token=My%20Client%20Certificate;id=%01;object=Certificate%20for%20Authentication;type=cert Type: X.509 Certificate (RSA-2048) ID: 01
In this case the certificate and private key would both be detected and used as expected.
pkcs_urimay also just reference an X.509 certificate object and then optionallyprivate_key_pkcs11_uriallows using a private key exposed under a different URI.Note that the private key is not accessed until usage and may fail or require a PIN later.
Added in version 2.68.
- Parameters:
pkcs11_uri – A PKCS #11 URI
private_key_pkcs11_uri – A PKCS #11 URI
- classmethod new_from_pkcs12(data: list[int], password: str | None = None) TlsCertificate#
Creates a
TlsCertificatefrom the data indata. It must contain a certificate and matching private key.If extra certificates are included they will be verified as a chain and the
TlsCertificate:issuer property will be set. All other data will be ignored.You can pass as single password for all of the data which will be used both for the PKCS
12container as well as encrypted private keys. If decryption fails it will error withBAD_CERTIFICATE_PASSWORD.This constructor requires support in the current
TlsBackend. If support is missing it will error withNOT_SUPPORTED.Other parsing failures will error with
BAD_CERTIFICATE.Added in version 2.72.
- Parameters:
data – DER-encoded PKCS
12format certificate datapassword – optional password for encrypted certificate data
Methods#
- class TlsCertificate
- do_verify(self, identity: SocketConnectable | None = None, trusted_ca: TlsCertificate | None = None) TlsCertificateFlags#
- Parameters:
identity
trusted_ca
- get_dns_names() list[Bytes] | None#
Gets the value of
TlsCertificate:dns-names.Added in version 2.70.
- get_ip_addresses() list[InetAddress] | None#
Gets the value of
TlsCertificate:ip-addresses.Added in version 2.70.
- get_issuer() TlsCertificate | None#
Gets the
TlsCertificaterepresentingcert's issuer, if knownAdded in version 2.28.
- get_not_valid_after() DateTime | None#
Returns the time at which the certificate became or will become invalid.
Added in version 2.70.
- get_not_valid_before() DateTime | None#
Returns the time at which the certificate became or will become valid.
Added in version 2.70.
- get_subject_name() str | None#
Returns the subject name from the certificate.
Added in version 2.70.
- is_same(cert_two: TlsCertificate) bool#
Check if two
TlsCertificateobjects represent the same certificate. The raw DER byte data of the two certificates are checked for equality. This has the effect that two certificates may compare equal even if theirTlsCertificate:issuer,TlsCertificate:private-key, orTlsCertificate:private-key-pem properties differ.Added in version 2.34.
- Parameters:
cert_two – second certificate to compare
- classmethod list_new_from_file() list[TlsCertificate]#
Creates one or more
TlsCertificatefrom the PEM-encoded data infile. Iffilecannot be read or parsed, the function will returnNoneand seterror. Iffiledoes not contain any PEM-encoded certificates, this will return an empty list and not seterror.Added in version 2.28.
- verify(identity: SocketConnectable | None = None, trusted_ca: TlsCertificate | None = None) TlsCertificateFlags#
This verifies
certand returns a set ofTlsCertificateFlagsindicating any problems found with it. This can be used to verify a certificate outside the context of making a connection, or to check a certificate against a CA that is not part of the system CA database.If
certis valid,NO_FLAGSis returned.If
identityis notNone,cert's name(s) will be compared against it, andBAD_IDENTITYwill be set in the return value if it does not match. IfidentityisNone, that bit will never be set in the return value.If
trusted_cais notNone, thencert(or one of the certificates in its chain) must be signed by it, or elseUNKNOWN_CAwill be set in the return value. Iftrusted_caisNone, that bit will never be set in the return value.GLib guarantees that if certificate verification fails, at least one error will be set in the return value, but it does not guarantee that all possible errors will be set. Accordingly, you may not safely decide to ignore any particular type of error. For example, it would be incorrect to mask
EXPIREDif you want to allow expired certificates, because this could potentially be the only error flag set even if other problems exist with the certificate.Because TLS session context is not used,
TlsCertificatemay not perform as many checks on the certificates asTlsConnectionwould. For example, certificate constraints may not be honored, and revocation checks may not be performed. The best way to verify TLS certificates used by a TLS connection is to letTlsConnectionhandle the verification.Added in version 2.28.
- Parameters:
identity – the expected peer identity
trusted_ca – the certificate of a trusted authority
Properties#
- class TlsCertificate
-
- props.issuer: TlsCertificate#
The type of the None singleton.
Added in version 2.28.
Virtual Methods#
- class TlsCertificate
- do_verify(identity: SocketConnectable | None = None, trusted_ca: TlsCertificate | None = None) TlsCertificateFlags#
This verifies
certand returns a set ofTlsCertificateFlagsindicating any problems found with it. This can be used to verify a certificate outside the context of making a connection, or to check a certificate against a CA that is not part of the system CA database.If
certis valid,NO_FLAGSis returned.If
identityis notNone,cert's name(s) will be compared against it, andBAD_IDENTITYwill be set in the return value if it does not match. IfidentityisNone, that bit will never be set in the return value.If
trusted_cais notNone, thencert(or one of the certificates in its chain) must be signed by it, or elseUNKNOWN_CAwill be set in the return value. Iftrusted_caisNone, that bit will never be set in the return value.GLib guarantees that if certificate verification fails, at least one error will be set in the return value, but it does not guarantee that all possible errors will be set. Accordingly, you may not safely decide to ignore any particular type of error. For example, it would be incorrect to mask
EXPIREDif you want to allow expired certificates, because this could potentially be the only error flag set even if other problems exist with the certificate.Because TLS session context is not used,
TlsCertificatemay not perform as many checks on the certificates asTlsConnectionwould. For example, certificate constraints may not be honored, and revocation checks may not be performed. The best way to verify TLS certificates used by a TLS connection is to letTlsConnectionhandle the verification.Added in version 2.28.
- Parameters:
identity – the expected peer identity
trusted_ca – the certificate of a trusted authority