blob: b5636a7628ee9a0963e3fa6f0794bf7a68299eb3 [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
Jonathan Balletc9e066c2011-07-17 22:56:05 +09009.. py:class:: CRL()
10
11 A class representing Certifcate Revocation List objects.
12
13
14.. py:class:: Revoked()
15
16 A class representing Revocation objects of CRL.
17
Jonathan Balletc9e066c2011-07-17 22:56:05 +090018.. py:data:: TYPE_RSA
Jonathan Ballet6381da32011-07-20 16:43:38 +090019 TYPE_DSA
Jonathan Balletc9e066c2011-07-17 22:56:05 +090020
21 Key type constants.
22
Jonathan Balletc9e066c2011-07-17 22:56:05 +090023.. py:exception:: Error
24
25 Generic exception used in the :py:mod:`.crypto` module.
26
Laurens Van Houtven07051d32014-06-19 12:00:30 +020027Elliptic curves
28---------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +090029
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040030.. py:function:: get_elliptic_curves
31
32 Return a set of objects representing the elliptic curves supported in the
33 OpenSSL build in use.
34
35 The curve objects have a :py:class:`unicode` ``name`` attribute by which
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -040036 they identify themselves.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040037
38 The curve objects are useful as values for the argument accepted by
Jean-Paul Calderone3b04e352014-04-19 09:29:10 -040039 :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be
40 used for ECDHE key exchange.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040041
42
43.. py:function:: get_elliptic_curve
44
45 Return a single curve object selected by name.
46
47 See :py:func:`get_elliptic_curves` for information about curve objects.
48
49 If the named curve is not supported then :py:class:`ValueError` is raised.
50
51
Laurens Van Houtven07051d32014-06-19 12:00:30 +020052Serialization and deserialization
53---------------------------------
54
55The following serialization functions take one of these constants to
56determine the format:
57
58.. py:data:: FILETYPE_PEM
59 FILETYPE_ASN1
60
61Certificates
62~~~~~~~~~~~~
63
Jonathan Balletc9e066c2011-07-17 22:56:05 +090064.. py:function:: dump_certificate(type, cert)
65
66 Dump the certificate *cert* into a buffer string encoded with the type
67 *type*.
68
Laurens Van Houtven07051d32014-06-19 12:00:30 +020069.. py:function:: load_certificate(type, buffer)
70
71 Load a certificate (X509) from the string *buffer* encoded with the
72 type *type*.
73
74Certificate signing requests
75~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090076
77.. py:function:: dump_certificate_request(type, req)
78
79 Dump the certificate request *req* into a buffer string encoded with the
80 type *type*.
81
Laurens Van Houtven07051d32014-06-19 12:00:30 +020082.. py:function:: load_certificate_request(type, buffer)
83
84 Load a certificate request (X509Req) from the string *buffer* encoded with
85 the type *type*.
86
87Private keys
88~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090089
90.. py:function:: dump_privatekey(type, pkey[, cipher, passphrase])
91
92 Dump the private key *pkey* into a buffer string encoded with the type
93 *type*, optionally (if *type* is :py:const:`FILETYPE_PEM`) encrypting it
94 using *cipher* and *passphrase*.
95
96 *passphrase* must be either a string or a callback for providing the
97 pass phrase.
98
Jonathan Balletc9e066c2011-07-17 22:56:05 +090099.. py:function:: load_privatekey(type, buffer[, passphrase])
100
101 Load a private key (PKey) from the string *buffer* encoded with the type
102 *type* (must be one of :py:const:`FILETYPE_PEM` and
103 :py:const:`FILETYPE_ASN1`).
104
105 *passphrase* must be either a string or a callback for providing the pass
106 phrase.
107
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200108Certificate revocation lists
109~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900110
111.. py:function:: load_crl(type, buffer)
112
113 Load Certificate Revocation List (CRL) data from a string *buffer*.
114 *buffer* encoded with the type *type*. The type *type* must either
115 :py:const:`FILETYPE_PEM` or :py:const:`FILETYPE_ASN1`).
116
117
118.. py:function:: load_pkcs7_data(type, buffer)
119
120 Load pkcs7 data from the string *buffer* encoded with the type *type*.
121
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
194.. _openssl-pkey:
195
196PKey objects
197------------
198
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200199.. autoclass:: PKey
200 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900201
202.. _openssl-pkcs7:
203
204PKCS7 objects
205-------------
206
207PKCS7 objects have the following methods:
208
Jonathan Ballet6381da32011-07-20 16:43:38 +0900209.. py:method:: PKCS7.type_is_signed()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900210
211 FIXME
212
Jonathan Ballet6381da32011-07-20 16:43:38 +0900213.. py:method:: PKCS7.type_is_enveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900214
215 FIXME
216
Jonathan Ballet6381da32011-07-20 16:43:38 +0900217.. py:method:: PKCS7.type_is_signedAndEnveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900218
219 FIXME
220
Jonathan Ballet6381da32011-07-20 16:43:38 +0900221.. py:method:: PKCS7.type_is_data()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900222
223 FIXME
224
Jonathan Ballet6381da32011-07-20 16:43:38 +0900225.. py:method:: PKCS7.get_type_name()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900226
227 Get the type name of the PKCS7.
228
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900229.. _openssl-pkcs12:
230
231PKCS12 objects
232--------------
233
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +0200234.. autoclass:: PKCS12
235 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900236
237.. _openssl-509ext:
238
239X509Extension objects
240---------------------
241
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200242.. autoclass:: X509Extension
243 :members:
244 :special-members:
245 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900246
247.. _openssl-netscape-spki:
248
249NetscapeSPKI objects
250--------------------
251
Laurens Van Houtven59152b52014-06-19 16:42:30 +0200252.. autoclass:: NetscapeSPKI
253 :members:
254 :special-members:
255 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900256
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900257.. _crl:
258
259CRL objects
260-----------
261
262CRL objects have the following methods:
263
264.. py:method:: CRL.add_revoked(revoked)
265
266 Add a Revoked object to the CRL, by value not reference.
267
268
269.. py:method:: CRL.export(cert, key[, type=FILETYPE_PEM][, days=100])
270
271 Use *cert* and *key* to sign the CRL and return the CRL as a string.
272 *days* is the number of days before the next CRL is due.
273
274
275.. py:method:: CRL.get_revoked()
276
277 Return a tuple of Revoked objects, by value not reference.
278
279
280.. _revoked:
281
282Revoked objects
283---------------
284
285Revoked objects have the following methods:
286
287.. py:method:: Revoked.all_reasons()
288
289 Return a list of all supported reasons.
290
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900291.. py:method:: Revoked.get_reason()
292
293 Return the revocation reason as a str. Can be
294 None, which differs from "Unspecified".
295
296
297.. py:method:: Revoked.get_rev_date()
298
299 Return the revocation date as a str.
300 The string is formatted as an ASN1 GENERALIZEDTIME.
301
302
303.. py:method:: Revoked.get_serial()
304
305 Return a str containing a hex number of the serial of the revoked certificate.
306
307
308.. py:method:: Revoked.set_reason(reason)
309
310 Set the revocation reason. *reason* must be None or a string, but the
311 values are limited. Spaces and case are ignored. See
312 :py:meth:`all_reasons`.
313
314
315.. py:method:: Revoked.set_rev_date(date)
316
317 Set the revocation date.
318 The string is formatted as an ASN1 GENERALIZEDTIME.
319
320
321.. py:method:: Revoked.set_serial(serial)
322
323 *serial* is a string containing a hex number of the serial of the revoked certificate.
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200324
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200325Digest names
326------------
327
328Several of the functions and methods in this module take a digest
329name. These must be strings describing a digest algorithm supported by
330OpenSSL (by ``EVP_get_digestbyname``, specifically). For example,
331:py:const:`b"md5"` or :py:const:`b"sha1"`.
332
333More information and a list of these digest names can be found in the
334``EVP_DigestInit(3)`` man page of your OpenSSL installation. This page
335can be found online for the latest version of OpenSSL:
336https://www.openssl.org/docs/crypto/EVP_DigestInit.html
337
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200338Backwards compatible type names
339-------------------------------
340
341When PyOpenSSL was originally written, the most current version of
342Python was 2.1. It made a distinction between classes and types. None
343of the versions of Python currently supported by PyOpenSSL still
344enforce that distinction: the type of an instance of an
345:py:class:`X509` object is now simply :py:class:`X509`. Originally,
346the type would have been :py:class:`X509Type`. These days,
347:py:class:`X509Type` and :py:class:`X509` are literally the same
348object. PyOpenSSL maintains these old names for backwards
349compatibility.
350
351Here's a table of these backwards-compatible names:
352
353========================= =============================
354Type name Backwards-compatible name
355========================= =============================
356:py:class:`X509` :py:class:`X509Type`
357:py:class:`X509Name` :py:class:`X509NameType`
358:py:class:`X509Req` :py:class:`X509ReqType`
359:py:class:`X509Store` :py:class:`X509StoreType`
360:py:class:`X509Extension` :py:class:`X509ExtensionType`
361:py:class:`PKey` :py:class:`PKeyType`
362:py:class:`PKCS7` :py:class:`PKCS7Type`
363:py:class:`PKCS12` :py:class:`PKCS12Type`
364:py:class:`NetscapeSPKI` :py:class:`NetscapeSPKIType`
365:py:class:`CRL` :py:class:`CRLType`
366========================= =============================
367
368Soem objects, such as py:class`Revoked`, don't have ``Type``
369equivalents, because they were added after the restriction had been
370lifted.