blob: 5aa5f2a1f3c371240a670f6fa9c01792d73ca5e4 [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 Kehrerd26c4db2015-03-15 15:36:24 -050010.. testsetup::
11
12 pem_req_data = b"""
13 -----BEGIN CERTIFICATE REQUEST-----
14 MIIC0zCCAbsCAQAwWTELMAkGA1UEBhMCVVMxETAPBgNVBAgMCElsbGlub2lzMRAw
15 DgYDVQQHDAdDaGljYWdvMREwDwYDVQQKDAhyNTA5IExMQzESMBAGA1UEAwwJaGVs
16 bG8uY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqhZx+Mo9VRd9
17 vsnWWa6NBCws21rZ0+1B/JGgB4hDsZS7iDE4Bj5z4idheFRtl8bBbdjPknq7BfoF
18 8v15Zq/Zv7i2xMSDL+LUrTBZezRd4bRTGqCm6YJ5EYkhqdcqeZleHCFImguHoq1J
19 Fh0+kObQrTHXw3ZP57a3o1IvyIUA3nNoCBL0QQhwBXaDXOojMKNR+bqB5ve8GS1y
20 Elr0AM/+cJsfaIahNQUgFKx3Eu3GeEOMKYOAG1lycgdQdmTUybLrT3U7vkClTseM
21 xHg1r5En7ALjONIhqRuq3rddYahrP8HXozb3zUy3cJ7P6IeaosuvNzvMXOX9P6HD
22 Ha9urDAJ1wIDAQABoDUwMwYJKoZIhvcNAQkOMSYwJDAiBgNVHREEGzAZggl3b3Js
23 ZC5jb22CDHdoYXRldmVyLmNvbTANBgkqhkiG9w0BAQUFAAOCAQEAS4Ro6h+z52SK
24 YSLCYARpnEu/rmh4jdqndt8naqcNb6uLx9mlKZ2W9on9XDjnSdQD9q+ZP5aZfESw
25 R0+rJhW9ZrNa/g1pt6M24ihclHYDAxYMWxT1z/TXXGM3TmZZ6gfYlNE1kkBuODHa
26 UYsR/1Ht1E1EsmmUimt2n+zQR2K8T9Coa+boaUW/GsTEuz1aaJAkj5ZvTDiIhRG4
27 AOCqFZOLAQmCCNgJnnspD9hDz/Ons085LF5wnYjN4/Nsk5tS6AGs3xjZ3jPoOGGn
28 82WQ9m4dBGoVDZXsobVTaN592JEYwN5iu72zRn7Einb4V4H5y3yD2dD4yWPlt4pk
29 5wFkeYsZEA==
30 -----END CERTIFICATE REQUEST-----
31 """.strip()
32
Paul Kehrerb2de9482014-12-11 14:54:48 -060033
34Loading Certificates
35~~~~~~~~~~~~~~~~~~~~
Paul Kehrer016e08a2014-11-26 09:41:18 -100036
37.. function:: load_pem_x509_certificate(data, backend)
38
39 .. versionadded:: 0.7
40
Paul Kehrere76cd272014-12-14 19:00:51 -060041 Deserialize a certificate from PEM encoded data. PEM certificates are
42 base64 decoded and have delimiters that look like
43 ``-----BEGIN CERTIFICATE-----``.
Paul Kehrer016e08a2014-11-26 09:41:18 -100044
45 :param bytes data: The PEM encoded certificate data.
46
47 :param backend: A backend supporting the
48 :class:`~cryptography.hazmat.backends.interfaces.X509Backend`
49 interface.
50
Paul Kehrere76cd272014-12-14 19:00:51 -060051 :returns: An instance of :class:`~cryptography.x509.Certificate`.
Paul Kehrer016e08a2014-11-26 09:41:18 -100052
53.. function:: load_der_x509_certificate(data, backend)
54
55 .. versionadded:: 0.7
56
Paul Kehrere76cd272014-12-14 19:00:51 -060057 Deserialize a certificate from DER encoded data. DER is a binary format
Paul Kehrer92aac382014-12-15 16:25:28 -060058 and is commonly found in files with the ``.cer`` extension (although file
59 extensions are not a guarantee of encoding type).
Paul Kehrer016e08a2014-11-26 09:41:18 -100060
61 :param bytes data: The DER encoded certificate data.
62
63 :param backend: A backend supporting the
64 :class:`~cryptography.hazmat.backends.interfaces.X509Backend`
65 interface.
66
Paul Kehrere76cd272014-12-14 19:00:51 -060067 :returns: An instance of :class:`~cryptography.x509.Certificate`.
Paul Kehrer016e08a2014-11-26 09:41:18 -100068
69.. testsetup::
70
71 pem_data = b"""
72 -----BEGIN CERTIFICATE-----
73 MIIDfDCCAmSgAwIBAgIBAjANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJVUzEf
74 MB0GA1UEChMWVGVzdCBDZXJ0aWZpY2F0ZXMgMjAxMTEVMBMGA1UEAxMMVHJ1c3Qg
75 QW5jaG9yMB4XDTEwMDEwMTA4MzAwMFoXDTMwMTIzMTA4MzAwMFowQDELMAkGA1UE
76 BhMCVVMxHzAdBgNVBAoTFlRlc3QgQ2VydGlmaWNhdGVzIDIwMTExEDAOBgNVBAMT
77 B0dvb2QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCQWJpHYo37
78 Xfb7oJSPe+WvfTlzIG21WQ7MyMbGtK/m8mejCzR6c+f/pJhEH/OcDSMsXq8h5kXa
79 BGqWK+vSwD/Pzp5OYGptXmGPcthDtAwlrafkGOS4GqIJ8+k9XGKs+vQUXJKsOk47
80 RuzD6PZupq4s16xaLVqYbUC26UcY08GpnoLNHJZS/EmXw1ZZ3d4YZjNlpIpWFNHn
81 UGmdiGKXUPX/9H0fVjIAaQwjnGAbpgyCumWgzIwPpX+ElFOUr3z7BoVnFKhIXze+
82 VmQGSWxZxvWDUN90Ul0tLEpLgk3OVxUB4VUGuf15OJOpgo1xibINPmWt14Vda2N9
83 yrNKloJGZNqLAgMBAAGjfDB6MB8GA1UdIwQYMBaAFOR9X9FclYYILAWuvnW2ZafZ
84 XahmMB0GA1UdDgQWBBRYAYQkG7wrUpRKPaUQchRR9a86yTAOBgNVHQ8BAf8EBAMC
85 AQYwFwYDVR0gBBAwDjAMBgpghkgBZQMCATABMA8GA1UdEwEB/wQFMAMBAf8wDQYJ
86 KoZIhvcNAQELBQADggEBADWHlxbmdTXNwBL/llwhQqwnazK7CC2WsXBBqgNPWj7m
87 tvQ+aLG8/50Qc2Sun7o2VnwF9D18UUe8Gj3uPUYH+oSI1vDdyKcjmMbKRU4rk0eo
88 3UHNDXwqIVc9CQS9smyV+x1HCwL4TTrq+LXLKx/qVij0Yqk+UJfAtrg2jnYKXsCu
89 FMBQQnWCGrwa1g1TphRp/RmYHnMynYFmZrXtzFz+U9XEA7C+gPq4kqDI/iVfIT1s
90 6lBtdB50lrDVwl2oYfAvW/6sC2se2QleZidUmrziVNP4oEeXINokU6T6p//HM1FG
91 QYw2jOvpKcKtWCSAnegEbgsGYzATKjmPJPJ0npHFqzM=
92 -----END CERTIFICATE-----
93 """.strip()
94
95.. doctest::
96
Paul Kehrercc8a26e2014-12-16 12:40:16 -060097 >>> from cryptography import x509
Paul Kehrer016e08a2014-11-26 09:41:18 -100098 >>> from cryptography.hazmat.backends import default_backend
Paul Kehrercc8a26e2014-12-16 12:40:16 -060099 >>> cert = x509.load_pem_x509_certificate(pem_data, default_backend())
Paul Kehrer016e08a2014-11-26 09:41:18 -1000100 >>> cert.serial
101 2
102
Paul Kehrera1a1f232015-03-15 15:34:35 -0500103Loading Certificate Signing Requests
104~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Paul Kehrerdc480ad2015-02-23 12:14:54 -0600105
Paul Kehrer31e39882015-03-11 11:37:04 -0500106.. function:: load_pem_x509_csr(data, backend)
Paul Kehrerdc480ad2015-02-23 12:14:54 -0600107
108 .. versionadded:: 0.9
109
Paul Kehrera1a1f232015-03-15 15:34:35 -0500110 Deserialize a certificate signing request (CSR) from PEM encoded data. PEM
Paul Kehrer5aadb9c2015-03-11 20:48:42 -0500111 requests are base64 decoded and have delimiters that look like
Paul Kehrerdc480ad2015-02-23 12:14:54 -0600112 ``-----BEGIN CERTIFICATE REQUEST-----``. This is also known as PKCS#10
113 format.
114
115 :param bytes data: The PEM encoded request data.
116
117 :param backend: A backend supporting the
118 :class:`~cryptography.hazmat.backends.interfaces.X509Backend`
119 interface.
120
Paul Kehrera1a1f232015-03-15 15:34:35 -0500121 :returns: An instance of
122 :class:`~cryptography.x509.CertificateSigningRequest`.
Paul Kehrerdc480ad2015-02-23 12:14:54 -0600123
Paul Kehrerdc480ad2015-02-23 12:14:54 -0600124.. doctest::
125
126 >>> from cryptography import x509
127 >>> from cryptography.hazmat.backends import default_backend
128 >>> from cryptography.hazmat.primitives import hashes
Paul Kehrera1a1f232015-03-15 15:34:35 -0500129 >>> csr = x509.load_pem_x509_csr(pem_req_data, default_backend())
130 >>> isinstance(csr.signature_hash_algorithm, hashes.SHA1)
Paul Kehrerdc480ad2015-02-23 12:14:54 -0600131 True
132
Paul Kehrere76cd272014-12-14 19:00:51 -0600133X.509 Certificate Object
134~~~~~~~~~~~~~~~~~~~~~~~~
Paul Kehrerb2de9482014-12-11 14:54:48 -0600135
Paul Kehrere76cd272014-12-14 19:00:51 -0600136.. class:: Certificate
Paul Kehrerb2de9482014-12-11 14:54:48 -0600137
138 .. versionadded:: 0.7
139
140 .. attribute:: version
141
Paul Kehrere76cd272014-12-14 19:00:51 -0600142 :type: :class:`~cryptography.x509.Version`
Paul Kehrerb2de9482014-12-11 14:54:48 -0600143
Paul Kehrere76cd272014-12-14 19:00:51 -0600144 The certificate version as an enumeration. Version 3 certificates are
145 the latest version and also the only type you should see in practice.
Paul Kehrerb2de9482014-12-11 14:54:48 -0600146
Alex Gaynor89c4dc82014-12-16 16:49:33 -0800147 :raises cryptography.x509.InvalidVersion: If the version in the
Alex Gaynor6d7ab4c2014-12-16 16:50:33 -0800148 certificate is not a known
149 :class:`X.509 version <cryptography.x509.Version>`.
Paul Kehrer92aac382014-12-15 16:25:28 -0600150
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600151 .. doctest::
152
153 >>> cert.version
154 <Version.v3: 2>
155
Paul Kehrerb2de9482014-12-11 14:54:48 -0600156 .. method:: fingerprint(algorithm)
157
158 :param algorithm: The
Paul Kehrer601278a2015-02-12 12:51:00 -0600159 :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`
Paul Kehrerb2de9482014-12-11 14:54:48 -0600160 that will be used to generate the fingerprint.
161
162 :return bytes: The fingerprint using the supplied hash algorithm as
163 bytes.
164
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600165 .. doctest::
166
167 >>> from cryptography.hazmat.primitives import hashes
168 >>> cert.fingerprint(hashes.SHA256())
Paul Kehrer78a81502014-12-16 14:47:52 -0600169 '\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 -0600170
Paul Kehrerb2de9482014-12-11 14:54:48 -0600171 .. attribute:: serial
172
173 :type: int
174
175 The serial as a Python integer.
176
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600177 .. doctest::
178
179 >>> cert.serial
180 2
181
Paul Kehrerb2de9482014-12-11 14:54:48 -0600182 .. method:: public_key()
183
184 :type:
Alex Stapletonf79c2312014-12-30 12:50:14 +0000185 :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey` or
Paul Kehrer45efdbc2015-02-12 10:58:22 -0600186 :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPublicKey` or
187 :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey`
Paul Kehrerb2de9482014-12-11 14:54:48 -0600188
189 The public key associated with the certificate.
190
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600191 .. doctest::
192
Alex Stapletonf79c2312014-12-30 12:50:14 +0000193 >>> from cryptography.hazmat.primitives.asymmetric import rsa
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600194 >>> public_key = cert.public_key()
Alex Stapletonf79c2312014-12-30 12:50:14 +0000195 >>> isinstance(public_key, rsa.RSAPublicKey)
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600196 True
197
Paul Kehrerb2de9482014-12-11 14:54:48 -0600198 .. attribute:: not_valid_before
199
200 :type: :class:`datetime.datetime`
201
Paul Kehrer78a81502014-12-16 14:47:52 -0600202 A naïve datetime representing the beginning of the validity period for
203 the certificate in UTC. This value is inclusive.
Paul Kehrerb2de9482014-12-11 14:54:48 -0600204
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600205 .. doctest::
206
207 >>> cert.not_valid_before
208 datetime.datetime(2010, 1, 1, 8, 30)
209
Paul Kehrerb2de9482014-12-11 14:54:48 -0600210 .. attribute:: not_valid_after
211
212 :type: :class:`datetime.datetime`
213
Paul Kehrer78a81502014-12-16 14:47:52 -0600214 A naïve datetime representing the end of the validity period for the
215 certificate in UTC. This value is inclusive.
Paul Kehrerb2de9482014-12-11 14:54:48 -0600216
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600217 .. doctest::
218
219 >>> cert.not_valid_after
220 datetime.datetime(2030, 12, 31, 8, 30)
221
Paul Kehrer719d5362015-01-01 20:03:52 -0600222 .. attribute:: issuer
223
224 .. versionadded:: 0.8
225
226 :type: :class:`Name`
227
228 The :class:`Name` of the issuer.
229
230 .. attribute:: subject
231
232 .. versionadded:: 0.8
233
234 :type: :class:`Name`
235
236 The :class:`Name` of the subject.
237
Paul Kehrer8802a5b2015-02-13 12:06:57 -0600238 .. attribute:: signature_hash_algorithm
Paul Kehrer56da2a52015-02-11 23:35:07 -0600239
Paul Kehrer8802a5b2015-02-13 12:06:57 -0600240 :type: :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`
Paul Kehrer56da2a52015-02-11 23:35:07 -0600241
Paul Kehrere612ec72015-02-16 14:33:35 -0600242 Returns the
Paul Kehrer71d40c62015-02-19 08:21:04 -0600243 :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` which
Paul Kehrer1a7ba872015-02-19 18:09:05 -0600244 was used in signing this certificate.
Paul Kehrer56da2a52015-02-11 23:35:07 -0600245
246 .. doctest::
247
Paul Kehrer8802a5b2015-02-13 12:06:57 -0600248 >>> from cryptography.hazmat.primitives import hashes
249 >>> isinstance(cert.signature_hash_algorithm, hashes.SHA256)
250 True
Paul Kehrer719d5362015-01-01 20:03:52 -0600251
Paul Kehrerfbb7ac82015-03-16 19:26:29 -0500252 .. attribute:: extensions
253
254 :type: :class:`Extensions`
255
256 The extensions encoded in the certificate.
257
258 :raises cryptography.x509.DuplicateExtension: If more than one
259 extension of the same type is found within the certificate.
260
Paul Kehrerfa56a232015-03-17 13:14:03 -0500261 .. doctest::
262
263 >>> for ext in cert.extensions:
264 ... print(ext)
265 <Extension(oid=<ObjectIdentifier(oid=2.5.29.19, name=basicConstraints)>, critical=True, value=<BasicConstraints(ca=True, path_length=None)>)>
266
Paul Kehrer5aadb9c2015-03-11 20:48:42 -0500267X.509 CSR (Certificate Signing Request) Object
268~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Paul Kehrerdc480ad2015-02-23 12:14:54 -0600269
Paul Kehrera1a1f232015-03-15 15:34:35 -0500270.. class:: CertificateSigningRequest
Paul Kehrerdc480ad2015-02-23 12:14:54 -0600271
272 .. versionadded:: 0.9
273
274 .. method:: public_key()
275
276 :type:
277 :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey` or
278 :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPublicKey` or
279 :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey`
280
281 The public key associated with the request.
282
283 .. doctest::
284
285 >>> from cryptography.hazmat.primitives.asymmetric import rsa
Paul Kehrera1a1f232015-03-15 15:34:35 -0500286 >>> public_key = csr.public_key()
Paul Kehrerdc480ad2015-02-23 12:14:54 -0600287 >>> isinstance(public_key, rsa.RSAPublicKey)
288 True
289
290 .. attribute:: subject
291
292 :type: :class:`Name`
293
294 The :class:`Name` of the subject.
295
296 .. attribute:: signature_hash_algorithm
297
298 :type: :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`
299
300 Returns the
301 :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` which
302 was used in signing this request.
303
304 .. doctest::
305
306 >>> from cryptography.hazmat.primitives import hashes
Paul Kehrera1a1f232015-03-15 15:34:35 -0500307 >>> isinstance(csr.signature_hash_algorithm, hashes.SHA1)
Paul Kehrerdc480ad2015-02-23 12:14:54 -0600308 True
309
Paul Kehrer719d5362015-01-01 20:03:52 -0600310.. class:: Name
311
312 .. versionadded:: 0.8
313
Paul Kehrer53d8d492015-02-13 18:47:30 -0600314 An X509 Name is an ordered list of attributes. The object is iterable to
Paul Kehrerd21596e2015-02-14 09:17:26 -0600315 get every attribute or you can use :meth:`Name.get_attributes_for_oid` to
Paul Kehrer719d5362015-01-01 20:03:52 -0600316 obtain the specific type you want. Names are sometimes represented as a
Paul Kehrer53d8d492015-02-13 18:47:30 -0600317 slash or comma delimited string (e.g. ``/CN=mydomain.com/O=My Org/C=US`` or
318 ``CN=mydomain.com, O=My Org, C=US``).
Paul Kehrer719d5362015-01-01 20:03:52 -0600319
Paul Kehrer53d8d492015-02-13 18:47:30 -0600320 .. doctest::
Paul Kehrer719d5362015-01-01 20:03:52 -0600321
Paul Kehrer8b21a4a2015-02-14 07:56:36 -0600322 >>> len(cert.subject)
Paul Kehrer53d8d492015-02-13 18:47:30 -0600323 3
Paul Kehrer8b21a4a2015-02-14 07:56:36 -0600324 >>> for attribute in cert.subject:
325 ... print(attribute)
326 <NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.6, name=countryName)>, value=u'US')>
327 <NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.10, name=organizationName)>, value=u'Test Certificates 2011')>
328 <NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)>, value=u'Good CA')>
Paul Kehrer719d5362015-01-01 20:03:52 -0600329
Paul Kehrere901d642015-02-11 18:50:58 -0600330 .. method:: get_attributes_for_oid(oid)
Paul Kehrer719d5362015-01-01 20:03:52 -0600331
Paul Kehrere901d642015-02-11 18:50:58 -0600332 :param oid: An :class:`ObjectIdentifier` instance.
Paul Kehrer719d5362015-01-01 20:03:52 -0600333
Paul Kehrere901d642015-02-11 18:50:58 -0600334 :returns: A list of :class:`NameAttribute` instances that match the
335 OID provided. If nothing matches an empty list will be returned.
Paul Kehrer719d5362015-01-01 20:03:52 -0600336
337 .. doctest::
338
Paul Kehrere901d642015-02-11 18:50:58 -0600339 >>> cert.subject.get_attributes_for_oid(x509.OID_COMMON_NAME)
340 [<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)>, value=u'Good CA')>]
Paul Kehrerb2de9482014-12-11 14:54:48 -0600341
Paul Kehrere76cd272014-12-14 19:00:51 -0600342.. class:: Version
Paul Kehrer016e08a2014-11-26 09:41:18 -1000343
344 .. versionadded:: 0.7
345
346 An enumeration for X.509 versions.
347
348 .. attribute:: v1
349
350 For version 1 X.509 certificates.
351
352 .. attribute:: v3
353
354 For version 3 X.509 certificates.
355
Paul Kehrer806bfb22015-02-02 17:05:24 -0600356.. class:: NameAttribute
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600357
358 .. versionadded:: 0.8
359
Paul Kehrer834d22f2015-02-06 11:01:07 -0600360 An X.509 name consists of a list of NameAttribute instances.
Paul Kehrer5b0a8d62015-01-30 20:05:55 -0600361
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600362 .. attribute:: oid
363
364 :type: :class:`ObjectIdentifier`
365
366 The attribute OID.
367
368 .. attribute:: value
369
Paul Kehrerd5852cb2015-01-30 08:25:23 -0600370 :type: :term:`text`
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600371
372 The value of the attribute.
373
374.. class:: ObjectIdentifier
375
376 .. versionadded:: 0.8
377
Paul Kehrer5b0a8d62015-01-30 20:05:55 -0600378 Object identifiers (frequently seen abbreviated as OID) identify the type
Paul Kehrer806bfb22015-02-02 17:05:24 -0600379 of a value (see: :class:`NameAttribute`).
Paul Kehrer5b0a8d62015-01-30 20:05:55 -0600380
Paul Kehrerd44f9a62015-02-04 14:47:34 -0600381 .. attribute:: dotted_string
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600382
383 :type: :class:`str`
384
Paul Kehrerfedf4f42015-02-06 11:22:07 -0600385 The dotted string value of the OID (e.g. ``"2.5.4.3"``)
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600386
Paul Kehrer8cf26422015-03-21 09:50:24 -0500387X.509 Extensions
388~~~~~~~~~~~~~~~~
389
Paul Kehrerfbb7ac82015-03-16 19:26:29 -0500390.. class:: Extensions
391
392 .. versionadded:: 0.9
393
394 An X.509 Extensions instance is an ordered list of extensions. The object
395 is iterable to get every extension.
396
Paul Kehrerfa56a232015-03-17 13:14:03 -0500397 .. method:: get_extension_for_oid(oid)
398
399 :param oid: An :class:`ObjectIdentifier` instance.
400
401 :returns: An instance of the extension class.
402
403 :raises cryptography.x509.ExtensionNotFound: If the certificate does
404 not have the extension requested.
405
406 :raises cryptography.x509.UnsupportedExtension: If the certificate
407 contains an extension that is not supported.
408
409 .. doctest::
410
411 >>> cert.extensions.get_extension_for_oid(x509.OID_BASIC_CONSTRAINTS)
412 <Extension(oid=<ObjectIdentifier(oid=2.5.29.19, name=basicConstraints)>, critical=True, value=<BasicConstraints(ca=True, path_length=None)>)>
413
Paul Kehrer8cf26422015-03-21 09:50:24 -0500414.. class:: Extension
415
416 .. versionadded:: 0.9
417
Paul Kehrer85894662015-03-22 13:19:31 -0500418 .. attribute:: oid
419
420 :type: :class:`ObjectIdentifier`
421
Paul Kehrer5553d572015-03-23 21:08:01 -0500422 The :ref:`extension OID <extension_oids>`.
Paul Kehrer8cf26422015-03-21 09:50:24 -0500423
424 .. attribute:: critical
425
426 :type: bool
427
Paul Kehrer58b75692015-03-22 23:24:58 -0500428 Determines whether a given extension is critical or not. :rfc:`5280`
429 requires that "A certificate-using system MUST reject the certificate
430 if it encounters a critical extension it does not recognize or a
431 critical extension that contains information that it cannot process".
Paul Kehrer8cf26422015-03-21 09:50:24 -0500432
Paul Kehrer85894662015-03-22 13:19:31 -0500433 .. attribute:: value
434
435 Returns an instance of the extension type corresponding to the OID.
436
Paul Kehrer8cf26422015-03-21 09:50:24 -0500437.. class:: BasicConstraints
438
439 .. versionadded:: 0.9
440
Paul Kehrer85894662015-03-22 13:19:31 -0500441 Basic constraints is an X.509 extension type that defines whether a given
Paul Kehrer8cf26422015-03-21 09:50:24 -0500442 certificate is allowed to sign additional certificates and what path
Paul Kehrer85894662015-03-22 13:19:31 -0500443 length restrictions may exist. It corresponds to
444 :data:`OID_BASIC_CONSTRAINTS`.
Paul Kehrer8cf26422015-03-21 09:50:24 -0500445
446 .. attribute:: ca
447
448 :type: bool
449
450 Whether the certificate can sign certificates.
451
452 .. attribute:: path_length
453
Paul Kehrerfd1444c2015-03-21 19:47:05 -0500454 :type: int or None
Paul Kehrer8cf26422015-03-21 09:50:24 -0500455
456 The maximum path length for certificates subordinate to this
457 certificate. This attribute only has meaning if ``ca`` is true.
458 If ``ca`` is true then a path length of None means there's no
459 restriction on the number of subordinate CAs in the certificate chain.
460 If it is zero or greater then that number defines the maximum length.
461 For example, a ``path_length`` of 1 means the certificate can sign a
462 subordinate CA, but the subordinate CA is not allowed to create
Paul Kehrerfd1444c2015-03-21 19:47:05 -0500463 subordinates with ``ca`` set to true.
Paul Kehrer8cf26422015-03-21 09:50:24 -0500464
465
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600466Object Identifiers
467~~~~~~~~~~~~~~~~~~
468
Paul Kehrer4bb46492015-02-07 16:59:14 -0600469X.509 elements are frequently identified by :class:`ObjectIdentifier`
470instances. The following common OIDs are available as constants.
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600471
Paul Kehrer56da2a52015-02-11 23:35:07 -0600472Name OIDs
473~~~~~~~~~
474
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600475.. data:: OID_COMMON_NAME
476
Paul Kehrerfb5ac9e2015-02-07 16:29:37 -0600477 Corresponds to the dotted string ``"2.5.4.3"``. Historically the domain
478 name would be encoded here for server certificates. :rfc:`2818` deprecates
479 this practice and names of that type should now be located in a
Paul Kehrer4bb46492015-02-07 16:59:14 -0600480 SubjectAlternativeName extension. This OID is typically seen in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600481
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600482.. data:: OID_COUNTRY_NAME
483
Paul Kehrer4bb46492015-02-07 16:59:14 -0600484 Corresponds to the dotted string ``"2.5.4.6"``. This OID is typically seen
485 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600486
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600487.. data:: OID_LOCALITY_NAME
488
Paul Kehrer4bb46492015-02-07 16:59:14 -0600489 Corresponds to the dotted string ``"2.5.4.7"``. This OID is typically seen
490 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600491
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600492.. data:: OID_STATE_OR_PROVINCE_NAME
493
Paul Kehrer4bb46492015-02-07 16:59:14 -0600494 Corresponds to the dotted string ``"2.5.4.8"``. This OID is typically seen
495 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600496
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600497.. data:: OID_ORGANIZATION_NAME
498
Paul Kehrer4bb46492015-02-07 16:59:14 -0600499 Corresponds to the dotted string ``"2.5.4.10"``. This OID is typically seen
500 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600501
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600502.. data:: OID_ORGANIZATIONAL_UNIT_NAME
503
Paul Kehrer4bb46492015-02-07 16:59:14 -0600504 Corresponds to the dotted string ``"2.5.4.11"``. This OID is typically seen
505 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600506
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600507.. data:: OID_SERIAL_NUMBER
508
Paul Kehrerfb5ac9e2015-02-07 16:29:37 -0600509 Corresponds to the dotted string ``"2.5.4.5"``. This is distinct from the
510 serial number of the certificate itself (which can be obtained with
Paul Kehrer4bb46492015-02-07 16:59:14 -0600511 :func:`Certificate.serial`). This OID is typically seen in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600512
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600513.. data:: OID_SURNAME
514
Paul Kehrer4bb46492015-02-07 16:59:14 -0600515 Corresponds to the dotted string ``"2.5.4.4"``. This OID is typically seen
516 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600517
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600518.. data:: OID_GIVEN_NAME
519
Paul Kehrer4bb46492015-02-07 16:59:14 -0600520 Corresponds to the dotted string ``"2.5.4.42"``. This OID is typically seen
521 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600522
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600523.. data:: OID_TITLE
524
Paul Kehrer4bb46492015-02-07 16:59:14 -0600525 Corresponds to the dotted string ``"2.5.4.12"``. This OID is typically seen
526 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600527
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600528.. data:: OID_GENERATION_QUALIFIER
529
Paul Kehrer4bb46492015-02-07 16:59:14 -0600530 Corresponds to the dotted string ``"2.5.4.44"``. This OID is typically seen
531 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600532
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600533.. data:: OID_DN_QUALIFIER
534
Paul Kehrerfb5ac9e2015-02-07 16:29:37 -0600535 Corresponds to the dotted string ``"2.5.4.46"``. This specifies
536 disambiguating information to add to the relative distinguished name of an
Paul Kehrer4bb46492015-02-07 16:59:14 -0600537 entry. See :rfc:`2256`. This OID is typically seen in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600538
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600539.. data:: OID_PSEUDONYM
540
Paul Kehrer4bb46492015-02-07 16:59:14 -0600541 Corresponds to the dotted string ``"2.5.4.65"``. This OID is typically seen
542 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600543
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600544.. data:: OID_DOMAIN_COMPONENT
545
Paul Kehrerfb5ac9e2015-02-07 16:29:37 -0600546 Corresponds to the dotted string ``"0.9.2342.19200300.100.1.25"``. A string
Paul Kehrer4bb46492015-02-07 16:59:14 -0600547 holding one component of a domain name. See :rfc:`4519`. This OID is
548 typically seen in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600549
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600550.. data:: OID_EMAIL_ADDRESS
551
Paul Kehrer4bb46492015-02-07 16:59:14 -0600552 Corresponds to the dotted string ``"1.2.840.113549.1.9.1"``. This OID is
553 typically seen in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600554
Paul Kehrer56da2a52015-02-11 23:35:07 -0600555Signature Algorithm OIDs
556~~~~~~~~~~~~~~~~~~~~~~~~
557
Paul Kehrer1a7ba872015-02-19 18:09:05 -0600558.. data:: OID_RSA_WITH_MD5
Paul Kehrer56da2a52015-02-11 23:35:07 -0600559
560 Corresponds to the dotted string ``"1.2.840.113549.1.1.4"``. This is
561 an MD5 digest signed by an RSA key.
562
Paul Kehrer1a7ba872015-02-19 18:09:05 -0600563.. data:: OID_RSA_WITH_SHA1
Paul Kehrer56da2a52015-02-11 23:35:07 -0600564
565 Corresponds to the dotted string ``"1.2.840.113549.1.1.5"``. This is
566 a SHA1 digest signed by an RSA key.
567
Paul Kehrer1a7ba872015-02-19 18:09:05 -0600568.. data:: OID_RSA_WITH_SHA224
Paul Kehrer56da2a52015-02-11 23:35:07 -0600569
570 Corresponds to the dotted string ``"1.2.840.113549.1.1.14"``. This is
571 a SHA224 digest signed by an RSA key.
572
Paul Kehrer1a7ba872015-02-19 18:09:05 -0600573.. data:: OID_RSA_WITH_SHA256
Paul Kehrer56da2a52015-02-11 23:35:07 -0600574
575 Corresponds to the dotted string ``"1.2.840.113549.1.1.11"``. This is
576 a SHA256 digest signed by an RSA key.
577
Paul Kehrer1a7ba872015-02-19 18:09:05 -0600578.. data:: OID_RSA_WITH_SHA384
Paul Kehrer56da2a52015-02-11 23:35:07 -0600579
580 Corresponds to the dotted string ``"1.2.840.113549.1.1.12"``. This is
581 a SHA384 digest signed by an RSA key.
582
Paul Kehrer1a7ba872015-02-19 18:09:05 -0600583.. data:: OID_RSA_WITH_SHA512
Paul Kehrer56da2a52015-02-11 23:35:07 -0600584
585 Corresponds to the dotted string ``"1.2.840.113549.1.1.13"``. This is
586 a SHA512 digest signed by an RSA key.
587
588.. data:: OID_ECDSA_WITH_SHA224
589
590 Corresponds to the dotted string ``"1.2.840.10045.4.3.1"``. This is
591 a SHA224 digest signed by an ECDSA key.
592
593.. data:: OID_ECDSA_WITH_SHA256
594
595 Corresponds to the dotted string ``"1.2.840.10045.4.3.2"``. This is
596 a SHA256 digest signed by an ECDSA key.
597
598.. data:: OID_ECDSA_WITH_SHA384
599
600 Corresponds to the dotted string ``"1.2.840.10045.4.3.3"``. This is
601 a SHA384 digest signed by an ECDSA key.
602
603.. data:: OID_ECDSA_WITH_SHA512
604
605 Corresponds to the dotted string ``"1.2.840.10045.4.3.4"``. This is
606 a SHA512 digest signed by an ECDSA key.
607
608.. data:: OID_DSA_WITH_SHA1
609
610 Corresponds to the dotted string ``"1.2.840.10040.4.3"``. This is
611 a SHA1 digest signed by a DSA key.
612
613.. data:: OID_DSA_WITH_SHA224
614
615 Corresponds to the dotted string ``"2.16.840.1.101.3.4.3.1"``. This is
616 a SHA224 digest signed by a DSA key.
617
618.. data:: OID_DSA_WITH_SHA256
619
Paul Kehrerfbb7ac82015-03-16 19:26:29 -0500620 Corresponds to the dotted string ``"2.16.840.1.101.3.4.3.2"``. This is
Paul Kehrer56da2a52015-02-11 23:35:07 -0600621 a SHA256 digest signed by a DSA key.
622
Paul Kehrer5553d572015-03-23 21:08:01 -0500623.. _extension_oids:
624
Paul Kehrer2bb94642015-03-21 09:54:17 -0500625Extension OIDs
626~~~~~~~~~~~~~~
627
628.. data:: OID_BASIC_CONSTRAINTS
629
630 Corresponds to the dotted string ``"2.5.29.19"``. The identifier for the
Paul Kehrer611d3d32015-03-22 13:31:18 -0500631 :class:`BasicConstraints` extension type.
Paul Kehrer2bb94642015-03-21 09:54:17 -0500632
Paul Kehrer56da2a52015-02-11 23:35:07 -0600633
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600634Exceptions
635~~~~~~~~~~
636
Paul Kehrere76cd272014-12-14 19:00:51 -0600637.. class:: InvalidVersion
Paul Kehrera68fd332014-11-27 07:08:40 -1000638
639 This is raised when an X.509 certificate has an invalid version number.
Paul Kehrer016e08a2014-11-26 09:41:18 -1000640
Paul Kehrerd5cccf72014-12-15 17:20:33 -0600641 .. attribute:: parsed_version
642
Paul Kehrerbbffc402014-12-17 13:33:55 -0600643 :type: int
644
645 Returns the raw version that was parsed from the certificate.
Paul Kehrerd5cccf72014-12-15 17:20:33 -0600646
Paul Kehrerfbb7ac82015-03-16 19:26:29 -0500647.. class:: DuplicateExtension
648
649 This is raised when more than one X.509 extension of the same type is
650 found within a certificate.
651
652 .. attribute:: oid
653
654 :type: :class:`ObjectIdentifier`
655
656 Returns the OID.
657
658.. class:: UnsupportedExtension
659
660 This is raised when a certificate contains an unsupported extension type.
661
662 .. attribute:: oid
663
664 :type: :class:`ObjectIdentifier`
665
666 Returns the OID.
667
Paul Kehrerfa56a232015-03-17 13:14:03 -0500668.. class:: ExtensionNotFound
669
670 This is raised when calling :meth:`Extensions.get_extension_for_oid` with
671 an extension OID that is not present in the certificate.
672
673 .. attribute:: oid
674
675 :type: :class:`ObjectIdentifier`
676
677 Returns the OID.
678
Paul Kehrer016e08a2014-11-26 09:41:18 -1000679
680.. _`public key infrastructure`: https://en.wikipedia.org/wiki/Public_key_infrastructure
Paul Kehrera68fd332014-11-27 07:08:40 -1000681.. _`TLS`: https://en.wikipedia.org/wiki/Transport_Layer_Security