blob: 50790b94748dd4fe776e0f8d07068279566a92de [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
Paul Kehrere76cd272014-12-14 19:00:51 -060020 Deserialize a certificate from PEM encoded data. PEM certificates are
21 base64 decoded and have delimiters that look like
22 ``-----BEGIN CERTIFICATE-----``.
Paul Kehrer016e08a2014-11-26 09:41:18 -100023
24 :param bytes data: The PEM encoded certificate data.
25
26 :param backend: A backend supporting the
27 :class:`~cryptography.hazmat.backends.interfaces.X509Backend`
28 interface.
29
Paul Kehrere76cd272014-12-14 19:00:51 -060030 :returns: An instance of :class:`~cryptography.x509.Certificate`.
Paul Kehrer016e08a2014-11-26 09:41:18 -100031
32.. function:: load_der_x509_certificate(data, backend)
33
34 .. versionadded:: 0.7
35
Paul Kehrere76cd272014-12-14 19:00:51 -060036 Deserialize a certificate from DER encoded data. DER is a binary format
Paul Kehrer92aac382014-12-15 16:25:28 -060037 and is commonly found in files with the ``.cer`` extension (although file
38 extensions are not a guarantee of encoding type).
Paul Kehrer016e08a2014-11-26 09:41:18 -100039
40 :param bytes data: The DER encoded certificate data.
41
42 :param backend: A backend supporting the
43 :class:`~cryptography.hazmat.backends.interfaces.X509Backend`
44 interface.
45
Paul Kehrere76cd272014-12-14 19:00:51 -060046 :returns: An instance of :class:`~cryptography.x509.Certificate`.
Paul Kehrer016e08a2014-11-26 09:41:18 -100047
48.. testsetup::
49
50 pem_data = b"""
51 -----BEGIN CERTIFICATE-----
52 MIIDfDCCAmSgAwIBAgIBAjANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJVUzEf
53 MB0GA1UEChMWVGVzdCBDZXJ0aWZpY2F0ZXMgMjAxMTEVMBMGA1UEAxMMVHJ1c3Qg
54 QW5jaG9yMB4XDTEwMDEwMTA4MzAwMFoXDTMwMTIzMTA4MzAwMFowQDELMAkGA1UE
55 BhMCVVMxHzAdBgNVBAoTFlRlc3QgQ2VydGlmaWNhdGVzIDIwMTExEDAOBgNVBAMT
56 B0dvb2QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCQWJpHYo37
57 Xfb7oJSPe+WvfTlzIG21WQ7MyMbGtK/m8mejCzR6c+f/pJhEH/OcDSMsXq8h5kXa
58 BGqWK+vSwD/Pzp5OYGptXmGPcthDtAwlrafkGOS4GqIJ8+k9XGKs+vQUXJKsOk47
59 RuzD6PZupq4s16xaLVqYbUC26UcY08GpnoLNHJZS/EmXw1ZZ3d4YZjNlpIpWFNHn
60 UGmdiGKXUPX/9H0fVjIAaQwjnGAbpgyCumWgzIwPpX+ElFOUr3z7BoVnFKhIXze+
61 VmQGSWxZxvWDUN90Ul0tLEpLgk3OVxUB4VUGuf15OJOpgo1xibINPmWt14Vda2N9
62 yrNKloJGZNqLAgMBAAGjfDB6MB8GA1UdIwQYMBaAFOR9X9FclYYILAWuvnW2ZafZ
63 XahmMB0GA1UdDgQWBBRYAYQkG7wrUpRKPaUQchRR9a86yTAOBgNVHQ8BAf8EBAMC
64 AQYwFwYDVR0gBBAwDjAMBgpghkgBZQMCATABMA8GA1UdEwEB/wQFMAMBAf8wDQYJ
65 KoZIhvcNAQELBQADggEBADWHlxbmdTXNwBL/llwhQqwnazK7CC2WsXBBqgNPWj7m
66 tvQ+aLG8/50Qc2Sun7o2VnwF9D18UUe8Gj3uPUYH+oSI1vDdyKcjmMbKRU4rk0eo
67 3UHNDXwqIVc9CQS9smyV+x1HCwL4TTrq+LXLKx/qVij0Yqk+UJfAtrg2jnYKXsCu
68 FMBQQnWCGrwa1g1TphRp/RmYHnMynYFmZrXtzFz+U9XEA7C+gPq4kqDI/iVfIT1s
69 6lBtdB50lrDVwl2oYfAvW/6sC2se2QleZidUmrziVNP4oEeXINokU6T6p//HM1FG
70 QYw2jOvpKcKtWCSAnegEbgsGYzATKjmPJPJ0npHFqzM=
71 -----END CERTIFICATE-----
72 """.strip()
73
74.. doctest::
75
Paul Kehrercc8a26e2014-12-16 12:40:16 -060076 >>> from cryptography import x509
Paul Kehrer016e08a2014-11-26 09:41:18 -100077 >>> from cryptography.hazmat.backends import default_backend
Paul Kehrercc8a26e2014-12-16 12:40:16 -060078 >>> cert = x509.load_pem_x509_certificate(pem_data, default_backend())
Paul Kehrer016e08a2014-11-26 09:41:18 -100079 >>> cert.serial
80 2
81
Paul Kehrere76cd272014-12-14 19:00:51 -060082X.509 Certificate Object
83~~~~~~~~~~~~~~~~~~~~~~~~
Paul Kehrerb2de9482014-12-11 14:54:48 -060084
Paul Kehrere76cd272014-12-14 19:00:51 -060085.. class:: Certificate
Paul Kehrerb2de9482014-12-11 14:54:48 -060086
87 .. versionadded:: 0.7
88
89 .. attribute:: version
90
Paul Kehrere76cd272014-12-14 19:00:51 -060091 :type: :class:`~cryptography.x509.Version`
Paul Kehrerb2de9482014-12-11 14:54:48 -060092
Paul Kehrere76cd272014-12-14 19:00:51 -060093 The certificate version as an enumeration. Version 3 certificates are
94 the latest version and also the only type you should see in practice.
Paul Kehrerb2de9482014-12-11 14:54:48 -060095
Paul Kehrer92aac382014-12-15 16:25:28 -060096 :raises cryptography.x509.InvalidVersion: If the version is
97 not valid.
98
Paul Kehrercc8a26e2014-12-16 12:40:16 -060099 .. doctest::
100
101 >>> cert.version
102 <Version.v3: 2>
103
Paul Kehrerb2de9482014-12-11 14:54:48 -0600104 .. method:: fingerprint(algorithm)
105
106 :param algorithm: The
107 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
108 that will be used to generate the fingerprint.
109
110 :return bytes: The fingerprint using the supplied hash algorithm as
111 bytes.
112
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600113 .. doctest::
114
115 >>> from cryptography.hazmat.primitives import hashes
116 >>> cert.fingerprint(hashes.SHA256())
Paul Kehrer78a81502014-12-16 14:47:52 -0600117 '\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 -0600118
Paul Kehrerb2de9482014-12-11 14:54:48 -0600119 .. attribute:: serial
120
121 :type: int
122
123 The serial as a Python integer.
124
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600125 .. doctest::
126
127 >>> cert.serial
128 2
129
Paul Kehrerb2de9482014-12-11 14:54:48 -0600130 .. method:: public_key()
131
132 :type:
133 :class:`~cryptography.hazmat.primitives.interfaces.RSAPublicKey` or
134 :class:`~cryptography.hazmat.primitives.interfaces.DSAPublicKey` or
135 :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurvePublicKey`
136
137 The public key associated with the certificate.
138
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600139 .. doctest::
140
141 >>> from cryptography.hazmat.primitives import interfaces
142 >>> public_key = cert.public_key()
143 >>> isinstance(public_key, interfaces.RSAPublicKey)
144 True
145
Paul Kehrerb2de9482014-12-11 14:54:48 -0600146 .. attribute:: not_valid_before
147
148 :type: :class:`datetime.datetime`
149
Paul Kehrer78a81502014-12-16 14:47:52 -0600150 A naïve datetime representing the beginning of the validity period for
151 the certificate in UTC. This value is inclusive.
Paul Kehrerb2de9482014-12-11 14:54:48 -0600152
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600153 .. doctest::
154
155 >>> cert.not_valid_before
156 datetime.datetime(2010, 1, 1, 8, 30)
157
Paul Kehrerb2de9482014-12-11 14:54:48 -0600158 .. attribute:: not_valid_after
159
160 :type: :class:`datetime.datetime`
161
Paul Kehrer78a81502014-12-16 14:47:52 -0600162 A naïve datetime representing the end of the validity period for the
163 certificate in UTC. This value is inclusive.
Paul Kehrerb2de9482014-12-11 14:54:48 -0600164
Paul Kehrercc8a26e2014-12-16 12:40:16 -0600165 .. doctest::
166
167 >>> cert.not_valid_after
168 datetime.datetime(2030, 12, 31, 8, 30)
169
Paul Kehrerb2de9482014-12-11 14:54:48 -0600170
Paul Kehrere76cd272014-12-14 19:00:51 -0600171.. class:: Version
Paul Kehrer016e08a2014-11-26 09:41:18 -1000172
173 .. versionadded:: 0.7
174
175 An enumeration for X.509 versions.
176
177 .. attribute:: v1
178
179 For version 1 X.509 certificates.
180
181 .. attribute:: v3
182
183 For version 3 X.509 certificates.
184
Paul Kehrere76cd272014-12-14 19:00:51 -0600185.. class:: InvalidVersion
Paul Kehrera68fd332014-11-27 07:08:40 -1000186
187 This is raised when an X.509 certificate has an invalid version number.
Paul Kehrer016e08a2014-11-26 09:41:18 -1000188
Paul Kehrerd5cccf72014-12-15 17:20:33 -0600189 .. attribute:: parsed_version
190
191 Returns the version that was parsed from the certificate.
192
Paul Kehrer016e08a2014-11-26 09:41:18 -1000193
194.. _`public key infrastructure`: https://en.wikipedia.org/wiki/Public_key_infrastructure
Paul Kehrera68fd332014-11-27 07:08:40 -1000195.. _`TLS`: https://en.wikipedia.org/wiki/Transport_Layer_Security