blob: 5d5ca9de77757bbad823e6138cb46ab4bf91dc34 [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
Rick Deanf94096c2009-07-18 14:23:06 -0500265encrypted, a \var{passphrase} must be included. The MAC is always
266checked and thus required.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500267\end{funcdesc}
268
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500269\subsubsection{X509 objects \label{openssl-x509}}
270
271X509 objects have the following methods:
272
273\begin{methoddesc}[X509]{get_issuer}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500274Return an X509Name object representing the issuer of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500275\end{methoddesc}
276
277\begin{methoddesc}[X509]{get_pubkey}{}
278Return a PKey object representing the public key of the certificate.
279\end{methoddesc}
280
281\begin{methoddesc}[X509]{get_serial_number}{}
282Return the certificate serial number.
283\end{methoddesc}
284
285\begin{methoddesc}[X509]{get_subject}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500286Return an X509Name object representing the subject of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500287\end{methoddesc}
288
289\begin{methoddesc}[X509]{get_version}{}
290Return the certificate version.
291\end{methoddesc}
292
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400293\begin{methoddesc}[X509]{get_notBefore}{}
294Return a string giving the time before which the certificate is not valid. The
295string is formatted as an ASN1 GENERALIZEDTIME:
296\begin{verbatim}
297 YYYYMMDDhhmmssZ
298 YYYYMMDDhhmmss+hhmm
299 YYYYMMDDhhmmss-hhmm
300\end{verbatim}
Jean-Paul Calderonee0615b52008-03-09 21:44:46 -0400301If no value exists for this field, \code{None} is returned.
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400302\end{methoddesc}
303
304\begin{methoddesc}[X509]{get_notAfter}{}
305Return a string giving the time after which the certificate is not valid. The
306string is formatted as an ASN1 GENERALIZEDTIME:
307\begin{verbatim}
308 YYYYMMDDhhmmssZ
309 YYYYMMDDhhmmss+hhmm
310 YYYYMMDDhhmmss-hhmm
311\end{verbatim}
Jean-Paul Calderonee0615b52008-03-09 21:44:46 -0400312If no value exists for this field, \code{None} is returned.
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400313\end{methoddesc}
314
315\begin{methoddesc}[X509]{set_notBefore}{when}
316Change the time before which the certificate is not valid. \var{when} is a
317string formatted as an ASN1 GENERALIZEDTIME:
318\begin{verbatim}
319 YYYYMMDDhhmmssZ
320 YYYYMMDDhhmmss+hhmm
321 YYYYMMDDhhmmss-hhmm
322\end{verbatim}
323\end{methoddesc}
324
325\begin{methoddesc}[X509]{set_notAfter}{when}
326Change the time after which the certificate is not valid. \var{when} is a
327string formatted as an ASN1 GENERALIZEDTIME:
328\begin{verbatim}
329 YYYYMMDDhhmmssZ
330 YYYYMMDDhhmmss+hhmm
331 YYYYMMDDhhmmss-hhmm
332\end{verbatim}
333\end{methoddesc}
334
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500335\begin{methoddesc}[X509]{gmtime_adj_notBefore}{time}
336Adjust the timestamp (in GMT) when the certificate starts being valid.
337\end{methoddesc}
338
339\begin{methoddesc}[X509]{gmtime_adj_notAfter}{time}
340Adjust the timestamp (in GMT) when the certificate stops being valid.
341\end{methoddesc}
342
343\begin{methoddesc}[X509]{has_expired}{}
344Checks the certificate's time stamp against current time. Returns true if the
345certificate has expired and false otherwise.
346\end{methoddesc}
347
348\begin{methoddesc}[X509]{set_issuer}{issuer}
349Set the issuer of the certificate to \var{issuer}.
350\end{methoddesc}
351
352\begin{methoddesc}[X509]{set_pubkey}{pkey}
353Set the public key of the certificate to \var{pkey}.
354\end{methoddesc}
355
356\begin{methoddesc}[X509]{set_serial_number}{serialno}
357Set the serial number of the certificate to \var{serialno}.
358\end{methoddesc}
359
360\begin{methoddesc}[X509]{set_subject}{subject}
361Set the subject of the certificate to \var{subject}.
362\end{methoddesc}
363
364\begin{methoddesc}[X509]{set_version}{version}
365Set the certificate version to \var{version}.
366\end{methoddesc}
367
368\begin{methoddesc}[X509]{sign}{pkey, digest}
369Sign the certificate, using the key \var{pkey} and the message digest algorithm
370identified by the string \var{digest}.
371\end{methoddesc}
372
373\begin{methoddesc}[X509]{subject_name_hash}{}
374Return the hash of the certificate subject.
375\end{methoddesc}
376
377\begin{methoddesc}[X509]{digest}{digest_name}
378Return a digest of the certificate, using the \var{digest_name} method.
Jean-Paul Calderoneb6f0d602008-12-28 21:20:01 -0500379\var{digest_name} must be a string describing a digest algorithm supported
380by OpenSSL (by EVP_get_digestbyname, specifically). For example,
381\constant{"md5"} or \constant{"sha1"}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500382\end{methoddesc}
383
384\begin{methoddesc}[X509]{add_extensions}{extensions}
385Add the extensions in the sequence \var{extensions} to the certificate.
386\end{methoddesc}
387
388\subsubsection{X509Name objects \label{openssl-x509name}}
389
Jean-Paul Calderone2dd8ff52008-03-24 17:43:58 -0400390X509Name objects have the following methods:
391
392\begin{methoddesc}[X509Name]{hash}{}
393Return an integer giving the first four bytes of the MD5 digest of the DER
394representation of the name.
395\end{methoddesc}
396
Jean-Paul Calderonea6edbf82008-03-25 15:19:11 -0400397\begin{methoddesc}[X509Name]{der}{}
398Return a string giving the DER representation of the name.
399\end{methoddesc}
400
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400401\begin{methoddesc}[X509Name]{get_components}{}
402Return a list of two-tuples of strings giving the components of the name.
403\end{methoddesc}
404
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500405X509Name objects have the following members:
406
407\begin{memberdesc}[X509Name]{countryName}
408The country of the entity. \code{C} may be used as an alias for
409\code{countryName}.
410\end{memberdesc}
411
412\begin{memberdesc}[X509Name]{stateOrProvinceName}
413The state or province of the entity. \code{ST} may be used as an alias for
414\code{stateOrProvinceName}·
415\end{memberdesc}
416
417\begin{memberdesc}[X509Name]{localityName}
418The locality of the entity. \code{L} may be used as an alias for
419\code{localityName}.
420\end{memberdesc}
421
422\begin{memberdesc}[X509Name]{organizationName}
423The organization name of the entity. \code{O} may be used as an alias for
424\code{organizationName}.
425\end{memberdesc}
426
427\begin{memberdesc}[X509Name]{organizationalUnitName}
428The organizational unit of the entity. \code{OU} may be used as an alias for
429\code{organizationalUnitName}.
430\end{memberdesc}
431
432\begin{memberdesc}[X509Name]{commonName}
433The common name of the entity. \code{CN} may be used as an alias for
434\code{commonName}.
435\end{memberdesc}
436
437\begin{memberdesc}[X509Name]{emailAddress}
438The e-mail address of the entity.
439\end{memberdesc}
440
441\subsubsection{X509Req objects \label{openssl-x509req}}
442
443X509Req objects have the following methods:
444
445\begin{methoddesc}[X509Req]{get_pubkey}{}
446Return a PKey object representing the public key of the certificate request.
447\end{methoddesc}
448
449\begin{methoddesc}[X509Req]{get_subject}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500450Return an X509Name object representing the subject of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500451\end{methoddesc}
452
453\begin{methoddesc}[X509Req]{set_pubkey}{pkey}
454Set the public key of the certificate request to \var{pkey}.
455\end{methoddesc}
456
457\begin{methoddesc}[X509Req]{sign}{pkey, digest}
458Sign the certificate request, using the key \var{pkey} and the message digest
459algorithm identified by the string \var{digest}.
460\end{methoddesc}
461
462\begin{methoddesc}[X509Req]{verify}{pkey}
463Verify a certificate request using the public key \var{pkey}.
464\end{methoddesc}
465
Jean-Paul Calderone8dd19b82008-12-28 20:41:16 -0500466\begin{methoddesc}[X509Req]{set_version}{version}
467Set the version (RFC 2459, 4.1.2.1) of the certificate request to
468\var{version}.
469\end{methoddesc}
470
471\begin{methoddesc}[X509Req]{get_version}{}
472Get the version (RFC 2459, 4.1.2.1) of the certificate request.
473\end{methoddesc}
474
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500475\subsubsection{X509Store objects \label{openssl-x509store}}
476
477The X509Store object has currently just one method:
478
479\begin{methoddesc}[X509Store]{add_cert}{cert}
480Add the certificate \var{cert} to the certificate store.
481\end{methoddesc}
482
483\subsubsection{PKey objects \label{openssl-pkey}}
484
485The PKey object has the following methods:
486
487\begin{methoddesc}[PKey]{bits}{}
488Return the number of bits of the key.
489\end{methoddesc}
490
491\begin{methoddesc}[PKey]{generate_key}{type, bits}
492Generate a public/private key pair of the type \var{type} (one of
493\constant{TYPE_RSA} and \constant{TYPE_DSA}) with the size \var{bits}.
494\end{methoddesc}
495
496\begin{methoddesc}[PKey]{type}{}
497Return the type of the key.
498\end{methoddesc}
499
500\subsubsection{PKCS7 objects \label{openssl-pkcs7}}
501
502PKCS7 objects have the following methods:
503
504\begin{methoddesc}[PKCS7]{type_is_signed}{}
505FIXME
506\end{methoddesc}
507
508\begin{methoddesc}[PKCS7]{type_is_enveloped}{}
509FIXME
510\end{methoddesc}
511
512\begin{methoddesc}[PKCS7]{type_is_signedAndEnveloped}{}
513FIXME
514\end{methoddesc}
515
516\begin{methoddesc}[PKCS7]{type_is_data}{}
517FIXME
518\end{methoddesc}
519
520\begin{methoddesc}[PKCS7]{get_type_name}{}
521Get the type name of the PKCS7.
522\end{methoddesc}
523
524\subsubsection{PKCS12 objects \label{openssl-pkcs12}}
525
526PKCS12 objects have the following methods:
527
Rick Deane182f482009-07-17 14:49:48 -0500528\begin{methoddesc}[PKCS12]{export}{\optional{passphrase=None}\optional{, friendly_name=None}\optional{, iter=2000}\optional{, maciter=0}}
529Returns a PKCS12 object as a string.
530
531The optional \var{passphrase} must be a string not a callback.
532
533See also the man page for the C function \function{PKCS12_create}.
534\end{methoddesc}
535
536\begin{methoddesc}[PKCS12]{get_ca_certificates}{}
537Return CA certificates within the PKCS12 object as a tuple. Returns
538\constant{None} if no CA certificates are present.
539\end{methoddesc}
540
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500541\begin{methoddesc}[PKCS12]{get_certificate}{}
542Return certificate portion of the PKCS12 structure.
543\end{methoddesc}
544
545\begin{methoddesc}[PKCS12]{get_privatekey}{}
546Return private key portion of the PKCS12 structure
547\end{methoddesc}
548
Rick Deane182f482009-07-17 14:49:48 -0500549\begin{methoddesc}[PKCS12]{set_ca_certificates}{cacerts}
550Replace or set the CA certificates within the PKCS12 object with the sequence \var{cacerts}.
551
552Set \var{cacerts} to \constant{None} to remove all CA certificates.
553\end{methoddesc}
554
555\begin{methoddesc}[PKCS12]{set_certificate}{cert}
556Replace or set the certificate portion of the PKCS12 structure.
557\end{methoddesc}
558
559\begin{methoddesc}[PKCS12]{set_privatekey}{pkey}
560Replace or set private key portion of the PKCS12 structure
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500561\end{methoddesc}
562
563\subsubsection{X509Extension objects \label{openssl-509ext}}
564
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -0500565X509Extension objects have several methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500566
567\begin{methoddesc}[X509Extension]{get_critical}{}
568Return the critical field of the extension object.
569\end{methoddesc}
570
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -0500571\begin{methoddesc}[X509Extension]{get_short_name}{}
572Return the short type name of the extension object.
573\end{methoddesc}
574
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500575\subsubsection{NetscapeSPKI objects \label{openssl-netscape-spki}}
576
577NetscapeSPKI objects have the following methods:
578
579\begin{methoddesc}[NetscapeSPKI]{b64_encode}{}
580Return a base64-encoded string representation of the object.
581\end{methoddesc}
582
583\begin{methoddesc}[NetscapeSPKI]{get_pubkey}{}
584Return the public key of object.
585\end{methoddesc}
586
587\begin{methoddesc}[NetscapeSPKI]{set_pubkey}{key}
588Set the public key of the object to \var{key}.
589\end{methoddesc}
590
591\begin{methoddesc}[NetscapeSPKI]{sign}{key, digest_name}
Jean-Paul Calderoneb6f0d602008-12-28 21:20:01 -0500592Sign the NetscapeSPKI object using the given \var{key} and
593\var{digest_name}. \var{digest_name} must be a string describing a digest
594algorithm supported by OpenSSL (by EVP_get_digestbyname, specifically). For
595example, \constant{"md5"} or \constant{"sha1"}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500596\end{methoddesc}
597
598\begin{methoddesc}[NetscapeSPKI]{verify}{key}
599Verify the NetscapeSPKI object using the given \var{key}.
600\end{methoddesc}
601
602
603% % % rand module
604
605\subsection{\module{rand} --- An interface to the OpenSSL pseudo random number generator \label{openssl-rand}}
606
607\declaremodule{extension}{rand}
608\modulesynopsis{An interface to the OpenSSL pseudo random number generator}
609
610This module handles the OpenSSL pseudo random number generator (PRNG) and
611declares the following:
612
613\begin{funcdesc}{add}{string, entropy}
614Mix bytes from \var{string} into the PRNG state. The \var{entropy} argument is
615(the lower bound of) an estimate of how much randomness is contained in
616\var{string}, measured in bytes. For more information, see e.g. \rfc{1750}.
617\end{funcdesc}
618
Rick Dean4fd5a4e2009-07-08 12:06:10 -0500619\begin{funcdesc}{bytes}{num_bytes}
Jean-Paul Calderonea45e2b92009-07-08 13:29:58 -0400620Get some random bytes from the PRNG as a string.
621
622This is a wrapper for the C function \function{RAND_bytes}.
Rick Dean4fd5a4e2009-07-08 12:06:10 -0500623\end{funcdesc}
624
Rick Dean433dc642009-07-07 13:11:55 -0500625\begin{funcdesc}{cleanup}{}
Jean-Paul Calderone427c0b32009-07-07 15:43:27 -0400626Erase the memory used by the PRNG.
627
628This is a wrapper for the C function \function{RAND_cleanup}.
Rick Dean433dc642009-07-07 13:11:55 -0500629\end{funcdesc}
630
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500631\begin{funcdesc}{egd}{path\optional{, bytes}}
632Query the Entropy Gathering Daemon\footnote{See
633\url{http://www.lothar.com/tech/crypto/}} on socket \var{path} for \var{bytes}
634bytes of random data and and uses \function{add} to seed the PRNG. The default
635value of \var{bytes} is 255.
636\end{funcdesc}
637
638\begin{funcdesc}{load_file}{path\optional{, bytes}}
639Read \var{bytes} bytes (or all of it, if \var{bytes} is negative) of data from
640the file \var{path} to seed the PRNG. The default value of \var{bytes} is -1.
641\end{funcdesc}
642
643\begin{funcdesc}{screen}{}
644Add the current contents of the screen to the PRNG state.
645Availability: Windows.
646\end{funcdesc}
647
648\begin{funcdesc}{seed}{string}
649This is equivalent to calling \function{add} with \var{entropy} as the length
650of the string.
651\end{funcdesc}
652
653\begin{funcdesc}{status}{}
654Returns true if the PRNG has been seeded with enough data, and false otherwise.
655\end{funcdesc}
656
657\begin{funcdesc}{write_file}{path}
658Write a number of random bytes (currently 1024) to the file \var{path}. This
659file can then be used with \function{load_file} to seed the PRNG again.
660\end{funcdesc}
661
Rick Deanfc69c812009-07-08 11:03:47 -0500662\begin{excdesc}{Error}
663If the current RAND method supports any errors, this is raised when needed.
664The default method does not raise this when the entropy pool is depleted.
665
666Whenever this exception is raised directly, it has a list of error messages
667from the OpenSSL error queue, where each item is a tuple \code{(\var{lib},
668\var{function}, \var{reason})}. Here \var{lib}, \var{function} and \var{reason}
669are all strings, describing where and what the problem is. See \manpage{err}{3}
670for more information.
671\end{excdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500672
673
674% % % SSL module
675
676\subsection{\module{SSL} --- An interface to the SSL-specific parts of OpenSSL \label{openssl-ssl}}
677
678\declaremodule{extension}{SSL}
679\modulesynopsis{An interface to the SSL-specific parts of OpenSSL}
680
681This module handles things specific to SSL. There are two objects defined:
682Context, Connection.
683
684\begin{datadesc}{SSLv2_METHOD}
685\dataline{SSLv3_METHOD}
686\dataline{SSLv23_METHOD}
687\dataline{TLSv1_METHOD}
688These constants represent the different SSL methods to use when creating a
689context object.
690\end{datadesc}
691
692\begin{datadesc}{VERIFY_NONE}
693\dataline{VERIFY_PEER}
694\dataline{VERIFY_FAIL_IF_NO_PEER_CERT}
695These constants represent the verification mode used by the Context
696object's \method{set_verify} method.
697\end{datadesc}
698
699\begin{datadesc}{FILETYPE_PEM}
700\dataline{FILETYPE_ASN1}
701File type constants used with the \method{use_certificate_file} and
702\method{use_privatekey_file} methods of Context objects.
703\end{datadesc}
704
705\begin{datadesc}{OP_SINGLE_DH_USE}
706\dataline{OP_EPHEMERAL_RSA}
707\dataline{OP_NO_SSLv2}
708\dataline{OP_NO_SSLv3}
709\dataline{OP_NO_TLSv1}
710Constants used with \method{set_options} of Context objects.
711\constant{OP_SINGLE_DH_USE} means to always create a new key when using ephemeral
712Diffie-Hellman. \constant{OP_EPHEMERAL_RSA} means to always use ephemeral RSA keys
713when doing RSA operations. \constant{OP_NO_SSLv2}, \constant{OP_NO_SSLv3} and
714\constant{OP_NO_TLSv1} means to disable those specific protocols. This is
715interesting if you're using e.g. \constant{SSLv23_METHOD} to get an SSLv2-compatible
716handshake, but don't want to use SSLv2.
717\end{datadesc}
718
719\begin{datadesc}{ContextType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400720See \class{Context}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500721\end{datadesc}
722
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400723\begin{classdesc}{Context}{method}
724A class representing SSL contexts. Contexts define the parameters of one or
725more SSL connections.
726
727\var{method} should be \constant{SSLv2_METHOD}, \constant{SSLv3_METHOD},
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500728\constant{SSLv23_METHOD} or \constant{TLSv1_METHOD}.
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400729\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500730
731\begin{datadesc}{ConnectionType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400732See \class{Connection}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500733\end{datadesc}
734
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400735\begin{classdesc}{Connection}{context, socket}
736A class representing SSL connections.
737
738\var{context} should be an instance of \class{Context} and \var{socket}
739should be a socket \footnote{Actually, all that is required is an object
740that \emph{behaves} like a socket, you could even use files, even though
741it'd be tricky to get the handshakes right!} object. \var{socket} may be
742\var{None}; in this case, the Connection is created with a memory BIO: see
743the \method{bio_read}, \method{bio_write}, and \method{bio_shutdown}
744methods.
745\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500746
747\begin{excdesc}{Error}
748This exception is used as a base class for the other SSL-related
749exceptions, but may also be raised directly.
750
751Whenever this exception is raised directly, it has a list of error messages
752from the OpenSSL error queue, where each item is a tuple \code{(\var{lib},
753\var{function}, \var{reason})}. Here \var{lib}, \var{function} and \var{reason}
754are all strings, describing where and what the problem is. See \manpage{err}{3}
755for more information.
756\end{excdesc}
757
758\begin{excdesc}{ZeroReturnError}
759This exception matches the error return code \code{SSL_ERROR_ZERO_RETURN}, and
760is raised when the SSL Connection has been closed. In SSL 3.0 and TLS 1.0, this
761only occurs if a closure alert has occurred in the protocol, i.e. the
762connection has been closed cleanly. Note that this does not necessarily
763mean that the transport layer (e.g. a socket) has been closed.
764
765It may seem a little strange that this is an exception, but it does match an
766\code{SSL_ERROR} code, and is very convenient.
767\end{excdesc}
768
769\begin{excdesc}{WantReadError}
770The operation did not complete; the same I/O method should be called again
771later, with the same arguments. Any I/O method can lead to this since new
772handshakes can occur at any time.
Rick Dean71fa0962009-07-09 23:56:39 -0500773
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400774The wanted read is for \emph{dirty} data sent over the network, not the
775\emph{clean} data inside the tunnel. For a socket based SSL connection,
776\emph{read} means data coming at us over the network. Until that read
777succeeds, the attempted \method{OpenSSL.SSL.Connection.recv},
Rick Dean71fa0962009-07-09 23:56:39 -0500778\method{OpenSSL.SSL.Connection.send}, or
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400779\method{OpenSSL.SSL.Connection.do_handshake} is prevented or incomplete. You
780probably want to \method{select()} on the socket before trying again.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500781\end{excdesc}
782
783\begin{excdesc}{WantWriteError}
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400784See \exception{WantReadError}. The socket send buffer may be too full to
Rick Dean71fa0962009-07-09 23:56:39 -0500785write more data.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500786\end{excdesc}
787
788\begin{excdesc}{WantX509LookupError}
789The operation did not complete because an application callback has asked to be
790called again. The I/O method should be called again later, with the same
791arguments. Note: This won't occur in this version, as there are no such
792callbacks in this version.
793\end{excdesc}
794
795\begin{excdesc}{SysCallError}
796The \exception{SysCallError} occurs when there's an I/O error and OpenSSL's
797error queue does not contain any information. This can mean two things: An
798error in the transport protocol, or an end of file that violates the protocol.
799The parameter to the exception is always a pair \code{(\var{errnum},
800\var{errstr})}.
801\end{excdesc}
802
803
804\subsubsection{Context objects \label{openssl-context}}
805
806Context objects have the following methods:
807
808\begin{methoddesc}[Context]{check_privatekey}{}
809Check if the private key (loaded with \method{use_privatekey\optional{_file}})
810matches the certificate (loaded with \method{use_certificate\optional{_file}}).
Jean-Paul Calderonef05fbbe2008-03-06 21:52:35 -0500811Returns \code{None} if they match, raises \exception{Error} otherwise.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500812\end{methoddesc}
813
814\begin{methoddesc}[Context]{get_app_data}{}
815Retrieve application data as set by \method{set_app_data}.
816\end{methoddesc}
817
818\begin{methoddesc}[Context]{get_cert_store}{}
819Retrieve the certificate store (a X509Store object) that the context uses.
820This can be used to add "trusted" certificates without using the.
821\method{load_verify_locations()} method.
822\end{methoddesc}
823
824\begin{methoddesc}[Context]{get_timeout}{}
825Retrieve session timeout, as set by \method{set_timeout}. The default is 300
826seconds.
827\end{methoddesc}
828
829\begin{methoddesc}[Context]{get_verify_depth}{}
830Retrieve the Context object's verify depth, as set by
831\method{set_verify_depth}.
832\end{methoddesc}
833
834\begin{methoddesc}[Context]{get_verify_mode}{}
Jean-Paul Calderoneae4238d2008-12-28 21:13:50 -0500835Retrieve the Context object's verify mode, as set by \method{set_verify}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500836\end{methoddesc}
837
838\begin{methoddesc}[Context]{load_client_ca}{pemfile}
839Read a file with PEM-formatted certificates that will be sent to the client
840when requesting a client certificate.
841\end{methoddesc}
842
Jean-Paul Calderone5601c242008-09-07 21:06:52 -0400843\begin{methoddesc}[Context]{load_verify_locations}{pemfile, capath}
844Specify where CA certificates for verification purposes are located. These
845are trusted certificates. Note that the certificates have to be in PEM
846format. If capath is passed, it must be a directory prepared using the
847\code{c_rehash} tool included with OpenSSL. Either, but not both, of
848\var{pemfile} or \var{capath} may be \code{None}.
849\end{methoddesc}
850
851\begin{methoddesc}[Context]{set_default_verify_paths}{}
852Specify that the platform provided CA certificates are to be used for
Jean-Paul Calderone1d287e52009-03-07 09:09:07 -0500853verification purposes. This method may not work properly on OS X.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500854\end{methoddesc}
855
856\begin{methoddesc}[Context]{load_tmp_dh}{dhfile}
857Load parameters for Ephemeral Diffie-Hellman from \var{dhfile}.
858\end{methoddesc}
859
860\begin{methoddesc}[Context]{set_app_data}{data}
861Associate \var{data} with this Context object. \var{data} can be retrieved
862later using the \method{get_app_data} method.
863\end{methoddesc}
864
865\begin{methoddesc}[Context]{set_cipher_list}{ciphers}
866Set the list of ciphers to be used in this context. See the OpenSSL manual for
867more information (e.g. ciphers(1))
868\end{methoddesc}
869
870\begin{methoddesc}[Context]{set_info_callback}{callback}
871Set the information callback to \var{callback}. This function will be called
872from time to time during SSL handshakes.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500873\var{callback} should take three arguments: a Connection object and two
874integers. The first integer specifies where in the SSL handshake the function
875was called, and the other the return code from a (possibly failed) internal
876function call.
877\end{methoddesc}
878
879\begin{methoddesc}[Context]{set_options}{options}
880Add SSL options. Options you have set before are not cleared!
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500881This method should be used with the \constant{OP_*} constants.
882\end{methoddesc}
883
884\begin{methoddesc}[Context]{set_passwd_cb}{callback\optional{, userdata}}
885Set the passphrase callback to \var{callback}. This function will be called
Jean-Paul Calderone1eeb29e2008-10-19 11:50:53 -0400886when a private key with a passphrase is loaded. \var{callback} must accept
887three positional arguments. First, an integer giving the maximum length of
888the passphrase it may return. If the returned passphrase is longer than
889this, it will be truncated. Second, a boolean value which will be true if
890the user should be prompted for the passphrase twice and the callback should
891verify that the two values supplied are equal. Third, the value given as the
892\var{userdata} parameter to \method{set_passwd_cb}. If an error occurs,
893\var{callback} should return a false value (e.g. an empty string).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500894\end{methoddesc}
895
896\begin{methoddesc}[Context]{set_session_id}{name}
897Set the context \var{name} within which a session can be reused for this
898Context object. This is needed when doing session resumption, because there is
899no way for a stored session to know which Context object it is associated with.
900\var{name} may be any binary data.
901\end{methoddesc}
902
903\begin{methoddesc}[Context]{set_timeout}{timeout}
904Set the timeout for newly created sessions for this Context object to
905\var{timeout}. \var{timeout} must be given in (whole) seconds. The default
906value is 300 seconds. See the OpenSSL manual for more information (e.g.
907SSL_CTX_set_timeout(3)).
908\end{methoddesc}
909
910\begin{methoddesc}[Context]{set_verify}{mode, callback}
911Set the verification flags for this Context object to \var{mode} and specify
912that \var{callback} should be used for verification callbacks. \var{mode}
913should be one of \constant{VERIFY_NONE} and \constant{VERIFY_PEER}. If
914\constant{VERIFY_PEER} is used, \var{mode} can be OR:ed with
915\constant{VERIFY_FAIL_IF_NO_PEER_CERT} and \constant{VERIFY_CLIENT_ONCE} to
916further control the behaviour.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500917\var{callback} should take five arguments: A Connection object, an X509 object,
918and three integer variables, which are in turn potential error number, error
919depth and return code. \var{callback} should return true if verification passes
920and false otherwise.
921\end{methoddesc}
922
923\begin{methoddesc}[Context]{set_verify_depth}{depth}
924Set the maximum depth for the certificate chain verification that shall be
925allowed for this Context object.
926\end{methoddesc}
927
928\begin{methoddesc}[Context]{use_certificate}{cert}
929Use the certificate \var{cert} which has to be a X509 object.
930\end{methoddesc}
931
Jean-Paul Calderone87b40602008-02-19 21:13:25 -0500932\begin{methoddesc}[Context]{add_extra_chain_cert}{cert}
933Adds the certificate \var{cert}, which has to be a X509 object, to the
934certificate chain presented together with the certificate.
935\end{methoddesc}
936
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500937\begin{methoddesc}[Context]{use_certificate_chain_file}{file}
938Load a certificate chain from \var{file} which must be PEM encoded.
939\end{methoddesc}
940
941\begin{methoddesc}[Context]{use_privatekey}{pkey}
942Use the private key \var{pkey} which has to be a PKey object.
943\end{methoddesc}
944
945\begin{methoddesc}[Context]{use_certificate_file}{file\optional{, format}}
946Load the first certificate found in \var{file}. The certificate must be in the
947format specified by \var{format}, which is either \constant{FILETYPE_PEM} or
948\constant{FILETYPE_ASN1}. The default is \constant{FILETYPE_PEM}.
949\end{methoddesc}
950
951\begin{methoddesc}[Context]{use_privatekey_file}{file\optional{, format}}
952Load the first private key found in \var{file}. The private key must be in the
953format specified by \var{format}, which is either \constant{FILETYPE_PEM} or
954\constant{FILETYPE_ASN1}. The default is \constant{FILETYPE_PEM}.
955\end{methoddesc}
956
957
958\subsubsection{Connection objects \label{openssl-connection}}
959
960Connection objects have the following methods:
961
962\begin{methoddesc}[Connection]{accept}{}
963Call the \method{accept} method of the underlying socket and set up SSL on the
964returned socket, using the Context object supplied to this Connection object at
965creation. Returns a pair \code{(\var{conn}, \var{address})}. where \var{conn}
966is the new Connection object created, and \var{address} is as returned by the
967socket's \method{accept}.
968\end{methoddesc}
969
970\begin{methoddesc}[Connection]{bind}{address}
971Call the \method{bind} method of the underlying socket.
972\end{methoddesc}
973
974\begin{methoddesc}[Connection]{close}{}
975Call the \method{close} method of the underlying socket. Note: If you want
976correct SSL closure, you need to call the \method{shutdown} method first.
977\end{methoddesc}
978
979\begin{methoddesc}[Connection]{connect}{address}
980Call the \method{connect} method of the underlying socket and set up SSL on the
981socket, using the Context object supplied to this Connection object at
982creation.
983\end{methoddesc}
984
985\begin{methoddesc}[Connection]{connect_ex}{address}
986Call the \method{connect_ex} method of the underlying socket and set up SSL on
987the socket, using the Context object supplied to this Connection object at
988creation. Note that if the \method{connect_ex} method of the socket doesn't
989return 0, SSL won't be initialized.
990\end{methoddesc}
991
992\begin{methoddesc}[Connection]{do_handshake}{}
993Perform an SSL handshake (usually called after \method{renegotiate} or one of
994\method{set_accept_state} or \method{set_accept_state}). This can raise the
995same exceptions as \method{send} and \method{recv}.
996\end{methoddesc}
997
998\begin{methoddesc}[Connection]{fileno}{}
999Retrieve the file descriptor number for the underlying socket.
1000\end{methoddesc}
1001
1002\begin{methoddesc}[Connection]{listen}{backlog}
1003Call the \method{listen} method of the underlying socket.
1004\end{methoddesc}
1005
1006\begin{methoddesc}[Connection]{get_app_data}{}
1007Retrieve application data as set by \method{set_app_data}.
1008\end{methoddesc}
1009
1010\begin{methoddesc}[Connection]{get_cipher_list}{}
1011Retrieve the list of ciphers used by the Connection object. WARNING: This API
1012has changed. It used to take an optional parameter and just return a string,
1013but not it returns the entire list in one go.
1014\end{methoddesc}
1015
1016\begin{methoddesc}[Connection]{get_context}{}
1017Retrieve the Context object associated with this Connection.
1018\end{methoddesc}
1019
1020\begin{methoddesc}[Connection]{get_peer_certificate}{}
1021Retrieve the other side's certificate (if any)
1022\end{methoddesc}
1023
1024\begin{methoddesc}[Connection]{getpeername}{}
1025Call the \method{getpeername} method of the underlying socket.
1026\end{methoddesc}
1027
1028\begin{methoddesc}[Connection]{getsockname}{}
1029Call the \method{getsockname} method of the underlying socket.
1030\end{methoddesc}
1031
1032\begin{methoddesc}[Connection]{getsockopt}{level, optname\optional{, buflen}}
1033Call the \method{getsockopt} method of the underlying socket.
1034\end{methoddesc}
1035
1036\begin{methoddesc}[Connection]{pending}{}
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001037Retrieve the number of bytes that can be safely read from the SSL buffer
1038(\emph{not} the underlying transport buffer).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001039\end{methoddesc}
1040
1041\begin{methoddesc}[Connection]{recv}{bufsize}
1042Receive data from the Connection. The return value is a string representing the
1043data received. The maximum amount of data to be received at once, is specified
1044by \var{bufsize}.
1045\end{methoddesc}
1046
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001047\begin{methoddesc}[Connection]{bio_write}{bytes}
1048If the Connection was created with a memory BIO, this method can be used to add
1049bytes to the read end of that memory BIO. The Connection can then read the
1050bytes (for example, in response to a call to \method{recv}).
1051\end{methoddesc}
1052
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001053\begin{methoddesc}[Connection]{renegotiate}{}
1054Renegotiate the SSL session. Call this if you wish to change cipher suites or
1055anything like that.
1056\end{methoddesc}
1057
1058\begin{methoddesc}[Connection]{send}{string}
1059Send the \var{string} data to the Connection.
1060\end{methoddesc}
1061
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001062\begin{methoddesc}[Connection]{bio_read}{bufsize}
1063If the Connection was created with a memory BIO, this method can be used to
1064read bytes from the write end of that memory BIO. Many Connection methods will
1065add bytes which must be read in this manner or the buffer will eventually fill
1066up and the Connection will be able to take no further actions.
1067\end{methoddesc}
1068
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001069\begin{methoddesc}[Connection]{sendall}{string}
1070Send all of the \var{string} data to the Connection. This calls \method{send}
1071repeatedly until all data is sent. If an error occurs, it's impossible to tell
1072how much data has been sent.
1073\end{methoddesc}
1074
1075\begin{methoddesc}[Connection]{set_accept_state}{}
1076Set the connection to work in server mode. The handshake will be handled
1077automatically by read/write.
1078\end{methoddesc}
1079
1080\begin{methoddesc}[Connection]{set_app_data}{data}
1081Associate \var{data} with this Connection object. \var{data} can be retrieved
1082later using the \method{get_app_data} method.
1083\end{methoddesc}
1084
1085\begin{methoddesc}[Connection]{set_connect_state}{}
1086Set the connection to work in client mode. The handshake will be handled
1087automatically by read/write.
1088\end{methoddesc}
1089
1090\begin{methoddesc}[Connection]{setblocking}{flag}
1091Call the \method{setblocking} method of the underlying socket.
1092\end{methoddesc}
1093
1094\begin{methoddesc}[Connection]{setsockopt}{level, optname, value}
1095Call the \method{setsockopt} method of the underlying socket.
1096\end{methoddesc}
1097
1098\begin{methoddesc}[Connection]{shutdown}{}
1099Send the shutdown message to the Connection. Returns true if the shutdown
1100message exchange is completed and false otherwise (in which case you call
1101\method{recv()} or \method{send()} when the connection becomes
1102readable/writeable.
1103\end{methoddesc}
1104
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001105\begin{methoddesc}[Connection]{get_shutdown}{}
1106Get the shutdown state of the Connection. Returns a bitvector of either or
1107both of \var{SENT_SHUTDOWN} and \var{RECEIVED_SHUTDOWN}.
1108\end{methoddesc}
1109
1110\begin{methoddesc}[Connection]{set_shutdown}{state}
1111Set the shutdown state of the Connection. \var{state} is a bitvector of
1112either or both of \var{SENT_SHUTDOWN} and \var{RECEIVED_SHUTDOWN}.
1113\end{methoddesc}
1114
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001115\begin{methoddesc}[Connection]{sock_shutdown}{how}
1116Call the \method{shutdown} method of the underlying socket.
1117\end{methoddesc}
1118
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001119\begin{methoddesc}[Connection]{bio_shutdown}{}
1120If the Connection was created with a memory BIO, this method can be used to
1121indicate that ``end of file'' has been reached on the read end of that memory
1122BIO.
1123\end{methoddesc}
1124
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001125\begin{methoddesc}[Connection]{state_string}{}
1126Retrieve a verbose string detailing the state of the Connection.
1127\end{methoddesc}
1128
Jean-Paul Calderonefd236f32009-05-03 19:45:07 -04001129\begin{methoddesc}[Connection]{client_random}{}
1130Retrieve the random value used with the client hello message.
1131\end{methoddesc}
1132
1133\begin{methoddesc}[Connection]{server_random}{}
1134Retrieve the random value used with the server hello message.
1135\end{methoddesc}
1136
1137\begin{methoddesc}[Connection]{master_key}{}
1138Retrieve the value of the master key for this session.
1139\end{methoddesc}
1140
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001141\begin{methoddesc}[Connection]{want_read}{}
1142Checks if more data has to be read from the transport layer to complete an
1143operation.
1144\end{methoddesc}
1145
1146\begin{methoddesc}[Connection]{want_write}{}
1147Checks if there is data to write to the transport layer to complete an
1148operation.
1149\end{methoddesc}
1150
1151
1152
1153\section{Internals \label{internals}}
1154
1155We ran into three main problems developing this: Exceptions, callbacks and
1156accessing socket methods. This is what this chapter is about.
1157
1158\subsection{Exceptions \label{exceptions}}
1159
1160We realized early that most of the exceptions would be raised by the I/O
1161functions of OpenSSL, so it felt natural to mimic OpenSSL's error code system,
1162translating them into Python exceptions. This naturally gives us the exceptions
1163\exception{SSL.ZeroReturnError}, \exception{SSL.WantReadError},
1164\exception{SSL.WantWriteError}, \exception{SSL.WantX509LookupError} and
1165\exception{SSL.SysCallError}.
1166
1167For more information about this, see section \ref{openssl-ssl}.
1168
1169
1170\subsection{Callbacks \label{callbacks}}
1171
1172There are a number of problems with callbacks. First of all, OpenSSL is written
1173as a C library, it's not meant to have Python callbacks, so a way around that
1174is needed. Another problem is thread support. A lot of the OpenSSL I/O
1175functions can block if the socket is in blocking mode, and then you want other
1176Python threads to be able to do other things. The real trouble is if you've
Jean-Paul Calderoneb7d6db22008-09-21 18:57:56 -04001177released the global CPython interpreter lock to do a potentially blocking
1178operation, and the operation calls a callback. Then we must take the GIL back,
1179since calling Python APIs without holding it is not allowed.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001180
1181There are two solutions to the first problem, both of which are necessary. The
1182first solution to use is if the C callback allows ''userdata'' to be passed to
1183it (an arbitrary pointer normally). This is great! We can set our Python
1184function object as the real userdata and emulate userdata for the Python
1185function in another way. The other solution can be used if an object with an
1186''app_data'' system always is passed to the callback. For example, the SSL
1187object in OpenSSL has app_data functions and in e.g. the verification
1188callbacks, you can retrieve the related SSL object. What we do is to set our
1189wrapper \class{Connection} object as app_data for the SSL object, and we can
1190easily find the Python callback.
1191
Jean-Paul Calderoneb7d6db22008-09-21 18:57:56 -04001192The other problem is solved using thread local variables. Whenever the GIL is
1193released before calling into an OpenSSL API, the PyThreadState pointer returned
1194by \cfunction{PyEval_SaveState} is stored in a global thread local variable
1195(using Python's own TLS API, \cfunction{PyThread_set_key_value}). When it is
1196necessary to re-acquire the GIL, either after the OpenSSL API returns or in a C
1197callback invoked by that OpenSSL API, the value of the thread local variable is
1198retrieved (\cfunction{PyThread_get_key_value}) and used to re-acquire the GIL.
1199This allows Python threads to execute while OpenSSL APIs are running and allows
1200use of any particular pyOpenSSL object from any Python thread, since there is
1201no per-thread state associated with any of these objects and since OpenSSL is
1202threadsafe (as long as properly initialized, as pyOpenSSL initializes it).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001203
1204
1205\subsection{Acessing Socket Methods \label{socket-methods}}
1206
1207We quickly saw the benefit of wrapping socket methods in the
1208\class{SSL.Connection} class, for an easy transition into using SSL. The
1209problem here is that the \module{socket} module lacks a C API, and all the
1210methods are declared static. One approach would be to have \module{OpenSSL} as
1211a submodule to the \module{socket} module, placing all the code in
1212\file{socketmodule.c}, but this is obviously not a good solution, since you
1213might not want to import tonnes of extra stuff you're not going to use when
1214importing the \module{socket} module. The other approach is to somehow get a
1215pointer to the method to be called, either the C function, or a callable Python
1216object. This is not really a good solution either, since there's a lot of
1217lookups involved.
1218
1219The way it works is that you have to supply a ``\class{socket}-like'' transport
1220object to the \class{SSL.Connection}. The only requirement of this object is
1221that it has a \method{fileno()} method that returns a file descriptor that's
1222valid at the C level (i.e. you can use the system calls read and write). If you
1223want to use the \method{connect()} or \method{accept()} methods of the
1224\class{SSL.Connection} object, the transport object has to supply such
1225methods too. Apart from them, any method lookups in the \class{SSL.Connection}
1226object that fail are passed on to the underlying transport object.
1227
1228Future changes might be to allow Python-level transport objects, that instead
1229of having \method{fileno()} methods, have \method{read()} and \method{write()}
1230methods, so more advanced features of Python can be used. This would probably
1231entail some sort of OpenSSL ``BIOs'', but converting Python strings back and
1232forth is expensive, so this shouldn't be used unless necessary. Other nice
1233things would be to be able to pass in different transport objects for reading
1234and writing, but then the \method{fileno()} method of \class{SSL.Connection}
1235becomes virtually useless. Also, should the method resolution be used on the
1236read-transport or the write-transport?
1237
1238
1239\end{document}