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