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