blob: ad4e02587cd6a6aec8d955c5ff82cb12f62dd73c [file] [log] [blame]
Jonathan Ballet6381da32011-07-20 16:43:38 +09001.. _openssl-crypto:
2
3:py:mod:`crypto` --- Generic cryptographic module
4=================================================
5
Jonathan Balletc9e066c2011-07-17 22:56:05 +09006.. py:module:: OpenSSL.crypto
Jonathan Ballet6381da32011-07-20 16:43:38 +09007 :synopsis: Generic cryptographic module
Jonathan Ballet648875f2011-07-16 14:14:58 +09008
9
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070010.. py:data:: X509StoreContext
11
12 A class representing the X.509 store context.
Jonathan Balletc9e066c2011-07-17 22:56:05 +090013
Laurens Van Houtven07051d32014-06-19 12:00:30 +020014Elliptic curves
15---------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +090016
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040017.. py:function:: get_elliptic_curves
18
19 Return a set of objects representing the elliptic curves supported in the
20 OpenSSL build in use.
21
22 The curve objects have a :py:class:`unicode` ``name`` attribute by which
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -040023 they identify themselves.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040024
25 The curve objects are useful as values for the argument accepted by
Jean-Paul Calderone3b04e352014-04-19 09:29:10 -040026 :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be
27 used for ECDHE key exchange.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040028
29
30.. py:function:: get_elliptic_curve
31
32 Return a single curve object selected by name.
33
34 See :py:func:`get_elliptic_curves` for information about curve objects.
35
36 If the named curve is not supported then :py:class:`ValueError` is raised.
37
38
Laurens Van Houtven07051d32014-06-19 12:00:30 +020039Serialization and deserialization
40---------------------------------
41
42The following serialization functions take one of these constants to
43determine the format:
44
45.. py:data:: FILETYPE_PEM
46 FILETYPE_ASN1
47
48Certificates
49~~~~~~~~~~~~
50
Jonathan Balletc9e066c2011-07-17 22:56:05 +090051.. py:function:: dump_certificate(type, cert)
52
53 Dump the certificate *cert* into a buffer string encoded with the type
54 *type*.
55
Laurens Van Houtven07051d32014-06-19 12:00:30 +020056.. py:function:: load_certificate(type, buffer)
57
58 Load a certificate (X509) from the string *buffer* encoded with the
59 type *type*.
60
61Certificate signing requests
62~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090063
64.. py:function:: dump_certificate_request(type, req)
65
66 Dump the certificate request *req* into a buffer string encoded with the
67 type *type*.
68
Laurens Van Houtven07051d32014-06-19 12:00:30 +020069.. py:function:: load_certificate_request(type, buffer)
70
71 Load a certificate request (X509Req) from the string *buffer* encoded with
72 the type *type*.
73
74Private keys
75~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090076
77.. py:function:: dump_privatekey(type, pkey[, cipher, passphrase])
78
79 Dump the private key *pkey* into a buffer string encoded with the type
80 *type*, optionally (if *type* is :py:const:`FILETYPE_PEM`) encrypting it
81 using *cipher* and *passphrase*.
82
83 *passphrase* must be either a string or a callback for providing the
84 pass phrase.
85
Jonathan Balletc9e066c2011-07-17 22:56:05 +090086.. py:function:: load_privatekey(type, buffer[, passphrase])
87
88 Load a private key (PKey) from the string *buffer* encoded with the type
89 *type* (must be one of :py:const:`FILETYPE_PEM` and
90 :py:const:`FILETYPE_ASN1`).
91
92 *passphrase* must be either a string or a callback for providing the pass
93 phrase.
94
Laurens Van Houtven07051d32014-06-19 12:00:30 +020095Certificate revocation lists
96~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090097
98.. py:function:: load_crl(type, buffer)
99
100 Load Certificate Revocation List (CRL) data from a string *buffer*.
101 *buffer* encoded with the type *type*. The type *type* must either
102 :py:const:`FILETYPE_PEM` or :py:const:`FILETYPE_ASN1`).
103
104
105.. py:function:: load_pkcs7_data(type, buffer)
106
Laurens Van Houtven0f820872015-04-20 11:25:57 -0700107 Load pkcs7 data from the string *buffer* encoded with the type
108 *type*. The type *type* must either :py:const:`FILETYPE_PEM` or
109 :py:const:`FILETYPE_ASN1`).
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900110
111
112.. py:function:: load_pkcs12(buffer[, passphrase])
113
114 Load pkcs12 data from the string *buffer*. If the pkcs12 structure is
115 encrypted, a *passphrase* must be included. The MAC is always
116 checked and thus required.
117
118 See also the man page for the C function :py:func:`PKCS12_parse`.
119
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200120Signing and verifying signatures
121--------------------------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900122
123.. py:function:: sign(key, data, digest)
124
125 Sign a data string using the given key and message digest.
126
127 *key* is a :py:class:`PKey` instance. *data* is a ``str`` instance.
128 *digest* is a ``str`` naming a supported message digest type, for example
129 :py:const:`sha1`.
130
131 .. versionadded:: 0.11
132
133
134.. py:function:: verify(certificate, signature, data, digest)
135
136 Verify the signature for a data string.
137
138 *certificate* is a :py:class:`X509` instance corresponding to the private
139 key which generated the signature. *signature* is a *str* instance giving
140 the signature itself. *data* is a *str* instance giving the data to which
141 the signature applies. *digest* is a *str* instance naming the message
142 digest type of the signature, for example :py:const:`sha1`.
143
144 .. versionadded:: 0.11
145
146
147.. _openssl-x509:
148
149X509 objects
150------------
151
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200152.. autoclass:: X509
153 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900154
155.. _openssl-x509name:
156
157X509Name objects
158----------------
159
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200160.. autoclass:: X509Name
161 :members:
162 :special-members:
163 :exclude-members: __repr__, __getattr__, __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900164
165.. _openssl-x509req:
166
167X509Req objects
168---------------
169
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200170.. autoclass:: X509Req
171 :members:
172 :special-members:
173 :exclude-members: __weakref__
Jean-Paul Calderone26e07d62014-03-02 08:08:23 -0500174
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900175.. _openssl-x509store:
176
177X509Store objects
178-----------------
179
Laurens Van Houtven8aeafdd2014-06-17 15:33:42 +0200180.. autoclass:: X509Store
181 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900182
Stephen Holsapple95a46652015-02-09 19:34:25 -0800183X509StoreContextError objects
184-----------------------------
185
186The X509StoreContextError is an exception raised from
187`X509StoreContext.verify_certificate` in circumstances where a certificate
188cannot be verified in a provided context.
189
Jean-Paul Calderone876b2ac2015-03-15 16:17:19 -0400190The certificate for which the verification error was detected is given by the
191``certificate`` attribute of the exception instance as a :class:`X509`
192instance.
193
Laurens Van Houtven9313a4e2015-04-20 11:27:28 -0700194Details about the verification error are given in the exception's
195``args`` attribute.
Stephen Holsapple95a46652015-02-09 19:34:25 -0800196
Stephen Holsapple08ffaa62015-01-30 17:18:40 -0800197X509StoreContext objects
198------------------------
199
200The X509StoreContext object is used for verifying a certificate against a set
201of trusted certificates.
202
203
204.. py:method:: X509StoreContext.verify_certificate()
205
206 Verify a certificate in the context of this initialized `X509StoreContext`.
207 On error, raises `X509StoreContextError`, otherwise does nothing.
208
209 .. versionadded:: 0.15
210
211
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900212.. _openssl-pkey:
213
214PKey objects
215------------
216
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200217.. autoclass:: PKey
218 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900219
220.. _openssl-pkcs7:
221
Laurens Van Houtven9d4c0742015-04-20 11:58:39 -0700222.. py:data:: TYPE_RSA
223 TYPE_DSA
224
225 Key type constants.
226
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900227PKCS7 objects
228-------------
229
230PKCS7 objects have the following methods:
231
Jonathan Ballet6381da32011-07-20 16:43:38 +0900232.. py:method:: PKCS7.type_is_signed()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900233
234 FIXME
235
Jonathan Ballet6381da32011-07-20 16:43:38 +0900236.. py:method:: PKCS7.type_is_enveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900237
238 FIXME
239
Jonathan Ballet6381da32011-07-20 16:43:38 +0900240.. py:method:: PKCS7.type_is_signedAndEnveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900241
242 FIXME
243
Jonathan Ballet6381da32011-07-20 16:43:38 +0900244.. py:method:: PKCS7.type_is_data()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900245
246 FIXME
247
Jonathan Ballet6381da32011-07-20 16:43:38 +0900248.. py:method:: PKCS7.get_type_name()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900249
250 Get the type name of the PKCS7.
251
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900252.. _openssl-pkcs12:
253
254PKCS12 objects
255--------------
256
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +0200257.. autoclass:: PKCS12
258 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900259
260.. _openssl-509ext:
261
262X509Extension objects
263---------------------
264
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200265.. autoclass:: X509Extension
266 :members:
267 :special-members:
268 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900269
270.. _openssl-netscape-spki:
271
272NetscapeSPKI objects
273--------------------
274
Laurens Van Houtven59152b52014-06-19 16:42:30 +0200275.. autoclass:: NetscapeSPKI
276 :members:
277 :special-members:
278 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900279
Laurens Van Houtven889b9d22015-04-20 12:18:28 -0700280.. _crl:
281
282CRL objects
283-----------
284
285.. autoclass:: CRL
286 :members:
287 :special-members:
288 :exclude-members: __weakref__
289
290.. _revoked:
291
292Revoked objects
293---------------
294
295.. autoclass:: Revoked
296 :members:
297
Laurens Van Houtven3de6b2b2015-04-20 12:20:42 -0700298Exceptions
299----------
300
301.. py:exception:: Error
302
303 Generic exception used in the :py:mod:`.crypto` module.
304
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200305Digest names
306------------
307
308Several of the functions and methods in this module take a digest
309name. These must be strings describing a digest algorithm supported by
310OpenSSL (by ``EVP_get_digestbyname``, specifically). For example,
311:py:const:`b"md5"` or :py:const:`b"sha1"`.
312
313More information and a list of these digest names can be found in the
314``EVP_DigestInit(3)`` man page of your OpenSSL installation. This page
315can be found online for the latest version of OpenSSL:
316https://www.openssl.org/docs/crypto/EVP_DigestInit.html
317
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200318Backwards compatible type names
319-------------------------------
320
321When PyOpenSSL was originally written, the most current version of
322Python was 2.1. It made a distinction between classes and types. None
323of the versions of Python currently supported by PyOpenSSL still
324enforce that distinction: the type of an instance of an
325:py:class:`X509` object is now simply :py:class:`X509`. Originally,
326the type would have been :py:class:`X509Type`. These days,
327:py:class:`X509Type` and :py:class:`X509` are literally the same
328object. PyOpenSSL maintains these old names for backwards
329compatibility.
330
331Here's a table of these backwards-compatible names:
332
333========================= =============================
334Type name Backwards-compatible name
335========================= =============================
336:py:class:`X509` :py:class:`X509Type`
337:py:class:`X509Name` :py:class:`X509NameType`
338:py:class:`X509Req` :py:class:`X509ReqType`
339:py:class:`X509Store` :py:class:`X509StoreType`
340:py:class:`X509Extension` :py:class:`X509ExtensionType`
341:py:class:`PKey` :py:class:`PKeyType`
342:py:class:`PKCS7` :py:class:`PKCS7Type`
343:py:class:`PKCS12` :py:class:`PKCS12Type`
344:py:class:`NetscapeSPKI` :py:class:`NetscapeSPKIType`
345:py:class:`CRL` :py:class:`CRLType`
346========================= =============================
347
Laurens Van Houtven2ec1fba2015-04-20 11:48:12 -0700348Some objects, such as py:class`Revoked`, don't have ``Type``
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200349equivalents, because they were added after the restriction had been
350lifted.