blob: fc5619ca7081d54dba7fb86988f40a098f0eb926 [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
Jonathan Balletc9e066c2011-07-17 22:56:05 +090010.. py:class:: X509Name(x509name)
11
12 A class representing X.509 Distinguished Names.
13
14 This constructor creates a copy of *x509name* which should be an
15 instance of :py:class:`X509Name`.
16
17
Jonathan Balletc9e066c2011-07-17 22:56:05 +090018.. py:class:: X509Req()
19
20 A class representing X.509 certificate requests.
21
22
Laurens Van Houtven0c26a462015-04-20 11:00:08 -070023.. py:data:: X509Store
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070024
25 A class representing the X.509 store.
26
27
28.. py:data:: X509StoreContext
29
30 A class representing the X.509 store context.
Jonathan Balletc9e066c2011-07-17 22:56:05 +090031
32
Jonathan Balletc9e066c2011-07-17 22:56:05 +090033.. py:class:: PKey()
34
35 A class representing DSA or RSA keys.
36
Jonathan Balletc9e066c2011-07-17 22:56:05 +090037.. py:data:: PKCS7Type
38
39 A Python type object representing the PKCS7 object type.
40
41
42.. py:data:: PKCS12Type
43
44 A Python type object representing the PKCS12 object type.
45
46
Jonathan Balletc9e066c2011-07-17 22:56:05 +090047.. py:class:: X509Extension(typename, critical, value[, subject][, issuer])
48
49 A class representing an X.509 v3 certificate extensions. See
50 http://openssl.org/docs/apps/x509v3_config.html#STANDARD_EXTENSIONS for
51 *typename* strings and their options. Optional parameters *subject* and
52 *issuer* must be X509 objects.
53
54
55.. py:data:: NetscapeSPKIType
56
57 See :py:class:`NetscapeSPKI`.
58
59
60.. py:class:: NetscapeSPKI([enc])
61
62 A class representing Netscape SPKI objects.
63
64 If the *enc* argument is present, it should be a base64-encoded string
65 representing a NetscapeSPKI object, as returned by the :py:meth:`b64_encode`
66 method.
67
68
69.. py:class:: CRL()
70
71 A class representing Certifcate Revocation List objects.
72
73
74.. py:class:: Revoked()
75
76 A class representing Revocation objects of CRL.
77
78
Jonathan Balletc9e066c2011-07-17 22:56:05 +090079.. py:data:: TYPE_RSA
Jonathan Ballet6381da32011-07-20 16:43:38 +090080 TYPE_DSA
Jonathan Balletc9e066c2011-07-17 22:56:05 +090081
82 Key type constants.
83
Jonathan Balletc9e066c2011-07-17 22:56:05 +090084.. py:exception:: Error
85
86 Generic exception used in the :py:mod:`.crypto` module.
87
Laurens Van Houtven07051d32014-06-19 12:00:30 +020088Elliptic curves
89---------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +090090
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040091.. py:function:: get_elliptic_curves
92
93 Return a set of objects representing the elliptic curves supported in the
94 OpenSSL build in use.
95
96 The curve objects have a :py:class:`unicode` ``name`` attribute by which
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -040097 they identify themselves.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040098
99 The curve objects are useful as values for the argument accepted by
Jean-Paul Calderone3b04e352014-04-19 09:29:10 -0400100 :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be
101 used for ECDHE key exchange.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400102
103
104.. py:function:: get_elliptic_curve
105
106 Return a single curve object selected by name.
107
108 See :py:func:`get_elliptic_curves` for information about curve objects.
109
110 If the named curve is not supported then :py:class:`ValueError` is raised.
111
112
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200113Serialization and deserialization
114---------------------------------
115
116The following serialization functions take one of these constants to
117determine the format:
118
119.. py:data:: FILETYPE_PEM
120 FILETYPE_ASN1
121
122Certificates
123~~~~~~~~~~~~
124
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900125.. py:function:: dump_certificate(type, cert)
126
127 Dump the certificate *cert* into a buffer string encoded with the type
128 *type*.
129
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200130.. py:function:: load_certificate(type, buffer)
131
132 Load a certificate (X509) from the string *buffer* encoded with the
133 type *type*.
134
135Certificate signing requests
136~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900137
138.. py:function:: dump_certificate_request(type, req)
139
140 Dump the certificate request *req* into a buffer string encoded with the
141 type *type*.
142
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200143.. py:function:: load_certificate_request(type, buffer)
144
145 Load a certificate request (X509Req) from the string *buffer* encoded with
146 the type *type*.
147
148Private keys
149~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900150
151.. py:function:: dump_privatekey(type, pkey[, cipher, passphrase])
152
153 Dump the private key *pkey* into a buffer string encoded with the type
154 *type*, optionally (if *type* is :py:const:`FILETYPE_PEM`) encrypting it
155 using *cipher* and *passphrase*.
156
157 *passphrase* must be either a string or a callback for providing the
158 pass phrase.
159
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900160.. py:function:: load_privatekey(type, buffer[, passphrase])
161
162 Load a private key (PKey) from the string *buffer* encoded with the type
163 *type* (must be one of :py:const:`FILETYPE_PEM` and
164 :py:const:`FILETYPE_ASN1`).
165
166 *passphrase* must be either a string or a callback for providing the pass
167 phrase.
168
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200169Certificate revocation lists
170~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900171
172.. py:function:: load_crl(type, buffer)
173
174 Load Certificate Revocation List (CRL) data from a string *buffer*.
175 *buffer* encoded with the type *type*. The type *type* must either
176 :py:const:`FILETYPE_PEM` or :py:const:`FILETYPE_ASN1`).
177
178
179.. py:function:: load_pkcs7_data(type, buffer)
180
Laurens Van Houtven0f820872015-04-20 11:25:57 -0700181 Load pkcs7 data from the string *buffer* encoded with the type
182 *type*. The type *type* must either :py:const:`FILETYPE_PEM` or
183 :py:const:`FILETYPE_ASN1`).
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900184
185
186.. py:function:: load_pkcs12(buffer[, passphrase])
187
188 Load pkcs12 data from the string *buffer*. If the pkcs12 structure is
189 encrypted, a *passphrase* must be included. The MAC is always
190 checked and thus required.
191
192 See also the man page for the C function :py:func:`PKCS12_parse`.
193
Laurens Van Houtven07051d32014-06-19 12:00:30 +0200194Signing and verifying signatures
195--------------------------------
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900196
197.. py:function:: sign(key, data, digest)
198
199 Sign a data string using the given key and message digest.
200
201 *key* is a :py:class:`PKey` instance. *data* is a ``str`` instance.
202 *digest* is a ``str`` naming a supported message digest type, for example
203 :py:const:`sha1`.
204
205 .. versionadded:: 0.11
206
207
208.. py:function:: verify(certificate, signature, data, digest)
209
210 Verify the signature for a data string.
211
212 *certificate* is a :py:class:`X509` instance corresponding to the private
213 key which generated the signature. *signature* is a *str* instance giving
214 the signature itself. *data* is a *str* instance giving the data to which
215 the signature applies. *digest* is a *str* instance naming the message
216 digest type of the signature, for example :py:const:`sha1`.
217
218 .. versionadded:: 0.11
219
220
221.. _openssl-x509:
222
223X509 objects
224------------
225
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200226.. autoclass:: X509
227 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900228
229.. _openssl-x509name:
230
231X509Name objects
232----------------
233
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200234.. autoclass:: X509Name
235 :members:
236 :special-members:
237 :exclude-members: __repr__, __getattr__, __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900238
239.. _openssl-x509req:
240
241X509Req objects
242---------------
243
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200244.. autoclass:: X509Req
245 :members:
246 :special-members:
247 :exclude-members: __weakref__
Jean-Paul Calderone26e07d62014-03-02 08:08:23 -0500248
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900249.. _openssl-x509store:
250
251X509Store objects
252-----------------
253
Laurens Van Houtven8aeafdd2014-06-17 15:33:42 +0200254.. autoclass:: X509Store
255 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900256
Stephen Holsapple95a46652015-02-09 19:34:25 -0800257X509StoreContextError objects
258-----------------------------
259
260The X509StoreContextError is an exception raised from
261`X509StoreContext.verify_certificate` in circumstances where a certificate
262cannot be verified in a provided context.
263
Jean-Paul Calderone876b2ac2015-03-15 16:17:19 -0400264The certificate for which the verification error was detected is given by the
265``certificate`` attribute of the exception instance as a :class:`X509`
266instance.
267
268Details about the verification error are given in the exception's ``args`` attribute.
Stephen Holsapple95a46652015-02-09 19:34:25 -0800269
270
Stephen Holsapple08ffaa62015-01-30 17:18:40 -0800271X509StoreContext objects
272------------------------
273
274The X509StoreContext object is used for verifying a certificate against a set
275of trusted certificates.
276
277
278.. py:method:: X509StoreContext.verify_certificate()
279
280 Verify a certificate in the context of this initialized `X509StoreContext`.
281 On error, raises `X509StoreContextError`, otherwise does nothing.
282
283 .. versionadded:: 0.15
284
285
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900286.. _openssl-pkey:
287
288PKey objects
289------------
290
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200291.. autoclass:: PKey
292 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900293
294.. _openssl-pkcs7:
295
296PKCS7 objects
297-------------
298
299PKCS7 objects have the following methods:
300
Jonathan Ballet6381da32011-07-20 16:43:38 +0900301.. py:method:: PKCS7.type_is_signed()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900302
303 FIXME
304
Jonathan Ballet6381da32011-07-20 16:43:38 +0900305.. py:method:: PKCS7.type_is_enveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900306
307 FIXME
308
Jonathan Ballet6381da32011-07-20 16:43:38 +0900309.. py:method:: PKCS7.type_is_signedAndEnveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900310
311 FIXME
312
Jonathan Ballet6381da32011-07-20 16:43:38 +0900313.. py:method:: PKCS7.type_is_data()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900314
315 FIXME
316
Jonathan Ballet6381da32011-07-20 16:43:38 +0900317.. py:method:: PKCS7.get_type_name()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900318
319 Get the type name of the PKCS7.
320
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900321.. _openssl-pkcs12:
322
323PKCS12 objects
324--------------
325
Laurens Van Houtvenbb503a32014-06-19 12:28:08 +0200326.. autoclass:: PKCS12
327 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900328
329.. _openssl-509ext:
330
331X509Extension objects
332---------------------
333
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200334.. autoclass:: X509Extension
335 :members:
336 :special-members:
337 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900338
339.. _openssl-netscape-spki:
340
341NetscapeSPKI objects
342--------------------
343
Laurens Van Houtven59152b52014-06-19 16:42:30 +0200344.. autoclass:: NetscapeSPKI
345 :members:
346 :special-members:
347 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900348
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900349.. _crl:
350
351CRL objects
352-----------
353
Laurens Van Houtvencb32e852014-06-19 17:36:28 +0200354.. autoclass:: CRL
355 :members:
356 :special-members:
357 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900358
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900359.. _revoked:
360
361Revoked objects
362---------------
363
Laurens Van Houtvend92f55c2014-06-19 17:08:41 +0200364.. autoclass:: Revoked
365 :members:
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200366
Laurens Van Houtvenc3baa7b2014-06-18 22:06:56 +0200367Digest names
368------------
369
370Several of the functions and methods in this module take a digest
371name. These must be strings describing a digest algorithm supported by
372OpenSSL (by ``EVP_get_digestbyname``, specifically). For example,
373:py:const:`b"md5"` or :py:const:`b"sha1"`.
374
375More information and a list of these digest names can be found in the
376``EVP_DigestInit(3)`` man page of your OpenSSL installation. This page
377can be found online for the latest version of OpenSSL:
378https://www.openssl.org/docs/crypto/EVP_DigestInit.html
379
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200380Backwards compatible type names
381-------------------------------
382
383When PyOpenSSL was originally written, the most current version of
384Python was 2.1. It made a distinction between classes and types. None
385of the versions of Python currently supported by PyOpenSSL still
386enforce that distinction: the type of an instance of an
387:py:class:`X509` object is now simply :py:class:`X509`. Originally,
388the type would have been :py:class:`X509Type`. These days,
389:py:class:`X509Type` and :py:class:`X509` are literally the same
390object. PyOpenSSL maintains these old names for backwards
391compatibility.
392
393Here's a table of these backwards-compatible names:
394
395========================= =============================
396Type name Backwards-compatible name
397========================= =============================
398:py:class:`X509` :py:class:`X509Type`
399:py:class:`X509Name` :py:class:`X509NameType`
400:py:class:`X509Req` :py:class:`X509ReqType`
401:py:class:`X509Store` :py:class:`X509StoreType`
402:py:class:`X509Extension` :py:class:`X509ExtensionType`
403:py:class:`PKey` :py:class:`PKeyType`
404:py:class:`PKCS7` :py:class:`PKCS7Type`
405:py:class:`PKCS12` :py:class:`PKCS12Type`
406:py:class:`NetscapeSPKI` :py:class:`NetscapeSPKIType`
407:py:class:`CRL` :py:class:`CRLType`
408========================= =============================
409
410Soem objects, such as py:class`Revoked`, don't have ``Type``
411equivalents, because they were added after the restriction had been
412lifted.