blob: aea084fe84ee597b447f6cefe184e5dcb9b1505d [file] [log] [blame]
Paul Kehrer016e08a2014-11-26 09:41:18 -10001X.509
2=====
3
Paul Kehrera9d78c12014-11-26 10:59:03 -10004.. currentmodule:: cryptography.x509
Paul Kehrer016e08a2014-11-26 09:41:18 -10005
6X.509 is an ITU-T standard for a `public key infrastructure`_. X.509v3 is
Paul Kehrera68fd332014-11-27 07:08:40 -10007defined in :rfc:`5280` (which obsoletes :rfc:`2459` and :rfc:`3280`). X.509
8certificates are commonly used in protocols like `TLS`_.
Paul Kehrer016e08a2014-11-26 09:41:18 -10009
Paul Kehrerb2de9482014-12-11 14:54:48 -060010
11Loading Certificates
12~~~~~~~~~~~~~~~~~~~~
Paul Kehrer016e08a2014-11-26 09:41:18 -100013
14.. function:: load_pem_x509_certificate(data, backend)
15
16 .. versionadded:: 0.7
17
Paul Kehrere76cd272014-12-14 19:00:51 -060018 Deserialize a certificate from PEM encoded data. PEM certificates are
19 base64 decoded and have delimiters that look like
20 ``-----BEGIN CERTIFICATE-----``.
Paul Kehrer016e08a2014-11-26 09:41:18 -100021
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 Kehrere76cd272014-12-14 19:00:51 -060028 :returns: An instance of :class:`~cryptography.x509.Certificate`.
Paul Kehrer016e08a2014-11-26 09:41:18 -100029
30.. function:: load_der_x509_certificate(data, backend)
31
32 .. versionadded:: 0.7
33
Paul Kehrere76cd272014-12-14 19:00:51 -060034 Deserialize a certificate from DER encoded data. DER is a binary format
Paul Kehrer92aac382014-12-15 16:25:28 -060035 and is commonly found in files with the ``.cer`` extension (although file
36 extensions are not a guarantee of encoding type).
Paul Kehrer016e08a2014-11-26 09:41:18 -100037
38 :param bytes data: The DER encoded certificate data.
39
40 :param backend: A backend supporting the
41 :class:`~cryptography.hazmat.backends.interfaces.X509Backend`
42 interface.
43
Paul Kehrere76cd272014-12-14 19:00:51 -060044 :returns: An instance of :class:`~cryptography.x509.Certificate`.
Paul Kehrer016e08a2014-11-26 09:41:18 -100045
46.. testsetup::
47
48 pem_data = b"""
49 -----BEGIN CERTIFICATE-----
50 MIIDfDCCAmSgAwIBAgIBAjANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJVUzEf
51 MB0GA1UEChMWVGVzdCBDZXJ0aWZpY2F0ZXMgMjAxMTEVMBMGA1UEAxMMVHJ1c3Qg
52 QW5jaG9yMB4XDTEwMDEwMTA4MzAwMFoXDTMwMTIzMTA4MzAwMFowQDELMAkGA1UE
53 BhMCVVMxHzAdBgNVBAoTFlRlc3QgQ2VydGlmaWNhdGVzIDIwMTExEDAOBgNVBAMT
54 B0dvb2QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCQWJpHYo37
55 Xfb7oJSPe+WvfTlzIG21WQ7MyMbGtK/m8mejCzR6c+f/pJhEH/OcDSMsXq8h5kXa
56 BGqWK+vSwD/Pzp5OYGptXmGPcthDtAwlrafkGOS4GqIJ8+k9XGKs+vQUXJKsOk47
57 RuzD6PZupq4s16xaLVqYbUC26UcY08GpnoLNHJZS/EmXw1ZZ3d4YZjNlpIpWFNHn
58 UGmdiGKXUPX/9H0fVjIAaQwjnGAbpgyCumWgzIwPpX+ElFOUr3z7BoVnFKhIXze+
59 VmQGSWxZxvWDUN90Ul0tLEpLgk3OVxUB4VUGuf15OJOpgo1xibINPmWt14Vda2N9
60 yrNKloJGZNqLAgMBAAGjfDB6MB8GA1UdIwQYMBaAFOR9X9FclYYILAWuvnW2ZafZ
61 XahmMB0GA1UdDgQWBBRYAYQkG7wrUpRKPaUQchRR9a86yTAOBgNVHQ8BAf8EBAMC
62 AQYwFwYDVR0gBBAwDjAMBgpghkgBZQMCATABMA8GA1UdEwEB/wQFMAMBAf8wDQYJ
63 KoZIhvcNAQELBQADggEBADWHlxbmdTXNwBL/llwhQqwnazK7CC2WsXBBqgNPWj7m
64 tvQ+aLG8/50Qc2Sun7o2VnwF9D18UUe8Gj3uPUYH+oSI1vDdyKcjmMbKRU4rk0eo
65 3UHNDXwqIVc9CQS9smyV+x1HCwL4TTrq+LXLKx/qVij0Yqk+UJfAtrg2jnYKXsCu
66 FMBQQnWCGrwa1g1TphRp/RmYHnMynYFmZrXtzFz+U9XEA7C+gPq4kqDI/iVfIT1s
67 6lBtdB50lrDVwl2oYfAvW/6sC2se2QleZidUmrziVNP4oEeXINokU6T6p//HM1FG
68 QYw2jOvpKcKtWCSAnegEbgsGYzATKjmPJPJ0npHFqzM=
69 -----END CERTIFICATE-----
70 """.strip()
71
72.. doctest::
73
Paul Kehrercc8a26e2014-12-16 12:40:16 -060074 >>> from cryptography import x509
Paul Kehrer016e08a2014-11-26 09:41:18 -100075 >>> from cryptography.hazmat.backends import default_backend
Paul Kehrercc8a26e2014-12-16 12:40:16 -060076 >>> cert = x509.load_pem_x509_certificate(pem_data, default_backend())
Paul Kehrer016e08a2014-11-26 09:41:18 -100077 >>> cert.serial
78 2
79
Paul Kehrere76cd272014-12-14 19:00:51 -060080X.509 Certificate Object
81~~~~~~~~~~~~~~~~~~~~~~~~
Paul Kehrerb2de9482014-12-11 14:54:48 -060082
Paul Kehrere76cd272014-12-14 19:00:51 -060083.. class:: Certificate
Paul Kehrerb2de9482014-12-11 14:54:48 -060084
85 .. versionadded:: 0.7
86
87 .. attribute:: version
88
Paul Kehrere76cd272014-12-14 19:00:51 -060089 :type: :class:`~cryptography.x509.Version`
Paul Kehrerb2de9482014-12-11 14:54:48 -060090
Paul Kehrere76cd272014-12-14 19:00:51 -060091 The certificate version as an enumeration. Version 3 certificates are
92 the latest version and also the only type you should see in practice.
Paul Kehrerb2de9482014-12-11 14:54:48 -060093
Alex Gaynor89c4dc82014-12-16 16:49:33 -080094 :raises cryptography.x509.InvalidVersion: If the version in the
Alex Gaynor6d7ab4c2014-12-16 16:50:33 -080095 certificate is not a known
96 :class:`X.509 version <cryptography.x509.Version>`.
Paul Kehrer92aac382014-12-15 16:25:28 -060097
Paul Kehrercc8a26e2014-12-16 12:40:16 -060098 .. doctest::
99
100 >>> cert.version
101 <Version.v3: 2>
102
Paul Kehrerb2de9482014-12-11 14:54:48 -0600103 .. method:: fingerprint(algorithm)
104
105 :param algorithm: The
106 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
107 that will be used to generate the fingerprint.
108
109 :return bytes: The fingerprint using the supplied hash algorithm as
110 bytes.
111
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600112 .. doctest::
113
114 >>> from cryptography.hazmat.primitives import hashes
115 >>> cert.fingerprint(hashes.SHA256())
Paul Kehrer78a81502014-12-16 14:47:52 -0600116 '\x86\xd2\x187Gc\xfc\xe7}[+E9\x8d\xb4\x8f\x10\xe5S\xda\x18u\xbe}a\x03\x08[\xac\xa04?'
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600117
Paul Kehrerb2de9482014-12-11 14:54:48 -0600118 .. attribute:: serial
119
120 :type: int
121
122 The serial as a Python integer.
123
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600124 .. doctest::
125
126 >>> cert.serial
127 2
128
Paul Kehrerb2de9482014-12-11 14:54:48 -0600129 .. method:: public_key()
130
131 :type:
Alex Stapletonf79c2312014-12-30 12:50:14 +0000132 :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey` or
Paul Kehrerb2de9482014-12-11 14:54:48 -0600133 :class:`~cryptography.hazmat.primitives.interfaces.DSAPublicKey` or
134 :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurvePublicKey`
135
136 The public key associated with the certificate.
137
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600138 .. doctest::
139
Alex Stapletonf79c2312014-12-30 12:50:14 +0000140 >>> from cryptography.hazmat.primitives.asymmetric import rsa
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600141 >>> public_key = cert.public_key()
Alex Stapletonf79c2312014-12-30 12:50:14 +0000142 >>> isinstance(public_key, rsa.RSAPublicKey)
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600143 True
144
Paul Kehrerb2de9482014-12-11 14:54:48 -0600145 .. attribute:: not_valid_before
146
147 :type: :class:`datetime.datetime`
148
Paul Kehrer78a81502014-12-16 14:47:52 -0600149 A naïve datetime representing the beginning of the validity period for
150 the certificate in UTC. This value is inclusive.
Paul Kehrerb2de9482014-12-11 14:54:48 -0600151
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600152 .. doctest::
153
154 >>> cert.not_valid_before
155 datetime.datetime(2010, 1, 1, 8, 30)
156
Paul Kehrerb2de9482014-12-11 14:54:48 -0600157 .. attribute:: not_valid_after
158
159 :type: :class:`datetime.datetime`
160
Paul Kehrer78a81502014-12-16 14:47:52 -0600161 A naïve datetime representing the end of the validity period for the
162 certificate in UTC. This value is inclusive.
Paul Kehrerb2de9482014-12-11 14:54:48 -0600163
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600164 .. doctest::
165
166 >>> cert.not_valid_after
167 datetime.datetime(2030, 12, 31, 8, 30)
168
Paul Kehrerb2de9482014-12-11 14:54:48 -0600169
Paul Kehrere76cd272014-12-14 19:00:51 -0600170.. class:: Version
Paul Kehrer016e08a2014-11-26 09:41:18 -1000171
172 .. versionadded:: 0.7
173
174 An enumeration for X.509 versions.
175
176 .. attribute:: v1
177
178 For version 1 X.509 certificates.
179
180 .. attribute:: v3
181
182 For version 3 X.509 certificates.
183
Paul Kehrer806bfb22015-02-02 17:05:24 -0600184.. class:: NameAttribute
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600185
186 .. versionadded:: 0.8
187
Paul Kehrer806bfb22015-02-02 17:05:24 -0600188 An X.509 name consists of a list of NameAttribute objects.
Paul Kehrer5b0a8d62015-01-30 20:05:55 -0600189
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600190 .. attribute:: oid
191
192 :type: :class:`ObjectIdentifier`
193
194 The attribute OID.
195
196 .. attribute:: value
197
Paul Kehrerd5852cb2015-01-30 08:25:23 -0600198 :type: :term:`text`
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600199
200 The value of the attribute.
201
202.. class:: ObjectIdentifier
203
204 .. versionadded:: 0.8
205
Paul Kehrer5b0a8d62015-01-30 20:05:55 -0600206 Object identifiers (frequently seen abbreviated as OID) identify the type
Paul Kehrer806bfb22015-02-02 17:05:24 -0600207 of a value (see: :class:`NameAttribute`).
Paul Kehrer5b0a8d62015-01-30 20:05:55 -0600208
Paul Kehrerd44f9a62015-02-04 14:47:34 -0600209 .. attribute:: dotted_string
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600210
211 :type: :class:`str`
212
213 The dotted string value of the OID (e.g. "2.5.4.3")
214
215Object Identifiers
216~~~~~~~~~~~~~~~~~~
217
218X.509 name elements are identified by :class:`ObjectIdentifier` instances. The
219following common OIDs are available as constants.
220
221.. data:: OID_COMMON_NAME
222
223.. data:: OID_COUNTRY_NAME
224
225.. data:: OID_LOCALITY_NAME
226
227.. data:: OID_STATE_OR_PROVINCE_NAME
228
229.. data:: OID_ORGANIZATION_NAME
230
231.. data:: OID_ORGANIZATIONAL_UNIT_NAME
232
233.. data:: OID_SERIAL_NUMBER
234
235.. data:: OID_SURNAME
236
237.. data:: OID_GIVEN_NAME
238
239.. data:: OID_TITLE
240
241.. data:: OID_GENERATION_QUALIFIER
242
243.. data:: OID_DN_QUALIFIER
244
245.. data:: OID_PSEUDONYM
246
247.. data:: OID_DOMAIN_COMPONENT
248
249.. data:: OID_EMAIL_ADDRESS
250
251Exceptions
252~~~~~~~~~~
253
Paul Kehrere76cd272014-12-14 19:00:51 -0600254.. class:: InvalidVersion
Paul Kehrera68fd332014-11-27 07:08:40 -1000255
256 This is raised when an X.509 certificate has an invalid version number.
Paul Kehrer016e08a2014-11-26 09:41:18 -1000257
Paul Kehrerd5cccf72014-12-15 17:20:33 -0600258 .. attribute:: parsed_version
259
Paul Kehrerbbffc402014-12-17 13:33:55 -0600260 :type: int
261
262 Returns the raw version that was parsed from the certificate.
Paul Kehrerd5cccf72014-12-15 17:20:33 -0600263
Paul Kehrer016e08a2014-11-26 09:41:18 -1000264
265.. _`public key infrastructure`: https://en.wikipedia.org/wiki/Public_key_infrastructure
Paul Kehrera68fd332014-11-27 07:08:40 -1000266.. _`TLS`: https://en.wikipedia.org/wiki/Transport_Layer_Security