Gio.TlsCertificate

g GObject.Object GObject.Object Gio.TlsCertificate Gio.TlsCertificate GObject.Object->Gio.TlsCertificate

Subclasses:

None

Methods

Inherited:

GObject.Object (37)

Structs:

GObject.ObjectClass (5)

class

list_new_from_file (file)

class

new_from_file (file)

class

new_from_file_with_password (file, password)

class

new_from_files (cert_file, key_file)

class

new_from_pem (data, length)

class

new_from_pkcs11_uris (pkcs11_uri, private_key_pkcs11_uri)

class

new_from_pkcs12 (data, password)

get_dns_names ()

get_ip_addresses ()

get_issuer ()

get_issuer_name ()

get_not_valid_after ()

get_not_valid_before ()

get_subject_name ()

is_same (cert_two)

verify (identity, trusted_ca)

Virtual Methods

Inherited:

GObject.Object (7)

do_verify (identity, trusted_ca)

Properties

Name

Type

Flags

Short Description

certificate

GLib.ByteArray

r/w/co

The DER representation of the certificate

certificate-pem

str

r/w/co

The PEM representation of the certificate

dns-names

GLib.PtrArray

r

DNS Names listed on the cert.

ip-addresses

GLib.PtrArray

r

IP Addresses listed on the cert.

issuer

Gio.TlsCertificate

r/w/co

The certificate for the issuing entity

issuer-name

str

r

The issuer from the certificate.

not-valid-after

GLib.DateTime

r

Cert should not be considered valid after this time.

not-valid-before

GLib.DateTime

r

Cert should not be considered valid before this time.

password

str

w/co

Password used when constructing from bytes

pkcs11-uri

str

r/w/co

The PKCS #11 URI

pkcs12-data

GLib.ByteArray

w/co

The PKCS #12 data used for construction

private-key

GLib.ByteArray

r/w/co

The DER representation of the certificate’s private key

private-key-pem

str

r/w/co

The PEM representation of the certificate’s private key

private-key-pkcs11-uri

str

r/w/co

The PKCS #11 URI for a private key

subject-name

str

r

The subject name from the certificate.

Signals

Inherited:

GObject.Object (1)

Fields

Inherited:

GObject.Object (1)

Name

Type

Access

Description

parent_instance

GObject.Object

r

Class Details

class Gio.TlsCertificate(**kwargs)
Bases:

GObject.Object

Abstract:

Yes

Structure:

Gio.TlsCertificateClass

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 Gio.TlsServerConnection).

New in version 2.28.

classmethod list_new_from_file(file)[source]
Parameters:

file (str) – file containing PEM-encoded certificates to import

Raises:

GLib.Error

Returns:

a GLib.List containing Gio.TlsCertificate objects. You must free the list and its contents when you are done with it.

Return type:

[Gio.TlsCertificate]

Creates one or more Gio.TlsCertificates from the PEM-encoded data in file. If file cannot be read or parsed, the function will return None and set error. If file does not contain any PEM-encoded certificates, this will return an empty list and not set error.

New in version 2.28.

classmethod new_from_file(file)[source]
Parameters:

file (str) – file containing a certificate to import

Raises:

GLib.Error

Returns:

the new certificate, or None on error

Return type:

Gio.TlsCertificate

Creates a Gio.TlsCertificate from the data in file.

As of 2.72, if the filename ends in .p12 or .pfx the data is loaded by Gio.TlsCertificate.new_from_pkcs12() otherwise it is loaded by Gio.TlsCertificate.new_from_pem(). See those functions for exact details.

If file cannot be read or parsed, the function will return None and set error.

New in version 2.28.

classmethod new_from_file_with_password(file, password)[source]
Parameters:
  • file (str) – file containing a certificate to import

  • password (str) – password for PKCS #12 files

Raises:

GLib.Error

Returns:

the new certificate, or None on error

Return type:

Gio.TlsCertificate

Creates a Gio.TlsCertificate from the data in file.

If file cannot be read or parsed, the function will return None and set error.

Any unknown file types will error with Gio.IOErrorEnum.NOT_SUPPORTED. Currently only .p12 and .pfx files are supported. See Gio.TlsCertificate.new_from_pkcs12() for more details.

New in version 2.72.

classmethod new_from_files(cert_file, key_file)[source]
Parameters:
  • cert_file (str) – file containing one or more PEM-encoded certificates to import

  • key_file (str) – file containing a PEM-encoded private key to import

Raises:

GLib.Error

Returns:

the new certificate, or None on error

Return type:

Gio.TlsCertificate

Creates a Gio.TlsCertificate from the PEM-encoded data in cert_file and key_file. The returned certificate will be the first certificate found in cert_file. As of GLib 2.44, if cert_file contains 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 the Gio.TlsCertificate :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 None and set error. Otherwise, this behaves like Gio.TlsCertificate.new_from_pem().

New in version 2.28.

classmethod new_from_pem(data, length)[source]
Parameters:
  • data (str) – PEM-encoded certificate data

  • length (int) – the length of data, or -1 if it’s 0-terminated.

Raises:

GLib.Error

Returns:

the new certificate, or None if data is invalid

Return type:

Gio.TlsCertificate

Creates a Gio.TlsCertificate from the PEM-encoded data in data. If data includes both a certificate and a private key, then the returned certificate will include the private key data as well. (See the Gio.TlsCertificate :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, if data contains 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 the Gio.TlsCertificate :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.

New in version 2.28.

classmethod new_from_pkcs11_uris(pkcs11_uri, private_key_pkcs11_uri)[source]
Parameters:
  • pkcs11_uri (str) – A PKCS \#11 URI

  • private_key_pkcs11_uri (str or None) – A PKCS \#11 URI

Raises:

GLib.Error

Returns:

the new certificate, or None on error

Return type:

Gio.TlsCertificate

Creates a Gio.TlsCertificate from a PKCS #11 URI.

An example pkcs11_uri would be pkcs11:model=Model;manufacturer=Manufacture;serial=1;token=My%20Client%20Certificate;id=%01

Where 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_uri may also just reference an X.509 certificate object and then optionally private_key_pkcs11_uri allows 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.

New in version 2.68.

classmethod new_from_pkcs12(data, password)[source]
Parameters:
  • data (bytes) – DER-encoded PKCS #12 format certificate data

  • password (str or None) – optional password for encrypted certificate data

Raises:

GLib.Error

Returns:

the new certificate, or None if data is invalid

Return type:

Gio.TlsCertificate

Creates a Gio.TlsCertificate from the data in data. It must contain a certificate and matching private key.

If extra certificates are included they will be verified as a chain and the Gio.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 #12 container as well as encrypted private keys. If decryption fails it will error with Gio.TlsError.BAD_CERTIFICATE_PASSWORD.

This constructor requires support in the current Gio.TlsBackend. If support is missing it will error with Gio.IOErrorEnum.NOT_SUPPORTED.

Other parsing failures will error with Gio.TlsError.BAD_CERTIFICATE.

New in version 2.72.

get_dns_names()[source]
Returns:

A GLib.PtrArray of GLib.Bytes elements, or None if it’s not available.

Return type:

[GLib.Bytes] or None

Gets the value of Gio.TlsCertificate :dns-names.

New in version 2.70.

get_ip_addresses()[source]
Returns:

A GLib.PtrArray of Gio.InetAddress elements, or None if it’s not available.

Return type:

[Gio.InetAddress] or None

Gets the value of Gio.TlsCertificate :ip-addresses.

New in version 2.70.

get_issuer()[source]
Returns:

The certificate of self's issuer, or None if self is self-signed or signed with an unknown certificate.

Return type:

Gio.TlsCertificate or None

Gets the Gio.TlsCertificate representing self's issuer, if known

New in version 2.28.

get_issuer_name()[source]
Returns:

The issuer name, or None if it’s not available.

Return type:

str or None

Returns the issuer name from the certificate.

New in version 2.70.

get_not_valid_after()[source]
Returns:

The not-valid-after date, or None if it’s not available.

Return type:

GLib.DateTime or None

Returns the time at which the certificate became or will become invalid.

New in version 2.70.

get_not_valid_before()[source]
Returns:

The not-valid-before date, or None if it’s not available.

Return type:

GLib.DateTime or None

Returns the time at which the certificate became or will become valid.

New in version 2.70.

get_subject_name()[source]
Returns:

The subject name, or None if it’s not available.

Return type:

str or None

Returns the subject name from the certificate.

New in version 2.70.

is_same(cert_two)[source]
Parameters:

cert_two (Gio.TlsCertificate) – second certificate to compare

Returns:

whether the same or not

Return type:

bool

Check if two Gio.TlsCertificate objects 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 their Gio.TlsCertificate :issuer, Gio.TlsCertificate :private-key, or Gio.TlsCertificate :private-key-pem properties differ.

New in version 2.34.

verify(identity, trusted_ca)[source]
Parameters:
Returns:

the appropriate Gio.TlsCertificateFlags

Return type:

Gio.TlsCertificateFlags

This verifies self and returns a set of Gio.TlsCertificateFlags indicating 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 self is valid, Gio.TlsCertificateFlags.NO_FLAGS is returned.

If identity is not None, self's name(s) will be compared against it, and Gio.TlsCertificateFlags.BAD_IDENTITY will be set in the return value if it does not match. If identity is None, that bit will never be set in the return value.

If trusted_ca is not None, then self (or one of the certificates in its chain) must be signed by it, or else Gio.TlsCertificateFlags.UNKNOWN_CA will be set in the return value. If trusted_ca is None, 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 Gio.TlsCertificateFlags.EXPIRED if 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, Gio.TlsCertificate may not perform as many checks on the certificates as Gio.TlsConnection would. 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 let Gio.TlsConnection handle the verification.

New in version 2.28.

do_verify(identity, trusted_ca) virtual
Parameters:
Returns:

the appropriate Gio.TlsCertificateFlags

Return type:

Gio.TlsCertificateFlags

This verifies cert and returns a set of Gio.TlsCertificateFlags indicating 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 cert is valid, Gio.TlsCertificateFlags.NO_FLAGS is returned.

If identity is not None, cert's name(s) will be compared against it, and Gio.TlsCertificateFlags.BAD_IDENTITY will be set in the return value if it does not match. If identity is None, that bit will never be set in the return value.

If trusted_ca is not None, then cert (or one of the certificates in its chain) must be signed by it, or else Gio.TlsCertificateFlags.UNKNOWN_CA will be set in the return value. If trusted_ca is None, 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 Gio.TlsCertificateFlags.EXPIRED if 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, Gio.TlsCertificate may not perform as many checks on the certificates as Gio.TlsConnection would. 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 let Gio.TlsConnection handle the verification.

New in version 2.28.

Property Details

Gio.TlsCertificate.props.certificate
Name:

certificate

Type:

GLib.ByteArray

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

The DER (binary) encoded representation of the certificate. This property and the Gio.TlsCertificate :certificate-pem property represent the same data, just in different forms.

New in version 2.28.

Gio.TlsCertificate.props.certificate_pem
Name:

certificate-pem

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

The PEM (ASCII) encoded representation of the certificate. This property and the Gio.TlsCertificate :certificate property represent the same data, just in different forms.

New in version 2.28.

Gio.TlsCertificate.props.dns_names
Name:

dns-names

Type:

GLib.PtrArray

Default Value:

None

Flags:

READABLE

The DNS names from the certificate’s Subject Alternative Names (SANs), None if unavailable.

New in version 2.70.

Gio.TlsCertificate.props.ip_addresses
Name:

ip-addresses

Type:

GLib.PtrArray

Default Value:

None

Flags:

READABLE

The IP addresses from the certificate’s Subject Alternative Names (SANs), None if unavailable.

New in version 2.70.

Gio.TlsCertificate.props.issuer
Name:

issuer

Type:

Gio.TlsCertificate

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

A Gio.TlsCertificate representing the entity that issued this certificate. If None, this means that the certificate is either self-signed, or else the certificate of the issuer is not available.

Beware the issuer certificate may not be the same as the certificate that would actually be used to construct a valid certification path during certificate verification. RFC 4158 explains why an issuer certificate cannot be naively assumed to be part of the the certification path (though GLib’s TLS backends may not follow the path building strategies outlined in this RFC). Due to the complexity of certification path building, GLib does not provide any way to know which certification path will actually be used. Accordingly, this property cannot be used to make security-related decisions. Only GLib itself should make security decisions about TLS certificates.

New in version 2.28.

Gio.TlsCertificate.props.issuer_name
Name:

issuer-name

Type:

str

Default Value:

None

Flags:

READABLE

The issuer from the certificate, None if unavailable.

New in version 2.70.

Gio.TlsCertificate.props.not_valid_after
Name:

not-valid-after

Type:

GLib.DateTime

Default Value:

None

Flags:

READABLE

The time at which this cert is no longer valid, None if unavailable.

New in version 2.70.

Gio.TlsCertificate.props.not_valid_before
Name:

not-valid-before

Type:

GLib.DateTime

Default Value:

None

Flags:

READABLE

The time at which this cert is considered to be valid, None if unavailable.

New in version 2.70.

Gio.TlsCertificate.props.password
Name:

password

Type:

str

Default Value:

None

Flags:

WRITABLE, CONSTRUCT_ONLY

An optional password used when constructed with Gio.TlsCertificate :pkcs12-data.

New in version 2.72.

Gio.TlsCertificate.props.pkcs11_uri
Name:

pkcs11-uri

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

A URI referencing the PKCS #11 objects containing an X.509 certificate and optionally a private key.

If None, the certificate is either not backed by PKCS \#11 or the Gio.TlsBackend does not support PKCS \#11.

New in version 2.68.

Gio.TlsCertificate.props.pkcs12_data
Name:

pkcs12-data

Type:

GLib.ByteArray

Default Value:

None

Flags:

WRITABLE, CONSTRUCT_ONLY

The PKCS #12 formatted data used to construct the object.

See also: Gio.TlsCertificate.new_from_pkcs12()

New in version 2.72.

Gio.TlsCertificate.props.private_key
Name:

private-key

Type:

GLib.ByteArray

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

The DER (binary) encoded representation of the certificate’s private key, in either PKCS #1 format or unencrypted PKCS #8 format. PKCS \#8 format is supported since 2.32; earlier releases only support PKCS \#1. You can use the openssl rsa tool to convert PKCS \#8 keys to PKCS \#1.

This property (or the Gio.TlsCertificate :private-key-pem property) can be set when constructing a key (for example, from a file). Since GLib 2.70, it is now also readable; however, be aware that if the private key is backed by a PKCS \#11 URI – for example, if it is stored on a smartcard – then this property will be None. If so, the private key must be referenced via its PKCS \#11 URI, Gio.TlsCertificate :private-key-pkcs11-uri. You must check both properties to see if the certificate really has a private key. When this property is read, the output format will be unencrypted PKCS \#8.

New in version 2.28.

Gio.TlsCertificate.props.private_key_pem
Name:

private-key-pem

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

The PEM (ASCII) encoded representation of the certificate’s private key in either PKCS #1 format (”BEGIN RSA PRIVATE KEY”) or unencrypted PKCS #8 format (”BEGIN PRIVATE KEY”). PKCS \#8 format is supported since 2.32; earlier releases only support PKCS \#1. You can use the openssl rsa tool to convert PKCS \#8 keys to PKCS \#1.

This property (or the Gio.TlsCertificate :private-key property) can be set when constructing a key (for example, from a file). Since GLib 2.70, it is now also readable; however, be aware that if the private key is backed by a PKCS \#11 URI - for example, if it is stored on a smartcard - then this property will be None. If so, the private key must be referenced via its PKCS \#11 URI, Gio.TlsCertificate :private-key-pkcs11-uri. You must check both properties to see if the certificate really has a private key. When this property is read, the output format will be unencrypted PKCS \#8.

New in version 2.28.

Gio.TlsCertificate.props.private_key_pkcs11_uri
Name:

private-key-pkcs11-uri

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

A URI referencing a PKCS #11 object containing a private key.

New in version 2.68.

Gio.TlsCertificate.props.subject_name
Name:

subject-name

Type:

str

Default Value:

None

Flags:

READABLE

The subject from the cert, None if unavailable.

New in version 2.70.