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