Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 1 | .. _openssl-crypto: |
| 2 | |
| 3 | :py:mod:`crypto` --- Generic cryptographic module |
| 4 | ================================================= |
| 5 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 6 | .. py:module:: OpenSSL.crypto |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 7 | :synopsis: Generic cryptographic module |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 8 | |
Paul Kehrer | dc996b1 | 2017-07-19 21:09:04 +0200 | [diff] [blame] | 9 | .. note:: |
| 10 | |
Hynek Schlawack | 8102128 | 2017-07-20 10:32:37 +0200 | [diff] [blame^] | 11 | `pyca/cryptography`_ is likely a better choice than using this module. |
| 12 | It contains a complete set of cryptographic primitives as well as a significantly better and more powerful X509 API. |
| 13 | If necessary you can convert to and from cryptography objects using the ``to_cryptography`` and ``from_cryptography`` methods on ``X509``, ``X509Req``, ``CRL``, and ``PKey``. |
| 14 | |
Paul Kehrer | dc996b1 | 2017-07-19 21:09:04 +0200 | [diff] [blame] | 15 | |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 16 | Elliptic curves |
| 17 | --------------- |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 18 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 19 | .. py:function:: get_elliptic_curves |
| 20 | |
| 21 | Return a set of objects representing the elliptic curves supported in the |
| 22 | OpenSSL build in use. |
| 23 | |
| 24 | The curve objects have a :py:class:`unicode` ``name`` attribute by which |
Jean-Paul Calderone | aaf516d | 2014-04-19 09:10:45 -0400 | [diff] [blame] | 25 | they identify themselves. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 26 | |
| 27 | The curve objects are useful as values for the argument accepted by |
Jean-Paul Calderone | 3b04e35 | 2014-04-19 09:29:10 -0400 | [diff] [blame] | 28 | :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be |
| 29 | used for ECDHE key exchange. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 30 | |
| 31 | |
Akihiro Yamazaki | b845044 | 2015-09-04 16:55:04 +0900 | [diff] [blame] | 32 | .. py:function:: get_elliptic_curve(name) |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 33 | |
Alex Gaynor | b6e92df | 2015-09-04 07:48:35 -0400 | [diff] [blame] | 34 | Return a single curve object selected by *name*. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 35 | |
| 36 | See :py:func:`get_elliptic_curves` for information about curve objects. |
| 37 | |
| 38 | If the named curve is not supported then :py:class:`ValueError` is raised. |
| 39 | |
| 40 | |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 41 | Serialization and deserialization |
| 42 | --------------------------------- |
| 43 | |
Cory Benfield | 4756925 | 2016-02-07 10:28:00 +0000 | [diff] [blame] | 44 | The following serialization functions take one of these constants to determine the format. |
Cory Benfield | 4d67d04 | 2016-01-22 18:42:13 +0000 | [diff] [blame] | 45 | |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 46 | .. py:data:: FILETYPE_PEM |
Cory Benfield | fb4d4fb | 2016-01-22 18:51:34 +0000 | [diff] [blame] | 47 | |
Cory Benfield | 4756925 | 2016-02-07 10:28:00 +0000 | [diff] [blame] | 48 | :data:`FILETYPE_PEM` serializes data to a Base64-encoded encoded representation of the underlying ASN.1 data structure. This representation includes delimiters that define what data structure is contained within the Base64-encoded block: for example, for a certificate, the delimiters are ``-----BEGIN CERTIFICATE-----`` and ``-----END CERTIFICATE-----``. |
Cory Benfield | fb4d4fb | 2016-01-22 18:51:34 +0000 | [diff] [blame] | 49 | |
| 50 | .. py:data:: FILETYPE_ASN1 |
| 51 | |
Cory Benfield | 4756925 | 2016-02-07 10:28:00 +0000 | [diff] [blame] | 52 | :data:`FILETYPE_ASN1` serializes data to the underlying ASN.1 data structure. The format used by :data:`FILETYPE_ASN1` is also sometimes referred to as DER. |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 53 | |
| 54 | Certificates |
| 55 | ~~~~~~~~~~~~ |
| 56 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 57 | .. py:function:: dump_certificate(type, cert) |
| 58 | |
| 59 | Dump the certificate *cert* into a buffer string encoded with the type |
| 60 | *type*. |
| 61 | |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 62 | .. py:function:: load_certificate(type, buffer) |
| 63 | |
| 64 | Load a certificate (X509) from the string *buffer* encoded with the |
| 65 | type *type*. |
| 66 | |
| 67 | Certificate signing requests |
| 68 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 69 | |
| 70 | .. py:function:: dump_certificate_request(type, req) |
| 71 | |
| 72 | Dump the certificate request *req* into a buffer string encoded with the |
| 73 | type *type*. |
| 74 | |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 75 | .. py:function:: load_certificate_request(type, buffer) |
| 76 | |
| 77 | Load a certificate request (X509Req) from the string *buffer* encoded with |
| 78 | the type *type*. |
| 79 | |
| 80 | Private keys |
| 81 | ~~~~~~~~~~~~ |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 82 | |
Hynek Schlawack | 11e43ad | 2016-07-03 14:40:20 +0200 | [diff] [blame] | 83 | .. autofunction:: dump_privatekey |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 84 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 85 | .. py:function:: load_privatekey(type, buffer[, passphrase]) |
| 86 | |
| 87 | Load a private key (PKey) from the string *buffer* encoded with the type |
| 88 | *type* (must be one of :py:const:`FILETYPE_PEM` and |
| 89 | :py:const:`FILETYPE_ASN1`). |
| 90 | |
| 91 | *passphrase* must be either a string or a callback for providing the pass |
| 92 | phrase. |
| 93 | |
Cory Benfield | 25338c5 | 2015-10-28 22:19:18 +0900 | [diff] [blame] | 94 | Public keys |
| 95 | ~~~~~~~~~~~ |
| 96 | |
| 97 | .. autofunction:: dump_publickey |
| 98 | |
| 99 | .. autofunction:: load_publickey |
| 100 | |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 101 | Certificate revocation lists |
| 102 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 103 | |
Dominic Chen | f05b212 | 2015-10-13 16:32:35 +0000 | [diff] [blame] | 104 | .. autofunction:: dump_crl |
| 105 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 106 | .. py:function:: load_crl(type, buffer) |
| 107 | |
| 108 | Load Certificate Revocation List (CRL) data from a string *buffer*. |
| 109 | *buffer* encoded with the type *type*. The type *type* must either |
| 110 | :py:const:`FILETYPE_PEM` or :py:const:`FILETYPE_ASN1`). |
| 111 | |
| 112 | |
| 113 | .. py:function:: load_pkcs7_data(type, buffer) |
| 114 | |
Laurens Van Houtven | 0f82087 | 2015-04-20 11:25:57 -0700 | [diff] [blame] | 115 | Load pkcs7 data from the string *buffer* encoded with the type |
| 116 | *type*. The type *type* must either :py:const:`FILETYPE_PEM` or |
| 117 | :py:const:`FILETYPE_ASN1`). |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 118 | |
| 119 | |
| 120 | .. py:function:: load_pkcs12(buffer[, passphrase]) |
| 121 | |
| 122 | Load pkcs12 data from the string *buffer*. If the pkcs12 structure is |
| 123 | encrypted, a *passphrase* must be included. The MAC is always |
| 124 | checked and thus required. |
| 125 | |
| 126 | See also the man page for the C function :py:func:`PKCS12_parse`. |
| 127 | |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 128 | Signing and verifying signatures |
| 129 | -------------------------------- |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 130 | |
| 131 | .. py:function:: sign(key, data, digest) |
| 132 | |
| 133 | Sign a data string using the given key and message digest. |
| 134 | |
| 135 | *key* is a :py:class:`PKey` instance. *data* is a ``str`` instance. |
| 136 | *digest* is a ``str`` naming a supported message digest type, for example |
Alex Gaynor | 239e2d3 | 2016-09-11 12:36:35 -0400 | [diff] [blame] | 137 | :py:const:`b"sha256"`. |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 138 | |
| 139 | .. versionadded:: 0.11 |
| 140 | |
| 141 | |
| 142 | .. py:function:: verify(certificate, signature, data, digest) |
| 143 | |
| 144 | Verify the signature for a data string. |
| 145 | |
| 146 | *certificate* is a :py:class:`X509` instance corresponding to the private |
| 147 | key which generated the signature. *signature* is a *str* instance giving |
| 148 | the signature itself. *data* is a *str* instance giving the data to which |
| 149 | the signature applies. *digest* is a *str* instance naming the message |
Alex Gaynor | 239e2d3 | 2016-09-11 12:36:35 -0400 | [diff] [blame] | 150 | digest type of the signature, for example :py:const:`b"sha256"`. |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 151 | |
| 152 | .. versionadded:: 0.11 |
| 153 | |
| 154 | |
| 155 | .. _openssl-x509: |
| 156 | |
| 157 | X509 objects |
| 158 | ------------ |
| 159 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 160 | .. autoclass:: X509 |
| 161 | :members: |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 162 | |
| 163 | .. _openssl-x509name: |
| 164 | |
| 165 | X509Name objects |
| 166 | ---------------- |
| 167 | |
Laurens Van Houtven | 196195b | 2014-06-17 17:06:34 +0200 | [diff] [blame] | 168 | .. autoclass:: X509Name |
| 169 | :members: |
| 170 | :special-members: |
| 171 | :exclude-members: __repr__, __getattr__, __weakref__ |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 172 | |
| 173 | .. _openssl-x509req: |
| 174 | |
| 175 | X509Req objects |
| 176 | --------------- |
| 177 | |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 178 | .. autoclass:: X509Req |
| 179 | :members: |
| 180 | :special-members: |
| 181 | :exclude-members: __weakref__ |
Jean-Paul Calderone | 26e07d6 | 2014-03-02 08:08:23 -0500 | [diff] [blame] | 182 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 183 | .. _openssl-x509store: |
| 184 | |
| 185 | X509Store objects |
| 186 | ----------------- |
| 187 | |
Laurens Van Houtven | 8aeafdd | 2014-06-17 15:33:42 +0200 | [diff] [blame] | 188 | .. autoclass:: X509Store |
| 189 | :members: |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 190 | |
Stephen Holsapple | 8ad4a19 | 2015-06-09 22:51:43 -0700 | [diff] [blame] | 191 | .. _openssl-x509storecontexterror: |
| 192 | |
Stephen Holsapple | 95a4665 | 2015-02-09 19:34:25 -0800 | [diff] [blame] | 193 | X509StoreContextError objects |
| 194 | ----------------------------- |
| 195 | |
Stephen Holsapple | 8ad4a19 | 2015-06-09 22:51:43 -0700 | [diff] [blame] | 196 | .. autoclass:: X509StoreContextError |
| 197 | :members: |
Stephen Holsapple | 95a4665 | 2015-02-09 19:34:25 -0800 | [diff] [blame] | 198 | |
Stephen Holsapple | 8ad4a19 | 2015-06-09 22:51:43 -0700 | [diff] [blame] | 199 | .. _openssl-x509storecontext: |
Stephen Holsapple | 95a4665 | 2015-02-09 19:34:25 -0800 | [diff] [blame] | 200 | |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 201 | X509StoreContext objects |
| 202 | ------------------------ |
| 203 | |
Stephen Holsapple | 8ad4a19 | 2015-06-09 22:51:43 -0700 | [diff] [blame] | 204 | .. autoclass:: X509StoreContext |
| 205 | :members: |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 206 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 207 | .. _openssl-pkey: |
| 208 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 209 | X509StoreFlags constants |
| 210 | ------------------------ |
| 211 | |
| 212 | .. autoclass:: X509StoreFlags |
| 213 | |
| 214 | .. data:: CRL_CHECK |
| 215 | .. data:: CRL_CHECK_ALL |
| 216 | .. data:: IGNORE_CRITICAL |
| 217 | .. data:: X509_STRICT |
| 218 | .. data:: ALLOW_PROXY_CERTS |
| 219 | .. data:: POLICY_CHECK |
| 220 | .. data:: EXPLICIT_POLICY |
| 221 | .. data:: INHIBIT_MAP |
| 222 | .. data:: NOTIFY_POLICY |
| 223 | .. data:: CHECK_SS_SIGNATURE |
| 224 | .. data:: CB_ISSUER_CHECK |
| 225 | |
| 226 | .. _openssl-x509storeflags: |
| 227 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 228 | PKey objects |
| 229 | ------------ |
| 230 | |
Laurens Van Houtven | 6e7dd43 | 2014-06-17 16:10:57 +0200 | [diff] [blame] | 231 | .. autoclass:: PKey |
| 232 | :members: |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 233 | |
| 234 | .. _openssl-pkcs7: |
| 235 | |
Laurens Van Houtven | 9d4c074 | 2015-04-20 11:58:39 -0700 | [diff] [blame] | 236 | .. py:data:: TYPE_RSA |
| 237 | TYPE_DSA |
| 238 | |
| 239 | Key type constants. |
| 240 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 241 | PKCS7 objects |
| 242 | ------------- |
| 243 | |
| 244 | PKCS7 objects have the following methods: |
| 245 | |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 246 | .. py:method:: PKCS7.type_is_signed() |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 247 | |
| 248 | FIXME |
| 249 | |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 250 | .. py:method:: PKCS7.type_is_enveloped() |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 251 | |
| 252 | FIXME |
| 253 | |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 254 | .. py:method:: PKCS7.type_is_signedAndEnveloped() |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 255 | |
| 256 | FIXME |
| 257 | |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 258 | .. py:method:: PKCS7.type_is_data() |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 259 | |
| 260 | FIXME |
| 261 | |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 262 | .. py:method:: PKCS7.get_type_name() |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 263 | |
| 264 | Get the type name of the PKCS7. |
| 265 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 266 | .. _openssl-pkcs12: |
| 267 | |
| 268 | PKCS12 objects |
| 269 | -------------- |
| 270 | |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 271 | .. autoclass:: PKCS12 |
| 272 | :members: |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 273 | |
| 274 | .. _openssl-509ext: |
| 275 | |
| 276 | X509Extension objects |
| 277 | --------------------- |
| 278 | |
Laurens Van Houtven | 2650de5 | 2014-06-18 13:47:47 +0200 | [diff] [blame] | 279 | .. autoclass:: X509Extension |
| 280 | :members: |
| 281 | :special-members: |
| 282 | :exclude-members: __weakref__ |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 283 | |
| 284 | .. _openssl-netscape-spki: |
| 285 | |
| 286 | NetscapeSPKI objects |
| 287 | -------------------- |
| 288 | |
Laurens Van Houtven | 59152b5 | 2014-06-19 16:42:30 +0200 | [diff] [blame] | 289 | .. autoclass:: NetscapeSPKI |
| 290 | :members: |
| 291 | :special-members: |
| 292 | :exclude-members: __weakref__ |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 293 | |
Laurens Van Houtven | 889b9d2 | 2015-04-20 12:18:28 -0700 | [diff] [blame] | 294 | .. _crl: |
| 295 | |
| 296 | CRL objects |
| 297 | ----------- |
| 298 | |
| 299 | .. autoclass:: CRL |
| 300 | :members: |
| 301 | :special-members: |
| 302 | :exclude-members: __weakref__ |
| 303 | |
| 304 | .. _revoked: |
| 305 | |
| 306 | Revoked objects |
| 307 | --------------- |
| 308 | |
| 309 | .. autoclass:: Revoked |
| 310 | :members: |
| 311 | |
Laurens Van Houtven | 3de6b2b | 2015-04-20 12:20:42 -0700 | [diff] [blame] | 312 | Exceptions |
| 313 | ---------- |
| 314 | |
| 315 | .. py:exception:: Error |
| 316 | |
| 317 | Generic exception used in the :py:mod:`.crypto` module. |
| 318 | |
Hynek Schlawack | 8d4f976 | 2016-03-19 08:15:03 +0100 | [diff] [blame] | 319 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 320 | Digest names |
| 321 | ------------ |
| 322 | |
Hynek Schlawack | 8d4f976 | 2016-03-19 08:15:03 +0100 | [diff] [blame] | 323 | Several of the functions and methods in this module take a digest name. |
| 324 | These must be strings describing a digest algorithm supported by OpenSSL (by ``EVP_get_digestbyname``, specifically). |
Alex Gaynor | 643aab8 | 2016-09-11 12:14:55 -0400 | [diff] [blame] | 325 | For example, :const:`b"sha256"` or :const:`b"sha384"`. |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 326 | |
Hynek Schlawack | 8d4f976 | 2016-03-19 08:15:03 +0100 | [diff] [blame] | 327 | More information and a list of these digest names can be found in the ``EVP_DigestInit(3)`` man page of your OpenSSL installation. |
| 328 | This page can be found online for the latest version of OpenSSL: |
Alex Chan | 54005ce | 2017-03-21 08:08:17 +0000 | [diff] [blame] | 329 | https://www.openssl.org/docs/manmaster/man3/EVP_DigestInit.html |
Hynek Schlawack | 8d4f976 | 2016-03-19 08:15:03 +0100 | [diff] [blame] | 330 | |
Paul Kehrer | dc996b1 | 2017-07-19 21:09:04 +0200 | [diff] [blame] | 331 | .. _`pyca/cryptography`: https://cryptography.io |