blob: 5f7df24cbdd0f4d38073087285c926b523ea62be [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
Cory Benfield47569252016-02-07 10:28:00 +000037The following serialization functions take one of these constants to determine the format.
Cory Benfield4d67d042016-01-22 18:42:13 +000038
Laurens Van Houtven07051d32014-06-19 12:00:30 +020039.. py:data:: FILETYPE_PEM
Cory Benfieldfb4d4fb2016-01-22 18:51:34 +000040
Cory Benfield47569252016-02-07 10:28:00 +000041:data:`FILETYPE_PEM` serializes data to a Base64-encoded encoded representation of the underlying ASN.1 data structure. This representation includes delimiters that define what data structure is contained within the Base64-encoded block: for example, for a certificate, the delimiters are ``-----BEGIN CERTIFICATE-----`` and ``-----END CERTIFICATE-----``.
Cory Benfieldfb4d4fb2016-01-22 18:51:34 +000042
43.. py:data:: FILETYPE_ASN1
44
Cory Benfield47569252016-02-07 10:28:00 +000045:data:`FILETYPE_ASN1` serializes data to the underlying ASN.1 data structure. The format used by :data:`FILETYPE_ASN1` is also sometimes referred to as DER.
Laurens Van Houtven07051d32014-06-19 12:00:30 +020046
47Certificates
48~~~~~~~~~~~~
49
Jonathan Balletc9e066c2011-07-17 22:56:05 +090050.. py:function:: dump_certificate(type, cert)
51
52 Dump the certificate *cert* into a buffer string encoded with the type
53 *type*.
54
Laurens Van Houtven07051d32014-06-19 12:00:30 +020055.. py:function:: load_certificate(type, buffer)
56
57 Load a certificate (X509) from the string *buffer* encoded with the
58 type *type*.
59
60Certificate signing requests
61~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090062
63.. py:function:: dump_certificate_request(type, req)
64
65 Dump the certificate request *req* into a buffer string encoded with the
66 type *type*.
67
Laurens Van Houtven07051d32014-06-19 12:00:30 +020068.. py:function:: load_certificate_request(type, buffer)
69
70 Load a certificate request (X509Req) from the string *buffer* encoded with
71 the type *type*.
72
73Private keys
74~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090075
76.. py:function:: dump_privatekey(type, pkey[, cipher, passphrase])
77
78 Dump the private key *pkey* into a buffer string encoded with the type
79 *type*, optionally (if *type* is :py:const:`FILETYPE_PEM`) encrypting it
80 using *cipher* and *passphrase*.
81
82 *passphrase* must be either a string or a callback for providing the
83 pass phrase.
84
Jonathan Balletc9e066c2011-07-17 22:56:05 +090085.. py:function:: load_privatekey(type, buffer[, passphrase])
86
87 Load a private key (PKey) from the string *buffer* encoded with the type
88 *type* (must be one of :py:const:`FILETYPE_PEM` and
89 :py:const:`FILETYPE_ASN1`).
90
91 *passphrase* must be either a string or a callback for providing the pass
92 phrase.
93
Cory Benfield25338c52015-10-28 22:19:18 +090094Public keys
95~~~~~~~~~~~
96
97.. autofunction:: dump_publickey
98
99.. autofunction:: load_publickey
100
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200101Certificate revocation lists
102~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900103
Dominic Chenf05b2122015-10-13 16:32:35 +0000104.. autofunction:: dump_crl
105
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900106.. py:function:: load_crl(type, buffer)
107
108 Load Certificate Revocation List (CRL) data from a string *buffer*.
109 *buffer* encoded with the type *type*. The type *type* must either
110 :py:const:`FILETYPE_PEM` or :py:const:`FILETYPE_ASN1`).
111
112
113.. py:function:: load_pkcs7_data(type, buffer)
114
Laurens Van Houtven0f820872015-04-20 11:25:57 -0700115 Load pkcs7 data from the string *buffer* encoded with the type
116 *type*. The type *type* must either :py:const:`FILETYPE_PEM` or
117 :py:const:`FILETYPE_ASN1`).
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900118
119
120.. py:function:: load_pkcs12(buffer[, passphrase])
121
122 Load pkcs12 data from the string *buffer*. If the pkcs12 structure is
123 encrypted, a *passphrase* must be included. The MAC is always
124 checked and thus required.
125
126 See also the man page for the C function :py:func:`PKCS12_parse`.
127
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200128Signing and verifying signatures
129--------------------------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900130
131.. py:function:: sign(key, data, digest)
132
133 Sign a data string using the given key and message digest.
134
135 *key* is a :py:class:`PKey` instance. *data* is a ``str`` instance.
136 *digest* is a ``str`` naming a supported message digest type, for example
137 :py:const:`sha1`.
138
139 .. versionadded:: 0.11
140
141
142.. py:function:: verify(certificate, signature, data, digest)
143
144 Verify the signature for a data string.
145
146 *certificate* is a :py:class:`X509` instance corresponding to the private
147 key which generated the signature. *signature* is a *str* instance giving
148 the signature itself. *data* is a *str* instance giving the data to which
149 the signature applies. *digest* is a *str* instance naming the message
150 digest type of the signature, for example :py:const:`sha1`.
151
152 .. versionadded:: 0.11
153
154
155.. _openssl-x509:
156
157X509 objects
158------------
159
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200160.. autoclass:: X509
161 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900162
163.. _openssl-x509name:
164
165X509Name objects
166----------------
167
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200168.. autoclass:: X509Name
169 :members:
170 :special-members:
171 :exclude-members: __repr__, __getattr__, __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900172
173.. _openssl-x509req:
174
175X509Req objects
176---------------
177
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200178.. autoclass:: X509Req
179 :members:
180 :special-members:
181 :exclude-members: __weakref__
Jean-Paul Calderone26e07d62014-03-02 08:08:23 -0500182
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900183.. _openssl-x509store:
184
185X509Store objects
186-----------------
187
Laurens Van Houtven8aeafdd2014-06-17 15:33:42 +0200188.. autoclass:: X509Store
189 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900190
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700191.. _openssl-x509storecontexterror:
192
Stephen Holsapple95a46652015-02-09 19:34:25 -0800193X509StoreContextError objects
194-----------------------------
195
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700196.. autoclass:: X509StoreContextError
197 :members:
Stephen Holsapple95a46652015-02-09 19:34:25 -0800198
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700199.. _openssl-x509storecontext:
Stephen Holsapple95a46652015-02-09 19:34:25 -0800200
Stephen Holsapple08ffaa62015-01-30 17:18:40 -0800201X509StoreContext objects
202------------------------
203
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700204.. autoclass:: X509StoreContext
205 :members:
Stephen Holsapple08ffaa62015-01-30 17:18:40 -0800206
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900207.. _openssl-pkey:
208
Dan Sully44e767a2016-06-04 18:05:27 -0700209X509StoreFlags constants
210------------------------
211
212.. autoclass:: X509StoreFlags
213
214 .. data:: CRL_CHECK
215 .. data:: CRL_CHECK_ALL
216 .. data:: IGNORE_CRITICAL
217 .. data:: X509_STRICT
218 .. data:: ALLOW_PROXY_CERTS
219 .. data:: POLICY_CHECK
220 .. data:: EXPLICIT_POLICY
221 .. data:: INHIBIT_MAP
222 .. data:: NOTIFY_POLICY
223 .. data:: CHECK_SS_SIGNATURE
224 .. data:: CB_ISSUER_CHECK
225
226.. _openssl-x509storeflags:
227
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900228PKey objects
229------------
230
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200231.. autoclass:: PKey
232 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900233
234.. _openssl-pkcs7:
235
Laurens Van Houtven9d4c0742015-04-20 11:58:39 -0700236.. py:data:: TYPE_RSA
237 TYPE_DSA
238
239 Key type constants.
240
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900241PKCS7 objects
242-------------
243
244PKCS7 objects have the following methods:
245
Jonathan Ballet6381da32011-07-20 16:43:38 +0900246.. py:method:: PKCS7.type_is_signed()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900247
248 FIXME
249
Jonathan Ballet6381da32011-07-20 16:43:38 +0900250.. py:method:: PKCS7.type_is_enveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900251
252 FIXME
253
Jonathan Ballet6381da32011-07-20 16:43:38 +0900254.. py:method:: PKCS7.type_is_signedAndEnveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900255
256 FIXME
257
Jonathan Ballet6381da32011-07-20 16:43:38 +0900258.. py:method:: PKCS7.type_is_data()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900259
260 FIXME
261
Jonathan Ballet6381da32011-07-20 16:43:38 +0900262.. py:method:: PKCS7.get_type_name()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900263
264 Get the type name of the PKCS7.
265
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900266.. _openssl-pkcs12:
267
268PKCS12 objects
269--------------
270
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +0200271.. autoclass:: PKCS12
272 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900273
274.. _openssl-509ext:
275
276X509Extension objects
277---------------------
278
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200279.. autoclass:: X509Extension
280 :members:
281 :special-members:
282 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900283
284.. _openssl-netscape-spki:
285
286NetscapeSPKI objects
287--------------------
288
Laurens Van Houtven59152b52014-06-19 16:42:30 +0200289.. autoclass:: NetscapeSPKI
290 :members:
291 :special-members:
292 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900293
Laurens Van Houtven889b9d22015-04-20 12:18:28 -0700294.. _crl:
295
296CRL objects
297-----------
298
299.. autoclass:: CRL
300 :members:
301 :special-members:
302 :exclude-members: __weakref__
303
304.. _revoked:
305
306Revoked objects
307---------------
308
309.. autoclass:: Revoked
310 :members:
311
Laurens Van Houtven3de6b2b2015-04-20 12:20:42 -0700312Exceptions
313----------
314
315.. py:exception:: Error
316
317 Generic exception used in the :py:mod:`.crypto` module.
318
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100319
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200320Digest names
321------------
322
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100323Several of the functions and methods in this module take a digest name.
324These must be strings describing a digest algorithm supported by OpenSSL (by ``EVP_get_digestbyname``, specifically).
325For example, :const:`b"md5"` or :const:`b"sha1"`.
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200326
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100327More information and a list of these digest names can be found in the ``EVP_DigestInit(3)`` man page of your OpenSSL installation.
328This page can be found online for the latest version of OpenSSL:
329https://www.openssl.org/docs/manmaster/crypto/EVP_DigestInit.html
330
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200331
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200332Backwards compatible type names
333-------------------------------
334
Hynek Schlawack890eb6d2016-03-19 08:17:43 +0100335When pyOpenSSL was originally written, the most current version of Python was 2.1.
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100336It made a distinction between classes and types.
337None of the versions of Python currently supported by pyOpenSSL still enforce that distinction:
338the type of an instance of an :class:`X509` object is now simply :class:`X509`.
339Originally, the type would have been :class:`X509Type`.
340These days, :class:`X509Type` and :class:`X509` are literally the same object.
341pyOpenSSL maintains these old names for backwards compatibility.
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200342
343Here's a table of these backwards-compatible names:
344
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100345====================== ==========================
346Type name Backwards-compatible name
347====================== ==========================
348:class:`X509` :class:`X509Type`
349:class:`X509Name` :class:`X509NameType`
350:class:`X509Req` :class:`X509ReqType`
351:class:`X509Store` :class:`X509StoreType`
352:class:`X509Extension` :class:`X509ExtensionType`
353:class:`PKey` :class:`PKeyType`
354:class:`PKCS7` :class:`PKCS7Type`
355:class:`PKCS12` :class:`PKCS12Type`
356:class:`NetscapeSPKI` :class:`NetscapeSPKIType`
357:class:`CRL` :class:`CRLType`
358====================== ==========================
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200359
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100360Some objects, such as :class:`Revoked`, don't have ``Type`` equivalents, because they were added after the restriction had been lifted.