blob: 293a8a3252685871f66df7790680adbd48670a6f [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
27
28.. py:data:: FILETYPE_PEM
Jonathan Ballet6381da32011-07-20 16:43:38 +090029 FILETYPE_ASN1
Jonathan Balletc9e066c2011-07-17 22:56:05 +090030
31 File type constants.
32
33
34.. py:data:: TYPE_RSA
Jonathan Ballet6381da32011-07-20 16:43:38 +090035 TYPE_DSA
Jonathan Balletc9e066c2011-07-17 22:56:05 +090036
37 Key type constants.
38
39
40.. py:exception:: Error
41
42 Generic exception used in the :py:mod:`.crypto` module.
43
44
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040045.. py:function:: get_elliptic_curves
46
47 Return a set of objects representing the elliptic curves supported in the
48 OpenSSL build in use.
49
50 The curve objects have a :py:class:`unicode` ``name`` attribute by which
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -040051 they identify themselves.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040052
53 The curve objects are useful as values for the argument accepted by
Jean-Paul Calderone3b04e352014-04-19 09:29:10 -040054 :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be
55 used for ECDHE key exchange.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040056
57
58.. py:function:: get_elliptic_curve
59
60 Return a single curve object selected by name.
61
62 See :py:func:`get_elliptic_curves` for information about curve objects.
63
64 If the named curve is not supported then :py:class:`ValueError` is raised.
65
66
Jonathan Balletc9e066c2011-07-17 22:56:05 +090067.. py:function:: dump_certificate(type, cert)
68
69 Dump the certificate *cert* into a buffer string encoded with the type
70 *type*.
71
72
73.. py:function:: dump_certificate_request(type, req)
74
75 Dump the certificate request *req* into a buffer string encoded with the
76 type *type*.
77
78
79.. py:function:: dump_privatekey(type, pkey[, cipher, passphrase])
80
81 Dump the private key *pkey* into a buffer string encoded with the type
82 *type*, optionally (if *type* is :py:const:`FILETYPE_PEM`) encrypting it
83 using *cipher* and *passphrase*.
84
85 *passphrase* must be either a string or a callback for providing the
86 pass phrase.
87
88
89.. py:function:: load_certificate(type, buffer)
90
91 Load a certificate (X509) from the string *buffer* encoded with the
92 type *type*.
93
94
95.. py:function:: load_certificate_request(type, buffer)
96
97 Load a certificate request (X509Req) from the string *buffer* encoded with
98 the type *type*.
99
100
101.. py:function:: load_privatekey(type, buffer[, passphrase])
102
103 Load a private key (PKey) from the string *buffer* encoded with the type
104 *type* (must be one of :py:const:`FILETYPE_PEM` and
105 :py:const:`FILETYPE_ASN1`).
106
107 *passphrase* must be either a string or a callback for providing the pass
108 phrase.
109
110
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
131
132.. py:function:: sign(key, data, digest)
133
134 Sign a data string using the given key and message digest.
135
136 *key* is a :py:class:`PKey` instance. *data* is a ``str`` instance.
137 *digest* is a ``str`` naming a supported message digest type, for example
138 :py:const:`sha1`.
139
140 .. versionadded:: 0.11
141
142
143.. py:function:: verify(certificate, signature, data, digest)
144
145 Verify the signature for a data string.
146
147 *certificate* is a :py:class:`X509` instance corresponding to the private
148 key which generated the signature. *signature* is a *str* instance giving
149 the signature itself. *data* is a *str* instance giving the data to which
150 the signature applies. *digest* is a *str* instance naming the message
151 digest type of the signature, for example :py:const:`sha1`.
152
153 .. versionadded:: 0.11
154
155
156.. _openssl-x509:
157
158X509 objects
159------------
160
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200161.. autoclass:: X509
162 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900163
164.. _openssl-x509name:
165
166X509Name objects
167----------------
168
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200169.. autoclass:: X509Name
170 :members:
171 :special-members:
172 :exclude-members: __repr__, __getattr__, __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900173
174.. _openssl-x509req:
175
176X509Req objects
177---------------
178
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200179.. autoclass:: X509Req
180 :members:
181 :special-members:
182 :exclude-members: __weakref__
Jean-Paul Calderone26e07d62014-03-02 08:08:23 -0500183
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900184.. _openssl-x509store:
185
186X509Store objects
187-----------------
188
Laurens Van Houtven8aeafdd2014-06-17 15:33:42 +0200189.. autoclass:: X509Store
190 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900191
192.. _openssl-pkey:
193
194PKey objects
195------------
196
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200197.. autoclass:: PKey
198 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900199
200.. _openssl-pkcs7:
201
202PKCS7 objects
203-------------
204
205PKCS7 objects have the following methods:
206
Jonathan Ballet6381da32011-07-20 16:43:38 +0900207.. py:method:: PKCS7.type_is_signed()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900208
209 FIXME
210
211
Jonathan Ballet6381da32011-07-20 16:43:38 +0900212.. py:method:: PKCS7.type_is_enveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900213
214 FIXME
215
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
221
Jonathan Ballet6381da32011-07-20 16:43:38 +0900222.. py:method:: PKCS7.type_is_data()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900223
224 FIXME
225
226
Jonathan Ballet6381da32011-07-20 16:43:38 +0900227.. py:method:: PKCS7.get_type_name()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900228
229 Get the type name of the PKCS7.
230
231
232.. _openssl-pkcs12:
233
234PKCS12 objects
235--------------
236
237PKCS12 objects have the following methods:
238
Jonathan Ballet6381da32011-07-20 16:43:38 +0900239.. py:method:: PKCS12.export([passphrase=None][, iter=2048][, maciter=1])
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900240
241 Returns a PKCS12 object as a string.
242
243 The optional *passphrase* must be a string not a callback.
244
245 See also the man page for the C function :py:func:`PKCS12_create`.
246
247
248.. py:method:: PKCS12.get_ca_certificates()
249
250 Return CA certificates within the PKCS12 object as a tuple. Returns
251 :py:const:`None` if no CA certificates are present.
252
253
254.. py:method:: PKCS12.get_certificate()
255
256 Return certificate portion of the PKCS12 structure.
257
258
259.. py:method:: PKCS12.get_friendlyname()
260
261 Return friendlyName portion of the PKCS12 structure.
262
263
264.. py:method:: PKCS12.get_privatekey()
265
266 Return private key portion of the PKCS12 structure
267
268
269.. py:method:: PKCS12.set_ca_certificates(cacerts)
270
271 Replace or set the CA certificates within the PKCS12 object with the sequence *cacerts*.
272
273 Set *cacerts* to :py:const:`None` to remove all CA certificates.
274
275
276.. py:method:: PKCS12.set_certificate(cert)
277
278 Replace or set the certificate portion of the PKCS12 structure.
279
280
281.. py:method:: PKCS12.set_friendlyname(name)
282
283 Replace or set the friendlyName portion of the PKCS12 structure.
284
285
286.. py:method:: PKCS12.set_privatekey(pkey)
287
288 Replace or set private key portion of the PKCS12 structure
289
290
291.. _openssl-509ext:
292
293X509Extension objects
294---------------------
295
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200296.. autoclass:: X509Extension
297 :members:
298 :special-members:
299 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900300
301.. _openssl-netscape-spki:
302
303NetscapeSPKI objects
304--------------------
305
306NetscapeSPKI objects have the following methods:
307
308.. py:method:: NetscapeSPKI.b64_encode()
309
310 Return a base64-encoded string representation of the object.
311
312
313.. py:method:: NetscapeSPKI.get_pubkey()
314
315 Return the public key of object.
316
317
318.. py:method:: NetscapeSPKI.set_pubkey(key)
319
320 Set the public key of the object to *key*.
321
322
323.. py:method:: NetscapeSPKI.sign(key, digest_name)
324
325 Sign the NetscapeSPKI object using the given *key* and *digest_name*.
326 *digest_name* must be a string describing a digest algorithm supported by
327 OpenSSL (by EVP_get_digestbyname, specifically). For example,
328 :py:const:`"md5"` or :py:const:`"sha1"`.
329
330
331.. py:method:: NetscapeSPKI.verify(key)
332
333 Verify the NetscapeSPKI object using the given *key*.
334
335
336.. _crl:
337
338CRL objects
339-----------
340
341CRL objects have the following methods:
342
343.. py:method:: CRL.add_revoked(revoked)
344
345 Add a Revoked object to the CRL, by value not reference.
346
347
348.. py:method:: CRL.export(cert, key[, type=FILETYPE_PEM][, days=100])
349
350 Use *cert* and *key* to sign the CRL and return the CRL as a string.
351 *days* is the number of days before the next CRL is due.
352
353
354.. py:method:: CRL.get_revoked()
355
356 Return a tuple of Revoked objects, by value not reference.
357
358
359.. _revoked:
360
361Revoked objects
362---------------
363
364Revoked objects have the following methods:
365
366.. py:method:: Revoked.all_reasons()
367
368 Return a list of all supported reasons.
369
370
371.. py:method:: Revoked.get_reason()
372
373 Return the revocation reason as a str. Can be
374 None, which differs from "Unspecified".
375
376
377.. py:method:: Revoked.get_rev_date()
378
379 Return the revocation date as a str.
380 The string is formatted as an ASN1 GENERALIZEDTIME.
381
382
383.. py:method:: Revoked.get_serial()
384
385 Return a str containing a hex number of the serial of the revoked certificate.
386
387
388.. py:method:: Revoked.set_reason(reason)
389
390 Set the revocation reason. *reason* must be None or a string, but the
391 values are limited. Spaces and case are ignored. See
392 :py:meth:`all_reasons`.
393
394
395.. py:method:: Revoked.set_rev_date(date)
396
397 Set the revocation date.
398 The string is formatted as an ASN1 GENERALIZEDTIME.
399
400
401.. py:method:: Revoked.set_serial(serial)
402
403 *serial* is a string containing a hex number of the serial of the revoked certificate.
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200404
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200405Digest names
406------------
407
408Several of the functions and methods in this module take a digest
409name. These must be strings describing a digest algorithm supported by
410OpenSSL (by ``EVP_get_digestbyname``, specifically). For example,
411:py:const:`b"md5"` or :py:const:`b"sha1"`.
412
413More information and a list of these digest names can be found in the
414``EVP_DigestInit(3)`` man page of your OpenSSL installation. This page
415can be found online for the latest version of OpenSSL:
416https://www.openssl.org/docs/crypto/EVP_DigestInit.html
417
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200418Backwards compatible type names
419-------------------------------
420
421When PyOpenSSL was originally written, the most current version of
422Python was 2.1. It made a distinction between classes and types. None
423of the versions of Python currently supported by PyOpenSSL still
424enforce that distinction: the type of an instance of an
425:py:class:`X509` object is now simply :py:class:`X509`. Originally,
426the type would have been :py:class:`X509Type`. These days,
427:py:class:`X509Type` and :py:class:`X509` are literally the same
428object. PyOpenSSL maintains these old names for backwards
429compatibility.
430
431Here's a table of these backwards-compatible names:
432
433========================= =============================
434Type name Backwards-compatible name
435========================= =============================
436:py:class:`X509` :py:class:`X509Type`
437:py:class:`X509Name` :py:class:`X509NameType`
438:py:class:`X509Req` :py:class:`X509ReqType`
439:py:class:`X509Store` :py:class:`X509StoreType`
440:py:class:`X509Extension` :py:class:`X509ExtensionType`
441:py:class:`PKey` :py:class:`PKeyType`
442:py:class:`PKCS7` :py:class:`PKCS7Type`
443:py:class:`PKCS12` :py:class:`PKCS12Type`
444:py:class:`NetscapeSPKI` :py:class:`NetscapeSPKIType`
445:py:class:`CRL` :py:class:`CRLType`
446========================= =============================
447
448Soem objects, such as py:class`Revoked`, don't have ``Type``
449equivalents, because they were added after the restriction had been
450lifted.