1.94 <GTlsCertificate>

Class: <GTlsCertificate>

It derives from <GObject>.

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 <GTlsServerConnection>).

Fields

parent-instance
priv

Properties

Property: tls-certificate:certificate self

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

Its value is a <GByteArray>, or #f.

It can only be set when the object is made, with #:certificate.

Property: tls-certificate:certificate-pem self

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

Its value is a string, or #f.

It can only be set when the object is made, with #:certificate-pem.

Property: tls-certificate:dns-names self

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

Its value is a <GPtrArray>, or #f.

It is read only.

Property: tls-certificate:ip-addresses self

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

Its value is a <GPtrArray>, or #f.

It is read only.

Property: tls-certificate:issuer self

A <GTlsCertificate> representing the entity that issued this certificate. If #f, 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.

Its value is a <GTlsCertificate>, or #f.

It can only be set when the object is made, with #:issuer.

Property: tls-certificate:issuer-name self

The issuer from the certificate, #f if unavailable.

Its value is a string, or #f.

It is read only.

Property: tls-certificate:not-valid-after self

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

Its value is a <GDateTime>, or #f.

It is read only.

Property: tls-certificate:not-valid-before self

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

Its value is a <GDateTime>, or #f.

It is read only.

Property: tls-certificate:password self

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

Its value is a string, or #f.

It can only be set when the object is made, with #:password. It cannot be read.

Property: tls-certificate:pkcs11-uri self

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

If #f, the certificate is either not backed by PKCS \#11 or the <GTlsBackend> does not support PKCS \#11.

Its value is a string, or #f.

It can only be set when the object is made, with #:pkcs11-uri.

Property: tls-certificate:pkcs12-data self

The PKCS 12 formatted data used to construct the object.

See also: tls-certificate:new-from-pkcs12

Its value is a <GByteArray>, or #f.

It can only be set when the object is made, with #:pkcs12-data. It cannot be read.

Property: tls-certificate:private-key self

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 tls-certificate: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 #f. If so, the private key must be referenced via its PKCS \#11 URI, tls-certificate: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.

Its value is a <GByteArray>, or #f.

It can only be set when the object is made, with #:private-key.

Property: tls-certificate:private-key-pem self

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 tls-certificate: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 #f. If so, the private key must be referenced via its PKCS \#11 URI, tls-certificate: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.

Its value is a string, or #f.

It can only be set when the object is made, with #:private-key-pem.

Property: tls-certificate:private-key-pkcs11-uri self

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

Its value is a string, or #f.

It can only be set when the object is made, with #:private-key-pkcs11-uri.

Property: tls-certificate:subject-name self

The subject from the cert, #f if unavailable.

Its value is a string, or #f.

It is read only.

Procedures

Method: tls-certificate:get-dns-names self ⇒ %return
Method: get-dns-names self ⇒ %return

Gets the value of tls-certificate:dns-names.

self

Passed: a <GTlsCertificate>.

a <GTlsCertificate>

%return

Returned: a vector, each a <GBytes>, or #f.

A <GPtrArray> of <GBytes> elements, or #f if it’s not available.

Method: tls-certificate:get-ip-addresses self ⇒ %return
Method: get-ip-addresses self ⇒ %return

Gets the value of tls-certificate:ip-addresses.

self

Passed: a <GTlsCertificate>.

a <GTlsCertificate>

%return

Returned: a vector, each a <GInetAddress>, or #f.

A <GPtrArray> of <GInetAddress> elements, or #f if it’s not available.

Method: tls-certificate:get-issuer self ⇒ %return
Method: get-issuer self ⇒ %return

Gets the <GTlsCertificate> representing self’s issuer, if known

self

Passed: a <GTlsCertificate>.

a <GTlsCertificate>

%return

Returned: a <GTlsCertificate>, or #f.

The certificate of self’s issuer, or #f if self is self-signed or signed with an unknown certificate.

Method: tls-certificate:get-issuer-name self ⇒ %return
Method: get-issuer-name self ⇒ %return

Returns the issuer name from the certificate.

self

Passed: a <GTlsCertificate>.

a <GTlsCertificate>

%return

Returned: a string, or #f.

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

Method: tls-certificate:get-not-valid-after self ⇒ %return
Method: get-not-valid-after self ⇒ %return

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

self

Passed: a <GTlsCertificate>.

a <GTlsCertificate>

%return

Returned: a <GDateTime>, or #f.

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

Method: tls-certificate:get-not-valid-before self ⇒ %return
Method: get-not-valid-before self ⇒ %return

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

self

Passed: a <GTlsCertificate>.

a <GTlsCertificate>

%return

Returned: a <GDateTime>, or #f.

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

Method: tls-certificate:get-subject-name self ⇒ %return
Method: get-subject-name self ⇒ %return

Returns the subject name from the certificate.

self

Passed: a <GTlsCertificate>.

a <GTlsCertificate>

%return

Returned: a string, or #f.

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

Method: tls-certificate:is-same? self cert-two ⇒ %return
Method: is-same? self cert-two ⇒ %return

Check if two <GTlsCertificate> 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 tls-certificate:issuer, tls-certificate:private-key, or tls-certificate:private-key-pem properties differ.

self

Passed: a <GTlsCertificate>.

first certificate to compare

cert-two

Passed: a <GTlsCertificate>.

second certificate to compare

%return

Returned: a boolean.

whether the same or not

Procedure: tls-certificate:list-new-from-file file ⇒ %return

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

file

Passed: a file name, as a string.

file containing PEM-encoded certificates to import

%return

Returned: a list, each a <GTlsCertificate>.

a GList containing <GTlsCertificate> objects. You must free the list and its contents when you are done with it.

Procedure: tls-certificate:new-from-file file ⇒ %return

Creates a <GTlsCertificate> from the data in file.

As of 2.72, if the filename ends in .p12 or .pfx the data is loaded by tls-certificate:new-from-pkcs12 otherwise it is loaded by tls-certificate:new-from-pem. See those functions for exact details.

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

file

Passed: a file name, as a string.

file containing a certificate to import

%return

Returned: a <GTlsCertificate>.

the new certificate, or #f on error

Procedure: tls-certificate:new-from-file-with-password file password ⇒ %return

Creates a <GTlsCertificate> from the data in file.

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

Any unknown file types will error with not-supported. Currently only .p12 and .pfx files are supported. See tls-certificate:new-from-pkcs12 for more details.

file

Passed: a file name, as a string.

file containing a certificate to import

password

Passed: a string.

password for PKCS 12 files

%return

Returned: a <GTlsCertificate>.

the new certificate, or #f on error

Procedure: tls-certificate:new-from-files cert-file key-file ⇒ %return

Creates a <GTlsCertificate> 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 tls-certificate: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 #f and set error. Otherwise, this behaves like tls-certificate:new-from-pem.

cert-file

Passed: a file name, as a string.

file containing one or more PEM-encoded certificates to import

key-file

Passed: a file name, as a string.

file containing a PEM-encoded private key to import

%return

Returned: a <GTlsCertificate>.

the new certificate, or #f on error

Procedure: tls-certificate:new-from-pem data length ⇒ %return

Creates a <GTlsCertificate> 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 tls-certificate: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 tls-certificate: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.

data

Passed: a string.

PEM-encoded certificate data

length

Passed: a 64-bit integer.

the length of data, or -1 if it’s 0-terminated.

%return

Returned: a <GTlsCertificate>.

the new certificate, or #f if data is invalid

Procedure: tls-certificate:new-from-pkcs11-uris pkcs11-uri [private-key-pkcs11-uri] ⇒ %return

Creates a <GTlsCertificate> 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.

pkcs11-uri

Passed: a string.

A PKCS \#11 URI

private-key-pkcs11-uri

Passed, or left out: a string, or #f.

A PKCS \#11 URI

%return

Returned: a <GTlsCertificate>.

the new certificate, or #f on error

Procedure: tls-certificate:new-from-pkcs12 data [password] ⇒ %return

Creates a <GTlsCertificate> 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 tls-certificate: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 bad-certificate-password.

This constructor requires support in the current <GTlsBackend>. If support is missing it will error with not-supported.

Other parsing failures will error with bad-certificate.

data

Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer.

DER-encoded PKCS 12 format certificate data

password

Passed, or left out: a string, or #f.

optional password for encrypted certificate data

%return

Returned: a <GTlsCertificate>.

the new certificate, or #f if data is invalid

length

Not passed: guile-gi gives the length of data.

the length of data

Method: tls-certificate:verify self [identity] [trusted-ca] ⇒ %return
Method: verify self [identity] [trusted-ca] ⇒ %return

This verifies self and returns a set of <GTlsCertificateFlags> 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, no-flags is returned.

If identity is not #f, self’s name(s) will be compared against it, and bad-identity will be set in the return value if it does not match. If identity is #f, that bit will never be set in the return value.

If trusted-ca is not #f, then self (or one of the certificates in its chain) must be signed by it, or else unknown-ca will be set in the return value. If trusted-ca is #f, 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 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, <GTlsCertificate> may not perform as many checks on the certificates as <GTlsConnection> 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 <GTlsConnection> handle the verification.

self

Passed: a <GTlsCertificate>.

a <GTlsCertificate>

identity

Passed, or left out: a <GSocketConnectable>, or #f.

the expected peer identity

trusted-ca

Passed, or left out: a <GTlsCertificate>, or #f.

the certificate of a trusted authority

%return

Returned: a <GTlsCertificateFlags>.

the appropriate <GTlsCertificateFlags>

Virtual methods

Virtual method: tls-certificate:do-verify implementor self [identity] [trusted-ca] ⇒ %return

This verifies self and returns a set of <GTlsCertificateFlags> 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, no-flags is returned.

If identity is not #f, self’s name(s) will be compared against it, and bad-identity will be set in the return value if it does not match. If identity is #f, that bit will never be set in the return value.

If trusted-ca is not #f, then self (or one of the certificates in its chain) must be signed by it, or else unknown-ca will be set in the return value. If trusted-ca is #f, 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 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, <GTlsCertificate> may not perform as many checks on the certificates as <GTlsConnection> 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 <GTlsConnection> handle the verification.

To override it in a new class, give register-type #:vfuncs `((verify . ,procedure)). The procedure is called as (procedure self identity trusted-ca) ⇒ %return.

implementor

Passed: the class whose implementation to call: <GTlsCertificate>, or a class that derives from it, or its GType number.

self

Passed: a <GTlsCertificate>.

a <GTlsCertificate>

identity

Passed, or left out: a <GSocketConnectable>, or #f.

the expected peer identity

trusted-ca

Passed, or left out: a <GTlsCertificate>, or #f.

the certificate of a trusted authority

%return

Returned: a <GTlsCertificateFlags>.

the appropriate <GTlsCertificateFlags>