blob: 198fe835767775977993fcd1cfdc46134278a4b2 [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
187 FILETYPE_PEM
188
189 FILETYPE_ASN1
190 File type constants.
191
192 TYPE_RSA
193
194 TYPE_DSA
195 Key type constants.
196
197 exception Error
198 Generic exception used in the crypto module.
199
200 dump_certificate(type, cert)
201 Dump the certificate cert into a buffer string encoded with the
202 type type.
203
204 dump_certificate_request(type, req)
205 Dump the certificate request req into a buffer string encoded
206 with the type type.
207
208 dump_privatekey(type, pkey[, cipher, passphrase])
209 Dump the private key pkey into a buffer string encoded with the
210 type type, optionally (if type is FILETYPE_PEM) encrypting it
211 using cipher and passphrase.
212
213 passphrase must be either a string or a callback for providing
214 the pass phrase.
215
216 load_certificate(type, buffer)
217 Load a certificate (X509) from the string buffer encoded with
218 the type type.
219
220 load_certificate_request(type, buffer)
221 Load a certificate request (X509Req) from the string buffer
222 encoded with the type type.
223
224 load_privatekey(type, buffer[, passphrase])
225 Load a private key (PKey) from the string buffer encoded with
226 the type type (must be one of FILETYPE_PEM and FILETYPE_ASN1).
227
228 passphrase must be either a string or a callback for providing
229 the pass phrase.
230
231 load_pkcs7_data(type, buffer)
232 Load pkcs7 data from the string buffer encoded with the type
233 type.
234
235 load_pkcs12(buffer[, passphrase])
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500236 Load pkcs12 data from the string buffer. If the pkcs12 structure
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500237 is encrypted, a passphrase must be included. The MAC is always
238 checked and thus required.
239
240 See also the man page for the C function PKCS12_parse.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500241
242
243 3.1.1 X509 objects
244
245 X509 objects have the following methods:
246
247 get_issuer()
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500248 Return an X509Name object representing the issuer of the
249 certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500250
251 get_pubkey()
252 Return a PKey object representing the public key of the
253 certificate.
254
255 get_serial_number()
256 Return the certificate serial number.
257
258 get_subject()
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500259 Return an X509Name object representing the subject of the
260 certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500261
262 get_version()
263 Return the certificate version.
264
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400265 get_notBefore()
266 Return a string giving the time before which the certificate is
267 not valid. The string is formatted as an ASN1 GENERALIZEDTIME:
268
269 YYYYMMDDhhmmssZ
270 YYYYMMDDhhmmss+hhmm
271 YYYYMMDDhhmmss-hhmm
272
Jean-Paul Calderone24c99262008-03-09 21:48:06 -0400273 If no value exists for this field, None is returned.
274
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400275 get_notAfter()
276 Return a string giving the time after which the certificate is
277 not valid. The string is formatted as an ASN1 GENERALIZEDTIME:
278
279 YYYYMMDDhhmmssZ
280 YYYYMMDDhhmmss+hhmm
281 YYYYMMDDhhmmss-hhmm
282
Jean-Paul Calderone24c99262008-03-09 21:48:06 -0400283 If no value exists for this field, None is returned.
284
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400285 set_notBefore(when)
286 Change the time before which the certificate is not valid. when
287 is a string formatted as an ASN1 GENERALIZEDTIME:
288
289 YYYYMMDDhhmmssZ
290 YYYYMMDDhhmmss+hhmm
291 YYYYMMDDhhmmss-hhmm
292
293 set_notAfter(when)
294 Change the time after which the certificate is not valid. when
295 is a string formatted as an ASN1 GENERALIZEDTIME:
296
297 YYYYMMDDhhmmssZ
298 YYYYMMDDhhmmss+hhmm
299 YYYYMMDDhhmmss-hhmm
300
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500301 gmtime_adj_notBefore(time)
302 Adjust the timestamp (in GMT) when the certificate starts being
303 valid.
304
305 gmtime_adj_notAfter(time)
306 Adjust the timestamp (in GMT) when the certificate stops being
307 valid.
308
309 has_expired()
310 Checks the certificate's time stamp against current time.
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500311 Returns true if the certificate has expired and false otherwise.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500312
313 set_issuer(issuer)
314 Set the issuer of the certificate to issuer.
315
316 set_pubkey(pkey)
317 Set the public key of the certificate to pkey.
318
319 set_serial_number(serialno)
320 Set the serial number of the certificate to serialno.
321
322 set_subject(subject)
323 Set the subject of the certificate to subject.
324
325 set_version(version)
326 Set the certificate version to version.
327
328 sign(pkey, digest)
329 Sign the certificate, using the key pkey and the message digest
330 algorithm identified by the string digest.
331
332 subject_name_hash()
333 Return the hash of the certificate subject.
334
335 digest(digest_name)
336 Return a digest of the certificate, using the digest_name
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500337 method. digest_name must be a string describing a digest
338 algorithm supported by OpenSSL (by EVP_get_digestbyname,
339 specifically). For example, "md5" or "sha1".
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500340
341 add_extensions(extensions)
342 Add the extensions in the sequence extensions to the
343 certificate.
344
345
346 3.1.2 X509Name objects
347
Jean-Paul Calderone2dd8ff52008-03-24 17:43:58 -0400348 X509Name objects have the following methods:
349
350 hash()
351 Return an integer giving the first four bytes of the MD5 digest
352 of the DER representation of the name.
353
Jean-Paul Calderoned2532d82008-03-25 15:20:39 -0400354 der()
355 Return a string giving the DER representation of the name.
356
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400357 get_components()
358 Return a list of two-tuples of strings giving the components of
359 the name.
360
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500361 X509Name objects have the following members:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500362
363 countryName
364 The country of the entity. C may be used as an alias for
365 countryName.
366
367 stateOrProvinceName
368 The state or province of the entity. ST may be used as an alias
369 for stateOrProvinceName·
370
371 localityName
372 The locality of the entity. L may be used as an alias for
373 localityName.
374
375 organizationName
376 The organization name of the entity. O may be used as an alias
377 for organizationName.
378
379 organizationalUnitName
380 The organizational unit of the entity. OU may be used as an
381 alias for organizationalUnitName.
382
383 commonName
384 The common name of the entity. CN may be used as an alias for
385 commonName.
386
387 emailAddress
388 The e-mail address of the entity.
389
390
391 3.1.3 X509Req objects
392
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500393 X509Req objects have the following methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500394
395 get_pubkey()
396 Return a PKey object representing the public key of the
397 certificate request.
398
399 get_subject()
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500400 Return an X509Name object representing the subject of the
401 certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500402
403 set_pubkey(pkey)
404 Set the public key of the certificate request to pkey.
405
406 sign(pkey, digest)
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500407 Sign the certificate request, using the key pkey and the message
408 digest algorithm identified by the string digest.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500409
410 verify(pkey)
411 Verify a certificate request using the public key pkey.
412
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500413 set_version(version)
414 Set the version (RFC 2459, 4.1.2.1) of the certificate request
415 to version.
416
417 get_version()
418 Get the version (RFC 2459, 4.1.2.1) of the certificate request.
419
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500420
421 3.1.4 X509Store objects
422
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500423 The X509Store object has currently just one method:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500424
425 add_cert(cert)
426 Add the certificate cert to the certificate store.
427
428
429 3.1.5 PKey objects
430
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500431 The PKey object has the following methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500432
433 bits()
434 Return the number of bits of the key.
435
436 generate_key(type, bits)
437 Generate a public/private key pair of the type type (one of
438 TYPE_RSA and TYPE_DSA) with the size bits.
439
440 type()
441 Return the type of the key.
442
443
444 3.1.6 PKCS7 objects
445
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500446 PKCS7 objects have the following methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500447
448 type_is_signed()
449 FIXME
450
451 type_is_enveloped()
452 FIXME
453
454 type_is_signedAndEnveloped()
455 FIXME
456
457 type_is_data()
458 FIXME
459
460 get_type_name()
461 Get the type name of the PKCS7.
462
463
464 3.1.7 PKCS12 objects
465
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500466 PKCS12 objects have the following methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500467
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500468 export([passphrase=None][, iter=2048][, maciter=1])
469 Returns a PKCS12 object as a string.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500470
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500471 The optional passphrase must be a string not a callback.
472
473 See also the man page for the C function PKCS12_create.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500474
475 get_ca_certificates()
476 Return CA certificates within the PKCS12 object as a tuple.
477 Returns None if no CA certificates are present.
478
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500479 get_certificate()
480 Return certificate portion of the PKCS12 structure.
481
482 get_friendlyname()
483 Return friendlyName portion of the PKCS12 structure.
484
485 get_privatekey()
486 Return private key portion of the PKCS12 structure
487
488 set_ca_certificates(cacerts)
489 Replace or set the CA certificates within the PKCS12 object with
490 the sequence cacerts.
491
492 Set cacerts to None to remove all CA certificates.
493
494 set_certificate(cert)
495 Replace or set the certificate portion of the PKCS12 structure.
496
497 set_friendlyname(name)
498 Replace or set the friendlyName portion of the PKCS12 structure.
499
500 set_privatekey(pkey)
501 Replace or set private key portion of the PKCS12 structure
502
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500503
504 3.1.8 X509Extension objects
505
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500506 X509Extension objects have several methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500507
508 get_critical()
509 Return the critical field of the extension object.
510
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500511 get_short_name()
512 Return the short type name of the extension object.
513
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500514
515 3.1.9 NetscapeSPKI objects
516
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500517 NetscapeSPKI objects have the following methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500518
519 b64_encode()
520 Return a base64-encoded string representation of the object.
521
522 get_pubkey()
523 Return the public key of object.
524
525 set_pubkey(key)
526 Set the public key of the object to key.
527
528 sign(key, digest_name)
529 Sign the NetscapeSPKI object using the given key and
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500530 digest_name. digest_name must be a string describing a digest
531 algorithm supported by OpenSSL (by EVP_get_digestbyname,
532 specifically). For example, "md5" or "sha1".
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500533
534 verify(key)
535 Verify the NetscapeSPKI object using the given key.
536
537
5383.2 rand -- An interface to the OpenSSL pseudo random number generator
539
540 This module handles the OpenSSL pseudo random number generator (PRNG)
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500541 and declares the following:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500542
543 add(string, entropy)
544 Mix bytes from string into the PRNG state. The entropy argument
545 is (the lower bound of) an estimate of how much randomness is
546 contained in string, measured in bytes. For more information,
547 see e.g. RFC 1750.
548
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500549 bytes(num_bytes)
550 Get some random bytes from the PRNG as a string.
551
552 This is a wrapper for the C function RAND_bytes.
553
554 cleanup()
555 Erase the memory used by the PRNG.
556
557 This is a wrapper for the C function RAND_cleanup.
558
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500559 egd(path[, bytes])
Jean-Paul Calderonef1b839d2008-09-01 12:06:06 -0400560 Query the Entropy Gathering Daemon^2 on socket path for bytes
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500561 bytes of random data and and uses add to seed the PRNG. The
562 default value of bytes is 255.
563
564 load_file(path[, bytes])
565 Read bytes bytes (or all of it, if bytes is negative) of data
566 from the file path to seed the PRNG. The default value of bytes
567 is -1.
568
569 screen()
570 Add the current contents of the screen to the PRNG state.
571 Availability: Windows.
572
573 seed(string)
574 This is equivalent to calling add with entropy as the length of
575 the string.
576
577 status()
578 Returns true if the PRNG has been seeded with enough data, and
579 false otherwise.
580
581 write_file(path)
582 Write a number of random bytes (currently 1024) to the file
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500583 path. This file can then be used with load_file to seed the PRNG
584 again.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500585
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500586 exception Error
587 If the current RAND method supports any errors, this is raised
588 when needed. The default method does not raise this when the
589 entropy pool is depleted.
590
591 Whenever this exception is raised directly, it has a list of
592 error messages from the OpenSSL error queue, where each item is
593 a tuple (lib, function, reason). Here lib, function and reason
594 are all strings, describing where and what the problem is. See
595 err(3) for more information.
596
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500597
5983.3 SSL -- An interface to the SSL-specific parts of OpenSSL
599
600 This module handles things specific to SSL. There are two objects
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500601 defined: Context, Connection.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500602
603 SSLv2_METHOD
604
605 SSLv3_METHOD
606
607 SSLv23_METHOD
608
609 TLSv1_METHOD
610 These constants represent the different SSL methods to use when
611 creating a context object.
612
613 VERIFY_NONE
614
615 VERIFY_PEER
616
617 VERIFY_FAIL_IF_NO_PEER_CERT
618 These constants represent the verification mode used by the
619 Context object's set_verify method.
620
621 FILETYPE_PEM
622
623 FILETYPE_ASN1
624 File type constants used with the use_certificate_file and
625 use_privatekey_file methods of Context objects.
626
627 OP_SINGLE_DH_USE
628
629 OP_EPHEMERAL_RSA
630
631 OP_NO_SSLv2
632
633 OP_NO_SSLv3
634
635 OP_NO_TLSv1
636 Constants used with set_options of Context objects.
637 OP_SINGLE_DH_USE means to always create a new key when using
638 ephemeral Diffie-Hellman. OP_EPHEMERAL_RSA means to always use
639 ephemeral RSA keys when doing RSA operations. OP_NO_SSLv2,
640 OP_NO_SSLv3 and OP_NO_TLSv1 means to disable those specific
641 protocols. This is interesting if you're using e.g.
642 SSLv23_METHOD to get an SSLv2-compatible handshake, but don't
643 want to use SSLv2.
644
645 ContextType
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500646 See Context.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500647
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500648 class Context(method)
649 A class representing SSL contexts. Contexts define the
650 parameters of one or more SSL connections.
651
652 method should be SSLv2_METHOD, SSLv3_METHOD, SSLv23_METHOD or
653 TLSv1_METHOD.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500654
655 ConnectionType
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500656 See Connection.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500657
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500658 class Connection(context, socket)
659 A class representing SSL connections.
660
661 context should be an instance of Context and socket should be a
662 socket ^3 object. socket may be None; in this case, the
663 Connection is created with a memory BIO: see the bio_read,
664 bio_write, and bio_shutdown methods.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500665
666 exception Error
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500667 This exception is used as a base class for the other SSL-related
668 exceptions, but may also be raised directly.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500669
670 Whenever this exception is raised directly, it has a list of
671 error messages from the OpenSSL error queue, where each item is
672 a tuple (lib, function, reason). Here lib, function and reason
673 are all strings, describing where and what the problem is. See
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500674 err(3) for more information.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500675
676 exception ZeroReturnError
677 This exception matches the error return code
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500678 SSL_ERROR_ZERO_RETURN, and is raised when the SSL Connection has
679 been closed. In SSL 3.0 and TLS 1.0, this only occurs if a
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500680 closure alert has occurred in the protocol, i.e. the connection
681 has been closed cleanly. Note that this does not necessarily
682 mean that the transport layer (e.g. a socket) has been closed.
683
684 It may seem a little strange that this is an exception, but it
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500685 does match an SSL_ERROR code, and is very convenient.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500686
687 exception WantReadError
688 The operation did not complete; the same I/O method should be
689 called again later, with the same arguments. Any I/O method can
690 lead to this since new handshakes can occur at any time.
691
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500692 The wanted read is for dirty data sent over the network, not the
693 clean data inside the tunnel. For a socket based SSL connection,
694 read means data coming at us over the network. Until that read
695 succeeds, the attempted OpenSSL.SSL.Connection.recv,
696 OpenSSL.SSL.Connection.send, or
697 OpenSSL.SSL.Connection.do_handshake is prevented or incomplete.
698 You probably want to select() on the socket before trying again.
699
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500700 exception WantWriteError
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500701 See WantReadError. The socket send buffer may be too full to
702 write more data.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500703
704 exception WantX509LookupError
705 The operation did not complete because an application callback
706 has asked to be called again. The I/O method should be called
707 again later, with the same arguments. Note: This won't occur in
708 this version, as there are no such callbacks in this version.
709
710 exception SysCallError
711 The SysCallError occurs when there's an I/O error and OpenSSL's
712 error queue does not contain any information. This can mean two
713 things: An error in the transport protocol, or an end of file
714 that violates the protocol. The parameter to the exception is
715 always a pair (errnum, errstr).
716
717
718 3.3.1 Context objects
719
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500720 Context objects have the following methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500721
722 check_privatekey()
723 Check if the private key (loaded with use_privatekey[_file])
724 matches the certificate (loaded with use_certificate[_file]).
Jean-Paul Calderonef05fbbe2008-03-06 21:52:35 -0500725 Returns None if they match, raises Error otherwise.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500726
727 get_app_data()
728 Retrieve application data as set by set_app_data.
729
730 get_cert_store()
731 Retrieve the certificate store (a X509Store object) that the
732 context uses. This can be used to add "trusted" certificates
733 without using the. load_verify_locations() method.
734
735 get_timeout()
736 Retrieve session timeout, as set by set_timeout. The default is
737 300 seconds.
738
739 get_verify_depth()
740 Retrieve the Context object's verify depth, as set by
741 set_verify_depth.
742
743 get_verify_mode()
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500744 Retrieve the Context object's verify mode, as set by set_verify.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500745
746 load_client_ca(pemfile)
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500747 Read a file with PEM-formatted certificates that will be sent to
748 the client when requesting a client certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500749
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500750 set_client_ca_list(certificate_authorities)
751 Replace the current list of preferred certificate signers that
752 would be sent to the client when requesting a client certificate
753 with the certificate_authorities sequence of
754 OpenSSL.crypto.X509Names.
755
756 New in version 0.10.
757
758 add_client_ca(certificate_authority)
759 Extract a OpenSSL.crypto.X509Name from the certificate_authority
760 OpenSSL.crypto.X509 certificate and add it to the list of
761 preferred certificate signers sent to the client when requesting
762 a client certificate.
763
764 New in version 0.10.
765
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500766 load_verify_locations(pemfile, capath)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500767 Specify where CA certificates for verification purposes are
768 located. These are trusted certificates. Note that the
Jean-Paul Calderone6c1d4f92009-03-07 09:10:30 -0500769 certificates have to be in PEM format. If capath is passed, it
770 must be a directory prepared using the c_rehash tool included
771 with OpenSSL. Either, but not both, of pemfile or capath may be
772 None.
773
774 set_default_verify_paths()
775 Specify that the platform provided CA certificates are to be
776 used for verification purposes. This method may not work
777 properly on OS X.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500778
779 load_tmp_dh(dhfile)
780 Load parameters for Ephemeral Diffie-Hellman from dhfile.
781
782 set_app_data(data)
783 Associate data with this Context object. data can be retrieved
784 later using the get_app_data method.
785
786 set_cipher_list(ciphers)
787 Set the list of ciphers to be used in this context. See the
788 OpenSSL manual for more information (e.g. ciphers(1))
789
790 set_info_callback(callback)
791 Set the information callback to callback. This function will be
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500792 called from time to time during SSL handshakes. callback should
793 take three arguments: a Connection object and two integers. The
794 first integer specifies where in the SSL handshake the function
795 was called, and the other the return code from a (possibly
796 failed) internal function call.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500797
798 set_options(options)
799 Add SSL options. Options you have set before are not cleared!
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500800 This method should be used with the OP_* constants.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500801
802 set_passwd_cb(callback[, userdata])
803 Set the passphrase callback to callback. This function will be
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500804 called when a private key with a passphrase is loaded. callback
Jean-Paul Calderone1eeb29e2008-10-19 11:50:53 -0400805 must accept three positional arguments. First, an integer giving
806 the maximum length of the passphrase it may return. If the
807 returned passphrase is longer than this, it will be truncated.
808 Second, a boolean value which will be true if the user should be
809 prompted for the passphrase twice and the callback should verify
810 that the two values supplied are equal. Third, the value given
811 as the userdata parameter to set_passwd_cb. If an error occurs,
812 callback should return a false value (e.g. an empty string).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500813
814 set_session_id(name)
815 Set the context name within which a session can be reused for
816 this Context object. This is needed when doing session
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500817 resumption, because there is no way for a stored session to know
818 which Context object it is associated with. name may be any
819 binary data.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500820
821 set_timeout(timeout)
822 Set the timeout for newly created sessions for this Context
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500823 object to timeout. timeout must be given in (whole) seconds. The
824 default value is 300 seconds. See the OpenSSL manual for more
825 information (e.g. SSL_CTX_set_timeout(3)).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500826
827 set_verify(mode, callback)
828 Set the verification flags for this Context object to mode and
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500829 specify that callback should be used for verification callbacks.
830 mode should be one of VERIFY_NONE and VERIFY_PEER. If
831 VERIFY_PEER is used, mode can be OR:ed with
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500832 VERIFY_FAIL_IF_NO_PEER_CERT and VERIFY_CLIENT_ONCE to further
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500833 control the behaviour. callback should take five arguments: A
834 Connection object, an X509 object, and three integer variables,
835 which are in turn potential error number, error depth and return
836 code. callback should return true if verification passes and
837 false otherwise.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500838
839 set_verify_depth(depth)
840 Set the maximum depth for the certificate chain verification
841 that shall be allowed for this Context object.
842
843 use_certificate(cert)
844 Use the certificate cert which has to be a X509 object.
845
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500846 add_extra_chain_cert(cert)
847 Adds the certificate cert, which has to be a X509 object, to the
848 certificate chain presented together with the certificate.
849
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500850 use_certificate_chain_file(file)
851 Load a certificate chain from file which must be PEM encoded.
852
853 use_privatekey(pkey)
854 Use the private key pkey which has to be a PKey object.
855
856 use_certificate_file(file[, format])
857 Load the first certificate found in file. The certificate must
858 be in the format specified by format, which is either
859 FILETYPE_PEM or FILETYPE_ASN1. The default is FILETYPE_PEM.
860
861 use_privatekey_file(file[, format])
862 Load the first private key found in file. The private key must
863 be in the format specified by format, which is either
864 FILETYPE_PEM or FILETYPE_ASN1. The default is FILETYPE_PEM.
865
866
867 3.3.2 Connection objects
868
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500869 Connection objects have the following methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500870
871 accept()
872 Call the accept method of the underlying socket and set up SSL
873 on the returned socket, using the Context object supplied to
874 this Connection object at creation. Returns a pair (conn,
875 address). where conn is the new Connection object created, and
876 address is as returned by the socket's accept.
877
878 bind(address)
879 Call the bind method of the underlying socket.
880
881 close()
882 Call the close method of the underlying socket. Note: If you
883 want correct SSL closure, you need to call the shutdown method
884 first.
885
886 connect(address)
887 Call the connect method of the underlying socket and set up SSL
888 on the socket, using the Context object supplied to this
889 Connection object at creation.
890
891 connect_ex(address)
892 Call the connect_ex method of the underlying socket and set up
893 SSL on the socket, using the Context object supplied to this
894 Connection object at creation. Note that if the connect_ex
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500895 method of the socket doesn't return 0, SSL won't be initialized.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500896
897 do_handshake()
898 Perform an SSL handshake (usually called after renegotiate or
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500899 one of set_accept_state or set_accept_state). This can raise the
900 same exceptions as send and recv.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500901
902 fileno()
903 Retrieve the file descriptor number for the underlying socket.
904
905 listen(backlog)
906 Call the listen method of the underlying socket.
907
908 get_app_data()
909 Retrieve application data as set by set_app_data.
910
911 get_cipher_list()
912 Retrieve the list of ciphers used by the Connection object.
913 WARNING: This API has changed. It used to take an optional
914 parameter and just return a string, but not it returns the
915 entire list in one go.
916
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500917 get_client_ca_list()
918 Retrieve the list of preferred client certificate issuers sent
919 by the server as OpenSSL.crypto.X509Name objects.
920
921 If this is a client Connection, the list will be empty until the
922 connection with the server is established.
923
924 If this is a server Connection, return the list of certificate
925 authorities that will be sent or has been sent to the client, as
926 controlled by this Connection's Context.
927
928 New in version 0.10.
929
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500930 get_context()
931 Retrieve the Context object associated with this Connection.
932
933 get_peer_certificate()
934 Retrieve the other side's certificate (if any)
935
936 getpeername()
937 Call the getpeername method of the underlying socket.
938
939 getsockname()
940 Call the getsockname method of the underlying socket.
941
942 getsockopt(level, optname[, buflen])
943 Call the getsockopt method of the underlying socket.
944
945 pending()
946 Retrieve the number of bytes that can be safely read from the
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500947 SSL buffer (not the underlying transport buffer).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500948
949 recv(bufsize)
950 Receive data from the Connection. The return value is a string
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500951 representing the data received. The maximum amount of data to be
952 received at once, is specified by bufsize.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500953
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500954 bio_write(bytes)
955 If the Connection was created with a memory BIO, this method can
956 be used to add bytes to the read end of that memory BIO. The
957 Connection can then read the bytes (for example, in response to
958 a call to recv).
959
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500960 renegotiate()
961 Renegotiate the SSL session. Call this if you wish to change
962 cipher suites or anything like that.
963
964 send(string)
965 Send the string data to the Connection.
966
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -0500967 bio_read(bufsize)
968 If the Connection was created with a memory BIO, this method can
969 be used to read bytes from the write end of that memory BIO.
970 Many Connection methods will add bytes which must be read in
971 this manner or the buffer will eventually fill up and the
972 Connection will be able to take no further actions.
973
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500974 sendall(string)
975 Send all of the string data to the Connection. This calls send
976 repeatedly until all data is sent. If an error occurs, it's
977 impossible to tell how much data has been sent.
978
979 set_accept_state()
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500980 Set the connection to work in server mode. The handshake will be
981 handled automatically by read/write.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500982
983 set_app_data(data)
984 Associate data with this Connection object. data can be
985 retrieved later using the get_app_data method.
986
987 set_connect_state()
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500988 Set the connection to work in client mode. The handshake will be
989 handled automatically by read/write.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500990
991 setblocking(flag)
992 Call the setblocking method of the underlying socket.
993
994 setsockopt(level, optname, value)
995 Call the setsockopt method of the underlying socket.
996
997 shutdown()
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500998 Send the shutdown message to the Connection. Returns true if the
999 shutdown message exchange is completed and false otherwise (in
1000 which case you call recv() or send() when the connection becomes
1001 readable/writeable.
1002
1003 get_shutdown()
1004 Get the shutdown state of the Connection. Returns a bitvector of
1005 either or both of SENT_SHUTDOWN and RECEIVED_SHUTDOWN.
1006
1007 set_shutdown(state)
1008 Set the shutdown state of the Connection. state is a bitvector
1009 of either or both of SENT_SHUTDOWN and RECEIVED_SHUTDOWN.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001010
1011 sock_shutdown(how)
1012 Call the shutdown method of the underlying socket.
1013
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -05001014 bio_shutdown()
1015 If the Connection was created with a memory BIO, this method can
1016 be used to indicate that ``end of file'' has been reached on the
1017 read end of that memory BIO.
1018
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001019 state_string()
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001020 Retrieve a verbose string detailing the state of the Connection.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001021
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -05001022 client_random()
1023 Retrieve the random value used with the client hello message.
1024
1025 server_random()
1026 Retrieve the random value used with the server hello message.
1027
1028 master_key()
1029 Retrieve the value of the master key for this session.
1030
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001031 want_read()
1032 Checks if more data has to be read from the transport layer to
1033 complete an operation.
1034
1035 want_write()
1036 Checks if there is data to write to the transport layer to
1037 complete an operation.
1038
1039
1040 4 Internals
1041
1042 We ran into three main problems developing this: Exceptions, callbacks
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001043 and accessing socket methods. This is what this chapter is about.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001044
1045
10464.1 Exceptions
1047
1048 We realized early that most of the exceptions would be raised by the
1049 I/O functions of OpenSSL, so it felt natural to mimic OpenSSL's error
1050 code system, translating them into Python exceptions. This naturally
1051 gives us the exceptions SSL.ZeroReturnError, SSL.WantReadError,
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001052 SSL.WantWriteError, SSL.WantX509LookupError and SSL.SysCallError.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001053
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001054 For more information about this, see section 3.3.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001055
1056
10574.2 Callbacks
1058
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001059 There are a number of problems with callbacks. First of all, OpenSSL is
1060 written as a C library, it's not meant to have Python callbacks, so a
1061 way around that is needed. Another problem is thread support. A lot of
1062 the OpenSSL I/O functions can block if the socket is in blocking mode,
1063 and then you want other Python threads to be able to do other things.
Jean-Paul Calderone657d3ec2008-09-21 18:59:46 -04001064 The real trouble is if you've released the global CPython interpreter
1065 lock to do a potentially blocking operation, and the operation calls a
1066 callback. Then we must take the GIL back, since calling Python APIs
1067 without holding it is not allowed.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001068
1069 There are two solutions to the first problem, both of which are
1070 necessary. The first solution to use is if the C callback allows
1071 ''userdata'' to be passed to it (an arbitrary pointer normally). This
1072 is great! We can set our Python function object as the real userdata
1073 and emulate userdata for the Python function in another way. The other
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001074 solution can be used if an object with an ''app_data'' system always is
1075 passed to the callback. For example, the SSL object in OpenSSL has
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001076 app_data functions and in e.g. the verification callbacks, you can
1077 retrieve the related SSL object. What we do is to set our wrapper
1078 Connection object as app_data for the SSL object, and we can easily
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001079 find the Python callback.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001080
Jean-Paul Calderone657d3ec2008-09-21 18:59:46 -04001081 The other problem is solved using thread local variables. Whenever the
1082 GIL is released before calling into an OpenSSL API, the PyThreadState
1083 pointer returned by PyEval_SaveState is stored in a global thread local
1084 variable (using Python's own TLS API, PyThread_set_key_value). When it
1085 is necessary to re-acquire the GIL, either after the OpenSSL API
1086 returns or in a C callback invoked by that OpenSSL API, the value of
1087 the thread local variable is retrieved (PyThread_get_key_value) and
1088 used to re-acquire the GIL. This allows Python threads to execute while
1089 OpenSSL APIs are running and allows use of any particular pyOpenSSL
1090 object from any Python thread, since there is no per-thread state
1091 associated with any of these objects and since OpenSSL is threadsafe
1092 (as long as properly initialized, as pyOpenSSL initializes it).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001093
1094
10954.3 Acessing Socket Methods
1096
1097 We quickly saw the benefit of wrapping socket methods in the
1098 SSL.Connection class, for an easy transition into using SSL. The
1099 problem here is that the socket module lacks a C API, and all the
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001100 methods are declared static. One approach would be to have OpenSSL as a
1101 submodule to the socket module, placing all the code in socketmodule.c,
1102 but this is obviously not a good solution, since you might not want to
1103 import tonnes of extra stuff you're not going to use when importing the
1104 socket module. The other approach is to somehow get a pointer to the
1105 method to be called, either the C function, or a callable Python
1106 object. This is not really a good solution either, since there's a lot
1107 of lookups involved.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001108
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001109 The way it works is that you have to supply a ``socket-like'' transport
1110 object to the SSL.Connection. The only requirement of this object is
1111 that it has a fileno() method that returns a file descriptor that's
1112 valid at the C level (i.e. you can use the system calls read and
1113 write). If you want to use the connect() or accept() methods of the
1114 SSL.Connection object, the transport object has to supply such methods
1115 too. Apart from them, any method lookups in the SSL.Connection object
1116 that fail are passed on to the underlying transport object.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001117
1118 Future changes might be to allow Python-level transport objects, that
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001119 instead of having fileno() methods, have read() and write() methods, so
1120 more advanced features of Python can be used. This would probably
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001121 entail some sort of OpenSSL ``BIOs'', but converting Python strings
1122 back and forth is expensive, so this shouldn't be used unless
1123 necessary. Other nice things would be to be able to pass in different
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001124 transport objects for reading and writing, but then the fileno() method
1125 of SSL.Connection becomes virtually useless. Also, should the method
1126 resolution be used on the read-transport or the write-transport?
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001127
1128 About this document ...
1129
1130 Python OpenSSL Manual
1131
1132 This document was generated using the LaTeX2HTML translator.
1133
1134 LaTeX2HTML is Copyright © 1993, 1994, 1995, 1996, 1997, Nikos Drakos,
1135 Computer Based Learning Unit, University of Leeds, and Copyright ©
1136 1997, 1998, Ross Moore, Mathematics Department, Macquarie University,
1137 Sydney.
1138
1139 The application of LaTeX2HTML to the Python documentation has been
1140 heavily tailored by Fred L. Drake, Jr. Original navigation icons were
1141 contributed by Christopher Petrilli.
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001142 __________________________________________________________________
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001143
1144 Footnotes
1145
1146 ... M2Crypto^1
Jean-Paul Calderonef1b839d2008-09-01 12:06:06 -04001147 See http://chandlerproject.org/Projects/MeTooCrypto
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001148
Jean-Paul Calderonef1b839d2008-09-01 12:06:06 -04001149 ... Daemon^2
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001150 See http://www.lothar.com/tech/crypto/
1151
Jean-Paul Calderonef1b839d2008-09-01 12:06:06 -04001152 ... socket^3
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001153 Actually, all that is required is an object that behaves like a
1154 socket, you could even use files, even though it'd be tricky to
1155 get the handshakes right!
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001156 __________________________________________________________________
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001157
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001158 Python OpenSSL Manual
1159 __________________________________________________________________
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001160
Jean-Paul Calderone215d51b2009-11-13 09:19:21 -05001161 Release 0.10.