blob: 949f9b22ba2d023885a11bcaee03f119f55e6c41 [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:: NetscapeSPKI([enc])
10
11 A class representing Netscape SPKI objects.
12
13 If the *enc* argument is present, it should be a base64-encoded string
14 representing a NetscapeSPKI object, as returned by the :py:meth:`b64_encode`
15 method.
16
17
18.. py:class:: CRL()
19
20 A class representing Certifcate Revocation List objects.
21
22
23.. py:class:: Revoked()
24
25 A class representing Revocation objects of CRL.
26
Jonathan Balletc9e066c2011-07-17 22:56:05 +090027.. py:data:: TYPE_RSA
Jonathan Ballet6381da32011-07-20 16:43:38 +090028 TYPE_DSA
Jonathan Balletc9e066c2011-07-17 22:56:05 +090029
30 Key type constants.
31
Jonathan Balletc9e066c2011-07-17 22:56:05 +090032.. py:exception:: Error
33
34 Generic exception used in the :py:mod:`.crypto` module.
35
Laurens Van Houtven07051d32014-06-19 12:00:30 +020036Elliptic curves
37---------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +090038
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040039.. py:function:: get_elliptic_curves
40
41 Return a set of objects representing the elliptic curves supported in the
42 OpenSSL build in use.
43
44 The curve objects have a :py:class:`unicode` ``name`` attribute by which
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -040045 they identify themselves.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040046
47 The curve objects are useful as values for the argument accepted by
Jean-Paul Calderone3b04e352014-04-19 09:29:10 -040048 :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be
49 used for ECDHE key exchange.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040050
51
52.. py:function:: get_elliptic_curve
53
54 Return a single curve object selected by name.
55
56 See :py:func:`get_elliptic_curves` for information about curve objects.
57
58 If the named curve is not supported then :py:class:`ValueError` is raised.
59
60
Laurens Van Houtven07051d32014-06-19 12:00:30 +020061Serialization and deserialization
62---------------------------------
63
64The following serialization functions take one of these constants to
65determine the format:
66
67.. py:data:: FILETYPE_PEM
68 FILETYPE_ASN1
69
70Certificates
71~~~~~~~~~~~~
72
Jonathan Balletc9e066c2011-07-17 22:56:05 +090073.. py:function:: dump_certificate(type, cert)
74
75 Dump the certificate *cert* into a buffer string encoded with the type
76 *type*.
77
Laurens Van Houtven07051d32014-06-19 12:00:30 +020078.. py:function:: load_certificate(type, buffer)
79
80 Load a certificate (X509) from the string *buffer* encoded with the
81 type *type*.
82
83Certificate signing requests
84~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090085
86.. py:function:: dump_certificate_request(type, req)
87
88 Dump the certificate request *req* into a buffer string encoded with the
89 type *type*.
90
Laurens Van Houtven07051d32014-06-19 12:00:30 +020091.. py:function:: load_certificate_request(type, buffer)
92
93 Load a certificate request (X509Req) from the string *buffer* encoded with
94 the type *type*.
95
96Private keys
97~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +090098
99.. py:function:: dump_privatekey(type, pkey[, cipher, passphrase])
100
101 Dump the private key *pkey* into a buffer string encoded with the type
102 *type*, optionally (if *type* is :py:const:`FILETYPE_PEM`) encrypting it
103 using *cipher* and *passphrase*.
104
105 *passphrase* must be either a string or a callback for providing the
106 pass phrase.
107
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900108.. py:function:: load_privatekey(type, buffer[, passphrase])
109
110 Load a private key (PKey) from the string *buffer* encoded with the type
111 *type* (must be one of :py:const:`FILETYPE_PEM` and
112 :py:const:`FILETYPE_ASN1`).
113
114 *passphrase* must be either a string or a callback for providing the pass
115 phrase.
116
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200117Certificate revocation lists
118~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900119
120.. py:function:: load_crl(type, buffer)
121
122 Load Certificate Revocation List (CRL) data from a string *buffer*.
123 *buffer* encoded with the type *type*. The type *type* must either
124 :py:const:`FILETYPE_PEM` or :py:const:`FILETYPE_ASN1`).
125
126
127.. py:function:: load_pkcs7_data(type, buffer)
128
129 Load pkcs7 data from the string *buffer* encoded with the type *type*.
130
131
132.. py:function:: load_pkcs12(buffer[, passphrase])
133
134 Load pkcs12 data from the string *buffer*. If the pkcs12 structure is
135 encrypted, a *passphrase* must be included. The MAC is always
136 checked and thus required.
137
138 See also the man page for the C function :py:func:`PKCS12_parse`.
139
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200140Signing and verifying signatures
141--------------------------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900142
143.. py:function:: sign(key, data, digest)
144
145 Sign a data string using the given key and message digest.
146
147 *key* is a :py:class:`PKey` instance. *data* is a ``str`` instance.
148 *digest* is a ``str`` naming a supported message digest type, for example
149 :py:const:`sha1`.
150
151 .. versionadded:: 0.11
152
153
154.. py:function:: verify(certificate, signature, data, digest)
155
156 Verify the signature for a data string.
157
158 *certificate* is a :py:class:`X509` instance corresponding to the private
159 key which generated the signature. *signature* is a *str* instance giving
160 the signature itself. *data* is a *str* instance giving the data to which
161 the signature applies. *digest* is a *str* instance naming the message
162 digest type of the signature, for example :py:const:`sha1`.
163
164 .. versionadded:: 0.11
165
166
167.. _openssl-x509:
168
169X509 objects
170------------
171
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200172.. autoclass:: X509
173 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900174
175.. _openssl-x509name:
176
177X509Name objects
178----------------
179
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200180.. autoclass:: X509Name
181 :members:
182 :special-members:
183 :exclude-members: __repr__, __getattr__, __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900184
185.. _openssl-x509req:
186
187X509Req objects
188---------------
189
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200190.. autoclass:: X509Req
191 :members:
192 :special-members:
193 :exclude-members: __weakref__
Jean-Paul Calderone26e07d62014-03-02 08:08:23 -0500194
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900195.. _openssl-x509store:
196
197X509Store objects
198-----------------
199
Laurens Van Houtven8aeafdd2014-06-17 15:33:42 +0200200.. autoclass:: X509Store
201 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900202
203.. _openssl-pkey:
204
205PKey objects
206------------
207
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200208.. autoclass:: PKey
209 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900210
211.. _openssl-pkcs7:
212
213PKCS7 objects
214-------------
215
216PKCS7 objects have the following methods:
217
Jonathan Ballet6381da32011-07-20 16:43:38 +0900218.. py:method:: PKCS7.type_is_signed()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900219
220 FIXME
221
Jonathan Ballet6381da32011-07-20 16:43:38 +0900222.. py:method:: PKCS7.type_is_enveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900223
224 FIXME
225
Jonathan Ballet6381da32011-07-20 16:43:38 +0900226.. py:method:: PKCS7.type_is_signedAndEnveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900227
228 FIXME
229
Jonathan Ballet6381da32011-07-20 16:43:38 +0900230.. py:method:: PKCS7.type_is_data()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900231
232 FIXME
233
Jonathan Ballet6381da32011-07-20 16:43:38 +0900234.. py:method:: PKCS7.get_type_name()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900235
236 Get the type name of the PKCS7.
237
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900238.. _openssl-pkcs12:
239
240PKCS12 objects
241--------------
242
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +0200243.. autoclass:: PKCS12
244 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900245
246.. _openssl-509ext:
247
248X509Extension objects
249---------------------
250
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200251.. autoclass:: X509Extension
252 :members:
253 :special-members:
254 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900255
256.. _openssl-netscape-spki:
257
258NetscapeSPKI objects
259--------------------
260
261NetscapeSPKI objects have the following methods:
262
263.. py:method:: NetscapeSPKI.b64_encode()
264
265 Return a base64-encoded string representation of the object.
266
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900267.. py:method:: NetscapeSPKI.get_pubkey()
268
269 Return the public key of object.
270
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900271.. py:method:: NetscapeSPKI.set_pubkey(key)
272
273 Set the public key of the object to *key*.
274
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900275.. py:method:: NetscapeSPKI.sign(key, digest_name)
276
277 Sign the NetscapeSPKI object using the given *key* and *digest_name*.
278 *digest_name* must be a string describing a digest algorithm supported by
279 OpenSSL (by EVP_get_digestbyname, specifically). For example,
280 :py:const:`"md5"` or :py:const:`"sha1"`.
281
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900282.. py:method:: NetscapeSPKI.verify(key)
283
284 Verify the NetscapeSPKI object using the given *key*.
285
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900286.. _crl:
287
288CRL objects
289-----------
290
291CRL objects have the following methods:
292
293.. py:method:: CRL.add_revoked(revoked)
294
295 Add a Revoked object to the CRL, by value not reference.
296
297
298.. py:method:: CRL.export(cert, key[, type=FILETYPE_PEM][, days=100])
299
300 Use *cert* and *key* to sign the CRL and return the CRL as a string.
301 *days* is the number of days before the next CRL is due.
302
303
304.. py:method:: CRL.get_revoked()
305
306 Return a tuple of Revoked objects, by value not reference.
307
308
309.. _revoked:
310
311Revoked objects
312---------------
313
314Revoked objects have the following methods:
315
316.. py:method:: Revoked.all_reasons()
317
318 Return a list of all supported reasons.
319
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900320.. py:method:: Revoked.get_reason()
321
322 Return the revocation reason as a str. Can be
323 None, which differs from "Unspecified".
324
325
326.. py:method:: Revoked.get_rev_date()
327
328 Return the revocation date as a str.
329 The string is formatted as an ASN1 GENERALIZEDTIME.
330
331
332.. py:method:: Revoked.get_serial()
333
334 Return a str containing a hex number of the serial of the revoked certificate.
335
336
337.. py:method:: Revoked.set_reason(reason)
338
339 Set the revocation reason. *reason* must be None or a string, but the
340 values are limited. Spaces and case are ignored. See
341 :py:meth:`all_reasons`.
342
343
344.. py:method:: Revoked.set_rev_date(date)
345
346 Set the revocation date.
347 The string is formatted as an ASN1 GENERALIZEDTIME.
348
349
350.. py:method:: Revoked.set_serial(serial)
351
352 *serial* is a string containing a hex number of the serial of the revoked certificate.
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200353
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200354Digest names
355------------
356
357Several of the functions and methods in this module take a digest
358name. These must be strings describing a digest algorithm supported by
359OpenSSL (by ``EVP_get_digestbyname``, specifically). For example,
360:py:const:`b"md5"` or :py:const:`b"sha1"`.
361
362More information and a list of these digest names can be found in the
363``EVP_DigestInit(3)`` man page of your OpenSSL installation. This page
364can be found online for the latest version of OpenSSL:
365https://www.openssl.org/docs/crypto/EVP_DigestInit.html
366
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200367Backwards compatible type names
368-------------------------------
369
370When PyOpenSSL was originally written, the most current version of
371Python was 2.1. It made a distinction between classes and types. None
372of the versions of Python currently supported by PyOpenSSL still
373enforce that distinction: the type of an instance of an
374:py:class:`X509` object is now simply :py:class:`X509`. Originally,
375the type would have been :py:class:`X509Type`. These days,
376:py:class:`X509Type` and :py:class:`X509` are literally the same
377object. PyOpenSSL maintains these old names for backwards
378compatibility.
379
380Here's a table of these backwards-compatible names:
381
382========================= =============================
383Type name Backwards-compatible name
384========================= =============================
385:py:class:`X509` :py:class:`X509Type`
386:py:class:`X509Name` :py:class:`X509NameType`
387:py:class:`X509Req` :py:class:`X509ReqType`
388:py:class:`X509Store` :py:class:`X509StoreType`
389:py:class:`X509Extension` :py:class:`X509ExtensionType`
390:py:class:`PKey` :py:class:`PKeyType`
391:py:class:`PKCS7` :py:class:`PKCS7Type`
392:py:class:`PKCS12` :py:class:`PKCS12Type`
393:py:class:`NetscapeSPKI` :py:class:`NetscapeSPKIType`
394:py:class:`CRL` :py:class:`CRLType`
395========================= =============================
396
397Soem objects, such as py:class`Revoked`, don't have ``Type``
398equivalents, because they were added after the restriction had been
399lifted.