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