Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 1 | .. hazmat:: |
| 2 | |
| 3 | X.509 |
| 4 | ===== |
| 5 | |
Paul Kehrer | a9d78c1 | 2014-11-26 10:59:03 -1000 | [diff] [blame] | 6 | .. currentmodule:: cryptography.x509 |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 7 | |
| 8 | X.509 is an ITU-T standard for a `public key infrastructure`_. X.509v3 is |
Paul Kehrer | a68fd33 | 2014-11-27 07:08:40 -1000 | [diff] [blame] | 9 | defined in :rfc:`5280` (which obsoletes :rfc:`2459` and :rfc:`3280`). X.509 |
| 10 | certificates are commonly used in protocols like `TLS`_. |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 11 | |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 12 | |
| 13 | Loading Certificates |
| 14 | ~~~~~~~~~~~~~~~~~~~~ |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 15 | |
| 16 | .. function:: load_pem_x509_certificate(data, backend) |
| 17 | |
| 18 | .. versionadded:: 0.7 |
| 19 | |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 20 | Deserialize a certificate from PEM encoded data. PEM certificates are |
| 21 | base64 decoded and have delimiters that look like |
| 22 | ``-----BEGIN CERTIFICATE-----``. |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 23 | |
| 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 Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 30 | :returns: An instance of :class:`~cryptography.x509.Certificate`. |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 31 | |
| 32 | .. function:: load_der_x509_certificate(data, backend) |
| 33 | |
| 34 | .. versionadded:: 0.7 |
| 35 | |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 36 | Deserialize a certificate from DER encoded data. DER is a binary format |
Paul Kehrer | 92aac38 | 2014-12-15 16:25:28 -0600 | [diff] [blame] | 37 | and is commonly found in files with the ``.cer`` extension (although file |
| 38 | extensions are not a guarantee of encoding type). |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 39 | |
| 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 Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 46 | :returns: An instance of :class:`~cryptography.x509.Certificate`. |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 47 | |
| 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 Kehrer | cc8a26e | 2014-12-16 12:40:16 -0600 | [diff] [blame] | 76 | >>> from cryptography import x509 |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 77 | >>> from cryptography.hazmat.backends import default_backend |
Paul Kehrer | cc8a26e | 2014-12-16 12:40:16 -0600 | [diff] [blame] | 78 | >>> cert = x509.load_pem_x509_certificate(pem_data, default_backend()) |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 79 | >>> cert.serial |
| 80 | 2 |
| 81 | |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 82 | X.509 Certificate Object |
| 83 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 84 | |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 85 | .. class:: Certificate |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 86 | |
| 87 | .. versionadded:: 0.7 |
| 88 | |
| 89 | .. attribute:: version |
| 90 | |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 91 | :type: :class:`~cryptography.x509.Version` |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 92 | |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 93 | 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 Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 95 | |
Alex Gaynor | 89c4dc8 | 2014-12-16 16:49:33 -0800 | [diff] [blame^] | 96 | :raises cryptography.x509.InvalidVersion: If the version in the |
| 97 | certificate is not a known :class:`X.509 version |
Paul Kehrer | 92aac38 | 2014-12-15 16:25:28 -0600 | [diff] [blame] | 98 | |
Paul Kehrer | cc8a26e | 2014-12-16 12:40:16 -0600 | [diff] [blame] | 99 | .. doctest:: |
| 100 | |
| 101 | >>> cert.version |
| 102 | <Version.v3: 2> |
| 103 | |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 104 | .. 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 Kehrer | cc8a26e | 2014-12-16 12:40:16 -0600 | [diff] [blame] | 113 | .. doctest:: |
| 114 | |
| 115 | >>> from cryptography.hazmat.primitives import hashes |
| 116 | >>> cert.fingerprint(hashes.SHA256()) |
Paul Kehrer | 78a8150 | 2014-12-16 14:47:52 -0600 | [diff] [blame] | 117 | '\x86\xd2\x187Gc\xfc\xe7}[+E9\x8d\xb4\x8f\x10\xe5S\xda\x18u\xbe}a\x03\x08[\xac\xa04?' |
Paul Kehrer | cc8a26e | 2014-12-16 12:40:16 -0600 | [diff] [blame] | 118 | |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 119 | .. attribute:: serial |
| 120 | |
| 121 | :type: int |
| 122 | |
| 123 | The serial as a Python integer. |
| 124 | |
Paul Kehrer | cc8a26e | 2014-12-16 12:40:16 -0600 | [diff] [blame] | 125 | .. doctest:: |
| 126 | |
| 127 | >>> cert.serial |
| 128 | 2 |
| 129 | |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 130 | .. 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 Kehrer | cc8a26e | 2014-12-16 12:40:16 -0600 | [diff] [blame] | 139 | .. 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 Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 146 | .. attribute:: not_valid_before |
| 147 | |
| 148 | :type: :class:`datetime.datetime` |
| 149 | |
Paul Kehrer | 78a8150 | 2014-12-16 14:47:52 -0600 | [diff] [blame] | 150 | A naïve datetime representing the beginning of the validity period for |
| 151 | the certificate in UTC. This value is inclusive. |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 152 | |
Paul Kehrer | cc8a26e | 2014-12-16 12:40:16 -0600 | [diff] [blame] | 153 | .. doctest:: |
| 154 | |
| 155 | >>> cert.not_valid_before |
| 156 | datetime.datetime(2010, 1, 1, 8, 30) |
| 157 | |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 158 | .. attribute:: not_valid_after |
| 159 | |
| 160 | :type: :class:`datetime.datetime` |
| 161 | |
Paul Kehrer | 78a8150 | 2014-12-16 14:47:52 -0600 | [diff] [blame] | 162 | A naïve datetime representing the end of the validity period for the |
| 163 | certificate in UTC. This value is inclusive. |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 164 | |
Paul Kehrer | cc8a26e | 2014-12-16 12:40:16 -0600 | [diff] [blame] | 165 | .. doctest:: |
| 166 | |
| 167 | >>> cert.not_valid_after |
| 168 | datetime.datetime(2030, 12, 31, 8, 30) |
| 169 | |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 170 | |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 171 | .. class:: Version |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 172 | |
| 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 Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 185 | .. class:: InvalidVersion |
Paul Kehrer | a68fd33 | 2014-11-27 07:08:40 -1000 | [diff] [blame] | 186 | |
| 187 | This is raised when an X.509 certificate has an invalid version number. |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 188 | |
Paul Kehrer | d5cccf7 | 2014-12-15 17:20:33 -0600 | [diff] [blame] | 189 | .. attribute:: parsed_version |
| 190 | |
| 191 | Returns the version that was parsed from the certificate. |
| 192 | |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 193 | |
| 194 | .. _`public key infrastructure`: https://en.wikipedia.org/wiki/Public_key_infrastructure |
Paul Kehrer | a68fd33 | 2014-11-27 07:08:40 -1000 | [diff] [blame] | 195 | .. _`TLS`: https://en.wikipedia.org/wiki/Transport_Layer_Security |