blob: 1a05e64f57ddd8bd841a398df0638d6aa4e4d3f3 [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001\documentclass{howto}
2
3\title{Python OpenSSL Manual}
4
Jean-Paul Calderone71ff3682009-04-25 08:30:11 -04005\release{0.9}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05006
Jean-Paul Calderone0ebe45a2009-04-25 10:40:31 -04007\author{Jean-Paul Calderone}
8\authoraddress{\email{exarkun@twistedmatrix.com}}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05009
10\usepackage[english]{babel}
11\usepackage[T1]{fontenc}
12
13\begin{document}
14
15\maketitle
16
17\begin{abstract}
18\noindent
19This module is a rather thin wrapper around (a subset of) the OpenSSL library.
20With thin wrapper I mean that a lot of the object methods do nothing more than
21calling a corresponding function in the OpenSSL library.
22\end{abstract}
23
24\tableofcontents
25
26
27\section{Introduction \label{intro}}
28
Jean-Paul Calderone9450d5b2008-09-01 12:04:20 -040029The reason pyOpenSSL was created is that the SSL support in the socket module
30in Python 2.1 (the contemporary version of Python when the pyOpenSSL project
31was begun) was severely limited. Other OpenSSL wrappers for Python at the time
32were also limited, though in different ways. Unfortunately, Python's standard
33library SSL support has remained weak, although other packages (such as
34M2Crypto\footnote{See \url{http://chandlerproject.org/Projects/MeTooCrypto}})
35have made great advances and now equal or exceed pyOpenSSL's functionality.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050036
Jean-Paul Calderone9450d5b2008-09-01 12:04:20 -040037The reason pyOpenSSL continues to be maintained is that there is a significant
38user community around it, as well as a large amount of software which depends
39on it. It is a great benefit to many people for pyOpenSSL to continue to exist
40and advance.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050041
42\section{Building and Installing \label{building}}
43
44These instructions can also be found in the file \verb|INSTALL|.
45
46I have tested this on Debian Linux systems (woody and sid), Solaris 2.6 and
472.7. Others have successfully compiled it on Windows and NT.
48
49\subsection{Building the Module on a Unix System \label{building-unix}}
50
51pyOpenSSL uses distutils, so there really shouldn't be any problems. To build
52the library:
53\begin{verbatim}
54python setup.py build
55\end{verbatim}
56
57If your OpenSSL header files aren't in \verb|/usr/include|, you may need to
58supply the \verb|-I| flag to let the setup script know where to look. The same
59goes for the libraries of course, use the \verb|-L| flag. Note that
60\verb|build| won't accept these flags, so you have to run first
61\verb|build_ext| and then \verb|build|! Example:
62\begin{verbatim}
63python setup.py build_ext -I/usr/local/ssl/include -L/usr/local/ssl/lib
64python setup.py build
65\end{verbatim}
66
67Now you should have a directory called \verb|OpenSSL| that contains e.g.
68\verb|SSL.so| and \verb|__init__.py| somewhere in the build dicrectory,
69so just:
70\begin{verbatim}
71python setup.py install
72\end{verbatim}
73
74If you, for some arcane reason, don't want the module to appear in the
75\verb|site-packages| directory, use the \verb|--prefix| option.
76
77You can, of course, do
78\begin{verbatim}
79python setup.py --help
80\end{verbatim}
81
82to find out more about how to use the script.
83
84\subsection{Building the Module on a Windows System \label{building-windows}}
85
86Big thanks to Itamar Shtull-Trauring and Oleg Orlov for their help with
87Windows build instructions. Same as for Unix systems, we have to separate
88the \verb|build_ext| and the \verb|build|.
89
90Building the library:
91
92\begin{verbatim}
93setup.py build_ext -I ...\openssl\inc32 -L ...\openssl\out32dll
94setup.py build
95\end{verbatim}
96
97Where \verb|...\openssl| is of course the location of your OpenSSL installation.
98
99Installation is the same as for Unix systems:
100\begin{verbatim}
101setup.py install
102\end{verbatim}
103
104And similarily, you can do
105\begin{verbatim}
106setup.py --help
107\end{verbatim}
108
109to get more information.
110
111
112\section{\module{OpenSSL} --- Python interface to OpenSSL \label{openssl}}
113
114\declaremodule{extension}{OpenSSL}
115\modulesynopsis{Python interface to OpenSSL}
116
117This package provides a high-level interface to the functions in the
118OpenSSL library. The following modules are defined:
119
120\begin{datadesc}{crypto}
121Generic cryptographic module. Note that if anything is incomplete, this module is!
122\end{datadesc}
123
124\begin{datadesc}{rand}
125An interface to the OpenSSL pseudo random number generator.
126\end{datadesc}
127
128\begin{datadesc}{SSL}
129An interface to the SSL-specific parts of OpenSSL.
130\end{datadesc}
131
132
133% % % crypto moduleOpenSSL
134
135\subsection{\module{crypto} --- Generic cryptographic module \label{openssl-crypto}}
136
137\declaremodule{extension}{crypto}
138\modulesynopsis{Generic cryptographic module}
139
140\begin{datadesc}{X509Type}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400141See \class{X509}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500142\end{datadesc}
143
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400144\begin{classdesc}{X509}{}
145A class representing X.509 certificates.
146\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500147
148\begin{datadesc}{X509NameType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400149See \class{X509Name}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500150\end{datadesc}
151
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400152\begin{classdesc}{X509Name}{x509name}
153A class representing X.509 Distinguished Names.
154
155This constructor creates a copy of \var{x509name} which should be an
156instance of \class{X509Name}.
157\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500158
159\begin{datadesc}{X509ReqType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400160See \class{X509Req}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500161\end{datadesc}
162
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400163\begin{classdesc}{X509Req}{}
164A class representing X.509 certificate requests.
165\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500166
167\begin{datadesc}{X509StoreType}
168A Python type object representing the X509Store object type.
169\end{datadesc}
170
171\begin{datadesc}{PKeyType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400172See \class{PKey}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500173\end{datadesc}
174
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400175\begin{classdesc}{PKey}{}
176A class representing DSA or RSA keys.
177\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500178
179\begin{datadesc}{PKCS7Type}
180A Python type object representing the PKCS7 object type.
181\end{datadesc}
182
183\begin{datadesc}{PKCS12Type}
184A Python type object representing the PKCS12 object type.
185\end{datadesc}
186
187\begin{datadesc}{X509ExtensionType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400188See \class{X509Extension}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500189\end{datadesc}
190
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400191\begin{classdesc}{X509Extension}{typename, critical, value}
192A class representing an X.509 v3 certificate extensions.
193\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500194
195\begin{datadesc}{NetscapeSPKIType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400196See \class{NetscapeSPKI}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500197\end{datadesc}
198
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400199\begin{classdesc}{NetscapeSPKI}{\optional{enc}}
200A class representing Netscape SPKI objects.
201
202If the \var{enc} argument is present, it should be a base64-encoded string
203representing a NetscapeSPKI object, as returned by the \method{b64_encode}
204method.
205\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500206
207\begin{datadesc}{FILETYPE_PEM}
208\dataline{FILETYPE_ASN1}
209File type constants.
210\end{datadesc}
211
212\begin{datadesc}{TYPE_RSA}
213\dataline{TYPE_DSA}
214Key type constants.
215\end{datadesc}
216
217\begin{excdesc}{Error}
218Generic exception used in the \module{crypto} module.
219\end{excdesc}
220
221\begin{funcdesc}{dump_certificate}{type, cert}
222Dump the certificate \var{cert} into a buffer string encoded with the type
223\var{type}.
224\end{funcdesc}
225
226\begin{funcdesc}{dump_certificate_request}{type, req}
227Dump the certificate request \var{req} into a buffer string encoded with the
228type \var{type}.
229\end{funcdesc}
230
231\begin{funcdesc}{dump_privatekey}{type, pkey\optional{, cipher, passphrase}}
232Dump the private key \var{pkey} into a buffer string encoded with the type
233\var{type}, optionally (if \var{type} is \constant{FILETYPE_PEM}) encrypting it
234using \var{cipher} and \var{passphrase}.
235
236\var{passphrase} must be either a string or a callback for providing the
237pass phrase.
238\end{funcdesc}
239
240\begin{funcdesc}{load_certificate}{type, buffer}
241Load a certificate (X509) from the string \var{buffer} encoded with the
242type \var{type}.
243\end{funcdesc}
244
245\begin{funcdesc}{load_certificate_request}{type, buffer}
246Load a certificate request (X509Req) from the string \var{buffer} encoded with
247the type \var{type}.
248\end{funcdesc}
249
250\begin{funcdesc}{load_privatekey}{type, buffer\optional{, passphrase}}
251Load a private key (PKey) from the string \var{buffer} encoded with
252the type \var{type} (must be one of \constant{FILETYPE_PEM} and
253\constant{FILETYPE_ASN1}).
254
255\var{passphrase} must be either a string or a callback for providing the
256pass phrase.
257\end{funcdesc}
258
259\begin{funcdesc}{load_pkcs7_data}{type, buffer}
260Load pkcs7 data from the string \var{buffer} encoded with the type \var{type}.
261\end{funcdesc}
262
263\begin{funcdesc}{load_pkcs12}{buffer\optional{, passphrase}}
264Load pkcs12 data from the string \var{buffer}. If the pkcs12 structure is
265encrypted, a \var{passphrase} must be included.
266\end{funcdesc}
267
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500268\subsubsection{X509 objects \label{openssl-x509}}
269
270X509 objects have the following methods:
271
272\begin{methoddesc}[X509]{get_issuer}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500273Return an X509Name object representing the issuer of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500274\end{methoddesc}
275
276\begin{methoddesc}[X509]{get_pubkey}{}
277Return a PKey object representing the public key of the certificate.
278\end{methoddesc}
279
280\begin{methoddesc}[X509]{get_serial_number}{}
281Return the certificate serial number.
282\end{methoddesc}
283
284\begin{methoddesc}[X509]{get_subject}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500285Return an X509Name object representing the subject of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500286\end{methoddesc}
287
288\begin{methoddesc}[X509]{get_version}{}
289Return the certificate version.
290\end{methoddesc}
291
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400292\begin{methoddesc}[X509]{get_notBefore}{}
293Return a string giving the time before which the certificate is not valid. The
294string is formatted as an ASN1 GENERALIZEDTIME:
295\begin{verbatim}
296 YYYYMMDDhhmmssZ
297 YYYYMMDDhhmmss+hhmm
298 YYYYMMDDhhmmss-hhmm
299\end{verbatim}
Jean-Paul Calderonee0615b52008-03-09 21:44:46 -0400300If no value exists for this field, \code{None} is returned.
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400301\end{methoddesc}
302
303\begin{methoddesc}[X509]{get_notAfter}{}
304Return a string giving the time after which the certificate is not valid. The
305string is formatted as an ASN1 GENERALIZEDTIME:
306\begin{verbatim}
307 YYYYMMDDhhmmssZ
308 YYYYMMDDhhmmss+hhmm
309 YYYYMMDDhhmmss-hhmm
310\end{verbatim}
Jean-Paul Calderonee0615b52008-03-09 21:44:46 -0400311If no value exists for this field, \code{None} is returned.
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400312\end{methoddesc}
313
314\begin{methoddesc}[X509]{set_notBefore}{when}
315Change the time before which the certificate is not valid. \var{when} is a
316string formatted as an ASN1 GENERALIZEDTIME:
317\begin{verbatim}
318 YYYYMMDDhhmmssZ
319 YYYYMMDDhhmmss+hhmm
320 YYYYMMDDhhmmss-hhmm
321\end{verbatim}
322\end{methoddesc}
323
324\begin{methoddesc}[X509]{set_notAfter}{when}
325Change the time after which the certificate is not valid. \var{when} is a
326string formatted as an ASN1 GENERALIZEDTIME:
327\begin{verbatim}
328 YYYYMMDDhhmmssZ
329 YYYYMMDDhhmmss+hhmm
330 YYYYMMDDhhmmss-hhmm
331\end{verbatim}
332\end{methoddesc}
333
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500334\begin{methoddesc}[X509]{gmtime_adj_notBefore}{time}
335Adjust the timestamp (in GMT) when the certificate starts being valid.
336\end{methoddesc}
337
338\begin{methoddesc}[X509]{gmtime_adj_notAfter}{time}
339Adjust the timestamp (in GMT) when the certificate stops being valid.
340\end{methoddesc}
341
342\begin{methoddesc}[X509]{has_expired}{}
343Checks the certificate's time stamp against current time. Returns true if the
344certificate has expired and false otherwise.
345\end{methoddesc}
346
347\begin{methoddesc}[X509]{set_issuer}{issuer}
348Set the issuer of the certificate to \var{issuer}.
349\end{methoddesc}
350
351\begin{methoddesc}[X509]{set_pubkey}{pkey}
352Set the public key of the certificate to \var{pkey}.
353\end{methoddesc}
354
355\begin{methoddesc}[X509]{set_serial_number}{serialno}
356Set the serial number of the certificate to \var{serialno}.
357\end{methoddesc}
358
359\begin{methoddesc}[X509]{set_subject}{subject}
360Set the subject of the certificate to \var{subject}.
361\end{methoddesc}
362
363\begin{methoddesc}[X509]{set_version}{version}
364Set the certificate version to \var{version}.
365\end{methoddesc}
366
367\begin{methoddesc}[X509]{sign}{pkey, digest}
368Sign the certificate, using the key \var{pkey} and the message digest algorithm
369identified by the string \var{digest}.
370\end{methoddesc}
371
372\begin{methoddesc}[X509]{subject_name_hash}{}
373Return the hash of the certificate subject.
374\end{methoddesc}
375
376\begin{methoddesc}[X509]{digest}{digest_name}
377Return a digest of the certificate, using the \var{digest_name} method.
Jean-Paul Calderoneb6f0d602008-12-28 21:20:01 -0500378\var{digest_name} must be a string describing a digest algorithm supported
379by OpenSSL (by EVP_get_digestbyname, specifically). For example,
380\constant{"md5"} or \constant{"sha1"}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500381\end{methoddesc}
382
383\begin{methoddesc}[X509]{add_extensions}{extensions}
384Add the extensions in the sequence \var{extensions} to the certificate.
385\end{methoddesc}
386
387\subsubsection{X509Name objects \label{openssl-x509name}}
388
Jean-Paul Calderone2dd8ff52008-03-24 17:43:58 -0400389X509Name objects have the following methods:
390
391\begin{methoddesc}[X509Name]{hash}{}
392Return an integer giving the first four bytes of the MD5 digest of the DER
393representation of the name.
394\end{methoddesc}
395
Jean-Paul Calderonea6edbf82008-03-25 15:19:11 -0400396\begin{methoddesc}[X509Name]{der}{}
397Return a string giving the DER representation of the name.
398\end{methoddesc}
399
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400400\begin{methoddesc}[X509Name]{get_components}{}
401Return a list of two-tuples of strings giving the components of the name.
402\end{methoddesc}
403
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500404X509Name objects have the following members:
405
406\begin{memberdesc}[X509Name]{countryName}
407The country of the entity. \code{C} may be used as an alias for
408\code{countryName}.
409\end{memberdesc}
410
411\begin{memberdesc}[X509Name]{stateOrProvinceName}
412The state or province of the entity. \code{ST} may be used as an alias for
413\code{stateOrProvinceName}·
414\end{memberdesc}
415
416\begin{memberdesc}[X509Name]{localityName}
417The locality of the entity. \code{L} may be used as an alias for
418\code{localityName}.
419\end{memberdesc}
420
421\begin{memberdesc}[X509Name]{organizationName}
422The organization name of the entity. \code{O} may be used as an alias for
423\code{organizationName}.
424\end{memberdesc}
425
426\begin{memberdesc}[X509Name]{organizationalUnitName}
427The organizational unit of the entity. \code{OU} may be used as an alias for
428\code{organizationalUnitName}.
429\end{memberdesc}
430
431\begin{memberdesc}[X509Name]{commonName}
432The common name of the entity. \code{CN} may be used as an alias for
433\code{commonName}.
434\end{memberdesc}
435
436\begin{memberdesc}[X509Name]{emailAddress}
437The e-mail address of the entity.
438\end{memberdesc}
439
440\subsubsection{X509Req objects \label{openssl-x509req}}
441
442X509Req objects have the following methods:
443
444\begin{methoddesc}[X509Req]{get_pubkey}{}
445Return a PKey object representing the public key of the certificate request.
446\end{methoddesc}
447
448\begin{methoddesc}[X509Req]{get_subject}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500449Return an X509Name object representing the subject of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500450\end{methoddesc}
451
452\begin{methoddesc}[X509Req]{set_pubkey}{pkey}
453Set the public key of the certificate request to \var{pkey}.
454\end{methoddesc}
455
456\begin{methoddesc}[X509Req]{sign}{pkey, digest}
457Sign the certificate request, using the key \var{pkey} and the message digest
458algorithm identified by the string \var{digest}.
459\end{methoddesc}
460
461\begin{methoddesc}[X509Req]{verify}{pkey}
462Verify a certificate request using the public key \var{pkey}.
463\end{methoddesc}
464
Jean-Paul Calderone8dd19b82008-12-28 20:41:16 -0500465\begin{methoddesc}[X509Req]{set_version}{version}
466Set the version (RFC 2459, 4.1.2.1) of the certificate request to
467\var{version}.
468\end{methoddesc}
469
470\begin{methoddesc}[X509Req]{get_version}{}
471Get the version (RFC 2459, 4.1.2.1) of the certificate request.
472\end{methoddesc}
473
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500474\subsubsection{X509Store objects \label{openssl-x509store}}
475
476The X509Store object has currently just one method:
477
478\begin{methoddesc}[X509Store]{add_cert}{cert}
479Add the certificate \var{cert} to the certificate store.
480\end{methoddesc}
481
482\subsubsection{PKey objects \label{openssl-pkey}}
483
484The PKey object has the following methods:
485
486\begin{methoddesc}[PKey]{bits}{}
487Return the number of bits of the key.
488\end{methoddesc}
489
490\begin{methoddesc}[PKey]{generate_key}{type, bits}
491Generate a public/private key pair of the type \var{type} (one of
492\constant{TYPE_RSA} and \constant{TYPE_DSA}) with the size \var{bits}.
493\end{methoddesc}
494
495\begin{methoddesc}[PKey]{type}{}
496Return the type of the key.
497\end{methoddesc}
498
499\subsubsection{PKCS7 objects \label{openssl-pkcs7}}
500
501PKCS7 objects have the following methods:
502
503\begin{methoddesc}[PKCS7]{type_is_signed}{}
504FIXME
505\end{methoddesc}
506
507\begin{methoddesc}[PKCS7]{type_is_enveloped}{}
508FIXME
509\end{methoddesc}
510
511\begin{methoddesc}[PKCS7]{type_is_signedAndEnveloped}{}
512FIXME
513\end{methoddesc}
514
515\begin{methoddesc}[PKCS7]{type_is_data}{}
516FIXME
517\end{methoddesc}
518
519\begin{methoddesc}[PKCS7]{get_type_name}{}
520Get the type name of the PKCS7.
521\end{methoddesc}
522
523\subsubsection{PKCS12 objects \label{openssl-pkcs12}}
524
525PKCS12 objects have the following methods:
526
527\begin{methoddesc}[PKCS12]{get_certificate}{}
528Return certificate portion of the PKCS12 structure.
529\end{methoddesc}
530
531\begin{methoddesc}[PKCS12]{get_privatekey}{}
532Return private key portion of the PKCS12 structure
533\end{methoddesc}
534
535\begin{methoddesc}[PKCS12]{get_ca_certificates}{}
536Return CA certificates within the PKCS12 object as a tuple. Returns
537None if no CA certificates are present.
538\end{methoddesc}
539
540\subsubsection{X509Extension objects \label{openssl-509ext}}
541
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -0500542X509Extension objects have several methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500543
544\begin{methoddesc}[X509Extension]{get_critical}{}
545Return the critical field of the extension object.
546\end{methoddesc}
547
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -0500548\begin{methoddesc}[X509Extension]{get_short_name}{}
549Return the short type name of the extension object.
550\end{methoddesc}
551
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500552\subsubsection{NetscapeSPKI objects \label{openssl-netscape-spki}}
553
554NetscapeSPKI objects have the following methods:
555
556\begin{methoddesc}[NetscapeSPKI]{b64_encode}{}
557Return a base64-encoded string representation of the object.
558\end{methoddesc}
559
560\begin{methoddesc}[NetscapeSPKI]{get_pubkey}{}
561Return the public key of object.
562\end{methoddesc}
563
564\begin{methoddesc}[NetscapeSPKI]{set_pubkey}{key}
565Set the public key of the object to \var{key}.
566\end{methoddesc}
567
568\begin{methoddesc}[NetscapeSPKI]{sign}{key, digest_name}
Jean-Paul Calderoneb6f0d602008-12-28 21:20:01 -0500569Sign the NetscapeSPKI object using the given \var{key} and
570\var{digest_name}. \var{digest_name} must be a string describing a digest
571algorithm supported by OpenSSL (by EVP_get_digestbyname, specifically). For
572example, \constant{"md5"} or \constant{"sha1"}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500573\end{methoddesc}
574
575\begin{methoddesc}[NetscapeSPKI]{verify}{key}
576Verify the NetscapeSPKI object using the given \var{key}.
577\end{methoddesc}
578
579
580% % % rand module
581
582\subsection{\module{rand} --- An interface to the OpenSSL pseudo random number generator \label{openssl-rand}}
583
584\declaremodule{extension}{rand}
585\modulesynopsis{An interface to the OpenSSL pseudo random number generator}
586
587This module handles the OpenSSL pseudo random number generator (PRNG) and
588declares the following:
589
590\begin{funcdesc}{add}{string, entropy}
591Mix bytes from \var{string} into the PRNG state. The \var{entropy} argument is
592(the lower bound of) an estimate of how much randomness is contained in
593\var{string}, measured in bytes. For more information, see e.g. \rfc{1750}.
594\end{funcdesc}
595
Rick Dean4fd5a4e2009-07-08 12:06:10 -0500596\begin{funcdesc}{bytes}{num_bytes}
Jean-Paul Calderonea45e2b92009-07-08 13:29:58 -0400597Get some random bytes from the PRNG as a string.
598
599This is a wrapper for the C function \function{RAND_bytes}.
Rick Dean4fd5a4e2009-07-08 12:06:10 -0500600\end{funcdesc}
601
Rick Dean433dc642009-07-07 13:11:55 -0500602\begin{funcdesc}{cleanup}{}
Jean-Paul Calderone427c0b32009-07-07 15:43:27 -0400603Erase the memory used by the PRNG.
604
605This is a wrapper for the C function \function{RAND_cleanup}.
Rick Dean433dc642009-07-07 13:11:55 -0500606\end{funcdesc}
607
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500608\begin{funcdesc}{egd}{path\optional{, bytes}}
609Query the Entropy Gathering Daemon\footnote{See
610\url{http://www.lothar.com/tech/crypto/}} on socket \var{path} for \var{bytes}
611bytes of random data and and uses \function{add} to seed the PRNG. The default
612value of \var{bytes} is 255.
613\end{funcdesc}
614
615\begin{funcdesc}{load_file}{path\optional{, bytes}}
616Read \var{bytes} bytes (or all of it, if \var{bytes} is negative) of data from
617the file \var{path} to seed the PRNG. The default value of \var{bytes} is -1.
618\end{funcdesc}
619
620\begin{funcdesc}{screen}{}
621Add the current contents of the screen to the PRNG state.
622Availability: Windows.
623\end{funcdesc}
624
625\begin{funcdesc}{seed}{string}
626This is equivalent to calling \function{add} with \var{entropy} as the length
627of the string.
628\end{funcdesc}
629
630\begin{funcdesc}{status}{}
631Returns true if the PRNG has been seeded with enough data, and false otherwise.
632\end{funcdesc}
633
634\begin{funcdesc}{write_file}{path}
635Write a number of random bytes (currently 1024) to the file \var{path}. This
636file can then be used with \function{load_file} to seed the PRNG again.
637\end{funcdesc}
638
Rick Deanfc69c812009-07-08 11:03:47 -0500639\begin{excdesc}{Error}
640If the current RAND method supports any errors, this is raised when needed.
641The default method does not raise this when the entropy pool is depleted.
642
643Whenever this exception is raised directly, it has a list of error messages
644from the OpenSSL error queue, where each item is a tuple \code{(\var{lib},
645\var{function}, \var{reason})}. Here \var{lib}, \var{function} and \var{reason}
646are all strings, describing where and what the problem is. See \manpage{err}{3}
647for more information.
648\end{excdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500649
650
651% % % SSL module
652
653\subsection{\module{SSL} --- An interface to the SSL-specific parts of OpenSSL \label{openssl-ssl}}
654
655\declaremodule{extension}{SSL}
656\modulesynopsis{An interface to the SSL-specific parts of OpenSSL}
657
658This module handles things specific to SSL. There are two objects defined:
659Context, Connection.
660
661\begin{datadesc}{SSLv2_METHOD}
662\dataline{SSLv3_METHOD}
663\dataline{SSLv23_METHOD}
664\dataline{TLSv1_METHOD}
665These constants represent the different SSL methods to use when creating a
666context object.
667\end{datadesc}
668
669\begin{datadesc}{VERIFY_NONE}
670\dataline{VERIFY_PEER}
671\dataline{VERIFY_FAIL_IF_NO_PEER_CERT}
672These constants represent the verification mode used by the Context
673object's \method{set_verify} method.
674\end{datadesc}
675
676\begin{datadesc}{FILETYPE_PEM}
677\dataline{FILETYPE_ASN1}
678File type constants used with the \method{use_certificate_file} and
679\method{use_privatekey_file} methods of Context objects.
680\end{datadesc}
681
682\begin{datadesc}{OP_SINGLE_DH_USE}
683\dataline{OP_EPHEMERAL_RSA}
684\dataline{OP_NO_SSLv2}
685\dataline{OP_NO_SSLv3}
686\dataline{OP_NO_TLSv1}
687Constants used with \method{set_options} of Context objects.
688\constant{OP_SINGLE_DH_USE} means to always create a new key when using ephemeral
689Diffie-Hellman. \constant{OP_EPHEMERAL_RSA} means to always use ephemeral RSA keys
690when doing RSA operations. \constant{OP_NO_SSLv2}, \constant{OP_NO_SSLv3} and
691\constant{OP_NO_TLSv1} means to disable those specific protocols. This is
692interesting if you're using e.g. \constant{SSLv23_METHOD} to get an SSLv2-compatible
693handshake, but don't want to use SSLv2.
694\end{datadesc}
695
696\begin{datadesc}{ContextType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400697See \class{Context}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500698\end{datadesc}
699
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400700\begin{classdesc}{Context}{method}
701A class representing SSL contexts. Contexts define the parameters of one or
702more SSL connections.
703
704\var{method} should be \constant{SSLv2_METHOD}, \constant{SSLv3_METHOD},
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500705\constant{SSLv23_METHOD} or \constant{TLSv1_METHOD}.
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400706\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500707
708\begin{datadesc}{ConnectionType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400709See \class{Connection}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500710\end{datadesc}
711
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400712\begin{classdesc}{Connection}{context, socket}
713A class representing SSL connections.
714
715\var{context} should be an instance of \class{Context} and \var{socket}
716should be a socket \footnote{Actually, all that is required is an object
717that \emph{behaves} like a socket, you could even use files, even though
718it'd be tricky to get the handshakes right!} object. \var{socket} may be
719\var{None}; in this case, the Connection is created with a memory BIO: see
720the \method{bio_read}, \method{bio_write}, and \method{bio_shutdown}
721methods.
722\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500723
724\begin{excdesc}{Error}
725This exception is used as a base class for the other SSL-related
726exceptions, but may also be raised directly.
727
728Whenever this exception is raised directly, it has a list of error messages
729from the OpenSSL error queue, where each item is a tuple \code{(\var{lib},
730\var{function}, \var{reason})}. Here \var{lib}, \var{function} and \var{reason}
731are all strings, describing where and what the problem is. See \manpage{err}{3}
732for more information.
733\end{excdesc}
734
735\begin{excdesc}{ZeroReturnError}
736This exception matches the error return code \code{SSL_ERROR_ZERO_RETURN}, and
737is raised when the SSL Connection has been closed. In SSL 3.0 and TLS 1.0, this
738only occurs if a closure alert has occurred in the protocol, i.e. the
739connection has been closed cleanly. Note that this does not necessarily
740mean that the transport layer (e.g. a socket) has been closed.
741
742It may seem a little strange that this is an exception, but it does match an
743\code{SSL_ERROR} code, and is very convenient.
744\end{excdesc}
745
746\begin{excdesc}{WantReadError}
747The operation did not complete; the same I/O method should be called again
748later, with the same arguments. Any I/O method can lead to this since new
749handshakes can occur at any time.
Rick Dean71fa0962009-07-09 23:56:39 -0500750
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400751The wanted read is for \emph{dirty} data sent over the network, not the
752\emph{clean} data inside the tunnel. For a socket based SSL connection,
753\emph{read} means data coming at us over the network. Until that read
754succeeds, the attempted \method{OpenSSL.SSL.Connection.recv},
Rick Dean71fa0962009-07-09 23:56:39 -0500755\method{OpenSSL.SSL.Connection.send}, or
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400756\method{OpenSSL.SSL.Connection.do_handshake} is prevented or incomplete. You
757probably want to \method{select()} on the socket before trying again.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500758\end{excdesc}
759
760\begin{excdesc}{WantWriteError}
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400761See \exception{WantReadError}. The socket send buffer may be too full to
Rick Dean71fa0962009-07-09 23:56:39 -0500762write more data.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500763\end{excdesc}
764
765\begin{excdesc}{WantX509LookupError}
766The operation did not complete because an application callback has asked to be
767called again. The I/O method should be called again later, with the same
768arguments. Note: This won't occur in this version, as there are no such
769callbacks in this version.
770\end{excdesc}
771
772\begin{excdesc}{SysCallError}
773The \exception{SysCallError} occurs when there's an I/O error and OpenSSL's
774error queue does not contain any information. This can mean two things: An
775error in the transport protocol, or an end of file that violates the protocol.
776The parameter to the exception is always a pair \code{(\var{errnum},
777\var{errstr})}.
778\end{excdesc}
779
780
781\subsubsection{Context objects \label{openssl-context}}
782
783Context objects have the following methods:
784
785\begin{methoddesc}[Context]{check_privatekey}{}
786Check if the private key (loaded with \method{use_privatekey\optional{_file}})
787matches the certificate (loaded with \method{use_certificate\optional{_file}}).
Jean-Paul Calderonef05fbbe2008-03-06 21:52:35 -0500788Returns \code{None} if they match, raises \exception{Error} otherwise.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500789\end{methoddesc}
790
791\begin{methoddesc}[Context]{get_app_data}{}
792Retrieve application data as set by \method{set_app_data}.
793\end{methoddesc}
794
795\begin{methoddesc}[Context]{get_cert_store}{}
796Retrieve the certificate store (a X509Store object) that the context uses.
797This can be used to add "trusted" certificates without using the.
798\method{load_verify_locations()} method.
799\end{methoddesc}
800
801\begin{methoddesc}[Context]{get_timeout}{}
802Retrieve session timeout, as set by \method{set_timeout}. The default is 300
803seconds.
804\end{methoddesc}
805
806\begin{methoddesc}[Context]{get_verify_depth}{}
807Retrieve the Context object's verify depth, as set by
808\method{set_verify_depth}.
809\end{methoddesc}
810
811\begin{methoddesc}[Context]{get_verify_mode}{}
Jean-Paul Calderoneae4238d2008-12-28 21:13:50 -0500812Retrieve the Context object's verify mode, as set by \method{set_verify}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500813\end{methoddesc}
814
815\begin{methoddesc}[Context]{load_client_ca}{pemfile}
816Read a file with PEM-formatted certificates that will be sent to the client
817when requesting a client certificate.
818\end{methoddesc}
819
Jean-Paul Calderone5601c242008-09-07 21:06:52 -0400820\begin{methoddesc}[Context]{load_verify_locations}{pemfile, capath}
821Specify where CA certificates for verification purposes are located. These
822are trusted certificates. Note that the certificates have to be in PEM
823format. If capath is passed, it must be a directory prepared using the
824\code{c_rehash} tool included with OpenSSL. Either, but not both, of
825\var{pemfile} or \var{capath} may be \code{None}.
826\end{methoddesc}
827
828\begin{methoddesc}[Context]{set_default_verify_paths}{}
829Specify that the platform provided CA certificates are to be used for
Jean-Paul Calderone1d287e52009-03-07 09:09:07 -0500830verification purposes. This method may not work properly on OS X.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500831\end{methoddesc}
832
833\begin{methoddesc}[Context]{load_tmp_dh}{dhfile}
834Load parameters for Ephemeral Diffie-Hellman from \var{dhfile}.
835\end{methoddesc}
836
837\begin{methoddesc}[Context]{set_app_data}{data}
838Associate \var{data} with this Context object. \var{data} can be retrieved
839later using the \method{get_app_data} method.
840\end{methoddesc}
841
842\begin{methoddesc}[Context]{set_cipher_list}{ciphers}
843Set the list of ciphers to be used in this context. See the OpenSSL manual for
844more information (e.g. ciphers(1))
845\end{methoddesc}
846
847\begin{methoddesc}[Context]{set_info_callback}{callback}
848Set the information callback to \var{callback}. This function will be called
849from time to time during SSL handshakes.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500850\var{callback} should take three arguments: a Connection object and two
851integers. The first integer specifies where in the SSL handshake the function
852was called, and the other the return code from a (possibly failed) internal
853function call.
854\end{methoddesc}
855
856\begin{methoddesc}[Context]{set_options}{options}
857Add SSL options. Options you have set before are not cleared!
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500858This method should be used with the \constant{OP_*} constants.
859\end{methoddesc}
860
861\begin{methoddesc}[Context]{set_passwd_cb}{callback\optional{, userdata}}
862Set the passphrase callback to \var{callback}. This function will be called
Jean-Paul Calderone1eeb29e2008-10-19 11:50:53 -0400863when a private key with a passphrase is loaded. \var{callback} must accept
864three positional arguments. First, an integer giving the maximum length of
865the passphrase it may return. If the returned passphrase is longer than
866this, it will be truncated. Second, a boolean value which will be true if
867the user should be prompted for the passphrase twice and the callback should
868verify that the two values supplied are equal. Third, the value given as the
869\var{userdata} parameter to \method{set_passwd_cb}. If an error occurs,
870\var{callback} should return a false value (e.g. an empty string).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500871\end{methoddesc}
872
873\begin{methoddesc}[Context]{set_session_id}{name}
874Set the context \var{name} within which a session can be reused for this
875Context object. This is needed when doing session resumption, because there is
876no way for a stored session to know which Context object it is associated with.
877\var{name} may be any binary data.
878\end{methoddesc}
879
880\begin{methoddesc}[Context]{set_timeout}{timeout}
881Set the timeout for newly created sessions for this Context object to
882\var{timeout}. \var{timeout} must be given in (whole) seconds. The default
883value is 300 seconds. See the OpenSSL manual for more information (e.g.
884SSL_CTX_set_timeout(3)).
885\end{methoddesc}
886
887\begin{methoddesc}[Context]{set_verify}{mode, callback}
888Set the verification flags for this Context object to \var{mode} and specify
889that \var{callback} should be used for verification callbacks. \var{mode}
890should be one of \constant{VERIFY_NONE} and \constant{VERIFY_PEER}. If
891\constant{VERIFY_PEER} is used, \var{mode} can be OR:ed with
892\constant{VERIFY_FAIL_IF_NO_PEER_CERT} and \constant{VERIFY_CLIENT_ONCE} to
893further control the behaviour.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500894\var{callback} should take five arguments: A Connection object, an X509 object,
895and three integer variables, which are in turn potential error number, error
896depth and return code. \var{callback} should return true if verification passes
897and false otherwise.
898\end{methoddesc}
899
900\begin{methoddesc}[Context]{set_verify_depth}{depth}
901Set the maximum depth for the certificate chain verification that shall be
902allowed for this Context object.
903\end{methoddesc}
904
905\begin{methoddesc}[Context]{use_certificate}{cert}
906Use the certificate \var{cert} which has to be a X509 object.
907\end{methoddesc}
908
Jean-Paul Calderone87b40602008-02-19 21:13:25 -0500909\begin{methoddesc}[Context]{add_extra_chain_cert}{cert}
910Adds the certificate \var{cert}, which has to be a X509 object, to the
911certificate chain presented together with the certificate.
912\end{methoddesc}
913
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500914\begin{methoddesc}[Context]{use_certificate_chain_file}{file}
915Load a certificate chain from \var{file} which must be PEM encoded.
916\end{methoddesc}
917
918\begin{methoddesc}[Context]{use_privatekey}{pkey}
919Use the private key \var{pkey} which has to be a PKey object.
920\end{methoddesc}
921
922\begin{methoddesc}[Context]{use_certificate_file}{file\optional{, format}}
923Load the first certificate found in \var{file}. The certificate must be in the
924format specified by \var{format}, which is either \constant{FILETYPE_PEM} or
925\constant{FILETYPE_ASN1}. The default is \constant{FILETYPE_PEM}.
926\end{methoddesc}
927
928\begin{methoddesc}[Context]{use_privatekey_file}{file\optional{, format}}
929Load the first private key found in \var{file}. The private key must be in the
930format specified by \var{format}, which is either \constant{FILETYPE_PEM} or
931\constant{FILETYPE_ASN1}. The default is \constant{FILETYPE_PEM}.
932\end{methoddesc}
933
934
935\subsubsection{Connection objects \label{openssl-connection}}
936
937Connection objects have the following methods:
938
939\begin{methoddesc}[Connection]{accept}{}
940Call the \method{accept} method of the underlying socket and set up SSL on the
941returned socket, using the Context object supplied to this Connection object at
942creation. Returns a pair \code{(\var{conn}, \var{address})}. where \var{conn}
943is the new Connection object created, and \var{address} is as returned by the
944socket's \method{accept}.
945\end{methoddesc}
946
947\begin{methoddesc}[Connection]{bind}{address}
948Call the \method{bind} method of the underlying socket.
949\end{methoddesc}
950
951\begin{methoddesc}[Connection]{close}{}
952Call the \method{close} method of the underlying socket. Note: If you want
953correct SSL closure, you need to call the \method{shutdown} method first.
954\end{methoddesc}
955
956\begin{methoddesc}[Connection]{connect}{address}
957Call the \method{connect} method of the underlying socket and set up SSL on the
958socket, using the Context object supplied to this Connection object at
959creation.
960\end{methoddesc}
961
962\begin{methoddesc}[Connection]{connect_ex}{address}
963Call the \method{connect_ex} method of the underlying socket and set up SSL on
964the socket, using the Context object supplied to this Connection object at
965creation. Note that if the \method{connect_ex} method of the socket doesn't
966return 0, SSL won't be initialized.
967\end{methoddesc}
968
969\begin{methoddesc}[Connection]{do_handshake}{}
970Perform an SSL handshake (usually called after \method{renegotiate} or one of
971\method{set_accept_state} or \method{set_accept_state}). This can raise the
972same exceptions as \method{send} and \method{recv}.
973\end{methoddesc}
974
975\begin{methoddesc}[Connection]{fileno}{}
976Retrieve the file descriptor number for the underlying socket.
977\end{methoddesc}
978
979\begin{methoddesc}[Connection]{listen}{backlog}
980Call the \method{listen} method of the underlying socket.
981\end{methoddesc}
982
983\begin{methoddesc}[Connection]{get_app_data}{}
984Retrieve application data as set by \method{set_app_data}.
985\end{methoddesc}
986
987\begin{methoddesc}[Connection]{get_cipher_list}{}
988Retrieve the list of ciphers used by the Connection object. WARNING: This API
989has changed. It used to take an optional parameter and just return a string,
990but not it returns the entire list in one go.
991\end{methoddesc}
992
993\begin{methoddesc}[Connection]{get_context}{}
994Retrieve the Context object associated with this Connection.
995\end{methoddesc}
996
997\begin{methoddesc}[Connection]{get_peer_certificate}{}
998Retrieve the other side's certificate (if any)
999\end{methoddesc}
1000
1001\begin{methoddesc}[Connection]{getpeername}{}
1002Call the \method{getpeername} method of the underlying socket.
1003\end{methoddesc}
1004
1005\begin{methoddesc}[Connection]{getsockname}{}
1006Call the \method{getsockname} method of the underlying socket.
1007\end{methoddesc}
1008
1009\begin{methoddesc}[Connection]{getsockopt}{level, optname\optional{, buflen}}
1010Call the \method{getsockopt} method of the underlying socket.
1011\end{methoddesc}
1012
1013\begin{methoddesc}[Connection]{pending}{}
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001014Retrieve the number of bytes that can be safely read from the SSL buffer
1015(\emph{not} the underlying transport buffer).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001016\end{methoddesc}
1017
1018\begin{methoddesc}[Connection]{recv}{bufsize}
1019Receive data from the Connection. The return value is a string representing the
1020data received. The maximum amount of data to be received at once, is specified
1021by \var{bufsize}.
1022\end{methoddesc}
1023
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001024\begin{methoddesc}[Connection]{bio_write}{bytes}
1025If the Connection was created with a memory BIO, this method can be used to add
1026bytes to the read end of that memory BIO. The Connection can then read the
1027bytes (for example, in response to a call to \method{recv}).
1028\end{methoddesc}
1029
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001030\begin{methoddesc}[Connection]{renegotiate}{}
1031Renegotiate the SSL session. Call this if you wish to change cipher suites or
1032anything like that.
1033\end{methoddesc}
1034
1035\begin{methoddesc}[Connection]{send}{string}
1036Send the \var{string} data to the Connection.
1037\end{methoddesc}
1038
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001039\begin{methoddesc}[Connection]{bio_read}{bufsize}
1040If the Connection was created with a memory BIO, this method can be used to
1041read bytes from the write end of that memory BIO. Many Connection methods will
1042add bytes which must be read in this manner or the buffer will eventually fill
1043up and the Connection will be able to take no further actions.
1044\end{methoddesc}
1045
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001046\begin{methoddesc}[Connection]{sendall}{string}
1047Send all of the \var{string} data to the Connection. This calls \method{send}
1048repeatedly until all data is sent. If an error occurs, it's impossible to tell
1049how much data has been sent.
1050\end{methoddesc}
1051
1052\begin{methoddesc}[Connection]{set_accept_state}{}
1053Set the connection to work in server mode. The handshake will be handled
1054automatically by read/write.
1055\end{methoddesc}
1056
1057\begin{methoddesc}[Connection]{set_app_data}{data}
1058Associate \var{data} with this Connection object. \var{data} can be retrieved
1059later using the \method{get_app_data} method.
1060\end{methoddesc}
1061
1062\begin{methoddesc}[Connection]{set_connect_state}{}
1063Set the connection to work in client mode. The handshake will be handled
1064automatically by read/write.
1065\end{methoddesc}
1066
1067\begin{methoddesc}[Connection]{setblocking}{flag}
1068Call the \method{setblocking} method of the underlying socket.
1069\end{methoddesc}
1070
1071\begin{methoddesc}[Connection]{setsockopt}{level, optname, value}
1072Call the \method{setsockopt} method of the underlying socket.
1073\end{methoddesc}
1074
1075\begin{methoddesc}[Connection]{shutdown}{}
1076Send the shutdown message to the Connection. Returns true if the shutdown
1077message exchange is completed and false otherwise (in which case you call
1078\method{recv()} or \method{send()} when the connection becomes
1079readable/writeable.
1080\end{methoddesc}
1081
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001082\begin{methoddesc}[Connection]{get_shutdown}{}
1083Get the shutdown state of the Connection. Returns a bitvector of either or
1084both of \var{SENT_SHUTDOWN} and \var{RECEIVED_SHUTDOWN}.
1085\end{methoddesc}
1086
1087\begin{methoddesc}[Connection]{set_shutdown}{state}
1088Set the shutdown state of the Connection. \var{state} is a bitvector of
1089either or both of \var{SENT_SHUTDOWN} and \var{RECEIVED_SHUTDOWN}.
1090\end{methoddesc}
1091
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001092\begin{methoddesc}[Connection]{sock_shutdown}{how}
1093Call the \method{shutdown} method of the underlying socket.
1094\end{methoddesc}
1095
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001096\begin{methoddesc}[Connection]{bio_shutdown}{}
1097If the Connection was created with a memory BIO, this method can be used to
1098indicate that ``end of file'' has been reached on the read end of that memory
1099BIO.
1100\end{methoddesc}
1101
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001102\begin{methoddesc}[Connection]{state_string}{}
1103Retrieve a verbose string detailing the state of the Connection.
1104\end{methoddesc}
1105
Jean-Paul Calderonefd236f32009-05-03 19:45:07 -04001106\begin{methoddesc}[Connection]{client_random}{}
1107Retrieve the random value used with the client hello message.
1108\end{methoddesc}
1109
1110\begin{methoddesc}[Connection]{server_random}{}
1111Retrieve the random value used with the server hello message.
1112\end{methoddesc}
1113
1114\begin{methoddesc}[Connection]{master_key}{}
1115Retrieve the value of the master key for this session.
1116\end{methoddesc}
1117
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001118\begin{methoddesc}[Connection]{want_read}{}
1119Checks if more data has to be read from the transport layer to complete an
1120operation.
1121\end{methoddesc}
1122
1123\begin{methoddesc}[Connection]{want_write}{}
1124Checks if there is data to write to the transport layer to complete an
1125operation.
1126\end{methoddesc}
1127
1128
1129
1130\section{Internals \label{internals}}
1131
1132We ran into three main problems developing this: Exceptions, callbacks and
1133accessing socket methods. This is what this chapter is about.
1134
1135\subsection{Exceptions \label{exceptions}}
1136
1137We realized early that most of the exceptions would be raised by the I/O
1138functions of OpenSSL, so it felt natural to mimic OpenSSL's error code system,
1139translating them into Python exceptions. This naturally gives us the exceptions
1140\exception{SSL.ZeroReturnError}, \exception{SSL.WantReadError},
1141\exception{SSL.WantWriteError}, \exception{SSL.WantX509LookupError} and
1142\exception{SSL.SysCallError}.
1143
1144For more information about this, see section \ref{openssl-ssl}.
1145
1146
1147\subsection{Callbacks \label{callbacks}}
1148
1149There are a number of problems with callbacks. First of all, OpenSSL is written
1150as a C library, it's not meant to have Python callbacks, so a way around that
1151is needed. Another problem is thread support. A lot of the OpenSSL I/O
1152functions can block if the socket is in blocking mode, and then you want other
1153Python threads to be able to do other things. The real trouble is if you've
Jean-Paul Calderoneb7d6db22008-09-21 18:57:56 -04001154released the global CPython interpreter lock to do a potentially blocking
1155operation, and the operation calls a callback. Then we must take the GIL back,
1156since calling Python APIs without holding it is not allowed.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001157
1158There are two solutions to the first problem, both of which are necessary. The
1159first solution to use is if the C callback allows ''userdata'' to be passed to
1160it (an arbitrary pointer normally). This is great! We can set our Python
1161function object as the real userdata and emulate userdata for the Python
1162function in another way. The other solution can be used if an object with an
1163''app_data'' system always is passed to the callback. For example, the SSL
1164object in OpenSSL has app_data functions and in e.g. the verification
1165callbacks, you can retrieve the related SSL object. What we do is to set our
1166wrapper \class{Connection} object as app_data for the SSL object, and we can
1167easily find the Python callback.
1168
Jean-Paul Calderoneb7d6db22008-09-21 18:57:56 -04001169The other problem is solved using thread local variables. Whenever the GIL is
1170released before calling into an OpenSSL API, the PyThreadState pointer returned
1171by \cfunction{PyEval_SaveState} is stored in a global thread local variable
1172(using Python's own TLS API, \cfunction{PyThread_set_key_value}). When it is
1173necessary to re-acquire the GIL, either after the OpenSSL API returns or in a C
1174callback invoked by that OpenSSL API, the value of the thread local variable is
1175retrieved (\cfunction{PyThread_get_key_value}) and used to re-acquire the GIL.
1176This allows Python threads to execute while OpenSSL APIs are running and allows
1177use of any particular pyOpenSSL object from any Python thread, since there is
1178no per-thread state associated with any of these objects and since OpenSSL is
1179threadsafe (as long as properly initialized, as pyOpenSSL initializes it).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001180
1181
1182\subsection{Acessing Socket Methods \label{socket-methods}}
1183
1184We quickly saw the benefit of wrapping socket methods in the
1185\class{SSL.Connection} class, for an easy transition into using SSL. The
1186problem here is that the \module{socket} module lacks a C API, and all the
1187methods are declared static. One approach would be to have \module{OpenSSL} as
1188a submodule to the \module{socket} module, placing all the code in
1189\file{socketmodule.c}, but this is obviously not a good solution, since you
1190might not want to import tonnes of extra stuff you're not going to use when
1191importing the \module{socket} module. The other approach is to somehow get a
1192pointer to the method to be called, either the C function, or a callable Python
1193object. This is not really a good solution either, since there's a lot of
1194lookups involved.
1195
1196The way it works is that you have to supply a ``\class{socket}-like'' transport
1197object to the \class{SSL.Connection}. The only requirement of this object is
1198that it has a \method{fileno()} method that returns a file descriptor that's
1199valid at the C level (i.e. you can use the system calls read and write). If you
1200want to use the \method{connect()} or \method{accept()} methods of the
1201\class{SSL.Connection} object, the transport object has to supply such
1202methods too. Apart from them, any method lookups in the \class{SSL.Connection}
1203object that fail are passed on to the underlying transport object.
1204
1205Future changes might be to allow Python-level transport objects, that instead
1206of having \method{fileno()} methods, have \method{read()} and \method{write()}
1207methods, so more advanced features of Python can be used. This would probably
1208entail some sort of OpenSSL ``BIOs'', but converting Python strings back and
1209forth is expensive, so this shouldn't be used unless necessary. Other nice
1210things would be to be able to pass in different transport objects for reading
1211and writing, but then the \method{fileno()} method of \class{SSL.Connection}
1212becomes virtually useless. Also, should the method resolution be used on the
1213read-transport or the write-transport?
1214
1215
1216\end{document}