blob: 2ff12902abc6d1a564a17213e07f6fc3fc4ec269 [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 Kehrerdc480ad2015-02-23 12:14:54 -060080Loading Certificate Requests
81~~~~~~~~~~~~~~~~~~~~~~~~~~~~
82
83.. function:: load_pem_x509_request(data, backend)
84
85 .. versionadded:: 0.9
86
87 Deserialize a certificate request from PEM encoded data. PEM requests are
88 base64 decoded and have delimiters that look like
89 ``-----BEGIN CERTIFICATE REQUEST-----``. This is also known as PKCS#10
90 format.
91
92 :param bytes data: The PEM encoded request data.
93
94 :param backend: A backend supporting the
95 :class:`~cryptography.hazmat.backends.interfaces.X509Backend`
96 interface.
97
98 :returns: An instance of :class:`~cryptography.x509.Request`.
99
100.. testsetup::
101
102 pem_req_data = b"""
103 -----BEGIN CERTIFICATE REQUEST-----
104 MIIC0zCCAbsCAQAwWTELMAkGA1UEBhMCVVMxETAPBgNVBAgMCElsbGlub2lzMRAw
105 DgYDVQQHDAdDaGljYWdvMREwDwYDVQQKDAhyNTA5IExMQzESMBAGA1UEAwwJaGVs
106 bG8uY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqhZx+Mo9VRd9
107 vsnWWa6NBCws21rZ0+1B/JGgB4hDsZS7iDE4Bj5z4idheFRtl8bBbdjPknq7BfoF
108 8v15Zq/Zv7i2xMSDL+LUrTBZezRd4bRTGqCm6YJ5EYkhqdcqeZleHCFImguHoq1J
109 Fh0+kObQrTHXw3ZP57a3o1IvyIUA3nNoCBL0QQhwBXaDXOojMKNR+bqB5ve8GS1y
110 Elr0AM/+cJsfaIahNQUgFKx3Eu3GeEOMKYOAG1lycgdQdmTUybLrT3U7vkClTseM
111 xHg1r5En7ALjONIhqRuq3rddYahrP8HXozb3zUy3cJ7P6IeaosuvNzvMXOX9P6HD
112 Ha9urDAJ1wIDAQABoDUwMwYJKoZIhvcNAQkOMSYwJDAiBgNVHREEGzAZggl3b3Js
113 ZC5jb22CDHdoYXRldmVyLmNvbTANBgkqhkiG9w0BAQUFAAOCAQEAS4Ro6h+z52SK
114 YSLCYARpnEu/rmh4jdqndt8naqcNb6uLx9mlKZ2W9on9XDjnSdQD9q+ZP5aZfESw
115 R0+rJhW9ZrNa/g1pt6M24ihclHYDAxYMWxT1z/TXXGM3TmZZ6gfYlNE1kkBuODHa
116 UYsR/1Ht1E1EsmmUimt2n+zQR2K8T9Coa+boaUW/GsTEuz1aaJAkj5ZvTDiIhRG4
117 AOCqFZOLAQmCCNgJnnspD9hDz/Ons085LF5wnYjN4/Nsk5tS6AGs3xjZ3jPoOGGn
118 82WQ9m4dBGoVDZXsobVTaN592JEYwN5iu72zRn7Einb4V4H5y3yD2dD4yWPlt4pk
119 5wFkeYsZEA==
120 -----END CERTIFICATE REQUEST-----
121 """.strip()
122
123.. doctest::
124
125 >>> from cryptography import x509
126 >>> from cryptography.hazmat.backends import default_backend
127 >>> from cryptography.hazmat.primitives import hashes
128 >>> request = x509.load_pem_x509_request(pem_req_data, default_backend())
129 >>> isinstance(request.signature_hash_algorithm, hashes.SHA1)
130 True
131
Paul Kehrere76cd272014-12-14 19:00:51 -0600132X.509 Certificate Object
133~~~~~~~~~~~~~~~~~~~~~~~~
Paul Kehrerb2de9482014-12-11 14:54:48 -0600134
Paul Kehrere76cd272014-12-14 19:00:51 -0600135.. class:: Certificate
Paul Kehrerb2de9482014-12-11 14:54:48 -0600136
137 .. versionadded:: 0.7
138
139 .. attribute:: version
140
Paul Kehrere76cd272014-12-14 19:00:51 -0600141 :type: :class:`~cryptography.x509.Version`
Paul Kehrerb2de9482014-12-11 14:54:48 -0600142
Paul Kehrere76cd272014-12-14 19:00:51 -0600143 The certificate version as an enumeration. Version 3 certificates are
144 the latest version and also the only type you should see in practice.
Paul Kehrerb2de9482014-12-11 14:54:48 -0600145
Alex Gaynor89c4dc82014-12-16 16:49:33 -0800146 :raises cryptography.x509.InvalidVersion: If the version in the
Alex Gaynor6d7ab4c2014-12-16 16:50:33 -0800147 certificate is not a known
148 :class:`X.509 version <cryptography.x509.Version>`.
Paul Kehrer92aac382014-12-15 16:25:28 -0600149
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600150 .. doctest::
151
152 >>> cert.version
153 <Version.v3: 2>
154
Paul Kehrerb2de9482014-12-11 14:54:48 -0600155 .. method:: fingerprint(algorithm)
156
157 :param algorithm: The
Paul Kehrer601278a2015-02-12 12:51:00 -0600158 :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`
Paul Kehrerb2de9482014-12-11 14:54:48 -0600159 that will be used to generate the fingerprint.
160
161 :return bytes: The fingerprint using the supplied hash algorithm as
162 bytes.
163
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600164 .. doctest::
165
166 >>> from cryptography.hazmat.primitives import hashes
167 >>> cert.fingerprint(hashes.SHA256())
Paul Kehrer78a81502014-12-16 14:47:52 -0600168 '\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 -0600169
Paul Kehrerb2de9482014-12-11 14:54:48 -0600170 .. attribute:: serial
171
172 :type: int
173
174 The serial as a Python integer.
175
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600176 .. doctest::
177
178 >>> cert.serial
179 2
180
Paul Kehrerb2de9482014-12-11 14:54:48 -0600181 .. method:: public_key()
182
183 :type:
Alex Stapletonf79c2312014-12-30 12:50:14 +0000184 :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey` or
Paul Kehrer45efdbc2015-02-12 10:58:22 -0600185 :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPublicKey` or
186 :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey`
Paul Kehrerb2de9482014-12-11 14:54:48 -0600187
188 The public key associated with the certificate.
189
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600190 .. doctest::
191
Alex Stapletonf79c2312014-12-30 12:50:14 +0000192 >>> from cryptography.hazmat.primitives.asymmetric import rsa
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600193 >>> public_key = cert.public_key()
Alex Stapletonf79c2312014-12-30 12:50:14 +0000194 >>> isinstance(public_key, rsa.RSAPublicKey)
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600195 True
196
Paul Kehrerb2de9482014-12-11 14:54:48 -0600197 .. attribute:: not_valid_before
198
199 :type: :class:`datetime.datetime`
200
Paul Kehrer78a81502014-12-16 14:47:52 -0600201 A naïve datetime representing the beginning of the validity period for
202 the certificate in UTC. This value is inclusive.
Paul Kehrerb2de9482014-12-11 14:54:48 -0600203
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600204 .. doctest::
205
206 >>> cert.not_valid_before
207 datetime.datetime(2010, 1, 1, 8, 30)
208
Paul Kehrerb2de9482014-12-11 14:54:48 -0600209 .. attribute:: not_valid_after
210
211 :type: :class:`datetime.datetime`
212
Paul Kehrer78a81502014-12-16 14:47:52 -0600213 A naïve datetime representing the end of the validity period for the
214 certificate in UTC. This value is inclusive.
Paul Kehrerb2de9482014-12-11 14:54:48 -0600215
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600216 .. doctest::
217
218 >>> cert.not_valid_after
219 datetime.datetime(2030, 12, 31, 8, 30)
220
Paul Kehrer719d5362015-01-01 20:03:52 -0600221 .. attribute:: issuer
222
223 .. versionadded:: 0.8
224
225 :type: :class:`Name`
226
227 The :class:`Name` of the issuer.
228
229 .. attribute:: subject
230
231 .. versionadded:: 0.8
232
233 :type: :class:`Name`
234
235 The :class:`Name` of the subject.
236
Paul Kehrer8802a5b2015-02-13 12:06:57 -0600237 .. attribute:: signature_hash_algorithm
Paul Kehrer56da2a52015-02-11 23:35:07 -0600238
Paul Kehrer8802a5b2015-02-13 12:06:57 -0600239 :type: :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`
Paul Kehrer56da2a52015-02-11 23:35:07 -0600240
Paul Kehrere612ec72015-02-16 14:33:35 -0600241 Returns the
Paul Kehrer71d40c62015-02-19 08:21:04 -0600242 :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` which
Paul Kehrer1a7ba872015-02-19 18:09:05 -0600243 was used in signing this certificate.
Paul Kehrer56da2a52015-02-11 23:35:07 -0600244
245 .. doctest::
246
Paul Kehrer8802a5b2015-02-13 12:06:57 -0600247 >>> from cryptography.hazmat.primitives import hashes
248 >>> isinstance(cert.signature_hash_algorithm, hashes.SHA256)
249 True
Paul Kehrer719d5362015-01-01 20:03:52 -0600250
Paul Kehrerfbb7ac82015-03-16 19:26:29 -0500251 .. attribute:: extensions
252
253 :type: :class:`Extensions`
254
255 The extensions encoded in the certificate.
256
257 :raises cryptography.x509.DuplicateExtension: If more than one
258 extension of the same type is found within the certificate.
259
Paul Kehrerfa56a232015-03-17 13:14:03 -0500260 .. doctest::
261
262 >>> for ext in cert.extensions:
263 ... print(ext)
264 <Extension(oid=<ObjectIdentifier(oid=2.5.29.19, name=basicConstraints)>, critical=True, value=<BasicConstraints(ca=True, path_length=None)>)>
265
Paul Kehrerdc480ad2015-02-23 12:14:54 -0600266X.509 Certificate Request Object
267~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
268
269.. class:: Request
270
271 .. versionadded:: 0.9
272
273 .. method:: public_key()
274
275 :type:
276 :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey` or
277 :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPublicKey` or
278 :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey`
279
280 The public key associated with the request.
281
282 .. doctest::
283
284 >>> from cryptography.hazmat.primitives.asymmetric import rsa
285 >>> public_key = request.public_key()
286 >>> isinstance(public_key, rsa.RSAPublicKey)
287 True
288
289 .. attribute:: subject
290
291 :type: :class:`Name`
292
293 The :class:`Name` of the subject.
294
295 .. attribute:: signature_hash_algorithm
296
297 :type: :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`
298
299 Returns the
300 :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` which
301 was used in signing this request.
302
303 .. doctest::
304
305 >>> from cryptography.hazmat.primitives import hashes
306 >>> isinstance(request.signature_hash_algorithm, hashes.SHA1)
307 True
308
Paul Kehrer719d5362015-01-01 20:03:52 -0600309.. class:: Name
310
311 .. versionadded:: 0.8
312
Paul Kehrer53d8d492015-02-13 18:47:30 -0600313 An X509 Name is an ordered list of attributes. The object is iterable to
Paul Kehrerd21596e2015-02-14 09:17:26 -0600314 get every attribute or you can use :meth:`Name.get_attributes_for_oid` to
Paul Kehrer719d5362015-01-01 20:03:52 -0600315 obtain the specific type you want. Names are sometimes represented as a
Paul Kehrer53d8d492015-02-13 18:47:30 -0600316 slash or comma delimited string (e.g. ``/CN=mydomain.com/O=My Org/C=US`` or
317 ``CN=mydomain.com, O=My Org, C=US``).
Paul Kehrer719d5362015-01-01 20:03:52 -0600318
Paul Kehrer53d8d492015-02-13 18:47:30 -0600319 .. doctest::
Paul Kehrer719d5362015-01-01 20:03:52 -0600320
Paul Kehrer8b21a4a2015-02-14 07:56:36 -0600321 >>> len(cert.subject)
Paul Kehrer53d8d492015-02-13 18:47:30 -0600322 3
Paul Kehrer8b21a4a2015-02-14 07:56:36 -0600323 >>> for attribute in cert.subject:
324 ... print(attribute)
325 <NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.6, name=countryName)>, value=u'US')>
326 <NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.10, name=organizationName)>, value=u'Test Certificates 2011')>
327 <NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)>, value=u'Good CA')>
Paul Kehrer719d5362015-01-01 20:03:52 -0600328
Paul Kehrere901d642015-02-11 18:50:58 -0600329 .. method:: get_attributes_for_oid(oid)
Paul Kehrer719d5362015-01-01 20:03:52 -0600330
Paul Kehrere901d642015-02-11 18:50:58 -0600331 :param oid: An :class:`ObjectIdentifier` instance.
Paul Kehrer719d5362015-01-01 20:03:52 -0600332
Paul Kehrere901d642015-02-11 18:50:58 -0600333 :returns: A list of :class:`NameAttribute` instances that match the
334 OID provided. If nothing matches an empty list will be returned.
Paul Kehrer719d5362015-01-01 20:03:52 -0600335
336 .. doctest::
337
Paul Kehrere901d642015-02-11 18:50:58 -0600338 >>> cert.subject.get_attributes_for_oid(x509.OID_COMMON_NAME)
339 [<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)>, value=u'Good CA')>]
Paul Kehrerb2de9482014-12-11 14:54:48 -0600340
Paul Kehrere76cd272014-12-14 19:00:51 -0600341.. class:: Version
Paul Kehrer016e08a2014-11-26 09:41:18 -1000342
343 .. versionadded:: 0.7
344
345 An enumeration for X.509 versions.
346
347 .. attribute:: v1
348
349 For version 1 X.509 certificates.
350
351 .. attribute:: v3
352
353 For version 3 X.509 certificates.
354
Paul Kehrer806bfb22015-02-02 17:05:24 -0600355.. class:: NameAttribute
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600356
357 .. versionadded:: 0.8
358
Paul Kehrer834d22f2015-02-06 11:01:07 -0600359 An X.509 name consists of a list of NameAttribute instances.
Paul Kehrer5b0a8d62015-01-30 20:05:55 -0600360
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600361 .. attribute:: oid
362
363 :type: :class:`ObjectIdentifier`
364
365 The attribute OID.
366
367 .. attribute:: value
368
Paul Kehrerd5852cb2015-01-30 08:25:23 -0600369 :type: :term:`text`
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600370
371 The value of the attribute.
372
373.. class:: ObjectIdentifier
374
375 .. versionadded:: 0.8
376
Paul Kehrer5b0a8d62015-01-30 20:05:55 -0600377 Object identifiers (frequently seen abbreviated as OID) identify the type
Paul Kehrer806bfb22015-02-02 17:05:24 -0600378 of a value (see: :class:`NameAttribute`).
Paul Kehrer5b0a8d62015-01-30 20:05:55 -0600379
Paul Kehrerd44f9a62015-02-04 14:47:34 -0600380 .. attribute:: dotted_string
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600381
382 :type: :class:`str`
383
Paul Kehrerfedf4f42015-02-06 11:22:07 -0600384 The dotted string value of the OID (e.g. ``"2.5.4.3"``)
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600385
Paul Kehrer8cf26422015-03-21 09:50:24 -0500386X.509 Extensions
387~~~~~~~~~~~~~~~~
388
Paul Kehrerfbb7ac82015-03-16 19:26:29 -0500389.. class:: Extensions
390
391 .. versionadded:: 0.9
392
393 An X.509 Extensions instance is an ordered list of extensions. The object
394 is iterable to get every extension.
395
Paul Kehrerfa56a232015-03-17 13:14:03 -0500396 .. method:: get_extension_for_oid(oid)
397
398 :param oid: An :class:`ObjectIdentifier` instance.
399
400 :returns: An instance of the extension class.
401
402 :raises cryptography.x509.ExtensionNotFound: If the certificate does
403 not have the extension requested.
404
405 :raises cryptography.x509.UnsupportedExtension: If the certificate
406 contains an extension that is not supported.
407
408 .. doctest::
409
410 >>> cert.extensions.get_extension_for_oid(x509.OID_BASIC_CONSTRAINTS)
411 <Extension(oid=<ObjectIdentifier(oid=2.5.29.19, name=basicConstraints)>, critical=True, value=<BasicConstraints(ca=True, path_length=None)>)>
412
Paul Kehrer8cf26422015-03-21 09:50:24 -0500413.. class:: Extension
414
415 .. versionadded:: 0.9
416
Paul Kehrer85894662015-03-22 13:19:31 -0500417 .. attribute:: oid
418
419 :type: :class:`ObjectIdentifier`
420
Paul Kehrer5553d572015-03-23 21:08:01 -0500421 The :ref:`extension OID <extension_oids>`.
Paul Kehrer8cf26422015-03-21 09:50:24 -0500422
423 .. attribute:: critical
424
425 :type: bool
426
Paul Kehrer58b75692015-03-22 23:24:58 -0500427 Determines whether a given extension is critical or not. :rfc:`5280`
428 requires that "A certificate-using system MUST reject the certificate
429 if it encounters a critical extension it does not recognize or a
430 critical extension that contains information that it cannot process".
Paul Kehrer8cf26422015-03-21 09:50:24 -0500431
Paul Kehrer85894662015-03-22 13:19:31 -0500432 .. attribute:: value
433
434 Returns an instance of the extension type corresponding to the OID.
435
Paul Kehrer8cf26422015-03-21 09:50:24 -0500436.. class:: BasicConstraints
437
438 .. versionadded:: 0.9
439
Paul Kehrer85894662015-03-22 13:19:31 -0500440 Basic constraints is an X.509 extension type that defines whether a given
Paul Kehrer8cf26422015-03-21 09:50:24 -0500441 certificate is allowed to sign additional certificates and what path
Paul Kehrer85894662015-03-22 13:19:31 -0500442 length restrictions may exist. It corresponds to
443 :data:`OID_BASIC_CONSTRAINTS`.
Paul Kehrer8cf26422015-03-21 09:50:24 -0500444
445 .. attribute:: ca
446
447 :type: bool
448
449 Whether the certificate can sign certificates.
450
451 .. attribute:: path_length
452
Paul Kehrerfd1444c2015-03-21 19:47:05 -0500453 :type: int or None
Paul Kehrer8cf26422015-03-21 09:50:24 -0500454
455 The maximum path length for certificates subordinate to this
456 certificate. This attribute only has meaning if ``ca`` is true.
457 If ``ca`` is true then a path length of None means there's no
458 restriction on the number of subordinate CAs in the certificate chain.
459 If it is zero or greater then that number defines the maximum length.
460 For example, a ``path_length`` of 1 means the certificate can sign a
461 subordinate CA, but the subordinate CA is not allowed to create
Paul Kehrerfd1444c2015-03-21 19:47:05 -0500462 subordinates with ``ca`` set to true.
Paul Kehrer8cf26422015-03-21 09:50:24 -0500463
464
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600465Object Identifiers
466~~~~~~~~~~~~~~~~~~
467
Paul Kehrer4bb46492015-02-07 16:59:14 -0600468X.509 elements are frequently identified by :class:`ObjectIdentifier`
469instances. The following common OIDs are available as constants.
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600470
Paul Kehrer56da2a52015-02-11 23:35:07 -0600471Name OIDs
472~~~~~~~~~
473
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600474.. data:: OID_COMMON_NAME
475
Paul Kehrerfb5ac9e2015-02-07 16:29:37 -0600476 Corresponds to the dotted string ``"2.5.4.3"``. Historically the domain
477 name would be encoded here for server certificates. :rfc:`2818` deprecates
478 this practice and names of that type should now be located in a
Paul Kehrer4bb46492015-02-07 16:59:14 -0600479 SubjectAlternativeName extension. This OID is typically seen in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600480
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600481.. data:: OID_COUNTRY_NAME
482
Paul Kehrer4bb46492015-02-07 16:59:14 -0600483 Corresponds to the dotted string ``"2.5.4.6"``. This OID is typically seen
484 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600485
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600486.. data:: OID_LOCALITY_NAME
487
Paul Kehrer4bb46492015-02-07 16:59:14 -0600488 Corresponds to the dotted string ``"2.5.4.7"``. This OID is typically seen
489 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600490
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600491.. data:: OID_STATE_OR_PROVINCE_NAME
492
Paul Kehrer4bb46492015-02-07 16:59:14 -0600493 Corresponds to the dotted string ``"2.5.4.8"``. This OID is typically seen
494 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600495
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600496.. data:: OID_ORGANIZATION_NAME
497
Paul Kehrer4bb46492015-02-07 16:59:14 -0600498 Corresponds to the dotted string ``"2.5.4.10"``. This OID is typically seen
499 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600500
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600501.. data:: OID_ORGANIZATIONAL_UNIT_NAME
502
Paul Kehrer4bb46492015-02-07 16:59:14 -0600503 Corresponds to the dotted string ``"2.5.4.11"``. This OID is typically seen
504 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600505
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600506.. data:: OID_SERIAL_NUMBER
507
Paul Kehrerfb5ac9e2015-02-07 16:29:37 -0600508 Corresponds to the dotted string ``"2.5.4.5"``. This is distinct from the
509 serial number of the certificate itself (which can be obtained with
Paul Kehrer4bb46492015-02-07 16:59:14 -0600510 :func:`Certificate.serial`). This OID is typically seen in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600511
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600512.. data:: OID_SURNAME
513
Paul Kehrer4bb46492015-02-07 16:59:14 -0600514 Corresponds to the dotted string ``"2.5.4.4"``. This OID is typically seen
515 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600516
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600517.. data:: OID_GIVEN_NAME
518
Paul Kehrer4bb46492015-02-07 16:59:14 -0600519 Corresponds to the dotted string ``"2.5.4.42"``. This OID is typically seen
520 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600521
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600522.. data:: OID_TITLE
523
Paul Kehrer4bb46492015-02-07 16:59:14 -0600524 Corresponds to the dotted string ``"2.5.4.12"``. This OID is typically seen
525 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600526
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600527.. data:: OID_GENERATION_QUALIFIER
528
Paul Kehrer4bb46492015-02-07 16:59:14 -0600529 Corresponds to the dotted string ``"2.5.4.44"``. This OID is typically seen
530 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600531
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600532.. data:: OID_DN_QUALIFIER
533
Paul Kehrerfb5ac9e2015-02-07 16:29:37 -0600534 Corresponds to the dotted string ``"2.5.4.46"``. This specifies
535 disambiguating information to add to the relative distinguished name of an
Paul Kehrer4bb46492015-02-07 16:59:14 -0600536 entry. See :rfc:`2256`. This OID is typically seen in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600537
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600538.. data:: OID_PSEUDONYM
539
Paul Kehrer4bb46492015-02-07 16:59:14 -0600540 Corresponds to the dotted string ``"2.5.4.65"``. This OID is typically seen
541 in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600542
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600543.. data:: OID_DOMAIN_COMPONENT
544
Paul Kehrerfb5ac9e2015-02-07 16:29:37 -0600545 Corresponds to the dotted string ``"0.9.2342.19200300.100.1.25"``. A string
Paul Kehrer4bb46492015-02-07 16:59:14 -0600546 holding one component of a domain name. See :rfc:`4519`. This OID is
547 typically seen in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600548
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600549.. data:: OID_EMAIL_ADDRESS
550
Paul Kehrer4bb46492015-02-07 16:59:14 -0600551 Corresponds to the dotted string ``"1.2.840.113549.1.9.1"``. This OID is
552 typically seen in X.509 names.
Paul Kehrer858b9b72015-02-05 09:50:31 -0600553
Paul Kehrer56da2a52015-02-11 23:35:07 -0600554Signature Algorithm OIDs
555~~~~~~~~~~~~~~~~~~~~~~~~
556
Paul Kehrer1a7ba872015-02-19 18:09:05 -0600557.. data:: OID_RSA_WITH_MD5
Paul Kehrer56da2a52015-02-11 23:35:07 -0600558
559 Corresponds to the dotted string ``"1.2.840.113549.1.1.4"``. This is
560 an MD5 digest signed by an RSA key.
561
Paul Kehrer1a7ba872015-02-19 18:09:05 -0600562.. data:: OID_RSA_WITH_SHA1
Paul Kehrer56da2a52015-02-11 23:35:07 -0600563
564 Corresponds to the dotted string ``"1.2.840.113549.1.1.5"``. This is
565 a SHA1 digest signed by an RSA key.
566
Paul Kehrer1a7ba872015-02-19 18:09:05 -0600567.. data:: OID_RSA_WITH_SHA224
Paul Kehrer56da2a52015-02-11 23:35:07 -0600568
569 Corresponds to the dotted string ``"1.2.840.113549.1.1.14"``. This is
570 a SHA224 digest signed by an RSA key.
571
Paul Kehrer1a7ba872015-02-19 18:09:05 -0600572.. data:: OID_RSA_WITH_SHA256
Paul Kehrer56da2a52015-02-11 23:35:07 -0600573
574 Corresponds to the dotted string ``"1.2.840.113549.1.1.11"``. This is
575 a SHA256 digest signed by an RSA key.
576
Paul Kehrer1a7ba872015-02-19 18:09:05 -0600577.. data:: OID_RSA_WITH_SHA384
Paul Kehrer56da2a52015-02-11 23:35:07 -0600578
579 Corresponds to the dotted string ``"1.2.840.113549.1.1.12"``. This is
580 a SHA384 digest signed by an RSA key.
581
Paul Kehrer1a7ba872015-02-19 18:09:05 -0600582.. data:: OID_RSA_WITH_SHA512
Paul Kehrer56da2a52015-02-11 23:35:07 -0600583
584 Corresponds to the dotted string ``"1.2.840.113549.1.1.13"``. This is
585 a SHA512 digest signed by an RSA key.
586
587.. data:: OID_ECDSA_WITH_SHA224
588
589 Corresponds to the dotted string ``"1.2.840.10045.4.3.1"``. This is
590 a SHA224 digest signed by an ECDSA key.
591
592.. data:: OID_ECDSA_WITH_SHA256
593
594 Corresponds to the dotted string ``"1.2.840.10045.4.3.2"``. This is
595 a SHA256 digest signed by an ECDSA key.
596
597.. data:: OID_ECDSA_WITH_SHA384
598
599 Corresponds to the dotted string ``"1.2.840.10045.4.3.3"``. This is
600 a SHA384 digest signed by an ECDSA key.
601
602.. data:: OID_ECDSA_WITH_SHA512
603
604 Corresponds to the dotted string ``"1.2.840.10045.4.3.4"``. This is
605 a SHA512 digest signed by an ECDSA key.
606
607.. data:: OID_DSA_WITH_SHA1
608
609 Corresponds to the dotted string ``"1.2.840.10040.4.3"``. This is
610 a SHA1 digest signed by a DSA key.
611
612.. data:: OID_DSA_WITH_SHA224
613
614 Corresponds to the dotted string ``"2.16.840.1.101.3.4.3.1"``. This is
615 a SHA224 digest signed by a DSA key.
616
617.. data:: OID_DSA_WITH_SHA256
618
Paul Kehrerfbb7ac82015-03-16 19:26:29 -0500619 Corresponds to the dotted string ``"2.16.840.1.101.3.4.3.2"``. This is
Paul Kehrer56da2a52015-02-11 23:35:07 -0600620 a SHA256 digest signed by a DSA key.
621
Paul Kehrer5553d572015-03-23 21:08:01 -0500622.. _extension_oids:
623
Paul Kehrer2bb94642015-03-21 09:54:17 -0500624Extension OIDs
625~~~~~~~~~~~~~~
626
627.. data:: OID_BASIC_CONSTRAINTS
628
629 Corresponds to the dotted string ``"2.5.29.19"``. The identifier for the
Paul Kehrer611d3d32015-03-22 13:31:18 -0500630 :class:`BasicConstraints` extension type.
Paul Kehrer2bb94642015-03-21 09:54:17 -0500631
Paul Kehrer56da2a52015-02-11 23:35:07 -0600632
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600633Exceptions
634~~~~~~~~~~
635
Paul Kehrere76cd272014-12-14 19:00:51 -0600636.. class:: InvalidVersion
Paul Kehrera68fd332014-11-27 07:08:40 -1000637
638 This is raised when an X.509 certificate has an invalid version number.
Paul Kehrer016e08a2014-11-26 09:41:18 -1000639
Paul Kehrerd5cccf72014-12-15 17:20:33 -0600640 .. attribute:: parsed_version
641
Paul Kehrerbbffc402014-12-17 13:33:55 -0600642 :type: int
643
644 Returns the raw version that was parsed from the certificate.
Paul Kehrerd5cccf72014-12-15 17:20:33 -0600645
Paul Kehrerfbb7ac82015-03-16 19:26:29 -0500646.. class:: DuplicateExtension
647
648 This is raised when more than one X.509 extension of the same type is
649 found within a certificate.
650
651 .. attribute:: oid
652
653 :type: :class:`ObjectIdentifier`
654
655 Returns the OID.
656
657.. class:: UnsupportedExtension
658
659 This is raised when a certificate contains an unsupported extension type.
660
661 .. attribute:: oid
662
663 :type: :class:`ObjectIdentifier`
664
665 Returns the OID.
666
Paul Kehrerfa56a232015-03-17 13:14:03 -0500667.. class:: ExtensionNotFound
668
669 This is raised when calling :meth:`Extensions.get_extension_for_oid` with
670 an extension OID that is not present in the certificate.
671
672 .. attribute:: oid
673
674 :type: :class:`ObjectIdentifier`
675
676 Returns the OID.
677
Paul Kehrer016e08a2014-11-26 09:41:18 -1000678
679.. _`public key infrastructure`: https://en.wikipedia.org/wiki/Public_key_infrastructure
Paul Kehrera68fd332014-11-27 07:08:40 -1000680.. _`TLS`: https://en.wikipedia.org/wiki/Transport_Layer_Security