blob: b070ed9d6bd9b00c34f1bda44f69445668ff9edd [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
Rick Dean47262da2009-07-08 16:17:17 -0500191\begin{classdesc}{X509Extension}{typename, critical, value\optional{, subject}\optional{, issuer}}
Ziga Seilnacht444e7cb2009-09-01 13:31:36 +0200192A class representing an X.509 v3 certificate extensions.
193See \url{http://openssl.org/docs/apps/x509v3_config.html\#STANDARD_EXTENSIONS}
Rick Dean47262da2009-07-08 16:17:17 -0500194for \var{typename} strings and their options.
195Optional parameters \var{subject} and \var{issuer} must be X509 objects.
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400196\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500197
198\begin{datadesc}{NetscapeSPKIType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400199See \class{NetscapeSPKI}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500200\end{datadesc}
201
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400202\begin{classdesc}{NetscapeSPKI}{\optional{enc}}
203A class representing Netscape SPKI objects.
204
205If the \var{enc} argument is present, it should be a base64-encoded string
206representing a NetscapeSPKI object, as returned by the \method{b64_encode}
207method.
208\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500209
210\begin{datadesc}{FILETYPE_PEM}
211\dataline{FILETYPE_ASN1}
212File type constants.
213\end{datadesc}
214
215\begin{datadesc}{TYPE_RSA}
216\dataline{TYPE_DSA}
217Key type constants.
218\end{datadesc}
219
220\begin{excdesc}{Error}
221Generic exception used in the \module{crypto} module.
222\end{excdesc}
223
224\begin{funcdesc}{dump_certificate}{type, cert}
225Dump the certificate \var{cert} into a buffer string encoded with the type
226\var{type}.
227\end{funcdesc}
228
229\begin{funcdesc}{dump_certificate_request}{type, req}
230Dump the certificate request \var{req} into a buffer string encoded with the
231type \var{type}.
232\end{funcdesc}
233
234\begin{funcdesc}{dump_privatekey}{type, pkey\optional{, cipher, passphrase}}
235Dump the private key \var{pkey} into a buffer string encoded with the type
236\var{type}, optionally (if \var{type} is \constant{FILETYPE_PEM}) encrypting it
237using \var{cipher} and \var{passphrase}.
238
239\var{passphrase} must be either a string or a callback for providing the
240pass phrase.
241\end{funcdesc}
242
243\begin{funcdesc}{load_certificate}{type, buffer}
244Load a certificate (X509) from the string \var{buffer} encoded with the
245type \var{type}.
246\end{funcdesc}
247
248\begin{funcdesc}{load_certificate_request}{type, buffer}
249Load a certificate request (X509Req) from the string \var{buffer} encoded with
250the type \var{type}.
251\end{funcdesc}
252
253\begin{funcdesc}{load_privatekey}{type, buffer\optional{, passphrase}}
254Load a private key (PKey) from the string \var{buffer} encoded with
255the type \var{type} (must be one of \constant{FILETYPE_PEM} and
256\constant{FILETYPE_ASN1}).
257
258\var{passphrase} must be either a string or a callback for providing the
259pass phrase.
260\end{funcdesc}
261
262\begin{funcdesc}{load_pkcs7_data}{type, buffer}
263Load pkcs7 data from the string \var{buffer} encoded with the type \var{type}.
264\end{funcdesc}
265
266\begin{funcdesc}{load_pkcs12}{buffer\optional{, passphrase}}
267Load pkcs12 data from the string \var{buffer}. If the pkcs12 structure is
Rick Deanf94096c2009-07-18 14:23:06 -0500268encrypted, a \var{passphrase} must be included. The MAC is always
Jean-Paul Calderonee7901d72009-07-24 18:21:26 -0400269checked and thus required.
Rick Dean42d69e12009-07-20 11:36:08 -0500270
271See also the man page for the C function \function{PKCS12_parse}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500272\end{funcdesc}
273
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500274\subsubsection{X509 objects \label{openssl-x509}}
275
276X509 objects have the following methods:
277
278\begin{methoddesc}[X509]{get_issuer}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500279Return an X509Name object representing the issuer of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500280\end{methoddesc}
281
282\begin{methoddesc}[X509]{get_pubkey}{}
283Return a PKey object representing the public key of the certificate.
284\end{methoddesc}
285
286\begin{methoddesc}[X509]{get_serial_number}{}
287Return the certificate serial number.
288\end{methoddesc}
289
290\begin{methoddesc}[X509]{get_subject}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500291Return an X509Name object representing the subject of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500292\end{methoddesc}
293
294\begin{methoddesc}[X509]{get_version}{}
295Return the certificate version.
296\end{methoddesc}
297
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400298\begin{methoddesc}[X509]{get_notBefore}{}
299Return a string giving the time before which the certificate is not valid. The
300string is formatted as an ASN1 GENERALIZEDTIME:
301\begin{verbatim}
302 YYYYMMDDhhmmssZ
303 YYYYMMDDhhmmss+hhmm
304 YYYYMMDDhhmmss-hhmm
305\end{verbatim}
Jean-Paul Calderonee0615b52008-03-09 21:44:46 -0400306If no value exists for this field, \code{None} is returned.
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400307\end{methoddesc}
308
309\begin{methoddesc}[X509]{get_notAfter}{}
310Return a string giving the time after which the certificate is not valid. The
311string is formatted as an ASN1 GENERALIZEDTIME:
312\begin{verbatim}
313 YYYYMMDDhhmmssZ
314 YYYYMMDDhhmmss+hhmm
315 YYYYMMDDhhmmss-hhmm
316\end{verbatim}
Jean-Paul Calderonee0615b52008-03-09 21:44:46 -0400317If no value exists for this field, \code{None} is returned.
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400318\end{methoddesc}
319
320\begin{methoddesc}[X509]{set_notBefore}{when}
321Change the time before which the certificate is not valid. \var{when} is a
322string formatted as an ASN1 GENERALIZEDTIME:
323\begin{verbatim}
324 YYYYMMDDhhmmssZ
325 YYYYMMDDhhmmss+hhmm
326 YYYYMMDDhhmmss-hhmm
327\end{verbatim}
328\end{methoddesc}
329
330\begin{methoddesc}[X509]{set_notAfter}{when}
331Change the time after which the certificate is not valid. \var{when} is a
332string formatted as an ASN1 GENERALIZEDTIME:
333\begin{verbatim}
334 YYYYMMDDhhmmssZ
335 YYYYMMDDhhmmss+hhmm
336 YYYYMMDDhhmmss-hhmm
337\end{verbatim}
338\end{methoddesc}
339
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500340\begin{methoddesc}[X509]{gmtime_adj_notBefore}{time}
341Adjust the timestamp (in GMT) when the certificate starts being valid.
342\end{methoddesc}
343
344\begin{methoddesc}[X509]{gmtime_adj_notAfter}{time}
345Adjust the timestamp (in GMT) when the certificate stops being valid.
346\end{methoddesc}
347
348\begin{methoddesc}[X509]{has_expired}{}
349Checks the certificate's time stamp against current time. Returns true if the
350certificate has expired and false otherwise.
351\end{methoddesc}
352
353\begin{methoddesc}[X509]{set_issuer}{issuer}
354Set the issuer of the certificate to \var{issuer}.
355\end{methoddesc}
356
357\begin{methoddesc}[X509]{set_pubkey}{pkey}
358Set the public key of the certificate to \var{pkey}.
359\end{methoddesc}
360
361\begin{methoddesc}[X509]{set_serial_number}{serialno}
362Set the serial number of the certificate to \var{serialno}.
363\end{methoddesc}
364
365\begin{methoddesc}[X509]{set_subject}{subject}
366Set the subject of the certificate to \var{subject}.
367\end{methoddesc}
368
369\begin{methoddesc}[X509]{set_version}{version}
370Set the certificate version to \var{version}.
371\end{methoddesc}
372
373\begin{methoddesc}[X509]{sign}{pkey, digest}
374Sign the certificate, using the key \var{pkey} and the message digest algorithm
375identified by the string \var{digest}.
376\end{methoddesc}
377
378\begin{methoddesc}[X509]{subject_name_hash}{}
379Return the hash of the certificate subject.
380\end{methoddesc}
381
382\begin{methoddesc}[X509]{digest}{digest_name}
383Return a digest of the certificate, using the \var{digest_name} method.
Jean-Paul Calderoneb6f0d602008-12-28 21:20:01 -0500384\var{digest_name} must be a string describing a digest algorithm supported
385by OpenSSL (by EVP_get_digestbyname, specifically). For example,
386\constant{"md5"} or \constant{"sha1"}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500387\end{methoddesc}
388
389\begin{methoddesc}[X509]{add_extensions}{extensions}
390Add the extensions in the sequence \var{extensions} to the certificate.
391\end{methoddesc}
392
393\subsubsection{X509Name objects \label{openssl-x509name}}
394
Jean-Paul Calderone2dd8ff52008-03-24 17:43:58 -0400395X509Name objects have the following methods:
396
397\begin{methoddesc}[X509Name]{hash}{}
398Return an integer giving the first four bytes of the MD5 digest of the DER
399representation of the name.
400\end{methoddesc}
401
Jean-Paul Calderonea6edbf82008-03-25 15:19:11 -0400402\begin{methoddesc}[X509Name]{der}{}
403Return a string giving the DER representation of the name.
404\end{methoddesc}
405
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400406\begin{methoddesc}[X509Name]{get_components}{}
407Return a list of two-tuples of strings giving the components of the name.
408\end{methoddesc}
409
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500410X509Name objects have the following members:
411
412\begin{memberdesc}[X509Name]{countryName}
413The country of the entity. \code{C} may be used as an alias for
414\code{countryName}.
415\end{memberdesc}
416
417\begin{memberdesc}[X509Name]{stateOrProvinceName}
418The state or province of the entity. \code{ST} may be used as an alias for
419\code{stateOrProvinceName}·
420\end{memberdesc}
421
422\begin{memberdesc}[X509Name]{localityName}
423The locality of the entity. \code{L} may be used as an alias for
424\code{localityName}.
425\end{memberdesc}
426
427\begin{memberdesc}[X509Name]{organizationName}
428The organization name of the entity. \code{O} may be used as an alias for
429\code{organizationName}.
430\end{memberdesc}
431
432\begin{memberdesc}[X509Name]{organizationalUnitName}
433The organizational unit of the entity. \code{OU} may be used as an alias for
434\code{organizationalUnitName}.
435\end{memberdesc}
436
437\begin{memberdesc}[X509Name]{commonName}
438The common name of the entity. \code{CN} may be used as an alias for
439\code{commonName}.
440\end{memberdesc}
441
442\begin{memberdesc}[X509Name]{emailAddress}
443The e-mail address of the entity.
444\end{memberdesc}
445
446\subsubsection{X509Req objects \label{openssl-x509req}}
447
448X509Req objects have the following methods:
449
450\begin{methoddesc}[X509Req]{get_pubkey}{}
451Return a PKey object representing the public key of the certificate request.
452\end{methoddesc}
453
454\begin{methoddesc}[X509Req]{get_subject}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500455Return an X509Name object representing the subject of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500456\end{methoddesc}
457
458\begin{methoddesc}[X509Req]{set_pubkey}{pkey}
459Set the public key of the certificate request to \var{pkey}.
460\end{methoddesc}
461
462\begin{methoddesc}[X509Req]{sign}{pkey, digest}
463Sign the certificate request, using the key \var{pkey} and the message digest
464algorithm identified by the string \var{digest}.
465\end{methoddesc}
466
467\begin{methoddesc}[X509Req]{verify}{pkey}
468Verify a certificate request using the public key \var{pkey}.
469\end{methoddesc}
470
Jean-Paul Calderone8dd19b82008-12-28 20:41:16 -0500471\begin{methoddesc}[X509Req]{set_version}{version}
472Set the version (RFC 2459, 4.1.2.1) of the certificate request to
473\var{version}.
474\end{methoddesc}
475
476\begin{methoddesc}[X509Req]{get_version}{}
477Get the version (RFC 2459, 4.1.2.1) of the certificate request.
478\end{methoddesc}
479
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500480\subsubsection{X509Store objects \label{openssl-x509store}}
481
482The X509Store object has currently just one method:
483
484\begin{methoddesc}[X509Store]{add_cert}{cert}
485Add the certificate \var{cert} to the certificate store.
486\end{methoddesc}
487
488\subsubsection{PKey objects \label{openssl-pkey}}
489
490The PKey object has the following methods:
491
492\begin{methoddesc}[PKey]{bits}{}
493Return the number of bits of the key.
494\end{methoddesc}
495
496\begin{methoddesc}[PKey]{generate_key}{type, bits}
497Generate a public/private key pair of the type \var{type} (one of
498\constant{TYPE_RSA} and \constant{TYPE_DSA}) with the size \var{bits}.
499\end{methoddesc}
500
501\begin{methoddesc}[PKey]{type}{}
502Return the type of the key.
503\end{methoddesc}
504
505\subsubsection{PKCS7 objects \label{openssl-pkcs7}}
506
507PKCS7 objects have the following methods:
508
509\begin{methoddesc}[PKCS7]{type_is_signed}{}
510FIXME
511\end{methoddesc}
512
513\begin{methoddesc}[PKCS7]{type_is_enveloped}{}
514FIXME
515\end{methoddesc}
516
517\begin{methoddesc}[PKCS7]{type_is_signedAndEnveloped}{}
518FIXME
519\end{methoddesc}
520
521\begin{methoddesc}[PKCS7]{type_is_data}{}
522FIXME
523\end{methoddesc}
524
525\begin{methoddesc}[PKCS7]{get_type_name}{}
526Get the type name of the PKCS7.
527\end{methoddesc}
528
529\subsubsection{PKCS12 objects \label{openssl-pkcs12}}
530
531PKCS12 objects have the following methods:
532
Rick Dean42d69e12009-07-20 11:36:08 -0500533\begin{methoddesc}[PKCS12]{export}{\optional{passphrase=None}\optional{, iter=2048}\optional{, maciter=1}}
Rick Deane182f482009-07-17 14:49:48 -0500534Returns a PKCS12 object as a string.
535
536The optional \var{passphrase} must be a string not a callback.
537
538See also the man page for the C function \function{PKCS12_create}.
539\end{methoddesc}
540
541\begin{methoddesc}[PKCS12]{get_ca_certificates}{}
542Return CA certificates within the PKCS12 object as a tuple. Returns
543\constant{None} if no CA certificates are present.
544\end{methoddesc}
545
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500546\begin{methoddesc}[PKCS12]{get_certificate}{}
547Return certificate portion of the PKCS12 structure.
548\end{methoddesc}
549
Rick Dean42d69e12009-07-20 11:36:08 -0500550\begin{methoddesc}[PKCS12]{get_friendlyname}{}
551Return friendlyName portion of the PKCS12 structure.
552\end{methoddesc}
553
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500554\begin{methoddesc}[PKCS12]{get_privatekey}{}
555Return private key portion of the PKCS12 structure
556\end{methoddesc}
557
Rick Deane182f482009-07-17 14:49:48 -0500558\begin{methoddesc}[PKCS12]{set_ca_certificates}{cacerts}
559Replace or set the CA certificates within the PKCS12 object with the sequence \var{cacerts}.
560
561Set \var{cacerts} to \constant{None} to remove all CA certificates.
562\end{methoddesc}
563
564\begin{methoddesc}[PKCS12]{set_certificate}{cert}
565Replace or set the certificate portion of the PKCS12 structure.
566\end{methoddesc}
567
Rick Dean42d69e12009-07-20 11:36:08 -0500568\begin{methoddesc}[PKCS12]{set_friendlyname}{name}
569Replace or set the friendlyName portion of the PKCS12 structure.
570\end{methoddesc}
571
Rick Deane182f482009-07-17 14:49:48 -0500572\begin{methoddesc}[PKCS12]{set_privatekey}{pkey}
573Replace or set private key portion of the PKCS12 structure
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500574\end{methoddesc}
575
576\subsubsection{X509Extension objects \label{openssl-509ext}}
577
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -0500578X509Extension objects have several methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500579
580\begin{methoddesc}[X509Extension]{get_critical}{}
581Return the critical field of the extension object.
582\end{methoddesc}
583
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -0500584\begin{methoddesc}[X509Extension]{get_short_name}{}
585Return the short type name of the extension object.
586\end{methoddesc}
587
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500588\subsubsection{NetscapeSPKI objects \label{openssl-netscape-spki}}
589
590NetscapeSPKI objects have the following methods:
591
592\begin{methoddesc}[NetscapeSPKI]{b64_encode}{}
593Return a base64-encoded string representation of the object.
594\end{methoddesc}
595
596\begin{methoddesc}[NetscapeSPKI]{get_pubkey}{}
597Return the public key of object.
598\end{methoddesc}
599
600\begin{methoddesc}[NetscapeSPKI]{set_pubkey}{key}
601Set the public key of the object to \var{key}.
602\end{methoddesc}
603
604\begin{methoddesc}[NetscapeSPKI]{sign}{key, digest_name}
Jean-Paul Calderoneb6f0d602008-12-28 21:20:01 -0500605Sign the NetscapeSPKI object using the given \var{key} and
606\var{digest_name}. \var{digest_name} must be a string describing a digest
607algorithm supported by OpenSSL (by EVP_get_digestbyname, specifically). For
608example, \constant{"md5"} or \constant{"sha1"}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500609\end{methoddesc}
610
611\begin{methoddesc}[NetscapeSPKI]{verify}{key}
612Verify the NetscapeSPKI object using the given \var{key}.
613\end{methoddesc}
614
615
616% % % rand module
617
618\subsection{\module{rand} --- An interface to the OpenSSL pseudo random number generator \label{openssl-rand}}
619
620\declaremodule{extension}{rand}
621\modulesynopsis{An interface to the OpenSSL pseudo random number generator}
622
623This module handles the OpenSSL pseudo random number generator (PRNG) and
624declares the following:
625
626\begin{funcdesc}{add}{string, entropy}
627Mix bytes from \var{string} into the PRNG state. The \var{entropy} argument is
628(the lower bound of) an estimate of how much randomness is contained in
629\var{string}, measured in bytes. For more information, see e.g. \rfc{1750}.
630\end{funcdesc}
631
Rick Dean4fd5a4e2009-07-08 12:06:10 -0500632\begin{funcdesc}{bytes}{num_bytes}
Jean-Paul Calderonea45e2b92009-07-08 13:29:58 -0400633Get some random bytes from the PRNG as a string.
634
635This is a wrapper for the C function \function{RAND_bytes}.
Rick Dean4fd5a4e2009-07-08 12:06:10 -0500636\end{funcdesc}
637
Rick Dean433dc642009-07-07 13:11:55 -0500638\begin{funcdesc}{cleanup}{}
Jean-Paul Calderone427c0b32009-07-07 15:43:27 -0400639Erase the memory used by the PRNG.
640
641This is a wrapper for the C function \function{RAND_cleanup}.
Rick Dean433dc642009-07-07 13:11:55 -0500642\end{funcdesc}
643
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500644\begin{funcdesc}{egd}{path\optional{, bytes}}
645Query the Entropy Gathering Daemon\footnote{See
646\url{http://www.lothar.com/tech/crypto/}} on socket \var{path} for \var{bytes}
647bytes of random data and and uses \function{add} to seed the PRNG. The default
648value of \var{bytes} is 255.
649\end{funcdesc}
650
651\begin{funcdesc}{load_file}{path\optional{, bytes}}
652Read \var{bytes} bytes (or all of it, if \var{bytes} is negative) of data from
653the file \var{path} to seed the PRNG. The default value of \var{bytes} is -1.
654\end{funcdesc}
655
656\begin{funcdesc}{screen}{}
657Add the current contents of the screen to the PRNG state.
658Availability: Windows.
659\end{funcdesc}
660
661\begin{funcdesc}{seed}{string}
662This is equivalent to calling \function{add} with \var{entropy} as the length
663of the string.
664\end{funcdesc}
665
666\begin{funcdesc}{status}{}
667Returns true if the PRNG has been seeded with enough data, and false otherwise.
668\end{funcdesc}
669
670\begin{funcdesc}{write_file}{path}
671Write a number of random bytes (currently 1024) to the file \var{path}. This
672file can then be used with \function{load_file} to seed the PRNG again.
673\end{funcdesc}
674
Rick Deanfc69c812009-07-08 11:03:47 -0500675\begin{excdesc}{Error}
Ziga Seilnacht444e7cb2009-09-01 13:31:36 +0200676If the current RAND method supports any errors, this is raised when needed.
Rick Deanfc69c812009-07-08 11:03:47 -0500677The default method does not raise this when the entropy pool is depleted.
678
679Whenever this exception is raised directly, it has a list of error messages
680from the OpenSSL error queue, where each item is a tuple \code{(\var{lib},
681\var{function}, \var{reason})}. Here \var{lib}, \var{function} and \var{reason}
682are all strings, describing where and what the problem is. See \manpage{err}{3}
683for more information.
684\end{excdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500685
686
687% % % SSL module
688
689\subsection{\module{SSL} --- An interface to the SSL-specific parts of OpenSSL \label{openssl-ssl}}
690
691\declaremodule{extension}{SSL}
692\modulesynopsis{An interface to the SSL-specific parts of OpenSSL}
693
694This module handles things specific to SSL. There are two objects defined:
695Context, Connection.
696
697\begin{datadesc}{SSLv2_METHOD}
698\dataline{SSLv3_METHOD}
699\dataline{SSLv23_METHOD}
700\dataline{TLSv1_METHOD}
701These constants represent the different SSL methods to use when creating a
702context object.
703\end{datadesc}
704
705\begin{datadesc}{VERIFY_NONE}
706\dataline{VERIFY_PEER}
707\dataline{VERIFY_FAIL_IF_NO_PEER_CERT}
708These constants represent the verification mode used by the Context
709object's \method{set_verify} method.
710\end{datadesc}
711
712\begin{datadesc}{FILETYPE_PEM}
713\dataline{FILETYPE_ASN1}
714File type constants used with the \method{use_certificate_file} and
715\method{use_privatekey_file} methods of Context objects.
716\end{datadesc}
717
718\begin{datadesc}{OP_SINGLE_DH_USE}
719\dataline{OP_EPHEMERAL_RSA}
720\dataline{OP_NO_SSLv2}
721\dataline{OP_NO_SSLv3}
722\dataline{OP_NO_TLSv1}
723Constants used with \method{set_options} of Context objects.
724\constant{OP_SINGLE_DH_USE} means to always create a new key when using ephemeral
725Diffie-Hellman. \constant{OP_EPHEMERAL_RSA} means to always use ephemeral RSA keys
726when doing RSA operations. \constant{OP_NO_SSLv2}, \constant{OP_NO_SSLv3} and
727\constant{OP_NO_TLSv1} means to disable those specific protocols. This is
728interesting if you're using e.g. \constant{SSLv23_METHOD} to get an SSLv2-compatible
729handshake, but don't want to use SSLv2.
730\end{datadesc}
731
732\begin{datadesc}{ContextType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400733See \class{Context}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500734\end{datadesc}
735
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400736\begin{classdesc}{Context}{method}
737A class representing SSL contexts. Contexts define the parameters of one or
738more SSL connections.
739
740\var{method} should be \constant{SSLv2_METHOD}, \constant{SSLv3_METHOD},
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500741\constant{SSLv23_METHOD} or \constant{TLSv1_METHOD}.
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400742\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500743
744\begin{datadesc}{ConnectionType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400745See \class{Connection}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500746\end{datadesc}
747
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400748\begin{classdesc}{Connection}{context, socket}
749A class representing SSL connections.
750
751\var{context} should be an instance of \class{Context} and \var{socket}
752should be a socket \footnote{Actually, all that is required is an object
753that \emph{behaves} like a socket, you could even use files, even though
754it'd be tricky to get the handshakes right!} object. \var{socket} may be
755\var{None}; in this case, the Connection is created with a memory BIO: see
756the \method{bio_read}, \method{bio_write}, and \method{bio_shutdown}
757methods.
758\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500759
760\begin{excdesc}{Error}
761This exception is used as a base class for the other SSL-related
762exceptions, but may also be raised directly.
763
764Whenever this exception is raised directly, it has a list of error messages
765from the OpenSSL error queue, where each item is a tuple \code{(\var{lib},
766\var{function}, \var{reason})}. Here \var{lib}, \var{function} and \var{reason}
767are all strings, describing where and what the problem is. See \manpage{err}{3}
768for more information.
769\end{excdesc}
770
771\begin{excdesc}{ZeroReturnError}
772This exception matches the error return code \code{SSL_ERROR_ZERO_RETURN}, and
773is raised when the SSL Connection has been closed. In SSL 3.0 and TLS 1.0, this
774only occurs if a closure alert has occurred in the protocol, i.e. the
775connection has been closed cleanly. Note that this does not necessarily
776mean that the transport layer (e.g. a socket) has been closed.
777
778It may seem a little strange that this is an exception, but it does match an
779\code{SSL_ERROR} code, and is very convenient.
780\end{excdesc}
781
782\begin{excdesc}{WantReadError}
783The operation did not complete; the same I/O method should be called again
784later, with the same arguments. Any I/O method can lead to this since new
785handshakes can occur at any time.
Rick Dean71fa0962009-07-09 23:56:39 -0500786
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400787The wanted read is for \emph{dirty} data sent over the network, not the
788\emph{clean} data inside the tunnel. For a socket based SSL connection,
789\emph{read} means data coming at us over the network. Until that read
790succeeds, the attempted \method{OpenSSL.SSL.Connection.recv},
Rick Dean71fa0962009-07-09 23:56:39 -0500791\method{OpenSSL.SSL.Connection.send}, or
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400792\method{OpenSSL.SSL.Connection.do_handshake} is prevented or incomplete. You
793probably want to \method{select()} on the socket before trying again.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500794\end{excdesc}
795
796\begin{excdesc}{WantWriteError}
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400797See \exception{WantReadError}. The socket send buffer may be too full to
Rick Dean71fa0962009-07-09 23:56:39 -0500798write more data.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500799\end{excdesc}
800
801\begin{excdesc}{WantX509LookupError}
802The operation did not complete because an application callback has asked to be
803called again. The I/O method should be called again later, with the same
804arguments. Note: This won't occur in this version, as there are no such
805callbacks in this version.
806\end{excdesc}
807
808\begin{excdesc}{SysCallError}
809The \exception{SysCallError} occurs when there's an I/O error and OpenSSL's
810error queue does not contain any information. This can mean two things: An
811error in the transport protocol, or an end of file that violates the protocol.
812The parameter to the exception is always a pair \code{(\var{errnum},
813\var{errstr})}.
814\end{excdesc}
815
816
817\subsubsection{Context objects \label{openssl-context}}
818
819Context objects have the following methods:
820
821\begin{methoddesc}[Context]{check_privatekey}{}
822Check if the private key (loaded with \method{use_privatekey\optional{_file}})
823matches the certificate (loaded with \method{use_certificate\optional{_file}}).
Jean-Paul Calderonef05fbbe2008-03-06 21:52:35 -0500824Returns \code{None} if they match, raises \exception{Error} otherwise.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500825\end{methoddesc}
826
827\begin{methoddesc}[Context]{get_app_data}{}
828Retrieve application data as set by \method{set_app_data}.
829\end{methoddesc}
830
831\begin{methoddesc}[Context]{get_cert_store}{}
832Retrieve the certificate store (a X509Store object) that the context uses.
833This can be used to add "trusted" certificates without using the.
834\method{load_verify_locations()} method.
835\end{methoddesc}
836
837\begin{methoddesc}[Context]{get_timeout}{}
838Retrieve session timeout, as set by \method{set_timeout}. The default is 300
839seconds.
840\end{methoddesc}
841
842\begin{methoddesc}[Context]{get_verify_depth}{}
843Retrieve the Context object's verify depth, as set by
844\method{set_verify_depth}.
845\end{methoddesc}
846
847\begin{methoddesc}[Context]{get_verify_mode}{}
Jean-Paul Calderoneae4238d2008-12-28 21:13:50 -0500848Retrieve the Context object's verify mode, as set by \method{set_verify}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500849\end{methoddesc}
850
851\begin{methoddesc}[Context]{load_client_ca}{pemfile}
852Read a file with PEM-formatted certificates that will be sent to the client
853when requesting a client certificate.
854\end{methoddesc}
855
Ziga Seilnacht444e7cb2009-09-01 13:31:36 +0200856\begin{methoddesc}[Context]{set_client_CA_list}{certificate_authorities}
857Replace the current list of \class{OpenSSL.crypto.X509Name} objects that
858would be sent to the client when requesting a client certificate with a
859new one.
860
861\versionadded{0.10}
862\end{methoddesc}
863
864\begin{methoddesc}[Context]{add_client_CA}{certificate_authority}
865Extract a \class{OpenSSL.crypto.X509Name} from the \var{certificate_authority}
866\class{OpenSSL.crypto.X509} certificate and add it to the list of preferred
867certificate signers sent to the client when requesting a client certificate.
868% Certificate certificate certificate. Certificate certificate. Certificate
869% certificate certificate certificate certificate certificate. Certificate.
870
871\versionadded{0.10}
872\end{methoddesc}
873
Jean-Paul Calderone5601c242008-09-07 21:06:52 -0400874\begin{methoddesc}[Context]{load_verify_locations}{pemfile, capath}
875Specify where CA certificates for verification purposes are located. These
876are trusted certificates. Note that the certificates have to be in PEM
877format. If capath is passed, it must be a directory prepared using the
878\code{c_rehash} tool included with OpenSSL. Either, but not both, of
879\var{pemfile} or \var{capath} may be \code{None}.
880\end{methoddesc}
881
882\begin{methoddesc}[Context]{set_default_verify_paths}{}
883Specify that the platform provided CA certificates are to be used for
Jean-Paul Calderone1d287e52009-03-07 09:09:07 -0500884verification purposes. This method may not work properly on OS X.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500885\end{methoddesc}
886
887\begin{methoddesc}[Context]{load_tmp_dh}{dhfile}
888Load parameters for Ephemeral Diffie-Hellman from \var{dhfile}.
889\end{methoddesc}
890
891\begin{methoddesc}[Context]{set_app_data}{data}
892Associate \var{data} with this Context object. \var{data} can be retrieved
893later using the \method{get_app_data} method.
894\end{methoddesc}
895
896\begin{methoddesc}[Context]{set_cipher_list}{ciphers}
897Set the list of ciphers to be used in this context. See the OpenSSL manual for
898more information (e.g. ciphers(1))
899\end{methoddesc}
900
901\begin{methoddesc}[Context]{set_info_callback}{callback}
902Set the information callback to \var{callback}. This function will be called
903from time to time during SSL handshakes.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500904\var{callback} should take three arguments: a Connection object and two
905integers. The first integer specifies where in the SSL handshake the function
906was called, and the other the return code from a (possibly failed) internal
907function call.
908\end{methoddesc}
909
910\begin{methoddesc}[Context]{set_options}{options}
911Add SSL options. Options you have set before are not cleared!
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500912This method should be used with the \constant{OP_*} constants.
913\end{methoddesc}
914
915\begin{methoddesc}[Context]{set_passwd_cb}{callback\optional{, userdata}}
916Set the passphrase callback to \var{callback}. This function will be called
Jean-Paul Calderone1eeb29e2008-10-19 11:50:53 -0400917when a private key with a passphrase is loaded. \var{callback} must accept
918three positional arguments. First, an integer giving the maximum length of
919the passphrase it may return. If the returned passphrase is longer than
920this, it will be truncated. Second, a boolean value which will be true if
921the user should be prompted for the passphrase twice and the callback should
922verify that the two values supplied are equal. Third, the value given as the
923\var{userdata} parameter to \method{set_passwd_cb}. If an error occurs,
924\var{callback} should return a false value (e.g. an empty string).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500925\end{methoddesc}
926
927\begin{methoddesc}[Context]{set_session_id}{name}
928Set the context \var{name} within which a session can be reused for this
929Context object. This is needed when doing session resumption, because there is
930no way for a stored session to know which Context object it is associated with.
931\var{name} may be any binary data.
932\end{methoddesc}
933
934\begin{methoddesc}[Context]{set_timeout}{timeout}
935Set the timeout for newly created sessions for this Context object to
936\var{timeout}. \var{timeout} must be given in (whole) seconds. The default
937value is 300 seconds. See the OpenSSL manual for more information (e.g.
938SSL_CTX_set_timeout(3)).
939\end{methoddesc}
940
941\begin{methoddesc}[Context]{set_verify}{mode, callback}
942Set the verification flags for this Context object to \var{mode} and specify
943that \var{callback} should be used for verification callbacks. \var{mode}
944should be one of \constant{VERIFY_NONE} and \constant{VERIFY_PEER}. If
945\constant{VERIFY_PEER} is used, \var{mode} can be OR:ed with
946\constant{VERIFY_FAIL_IF_NO_PEER_CERT} and \constant{VERIFY_CLIENT_ONCE} to
947further control the behaviour.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500948\var{callback} should take five arguments: A Connection object, an X509 object,
949and three integer variables, which are in turn potential error number, error
950depth and return code. \var{callback} should return true if verification passes
951and false otherwise.
952\end{methoddesc}
953
954\begin{methoddesc}[Context]{set_verify_depth}{depth}
955Set the maximum depth for the certificate chain verification that shall be
956allowed for this Context object.
957\end{methoddesc}
958
959\begin{methoddesc}[Context]{use_certificate}{cert}
960Use the certificate \var{cert} which has to be a X509 object.
961\end{methoddesc}
962
Jean-Paul Calderone87b40602008-02-19 21:13:25 -0500963\begin{methoddesc}[Context]{add_extra_chain_cert}{cert}
964Adds the certificate \var{cert}, which has to be a X509 object, to the
965certificate chain presented together with the certificate.
966\end{methoddesc}
967
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500968\begin{methoddesc}[Context]{use_certificate_chain_file}{file}
969Load a certificate chain from \var{file} which must be PEM encoded.
970\end{methoddesc}
971
972\begin{methoddesc}[Context]{use_privatekey}{pkey}
973Use the private key \var{pkey} which has to be a PKey object.
974\end{methoddesc}
975
976\begin{methoddesc}[Context]{use_certificate_file}{file\optional{, format}}
977Load the first certificate found in \var{file}. The certificate must be in the
978format specified by \var{format}, which is either \constant{FILETYPE_PEM} or
979\constant{FILETYPE_ASN1}. The default is \constant{FILETYPE_PEM}.
980\end{methoddesc}
981
982\begin{methoddesc}[Context]{use_privatekey_file}{file\optional{, format}}
983Load the first private key found in \var{file}. The private key must be in the
984format specified by \var{format}, which is either \constant{FILETYPE_PEM} or
985\constant{FILETYPE_ASN1}. The default is \constant{FILETYPE_PEM}.
986\end{methoddesc}
987
988
989\subsubsection{Connection objects \label{openssl-connection}}
990
991Connection objects have the following methods:
992
993\begin{methoddesc}[Connection]{accept}{}
994Call the \method{accept} method of the underlying socket and set up SSL on the
995returned socket, using the Context object supplied to this Connection object at
996creation. Returns a pair \code{(\var{conn}, \var{address})}. where \var{conn}
997is the new Connection object created, and \var{address} is as returned by the
998socket's \method{accept}.
999\end{methoddesc}
1000
1001\begin{methoddesc}[Connection]{bind}{address}
1002Call the \method{bind} method of the underlying socket.
1003\end{methoddesc}
1004
1005\begin{methoddesc}[Connection]{close}{}
1006Call the \method{close} method of the underlying socket. Note: If you want
1007correct SSL closure, you need to call the \method{shutdown} method first.
1008\end{methoddesc}
1009
1010\begin{methoddesc}[Connection]{connect}{address}
1011Call the \method{connect} method of the underlying socket and set up SSL on the
1012socket, using the Context object supplied to this Connection object at
1013creation.
1014\end{methoddesc}
1015
1016\begin{methoddesc}[Connection]{connect_ex}{address}
1017Call the \method{connect_ex} method of the underlying socket and set up SSL on
1018the socket, using the Context object supplied to this Connection object at
1019creation. Note that if the \method{connect_ex} method of the socket doesn't
1020return 0, SSL won't be initialized.
1021\end{methoddesc}
1022
1023\begin{methoddesc}[Connection]{do_handshake}{}
1024Perform an SSL handshake (usually called after \method{renegotiate} or one of
1025\method{set_accept_state} or \method{set_accept_state}). This can raise the
1026same exceptions as \method{send} and \method{recv}.
1027\end{methoddesc}
1028
1029\begin{methoddesc}[Connection]{fileno}{}
1030Retrieve the file descriptor number for the underlying socket.
1031\end{methoddesc}
1032
1033\begin{methoddesc}[Connection]{listen}{backlog}
1034Call the \method{listen} method of the underlying socket.
1035\end{methoddesc}
1036
1037\begin{methoddesc}[Connection]{get_app_data}{}
1038Retrieve application data as set by \method{set_app_data}.
1039\end{methoddesc}
1040
1041\begin{methoddesc}[Connection]{get_cipher_list}{}
1042Retrieve the list of ciphers used by the Connection object. WARNING: This API
1043has changed. It used to take an optional parameter and just return a string,
1044but not it returns the entire list in one go.
1045\end{methoddesc}
1046
Ziga Seilnacht444e7cb2009-09-01 13:31:36 +02001047\begin{methoddesc}[Connection]{get_client_CA_list}{}
1048Retrieve the list of preferred client certificate issuers sent by the server
1049as \class{OpenSSL.crypto.X509Name} objects.
1050
1051If this is a client \class{Connection}, the list will be empty until the
1052connection with the server is established.
1053
1054If this is a server \class{Connection}, return the list of certificate
1055authorities that will be sent or has been sent to the client, as controlled
1056by this \class{Connection}'s \class{Context}.
1057
1058\versionadded{0.10}
1059\end{methoddesc}
1060
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001061\begin{methoddesc}[Connection]{get_context}{}
1062Retrieve the Context object associated with this Connection.
1063\end{methoddesc}
1064
1065\begin{methoddesc}[Connection]{get_peer_certificate}{}
1066Retrieve the other side's certificate (if any)
1067\end{methoddesc}
1068
1069\begin{methoddesc}[Connection]{getpeername}{}
1070Call the \method{getpeername} method of the underlying socket.
1071\end{methoddesc}
1072
1073\begin{methoddesc}[Connection]{getsockname}{}
1074Call the \method{getsockname} method of the underlying socket.
1075\end{methoddesc}
1076
1077\begin{methoddesc}[Connection]{getsockopt}{level, optname\optional{, buflen}}
1078Call the \method{getsockopt} method of the underlying socket.
1079\end{methoddesc}
1080
1081\begin{methoddesc}[Connection]{pending}{}
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001082Retrieve the number of bytes that can be safely read from the SSL buffer
1083(\emph{not} the underlying transport buffer).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001084\end{methoddesc}
1085
1086\begin{methoddesc}[Connection]{recv}{bufsize}
1087Receive data from the Connection. The return value is a string representing the
1088data received. The maximum amount of data to be received at once, is specified
1089by \var{bufsize}.
1090\end{methoddesc}
1091
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001092\begin{methoddesc}[Connection]{bio_write}{bytes}
1093If the Connection was created with a memory BIO, this method can be used to add
1094bytes to the read end of that memory BIO. The Connection can then read the
1095bytes (for example, in response to a call to \method{recv}).
1096\end{methoddesc}
1097
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001098\begin{methoddesc}[Connection]{renegotiate}{}
1099Renegotiate the SSL session. Call this if you wish to change cipher suites or
1100anything like that.
1101\end{methoddesc}
1102
1103\begin{methoddesc}[Connection]{send}{string}
1104Send the \var{string} data to the Connection.
1105\end{methoddesc}
1106
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001107\begin{methoddesc}[Connection]{bio_read}{bufsize}
1108If the Connection was created with a memory BIO, this method can be used to
1109read bytes from the write end of that memory BIO. Many Connection methods will
1110add bytes which must be read in this manner or the buffer will eventually fill
1111up and the Connection will be able to take no further actions.
1112\end{methoddesc}
1113
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001114\begin{methoddesc}[Connection]{sendall}{string}
1115Send all of the \var{string} data to the Connection. This calls \method{send}
1116repeatedly until all data is sent. If an error occurs, it's impossible to tell
1117how much data has been sent.
1118\end{methoddesc}
1119
1120\begin{methoddesc}[Connection]{set_accept_state}{}
1121Set the connection to work in server mode. The handshake will be handled
1122automatically by read/write.
1123\end{methoddesc}
1124
1125\begin{methoddesc}[Connection]{set_app_data}{data}
1126Associate \var{data} with this Connection object. \var{data} can be retrieved
1127later using the \method{get_app_data} method.
1128\end{methoddesc}
1129
1130\begin{methoddesc}[Connection]{set_connect_state}{}
1131Set the connection to work in client mode. The handshake will be handled
1132automatically by read/write.
1133\end{methoddesc}
1134
1135\begin{methoddesc}[Connection]{setblocking}{flag}
1136Call the \method{setblocking} method of the underlying socket.
1137\end{methoddesc}
1138
1139\begin{methoddesc}[Connection]{setsockopt}{level, optname, value}
1140Call the \method{setsockopt} method of the underlying socket.
1141\end{methoddesc}
1142
1143\begin{methoddesc}[Connection]{shutdown}{}
1144Send the shutdown message to the Connection. Returns true if the shutdown
1145message exchange is completed and false otherwise (in which case you call
1146\method{recv()} or \method{send()} when the connection becomes
1147readable/writeable.
1148\end{methoddesc}
1149
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001150\begin{methoddesc}[Connection]{get_shutdown}{}
1151Get the shutdown state of the Connection. Returns a bitvector of either or
1152both of \var{SENT_SHUTDOWN} and \var{RECEIVED_SHUTDOWN}.
1153\end{methoddesc}
1154
1155\begin{methoddesc}[Connection]{set_shutdown}{state}
1156Set the shutdown state of the Connection. \var{state} is a bitvector of
1157either or both of \var{SENT_SHUTDOWN} and \var{RECEIVED_SHUTDOWN}.
1158\end{methoddesc}
1159
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001160\begin{methoddesc}[Connection]{sock_shutdown}{how}
1161Call the \method{shutdown} method of the underlying socket.
1162\end{methoddesc}
1163
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001164\begin{methoddesc}[Connection]{bio_shutdown}{}
1165If the Connection was created with a memory BIO, this method can be used to
1166indicate that ``end of file'' has been reached on the read end of that memory
1167BIO.
1168\end{methoddesc}
1169
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001170\begin{methoddesc}[Connection]{state_string}{}
1171Retrieve a verbose string detailing the state of the Connection.
1172\end{methoddesc}
1173
Jean-Paul Calderonefd236f32009-05-03 19:45:07 -04001174\begin{methoddesc}[Connection]{client_random}{}
1175Retrieve the random value used with the client hello message.
1176\end{methoddesc}
1177
1178\begin{methoddesc}[Connection]{server_random}{}
1179Retrieve the random value used with the server hello message.
1180\end{methoddesc}
1181
1182\begin{methoddesc}[Connection]{master_key}{}
1183Retrieve the value of the master key for this session.
1184\end{methoddesc}
1185
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001186\begin{methoddesc}[Connection]{want_read}{}
1187Checks if more data has to be read from the transport layer to complete an
1188operation.
1189\end{methoddesc}
1190
1191\begin{methoddesc}[Connection]{want_write}{}
1192Checks if there is data to write to the transport layer to complete an
1193operation.
1194\end{methoddesc}
1195
1196
1197
1198\section{Internals \label{internals}}
1199
1200We ran into three main problems developing this: Exceptions, callbacks and
1201accessing socket methods. This is what this chapter is about.
1202
1203\subsection{Exceptions \label{exceptions}}
1204
1205We realized early that most of the exceptions would be raised by the I/O
1206functions of OpenSSL, so it felt natural to mimic OpenSSL's error code system,
1207translating them into Python exceptions. This naturally gives us the exceptions
1208\exception{SSL.ZeroReturnError}, \exception{SSL.WantReadError},
1209\exception{SSL.WantWriteError}, \exception{SSL.WantX509LookupError} and
1210\exception{SSL.SysCallError}.
1211
1212For more information about this, see section \ref{openssl-ssl}.
1213
1214
1215\subsection{Callbacks \label{callbacks}}
1216
1217There are a number of problems with callbacks. First of all, OpenSSL is written
1218as a C library, it's not meant to have Python callbacks, so a way around that
1219is needed. Another problem is thread support. A lot of the OpenSSL I/O
1220functions can block if the socket is in blocking mode, and then you want other
1221Python threads to be able to do other things. The real trouble is if you've
Jean-Paul Calderoneb7d6db22008-09-21 18:57:56 -04001222released the global CPython interpreter lock to do a potentially blocking
1223operation, and the operation calls a callback. Then we must take the GIL back,
1224since calling Python APIs without holding it is not allowed.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001225
1226There are two solutions to the first problem, both of which are necessary. The
1227first solution to use is if the C callback allows ''userdata'' to be passed to
1228it (an arbitrary pointer normally). This is great! We can set our Python
1229function object as the real userdata and emulate userdata for the Python
1230function in another way. The other solution can be used if an object with an
1231''app_data'' system always is passed to the callback. For example, the SSL
1232object in OpenSSL has app_data functions and in e.g. the verification
1233callbacks, you can retrieve the related SSL object. What we do is to set our
1234wrapper \class{Connection} object as app_data for the SSL object, and we can
1235easily find the Python callback.
1236
Jean-Paul Calderoneb7d6db22008-09-21 18:57:56 -04001237The other problem is solved using thread local variables. Whenever the GIL is
1238released before calling into an OpenSSL API, the PyThreadState pointer returned
1239by \cfunction{PyEval_SaveState} is stored in a global thread local variable
1240(using Python's own TLS API, \cfunction{PyThread_set_key_value}). When it is
1241necessary to re-acquire the GIL, either after the OpenSSL API returns or in a C
1242callback invoked by that OpenSSL API, the value of the thread local variable is
1243retrieved (\cfunction{PyThread_get_key_value}) and used to re-acquire the GIL.
1244This allows Python threads to execute while OpenSSL APIs are running and allows
1245use of any particular pyOpenSSL object from any Python thread, since there is
1246no per-thread state associated with any of these objects and since OpenSSL is
1247threadsafe (as long as properly initialized, as pyOpenSSL initializes it).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001248
1249
1250\subsection{Acessing Socket Methods \label{socket-methods}}
1251
1252We quickly saw the benefit of wrapping socket methods in the
1253\class{SSL.Connection} class, for an easy transition into using SSL. The
1254problem here is that the \module{socket} module lacks a C API, and all the
1255methods are declared static. One approach would be to have \module{OpenSSL} as
1256a submodule to the \module{socket} module, placing all the code in
1257\file{socketmodule.c}, but this is obviously not a good solution, since you
1258might not want to import tonnes of extra stuff you're not going to use when
1259importing the \module{socket} module. The other approach is to somehow get a
1260pointer to the method to be called, either the C function, or a callable Python
1261object. This is not really a good solution either, since there's a lot of
1262lookups involved.
1263
1264The way it works is that you have to supply a ``\class{socket}-like'' transport
1265object to the \class{SSL.Connection}. The only requirement of this object is
1266that it has a \method{fileno()} method that returns a file descriptor that's
1267valid at the C level (i.e. you can use the system calls read and write). If you
1268want to use the \method{connect()} or \method{accept()} methods of the
1269\class{SSL.Connection} object, the transport object has to supply such
1270methods too. Apart from them, any method lookups in the \class{SSL.Connection}
1271object that fail are passed on to the underlying transport object.
1272
1273Future changes might be to allow Python-level transport objects, that instead
1274of having \method{fileno()} methods, have \method{read()} and \method{write()}
1275methods, so more advanced features of Python can be used. This would probably
1276entail some sort of OpenSSL ``BIOs'', but converting Python strings back and
1277forth is expensive, so this shouldn't be used unless necessary. Other nice
1278things would be to be able to pass in different transport objects for reading
1279and writing, but then the \method{fileno()} method of \class{SSL.Connection}
1280becomes virtually useless. Also, should the method resolution be used on the
1281read-transport or the write-transport?
1282
1283
1284\end{document}