blob: 6e5aceeac31f2293651b0fa11d913d19ab192e45 [file] [log] [blame]
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001 Python OpenSSL Manual
2 __________________________________________________________________
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05003
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05004 Python OpenSSL Manual
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05005
Jean-Paul Calderone0ebe45a2009-04-25 10:40:31 -04006 Jean-Paul Calderone
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05007
Jean-Paul Calderone0ebe45a2009-04-25 10:40:31 -04008 exarkun@twistedmatrix.com
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05009
10 Abstract:
11
12 This module is a rather thin wrapper around (a subset of) the OpenSSL
13 library. With thin wrapper I mean that a lot of the object methods do
14 nothing more than calling a corresponding function in the OpenSSL
15 library.
16
17Contents
18
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -050019 * 1 Introduction
20 * 2 Building and Installing
21 + 2.1 Building the Module on a Unix System
22 + 2.2 Building the Module on a Windows System
23 * 3 OpenSSL -- Python interface to OpenSSL
24 + 3.1 crypto -- Generic cryptographic module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050025 + 3.2 rand -- An interface to the OpenSSL pseudo random number
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -050026 generator
27 + 3.3 SSL -- An interface to the SSL-specific parts of OpenSSL
28 * 4 Internals
29 + 4.1 Exceptions
30 + 4.2 Callbacks
31 + 4.3 Acessing Socket Methods
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050032
33
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -050034 1 Introduction
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050035
Jean-Paul Calderonef1b839d2008-09-01 12:06:06 -040036 The reason pyOpenSSL was created is that the SSL support in the socket
37 module in Python 2.1 (the contemporary version of Python when the
38 pyOpenSSL project was begun) was severely limited. Other OpenSSL
39 wrappers for Python at the time were also limited, though in different
40 ways. Unfortunately, Python's standard library SSL support has remained
41 weak, although other packages (such as M2Crypto^1) have made great
42 advances and now equal or exceed pyOpenSSL's functionality.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050043
Jean-Paul Calderonef1b839d2008-09-01 12:06:06 -040044 The reason pyOpenSSL continues to be maintained is that there is a
45 significant user community around it, as well as a large amount of
46 software which depends on it. It is a great benefit to many people for
47 pyOpenSSL to continue to exist and advance.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050048
49
50 2 Building and Installing
51
52 These instructions can also be found in the file INSTALL.
53
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -050054 I have tested this on Debian Linux systems (woody and sid), Solaris 2.6
55 and 2.7. Others have successfully compiled it on Windows and NT.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050056
57
582.1 Building the Module on a Unix System
59
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -050060 pyOpenSSL uses distutils, so there really shouldn't be any problems. To
61 build the library:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050062
63python setup.py build
64
65 If your OpenSSL header files aren't in /usr/include, you may need to
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -050066 supply the -I flag to let the setup script know where to look. The same
67 goes for the libraries of course, use the -L flag. Note that build
68 won't accept these flags, so you have to run first build_ext and then
69 build! Example:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050070
71python setup.py build_ext -I/usr/local/ssl/include -L/usr/local/ssl/lib
72python setup.py build
73
74 Now you should have a directory called OpenSSL that contains e.g.
75 SSL.so and __init__.py somewhere in the build dicrectory, so just:
76
77python setup.py install
78
79 If you, for some arcane reason, don't want the module to appear in the
80 site-packages directory, use the --prefix option.
81
82 You can, of course, do
83
84python setup.py --help
85
86 to find out more about how to use the script.
87
88
892.2 Building the Module on a Windows System
90
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -050091 Big thanks to Itamar Shtull-Trauring and Oleg Orlov for their help with
92 Windows build instructions. Same as for Unix systems, we have to
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050093 separate the build_ext and the build.
94
95 Building the library:
96
97setup.py build_ext -I ...\openssl\inc32 -L ...\openssl\out32dll
98setup.py build
99
100 Where ...\openssl is of course the location of your OpenSSL
101 installation.
102
103 Installation is the same as for Unix systems:
104
105setup.py install
106
107 And similarily, you can do
108
109setup.py --help
110
111 to get more information.
112
113
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500114 3 OpenSSL -- Python interface to OpenSSL
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500115
116 This package provides a high-level interface to the functions in the
117 OpenSSL library. The following modules are defined:
118
119 crypto
120 Generic cryptographic module. Note that if anything is
121 incomplete, this module is!
122
123 rand
124 An interface to the OpenSSL pseudo random number generator.
125
126 SSL
127 An interface to the SSL-specific parts of OpenSSL.
128
129
1303.1 crypto -- Generic cryptographic module
131
132 X509Type
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500133 See X509.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500134
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500135 class X509()
136 A class representing X.509 certificates.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500137
138 X509NameType
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500139 See X509Name.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500140
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500141 class X509Name(x509name)
142 A class representing X.509 Distinguished Names.
143
144 This constructor creates a copy of x509name which should be an
145 instance of X509Name.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500146
147 X509ReqType
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500148 See X509Req.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500149
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500150 class X509Req()
151 A class representing X.509 certificate requests.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500152
153 X509StoreType
154 A Python type object representing the X509Store object type.
155
156 PKeyType
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500157 See PKey.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500158
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500159 class PKey()
160 A class representing DSA or RSA keys.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500161
162 PKCS7Type
163 A Python type object representing the PKCS7 object type.
164
165 PKCS12Type
166 A Python type object representing the PKCS12 object type.
167
168 X509ExtensionType
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500169 See X509Extension.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500170
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500171 class X509Extension(typename, critical, value[, subject][, issuer])
172 A class representing an X.509 v3 certificate extensions. See
173 http://openssl.org/docs/apps/x509v3_config.html#STANDARD_EXTENSI
174 ONSfor typename strings and their options. Optional parameters
175 subject and issuer must be X509 objects.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500176
177 NetscapeSPKIType
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500178 See NetscapeSPKI.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500179
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500180 class NetscapeSPKI([enc])
181 A class representing Netscape SPKI objects.
182
183 If the enc argument is present, it should be a base64-encoded
184 string representing a NetscapeSPKI object, as returned by the
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500185 b64_encode method.
186
Jean-Paul Calderoneec155e52010-10-07 22:34:15 -0400187 class CRL()
188 A class representing Certifcate Revocation List objects.
189
190 class Revoked()
191 A class representing Revocation objects of CRL.
192
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500193 FILETYPE_PEM
194
195 FILETYPE_ASN1
196 File type constants.
197
198 TYPE_RSA
199
200 TYPE_DSA
201 Key type constants.
202
203 exception Error
204 Generic exception used in the crypto module.
205
206 dump_certificate(type, cert)
207 Dump the certificate cert into a buffer string encoded with the
208 type type.
209
210 dump_certificate_request(type, req)
211 Dump the certificate request req into a buffer string encoded
212 with the type type.
213
214 dump_privatekey(type, pkey[, cipher, passphrase])
215 Dump the private key pkey into a buffer string encoded with the
216 type type, optionally (if type is FILETYPE_PEM) encrypting it
217 using cipher and passphrase.
218
219 passphrase must be either a string or a callback for providing
220 the pass phrase.
221
222 load_certificate(type, buffer)
223 Load a certificate (X509) from the string buffer encoded with
224 the type type.
225
226 load_certificate_request(type, buffer)
227 Load a certificate request (X509Req) from the string buffer
228 encoded with the type type.
229
230 load_privatekey(type, buffer[, passphrase])
231 Load a private key (PKey) from the string buffer encoded with
232 the type type (must be one of FILETYPE_PEM and FILETYPE_ASN1).
233
234 passphrase must be either a string or a callback for providing
235 the pass phrase.
236
Jean-Paul Calderoneec155e52010-10-07 22:34:15 -0400237 load_crl(type, buffer)
238 Load Certificate Revocation List (CRL) data from a string
239 buffer. buffer encoded with the type type. The type type must
240 either FILETYPE_PEM or FILETYPE_ASN1).
241
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500242 load_pkcs7_data(type, buffer)
243 Load pkcs7 data from the string buffer encoded with the type
244 type.
245
246 load_pkcs12(buffer[, passphrase])
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500247 Load pkcs12 data from the string buffer. If the pkcs12 structure
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500248 is encrypted, a passphrase must be included. The MAC is always
249 checked and thus required.
250
251 See also the man page for the C function PKCS12_parse.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500252
Jean-Paul Calderoneec155e52010-10-07 22:34:15 -0400253 sign(key, data, digest)
254 Sign a data string using the given key and message digest.
255
256 key is a PKey instance. data is a str instance. digest is a str
257 naming a supported message digest type, for example ``sha1''.
258 New in version 0.11.
259
260 verify(certificate, signature, data, digest)
261 Verify the signature for a data string.
262
263 certificate is a X509 instance corresponding to the private key
264 which generated the signature. signature is a str instance
265 giving the signature itself. data is a str instance giving the
266 data to which the signature applies. digest is a str instance
267 naming the message digest type of the signature, for example
268 ``sha1''. New in version 0.11.
269
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500270
Jean-Paul Calderone78caacc2011-04-07 15:41:56 -0400271 3.1.1 X509Extension objects
272
273 X509Extension objects have the following methods:
274
275 get_short_name()
276 Retrieve the short descriptive name for this extension.
277
278 The result is a byte string like ``basicConstraints''. New in
279 version 0.12.
280
281 get_data()
282 Retrieve the data for this extension.
283
284 The result is the ASN.1 encoded form of the extension data as a
285 byte string. New in version 0.12.
286
287
288 3.1.2 X509 objects
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500289
290 X509 objects have the following methods:
291
292 get_issuer()
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500293 Return an X509Name object representing the issuer of the
294 certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500295
296 get_pubkey()
297 Return a PKey object representing the public key of the
298 certificate.
299
300 get_serial_number()
301 Return the certificate serial number.
302
303 get_subject()
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500304 Return an X509Name object representing the subject of the
305 certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500306
307 get_version()
308 Return the certificate version.
309
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400310 get_notBefore()
311 Return a string giving the time before which the certificate is
312 not valid. The string is formatted as an ASN1 GENERALIZEDTIME:
313
314 YYYYMMDDhhmmssZ
315 YYYYMMDDhhmmss+hhmm
316 YYYYMMDDhhmmss-hhmm
317
Jean-Paul Calderone24c99262008-03-09 21:48:06 -0400318 If no value exists for this field, None is returned.
319
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400320 get_notAfter()
321 Return a string giving the time after which the certificate is
322 not valid. The string is formatted as an ASN1 GENERALIZEDTIME:
323
324 YYYYMMDDhhmmssZ
325 YYYYMMDDhhmmss+hhmm
326 YYYYMMDDhhmmss-hhmm
327
Jean-Paul Calderone24c99262008-03-09 21:48:06 -0400328 If no value exists for this field, None is returned.
329
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400330 set_notBefore(when)
331 Change the time before which the certificate is not valid. when
332 is a string formatted as an ASN1 GENERALIZEDTIME:
333
334 YYYYMMDDhhmmssZ
335 YYYYMMDDhhmmss+hhmm
336 YYYYMMDDhhmmss-hhmm
337
338 set_notAfter(when)
339 Change the time after which the certificate is not valid. when
340 is a string formatted as an ASN1 GENERALIZEDTIME:
341
342 YYYYMMDDhhmmssZ
343 YYYYMMDDhhmmss+hhmm
344 YYYYMMDDhhmmss-hhmm
345
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500346 gmtime_adj_notBefore(time)
347 Adjust the timestamp (in GMT) when the certificate starts being
348 valid.
349
350 gmtime_adj_notAfter(time)
351 Adjust the timestamp (in GMT) when the certificate stops being
352 valid.
353
354 has_expired()
355 Checks the certificate's time stamp against current time.
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500356 Returns true if the certificate has expired and false otherwise.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500357
358 set_issuer(issuer)
359 Set the issuer of the certificate to issuer.
360
361 set_pubkey(pkey)
362 Set the public key of the certificate to pkey.
363
364 set_serial_number(serialno)
365 Set the serial number of the certificate to serialno.
366
367 set_subject(subject)
368 Set the subject of the certificate to subject.
369
370 set_version(version)
371 Set the certificate version to version.
372
373 sign(pkey, digest)
374 Sign the certificate, using the key pkey and the message digest
375 algorithm identified by the string digest.
376
377 subject_name_hash()
378 Return the hash of the certificate subject.
379
380 digest(digest_name)
381 Return a digest of the certificate, using the digest_name
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500382 method. digest_name must be a string describing a digest
383 algorithm supported by OpenSSL (by EVP_get_digestbyname,
384 specifically). For example, "md5" or "sha1".
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500385
386 add_extensions(extensions)
387 Add the extensions in the sequence extensions to the
388 certificate.
389
Jean-Paul Calderone78caacc2011-04-07 15:41:56 -0400390 get_extension_count()
391 Return the number of extensions on this certificate. New in
392 version 0.12.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500393
Jean-Paul Calderone78caacc2011-04-07 15:41:56 -0400394 get_extension(index)
395 Retrieve the extension on this certificate at the given index.
396
397 Extensions on a certificate are kept in order. The index
398 parameter selects which extension will be returned. The returned
399 object will be an X509Extension instance. New in version 0.12.
400
401
402 3.1.3 X509Name objects
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500403
Jean-Paul Calderone2dd8ff52008-03-24 17:43:58 -0400404 X509Name objects have the following methods:
405
406 hash()
407 Return an integer giving the first four bytes of the MD5 digest
408 of the DER representation of the name.
409
Jean-Paul Calderoned2532d82008-03-25 15:20:39 -0400410 der()
411 Return a string giving the DER representation of the name.
412
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400413 get_components()
414 Return a list of two-tuples of strings giving the components of
415 the name.
416
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500417 X509Name objects have the following members:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500418
419 countryName
420 The country of the entity. C may be used as an alias for
421 countryName.
422
423 stateOrProvinceName
424 The state or province of the entity. ST may be used as an alias
425 for stateOrProvinceName·
426
427 localityName
428 The locality of the entity. L may be used as an alias for
429 localityName.
430
431 organizationName
432 The organization name of the entity. O may be used as an alias
433 for organizationName.
434
435 organizationalUnitName
436 The organizational unit of the entity. OU may be used as an
437 alias for organizationalUnitName.
438
439 commonName
440 The common name of the entity. CN may be used as an alias for
441 commonName.
442
443 emailAddress
444 The e-mail address of the entity.
445
446
Jean-Paul Calderone78caacc2011-04-07 15:41:56 -0400447 3.1.4 X509Req objects
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500448
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500449 X509Req objects have the following methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500450
451 get_pubkey()
452 Return a PKey object representing the public key of the
453 certificate request.
454
455 get_subject()
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500456 Return an X509Name object representing the subject of the
457 certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500458
459 set_pubkey(pkey)
460 Set the public key of the certificate request to pkey.
461
462 sign(pkey, digest)
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500463 Sign the certificate request, using the key pkey and the message
464 digest algorithm identified by the string digest.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500465
466 verify(pkey)
467 Verify a certificate request using the public key pkey.
468
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500469 set_version(version)
470 Set the version (RFC 2459, 4.1.2.1) of the certificate request
471 to version.
472
473 get_version()
474 Get the version (RFC 2459, 4.1.2.1) of the certificate request.
475
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500476
Jean-Paul Calderone78caacc2011-04-07 15:41:56 -0400477 3.1.5 X509Store objects
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500478
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500479 The X509Store object has currently just one method:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500480
481 add_cert(cert)
482 Add the certificate cert to the certificate store.
483
484
Jean-Paul Calderone78caacc2011-04-07 15:41:56 -0400485 3.1.6 PKey objects
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500486
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500487 The PKey object has the following methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500488
489 bits()
490 Return the number of bits of the key.
491
492 generate_key(type, bits)
493 Generate a public/private key pair of the type type (one of
494 TYPE_RSA and TYPE_DSA) with the size bits.
495
496 type()
497 Return the type of the key.
498
499
Jean-Paul Calderone78caacc2011-04-07 15:41:56 -0400500 3.1.7 PKCS7 objects
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500501
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500502 PKCS7 objects have the following methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500503
504 type_is_signed()
505 FIXME
506
507 type_is_enveloped()
508 FIXME
509
510 type_is_signedAndEnveloped()
511 FIXME
512
513 type_is_data()
514 FIXME
515
516 get_type_name()
517 Get the type name of the PKCS7.
518
519
Jean-Paul Calderone78caacc2011-04-07 15:41:56 -0400520 3.1.8 PKCS12 objects
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500521
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500522 PKCS12 objects have the following methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500523
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500524 export([passphrase=None][, iter=2048][, maciter=1])
525 Returns a PKCS12 object as a string.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500526
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500527 The optional passphrase must be a string not a callback.
528
529 See also the man page for the C function PKCS12_create.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500530
531 get_ca_certificates()
532 Return CA certificates within the PKCS12 object as a tuple.
533 Returns None if no CA certificates are present.
534
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500535 get_certificate()
536 Return certificate portion of the PKCS12 structure.
537
538 get_friendlyname()
539 Return friendlyName portion of the PKCS12 structure.
540
541 get_privatekey()
542 Return private key portion of the PKCS12 structure
543
544 set_ca_certificates(cacerts)
545 Replace or set the CA certificates within the PKCS12 object with
546 the sequence cacerts.
547
548 Set cacerts to None to remove all CA certificates.
549
550 set_certificate(cert)
551 Replace or set the certificate portion of the PKCS12 structure.
552
553 set_friendlyname(name)
554 Replace or set the friendlyName portion of the PKCS12 structure.
555
556 set_privatekey(pkey)
557 Replace or set private key portion of the PKCS12 structure
558
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500559
Jean-Paul Calderone78caacc2011-04-07 15:41:56 -0400560 3.1.9 X509Extension objects
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500561
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500562 X509Extension objects have several methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500563
564 get_critical()
565 Return the critical field of the extension object.
566
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500567 get_short_name()
568 Return the short type name of the extension object.
569
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500570
Jean-Paul Calderone78caacc2011-04-07 15:41:56 -0400571 3.1.10 NetscapeSPKI objects
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500572
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500573 NetscapeSPKI objects have the following methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500574
575 b64_encode()
576 Return a base64-encoded string representation of the object.
577
578 get_pubkey()
579 Return the public key of object.
580
581 set_pubkey(key)
582 Set the public key of the object to key.
583
584 sign(key, digest_name)
585 Sign the NetscapeSPKI object using the given key and
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500586 digest_name. digest_name must be a string describing a digest
587 algorithm supported by OpenSSL (by EVP_get_digestbyname,
588 specifically). For example, "md5" or "sha1".
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500589
590 verify(key)
591 Verify the NetscapeSPKI object using the given key.
592
593
Jean-Paul Calderone78caacc2011-04-07 15:41:56 -0400594 3.1.11 CRL objects
Jean-Paul Calderoneec155e52010-10-07 22:34:15 -0400595
596 CRL objects have the following methods:
597
598 add_revoked(revoked)
599 Add a Revoked object to the CRL, by value not reference.
600
601 export(cert, key[, type=FILETYPE_PEM][, days=100])
602 Use cert and key to sign the CRL and return the CRL as a string.
603 days is the number of days before the next CRL is due.
604
605 get_revoked()
606 Return a tuple of Revoked objects, by value not reference.
607
608
Jean-Paul Calderone78caacc2011-04-07 15:41:56 -0400609 3.1.12 Revoked objects
Jean-Paul Calderoneec155e52010-10-07 22:34:15 -0400610
611 Revoked objects have the following methods:
612
613 all_reasons()
614 Return a list of all supported reasons.
615
616 get_reason()
617 Return the revocation reason as a str. Can be None, which
618 differs from "Unspecified".
619
620 get_rev_date()
621 Return the revocation date as a str. The string is formatted as
622 an ASN1 GENERALIZEDTIME.
623
624 get_serial()
625 Return a str containing a hex number of the serial of the
626 revoked certificate.
627
628 set_reason(reason)
629 Set the revocation reason. reason must be None or a string, but
630 the values are limited. Spaces and case are ignored. See
631 all_reasons.
632
633 set_rev_date(date)
634 Set the revocation date. The string is formatted as an ASN1
635 GENERALIZEDTIME.
636
637 set_serial(serial)
638 serial is a string containing a hex number of the serial of the
639 revoked certificate.
640
641
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05006423.2 rand -- An interface to the OpenSSL pseudo random number generator
643
644 This module handles the OpenSSL pseudo random number generator (PRNG)
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500645 and declares the following:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500646
647 add(string, entropy)
648 Mix bytes from string into the PRNG state. The entropy argument
649 is (the lower bound of) an estimate of how much randomness is
650 contained in string, measured in bytes. For more information,
651 see e.g. RFC 1750.
652
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500653 bytes(num_bytes)
654 Get some random bytes from the PRNG as a string.
655
656 This is a wrapper for the C function RAND_bytes.
657
658 cleanup()
659 Erase the memory used by the PRNG.
660
661 This is a wrapper for the C function RAND_cleanup.
662
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500663 egd(path[, bytes])
Jean-Paul Calderonef1b839d2008-09-01 12:06:06 -0400664 Query the Entropy Gathering Daemon^2 on socket path for bytes
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500665 bytes of random data and and uses add to seed the PRNG. The
666 default value of bytes is 255.
667
668 load_file(path[, bytes])
669 Read bytes bytes (or all of it, if bytes is negative) of data
670 from the file path to seed the PRNG. The default value of bytes
671 is -1.
672
673 screen()
674 Add the current contents of the screen to the PRNG state.
675 Availability: Windows.
676
677 seed(string)
678 This is equivalent to calling add with entropy as the length of
679 the string.
680
681 status()
682 Returns true if the PRNG has been seeded with enough data, and
683 false otherwise.
684
685 write_file(path)
686 Write a number of random bytes (currently 1024) to the file
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500687 path. This file can then be used with load_file to seed the PRNG
688 again.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500689
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500690 exception Error
691 If the current RAND method supports any errors, this is raised
692 when needed. The default method does not raise this when the
693 entropy pool is depleted.
694
695 Whenever this exception is raised directly, it has a list of
696 error messages from the OpenSSL error queue, where each item is
697 a tuple (lib, function, reason). Here lib, function and reason
698 are all strings, describing where and what the problem is. See
699 err(3) for more information.
700
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500701
7023.3 SSL -- An interface to the SSL-specific parts of OpenSSL
703
704 This module handles things specific to SSL. There are two objects
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500705 defined: Context, Connection.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500706
707 SSLv2_METHOD
708
709 SSLv3_METHOD
710
711 SSLv23_METHOD
712
713 TLSv1_METHOD
714 These constants represent the different SSL methods to use when
715 creating a context object.
716
717 VERIFY_NONE
718
719 VERIFY_PEER
720
721 VERIFY_FAIL_IF_NO_PEER_CERT
722 These constants represent the verification mode used by the
723 Context object's set_verify method.
724
725 FILETYPE_PEM
726
727 FILETYPE_ASN1
728 File type constants used with the use_certificate_file and
729 use_privatekey_file methods of Context objects.
730
731 OP_SINGLE_DH_USE
732
733 OP_EPHEMERAL_RSA
734
735 OP_NO_SSLv2
736
737 OP_NO_SSLv3
738
739 OP_NO_TLSv1
740 Constants used with set_options of Context objects.
741 OP_SINGLE_DH_USE means to always create a new key when using
742 ephemeral Diffie-Hellman. OP_EPHEMERAL_RSA means to always use
743 ephemeral RSA keys when doing RSA operations. OP_NO_SSLv2,
744 OP_NO_SSLv3 and OP_NO_TLSv1 means to disable those specific
745 protocols. This is interesting if you're using e.g.
746 SSLv23_METHOD to get an SSLv2-compatible handshake, but don't
747 want to use SSLv2.
748
749 ContextType
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500750 See Context.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500751
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500752 class Context(method)
753 A class representing SSL contexts. Contexts define the
754 parameters of one or more SSL connections.
755
756 method should be SSLv2_METHOD, SSLv3_METHOD, SSLv23_METHOD or
757 TLSv1_METHOD.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500758
759 ConnectionType
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500760 See Connection.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500761
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500762 class Connection(context, socket)
763 A class representing SSL connections.
764
765 context should be an instance of Context and socket should be a
766 socket ^3 object. socket may be None; in this case, the
767 Connection is created with a memory BIO: see the bio_read,
768 bio_write, and bio_shutdown methods.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500769
770 exception Error
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500771 This exception is used as a base class for the other SSL-related
772 exceptions, but may also be raised directly.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500773
774 Whenever this exception is raised directly, it has a list of
775 error messages from the OpenSSL error queue, where each item is
776 a tuple (lib, function, reason). Here lib, function and reason
777 are all strings, describing where and what the problem is. See
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500778 err(3) for more information.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500779
780 exception ZeroReturnError
781 This exception matches the error return code
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500782 SSL_ERROR_ZERO_RETURN, and is raised when the SSL Connection has
783 been closed. In SSL 3.0 and TLS 1.0, this only occurs if a
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500784 closure alert has occurred in the protocol, i.e. the connection
785 has been closed cleanly. Note that this does not necessarily
786 mean that the transport layer (e.g. a socket) has been closed.
787
788 It may seem a little strange that this is an exception, but it
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500789 does match an SSL_ERROR code, and is very convenient.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500790
791 exception WantReadError
792 The operation did not complete; the same I/O method should be
793 called again later, with the same arguments. Any I/O method can
794 lead to this since new handshakes can occur at any time.
795
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500796 The wanted read is for dirty data sent over the network, not the
797 clean data inside the tunnel. For a socket based SSL connection,
798 read means data coming at us over the network. Until that read
799 succeeds, the attempted OpenSSL.SSL.Connection.recv,
800 OpenSSL.SSL.Connection.send, or
801 OpenSSL.SSL.Connection.do_handshake is prevented or incomplete.
802 You probably want to select() on the socket before trying again.
803
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500804 exception WantWriteError
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500805 See WantReadError. The socket send buffer may be too full to
806 write more data.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500807
808 exception WantX509LookupError
809 The operation did not complete because an application callback
810 has asked to be called again. The I/O method should be called
811 again later, with the same arguments. Note: This won't occur in
812 this version, as there are no such callbacks in this version.
813
814 exception SysCallError
815 The SysCallError occurs when there's an I/O error and OpenSSL's
816 error queue does not contain any information. This can mean two
817 things: An error in the transport protocol, or an end of file
818 that violates the protocol. The parameter to the exception is
819 always a pair (errnum, errstr).
820
821
822 3.3.1 Context objects
823
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500824 Context objects have the following methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500825
826 check_privatekey()
827 Check if the private key (loaded with use_privatekey[_file])
828 matches the certificate (loaded with use_certificate[_file]).
Jean-Paul Calderonef05fbbe2008-03-06 21:52:35 -0500829 Returns None if they match, raises Error otherwise.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500830
831 get_app_data()
832 Retrieve application data as set by set_app_data.
833
834 get_cert_store()
835 Retrieve the certificate store (a X509Store object) that the
836 context uses. This can be used to add "trusted" certificates
837 without using the. load_verify_locations() method.
838
839 get_timeout()
840 Retrieve session timeout, as set by set_timeout. The default is
841 300 seconds.
842
843 get_verify_depth()
844 Retrieve the Context object's verify depth, as set by
845 set_verify_depth.
846
847 get_verify_mode()
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500848 Retrieve the Context object's verify mode, as set by set_verify.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500849
850 load_client_ca(pemfile)
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500851 Read a file with PEM-formatted certificates that will be sent to
852 the client when requesting a client certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500853
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500854 set_client_ca_list(certificate_authorities)
855 Replace the current list of preferred certificate signers that
856 would be sent to the client when requesting a client certificate
857 with the certificate_authorities sequence of
858 OpenSSL.crypto.X509Names.
859
860 New in version 0.10.
861
862 add_client_ca(certificate_authority)
863 Extract a OpenSSL.crypto.X509Name from the certificate_authority
864 OpenSSL.crypto.X509 certificate and add it to the list of
865 preferred certificate signers sent to the client when requesting
866 a client certificate.
867
868 New in version 0.10.
869
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500870 load_verify_locations(pemfile, capath)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500871 Specify where CA certificates for verification purposes are
872 located. These are trusted certificates. Note that the
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500873 certificates have to be in PEM format. If capath is passed, it
874 must be a directory prepared using the c_rehash tool included
875 with OpenSSL. Either, but not both, of pemfile or capath may be
876 None.
877
878 set_default_verify_paths()
879 Specify that the platform provided CA certificates are to be
880 used for verification purposes. This method may not work
881 properly on OS X.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500882
883 load_tmp_dh(dhfile)
884 Load parameters for Ephemeral Diffie-Hellman from dhfile.
885
886 set_app_data(data)
887 Associate data with this Context object. data can be retrieved
888 later using the get_app_data method.
889
890 set_cipher_list(ciphers)
891 Set the list of ciphers to be used in this context. See the
892 OpenSSL manual for more information (e.g. ciphers(1))
893
894 set_info_callback(callback)
895 Set the information callback to callback. This function will be
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500896 called from time to time during SSL handshakes. callback should
897 take three arguments: a Connection object and two integers. The
898 first integer specifies where in the SSL handshake the function
899 was called, and the other the return code from a (possibly
900 failed) internal function call.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500901
902 set_options(options)
903 Add SSL options. Options you have set before are not cleared!
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500904 This method should be used with the OP_* constants.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500905
906 set_passwd_cb(callback[, userdata])
907 Set the passphrase callback to callback. This function will be
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500908 called when a private key with a passphrase is loaded. callback
Jean-Paul Calderone1eeb29e2008-10-19 11:50:53 -0400909 must accept three positional arguments. First, an integer giving
910 the maximum length of the passphrase it may return. If the
911 returned passphrase is longer than this, it will be truncated.
912 Second, a boolean value which will be true if the user should be
913 prompted for the passphrase twice and the callback should verify
914 that the two values supplied are equal. Third, the value given
915 as the userdata parameter to set_passwd_cb. If an error occurs,
916 callback should return a false value (e.g. an empty string).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500917
918 set_session_id(name)
919 Set the context name within which a session can be reused for
920 this Context object. This is needed when doing session
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500921 resumption, because there is no way for a stored session to know
922 which Context object it is associated with. name may be any
923 binary data.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500924
925 set_timeout(timeout)
926 Set the timeout for newly created sessions for this Context
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500927 object to timeout. timeout must be given in (whole) seconds. The
928 default value is 300 seconds. See the OpenSSL manual for more
929 information (e.g. SSL_CTX_set_timeout(3)).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500930
931 set_verify(mode, callback)
932 Set the verification flags for this Context object to mode and
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500933 specify that callback should be used for verification callbacks.
934 mode should be one of VERIFY_NONE and VERIFY_PEER. If
935 VERIFY_PEER is used, mode can be OR:ed with
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500936 VERIFY_FAIL_IF_NO_PEER_CERT and VERIFY_CLIENT_ONCE to further
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500937 control the behaviour. callback should take five arguments: A
938 Connection object, an X509 object, and three integer variables,
939 which are in turn potential error number, error depth and return
940 code. callback should return true if verification passes and
941 false otherwise.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500942
943 set_verify_depth(depth)
944 Set the maximum depth for the certificate chain verification
945 that shall be allowed for this Context object.
946
947 use_certificate(cert)
948 Use the certificate cert which has to be a X509 object.
949
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500950 add_extra_chain_cert(cert)
951 Adds the certificate cert, which has to be a X509 object, to the
952 certificate chain presented together with the certificate.
953
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500954 use_certificate_chain_file(file)
955 Load a certificate chain from file which must be PEM encoded.
956
957 use_privatekey(pkey)
958 Use the private key pkey which has to be a PKey object.
959
960 use_certificate_file(file[, format])
961 Load the first certificate found in file. The certificate must
962 be in the format specified by format, which is either
963 FILETYPE_PEM or FILETYPE_ASN1. The default is FILETYPE_PEM.
964
965 use_privatekey_file(file[, format])
966 Load the first private key found in file. The private key must
967 be in the format specified by format, which is either
968 FILETYPE_PEM or FILETYPE_ASN1. The default is FILETYPE_PEM.
969
970
971 3.3.2 Connection objects
972
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500973 Connection objects have the following methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500974
975 accept()
976 Call the accept method of the underlying socket and set up SSL
977 on the returned socket, using the Context object supplied to
978 this Connection object at creation. Returns a pair (conn,
979 address). where conn is the new Connection object created, and
980 address is as returned by the socket's accept.
981
982 bind(address)
983 Call the bind method of the underlying socket.
984
985 close()
986 Call the close method of the underlying socket. Note: If you
987 want correct SSL closure, you need to call the shutdown method
988 first.
989
990 connect(address)
991 Call the connect method of the underlying socket and set up SSL
992 on the socket, using the Context object supplied to this
993 Connection object at creation.
994
995 connect_ex(address)
996 Call the connect_ex method of the underlying socket and set up
997 SSL on the socket, using the Context object supplied to this
998 Connection object at creation. Note that if the connect_ex
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500999 method of the socket doesn't return 0, SSL won't be initialized.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001000
1001 do_handshake()
1002 Perform an SSL handshake (usually called after renegotiate or
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001003 one of set_accept_state or set_accept_state). This can raise the
1004 same exceptions as send and recv.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001005
1006 fileno()
1007 Retrieve the file descriptor number for the underlying socket.
1008
1009 listen(backlog)
1010 Call the listen method of the underlying socket.
1011
1012 get_app_data()
1013 Retrieve application data as set by set_app_data.
1014
1015 get_cipher_list()
1016 Retrieve the list of ciphers used by the Connection object.
1017 WARNING: This API has changed. It used to take an optional
1018 parameter and just return a string, but not it returns the
1019 entire list in one go.
1020
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -05001021 get_client_ca_list()
1022 Retrieve the list of preferred client certificate issuers sent
1023 by the server as OpenSSL.crypto.X509Name objects.
1024
1025 If this is a client Connection, the list will be empty until the
1026 connection with the server is established.
1027
1028 If this is a server Connection, return the list of certificate
1029 authorities that will be sent or has been sent to the client, as
1030 controlled by this Connection's Context.
1031
1032 New in version 0.10.
1033
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001034 get_context()
1035 Retrieve the Context object associated with this Connection.
1036
1037 get_peer_certificate()
1038 Retrieve the other side's certificate (if any)
1039
1040 getpeername()
1041 Call the getpeername method of the underlying socket.
1042
1043 getsockname()
1044 Call the getsockname method of the underlying socket.
1045
1046 getsockopt(level, optname[, buflen])
1047 Call the getsockopt method of the underlying socket.
1048
1049 pending()
1050 Retrieve the number of bytes that can be safely read from the
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001051 SSL buffer (not the underlying transport buffer).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001052
1053 recv(bufsize)
1054 Receive data from the Connection. The return value is a string
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001055 representing the data received. The maximum amount of data to be
1056 received at once, is specified by bufsize.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001057
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -05001058 bio_write(bytes)
1059 If the Connection was created with a memory BIO, this method can
1060 be used to add bytes to the read end of that memory BIO. The
1061 Connection can then read the bytes (for example, in response to
1062 a call to recv).
1063
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001064 renegotiate()
1065 Renegotiate the SSL session. Call this if you wish to change
1066 cipher suites or anything like that.
1067
1068 send(string)
1069 Send the string data to the Connection.
1070
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -05001071 bio_read(bufsize)
1072 If the Connection was created with a memory BIO, this method can
1073 be used to read bytes from the write end of that memory BIO.
1074 Many Connection methods will add bytes which must be read in
1075 this manner or the buffer will eventually fill up and the
1076 Connection will be able to take no further actions.
1077
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001078 sendall(string)
1079 Send all of the string data to the Connection. This calls send
1080 repeatedly until all data is sent. If an error occurs, it's
1081 impossible to tell how much data has been sent.
1082
1083 set_accept_state()
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001084 Set the connection to work in server mode. The handshake will be
1085 handled automatically by read/write.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001086
1087 set_app_data(data)
1088 Associate data with this Connection object. data can be
1089 retrieved later using the get_app_data method.
1090
1091 set_connect_state()
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001092 Set the connection to work in client mode. The handshake will be
1093 handled automatically by read/write.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001094
1095 setblocking(flag)
1096 Call the setblocking method of the underlying socket.
1097
1098 setsockopt(level, optname, value)
1099 Call the setsockopt method of the underlying socket.
1100
1101 shutdown()
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001102 Send the shutdown message to the Connection. Returns true if the
1103 shutdown message exchange is completed and false otherwise (in
1104 which case you call recv() or send() when the connection becomes
1105 readable/writeable.
1106
1107 get_shutdown()
1108 Get the shutdown state of the Connection. Returns a bitvector of
1109 either or both of SENT_SHUTDOWN and RECEIVED_SHUTDOWN.
1110
1111 set_shutdown(state)
1112 Set the shutdown state of the Connection. state is a bitvector
1113 of either or both of SENT_SHUTDOWN and RECEIVED_SHUTDOWN.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001114
1115 sock_shutdown(how)
1116 Call the shutdown method of the underlying socket.
1117
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -05001118 bio_shutdown()
1119 If the Connection was created with a memory BIO, this method can
1120 be used to indicate that ``end of file'' has been reached on the
1121 read end of that memory BIO.
1122
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001123 state_string()
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001124 Retrieve a verbose string detailing the state of the Connection.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001125
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -05001126 client_random()
1127 Retrieve the random value used with the client hello message.
1128
1129 server_random()
1130 Retrieve the random value used with the server hello message.
1131
1132 master_key()
1133 Retrieve the value of the master key for this session.
1134
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001135 want_read()
1136 Checks if more data has to be read from the transport layer to
1137 complete an operation.
1138
1139 want_write()
1140 Checks if there is data to write to the transport layer to
1141 complete an operation.
1142
1143
1144 4 Internals
1145
1146 We ran into three main problems developing this: Exceptions, callbacks
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001147 and accessing socket methods. This is what this chapter is about.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001148
1149
11504.1 Exceptions
1151
1152 We realized early that most of the exceptions would be raised by the
1153 I/O functions of OpenSSL, so it felt natural to mimic OpenSSL's error
1154 code system, translating them into Python exceptions. This naturally
1155 gives us the exceptions SSL.ZeroReturnError, SSL.WantReadError,
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001156 SSL.WantWriteError, SSL.WantX509LookupError and SSL.SysCallError.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001157
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001158 For more information about this, see section 3.3.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001159
1160
11614.2 Callbacks
1162
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001163 There are a number of problems with callbacks. First of all, OpenSSL is
1164 written as a C library, it's not meant to have Python callbacks, so a
1165 way around that is needed. Another problem is thread support. A lot of
1166 the OpenSSL I/O functions can block if the socket is in blocking mode,
1167 and then you want other Python threads to be able to do other things.
Jean-Paul Calderone657d3ec2008-09-21 18:59:46 -04001168 The real trouble is if you've released the global CPython interpreter
1169 lock to do a potentially blocking operation, and the operation calls a
1170 callback. Then we must take the GIL back, since calling Python APIs
1171 without holding it is not allowed.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001172
1173 There are two solutions to the first problem, both of which are
1174 necessary. The first solution to use is if the C callback allows
1175 ''userdata'' to be passed to it (an arbitrary pointer normally). This
1176 is great! We can set our Python function object as the real userdata
1177 and emulate userdata for the Python function in another way. The other
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001178 solution can be used if an object with an ''app_data'' system always is
1179 passed to the callback. For example, the SSL object in OpenSSL has
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001180 app_data functions and in e.g. the verification callbacks, you can
1181 retrieve the related SSL object. What we do is to set our wrapper
1182 Connection object as app_data for the SSL object, and we can easily
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001183 find the Python callback.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001184
Jean-Paul Calderone657d3ec2008-09-21 18:59:46 -04001185 The other problem is solved using thread local variables. Whenever the
1186 GIL is released before calling into an OpenSSL API, the PyThreadState
1187 pointer returned by PyEval_SaveState is stored in a global thread local
1188 variable (using Python's own TLS API, PyThread_set_key_value). When it
1189 is necessary to re-acquire the GIL, either after the OpenSSL API
1190 returns or in a C callback invoked by that OpenSSL API, the value of
1191 the thread local variable is retrieved (PyThread_get_key_value) and
1192 used to re-acquire the GIL. This allows Python threads to execute while
1193 OpenSSL APIs are running and allows use of any particular pyOpenSSL
1194 object from any Python thread, since there is no per-thread state
1195 associated with any of these objects and since OpenSSL is threadsafe
1196 (as long as properly initialized, as pyOpenSSL initializes it).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001197
1198
11994.3 Acessing Socket Methods
1200
1201 We quickly saw the benefit of wrapping socket methods in the
1202 SSL.Connection class, for an easy transition into using SSL. The
1203 problem here is that the socket module lacks a C API, and all the
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001204 methods are declared static. One approach would be to have OpenSSL as a
1205 submodule to the socket module, placing all the code in socketmodule.c,
1206 but this is obviously not a good solution, since you might not want to
1207 import tonnes of extra stuff you're not going to use when importing the
1208 socket module. The other approach is to somehow get a pointer to the
1209 method to be called, either the C function, or a callable Python
1210 object. This is not really a good solution either, since there's a lot
1211 of lookups involved.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001212
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001213 The way it works is that you have to supply a ``socket-like'' transport
1214 object to the SSL.Connection. The only requirement of this object is
1215 that it has a fileno() method that returns a file descriptor that's
1216 valid at the C level (i.e. you can use the system calls read and
1217 write). If you want to use the connect() or accept() methods of the
1218 SSL.Connection object, the transport object has to supply such methods
1219 too. Apart from them, any method lookups in the SSL.Connection object
1220 that fail are passed on to the underlying transport object.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001221
1222 Future changes might be to allow Python-level transport objects, that
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001223 instead of having fileno() methods, have read() and write() methods, so
1224 more advanced features of Python can be used. This would probably
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001225 entail some sort of OpenSSL ``BIOs'', but converting Python strings
1226 back and forth is expensive, so this shouldn't be used unless
1227 necessary. Other nice things would be to be able to pass in different
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001228 transport objects for reading and writing, but then the fileno() method
1229 of SSL.Connection becomes virtually useless. Also, should the method
1230 resolution be used on the read-transport or the write-transport?
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001231
1232 About this document ...
1233
1234 Python OpenSSL Manual
1235
1236 This document was generated using the LaTeX2HTML translator.
1237
1238 LaTeX2HTML is Copyright © 1993, 1994, 1995, 1996, 1997, Nikos Drakos,
1239 Computer Based Learning Unit, University of Leeds, and Copyright ©
1240 1997, 1998, Ross Moore, Mathematics Department, Macquarie University,
1241 Sydney.
1242
1243 The application of LaTeX2HTML to the Python documentation has been
1244 heavily tailored by Fred L. Drake, Jr. Original navigation icons were
1245 contributed by Christopher Petrilli.
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001246 __________________________________________________________________
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001247
1248 Footnotes
1249
1250 ... M2Crypto^1
Jean-Paul Calderonef1b839d2008-09-01 12:06:06 -04001251 See http://chandlerproject.org/Projects/MeTooCrypto
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001252
Jean-Paul Calderonef1b839d2008-09-01 12:06:06 -04001253 ... Daemon^2
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001254 See http://www.lothar.com/tech/crypto/
1255
Jean-Paul Calderonef1b839d2008-09-01 12:06:06 -04001256 ... socket^3
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001257 Actually, all that is required is an object that behaves like a
1258 socket, you could even use files, even though it'd be tricky to
1259 get the handshakes right!
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001260 __________________________________________________________________
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001261
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001262 Python OpenSSL Manual
1263 __________________________________________________________________
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001264
Jean-Paul Calderone26ebc9e2011-04-11 19:57:10 -04001265 Release 0.12.