blob: 41b659bdc1e0520353564b763fbcf804169efcd5 [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
38determine the format:
39
40.. py:data:: FILETYPE_PEM
41 FILETYPE_ASN1
42
43Certificates
44~~~~~~~~~~~~
45
Jonathan Balletc9e066c2011-07-17 22:56:05 +090046.. py:function:: dump_certificate(type, cert)
47
48 Dump the certificate *cert* into a buffer string encoded with the type
49 *type*.
50
Laurens Van Houtven07051d32014-06-19 12:00:30 +020051.. py:function:: load_certificate(type, buffer)
52
53 Load a certificate (X509) from the string *buffer* encoded with the
54 type *type*.
55
56Certificate signing requests
57~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090058
59.. py:function:: dump_certificate_request(type, req)
60
61 Dump the certificate request *req* into a buffer string encoded with the
62 type *type*.
63
Laurens Van Houtven07051d32014-06-19 12:00:30 +020064.. py:function:: load_certificate_request(type, buffer)
65
66 Load a certificate request (X509Req) from the string *buffer* encoded with
67 the type *type*.
68
69Private keys
70~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090071
72.. py:function:: dump_privatekey(type, pkey[, cipher, passphrase])
73
74 Dump the private key *pkey* into a buffer string encoded with the type
75 *type*, optionally (if *type* is :py:const:`FILETYPE_PEM`) encrypting it
76 using *cipher* and *passphrase*.
77
78 *passphrase* must be either a string or a callback for providing the
79 pass phrase.
80
Jonathan Balletc9e066c2011-07-17 22:56:05 +090081.. py:function:: load_privatekey(type, buffer[, passphrase])
82
83 Load a private key (PKey) from the string *buffer* encoded with the type
84 *type* (must be one of :py:const:`FILETYPE_PEM` and
85 :py:const:`FILETYPE_ASN1`).
86
87 *passphrase* must be either a string or a callback for providing the pass
88 phrase.
89
Cory Benfield25338c52015-10-28 22:19:18 +090090Public keys
91~~~~~~~~~~~
92
93.. autofunction:: dump_publickey
94
95.. autofunction:: load_publickey
96
Laurens Van Houtven07051d32014-06-19 12:00:30 +020097Certificate revocation lists
98~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090099
Dominic Chenf05b2122015-10-13 16:32:35 +0000100.. autofunction:: dump_crl
101
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900102.. py:function:: load_crl(type, buffer)
103
104 Load Certificate Revocation List (CRL) data from a string *buffer*.
105 *buffer* encoded with the type *type*. The type *type* must either
106 :py:const:`FILETYPE_PEM` or :py:const:`FILETYPE_ASN1`).
107
108
109.. py:function:: load_pkcs7_data(type, buffer)
110
Laurens Van Houtven0f820872015-04-20 11:25:57 -0700111 Load pkcs7 data from the string *buffer* encoded with the type
112 *type*. The type *type* must either :py:const:`FILETYPE_PEM` or
113 :py:const:`FILETYPE_ASN1`).
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900114
115
116.. py:function:: load_pkcs12(buffer[, passphrase])
117
118 Load pkcs12 data from the string *buffer*. If the pkcs12 structure is
119 encrypted, a *passphrase* must be included. The MAC is always
120 checked and thus required.
121
122 See also the man page for the C function :py:func:`PKCS12_parse`.
123
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200124Signing and verifying signatures
125--------------------------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900126
127.. py:function:: sign(key, data, digest)
128
129 Sign a data string using the given key and message digest.
130
131 *key* is a :py:class:`PKey` instance. *data* is a ``str`` instance.
132 *digest* is a ``str`` naming a supported message digest type, for example
133 :py:const:`sha1`.
134
135 .. versionadded:: 0.11
136
137
138.. py:function:: verify(certificate, signature, data, digest)
139
140 Verify the signature for a data string.
141
142 *certificate* is a :py:class:`X509` instance corresponding to the private
143 key which generated the signature. *signature* is a *str* instance giving
144 the signature itself. *data* is a *str* instance giving the data to which
145 the signature applies. *digest* is a *str* instance naming the message
146 digest type of the signature, for example :py:const:`sha1`.
147
148 .. versionadded:: 0.11
149
150
151.. _openssl-x509:
152
153X509 objects
154------------
155
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200156.. autoclass:: X509
157 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900158
159.. _openssl-x509name:
160
161X509Name objects
162----------------
163
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200164.. autoclass:: X509Name
165 :members:
166 :special-members:
167 :exclude-members: __repr__, __getattr__, __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900168
169.. _openssl-x509req:
170
171X509Req objects
172---------------
173
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200174.. autoclass:: X509Req
175 :members:
176 :special-members:
177 :exclude-members: __weakref__
Jean-Paul Calderone26e07d62014-03-02 08:08:23 -0500178
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900179.. _openssl-x509store:
180
181X509Store objects
182-----------------
183
Laurens Van Houtven8aeafdd2014-06-17 15:33:42 +0200184.. autoclass:: X509Store
185 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900186
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700187.. _openssl-x509storecontexterror:
188
Stephen Holsapple95a46652015-02-09 19:34:25 -0800189X509StoreContextError objects
190-----------------------------
191
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700192.. autoclass:: X509StoreContextError
193 :members:
Stephen Holsapple95a46652015-02-09 19:34:25 -0800194
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700195.. _openssl-x509storecontext:
Stephen Holsapple95a46652015-02-09 19:34:25 -0800196
Stephen Holsapple08ffaa62015-01-30 17:18:40 -0800197X509StoreContext objects
198------------------------
199
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700200.. autoclass:: X509StoreContext
201 :members:
Stephen Holsapple08ffaa62015-01-30 17:18:40 -0800202
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900203.. _openssl-pkey:
204
205PKey objects
206------------
207
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200208.. autoclass:: PKey
209 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900210
211.. _openssl-pkcs7:
212
Laurens Van Houtven9d4c0742015-04-20 11:58:39 -0700213.. py:data:: TYPE_RSA
214 TYPE_DSA
215
216 Key type constants.
217
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900218PKCS7 objects
219-------------
220
221PKCS7 objects have the following methods:
222
Jonathan Ballet6381da32011-07-20 16:43:38 +0900223.. py:method:: PKCS7.type_is_signed()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900224
225 FIXME
226
Jonathan Ballet6381da32011-07-20 16:43:38 +0900227.. py:method:: PKCS7.type_is_enveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900228
229 FIXME
230
Jonathan Ballet6381da32011-07-20 16:43:38 +0900231.. py:method:: PKCS7.type_is_signedAndEnveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900232
233 FIXME
234
Jonathan Ballet6381da32011-07-20 16:43:38 +0900235.. py:method:: PKCS7.type_is_data()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900236
237 FIXME
238
Jonathan Ballet6381da32011-07-20 16:43:38 +0900239.. py:method:: PKCS7.get_type_name()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900240
241 Get the type name of the PKCS7.
242
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900243.. _openssl-pkcs12:
244
245PKCS12 objects
246--------------
247
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +0200248.. autoclass:: PKCS12
249 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900250
251.. _openssl-509ext:
252
253X509Extension objects
254---------------------
255
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200256.. autoclass:: X509Extension
257 :members:
258 :special-members:
259 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900260
261.. _openssl-netscape-spki:
262
263NetscapeSPKI objects
264--------------------
265
Laurens Van Houtven59152b52014-06-19 16:42:30 +0200266.. autoclass:: NetscapeSPKI
267 :members:
268 :special-members:
269 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900270
Laurens Van Houtven889b9d22015-04-20 12:18:28 -0700271.. _crl:
272
273CRL objects
274-----------
275
276.. autoclass:: CRL
277 :members:
278 :special-members:
279 :exclude-members: __weakref__
280
281.. _revoked:
282
283Revoked objects
284---------------
285
286.. autoclass:: Revoked
287 :members:
288
Laurens Van Houtven3de6b2b2015-04-20 12:20:42 -0700289Exceptions
290----------
291
292.. py:exception:: Error
293
294 Generic exception used in the :py:mod:`.crypto` module.
295
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200296Digest names
297------------
298
299Several of the functions and methods in this module take a digest
300name. These must be strings describing a digest algorithm supported by
301OpenSSL (by ``EVP_get_digestbyname``, specifically). For example,
302:py:const:`b"md5"` or :py:const:`b"sha1"`.
303
304More information and a list of these digest names can be found in the
305``EVP_DigestInit(3)`` man page of your OpenSSL installation. This page
306can be found online for the latest version of OpenSSL:
307https://www.openssl.org/docs/crypto/EVP_DigestInit.html
308
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200309Backwards compatible type names
310-------------------------------
311
312When PyOpenSSL was originally written, the most current version of
313Python was 2.1. It made a distinction between classes and types. None
314of the versions of Python currently supported by PyOpenSSL still
315enforce that distinction: the type of an instance of an
316:py:class:`X509` object is now simply :py:class:`X509`. Originally,
317the type would have been :py:class:`X509Type`. These days,
318:py:class:`X509Type` and :py:class:`X509` are literally the same
319object. PyOpenSSL maintains these old names for backwards
320compatibility.
321
322Here's a table of these backwards-compatible names:
323
324========================= =============================
325Type name Backwards-compatible name
326========================= =============================
327:py:class:`X509` :py:class:`X509Type`
328:py:class:`X509Name` :py:class:`X509NameType`
329:py:class:`X509Req` :py:class:`X509ReqType`
330:py:class:`X509Store` :py:class:`X509StoreType`
331:py:class:`X509Extension` :py:class:`X509ExtensionType`
332:py:class:`PKey` :py:class:`PKeyType`
333:py:class:`PKCS7` :py:class:`PKCS7Type`
334:py:class:`PKCS12` :py:class:`PKCS12Type`
335:py:class:`NetscapeSPKI` :py:class:`NetscapeSPKIType`
336:py:class:`CRL` :py:class:`CRLType`
337========================= =============================
338
Laurens Van Houtven5faa2812015-04-23 10:44:10 -0700339Some objects, such as :py:class`Revoked`, don't have ``Type``
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200340equivalents, because they were added after the restriction had been
341lifted.