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 | |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 9 | Elliptic curves |
| 10 | --------------- |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 11 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 12 | .. py:function:: get_elliptic_curves |
| 13 | |
| 14 | Return a set of objects representing the elliptic curves supported in the |
| 15 | OpenSSL build in use. |
| 16 | |
| 17 | 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] | 18 | they identify themselves. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 19 | |
| 20 | 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] | 21 | :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be |
| 22 | used for ECDHE key exchange. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 23 | |
| 24 | |
Akihiro Yamazaki | b845044 | 2015-09-04 16:55:04 +0900 | [diff] [blame] | 25 | .. py:function:: get_elliptic_curve(name) |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 26 | |
Alex Gaynor | b6e92df | 2015-09-04 07:48:35 -0400 | [diff] [blame] | 27 | Return a single curve object selected by *name*. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 28 | |
| 29 | See :py:func:`get_elliptic_curves` for information about curve objects. |
| 30 | |
| 31 | If the named curve is not supported then :py:class:`ValueError` is raised. |
| 32 | |
| 33 | |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 34 | Serialization and deserialization |
| 35 | --------------------------------- |
| 36 | |
| 37 | The following serialization functions take one of these constants to |
Cory Benfield | 4d67d04 | 2016-01-22 18:42:13 +0000 | [diff] [blame^] | 38 | determine the format. |
| 39 | |
| 40 | :data:`FILETYPE_PEM` serializes data to a Base64-encoded encoded representation |
| 41 | of the underlying ASN.1 data structure. :data:`FILETYPE_ASN1` serializes data |
| 42 | to the underlying ASN.1 data structure. The format used by |
| 43 | :data:`FILETYPE_ASN1` is also sometimes referred to as DER. |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 44 | |
| 45 | .. py:data:: FILETYPE_PEM |
| 46 | FILETYPE_ASN1 |
| 47 | |
| 48 | Certificates |
| 49 | ~~~~~~~~~~~~ |
| 50 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 51 | .. py:function:: dump_certificate(type, cert) |
| 52 | |
| 53 | Dump the certificate *cert* into a buffer string encoded with the type |
| 54 | *type*. |
| 55 | |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 56 | .. py:function:: load_certificate(type, buffer) |
| 57 | |
| 58 | Load a certificate (X509) from the string *buffer* encoded with the |
| 59 | type *type*. |
| 60 | |
| 61 | Certificate signing requests |
| 62 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 63 | |
| 64 | .. py:function:: dump_certificate_request(type, req) |
| 65 | |
| 66 | Dump the certificate request *req* into a buffer string encoded with the |
| 67 | type *type*. |
| 68 | |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 69 | .. py:function:: load_certificate_request(type, buffer) |
| 70 | |
| 71 | Load a certificate request (X509Req) from the string *buffer* encoded with |
| 72 | the type *type*. |
| 73 | |
| 74 | Private keys |
| 75 | ~~~~~~~~~~~~ |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 76 | |
| 77 | .. py:function:: dump_privatekey(type, pkey[, cipher, passphrase]) |
| 78 | |
| 79 | Dump the private key *pkey* into a buffer string encoded with the type |
| 80 | *type*, optionally (if *type* is :py:const:`FILETYPE_PEM`) encrypting it |
| 81 | using *cipher* and *passphrase*. |
| 82 | |
| 83 | *passphrase* must be either a string or a callback for providing the |
| 84 | pass phrase. |
| 85 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 86 | .. py:function:: load_privatekey(type, buffer[, passphrase]) |
| 87 | |
| 88 | Load a private key (PKey) from the string *buffer* encoded with the type |
| 89 | *type* (must be one of :py:const:`FILETYPE_PEM` and |
| 90 | :py:const:`FILETYPE_ASN1`). |
| 91 | |
| 92 | *passphrase* must be either a string or a callback for providing the pass |
| 93 | phrase. |
| 94 | |
Cory Benfield | 25338c5 | 2015-10-28 22:19:18 +0900 | [diff] [blame] | 95 | Public keys |
| 96 | ~~~~~~~~~~~ |
| 97 | |
| 98 | .. autofunction:: dump_publickey |
| 99 | |
| 100 | .. autofunction:: load_publickey |
| 101 | |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 102 | Certificate revocation lists |
| 103 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 104 | |
Dominic Chen | f05b212 | 2015-10-13 16:32:35 +0000 | [diff] [blame] | 105 | .. autofunction:: dump_crl |
| 106 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 107 | .. py:function:: load_crl(type, buffer) |
| 108 | |
| 109 | Load Certificate Revocation List (CRL) data from a string *buffer*. |
| 110 | *buffer* encoded with the type *type*. The type *type* must either |
| 111 | :py:const:`FILETYPE_PEM` or :py:const:`FILETYPE_ASN1`). |
| 112 | |
| 113 | |
| 114 | .. py:function:: load_pkcs7_data(type, buffer) |
| 115 | |
Laurens Van Houtven | 0f82087 | 2015-04-20 11:25:57 -0700 | [diff] [blame] | 116 | Load pkcs7 data from the string *buffer* encoded with the type |
| 117 | *type*. The type *type* must either :py:const:`FILETYPE_PEM` or |
| 118 | :py:const:`FILETYPE_ASN1`). |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 119 | |
| 120 | |
| 121 | .. py:function:: load_pkcs12(buffer[, passphrase]) |
| 122 | |
| 123 | Load pkcs12 data from the string *buffer*. If the pkcs12 structure is |
| 124 | encrypted, a *passphrase* must be included. The MAC is always |
| 125 | checked and thus required. |
| 126 | |
| 127 | See also the man page for the C function :py:func:`PKCS12_parse`. |
| 128 | |
Laurens Van Houtven | 07051d3 | 2014-06-19 12:00:30 +0200 | [diff] [blame] | 129 | Signing and verifying signatures |
| 130 | -------------------------------- |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 131 | |
| 132 | .. py:function:: sign(key, data, digest) |
| 133 | |
| 134 | Sign a data string using the given key and message digest. |
| 135 | |
| 136 | *key* is a :py:class:`PKey` instance. *data* is a ``str`` instance. |
| 137 | *digest* is a ``str`` naming a supported message digest type, for example |
| 138 | :py:const:`sha1`. |
| 139 | |
| 140 | .. versionadded:: 0.11 |
| 141 | |
| 142 | |
| 143 | .. py:function:: verify(certificate, signature, data, digest) |
| 144 | |
| 145 | Verify the signature for a data string. |
| 146 | |
| 147 | *certificate* is a :py:class:`X509` instance corresponding to the private |
| 148 | key which generated the signature. *signature* is a *str* instance giving |
| 149 | the signature itself. *data* is a *str* instance giving the data to which |
| 150 | the signature applies. *digest* is a *str* instance naming the message |
| 151 | digest type of the signature, for example :py:const:`sha1`. |
| 152 | |
| 153 | .. versionadded:: 0.11 |
| 154 | |
| 155 | |
| 156 | .. _openssl-x509: |
| 157 | |
| 158 | X509 objects |
| 159 | ------------ |
| 160 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 161 | .. autoclass:: X509 |
| 162 | :members: |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 163 | |
| 164 | .. _openssl-x509name: |
| 165 | |
| 166 | X509Name objects |
| 167 | ---------------- |
| 168 | |
Laurens Van Houtven | 196195b | 2014-06-17 17:06:34 +0200 | [diff] [blame] | 169 | .. autoclass:: X509Name |
| 170 | :members: |
| 171 | :special-members: |
| 172 | :exclude-members: __repr__, __getattr__, __weakref__ |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 173 | |
| 174 | .. _openssl-x509req: |
| 175 | |
| 176 | X509Req objects |
| 177 | --------------- |
| 178 | |
Laurens Van Houtven | 3e83d24 | 2014-06-18 14:29:47 +0200 | [diff] [blame] | 179 | .. autoclass:: X509Req |
| 180 | :members: |
| 181 | :special-members: |
| 182 | :exclude-members: __weakref__ |
Jean-Paul Calderone | 26e07d6 | 2014-03-02 08:08:23 -0500 | [diff] [blame] | 183 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 184 | .. _openssl-x509store: |
| 185 | |
| 186 | X509Store objects |
| 187 | ----------------- |
| 188 | |
Laurens Van Houtven | 8aeafdd | 2014-06-17 15:33:42 +0200 | [diff] [blame] | 189 | .. autoclass:: X509Store |
| 190 | :members: |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 191 | |
Stephen Holsapple | 8ad4a19 | 2015-06-09 22:51:43 -0700 | [diff] [blame] | 192 | .. _openssl-x509storecontexterror: |
| 193 | |
Stephen Holsapple | 95a4665 | 2015-02-09 19:34:25 -0800 | [diff] [blame] | 194 | X509StoreContextError objects |
| 195 | ----------------------------- |
| 196 | |
Stephen Holsapple | 8ad4a19 | 2015-06-09 22:51:43 -0700 | [diff] [blame] | 197 | .. autoclass:: X509StoreContextError |
| 198 | :members: |
Stephen Holsapple | 95a4665 | 2015-02-09 19:34:25 -0800 | [diff] [blame] | 199 | |
Stephen Holsapple | 8ad4a19 | 2015-06-09 22:51:43 -0700 | [diff] [blame] | 200 | .. _openssl-x509storecontext: |
Stephen Holsapple | 95a4665 | 2015-02-09 19:34:25 -0800 | [diff] [blame] | 201 | |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 202 | X509StoreContext objects |
| 203 | ------------------------ |
| 204 | |
Stephen Holsapple | 8ad4a19 | 2015-06-09 22:51:43 -0700 | [diff] [blame] | 205 | .. autoclass:: X509StoreContext |
| 206 | :members: |
Stephen Holsapple | 08ffaa6 | 2015-01-30 17:18:40 -0800 | [diff] [blame] | 207 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 208 | .. _openssl-pkey: |
| 209 | |
| 210 | PKey objects |
| 211 | ------------ |
| 212 | |
Laurens Van Houtven | 6e7dd43 | 2014-06-17 16:10:57 +0200 | [diff] [blame] | 213 | .. autoclass:: PKey |
| 214 | :members: |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 215 | |
| 216 | .. _openssl-pkcs7: |
| 217 | |
Laurens Van Houtven | 9d4c074 | 2015-04-20 11:58:39 -0700 | [diff] [blame] | 218 | .. py:data:: TYPE_RSA |
| 219 | TYPE_DSA |
| 220 | |
| 221 | Key type constants. |
| 222 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 223 | PKCS7 objects |
| 224 | ------------- |
| 225 | |
| 226 | PKCS7 objects have the following methods: |
| 227 | |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 228 | .. py:method:: PKCS7.type_is_signed() |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 229 | |
| 230 | FIXME |
| 231 | |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 232 | .. py:method:: PKCS7.type_is_enveloped() |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 233 | |
| 234 | FIXME |
| 235 | |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 236 | .. py:method:: PKCS7.type_is_signedAndEnveloped() |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 237 | |
| 238 | FIXME |
| 239 | |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 240 | .. py:method:: PKCS7.type_is_data() |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 241 | |
| 242 | FIXME |
| 243 | |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 244 | .. py:method:: PKCS7.get_type_name() |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 245 | |
| 246 | Get the type name of the PKCS7. |
| 247 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 248 | .. _openssl-pkcs12: |
| 249 | |
| 250 | PKCS12 objects |
| 251 | -------------- |
| 252 | |
Laurens Van Houtven | bb503a3 | 2014-06-19 12:28:08 +0200 | [diff] [blame] | 253 | .. autoclass:: PKCS12 |
| 254 | :members: |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 255 | |
| 256 | .. _openssl-509ext: |
| 257 | |
| 258 | X509Extension objects |
| 259 | --------------------- |
| 260 | |
Laurens Van Houtven | 2650de5 | 2014-06-18 13:47:47 +0200 | [diff] [blame] | 261 | .. autoclass:: X509Extension |
| 262 | :members: |
| 263 | :special-members: |
| 264 | :exclude-members: __weakref__ |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 265 | |
| 266 | .. _openssl-netscape-spki: |
| 267 | |
| 268 | NetscapeSPKI objects |
| 269 | -------------------- |
| 270 | |
Laurens Van Houtven | 59152b5 | 2014-06-19 16:42:30 +0200 | [diff] [blame] | 271 | .. autoclass:: NetscapeSPKI |
| 272 | :members: |
| 273 | :special-members: |
| 274 | :exclude-members: __weakref__ |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 275 | |
Laurens Van Houtven | 889b9d2 | 2015-04-20 12:18:28 -0700 | [diff] [blame] | 276 | .. _crl: |
| 277 | |
| 278 | CRL objects |
| 279 | ----------- |
| 280 | |
| 281 | .. autoclass:: CRL |
| 282 | :members: |
| 283 | :special-members: |
| 284 | :exclude-members: __weakref__ |
| 285 | |
| 286 | .. _revoked: |
| 287 | |
| 288 | Revoked objects |
| 289 | --------------- |
| 290 | |
| 291 | .. autoclass:: Revoked |
| 292 | :members: |
| 293 | |
Laurens Van Houtven | 3de6b2b | 2015-04-20 12:20:42 -0700 | [diff] [blame] | 294 | Exceptions |
| 295 | ---------- |
| 296 | |
| 297 | .. py:exception:: Error |
| 298 | |
| 299 | Generic exception used in the :py:mod:`.crypto` module. |
| 300 | |
Laurens Van Houtven | c3baa7b | 2014-06-18 22:06:56 +0200 | [diff] [blame] | 301 | Digest names |
| 302 | ------------ |
| 303 | |
| 304 | Several of the functions and methods in this module take a digest |
| 305 | name. These must be strings describing a digest algorithm supported by |
| 306 | OpenSSL (by ``EVP_get_digestbyname``, specifically). For example, |
| 307 | :py:const:`b"md5"` or :py:const:`b"sha1"`. |
| 308 | |
| 309 | More information and a list of these digest names can be found in the |
| 310 | ``EVP_DigestInit(3)`` man page of your OpenSSL installation. This page |
| 311 | can be found online for the latest version of OpenSSL: |
| 312 | https://www.openssl.org/docs/crypto/EVP_DigestInit.html |
| 313 | |
Laurens Van Houtven | 13d56ba | 2014-06-17 16:00:26 +0200 | [diff] [blame] | 314 | Backwards compatible type names |
| 315 | ------------------------------- |
| 316 | |
| 317 | When PyOpenSSL was originally written, the most current version of |
| 318 | Python was 2.1. It made a distinction between classes and types. None |
| 319 | of the versions of Python currently supported by PyOpenSSL still |
| 320 | enforce that distinction: the type of an instance of an |
| 321 | :py:class:`X509` object is now simply :py:class:`X509`. Originally, |
| 322 | the type would have been :py:class:`X509Type`. These days, |
| 323 | :py:class:`X509Type` and :py:class:`X509` are literally the same |
| 324 | object. PyOpenSSL maintains these old names for backwards |
| 325 | compatibility. |
| 326 | |
| 327 | Here's a table of these backwards-compatible names: |
| 328 | |
| 329 | ========================= ============================= |
| 330 | Type name Backwards-compatible name |
| 331 | ========================= ============================= |
| 332 | :py:class:`X509` :py:class:`X509Type` |
| 333 | :py:class:`X509Name` :py:class:`X509NameType` |
| 334 | :py:class:`X509Req` :py:class:`X509ReqType` |
| 335 | :py:class:`X509Store` :py:class:`X509StoreType` |
| 336 | :py:class:`X509Extension` :py:class:`X509ExtensionType` |
| 337 | :py:class:`PKey` :py:class:`PKeyType` |
| 338 | :py:class:`PKCS7` :py:class:`PKCS7Type` |
| 339 | :py:class:`PKCS12` :py:class:`PKCS12Type` |
| 340 | :py:class:`NetscapeSPKI` :py:class:`NetscapeSPKIType` |
| 341 | :py:class:`CRL` :py:class:`CRLType` |
| 342 | ========================= ============================= |
| 343 | |
Laurens Van Houtven | 5faa281 | 2015-04-23 10:44:10 -0700 | [diff] [blame] | 344 | Some objects, such as :py:class`Revoked`, don't have ``Type`` |
Laurens Van Houtven | 13d56ba | 2014-06-17 16:00:26 +0200 | [diff] [blame] | 345 | equivalents, because they were added after the restriction had been |
| 346 | lifted. |