blob: 194857af47e18abe3d87668f8bdc7f477999721c [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:: X509()
10
11 A class representing X.509 certificates.
12
Jonathan Balletc9e066c2011-07-17 22:56:05 +090013.. py:class:: NetscapeSPKI([enc])
14
15 A class representing Netscape SPKI objects.
16
17 If the *enc* argument is present, it should be a base64-encoded string
18 representing a NetscapeSPKI object, as returned by the :py:meth:`b64_encode`
19 method.
20
21
22.. py:class:: CRL()
23
24 A class representing Certifcate Revocation List objects.
25
26
27.. py:class:: Revoked()
28
29 A class representing Revocation objects of CRL.
30
31
32.. py:data:: FILETYPE_PEM
Jonathan Ballet6381da32011-07-20 16:43:38 +090033 FILETYPE_ASN1
Jonathan Balletc9e066c2011-07-17 22:56:05 +090034
35 File type constants.
36
37
38.. py:data:: TYPE_RSA
Jonathan Ballet6381da32011-07-20 16:43:38 +090039 TYPE_DSA
Jonathan Balletc9e066c2011-07-17 22:56:05 +090040
41 Key type constants.
42
43
44.. py:exception:: Error
45
46 Generic exception used in the :py:mod:`.crypto` module.
47
48
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040049.. py:function:: get_elliptic_curves
50
51 Return a set of objects representing the elliptic curves supported in the
52 OpenSSL build in use.
53
54 The curve objects have a :py:class:`unicode` ``name`` attribute by which
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -040055 they identify themselves.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040056
57 The curve objects are useful as values for the argument accepted by
Jean-Paul Calderone3b04e352014-04-19 09:29:10 -040058 :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be
59 used for ECDHE key exchange.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040060
61
62.. py:function:: get_elliptic_curve
63
64 Return a single curve object selected by name.
65
66 See :py:func:`get_elliptic_curves` for information about curve objects.
67
68 If the named curve is not supported then :py:class:`ValueError` is raised.
69
70
Jonathan Balletc9e066c2011-07-17 22:56:05 +090071.. py:function:: dump_certificate(type, cert)
72
73 Dump the certificate *cert* into a buffer string encoded with the type
74 *type*.
75
76
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
82
83.. py:function:: dump_privatekey(type, pkey[, cipher, passphrase])
84
85 Dump the private key *pkey* into a buffer string encoded with the type
86 *type*, optionally (if *type* is :py:const:`FILETYPE_PEM`) encrypting it
87 using *cipher* and *passphrase*.
88
89 *passphrase* must be either a string or a callback for providing the
90 pass phrase.
91
92
93.. py:function:: load_certificate(type, buffer)
94
95 Load a certificate (X509) from the string *buffer* encoded with the
96 type *type*.
97
98
99.. py:function:: load_certificate_request(type, buffer)
100
101 Load a certificate request (X509Req) from the string *buffer* encoded with
102 the type *type*.
103
104
105.. py:function:: load_privatekey(type, buffer[, passphrase])
106
107 Load a private key (PKey) from the string *buffer* encoded with the type
108 *type* (must be one of :py:const:`FILETYPE_PEM` and
109 :py:const:`FILETYPE_ASN1`).
110
111 *passphrase* must be either a string or a callback for providing the pass
112 phrase.
113
114
115.. py:function:: load_crl(type, buffer)
116
117 Load Certificate Revocation List (CRL) data from a string *buffer*.
118 *buffer* encoded with the type *type*. The type *type* must either
119 :py:const:`FILETYPE_PEM` or :py:const:`FILETYPE_ASN1`).
120
121
122.. py:function:: load_pkcs7_data(type, buffer)
123
124 Load pkcs7 data from the string *buffer* encoded with the type *type*.
125
126
127.. py:function:: load_pkcs12(buffer[, passphrase])
128
129 Load pkcs12 data from the string *buffer*. If the pkcs12 structure is
130 encrypted, a *passphrase* must be included. The MAC is always
131 checked and thus required.
132
133 See also the man page for the C function :py:func:`PKCS12_parse`.
134
135
136.. py:function:: sign(key, data, digest)
137
138 Sign a data string using the given key and message digest.
139
140 *key* is a :py:class:`PKey` instance. *data* is a ``str`` instance.
141 *digest* is a ``str`` naming a supported message digest type, for example
142 :py:const:`sha1`.
143
144 .. versionadded:: 0.11
145
146
147.. py:function:: verify(certificate, signature, data, digest)
148
149 Verify the signature for a data string.
150
151 *certificate* is a :py:class:`X509` instance corresponding to the private
152 key which generated the signature. *signature* is a *str* instance giving
153 the signature itself. *data* is a *str* instance giving the data to which
154 the signature applies. *digest* is a *str* instance naming the message
155 digest type of the signature, for example :py:const:`sha1`.
156
157 .. versionadded:: 0.11
158
159
160.. _openssl-x509:
161
162X509 objects
163------------
164
165X509 objects have the following methods:
166
167.. py:method:: X509.get_issuer()
168
169 Return an X509Name object representing the issuer of the certificate.
170
171
172.. py:method:: X509.get_pubkey()
173
174 Return a :py:class:`PKey` object representing the public key of the certificate.
175
176
177.. py:method:: X509.get_serial_number()
178
179 Return the certificate serial number.
180
181
182.. py:method:: X509.get_signature_algorithm()
183
184 Return the signature algorithm used in the certificate. If the algorithm is
185 undefined, raise :py:data:`ValueError`.
186
Jean-Paul Calderone54cc3902012-09-12 10:49:07 -0400187 ..versionadded:: 0.13
188
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900189
190.. py:method:: X509.get_subject()
191
192 Return an :py:class:`X509Name` object representing the subject of the certificate.
193
194
195.. py:method:: X509.get_version()
196
197 Return the certificate version.
198
199
200.. py:method:: X509.get_notBefore()
201
202 Return a string giving the time before which the certificate is not valid. The
203 string is formatted as an ASN1 GENERALIZEDTIME::
204
Jonathan Ballet6381da32011-07-20 16:43:38 +0900205 YYYYMMDDhhmmssZ
206 YYYYMMDDhhmmss+hhmm
207 YYYYMMDDhhmmss-hhmm
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900208
209 If no value exists for this field, :py:data:`None` is returned.
210
211
212.. py:method:: X509.get_notAfter()
213
214 Return a string giving the time after which the certificate is not valid. The
215 string is formatted as an ASN1 GENERALIZEDTIME::
216
Jonathan Ballet6381da32011-07-20 16:43:38 +0900217 YYYYMMDDhhmmssZ
218 YYYYMMDDhhmmss+hhmm
219 YYYYMMDDhhmmss-hhmm
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900220
221 If no value exists for this field, :py:data:`None` is returned.
222
223
224.. py:method:: X509.set_notBefore(when)
225
226 Change the time before which the certificate is not valid. *when* is a
227 string formatted as an ASN1 GENERALIZEDTIME::
228
Jonathan Ballet6381da32011-07-20 16:43:38 +0900229 YYYYMMDDhhmmssZ
230 YYYYMMDDhhmmss+hhmm
231 YYYYMMDDhhmmss-hhmm
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900232
233
234.. py:method:: X509.set_notAfter(when)
235
236 Change the time after which the certificate is not valid. *when* is a
237 string formatted as an ASN1 GENERALIZEDTIME::
238
Jonathan Ballet6381da32011-07-20 16:43:38 +0900239 YYYYMMDDhhmmssZ
240 YYYYMMDDhhmmss+hhmm
241 YYYYMMDDhhmmss-hhmm
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900242
243
244
245.. py:method:: X509.gmtime_adj_notBefore(time)
246
247 Adjust the timestamp (in GMT) when the certificate starts being valid.
248
249
250.. py:method:: X509.gmtime_adj_notAfter(time)
251
252 Adjust the timestamp (in GMT) when the certificate stops being valid.
253
254
255.. py:method:: X509.has_expired()
256
257 Checks the certificate's time stamp against current time. Returns true if the
258 certificate has expired and false otherwise.
259
260
261.. py:method:: X509.set_issuer(issuer)
262
263 Set the issuer of the certificate to *issuer*.
264
265
266.. py:method:: X509.set_pubkey(pkey)
267
268 Set the public key of the certificate to *pkey*.
269
270
271.. py:method:: X509.set_serial_number(serialno)
272
273 Set the serial number of the certificate to *serialno*.
274
275
276.. py:method:: X509.set_subject(subject)
277
278 Set the subject of the certificate to *subject*.
279
280
281.. py:method:: X509.set_version(version)
282
283 Set the certificate version to *version*.
284
285
286.. py:method:: X509.sign(pkey, digest)
287
288 Sign the certificate, using the key *pkey* and the message digest algorithm
289 identified by the string *digest*.
290
291
292.. py:method:: X509.subject_name_hash()
293
294 Return the hash of the certificate subject.
295
296.. py:method:: X509.digest(digest_name)
297
298 Return a digest of the certificate, using the *digest_name* method.
299 *digest_name* must be a string describing a digest algorithm supported
300 by OpenSSL (by EVP_get_digestbyname, specifically). For example,
301 :py:const:`"md5"` or :py:const:`"sha1"`.
302
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900303.. py:method:: X509.add_extensions(extensions)
304
305 Add the extensions in the sequence *extensions* to the certificate.
306
307
308.. py:method:: X509.get_extension_count()
309
310 Return the number of extensions on this certificate.
311
312 .. versionadded:: 0.12
313
314
315.. py:method:: X509.get_extension(index)
316
317 Retrieve the extension on this certificate at the given index.
318
319 Extensions on a certificate are kept in order. The index parameter selects
320 which extension will be returned. The returned object will be an
321 :py:class:`X509Extension` instance.
322
323 .. versionadded:: 0.12
324
325
326.. _openssl-x509name:
327
328X509Name objects
329----------------
330
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200331.. autoclass:: X509Name
332 :members:
333 :special-members:
334 :exclude-members: __repr__, __getattr__, __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900335
336.. _openssl-x509req:
337
338X509Req objects
339---------------
340
Laurens Van Houtven3e83d242014-06-18 14:29:47 +0200341.. autoclass:: X509Req
342 :members:
343 :special-members:
344 :exclude-members: __weakref__
Jean-Paul Calderone26e07d62014-03-02 08:08:23 -0500345
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900346.. _openssl-x509store:
347
348X509Store objects
349-----------------
350
Laurens Van Houtven8aeafdd2014-06-17 15:33:42 +0200351.. autoclass:: X509Store
352 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900353
354.. _openssl-pkey:
355
356PKey objects
357------------
358
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200359.. autoclass:: PKey
360 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900361
362.. _openssl-pkcs7:
363
364PKCS7 objects
365-------------
366
367PKCS7 objects have the following methods:
368
Jonathan Ballet6381da32011-07-20 16:43:38 +0900369.. py:method:: PKCS7.type_is_signed()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900370
371 FIXME
372
373
Jonathan Ballet6381da32011-07-20 16:43:38 +0900374.. py:method:: PKCS7.type_is_enveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900375
376 FIXME
377
378
Jonathan Ballet6381da32011-07-20 16:43:38 +0900379.. py:method:: PKCS7.type_is_signedAndEnveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900380
381 FIXME
382
383
Jonathan Ballet6381da32011-07-20 16:43:38 +0900384.. py:method:: PKCS7.type_is_data()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900385
386 FIXME
387
388
Jonathan Ballet6381da32011-07-20 16:43:38 +0900389.. py:method:: PKCS7.get_type_name()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900390
391 Get the type name of the PKCS7.
392
393
394.. _openssl-pkcs12:
395
396PKCS12 objects
397--------------
398
399PKCS12 objects have the following methods:
400
Jonathan Ballet6381da32011-07-20 16:43:38 +0900401.. py:method:: PKCS12.export([passphrase=None][, iter=2048][, maciter=1])
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900402
403 Returns a PKCS12 object as a string.
404
405 The optional *passphrase* must be a string not a callback.
406
407 See also the man page for the C function :py:func:`PKCS12_create`.
408
409
410.. py:method:: PKCS12.get_ca_certificates()
411
412 Return CA certificates within the PKCS12 object as a tuple. Returns
413 :py:const:`None` if no CA certificates are present.
414
415
416.. py:method:: PKCS12.get_certificate()
417
418 Return certificate portion of the PKCS12 structure.
419
420
421.. py:method:: PKCS12.get_friendlyname()
422
423 Return friendlyName portion of the PKCS12 structure.
424
425
426.. py:method:: PKCS12.get_privatekey()
427
428 Return private key portion of the PKCS12 structure
429
430
431.. py:method:: PKCS12.set_ca_certificates(cacerts)
432
433 Replace or set the CA certificates within the PKCS12 object with the sequence *cacerts*.
434
435 Set *cacerts* to :py:const:`None` to remove all CA certificates.
436
437
438.. py:method:: PKCS12.set_certificate(cert)
439
440 Replace or set the certificate portion of the PKCS12 structure.
441
442
443.. py:method:: PKCS12.set_friendlyname(name)
444
445 Replace or set the friendlyName portion of the PKCS12 structure.
446
447
448.. py:method:: PKCS12.set_privatekey(pkey)
449
450 Replace or set private key portion of the PKCS12 structure
451
452
453.. _openssl-509ext:
454
455X509Extension objects
456---------------------
457
Laurens Van Houtven2650de52014-06-18 13:47:47 +0200458.. autoclass:: X509Extension
459 :members:
460 :special-members:
461 :exclude-members: __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900462
463.. _openssl-netscape-spki:
464
465NetscapeSPKI objects
466--------------------
467
468NetscapeSPKI objects have the following methods:
469
470.. py:method:: NetscapeSPKI.b64_encode()
471
472 Return a base64-encoded string representation of the object.
473
474
475.. py:method:: NetscapeSPKI.get_pubkey()
476
477 Return the public key of object.
478
479
480.. py:method:: NetscapeSPKI.set_pubkey(key)
481
482 Set the public key of the object to *key*.
483
484
485.. py:method:: NetscapeSPKI.sign(key, digest_name)
486
487 Sign the NetscapeSPKI object using the given *key* and *digest_name*.
488 *digest_name* must be a string describing a digest algorithm supported by
489 OpenSSL (by EVP_get_digestbyname, specifically). For example,
490 :py:const:`"md5"` or :py:const:`"sha1"`.
491
492
493.. py:method:: NetscapeSPKI.verify(key)
494
495 Verify the NetscapeSPKI object using the given *key*.
496
497
498.. _crl:
499
500CRL objects
501-----------
502
503CRL objects have the following methods:
504
505.. py:method:: CRL.add_revoked(revoked)
506
507 Add a Revoked object to the CRL, by value not reference.
508
509
510.. py:method:: CRL.export(cert, key[, type=FILETYPE_PEM][, days=100])
511
512 Use *cert* and *key* to sign the CRL and return the CRL as a string.
513 *days* is the number of days before the next CRL is due.
514
515
516.. py:method:: CRL.get_revoked()
517
518 Return a tuple of Revoked objects, by value not reference.
519
520
521.. _revoked:
522
523Revoked objects
524---------------
525
526Revoked objects have the following methods:
527
528.. py:method:: Revoked.all_reasons()
529
530 Return a list of all supported reasons.
531
532
533.. py:method:: Revoked.get_reason()
534
535 Return the revocation reason as a str. Can be
536 None, which differs from "Unspecified".
537
538
539.. py:method:: Revoked.get_rev_date()
540
541 Return the revocation date as a str.
542 The string is formatted as an ASN1 GENERALIZEDTIME.
543
544
545.. py:method:: Revoked.get_serial()
546
547 Return a str containing a hex number of the serial of the revoked certificate.
548
549
550.. py:method:: Revoked.set_reason(reason)
551
552 Set the revocation reason. *reason* must be None or a string, but the
553 values are limited. Spaces and case are ignored. See
554 :py:meth:`all_reasons`.
555
556
557.. py:method:: Revoked.set_rev_date(date)
558
559 Set the revocation date.
560 The string is formatted as an ASN1 GENERALIZEDTIME.
561
562
563.. py:method:: Revoked.set_serial(serial)
564
565 *serial* is a string containing a hex number of the serial of the revoked certificate.
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200566
567Backwards compatible type names
568-------------------------------
569
570When PyOpenSSL was originally written, the most current version of
571Python was 2.1. It made a distinction between classes and types. None
572of the versions of Python currently supported by PyOpenSSL still
573enforce that distinction: the type of an instance of an
574:py:class:`X509` object is now simply :py:class:`X509`. Originally,
575the type would have been :py:class:`X509Type`. These days,
576:py:class:`X509Type` and :py:class:`X509` are literally the same
577object. PyOpenSSL maintains these old names for backwards
578compatibility.
579
580Here's a table of these backwards-compatible names:
581
582========================= =============================
583Type name Backwards-compatible name
584========================= =============================
585:py:class:`X509` :py:class:`X509Type`
586:py:class:`X509Name` :py:class:`X509NameType`
587:py:class:`X509Req` :py:class:`X509ReqType`
588:py:class:`X509Store` :py:class:`X509StoreType`
589:py:class:`X509Extension` :py:class:`X509ExtensionType`
590:py:class:`PKey` :py:class:`PKeyType`
591:py:class:`PKCS7` :py:class:`PKCS7Type`
592:py:class:`PKCS12` :py:class:`PKCS12Type`
593:py:class:`NetscapeSPKI` :py:class:`NetscapeSPKIType`
594:py:class:`CRL` :py:class:`CRLType`
595========================= =============================
596
597Soem objects, such as py:class`Revoked`, don't have ``Type``
598equivalents, because they were added after the restriction had been
599lifted.