blob: 5278d07b2c445735b17596b293288c1723d61ee0 [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
76 >>> from cryptography.x509 import load_pem_x509_certificate
77 >>> from cryptography.hazmat.backends import default_backend
78 >>> cert = load_pem_x509_certificate(pem_data, default_backend())
79 >>> 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 Kehrerb2de9482014-12-11 14:54:48 -060099 .. method:: fingerprint(algorithm)
100
101 :param algorithm: The
102 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
103 that will be used to generate the fingerprint.
104
105 :return bytes: The fingerprint using the supplied hash algorithm as
106 bytes.
107
108 .. attribute:: serial
109
110 :type: int
111
112 The serial as a Python integer.
113
114 .. method:: public_key()
115
116 :type:
117 :class:`~cryptography.hazmat.primitives.interfaces.RSAPublicKey` or
118 :class:`~cryptography.hazmat.primitives.interfaces.DSAPublicKey` or
119 :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurvePublicKey`
120
121 The public key associated with the certificate.
122
123 .. attribute:: not_valid_before
124
125 :type: :class:`datetime.datetime`
126
127 A naïve datetime representing the beginning of the validity period for the
128 certificate in UTC. This value is inclusive.
129
130 .. attribute:: not_valid_after
131
132 :type: :class:`datetime.datetime`
133
134 A naïve datetime representing the end of the validity period for the
135 certificate in UTC. This value is inclusive.
136
137
Paul Kehrere76cd272014-12-14 19:00:51 -0600138.. class:: Version
Paul Kehrer016e08a2014-11-26 09:41:18 -1000139
140 .. versionadded:: 0.7
141
142 An enumeration for X.509 versions.
143
144 .. attribute:: v1
145
146 For version 1 X.509 certificates.
147
148 .. attribute:: v3
149
150 For version 3 X.509 certificates.
151
Paul Kehrere76cd272014-12-14 19:00:51 -0600152.. class:: InvalidVersion
Paul Kehrera68fd332014-11-27 07:08:40 -1000153
154 This is raised when an X.509 certificate has an invalid version number.
Paul Kehrer016e08a2014-11-26 09:41:18 -1000155
156
157.. _`public key infrastructure`: https://en.wikipedia.org/wiki/Public_key_infrastructure
Paul Kehrera68fd332014-11-27 07:08:40 -1000158.. _`TLS`: https://en.wikipedia.org/wiki/Transport_Layer_Security