Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 1 | Python OpenSSL Manual |
| 2 | __________________________________________________________________ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 3 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 4 | Python OpenSSL Manual |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 5 | |
| 6 | Martin Sjögren |
| 7 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 8 | martin@strakt.com |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 9 | |
| 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 | |
| 17 | Contents |
| 18 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 19 | * 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 Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 25 | + 3.2 rand -- An interface to the OpenSSL pseudo random number |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 26 | 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 Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 32 | |
| 33 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 34 | 1 Introduction |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 35 | |
| 36 | The reason this module exists at all is that the SSL support in the |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 37 | socket module in the Python 2.1 distribution (which is what we used, of |
| 38 | course I cannot speak for later versions) is severely limited. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 39 | |
| 40 | When asking about SSL on the comp.lang.python newsgroup (or on |
| 41 | python-list@python.org) people usually pointed you to the M2Crypto |
| 42 | package. The M2Crypto.SSL module does implement a lot of OpenSSL's |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 43 | functionality but unfortunately its error handling system does not seem |
| 44 | to be finished, especially for non-blocking I/O. I think that much of |
| 45 | the reason for this is that M2Crypto^1 is developed using SWIG^2. This |
| 46 | makes it awkward to create functions that e.g. can return both an |
| 47 | integer and NULL since (as far as I know) you basically write C |
| 48 | functions and SWIG makes wrapper functions that parses the Python |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 49 | argument list and calls your C function, and finally transforms your |
| 50 | return value to a Python object. |
| 51 | |
| 52 | |
| 53 | 2 Building and Installing |
| 54 | |
| 55 | These instructions can also be found in the file INSTALL. |
| 56 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 57 | I have tested this on Debian Linux systems (woody and sid), Solaris 2.6 |
| 58 | and 2.7. Others have successfully compiled it on Windows and NT. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 59 | |
| 60 | |
| 61 | 2.1 Building the Module on a Unix System |
| 62 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 63 | pyOpenSSL uses distutils, so there really shouldn't be any problems. To |
| 64 | build the library: |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 65 | |
| 66 | python setup.py build |
| 67 | |
| 68 | If your OpenSSL header files aren't in /usr/include, you may need to |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 69 | supply the -I flag to let the setup script know where to look. The same |
| 70 | goes for the libraries of course, use the -L flag. Note that build |
| 71 | won't accept these flags, so you have to run first build_ext and then |
| 72 | build! Example: |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 73 | |
| 74 | python setup.py build_ext -I/usr/local/ssl/include -L/usr/local/ssl/lib |
| 75 | python setup.py build |
| 76 | |
| 77 | Now you should have a directory called OpenSSL that contains e.g. |
| 78 | SSL.so and __init__.py somewhere in the build dicrectory, so just: |
| 79 | |
| 80 | python setup.py install |
| 81 | |
| 82 | If you, for some arcane reason, don't want the module to appear in the |
| 83 | site-packages directory, use the --prefix option. |
| 84 | |
| 85 | You can, of course, do |
| 86 | |
| 87 | python setup.py --help |
| 88 | |
| 89 | to find out more about how to use the script. |
| 90 | |
| 91 | |
| 92 | 2.2 Building the Module on a Windows System |
| 93 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 94 | Big thanks to Itamar Shtull-Trauring and Oleg Orlov for their help with |
| 95 | Windows build instructions. Same as for Unix systems, we have to |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 96 | separate the build_ext and the build. |
| 97 | |
| 98 | Building the library: |
| 99 | |
| 100 | setup.py build_ext -I ...\openssl\inc32 -L ...\openssl\out32dll |
| 101 | setup.py build |
| 102 | |
| 103 | Where ...\openssl is of course the location of your OpenSSL |
| 104 | installation. |
| 105 | |
| 106 | Installation is the same as for Unix systems: |
| 107 | |
| 108 | setup.py install |
| 109 | |
| 110 | And similarily, you can do |
| 111 | |
| 112 | setup.py --help |
| 113 | |
| 114 | to get more information. |
| 115 | |
| 116 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 117 | 3 OpenSSL -- Python interface to OpenSSL |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 118 | |
| 119 | This package provides a high-level interface to the functions in the |
| 120 | OpenSSL library. The following modules are defined: |
| 121 | |
| 122 | crypto |
| 123 | Generic cryptographic module. Note that if anything is |
| 124 | incomplete, this module is! |
| 125 | |
| 126 | rand |
| 127 | An interface to the OpenSSL pseudo random number generator. |
| 128 | |
| 129 | SSL |
| 130 | An interface to the SSL-specific parts of OpenSSL. |
| 131 | |
| 132 | |
| 133 | 3.1 crypto -- Generic cryptographic module |
| 134 | |
| 135 | X509Type |
| 136 | A Python type object representing the X509 object type. |
| 137 | |
| 138 | X509() |
| 139 | Factory function that creates an X509 object. |
| 140 | |
| 141 | X509NameType |
| 142 | A Python type object representing the X509Name object type. |
| 143 | |
| 144 | X509Name(x509name) |
| 145 | Factory function that creates a copy of x509name. |
| 146 | |
| 147 | X509ReqType |
| 148 | A Python type object representing the X509Req object type. |
| 149 | |
| 150 | X509Req() |
| 151 | Factory function that creates an X509Req object. |
| 152 | |
| 153 | X509StoreType |
| 154 | A Python type object representing the X509Store object type. |
| 155 | |
| 156 | PKeyType |
| 157 | A Python type object representing the PKey object type. |
| 158 | |
| 159 | PKey() |
| 160 | Factory function that creates a PKey object. |
| 161 | |
| 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 Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 169 | A Python type object representing the X509Extension object type. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 170 | |
| 171 | X509Extension(typename, critical, value) |
| 172 | Factory function that creates a X509Extension object. |
| 173 | |
| 174 | NetscapeSPKIType |
| 175 | A Python type object representing the NetscapeSPKI object type. |
| 176 | |
| 177 | NetscapeSPKI([enc]) |
| 178 | Factory function that creates a NetscapeSPKI object. If the enc |
| 179 | argument is present, it should be a base64-encoded string |
| 180 | representing a NetscapeSPKI object, as returned by the |
| 181 | b64_encode method. |
| 182 | |
| 183 | FILETYPE_PEM |
| 184 | |
| 185 | FILETYPE_ASN1 |
| 186 | File type constants. |
| 187 | |
| 188 | TYPE_RSA |
| 189 | |
| 190 | TYPE_DSA |
| 191 | Key type constants. |
| 192 | |
| 193 | exception Error |
| 194 | Generic exception used in the crypto module. |
| 195 | |
| 196 | dump_certificate(type, cert) |
| 197 | Dump the certificate cert into a buffer string encoded with the |
| 198 | type type. |
| 199 | |
| 200 | dump_certificate_request(type, req) |
| 201 | Dump the certificate request req into a buffer string encoded |
| 202 | with the type type. |
| 203 | |
| 204 | dump_privatekey(type, pkey[, cipher, passphrase]) |
| 205 | Dump the private key pkey into a buffer string encoded with the |
| 206 | type type, optionally (if type is FILETYPE_PEM) encrypting it |
| 207 | using cipher and passphrase. |
| 208 | |
| 209 | passphrase must be either a string or a callback for providing |
| 210 | the pass phrase. |
| 211 | |
| 212 | load_certificate(type, buffer) |
| 213 | Load a certificate (X509) from the string buffer encoded with |
| 214 | the type type. |
| 215 | |
| 216 | load_certificate_request(type, buffer) |
| 217 | Load a certificate request (X509Req) from the string buffer |
| 218 | encoded with the type type. |
| 219 | |
| 220 | load_privatekey(type, buffer[, passphrase]) |
| 221 | Load a private key (PKey) from the string buffer encoded with |
| 222 | the type type (must be one of FILETYPE_PEM and FILETYPE_ASN1). |
| 223 | |
| 224 | passphrase must be either a string or a callback for providing |
| 225 | the pass phrase. |
| 226 | |
| 227 | load_pkcs7_data(type, buffer) |
| 228 | Load pkcs7 data from the string buffer encoded with the type |
| 229 | type. |
| 230 | |
| 231 | load_pkcs12(buffer[, passphrase]) |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 232 | Load pkcs12 data from the string buffer. If the pkcs12 structure |
| 233 | is encrypted, a passphrase must be included. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 234 | |
| 235 | |
| 236 | 3.1.1 X509 objects |
| 237 | |
| 238 | X509 objects have the following methods: |
| 239 | |
| 240 | get_issuer() |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 241 | Return an X509Name object representing the issuer of the |
| 242 | certificate. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 243 | |
| 244 | get_pubkey() |
| 245 | Return a PKey object representing the public key of the |
| 246 | certificate. |
| 247 | |
| 248 | get_serial_number() |
| 249 | Return the certificate serial number. |
| 250 | |
| 251 | get_subject() |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 252 | Return an X509Name object representing the subject of the |
| 253 | certificate. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 254 | |
| 255 | get_version() |
| 256 | Return the certificate version. |
| 257 | |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 258 | get_notBefore() |
| 259 | Return a string giving the time before which the certificate is |
| 260 | not valid. The string is formatted as an ASN1 GENERALIZEDTIME: |
| 261 | |
| 262 | YYYYMMDDhhmmssZ |
| 263 | YYYYMMDDhhmmss+hhmm |
| 264 | YYYYMMDDhhmmss-hhmm |
| 265 | |
Jean-Paul Calderone | 24c9926 | 2008-03-09 21:48:06 -0400 | [diff] [blame^] | 266 | If no value exists for this field, None is returned. |
| 267 | |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 268 | get_notAfter() |
| 269 | Return a string giving the time after which the certificate is |
| 270 | not valid. The string is formatted as an ASN1 GENERALIZEDTIME: |
| 271 | |
| 272 | YYYYMMDDhhmmssZ |
| 273 | YYYYMMDDhhmmss+hhmm |
| 274 | YYYYMMDDhhmmss-hhmm |
| 275 | |
Jean-Paul Calderone | 24c9926 | 2008-03-09 21:48:06 -0400 | [diff] [blame^] | 276 | If no value exists for this field, None is returned. |
| 277 | |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 278 | set_notBefore(when) |
| 279 | Change the time before which the certificate is not valid. when |
| 280 | is a string formatted as an ASN1 GENERALIZEDTIME: |
| 281 | |
| 282 | YYYYMMDDhhmmssZ |
| 283 | YYYYMMDDhhmmss+hhmm |
| 284 | YYYYMMDDhhmmss-hhmm |
| 285 | |
| 286 | set_notAfter(when) |
| 287 | Change the time after which the certificate is not valid. when |
| 288 | is a string formatted as an ASN1 GENERALIZEDTIME: |
| 289 | |
| 290 | YYYYMMDDhhmmssZ |
| 291 | YYYYMMDDhhmmss+hhmm |
| 292 | YYYYMMDDhhmmss-hhmm |
| 293 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 294 | gmtime_adj_notBefore(time) |
| 295 | Adjust the timestamp (in GMT) when the certificate starts being |
| 296 | valid. |
| 297 | |
| 298 | gmtime_adj_notAfter(time) |
| 299 | Adjust the timestamp (in GMT) when the certificate stops being |
| 300 | valid. |
| 301 | |
| 302 | has_expired() |
| 303 | Checks the certificate's time stamp against current time. |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 304 | Returns true if the certificate has expired and false otherwise. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 305 | |
| 306 | set_issuer(issuer) |
| 307 | Set the issuer of the certificate to issuer. |
| 308 | |
| 309 | set_pubkey(pkey) |
| 310 | Set the public key of the certificate to pkey. |
| 311 | |
| 312 | set_serial_number(serialno) |
| 313 | Set the serial number of the certificate to serialno. |
| 314 | |
| 315 | set_subject(subject) |
| 316 | Set the subject of the certificate to subject. |
| 317 | |
| 318 | set_version(version) |
| 319 | Set the certificate version to version. |
| 320 | |
| 321 | sign(pkey, digest) |
| 322 | Sign the certificate, using the key pkey and the message digest |
| 323 | algorithm identified by the string digest. |
| 324 | |
| 325 | subject_name_hash() |
| 326 | Return the hash of the certificate subject. |
| 327 | |
| 328 | digest(digest_name) |
| 329 | Return a digest of the certificate, using the digest_name |
| 330 | method. |
| 331 | |
| 332 | add_extensions(extensions) |
| 333 | Add the extensions in the sequence extensions to the |
| 334 | certificate. |
| 335 | |
| 336 | |
| 337 | 3.1.2 X509Name objects |
| 338 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 339 | X509Name objects have the following members: |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 340 | |
| 341 | countryName |
| 342 | The country of the entity. C may be used as an alias for |
| 343 | countryName. |
| 344 | |
| 345 | stateOrProvinceName |
| 346 | The state or province of the entity. ST may be used as an alias |
| 347 | for stateOrProvinceName· |
| 348 | |
| 349 | localityName |
| 350 | The locality of the entity. L may be used as an alias for |
| 351 | localityName. |
| 352 | |
| 353 | organizationName |
| 354 | The organization name of the entity. O may be used as an alias |
| 355 | for organizationName. |
| 356 | |
| 357 | organizationalUnitName |
| 358 | The organizational unit of the entity. OU may be used as an |
| 359 | alias for organizationalUnitName. |
| 360 | |
| 361 | commonName |
| 362 | The common name of the entity. CN may be used as an alias for |
| 363 | commonName. |
| 364 | |
| 365 | emailAddress |
| 366 | The e-mail address of the entity. |
| 367 | |
| 368 | |
| 369 | 3.1.3 X509Req objects |
| 370 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 371 | X509Req objects have the following methods: |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 372 | |
| 373 | get_pubkey() |
| 374 | Return a PKey object representing the public key of the |
| 375 | certificate request. |
| 376 | |
| 377 | get_subject() |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 378 | Return an X509Name object representing the subject of the |
| 379 | certificate. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 380 | |
| 381 | set_pubkey(pkey) |
| 382 | Set the public key of the certificate request to pkey. |
| 383 | |
| 384 | sign(pkey, digest) |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 385 | Sign the certificate request, using the key pkey and the message |
| 386 | digest algorithm identified by the string digest. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 387 | |
| 388 | verify(pkey) |
| 389 | Verify a certificate request using the public key pkey. |
| 390 | |
| 391 | |
| 392 | 3.1.4 X509Store objects |
| 393 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 394 | The X509Store object has currently just one method: |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 395 | |
| 396 | add_cert(cert) |
| 397 | Add the certificate cert to the certificate store. |
| 398 | |
| 399 | |
| 400 | 3.1.5 PKey objects |
| 401 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 402 | The PKey object has the following methods: |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 403 | |
| 404 | bits() |
| 405 | Return the number of bits of the key. |
| 406 | |
| 407 | generate_key(type, bits) |
| 408 | Generate a public/private key pair of the type type (one of |
| 409 | TYPE_RSA and TYPE_DSA) with the size bits. |
| 410 | |
| 411 | type() |
| 412 | Return the type of the key. |
| 413 | |
| 414 | |
| 415 | 3.1.6 PKCS7 objects |
| 416 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 417 | PKCS7 objects have the following methods: |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 418 | |
| 419 | type_is_signed() |
| 420 | FIXME |
| 421 | |
| 422 | type_is_enveloped() |
| 423 | FIXME |
| 424 | |
| 425 | type_is_signedAndEnveloped() |
| 426 | FIXME |
| 427 | |
| 428 | type_is_data() |
| 429 | FIXME |
| 430 | |
| 431 | get_type_name() |
| 432 | Get the type name of the PKCS7. |
| 433 | |
| 434 | |
| 435 | 3.1.7 PKCS12 objects |
| 436 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 437 | PKCS12 objects have the following methods: |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 438 | |
| 439 | get_certificate() |
| 440 | Return certificate portion of the PKCS12 structure. |
| 441 | |
| 442 | get_privatekey() |
| 443 | Return private key portion of the PKCS12 structure |
| 444 | |
| 445 | get_ca_certificates() |
| 446 | Return CA certificates within the PKCS12 object as a tuple. |
| 447 | Returns None if no CA certificates are present. |
| 448 | |
| 449 | |
| 450 | 3.1.8 X509Extension objects |
| 451 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 452 | X509Extension objects currently only have one method: |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 453 | |
| 454 | get_critical() |
| 455 | Return the critical field of the extension object. |
| 456 | |
| 457 | |
| 458 | 3.1.9 NetscapeSPKI objects |
| 459 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 460 | NetscapeSPKI objects have the following methods: |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 461 | |
| 462 | b64_encode() |
| 463 | Return a base64-encoded string representation of the object. |
| 464 | |
| 465 | get_pubkey() |
| 466 | Return the public key of object. |
| 467 | |
| 468 | set_pubkey(key) |
| 469 | Set the public key of the object to key. |
| 470 | |
| 471 | sign(key, digest_name) |
| 472 | Sign the NetscapeSPKI object using the given key and |
| 473 | digest_name. |
| 474 | |
| 475 | verify(key) |
| 476 | Verify the NetscapeSPKI object using the given key. |
| 477 | |
| 478 | |
| 479 | 3.2 rand -- An interface to the OpenSSL pseudo random number generator |
| 480 | |
| 481 | This module handles the OpenSSL pseudo random number generator (PRNG) |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 482 | and declares the following: |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 483 | |
| 484 | add(string, entropy) |
| 485 | Mix bytes from string into the PRNG state. The entropy argument |
| 486 | is (the lower bound of) an estimate of how much randomness is |
| 487 | contained in string, measured in bytes. For more information, |
| 488 | see e.g. RFC 1750. |
| 489 | |
| 490 | egd(path[, bytes]) |
| 491 | Query the Entropy Gathering Daemon^3 on socket path for bytes |
| 492 | bytes of random data and and uses add to seed the PRNG. The |
| 493 | default value of bytes is 255. |
| 494 | |
| 495 | load_file(path[, bytes]) |
| 496 | Read bytes bytes (or all of it, if bytes is negative) of data |
| 497 | from the file path to seed the PRNG. The default value of bytes |
| 498 | is -1. |
| 499 | |
| 500 | screen() |
| 501 | Add the current contents of the screen to the PRNG state. |
| 502 | Availability: Windows. |
| 503 | |
| 504 | seed(string) |
| 505 | This is equivalent to calling add with entropy as the length of |
| 506 | the string. |
| 507 | |
| 508 | status() |
| 509 | Returns true if the PRNG has been seeded with enough data, and |
| 510 | false otherwise. |
| 511 | |
| 512 | write_file(path) |
| 513 | Write a number of random bytes (currently 1024) to the file |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 514 | path. This file can then be used with load_file to seed the PRNG |
| 515 | again. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 516 | |
| 517 | |
| 518 | 3.3 SSL -- An interface to the SSL-specific parts of OpenSSL |
| 519 | |
| 520 | This module handles things specific to SSL. There are two objects |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 521 | defined: Context, Connection. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 522 | |
| 523 | SSLv2_METHOD |
| 524 | |
| 525 | SSLv3_METHOD |
| 526 | |
| 527 | SSLv23_METHOD |
| 528 | |
| 529 | TLSv1_METHOD |
| 530 | These constants represent the different SSL methods to use when |
| 531 | creating a context object. |
| 532 | |
| 533 | VERIFY_NONE |
| 534 | |
| 535 | VERIFY_PEER |
| 536 | |
| 537 | VERIFY_FAIL_IF_NO_PEER_CERT |
| 538 | These constants represent the verification mode used by the |
| 539 | Context object's set_verify method. |
| 540 | |
| 541 | FILETYPE_PEM |
| 542 | |
| 543 | FILETYPE_ASN1 |
| 544 | File type constants used with the use_certificate_file and |
| 545 | use_privatekey_file methods of Context objects. |
| 546 | |
| 547 | OP_SINGLE_DH_USE |
| 548 | |
| 549 | OP_EPHEMERAL_RSA |
| 550 | |
| 551 | OP_NO_SSLv2 |
| 552 | |
| 553 | OP_NO_SSLv3 |
| 554 | |
| 555 | OP_NO_TLSv1 |
| 556 | Constants used with set_options of Context objects. |
| 557 | OP_SINGLE_DH_USE means to always create a new key when using |
| 558 | ephemeral Diffie-Hellman. OP_EPHEMERAL_RSA means to always use |
| 559 | ephemeral RSA keys when doing RSA operations. OP_NO_SSLv2, |
| 560 | OP_NO_SSLv3 and OP_NO_TLSv1 means to disable those specific |
| 561 | protocols. This is interesting if you're using e.g. |
| 562 | SSLv23_METHOD to get an SSLv2-compatible handshake, but don't |
| 563 | want to use SSLv2. |
| 564 | |
| 565 | ContextType |
| 566 | A Python type object representing the Context object type. |
| 567 | |
| 568 | Context(method) |
| 569 | Factory function that creates a new Context object given an SSL |
| 570 | method. The method should be SSLv2_METHOD, SSLv3_METHOD, |
| 571 | SSLv23_METHOD or TLSv1_METHOD. |
| 572 | |
| 573 | ConnectionType |
| 574 | A Python type object representing the Connection object type. |
| 575 | |
| 576 | Connection(context, socket) |
| 577 | Factory fucnction that creates a new Connection object given an |
| 578 | SSL context and a socket ^4 object. |
| 579 | |
| 580 | exception Error |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 581 | This exception is used as a base class for the other SSL-related |
| 582 | exceptions, but may also be raised directly. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 583 | |
| 584 | Whenever this exception is raised directly, it has a list of |
| 585 | error messages from the OpenSSL error queue, where each item is |
| 586 | a tuple (lib, function, reason). Here lib, function and reason |
| 587 | are all strings, describing where and what the problem is. See |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 588 | err(3) for more information. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 589 | |
| 590 | exception ZeroReturnError |
| 591 | This exception matches the error return code |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 592 | SSL_ERROR_ZERO_RETURN, and is raised when the SSL Connection has |
| 593 | been closed. In SSL 3.0 and TLS 1.0, this only occurs if a |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 594 | closure alert has occurred in the protocol, i.e. the connection |
| 595 | has been closed cleanly. Note that this does not necessarily |
| 596 | mean that the transport layer (e.g. a socket) has been closed. |
| 597 | |
| 598 | It may seem a little strange that this is an exception, but it |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 599 | does match an SSL_ERROR code, and is very convenient. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 600 | |
| 601 | exception WantReadError |
| 602 | The operation did not complete; the same I/O method should be |
| 603 | called again later, with the same arguments. Any I/O method can |
| 604 | lead to this since new handshakes can occur at any time. |
| 605 | |
| 606 | exception WantWriteError |
| 607 | See WantReadError. |
| 608 | |
| 609 | exception WantX509LookupError |
| 610 | The operation did not complete because an application callback |
| 611 | has asked to be called again. The I/O method should be called |
| 612 | again later, with the same arguments. Note: This won't occur in |
| 613 | this version, as there are no such callbacks in this version. |
| 614 | |
| 615 | exception SysCallError |
| 616 | The SysCallError occurs when there's an I/O error and OpenSSL's |
| 617 | error queue does not contain any information. This can mean two |
| 618 | things: An error in the transport protocol, or an end of file |
| 619 | that violates the protocol. The parameter to the exception is |
| 620 | always a pair (errnum, errstr). |
| 621 | |
| 622 | |
| 623 | 3.3.1 Context objects |
| 624 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 625 | Context objects have the following methods: |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 626 | |
| 627 | check_privatekey() |
| 628 | Check if the private key (loaded with use_privatekey[_file]) |
| 629 | matches the certificate (loaded with use_certificate[_file]). |
Jean-Paul Calderone | f05fbbe | 2008-03-06 21:52:35 -0500 | [diff] [blame] | 630 | Returns None if they match, raises Error otherwise. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 631 | |
| 632 | get_app_data() |
| 633 | Retrieve application data as set by set_app_data. |
| 634 | |
| 635 | get_cert_store() |
| 636 | Retrieve the certificate store (a X509Store object) that the |
| 637 | context uses. This can be used to add "trusted" certificates |
| 638 | without using the. load_verify_locations() method. |
| 639 | |
| 640 | get_timeout() |
| 641 | Retrieve session timeout, as set by set_timeout. The default is |
| 642 | 300 seconds. |
| 643 | |
| 644 | get_verify_depth() |
| 645 | Retrieve the Context object's verify depth, as set by |
| 646 | set_verify_depth. |
| 647 | |
| 648 | get_verify_mode() |
| 649 | Retrieve the Context object's verify mode, as set by |
| 650 | set_verify_mode. |
| 651 | |
| 652 | load_client_ca(pemfile) |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 653 | Read a file with PEM-formatted certificates that will be sent to |
| 654 | the client when requesting a client certificate. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 655 | |
| 656 | load_verify_locations(pemfile) |
| 657 | Specify where CA certificates for verification purposes are |
| 658 | located. These are trusted certificates. Note that the |
| 659 | certificates have to be in PEM format. |
| 660 | |
| 661 | load_tmp_dh(dhfile) |
| 662 | Load parameters for Ephemeral Diffie-Hellman from dhfile. |
| 663 | |
| 664 | set_app_data(data) |
| 665 | Associate data with this Context object. data can be retrieved |
| 666 | later using the get_app_data method. |
| 667 | |
| 668 | set_cipher_list(ciphers) |
| 669 | Set the list of ciphers to be used in this context. See the |
| 670 | OpenSSL manual for more information (e.g. ciphers(1)) |
| 671 | |
| 672 | set_info_callback(callback) |
| 673 | Set the information callback to callback. This function will be |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 674 | called from time to time during SSL handshakes. callback should |
| 675 | take three arguments: a Connection object and two integers. The |
| 676 | first integer specifies where in the SSL handshake the function |
| 677 | was called, and the other the return code from a (possibly |
| 678 | failed) internal function call. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 679 | |
| 680 | set_options(options) |
| 681 | Add SSL options. Options you have set before are not cleared! |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 682 | This method should be used with the OP_* constants. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 683 | |
| 684 | set_passwd_cb(callback[, userdata]) |
| 685 | Set the passphrase callback to callback. This function will be |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 686 | called when a private key with a passphrase is loaded. callback |
| 687 | should take a boolean argument repeat and an arbitrary argument |
| 688 | data and return the passphrase entered by the user. If repeat is |
| 689 | true then callback should ask for the passphrase twice and make |
| 690 | sure that the two entries are equal. The data argument is the |
| 691 | userdata variable passed to the set_passwd_cb method. If an |
| 692 | error occurs, callback should return a false value (e.g. an |
| 693 | empty string). |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 694 | |
| 695 | set_session_id(name) |
| 696 | Set the context name within which a session can be reused for |
| 697 | this Context object. This is needed when doing session |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 698 | resumption, because there is no way for a stored session to know |
| 699 | which Context object it is associated with. name may be any |
| 700 | binary data. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 701 | |
| 702 | set_timeout(timeout) |
| 703 | Set the timeout for newly created sessions for this Context |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 704 | object to timeout. timeout must be given in (whole) seconds. The |
| 705 | default value is 300 seconds. See the OpenSSL manual for more |
| 706 | information (e.g. SSL_CTX_set_timeout(3)). |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 707 | |
| 708 | set_verify(mode, callback) |
| 709 | Set the verification flags for this Context object to mode and |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 710 | specify that callback should be used for verification callbacks. |
| 711 | mode should be one of VERIFY_NONE and VERIFY_PEER. If |
| 712 | VERIFY_PEER is used, mode can be OR:ed with |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 713 | VERIFY_FAIL_IF_NO_PEER_CERT and VERIFY_CLIENT_ONCE to further |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 714 | control the behaviour. callback should take five arguments: A |
| 715 | Connection object, an X509 object, and three integer variables, |
| 716 | which are in turn potential error number, error depth and return |
| 717 | code. callback should return true if verification passes and |
| 718 | false otherwise. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 719 | |
| 720 | set_verify_depth(depth) |
| 721 | Set the maximum depth for the certificate chain verification |
| 722 | that shall be allowed for this Context object. |
| 723 | |
| 724 | use_certificate(cert) |
| 725 | Use the certificate cert which has to be a X509 object. |
| 726 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 727 | add_extra_chain_cert(cert) |
| 728 | Adds the certificate cert, which has to be a X509 object, to the |
| 729 | certificate chain presented together with the certificate. |
| 730 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 731 | use_certificate_chain_file(file) |
| 732 | Load a certificate chain from file which must be PEM encoded. |
| 733 | |
| 734 | use_privatekey(pkey) |
| 735 | Use the private key pkey which has to be a PKey object. |
| 736 | |
| 737 | use_certificate_file(file[, format]) |
| 738 | Load the first certificate found in file. The certificate must |
| 739 | be in the format specified by format, which is either |
| 740 | FILETYPE_PEM or FILETYPE_ASN1. The default is FILETYPE_PEM. |
| 741 | |
| 742 | use_privatekey_file(file[, format]) |
| 743 | Load the first private key found in file. The private key must |
| 744 | be in the format specified by format, which is either |
| 745 | FILETYPE_PEM or FILETYPE_ASN1. The default is FILETYPE_PEM. |
| 746 | |
| 747 | |
| 748 | 3.3.2 Connection objects |
| 749 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 750 | Connection objects have the following methods: |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 751 | |
| 752 | accept() |
| 753 | Call the accept method of the underlying socket and set up SSL |
| 754 | on the returned socket, using the Context object supplied to |
| 755 | this Connection object at creation. Returns a pair (conn, |
| 756 | address). where conn is the new Connection object created, and |
| 757 | address is as returned by the socket's accept. |
| 758 | |
| 759 | bind(address) |
| 760 | Call the bind method of the underlying socket. |
| 761 | |
| 762 | close() |
| 763 | Call the close method of the underlying socket. Note: If you |
| 764 | want correct SSL closure, you need to call the shutdown method |
| 765 | first. |
| 766 | |
| 767 | connect(address) |
| 768 | Call the connect method of the underlying socket and set up SSL |
| 769 | on the socket, using the Context object supplied to this |
| 770 | Connection object at creation. |
| 771 | |
| 772 | connect_ex(address) |
| 773 | Call the connect_ex method of the underlying socket and set up |
| 774 | SSL on the socket, using the Context object supplied to this |
| 775 | Connection object at creation. Note that if the connect_ex |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 776 | method of the socket doesn't return 0, SSL won't be initialized. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 777 | |
| 778 | do_handshake() |
| 779 | Perform an SSL handshake (usually called after renegotiate or |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 780 | one of set_accept_state or set_accept_state). This can raise the |
| 781 | same exceptions as send and recv. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 782 | |
| 783 | fileno() |
| 784 | Retrieve the file descriptor number for the underlying socket. |
| 785 | |
| 786 | listen(backlog) |
| 787 | Call the listen method of the underlying socket. |
| 788 | |
| 789 | get_app_data() |
| 790 | Retrieve application data as set by set_app_data. |
| 791 | |
| 792 | get_cipher_list() |
| 793 | Retrieve the list of ciphers used by the Connection object. |
| 794 | WARNING: This API has changed. It used to take an optional |
| 795 | parameter and just return a string, but not it returns the |
| 796 | entire list in one go. |
| 797 | |
| 798 | get_context() |
| 799 | Retrieve the Context object associated with this Connection. |
| 800 | |
| 801 | get_peer_certificate() |
| 802 | Retrieve the other side's certificate (if any) |
| 803 | |
| 804 | getpeername() |
| 805 | Call the getpeername method of the underlying socket. |
| 806 | |
| 807 | getsockname() |
| 808 | Call the getsockname method of the underlying socket. |
| 809 | |
| 810 | getsockopt(level, optname[, buflen]) |
| 811 | Call the getsockopt method of the underlying socket. |
| 812 | |
| 813 | pending() |
| 814 | Retrieve the number of bytes that can be safely read from the |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 815 | SSL buffer (not the underlying transport buffer). |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 816 | |
| 817 | recv(bufsize) |
| 818 | Receive data from the Connection. The return value is a string |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 819 | representing the data received. The maximum amount of data to be |
| 820 | received at once, is specified by bufsize. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 821 | |
| 822 | renegotiate() |
| 823 | Renegotiate the SSL session. Call this if you wish to change |
| 824 | cipher suites or anything like that. |
| 825 | |
| 826 | send(string) |
| 827 | Send the string data to the Connection. |
| 828 | |
| 829 | sendall(string) |
| 830 | Send all of the string data to the Connection. This calls send |
| 831 | repeatedly until all data is sent. If an error occurs, it's |
| 832 | impossible to tell how much data has been sent. |
| 833 | |
| 834 | set_accept_state() |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 835 | Set the connection to work in server mode. The handshake will be |
| 836 | handled automatically by read/write. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 837 | |
| 838 | set_app_data(data) |
| 839 | Associate data with this Connection object. data can be |
| 840 | retrieved later using the get_app_data method. |
| 841 | |
| 842 | set_connect_state() |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 843 | Set the connection to work in client mode. The handshake will be |
| 844 | handled automatically by read/write. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 845 | |
| 846 | setblocking(flag) |
| 847 | Call the setblocking method of the underlying socket. |
| 848 | |
| 849 | setsockopt(level, optname, value) |
| 850 | Call the setsockopt method of the underlying socket. |
| 851 | |
| 852 | shutdown() |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 853 | Send the shutdown message to the Connection. Returns true if the |
| 854 | shutdown message exchange is completed and false otherwise (in |
| 855 | which case you call recv() or send() when the connection becomes |
| 856 | readable/writeable. |
| 857 | |
| 858 | get_shutdown() |
| 859 | Get the shutdown state of the Connection. Returns a bitvector of |
| 860 | either or both of SENT_SHUTDOWN and RECEIVED_SHUTDOWN. |
| 861 | |
| 862 | set_shutdown(state) |
| 863 | Set the shutdown state of the Connection. state is a bitvector |
| 864 | of either or both of SENT_SHUTDOWN and RECEIVED_SHUTDOWN. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 865 | |
| 866 | sock_shutdown(how) |
| 867 | Call the shutdown method of the underlying socket. |
| 868 | |
| 869 | state_string() |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 870 | Retrieve a verbose string detailing the state of the Connection. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 871 | |
| 872 | want_read() |
| 873 | Checks if more data has to be read from the transport layer to |
| 874 | complete an operation. |
| 875 | |
| 876 | want_write() |
| 877 | Checks if there is data to write to the transport layer to |
| 878 | complete an operation. |
| 879 | |
| 880 | |
| 881 | 4 Internals |
| 882 | |
| 883 | We ran into three main problems developing this: Exceptions, callbacks |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 884 | and accessing socket methods. This is what this chapter is about. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 885 | |
| 886 | |
| 887 | 4.1 Exceptions |
| 888 | |
| 889 | We realized early that most of the exceptions would be raised by the |
| 890 | I/O functions of OpenSSL, so it felt natural to mimic OpenSSL's error |
| 891 | code system, translating them into Python exceptions. This naturally |
| 892 | gives us the exceptions SSL.ZeroReturnError, SSL.WantReadError, |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 893 | SSL.WantWriteError, SSL.WantX509LookupError and SSL.SysCallError. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 894 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 895 | For more information about this, see section 3.3. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 896 | |
| 897 | |
| 898 | 4.2 Callbacks |
| 899 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 900 | There are a number of problems with callbacks. First of all, OpenSSL is |
| 901 | written as a C library, it's not meant to have Python callbacks, so a |
| 902 | way around that is needed. Another problem is thread support. A lot of |
| 903 | the OpenSSL I/O functions can block if the socket is in blocking mode, |
| 904 | and then you want other Python threads to be able to do other things. |
| 905 | The real trouble is if you've released the thread lock to do a |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 906 | potentially blocking operation, and the operation calls a callback. |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 907 | Then we must take the thread lock back^5. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 908 | |
| 909 | There are two solutions to the first problem, both of which are |
| 910 | necessary. The first solution to use is if the C callback allows |
| 911 | ''userdata'' to be passed to it (an arbitrary pointer normally). This |
| 912 | is great! We can set our Python function object as the real userdata |
| 913 | and emulate userdata for the Python function in another way. The other |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 914 | solution can be used if an object with an ''app_data'' system always is |
| 915 | passed to the callback. For example, the SSL object in OpenSSL has |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 916 | app_data functions and in e.g. the verification callbacks, you can |
| 917 | retrieve the related SSL object. What we do is to set our wrapper |
| 918 | Connection object as app_data for the SSL object, and we can easily |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 919 | find the Python callback. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 920 | |
| 921 | The other problem is also partially solved by app_data. Since we're |
| 922 | associating our wrapper objects with the ''real'' objects, we can |
| 923 | easily access data from the Connection object. The solution then is to |
| 924 | simply include a PyThreadState variable in the Connection declaration, |
| 925 | and write macros similar to Py_BEGIN_ALLOW_THREADS and |
| 926 | Py_END_ALLOW_THREADS that allows specifying of the PyThreadState |
| 927 | variable to use. Now we can simply ''begin allow threads'' before a |
| 928 | potentially blocking operation, and ''end allow threads'' before |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 929 | calling a callback. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 930 | |
| 931 | |
| 932 | 4.3 Acessing Socket Methods |
| 933 | |
| 934 | We quickly saw the benefit of wrapping socket methods in the |
| 935 | SSL.Connection class, for an easy transition into using SSL. The |
| 936 | problem here is that the socket module lacks a C API, and all the |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 937 | methods are declared static. One approach would be to have OpenSSL as a |
| 938 | submodule to the socket module, placing all the code in socketmodule.c, |
| 939 | but this is obviously not a good solution, since you might not want to |
| 940 | import tonnes of extra stuff you're not going to use when importing the |
| 941 | socket module. The other approach is to somehow get a pointer to the |
| 942 | method to be called, either the C function, or a callable Python |
| 943 | object. This is not really a good solution either, since there's a lot |
| 944 | of lookups involved. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 945 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 946 | The way it works is that you have to supply a ``socket-like'' transport |
| 947 | object to the SSL.Connection. The only requirement of this object is |
| 948 | that it has a fileno() method that returns a file descriptor that's |
| 949 | valid at the C level (i.e. you can use the system calls read and |
| 950 | write). If you want to use the connect() or accept() methods of the |
| 951 | SSL.Connection object, the transport object has to supply such methods |
| 952 | too. Apart from them, any method lookups in the SSL.Connection object |
| 953 | that fail are passed on to the underlying transport object. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 954 | |
| 955 | Future changes might be to allow Python-level transport objects, that |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 956 | instead of having fileno() methods, have read() and write() methods, so |
| 957 | more advanced features of Python can be used. This would probably |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 958 | entail some sort of OpenSSL ``BIOs'', but converting Python strings |
| 959 | back and forth is expensive, so this shouldn't be used unless |
| 960 | necessary. Other nice things would be to be able to pass in different |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 961 | transport objects for reading and writing, but then the fileno() method |
| 962 | of SSL.Connection becomes virtually useless. Also, should the method |
| 963 | resolution be used on the read-transport or the write-transport? |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 964 | |
| 965 | About this document ... |
| 966 | |
| 967 | Python OpenSSL Manual |
| 968 | |
| 969 | This document was generated using the LaTeX2HTML translator. |
| 970 | |
| 971 | LaTeX2HTML is Copyright © 1993, 1994, 1995, 1996, 1997, Nikos Drakos, |
| 972 | Computer Based Learning Unit, University of Leeds, and Copyright © |
| 973 | 1997, 1998, Ross Moore, Mathematics Department, Macquarie University, |
| 974 | Sydney. |
| 975 | |
| 976 | The application of LaTeX2HTML to the Python documentation has been |
| 977 | heavily tailored by Fred L. Drake, Jr. Original navigation icons were |
| 978 | contributed by Christopher Petrilli. |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 979 | __________________________________________________________________ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 980 | |
| 981 | Footnotes |
| 982 | |
| 983 | ... M2Crypto^1 |
| 984 | See http://www.post1.com/home/ngps/m2/ |
| 985 | |
| 986 | ... SWIG^2 |
| 987 | See http://swig.sourceforge.net/ |
| 988 | |
| 989 | ... Daemon^3 |
| 990 | See http://www.lothar.com/tech/crypto/ |
| 991 | |
| 992 | ... socket^4 |
| 993 | Actually, all that is required is an object that behaves like a |
| 994 | socket, you could even use files, even though it'd be tricky to |
| 995 | get the handshakes right! |
| 996 | |
| 997 | ... back^5 |
| 998 | I'm not sure why this is necessary, but otherwise I get a |
| 999 | segmentation violation on PyEval_CallObject |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 1000 | __________________________________________________________________ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1001 | |
Jean-Paul Calderone | b6f57be | 2008-03-06 21:22:16 -0500 | [diff] [blame] | 1002 | Python OpenSSL Manual |
| 1003 | __________________________________________________________________ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1004 | |
| 1005 | Release 0.6. |