blob: 04d7ccf20ce2b9498752b6b26e8fa3e1c536e826 [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
Paul Kehrerdc996b12017-07-19 21:09:04 +02009.. note::
10
11 `pyca/cryptography`_ is likely a better choice than using this module. It
12 contains a complete set of cryptographic primitives as well as a
13 significantly better and more powerful X509 API. If necessary you can
14 convert to and from cryptography objects using the ``to_cryptography``
15 and ``from_cryptography`` methods on ``X509``, ``X509Req``, ``CRL``,
16 and ``PKey``.
17
Laurens Van Houtven07051d32014-06-19 12:00:30 +020018Elliptic curves
19---------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +090020
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040021.. py:function:: get_elliptic_curves
22
23 Return a set of objects representing the elliptic curves supported in the
24 OpenSSL build in use.
25
26 The curve objects have a :py:class:`unicode` ``name`` attribute by which
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -040027 they identify themselves.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040028
29 The curve objects are useful as values for the argument accepted by
Jean-Paul Calderone3b04e352014-04-19 09:29:10 -040030 :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be
31 used for ECDHE key exchange.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040032
33
Akihiro Yamazakib8450442015-09-04 16:55:04 +090034.. py:function:: get_elliptic_curve(name)
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040035
Alex Gaynorb6e92df2015-09-04 07:48:35 -040036 Return a single curve object selected by *name*.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040037
38 See :py:func:`get_elliptic_curves` for information about curve objects.
39
40 If the named curve is not supported then :py:class:`ValueError` is raised.
41
42
Laurens Van Houtven07051d32014-06-19 12:00:30 +020043Serialization and deserialization
44---------------------------------
45
Cory Benfield47569252016-02-07 10:28:00 +000046The following serialization functions take one of these constants to determine the format.
Cory Benfield4d67d042016-01-22 18:42:13 +000047
Laurens Van Houtven07051d32014-06-19 12:00:30 +020048.. py:data:: FILETYPE_PEM
Cory Benfieldfb4d4fb2016-01-22 18:51:34 +000049
Cory Benfield47569252016-02-07 10:28:00 +000050: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 +000051
52.. py:data:: FILETYPE_ASN1
53
Cory Benfield47569252016-02-07 10:28:00 +000054: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 +020055
56Certificates
57~~~~~~~~~~~~
58
Jonathan Balletc9e066c2011-07-17 22:56:05 +090059.. py:function:: dump_certificate(type, cert)
60
61 Dump the certificate *cert* into a buffer string encoded with the type
62 *type*.
63
Laurens Van Houtven07051d32014-06-19 12:00:30 +020064.. py:function:: load_certificate(type, buffer)
65
66 Load a certificate (X509) from the string *buffer* encoded with the
67 type *type*.
68
69Certificate signing requests
70~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090071
72.. py:function:: dump_certificate_request(type, req)
73
74 Dump the certificate request *req* into a buffer string encoded with the
75 type *type*.
76
Laurens Van Houtven07051d32014-06-19 12:00:30 +020077.. py:function:: load_certificate_request(type, buffer)
78
79 Load a certificate request (X509Req) from the string *buffer* encoded with
80 the type *type*.
81
82Private keys
83~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090084
Hynek Schlawack11e43ad2016-07-03 14:40:20 +020085.. autofunction:: dump_privatekey
Jonathan Balletc9e066c2011-07-17 22:56:05 +090086
Jonathan Balletc9e066c2011-07-17 22:56:05 +090087.. py:function:: load_privatekey(type, buffer[, passphrase])
88
89 Load a private key (PKey) from the string *buffer* encoded with the type
90 *type* (must be one of :py:const:`FILETYPE_PEM` and
91 :py:const:`FILETYPE_ASN1`).
92
93 *passphrase* must be either a string or a callback for providing the pass
94 phrase.
95
Cory Benfield25338c52015-10-28 22:19:18 +090096Public keys
97~~~~~~~~~~~
98
99.. autofunction:: dump_publickey
100
101.. autofunction:: load_publickey
102
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200103Certificate revocation lists
104~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900105
Dominic Chenf05b2122015-10-13 16:32:35 +0000106.. autofunction:: dump_crl
107
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900108.. py:function:: load_crl(type, buffer)
109
110 Load Certificate Revocation List (CRL) data from a string *buffer*.
111 *buffer* encoded with the type *type*. The type *type* must either
112 :py:const:`FILETYPE_PEM` or :py:const:`FILETYPE_ASN1`).
113
114
115.. py:function:: load_pkcs7_data(type, buffer)
116
Laurens Van Houtven0f820872015-04-20 11:25:57 -0700117 Load pkcs7 data from the string *buffer* encoded with the type
118 *type*. The type *type* must either :py:const:`FILETYPE_PEM` or
119 :py:const:`FILETYPE_ASN1`).
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900120
121
122.. py:function:: load_pkcs12(buffer[, passphrase])
123
124 Load pkcs12 data from the string *buffer*. If the pkcs12 structure is
125 encrypted, a *passphrase* must be included. The MAC is always
126 checked and thus required.
127
128 See also the man page for the C function :py:func:`PKCS12_parse`.
129
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200130Signing and verifying signatures
131--------------------------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900132
133.. py:function:: sign(key, data, digest)
134
135 Sign a data string using the given key and message digest.
136
137 *key* is a :py:class:`PKey` instance. *data* is a ``str`` instance.
138 *digest* is a ``str`` naming a supported message digest type, for example
Alex Gaynor239e2d32016-09-11 12:36:35 -0400139 :py:const:`b"sha256"`.
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900140
141 .. versionadded:: 0.11
142
143
144.. py:function:: verify(certificate, signature, data, digest)
145
146 Verify the signature for a data string.
147
148 *certificate* is a :py:class:`X509` instance corresponding to the private
149 key which generated the signature. *signature* is a *str* instance giving
150 the signature itself. *data* is a *str* instance giving the data to which
151 the signature applies. *digest* is a *str* instance naming the message
Alex Gaynor239e2d32016-09-11 12:36:35 -0400152 digest type of the signature, for example :py:const:`b"sha256"`.
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900153
154 .. versionadded:: 0.11
155
156
157.. _openssl-x509:
158
159X509 objects
160------------
161
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200162.. autoclass:: X509
163 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900164
165.. _openssl-x509name:
166
167X509Name objects
168----------------
169
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200170.. autoclass:: X509Name
171 :members:
172 :special-members:
173 :exclude-members: __repr__, __getattr__, __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900174
175.. _openssl-x509req:
176
177X509Req objects
178---------------
179
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200180.. autoclass:: X509Req
181 :members:
182 :special-members:
183 :exclude-members: __weakref__
Jean-Paul Calderone26e07d62014-03-02 08:08:23 -0500184
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900185.. _openssl-x509store:
186
187X509Store objects
188-----------------
189
Laurens Van Houtven8aeafdd2014-06-17 15:33:42 +0200190.. autoclass:: X509Store
191 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900192
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700193.. _openssl-x509storecontexterror:
194
Stephen Holsapple95a46652015-02-09 19:34:25 -0800195X509StoreContextError objects
196-----------------------------
197
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700198.. autoclass:: X509StoreContextError
199 :members:
Stephen Holsapple95a46652015-02-09 19:34:25 -0800200
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700201.. _openssl-x509storecontext:
Stephen Holsapple95a46652015-02-09 19:34:25 -0800202
Stephen Holsapple08ffaa62015-01-30 17:18:40 -0800203X509StoreContext objects
204------------------------
205
Stephen Holsapple8ad4a192015-06-09 22:51:43 -0700206.. autoclass:: X509StoreContext
207 :members:
Stephen Holsapple08ffaa62015-01-30 17:18:40 -0800208
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900209.. _openssl-pkey:
210
Dan Sully44e767a2016-06-04 18:05:27 -0700211X509StoreFlags constants
212------------------------
213
214.. autoclass:: X509StoreFlags
215
216 .. data:: CRL_CHECK
217 .. data:: CRL_CHECK_ALL
218 .. data:: IGNORE_CRITICAL
219 .. data:: X509_STRICT
220 .. data:: ALLOW_PROXY_CERTS
221 .. data:: POLICY_CHECK
222 .. data:: EXPLICIT_POLICY
223 .. data:: INHIBIT_MAP
224 .. data:: NOTIFY_POLICY
225 .. data:: CHECK_SS_SIGNATURE
226 .. data:: CB_ISSUER_CHECK
227
228.. _openssl-x509storeflags:
229
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900230PKey objects
231------------
232
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200233.. autoclass:: PKey
234 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900235
236.. _openssl-pkcs7:
237
Laurens Van Houtven9d4c0742015-04-20 11:58:39 -0700238.. py:data:: TYPE_RSA
239 TYPE_DSA
240
241 Key type constants.
242
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900243PKCS7 objects
244-------------
245
246PKCS7 objects have the following methods:
247
Jonathan Ballet6381da32011-07-20 16:43:38 +0900248.. py:method:: PKCS7.type_is_signed()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900249
250 FIXME
251
Jonathan Ballet6381da32011-07-20 16:43:38 +0900252.. py:method:: PKCS7.type_is_enveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900253
254 FIXME
255
Jonathan Ballet6381da32011-07-20 16:43:38 +0900256.. py:method:: PKCS7.type_is_signedAndEnveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900257
258 FIXME
259
Jonathan Ballet6381da32011-07-20 16:43:38 +0900260.. py:method:: PKCS7.type_is_data()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900261
262 FIXME
263
Jonathan Ballet6381da32011-07-20 16:43:38 +0900264.. py:method:: PKCS7.get_type_name()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900265
266 Get the type name of the PKCS7.
267
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900268.. _openssl-pkcs12:
269
270PKCS12 objects
271--------------
272
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +0200273.. autoclass:: PKCS12
274 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900275
276.. _openssl-509ext:
277
278X509Extension objects
279---------------------
280
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200281.. autoclass:: X509Extension
282 :members:
283 :special-members:
284 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900285
286.. _openssl-netscape-spki:
287
288NetscapeSPKI objects
289--------------------
290
Laurens Van Houtven59152b52014-06-19 16:42:30 +0200291.. autoclass:: NetscapeSPKI
292 :members:
293 :special-members:
294 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900295
Laurens Van Houtven889b9d22015-04-20 12:18:28 -0700296.. _crl:
297
298CRL objects
299-----------
300
301.. autoclass:: CRL
302 :members:
303 :special-members:
304 :exclude-members: __weakref__
305
306.. _revoked:
307
308Revoked objects
309---------------
310
311.. autoclass:: Revoked
312 :members:
313
Laurens Van Houtven3de6b2b2015-04-20 12:20:42 -0700314Exceptions
315----------
316
317.. py:exception:: Error
318
319 Generic exception used in the :py:mod:`.crypto` module.
320
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100321
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200322Digest names
323------------
324
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100325Several of the functions and methods in this module take a digest name.
326These must be strings describing a digest algorithm supported by OpenSSL (by ``EVP_get_digestbyname``, specifically).
Alex Gaynor643aab82016-09-11 12:14:55 -0400327For example, :const:`b"sha256"` or :const:`b"sha384"`.
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200328
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100329More information and a list of these digest names can be found in the ``EVP_DigestInit(3)`` man page of your OpenSSL installation.
330This page can be found online for the latest version of OpenSSL:
Alex Chan54005ce2017-03-21 08:08:17 +0000331https://www.openssl.org/docs/manmaster/man3/EVP_DigestInit.html
Hynek Schlawack8d4f9762016-03-19 08:15:03 +0100332
Paul Kehrerdc996b12017-07-19 21:09:04 +0200333.. _`pyca/cryptography`: https://cryptography.io