blob: e7190ab688e56d53975d1e9f0303c53cd2211f90 [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
Hynek Schlawack11e43ad2016-07-03 14:40:20 +020076.. autofunction:: dump_privatekey
Jonathan Balletc9e066c2011-07-17 22:56:05 +090077
Jonathan Balletc9e066c2011-07-17 22:56:05 +090078.. py:function:: load_privatekey(type, buffer[, passphrase])
79
80 Load a private key (PKey) from the string *buffer* encoded with the type
81 *type* (must be one of :py:const:`FILETYPE_PEM` and
82 :py:const:`FILETYPE_ASN1`).
83
84 *passphrase* must be either a string or a callback for providing the pass
85 phrase.
86
Cory Benfield25338c52015-10-28 22:19:18 +090087Public keys
88~~~~~~~~~~~
89
90.. autofunction:: dump_publickey
91
92.. autofunction:: load_publickey
93
Laurens Van Houtven07051d32014-06-19 12:00:30 +020094Certificate revocation lists
95~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090096
Dominic Chenf05b2122015-10-13 16:32:35 +000097.. autofunction:: dump_crl
98
Jonathan Balletc9e066c2011-07-17 22:56:05 +090099.. py:function:: load_crl(type, buffer)
100
101 Load Certificate Revocation List (CRL) data from a string *buffer*.
102 *buffer* encoded with the type *type*. The type *type* must either
103 :py:const:`FILETYPE_PEM` or :py:const:`FILETYPE_ASN1`).
104
105
106.. py:function:: load_pkcs7_data(type, buffer)
107
Laurens Van Houtven0f820872015-04-20 11:25:57 -0700108 Load pkcs7 data from the string *buffer* encoded with the type
109 *type*. The type *type* must either :py:const:`FILETYPE_PEM` or
110 :py:const:`FILETYPE_ASN1`).
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900111
112
113.. py:function:: load_pkcs12(buffer[, passphrase])
114
115 Load pkcs12 data from the string *buffer*. If the pkcs12 structure is
116 encrypted, a *passphrase* must be included. The MAC is always
117 checked and thus required.
118
119 See also the man page for the C function :py:func:`PKCS12_parse`.
120
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200121Signing and verifying signatures
122--------------------------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900123
124.. py:function:: sign(key, data, digest)
125
126 Sign a data string using the given key and message digest.
127
128 *key* is a :py:class:`PKey` instance. *data* is a ``str`` instance.
129 *digest* is a ``str`` naming a supported message digest type, for example
Alex Gaynor239e2d32016-09-11 12:36:35 -0400130 :py:const:`b"sha256"`.
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900131
132 .. versionadded:: 0.11
133
134
135.. py:function:: verify(certificate, signature, data, digest)
136
137 Verify the signature for a data string.
138
139 *certificate* is a :py:class:`X509` instance corresponding to the private
140 key which generated the signature. *signature* is a *str* instance giving
141 the signature itself. *data* is a *str* instance giving the data to which
142 the signature applies. *digest* is a *str* instance naming the message
Alex Gaynor239e2d32016-09-11 12:36:35 -0400143 digest type of the signature, for example :py:const:`b"sha256"`.
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900144
145 .. versionadded:: 0.11
146
147
148.. _openssl-x509:
149
150X509 objects
151------------
152
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200153.. autoclass:: X509
154 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900155
156.. _openssl-x509name:
157
158X509Name objects
159----------------
160
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200161.. autoclass:: X509Name
162 :members:
163 :special-members:
164 :exclude-members: __repr__, __getattr__, __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900165
166.. _openssl-x509req:
167
168X509Req objects
169---------------
170
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200171.. autoclass:: X509Req
172 :members:
173 :special-members:
174 :exclude-members: __weakref__
Jean-Paul Calderone26e07d62014-03-02 08:08:23 -0500175
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900176.. _openssl-x509store:
177
178X509Store objects
179-----------------
180
Laurens Van Houtven8aeafdd2014-06-17 15:33:42 +0200181.. autoclass:: X509Store
182 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900183
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700184.. _openssl-x509storecontexterror:
185
Stephen Holsapple95a46652015-02-09 19:34:25 -0800186X509StoreContextError objects
187-----------------------------
188
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700189.. autoclass:: X509StoreContextError
190 :members:
Stephen Holsapple95a46652015-02-09 19:34:25 -0800191
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700192.. _openssl-x509storecontext:
Stephen Holsapple95a46652015-02-09 19:34:25 -0800193
Stephen Holsapple08ffaa62015-01-30 17:18:40 -0800194X509StoreContext objects
195------------------------
196
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700197.. autoclass:: X509StoreContext
198 :members:
Stephen Holsapple08ffaa62015-01-30 17:18:40 -0800199
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900200.. _openssl-pkey:
201
Dan Sully44e767a2016-06-04 18:05:27 -0700202X509StoreFlags constants
203------------------------
204
205.. autoclass:: X509StoreFlags
206
207 .. data:: CRL_CHECK
208 .. data:: CRL_CHECK_ALL
209 .. data:: IGNORE_CRITICAL
210 .. data:: X509_STRICT
211 .. data:: ALLOW_PROXY_CERTS
212 .. data:: POLICY_CHECK
213 .. data:: EXPLICIT_POLICY
214 .. data:: INHIBIT_MAP
215 .. data:: NOTIFY_POLICY
216 .. data:: CHECK_SS_SIGNATURE
217 .. data:: CB_ISSUER_CHECK
218
219.. _openssl-x509storeflags:
220
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900221PKey objects
222------------
223
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200224.. autoclass:: PKey
225 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900226
227.. _openssl-pkcs7:
228
Laurens Van Houtven9d4c0742015-04-20 11:58:39 -0700229.. py:data:: TYPE_RSA
230 TYPE_DSA
231
232 Key type constants.
233
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900234PKCS7 objects
235-------------
236
237PKCS7 objects have the following methods:
238
Jonathan Ballet6381da32011-07-20 16:43:38 +0900239.. py:method:: PKCS7.type_is_signed()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900240
241 FIXME
242
Jonathan Ballet6381da32011-07-20 16:43:38 +0900243.. py:method:: PKCS7.type_is_enveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900244
245 FIXME
246
Jonathan Ballet6381da32011-07-20 16:43:38 +0900247.. py:method:: PKCS7.type_is_signedAndEnveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900248
249 FIXME
250
Jonathan Ballet6381da32011-07-20 16:43:38 +0900251.. py:method:: PKCS7.type_is_data()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900252
253 FIXME
254
Jonathan Ballet6381da32011-07-20 16:43:38 +0900255.. py:method:: PKCS7.get_type_name()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900256
257 Get the type name of the PKCS7.
258
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900259.. _openssl-pkcs12:
260
261PKCS12 objects
262--------------
263
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +0200264.. autoclass:: PKCS12
265 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900266
267.. _openssl-509ext:
268
269X509Extension objects
270---------------------
271
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200272.. autoclass:: X509Extension
273 :members:
274 :special-members:
275 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900276
277.. _openssl-netscape-spki:
278
279NetscapeSPKI objects
280--------------------
281
Laurens Van Houtven59152b52014-06-19 16:42:30 +0200282.. autoclass:: NetscapeSPKI
283 :members:
284 :special-members:
285 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900286
Laurens Van Houtven889b9d22015-04-20 12:18:28 -0700287.. _crl:
288
289CRL objects
290-----------
291
292.. autoclass:: CRL
293 :members:
294 :special-members:
295 :exclude-members: __weakref__
296
297.. _revoked:
298
299Revoked objects
300---------------
301
302.. autoclass:: Revoked
303 :members:
304
Laurens Van Houtven3de6b2b2015-04-20 12:20:42 -0700305Exceptions
306----------
307
308.. py:exception:: Error
309
310 Generic exception used in the :py:mod:`.crypto` module.
311
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100312
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200313Digest names
314------------
315
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100316Several of the functions and methods in this module take a digest name.
317These must be strings describing a digest algorithm supported by OpenSSL (by ``EVP_get_digestbyname``, specifically).
Alex Gaynor643aab82016-09-11 12:14:55 -0400318For example, :const:`b"sha256"` or :const:`b"sha384"`.
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200319
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100320More information and a list of these digest names can be found in the ``EVP_DigestInit(3)`` man page of your OpenSSL installation.
321This page can be found online for the latest version of OpenSSL:
Alex Chan54005ce2017-03-21 08:08:17 +0000322https://www.openssl.org/docs/manmaster/man3/EVP_DigestInit.html
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100323