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