blob: 23d8521ecbc1984b5d73f1a387a008a5fd585342 [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
9
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070010.. py:data:: X509StoreContext
11
12 A class representing the X.509 store context.
Jonathan Balletc9e066c2011-07-17 22:56:05 +090013
Jonathan Balletc9e066c2011-07-17 22:56:05 +090014.. py:data:: PKCS12Type
15
16 A Python type object representing the PKCS12 object type.
17
18
Jonathan Balletc9e066c2011-07-17 22:56:05 +090019.. py:class:: X509Extension(typename, critical, value[, subject][, issuer])
20
21 A class representing an X.509 v3 certificate extensions. See
22 http://openssl.org/docs/apps/x509v3_config.html#STANDARD_EXTENSIONS for
23 *typename* strings and their options. Optional parameters *subject* and
24 *issuer* must be X509 objects.
25
26
27.. py:data:: NetscapeSPKIType
28
29 See :py:class:`NetscapeSPKI`.
30
31
32.. py:class:: NetscapeSPKI([enc])
33
34 A class representing Netscape SPKI objects.
35
36 If the *enc* argument is present, it should be a base64-encoded string
37 representing a NetscapeSPKI object, as returned by the :py:meth:`b64_encode`
38 method.
39
40
41.. py:class:: CRL()
42
43 A class representing Certifcate Revocation List objects.
44
45
46.. py:class:: Revoked()
47
48 A class representing Revocation objects of CRL.
49
Jonathan Balletc9e066c2011-07-17 22:56:05 +090050.. py:exception:: Error
51
52 Generic exception used in the :py:mod:`.crypto` module.
53
Laurens Van Houtven07051d32014-06-19 12:00:30 +020054Elliptic curves
55---------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +090056
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040057.. py:function:: get_elliptic_curves
58
59 Return a set of objects representing the elliptic curves supported in the
60 OpenSSL build in use.
61
62 The curve objects have a :py:class:`unicode` ``name`` attribute by which
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -040063 they identify themselves.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040064
65 The curve objects are useful as values for the argument accepted by
Jean-Paul Calderone3b04e352014-04-19 09:29:10 -040066 :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be
67 used for ECDHE key exchange.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040068
69
70.. py:function:: get_elliptic_curve
71
72 Return a single curve object selected by name.
73
74 See :py:func:`get_elliptic_curves` for information about curve objects.
75
76 If the named curve is not supported then :py:class:`ValueError` is raised.
77
78
Laurens Van Houtven07051d32014-06-19 12:00:30 +020079Serialization and deserialization
80---------------------------------
81
82The following serialization functions take one of these constants to
83determine the format:
84
85.. py:data:: FILETYPE_PEM
86 FILETYPE_ASN1
87
88Certificates
89~~~~~~~~~~~~
90
Jonathan Balletc9e066c2011-07-17 22:56:05 +090091.. py:function:: dump_certificate(type, cert)
92
93 Dump the certificate *cert* into a buffer string encoded with the type
94 *type*.
95
Laurens Van Houtven07051d32014-06-19 12:00:30 +020096.. py:function:: load_certificate(type, buffer)
97
98 Load a certificate (X509) from the string *buffer* encoded with the
99 type *type*.
100
101Certificate signing requests
102~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900103
104.. py:function:: dump_certificate_request(type, req)
105
106 Dump the certificate request *req* into a buffer string encoded with the
107 type *type*.
108
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200109.. py:function:: load_certificate_request(type, buffer)
110
111 Load a certificate request (X509Req) from the string *buffer* encoded with
112 the type *type*.
113
114Private keys
115~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900116
117.. py:function:: dump_privatekey(type, pkey[, cipher, passphrase])
118
119 Dump the private key *pkey* into a buffer string encoded with the type
120 *type*, optionally (if *type* is :py:const:`FILETYPE_PEM`) encrypting it
121 using *cipher* and *passphrase*.
122
123 *passphrase* must be either a string or a callback for providing the
124 pass phrase.
125
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900126.. py:function:: load_privatekey(type, buffer[, passphrase])
127
128 Load a private key (PKey) from the string *buffer* encoded with the type
129 *type* (must be one of :py:const:`FILETYPE_PEM` and
130 :py:const:`FILETYPE_ASN1`).
131
132 *passphrase* must be either a string or a callback for providing the pass
133 phrase.
134
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200135Certificate revocation lists
136~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900137
138.. py:function:: load_crl(type, buffer)
139
140 Load Certificate Revocation List (CRL) data from a string *buffer*.
141 *buffer* encoded with the type *type*. The type *type* must either
142 :py:const:`FILETYPE_PEM` or :py:const:`FILETYPE_ASN1`).
143
144
145.. py:function:: load_pkcs7_data(type, buffer)
146
Laurens Van Houtven0f820872015-04-20 11:25:57 -0700147 Load pkcs7 data from the string *buffer* encoded with the type
148 *type*. The type *type* must either :py:const:`FILETYPE_PEM` or
149 :py:const:`FILETYPE_ASN1`).
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900150
151
152.. py:function:: load_pkcs12(buffer[, passphrase])
153
154 Load pkcs12 data from the string *buffer*. If the pkcs12 structure is
155 encrypted, a *passphrase* must be included. The MAC is always
156 checked and thus required.
157
158 See also the man page for the C function :py:func:`PKCS12_parse`.
159
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200160Signing and verifying signatures
161--------------------------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900162
163.. py:function:: sign(key, data, digest)
164
165 Sign a data string using the given key and message digest.
166
167 *key* is a :py:class:`PKey` instance. *data* is a ``str`` instance.
168 *digest* is a ``str`` naming a supported message digest type, for example
169 :py:const:`sha1`.
170
171 .. versionadded:: 0.11
172
173
174.. py:function:: verify(certificate, signature, data, digest)
175
176 Verify the signature for a data string.
177
178 *certificate* is a :py:class:`X509` instance corresponding to the private
179 key which generated the signature. *signature* is a *str* instance giving
180 the signature itself. *data* is a *str* instance giving the data to which
181 the signature applies. *digest* is a *str* instance naming the message
182 digest type of the signature, for example :py:const:`sha1`.
183
184 .. versionadded:: 0.11
185
186
187.. _openssl-x509:
188
189X509 objects
190------------
191
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200192.. autoclass:: X509
193 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900194
195.. _openssl-x509name:
196
197X509Name objects
198----------------
199
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200200.. autoclass:: X509Name
201 :members:
202 :special-members:
203 :exclude-members: __repr__, __getattr__, __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900204
205.. _openssl-x509req:
206
207X509Req objects
208---------------
209
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200210.. autoclass:: X509Req
211 :members:
212 :special-members:
213 :exclude-members: __weakref__
Jean-Paul Calderone26e07d62014-03-02 08:08:23 -0500214
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900215.. _openssl-x509store:
216
217X509Store objects
218-----------------
219
Laurens Van Houtven8aeafdd2014-06-17 15:33:42 +0200220.. autoclass:: X509Store
221 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900222
Stephen Holsapple95a46652015-02-09 19:34:25 -0800223X509StoreContextError objects
224-----------------------------
225
226The X509StoreContextError is an exception raised from
227`X509StoreContext.verify_certificate` in circumstances where a certificate
228cannot be verified in a provided context.
229
Jean-Paul Calderone876b2ac2015-03-15 16:17:19 -0400230The certificate for which the verification error was detected is given by the
231``certificate`` attribute of the exception instance as a :class:`X509`
232instance.
233
Laurens Van Houtven9313a4e2015-04-20 11:27:28 -0700234Details about the verification error are given in the exception's
235``args`` attribute.
Stephen Holsapple95a46652015-02-09 19:34:25 -0800236
Stephen Holsapple08ffaa62015-01-30 17:18:40 -0800237X509StoreContext objects
238------------------------
239
240The X509StoreContext object is used for verifying a certificate against a set
241of trusted certificates.
242
243
244.. py:method:: X509StoreContext.verify_certificate()
245
246 Verify a certificate in the context of this initialized `X509StoreContext`.
247 On error, raises `X509StoreContextError`, otherwise does nothing.
248
249 .. versionadded:: 0.15
250
251
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900252.. _openssl-pkey:
253
254PKey objects
255------------
256
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200257.. autoclass:: PKey
258 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900259
260.. _openssl-pkcs7:
261
Laurens Van Houtven9d4c0742015-04-20 11:58:39 -0700262.. py:data:: TYPE_RSA
263 TYPE_DSA
264
265 Key type constants.
266
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900267PKCS7 objects
268-------------
269
270PKCS7 objects have the following methods:
271
Jonathan Ballet6381da32011-07-20 16:43:38 +0900272.. py:method:: PKCS7.type_is_signed()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900273
274 FIXME
275
Jonathan Ballet6381da32011-07-20 16:43:38 +0900276.. py:method:: PKCS7.type_is_enveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900277
278 FIXME
279
Jonathan Ballet6381da32011-07-20 16:43:38 +0900280.. py:method:: PKCS7.type_is_signedAndEnveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900281
282 FIXME
283
Jonathan Ballet6381da32011-07-20 16:43:38 +0900284.. py:method:: PKCS7.type_is_data()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900285
286 FIXME
287
Jonathan Ballet6381da32011-07-20 16:43:38 +0900288.. py:method:: PKCS7.get_type_name()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900289
290 Get the type name of the PKCS7.
291
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900292.. _openssl-pkcs12:
293
294PKCS12 objects
295--------------
296
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +0200297.. autoclass:: PKCS12
298 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900299
300.. _openssl-509ext:
301
302X509Extension objects
303---------------------
304
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200305.. autoclass:: X509Extension
306 :members:
307 :special-members:
308 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900309
310.. _openssl-netscape-spki:
311
312NetscapeSPKI objects
313--------------------
314
Laurens Van Houtven59152b52014-06-19 16:42:30 +0200315.. autoclass:: NetscapeSPKI
316 :members:
317 :special-members:
318 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900319
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900320.. _crl:
321
322CRL objects
323-----------
324
Laurens Van Houtvencb32e852014-06-19 17:36:28 +0200325.. autoclass:: CRL
326 :members:
327 :special-members:
328 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900329
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900330.. _revoked:
331
332Revoked objects
333---------------
334
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +0200335.. autoclass:: Revoked
336 :members:
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200337
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200338Digest names
339------------
340
341Several of the functions and methods in this module take a digest
342name. These must be strings describing a digest algorithm supported by
343OpenSSL (by ``EVP_get_digestbyname``, specifically). For example,
344:py:const:`b"md5"` or :py:const:`b"sha1"`.
345
346More information and a list of these digest names can be found in the
347``EVP_DigestInit(3)`` man page of your OpenSSL installation. This page
348can be found online for the latest version of OpenSSL:
349https://www.openssl.org/docs/crypto/EVP_DigestInit.html
350
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200351Backwards compatible type names
352-------------------------------
353
354When PyOpenSSL was originally written, the most current version of
355Python was 2.1. It made a distinction between classes and types. None
356of the versions of Python currently supported by PyOpenSSL still
357enforce that distinction: the type of an instance of an
358:py:class:`X509` object is now simply :py:class:`X509`. Originally,
359the type would have been :py:class:`X509Type`. These days,
360:py:class:`X509Type` and :py:class:`X509` are literally the same
361object. PyOpenSSL maintains these old names for backwards
362compatibility.
363
364Here's a table of these backwards-compatible names:
365
366========================= =============================
367Type name Backwards-compatible name
368========================= =============================
369:py:class:`X509` :py:class:`X509Type`
370:py:class:`X509Name` :py:class:`X509NameType`
371:py:class:`X509Req` :py:class:`X509ReqType`
372:py:class:`X509Store` :py:class:`X509StoreType`
373:py:class:`X509Extension` :py:class:`X509ExtensionType`
374:py:class:`PKey` :py:class:`PKeyType`
375:py:class:`PKCS7` :py:class:`PKCS7Type`
376:py:class:`PKCS12` :py:class:`PKCS12Type`
377:py:class:`NetscapeSPKI` :py:class:`NetscapeSPKIType`
378:py:class:`CRL` :py:class:`CRLType`
379========================= =============================
380
Laurens Van Houtven2ec1fba2015-04-20 11:48:12 -0700381Some objects, such as py:class`Revoked`, don't have ``Type``
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200382equivalents, because they were added after the restriction had been
383lifted.