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