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