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