blob: a10e9e30dfc63ac1346f405c02ed06b214492814 [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
Laurens Van Houtven07051d32014-06-19 12:00:30 +02009Elliptic curves
10---------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +090011
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040012.. py:function:: get_elliptic_curves
13
14 Return a set of objects representing the elliptic curves supported in the
15 OpenSSL build in use.
16
17 The curve objects have a :py:class:`unicode` ``name`` attribute by which
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -040018 they identify themselves.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040019
20 The curve objects are useful as values for the argument accepted by
Jean-Paul Calderone3b04e352014-04-19 09:29:10 -040021 :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be
22 used for ECDHE key exchange.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040023
24
Akihiro Yamazakib8450442015-09-04 16:55:04 +090025.. py:function:: get_elliptic_curve(name)
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040026
Alex Gaynorb6e92df2015-09-04 07:48:35 -040027 Return a single curve object selected by *name*.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040028
29 See :py:func:`get_elliptic_curves` for information about curve objects.
30
31 If the named curve is not supported then :py:class:`ValueError` is raised.
32
33
Laurens Van Houtven07051d32014-06-19 12:00:30 +020034Serialization and deserialization
35---------------------------------
36
37The following serialization functions take one of these constants to
Cory Benfield4d67d042016-01-22 18:42:13 +000038determine the format.
39
Laurens Van Houtven07051d32014-06-19 12:00:30 +020040.. py:data:: FILETYPE_PEM
Cory Benfieldfb4d4fb2016-01-22 18:51:34 +000041
42:data:`FILETYPE_PEM` serializes data to a Base64-encoded encoded representation
43of the underlying ASN.1 data structure.
44
45.. py:data:: FILETYPE_ASN1
46
47:data:`FILETYPE_ASN1` serializes data to the underlying ASN.1 data structure.
48The format used by :data:`FILETYPE_ASN1` is also sometimes referred to as DER.
Laurens Van Houtven07051d32014-06-19 12:00:30 +020049
50Certificates
51~~~~~~~~~~~~
52
Jonathan Balletc9e066c2011-07-17 22:56:05 +090053.. py:function:: dump_certificate(type, cert)
54
55 Dump the certificate *cert* into a buffer string encoded with the type
56 *type*.
57
Laurens Van Houtven07051d32014-06-19 12:00:30 +020058.. py:function:: load_certificate(type, buffer)
59
60 Load a certificate (X509) from the string *buffer* encoded with the
61 type *type*.
62
63Certificate signing requests
64~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090065
66.. py:function:: dump_certificate_request(type, req)
67
68 Dump the certificate request *req* into a buffer string encoded with the
69 type *type*.
70
Laurens Van Houtven07051d32014-06-19 12:00:30 +020071.. py:function:: load_certificate_request(type, buffer)
72
73 Load a certificate request (X509Req) from the string *buffer* encoded with
74 the type *type*.
75
76Private keys
77~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090078
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
Jonathan Balletc9e066c2011-07-17 22:56:05 +090088.. py:function:: load_privatekey(type, buffer[, passphrase])
89
90 Load a private key (PKey) from the string *buffer* encoded with the type
91 *type* (must be one of :py:const:`FILETYPE_PEM` and
92 :py:const:`FILETYPE_ASN1`).
93
94 *passphrase* must be either a string or a callback for providing the pass
95 phrase.
96
Cory Benfield25338c52015-10-28 22:19:18 +090097Public keys
98~~~~~~~~~~~
99
100.. autofunction:: dump_publickey
101
102.. autofunction:: load_publickey
103
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200104Certificate revocation lists
105~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900106
Dominic Chenf05b2122015-10-13 16:32:35 +0000107.. autofunction:: dump_crl
108
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900109.. py:function:: load_crl(type, buffer)
110
111 Load Certificate Revocation List (CRL) data from a string *buffer*.
112 *buffer* encoded with the type *type*. The type *type* must either
113 :py:const:`FILETYPE_PEM` or :py:const:`FILETYPE_ASN1`).
114
115
116.. py:function:: load_pkcs7_data(type, buffer)
117
Laurens Van Houtven0f820872015-04-20 11:25:57 -0700118 Load pkcs7 data from the string *buffer* encoded with the type
119 *type*. The type *type* must either :py:const:`FILETYPE_PEM` or
120 :py:const:`FILETYPE_ASN1`).
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900121
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 Houtven07051d32014-06-19 12:00:30 +0200131Signing and verifying signatures
132--------------------------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900133
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
160X509 objects
161------------
162
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200163.. autoclass:: X509
164 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900165
166.. _openssl-x509name:
167
168X509Name objects
169----------------
170
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200171.. autoclass:: X509Name
172 :members:
173 :special-members:
174 :exclude-members: __repr__, __getattr__, __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900175
176.. _openssl-x509req:
177
178X509Req objects
179---------------
180
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200181.. autoclass:: X509Req
182 :members:
183 :special-members:
184 :exclude-members: __weakref__
Jean-Paul Calderone26e07d62014-03-02 08:08:23 -0500185
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900186.. _openssl-x509store:
187
188X509Store objects
189-----------------
190
Laurens Van Houtven8aeafdd2014-06-17 15:33:42 +0200191.. autoclass:: X509Store
192 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900193
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700194.. _openssl-x509storecontexterror:
195
Stephen Holsapple95a46652015-02-09 19:34:25 -0800196X509StoreContextError objects
197-----------------------------
198
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700199.. autoclass:: X509StoreContextError
200 :members:
Stephen Holsapple95a46652015-02-09 19:34:25 -0800201
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700202.. _openssl-x509storecontext:
Stephen Holsapple95a46652015-02-09 19:34:25 -0800203
Stephen Holsapple08ffaa62015-01-30 17:18:40 -0800204X509StoreContext objects
205------------------------
206
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700207.. autoclass:: X509StoreContext
208 :members:
Stephen Holsapple08ffaa62015-01-30 17:18:40 -0800209
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900210.. _openssl-pkey:
211
212PKey objects
213------------
214
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200215.. autoclass:: PKey
216 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900217
218.. _openssl-pkcs7:
219
Laurens Van Houtven9d4c0742015-04-20 11:58:39 -0700220.. py:data:: TYPE_RSA
221 TYPE_DSA
222
223 Key type constants.
224
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900225PKCS7 objects
226-------------
227
228PKCS7 objects have the following methods:
229
Jonathan Ballet6381da32011-07-20 16:43:38 +0900230.. py:method:: PKCS7.type_is_signed()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900231
232 FIXME
233
Jonathan Ballet6381da32011-07-20 16:43:38 +0900234.. py:method:: PKCS7.type_is_enveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900235
236 FIXME
237
Jonathan Ballet6381da32011-07-20 16:43:38 +0900238.. py:method:: PKCS7.type_is_signedAndEnveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900239
240 FIXME
241
Jonathan Ballet6381da32011-07-20 16:43:38 +0900242.. py:method:: PKCS7.type_is_data()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900243
244 FIXME
245
Jonathan Ballet6381da32011-07-20 16:43:38 +0900246.. py:method:: PKCS7.get_type_name()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900247
248 Get the type name of the PKCS7.
249
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900250.. _openssl-pkcs12:
251
252PKCS12 objects
253--------------
254
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +0200255.. autoclass:: PKCS12
256 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900257
258.. _openssl-509ext:
259
260X509Extension objects
261---------------------
262
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200263.. autoclass:: X509Extension
264 :members:
265 :special-members:
266 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900267
268.. _openssl-netscape-spki:
269
270NetscapeSPKI objects
271--------------------
272
Laurens Van Houtven59152b52014-06-19 16:42:30 +0200273.. autoclass:: NetscapeSPKI
274 :members:
275 :special-members:
276 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900277
Laurens Van Houtven889b9d22015-04-20 12:18:28 -0700278.. _crl:
279
280CRL objects
281-----------
282
283.. autoclass:: CRL
284 :members:
285 :special-members:
286 :exclude-members: __weakref__
287
288.. _revoked:
289
290Revoked objects
291---------------
292
293.. autoclass:: Revoked
294 :members:
295
Laurens Van Houtven3de6b2b2015-04-20 12:20:42 -0700296Exceptions
297----------
298
299.. py:exception:: Error
300
301 Generic exception used in the :py:mod:`.crypto` module.
302
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200303Digest names
304------------
305
306Several of the functions and methods in this module take a digest
307name. These must be strings describing a digest algorithm supported by
308OpenSSL (by ``EVP_get_digestbyname``, specifically). For example,
309:py:const:`b"md5"` or :py:const:`b"sha1"`.
310
311More information and a list of these digest names can be found in the
312``EVP_DigestInit(3)`` man page of your OpenSSL installation. This page
313can be found online for the latest version of OpenSSL:
314https://www.openssl.org/docs/crypto/EVP_DigestInit.html
315
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200316Backwards compatible type names
317-------------------------------
318
319When PyOpenSSL was originally written, the most current version of
320Python was 2.1. It made a distinction between classes and types. None
321of the versions of Python currently supported by PyOpenSSL still
322enforce that distinction: the type of an instance of an
323:py:class:`X509` object is now simply :py:class:`X509`. Originally,
324the type would have been :py:class:`X509Type`. These days,
325:py:class:`X509Type` and :py:class:`X509` are literally the same
326object. PyOpenSSL maintains these old names for backwards
327compatibility.
328
329Here's a table of these backwards-compatible names:
330
331========================= =============================
332Type name Backwards-compatible name
333========================= =============================
334:py:class:`X509` :py:class:`X509Type`
335:py:class:`X509Name` :py:class:`X509NameType`
336:py:class:`X509Req` :py:class:`X509ReqType`
337:py:class:`X509Store` :py:class:`X509StoreType`
338:py:class:`X509Extension` :py:class:`X509ExtensionType`
339:py:class:`PKey` :py:class:`PKeyType`
340:py:class:`PKCS7` :py:class:`PKCS7Type`
341:py:class:`PKCS12` :py:class:`PKCS12Type`
342:py:class:`NetscapeSPKI` :py:class:`NetscapeSPKIType`
343:py:class:`CRL` :py:class:`CRLType`
344========================= =============================
345
Laurens Van Houtven5faa2812015-04-23 10:44:10 -0700346Some objects, such as :py:class`Revoked`, don't have ``Type``
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200347equivalents, because they were added after the restriction had been
348lifted.