blob: ba84f6e7257d9448a44debd02917965b949a28e7 [file] [log] [blame]
Paul Kehrer016e08a2014-11-26 09:41:18 -10001.. hazmat::
2
3X.509
4=====
5
Paul Kehrera9d78c12014-11-26 10:59:03 -10006.. currentmodule:: cryptography.x509
Paul Kehrer016e08a2014-11-26 09:41:18 -10007
8X.509 is an ITU-T standard for a `public key infrastructure`_. X.509v3 is
Paul Kehrera68fd332014-11-27 07:08:40 -10009defined in :rfc:`5280` (which obsoletes :rfc:`2459` and :rfc:`3280`). X.509
10certificates are commonly used in protocols like `TLS`_.
Paul Kehrer016e08a2014-11-26 09:41:18 -100011
Paul Kehrerb2de9482014-12-11 14:54:48 -060012
13Loading Certificates
14~~~~~~~~~~~~~~~~~~~~
Paul Kehrer016e08a2014-11-26 09:41:18 -100015
16.. function:: load_pem_x509_certificate(data, backend)
17
18 .. versionadded:: 0.7
19
20 Deserialize a certificate from PEM encoded data.
21
22 :param bytes data: The PEM encoded certificate data.
23
24 :param backend: A backend supporting the
25 :class:`~cryptography.hazmat.backends.interfaces.X509Backend`
26 interface.
27
Paul Kehrerb2de9482014-12-11 14:54:48 -060028 :returns: An instance of :class:`~cryptography.x509.X509Certificate`.
Paul Kehrer016e08a2014-11-26 09:41:18 -100029
30.. function:: load_der_x509_certificate(data, backend)
31
32 .. versionadded:: 0.7
33
34 Deserialize a certificate from DER encoded data.
35
36 :param bytes data: The DER encoded certificate data.
37
38 :param backend: A backend supporting the
39 :class:`~cryptography.hazmat.backends.interfaces.X509Backend`
40 interface.
41
Paul Kehrerb2de9482014-12-11 14:54:48 -060042 :returns: An instance of :class:`~cryptography.x509.X509Certificate`.
Paul Kehrer016e08a2014-11-26 09:41:18 -100043
44.. testsetup::
45
46 pem_data = b"""
47 -----BEGIN CERTIFICATE-----
48 MIIDfDCCAmSgAwIBAgIBAjANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJVUzEf
49 MB0GA1UEChMWVGVzdCBDZXJ0aWZpY2F0ZXMgMjAxMTEVM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2se2QleZidUmrziVNP4oEeXINokU6T6p//HM1FG
66 QYw2jOvpKcKtWCSAnegEbgsGYzATKjmPJPJ0npHFqzM=
67 -----END CERTIFICATE-----
68 """.strip()
69
70.. doctest::
71
72 >>> from cryptography.x509 import load_pem_x509_certificate
73 >>> from cryptography.hazmat.backends import default_backend
74 >>> cert = load_pem_x509_certificate(pem_data, default_backend())
75 >>> cert.serial
76 2
77
Paul Kehrerb2de9482014-12-11 14:54:48 -060078Interface
79~~~~~~~~~
80
81.. class:: X509Certificate
82
83 .. versionadded:: 0.7
84
85 .. attribute:: version
86
87 :type: :class:`~cryptography.x509.X509Version`
88
89 The certificate version as an enumeration.
90
91 .. method:: fingerprint(algorithm)
92
93 :param algorithm: The
94 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
95 that will be used to generate the fingerprint.
96
97 :return bytes: The fingerprint using the supplied hash algorithm as
98 bytes.
99
100 .. attribute:: serial
101
102 :type: int
103
104 The serial as a Python integer.
105
106 .. method:: public_key()
107
108 :type:
109 :class:`~cryptography.hazmat.primitives.interfaces.RSAPublicKey` or
110 :class:`~cryptography.hazmat.primitives.interfaces.DSAPublicKey` or
111 :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurvePublicKey`
112
113 The public key associated with the certificate.
114
115 .. attribute:: not_valid_before
116
117 :type: :class:`datetime.datetime`
118
119 A naïve datetime representing the beginning of the validity period for the
120 certificate in UTC. This value is inclusive.
121
122 .. attribute:: not_valid_after
123
124 :type: :class:`datetime.datetime`
125
126 A naïve datetime representing the end of the validity period for the
127 certificate in UTC. This value is inclusive.
128
129
Paul Kehrer016e08a2014-11-26 09:41:18 -1000130Support Classes
131~~~~~~~~~~~~~~~
132
133.. class:: X509Version
134
135 .. versionadded:: 0.7
136
137 An enumeration for X.509 versions.
138
139 .. attribute:: v1
140
141 For version 1 X.509 certificates.
142
143 .. attribute:: v3
144
145 For version 3 X.509 certificates.
146
Paul Kehrera68fd332014-11-27 07:08:40 -1000147.. class:: InvalidX509Version
148
149 This is raised when an X.509 certificate has an invalid version number.
Paul Kehrer016e08a2014-11-26 09:41:18 -1000150
151
152.. _`public key infrastructure`: https://en.wikipedia.org/wiki/Public_key_infrastructure
Paul Kehrera68fd332014-11-27 07:08:40 -1000153.. _`TLS`: https://en.wikipedia.org/wiki/Transport_Layer_Security