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