blob: 9beadcea0e8b1c334e025efad6a45b7a84d363ab [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:: X509()
11
12 A class representing X.509 certificates.
13
Jonathan Balletc9e066c2011-07-17 22:56:05 +090014.. py:class:: X509Req()
15
16 A class representing X.509 certificate requests.
17
Jonathan Balletc9e066c2011-07-17 22:56:05 +090018.. py:class:: X509Extension(typename, critical, value[, subject][, issuer])
19
20 A class representing an X.509 v3 certificate extensions. See
21 http://openssl.org/docs/apps/x509v3_config.html#STANDARD_EXTENSIONS for
22 *typename* strings and their options. Optional parameters *subject* and
23 *issuer* must be X509 objects.
24
25
Jonathan Balletc9e066c2011-07-17 22:56:05 +090026.. py:class:: NetscapeSPKI([enc])
27
28 A class representing Netscape SPKI objects.
29
30 If the *enc* argument is present, it should be a base64-encoded string
31 representing a NetscapeSPKI object, as returned by the :py:meth:`b64_encode`
32 method.
33
34
35.. py:class:: CRL()
36
37 A class representing Certifcate Revocation List objects.
38
39
40.. py:class:: Revoked()
41
42 A class representing Revocation objects of CRL.
43
44
45.. py:data:: FILETYPE_PEM
Jonathan Ballet6381da32011-07-20 16:43:38 +090046 FILETYPE_ASN1
Jonathan Balletc9e066c2011-07-17 22:56:05 +090047
48 File type constants.
49
50
51.. py:data:: TYPE_RSA
Jonathan Ballet6381da32011-07-20 16:43:38 +090052 TYPE_DSA
Jonathan Balletc9e066c2011-07-17 22:56:05 +090053
54 Key type constants.
55
56
57.. py:exception:: Error
58
59 Generic exception used in the :py:mod:`.crypto` module.
60
61
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040062.. py:function:: get_elliptic_curves
63
64 Return a set of objects representing the elliptic curves supported in the
65 OpenSSL build in use.
66
67 The curve objects have a :py:class:`unicode` ``name`` attribute by which
Jean-Paul Calderoneaaf516d2014-04-19 09:10:45 -040068 they identify themselves.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040069
70 The curve objects are useful as values for the argument accepted by
Jean-Paul Calderone3b04e352014-04-19 09:29:10 -040071 :py:meth:`Context.set_tmp_ecdh` to specify which elliptical curve should be
72 used for ECDHE key exchange.
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -040073
74
75.. py:function:: get_elliptic_curve
76
77 Return a single curve object selected by name.
78
79 See :py:func:`get_elliptic_curves` for information about curve objects.
80
81 If the named curve is not supported then :py:class:`ValueError` is raised.
82
83
Jonathan Balletc9e066c2011-07-17 22:56:05 +090084.. py:function:: dump_certificate(type, cert)
85
86 Dump the certificate *cert* into a buffer string encoded with the type
87 *type*.
88
89
90.. py:function:: dump_certificate_request(type, req)
91
92 Dump the certificate request *req* into a buffer string encoded with the
93 type *type*.
94
95
96.. py:function:: dump_privatekey(type, pkey[, cipher, passphrase])
97
98 Dump the private key *pkey* into a buffer string encoded with the type
99 *type*, optionally (if *type* is :py:const:`FILETYPE_PEM`) encrypting it
100 using *cipher* and *passphrase*.
101
102 *passphrase* must be either a string or a callback for providing the
103 pass phrase.
104
105
106.. py:function:: load_certificate(type, buffer)
107
108 Load a certificate (X509) from the string *buffer* encoded with the
109 type *type*.
110
111
112.. py:function:: load_certificate_request(type, buffer)
113
114 Load a certificate request (X509Req) from the string *buffer* encoded with
115 the type *type*.
116
117
118.. py:function:: load_privatekey(type, buffer[, passphrase])
119
120 Load a private key (PKey) from the string *buffer* encoded with the type
121 *type* (must be one of :py:const:`FILETYPE_PEM` and
122 :py:const:`FILETYPE_ASN1`).
123
124 *passphrase* must be either a string or a callback for providing the pass
125 phrase.
126
127
128.. py:function:: load_crl(type, buffer)
129
130 Load Certificate Revocation List (CRL) data from a string *buffer*.
131 *buffer* encoded with the type *type*. The type *type* must either
132 :py:const:`FILETYPE_PEM` or :py:const:`FILETYPE_ASN1`).
133
134
135.. py:function:: load_pkcs7_data(type, buffer)
136
137 Load pkcs7 data from the string *buffer* encoded with the type *type*.
138
139
140.. py:function:: load_pkcs12(buffer[, passphrase])
141
142 Load pkcs12 data from the string *buffer*. If the pkcs12 structure is
143 encrypted, a *passphrase* must be included. The MAC is always
144 checked and thus required.
145
146 See also the man page for the C function :py:func:`PKCS12_parse`.
147
148
149.. py:function:: sign(key, data, digest)
150
151 Sign a data string using the given key and message digest.
152
153 *key* is a :py:class:`PKey` instance. *data* is a ``str`` instance.
154 *digest* is a ``str`` naming a supported message digest type, for example
155 :py:const:`sha1`.
156
157 .. versionadded:: 0.11
158
159
160.. py:function:: verify(certificate, signature, data, digest)
161
162 Verify the signature for a data string.
163
164 *certificate* is a :py:class:`X509` instance corresponding to the private
165 key which generated the signature. *signature* is a *str* instance giving
166 the signature itself. *data* is a *str* instance giving the data to which
167 the signature applies. *digest* is a *str* instance naming the message
168 digest type of the signature, for example :py:const:`sha1`.
169
170 .. versionadded:: 0.11
171
172
173.. _openssl-x509:
174
175X509 objects
176------------
177
178X509 objects have the following methods:
179
180.. py:method:: X509.get_issuer()
181
182 Return an X509Name object representing the issuer of the certificate.
183
184
185.. py:method:: X509.get_pubkey()
186
187 Return a :py:class:`PKey` object representing the public key of the certificate.
188
189
190.. py:method:: X509.get_serial_number()
191
192 Return the certificate serial number.
193
194
195.. py:method:: X509.get_signature_algorithm()
196
197 Return the signature algorithm used in the certificate. If the algorithm is
198 undefined, raise :py:data:`ValueError`.
199
Jean-Paul Calderone54cc3902012-09-12 10:49:07 -0400200 ..versionadded:: 0.13
201
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900202
203.. py:method:: X509.get_subject()
204
205 Return an :py:class:`X509Name` object representing the subject of the certificate.
206
207
208.. py:method:: X509.get_version()
209
210 Return the certificate version.
211
212
213.. py:method:: X509.get_notBefore()
214
215 Return a string giving the time before which the certificate is not valid. The
216 string is formatted as an ASN1 GENERALIZEDTIME::
217
Jonathan Ballet6381da32011-07-20 16:43:38 +0900218 YYYYMMDDhhmmssZ
219 YYYYMMDDhhmmss+hhmm
220 YYYYMMDDhhmmss-hhmm
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900221
222 If no value exists for this field, :py:data:`None` is returned.
223
224
225.. py:method:: X509.get_notAfter()
226
227 Return a string giving the time after which the certificate is not valid. The
228 string is formatted as an ASN1 GENERALIZEDTIME::
229
Jonathan Ballet6381da32011-07-20 16:43:38 +0900230 YYYYMMDDhhmmssZ
231 YYYYMMDDhhmmss+hhmm
232 YYYYMMDDhhmmss-hhmm
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900233
234 If no value exists for this field, :py:data:`None` is returned.
235
236
237.. py:method:: X509.set_notBefore(when)
238
239 Change the time before which the certificate is not valid. *when* is a
240 string formatted as an ASN1 GENERALIZEDTIME::
241
Jonathan Ballet6381da32011-07-20 16:43:38 +0900242 YYYYMMDDhhmmssZ
243 YYYYMMDDhhmmss+hhmm
244 YYYYMMDDhhmmss-hhmm
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900245
246
247.. py:method:: X509.set_notAfter(when)
248
249 Change the time after which the certificate is not valid. *when* is a
250 string formatted as an ASN1 GENERALIZEDTIME::
251
Jonathan Ballet6381da32011-07-20 16:43:38 +0900252 YYYYMMDDhhmmssZ
253 YYYYMMDDhhmmss+hhmm
254 YYYYMMDDhhmmss-hhmm
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900255
256
257
258.. py:method:: X509.gmtime_adj_notBefore(time)
259
260 Adjust the timestamp (in GMT) when the certificate starts being valid.
261
262
263.. py:method:: X509.gmtime_adj_notAfter(time)
264
265 Adjust the timestamp (in GMT) when the certificate stops being valid.
266
267
268.. py:method:: X509.has_expired()
269
270 Checks the certificate's time stamp against current time. Returns true if the
271 certificate has expired and false otherwise.
272
273
274.. py:method:: X509.set_issuer(issuer)
275
276 Set the issuer of the certificate to *issuer*.
277
278
279.. py:method:: X509.set_pubkey(pkey)
280
281 Set the public key of the certificate to *pkey*.
282
283
284.. py:method:: X509.set_serial_number(serialno)
285
286 Set the serial number of the certificate to *serialno*.
287
288
289.. py:method:: X509.set_subject(subject)
290
291 Set the subject of the certificate to *subject*.
292
293
294.. py:method:: X509.set_version(version)
295
296 Set the certificate version to *version*.
297
298
299.. py:method:: X509.sign(pkey, digest)
300
301 Sign the certificate, using the key *pkey* and the message digest algorithm
302 identified by the string *digest*.
303
304
305.. py:method:: X509.subject_name_hash()
306
307 Return the hash of the certificate subject.
308
309.. py:method:: X509.digest(digest_name)
310
311 Return a digest of the certificate, using the *digest_name* method.
312 *digest_name* must be a string describing a digest algorithm supported
313 by OpenSSL (by EVP_get_digestbyname, specifically). For example,
314 :py:const:`"md5"` or :py:const:`"sha1"`.
315
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900316.. py:method:: X509.add_extensions(extensions)
317
318 Add the extensions in the sequence *extensions* to the certificate.
319
320
321.. py:method:: X509.get_extension_count()
322
323 Return the number of extensions on this certificate.
324
325 .. versionadded:: 0.12
326
327
328.. py:method:: X509.get_extension(index)
329
330 Retrieve the extension on this certificate at the given index.
331
332 Extensions on a certificate are kept in order. The index parameter selects
333 which extension will be returned. The returned object will be an
334 :py:class:`X509Extension` instance.
335
336 .. versionadded:: 0.12
337
338
339.. _openssl-x509name:
340
341X509Name objects
342----------------
343
Laurens Van Houtven196195b2014-06-17 17:06:34 +0200344.. autoclass:: X509Name
345 :members:
346 :special-members:
347 :exclude-members: __repr__, __getattr__, __weakref__
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900348
349.. _openssl-x509req:
350
351X509Req objects
352---------------
353
354X509Req objects have the following methods:
355
356.. py:method:: X509Req.get_pubkey()
357
358 Return a :py:class:`PKey` object representing the public key of the certificate request.
359
360
361.. py:method:: X509Req.get_subject()
362
363 Return an :py:class:`X509Name` object representing the subject of the certificate.
364
365
366.. py:method:: X509Req.set_pubkey(pkey)
367
368 Set the public key of the certificate request to *pkey*.
369
370
371.. py:method:: X509Req.sign(pkey, digest)
372
373 Sign the certificate request, using the key *pkey* and the message digest
374 algorithm identified by the string *digest*.
375
376
377.. py:method:: X509Req.verify(pkey)
378
379 Verify a certificate request using the public key *pkey*.
380
381
382.. py:method:: X509Req.set_version(version)
383
384 Set the version (RFC 2459, 4.1.2.1) of the certificate request to
385 *version*.
386
387
388.. py:method:: X509Req.get_version()
389
390 Get the version (RFC 2459, 4.1.2.1) of the certificate request.
391
392
Jean-Paul Calderone26e07d62014-03-02 08:08:23 -0500393.. py:method:: X509Req.get_extensions()
394
395 Get extensions to the request.
396
397 .. versionadded:: 0.15
398
399
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900400.. _openssl-x509store:
401
402X509Store objects
403-----------------
404
Laurens Van Houtven8aeafdd2014-06-17 15:33:42 +0200405.. autoclass:: X509Store
406 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900407
408.. _openssl-pkey:
409
410PKey objects
411------------
412
Laurens Van Houtven6e7dd432014-06-17 16:10:57 +0200413.. autoclass:: PKey
414 :members:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900415
416.. _openssl-pkcs7:
417
418PKCS7 objects
419-------------
420
421PKCS7 objects have the following methods:
422
Jonathan Ballet6381da32011-07-20 16:43:38 +0900423.. py:method:: PKCS7.type_is_signed()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900424
425 FIXME
426
427
Jonathan Ballet6381da32011-07-20 16:43:38 +0900428.. py:method:: PKCS7.type_is_enveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900429
430 FIXME
431
432
Jonathan Ballet6381da32011-07-20 16:43:38 +0900433.. py:method:: PKCS7.type_is_signedAndEnveloped()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900434
435 FIXME
436
437
Jonathan Ballet6381da32011-07-20 16:43:38 +0900438.. py:method:: PKCS7.type_is_data()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900439
440 FIXME
441
442
Jonathan Ballet6381da32011-07-20 16:43:38 +0900443.. py:method:: PKCS7.get_type_name()
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900444
445 Get the type name of the PKCS7.
446
447
448.. _openssl-pkcs12:
449
450PKCS12 objects
451--------------
452
453PKCS12 objects have the following methods:
454
Jonathan Ballet6381da32011-07-20 16:43:38 +0900455.. py:method:: PKCS12.export([passphrase=None][, iter=2048][, maciter=1])
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900456
457 Returns a PKCS12 object as a string.
458
459 The optional *passphrase* must be a string not a callback.
460
461 See also the man page for the C function :py:func:`PKCS12_create`.
462
463
464.. py:method:: PKCS12.get_ca_certificates()
465
466 Return CA certificates within the PKCS12 object as a tuple. Returns
467 :py:const:`None` if no CA certificates are present.
468
469
470.. py:method:: PKCS12.get_certificate()
471
472 Return certificate portion of the PKCS12 structure.
473
474
475.. py:method:: PKCS12.get_friendlyname()
476
477 Return friendlyName portion of the PKCS12 structure.
478
479
480.. py:method:: PKCS12.get_privatekey()
481
482 Return private key portion of the PKCS12 structure
483
484
485.. py:method:: PKCS12.set_ca_certificates(cacerts)
486
487 Replace or set the CA certificates within the PKCS12 object with the sequence *cacerts*.
488
489 Set *cacerts* to :py:const:`None` to remove all CA certificates.
490
491
492.. py:method:: PKCS12.set_certificate(cert)
493
494 Replace or set the certificate portion of the PKCS12 structure.
495
496
497.. py:method:: PKCS12.set_friendlyname(name)
498
499 Replace or set the friendlyName portion of the PKCS12 structure.
500
501
502.. py:method:: PKCS12.set_privatekey(pkey)
503
504 Replace or set private key portion of the PKCS12 structure
505
506
507.. _openssl-509ext:
508
509X509Extension objects
510---------------------
511
512X509Extension objects have several methods:
513
514.. py:method:: X509Extension.get_critical()
515
516 Return the critical field of the extension object.
517
518
519.. py:method:: X509Extension.get_short_name()
520
521 Retrieve the short descriptive name for this extension.
522
523 The result is a byte string like :py:const:`basicConstraints`.
524
525 .. versionadded:: 0.12
526
527
528.. py:method:: X509Extension.get_data()
529
530 Retrieve the data for this extension.
531
532 The result is the ASN.1 encoded form of the extension data as a byte string.
533
534 .. versionadded:: 0.12
535
536
537.. _openssl-netscape-spki:
538
539NetscapeSPKI objects
540--------------------
541
542NetscapeSPKI objects have the following methods:
543
544.. py:method:: NetscapeSPKI.b64_encode()
545
546 Return a base64-encoded string representation of the object.
547
548
549.. py:method:: NetscapeSPKI.get_pubkey()
550
551 Return the public key of object.
552
553
554.. py:method:: NetscapeSPKI.set_pubkey(key)
555
556 Set the public key of the object to *key*.
557
558
559.. py:method:: NetscapeSPKI.sign(key, digest_name)
560
561 Sign the NetscapeSPKI object using the given *key* and *digest_name*.
562 *digest_name* must be a string describing a digest algorithm supported by
563 OpenSSL (by EVP_get_digestbyname, specifically). For example,
564 :py:const:`"md5"` or :py:const:`"sha1"`.
565
566
567.. py:method:: NetscapeSPKI.verify(key)
568
569 Verify the NetscapeSPKI object using the given *key*.
570
571
572.. _crl:
573
574CRL objects
575-----------
576
577CRL objects have the following methods:
578
579.. py:method:: CRL.add_revoked(revoked)
580
581 Add a Revoked object to the CRL, by value not reference.
582
583
584.. py:method:: CRL.export(cert, key[, type=FILETYPE_PEM][, days=100])
585
586 Use *cert* and *key* to sign the CRL and return the CRL as a string.
587 *days* is the number of days before the next CRL is due.
588
589
590.. py:method:: CRL.get_revoked()
591
592 Return a tuple of Revoked objects, by value not reference.
593
594
595.. _revoked:
596
597Revoked objects
598---------------
599
600Revoked objects have the following methods:
601
602.. py:method:: Revoked.all_reasons()
603
604 Return a list of all supported reasons.
605
606
607.. py:method:: Revoked.get_reason()
608
609 Return the revocation reason as a str. Can be
610 None, which differs from "Unspecified".
611
612
613.. py:method:: Revoked.get_rev_date()
614
615 Return the revocation date as a str.
616 The string is formatted as an ASN1 GENERALIZEDTIME.
617
618
619.. py:method:: Revoked.get_serial()
620
621 Return a str containing a hex number of the serial of the revoked certificate.
622
623
624.. py:method:: Revoked.set_reason(reason)
625
626 Set the revocation reason. *reason* must be None or a string, but the
627 values are limited. Spaces and case are ignored. See
628 :py:meth:`all_reasons`.
629
630
631.. py:method:: Revoked.set_rev_date(date)
632
633 Set the revocation date.
634 The string is formatted as an ASN1 GENERALIZEDTIME.
635
636
637.. py:method:: Revoked.set_serial(serial)
638
639 *serial* is a string containing a hex number of the serial of the revoked certificate.
Laurens Van Houtven13d56ba2014-06-17 16:00:26 +0200640
641Backwards compatible type names
642-------------------------------
643
644When PyOpenSSL was originally written, the most current version of
645Python was 2.1. It made a distinction between classes and types. None
646of the versions of Python currently supported by PyOpenSSL still
647enforce that distinction: the type of an instance of an
648:py:class:`X509` object is now simply :py:class:`X509`. Originally,
649the type would have been :py:class:`X509Type`. These days,
650:py:class:`X509Type` and :py:class:`X509` are literally the same
651object. PyOpenSSL maintains these old names for backwards
652compatibility.
653
654Here's a table of these backwards-compatible names:
655
656========================= =============================
657Type name Backwards-compatible name
658========================= =============================
659:py:class:`X509` :py:class:`X509Type`
660:py:class:`X509Name` :py:class:`X509NameType`
661:py:class:`X509Req` :py:class:`X509ReqType`
662:py:class:`X509Store` :py:class:`X509StoreType`
663:py:class:`X509Extension` :py:class:`X509ExtensionType`
664:py:class:`PKey` :py:class:`PKeyType`
665:py:class:`PKCS7` :py:class:`PKCS7Type`
666:py:class:`PKCS12` :py:class:`PKCS12Type`
667:py:class:`NetscapeSPKI` :py:class:`NetscapeSPKIType`
668:py:class:`CRL` :py:class:`CRLType`
669========================= =============================
670
671Soem objects, such as py:class`Revoked`, don't have ``Type``
672equivalents, because they were added after the restriction had been
673lifted.