blob: 575eb8ed911715fc9037aabe55e24ae7085ab4c0 [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001\documentclass{howto}
2
3\title{Python OpenSSL Manual}
4
Jean-Paul Calderone3ed4a182011-04-11 19:56:31 -04005\release{0.12}
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
Rick Dean536ba022009-07-24 23:57:27 -0500210\begin{classdesc}{CRL}{}
211A class representing Certifcate Revocation List objects.
212\end{classdesc}
213
214\begin{classdesc}{Revoked}{}
215A class representing Revocation objects of CRL.
216\end{classdesc}
217
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500218\begin{datadesc}{FILETYPE_PEM}
219\dataline{FILETYPE_ASN1}
220File type constants.
221\end{datadesc}
222
223\begin{datadesc}{TYPE_RSA}
224\dataline{TYPE_DSA}
225Key type constants.
226\end{datadesc}
227
228\begin{excdesc}{Error}
229Generic exception used in the \module{crypto} module.
230\end{excdesc}
231
232\begin{funcdesc}{dump_certificate}{type, cert}
233Dump the certificate \var{cert} into a buffer string encoded with the type
234\var{type}.
235\end{funcdesc}
236
237\begin{funcdesc}{dump_certificate_request}{type, req}
238Dump the certificate request \var{req} into a buffer string encoded with the
239type \var{type}.
240\end{funcdesc}
241
242\begin{funcdesc}{dump_privatekey}{type, pkey\optional{, cipher, passphrase}}
243Dump the private key \var{pkey} into a buffer string encoded with the type
244\var{type}, optionally (if \var{type} is \constant{FILETYPE_PEM}) encrypting it
245using \var{cipher} and \var{passphrase}.
246
247\var{passphrase} must be either a string or a callback for providing the
248pass phrase.
249\end{funcdesc}
250
251\begin{funcdesc}{load_certificate}{type, buffer}
252Load a certificate (X509) from the string \var{buffer} encoded with the
253type \var{type}.
254\end{funcdesc}
255
256\begin{funcdesc}{load_certificate_request}{type, buffer}
257Load a certificate request (X509Req) from the string \var{buffer} encoded with
258the type \var{type}.
259\end{funcdesc}
260
261\begin{funcdesc}{load_privatekey}{type, buffer\optional{, passphrase}}
262Load a private key (PKey) from the string \var{buffer} encoded with
263the type \var{type} (must be one of \constant{FILETYPE_PEM} and
264\constant{FILETYPE_ASN1}).
265
266\var{passphrase} must be either a string or a callback for providing the
267pass phrase.
268\end{funcdesc}
269
Rick Dean536ba022009-07-24 23:57:27 -0500270\begin{funcdesc}{load_crl}{type, buffer}
271Load Certificate Revocation List (CRL) data from a string \var{buffer}.
Jean-Paul Calderone49de5932011-04-06 22:01:20 -0400272\var{buffer} encoded with the type \var{type}. The type \var{type}
Rick Dean536ba022009-07-24 23:57:27 -0500273must either \constant{FILETYPE_PEM} or \constant{FILETYPE_ASN1}).
274\end{funcdesc}
275
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500276\begin{funcdesc}{load_pkcs7_data}{type, buffer}
277Load pkcs7 data from the string \var{buffer} encoded with the type \var{type}.
278\end{funcdesc}
279
280\begin{funcdesc}{load_pkcs12}{buffer\optional{, passphrase}}
281Load pkcs12 data from the string \var{buffer}. If the pkcs12 structure is
Rick Deanf94096c2009-07-18 14:23:06 -0500282encrypted, a \var{passphrase} must be included. The MAC is always
Jean-Paul Calderonee7901d72009-07-24 18:21:26 -0400283checked and thus required.
Rick Dean42d69e12009-07-20 11:36:08 -0500284
285See also the man page for the C function \function{PKCS12_parse}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500286\end{funcdesc}
287
Jean-Paul Calderone6f256782010-06-22 09:55:53 -0400288\begin{funcdesc}{sign}{key, data, digest}
289Sign a data string using the given key and message digest.
290
291\var{key} is a \code{PKey} instance. \var{data} is a \code{str} instance.
292\var{digest} is a \code{str} naming a supported message digest type, for example
293\code{``sha1''}.
Jean-Paul Calderone47136d92010-10-07 22:33:46 -0400294\versionadded{0.11}
Jean-Paul Calderone6f256782010-06-22 09:55:53 -0400295\end{funcdesc}
296
297\begin{funcdesc}{verify}{certificate, signature, data, digest}
298Verify the signature for a data string.
299
300\var{certificate} is a \code{X509} instance corresponding to the private key
301which generated the signature. \var{signature} is a \var{str} instance giving
302the signature itself. \var{data} is a \var{str} instance giving the data to
Jean-Paul Calderonef41d6172010-06-22 09:57:47 -0400303which the signature applies. \var{digest} is a \var{str} instance naming the
Jean-Paul Calderone6f256782010-06-22 09:55:53 -0400304message digest type of the signature, for example \code{``sha1''}.
Jean-Paul Calderone47136d92010-10-07 22:33:46 -0400305\versionadded{0.11}
Jean-Paul Calderone6f256782010-06-22 09:55:53 -0400306\end{funcdesc}
307
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500308\subsubsection{X509 objects \label{openssl-x509}}
309
310X509 objects have the following methods:
311
312\begin{methoddesc}[X509]{get_issuer}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500313Return an X509Name object representing the issuer of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500314\end{methoddesc}
315
316\begin{methoddesc}[X509]{get_pubkey}{}
317Return a PKey object representing the public key of the certificate.
318\end{methoddesc}
319
320\begin{methoddesc}[X509]{get_serial_number}{}
321Return the certificate serial number.
322\end{methoddesc}
323
Jean-Paul Calderone5d190522011-05-17 15:43:51 -0400324\begin{methoddesc}[X509]{get_signature_algorithm}{}
325Return the signature algorithem used in the certificate.
326\end{methoddesc}
327
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500328\begin{methoddesc}[X509]{get_subject}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500329Return an X509Name object representing the subject of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500330\end{methoddesc}
331
332\begin{methoddesc}[X509]{get_version}{}
333Return the certificate version.
334\end{methoddesc}
335
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400336\begin{methoddesc}[X509]{get_notBefore}{}
337Return a string giving the time before which the certificate is not valid. The
338string is formatted as an ASN1 GENERALIZEDTIME:
339\begin{verbatim}
340 YYYYMMDDhhmmssZ
341 YYYYMMDDhhmmss+hhmm
342 YYYYMMDDhhmmss-hhmm
343\end{verbatim}
Jean-Paul Calderonee0615b52008-03-09 21:44:46 -0400344If no value exists for this field, \code{None} is returned.
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400345\end{methoddesc}
346
347\begin{methoddesc}[X509]{get_notAfter}{}
348Return a string giving the time after which the certificate is not valid. The
349string is formatted as an ASN1 GENERALIZEDTIME:
350\begin{verbatim}
351 YYYYMMDDhhmmssZ
352 YYYYMMDDhhmmss+hhmm
353 YYYYMMDDhhmmss-hhmm
354\end{verbatim}
Jean-Paul Calderonee0615b52008-03-09 21:44:46 -0400355If no value exists for this field, \code{None} is returned.
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400356\end{methoddesc}
357
358\begin{methoddesc}[X509]{set_notBefore}{when}
359Change the time before which the certificate is not valid. \var{when} is a
360string formatted as an ASN1 GENERALIZEDTIME:
361\begin{verbatim}
362 YYYYMMDDhhmmssZ
363 YYYYMMDDhhmmss+hhmm
364 YYYYMMDDhhmmss-hhmm
365\end{verbatim}
366\end{methoddesc}
367
368\begin{methoddesc}[X509]{set_notAfter}{when}
369Change the time after which the certificate is not valid. \var{when} is a
370string formatted as an ASN1 GENERALIZEDTIME:
371\begin{verbatim}
372 YYYYMMDDhhmmssZ
373 YYYYMMDDhhmmss+hhmm
374 YYYYMMDDhhmmss-hhmm
375\end{verbatim}
376\end{methoddesc}
377
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500378\begin{methoddesc}[X509]{gmtime_adj_notBefore}{time}
379Adjust the timestamp (in GMT) when the certificate starts being valid.
380\end{methoddesc}
381
382\begin{methoddesc}[X509]{gmtime_adj_notAfter}{time}
383Adjust the timestamp (in GMT) when the certificate stops being valid.
384\end{methoddesc}
385
386\begin{methoddesc}[X509]{has_expired}{}
387Checks the certificate's time stamp against current time. Returns true if the
388certificate has expired and false otherwise.
389\end{methoddesc}
390
391\begin{methoddesc}[X509]{set_issuer}{issuer}
392Set the issuer of the certificate to \var{issuer}.
393\end{methoddesc}
394
395\begin{methoddesc}[X509]{set_pubkey}{pkey}
396Set the public key of the certificate to \var{pkey}.
397\end{methoddesc}
398
399\begin{methoddesc}[X509]{set_serial_number}{serialno}
400Set the serial number of the certificate to \var{serialno}.
401\end{methoddesc}
402
403\begin{methoddesc}[X509]{set_subject}{subject}
404Set the subject of the certificate to \var{subject}.
405\end{methoddesc}
406
407\begin{methoddesc}[X509]{set_version}{version}
408Set the certificate version to \var{version}.
409\end{methoddesc}
410
411\begin{methoddesc}[X509]{sign}{pkey, digest}
412Sign the certificate, using the key \var{pkey} and the message digest algorithm
413identified by the string \var{digest}.
414\end{methoddesc}
415
416\begin{methoddesc}[X509]{subject_name_hash}{}
417Return the hash of the certificate subject.
418\end{methoddesc}
419
420\begin{methoddesc}[X509]{digest}{digest_name}
421Return a digest of the certificate, using the \var{digest_name} method.
Jean-Paul Calderoneb6f0d602008-12-28 21:20:01 -0500422\var{digest_name} must be a string describing a digest algorithm supported
423by OpenSSL (by EVP_get_digestbyname, specifically). For example,
424\constant{"md5"} or \constant{"sha1"}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500425\end{methoddesc}
426
427\begin{methoddesc}[X509]{add_extensions}{extensions}
428Add the extensions in the sequence \var{extensions} to the certificate.
429\end{methoddesc}
430
Jean-Paul Calderone1eba6a82011-04-06 21:57:09 -0400431\begin{methoddesc}[X509]{get_extension_count}{}
432Return the number of extensions on this certificate.
433\versionadded{0.12}
434\end{methoddesc}
435
436\begin{methoddesc}[X509]{get_extension}{index}
437Retrieve the extension on this certificate at the given index.
438
439Extensions on a certificate are kept in order. The index parameter selects
440which extension will be returned. The returned object will be an X509Extension
441instance.
442\versionadded{0.12}
Jean-Paul Calderone49de5932011-04-06 22:01:20 -0400443\end{methoddesc}
Jean-Paul Calderone1eba6a82011-04-06 21:57:09 -0400444
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500445\subsubsection{X509Name objects \label{openssl-x509name}}
446
Jean-Paul Calderone2dd8ff52008-03-24 17:43:58 -0400447X509Name objects have the following methods:
448
449\begin{methoddesc}[X509Name]{hash}{}
450Return an integer giving the first four bytes of the MD5 digest of the DER
451representation of the name.
452\end{methoddesc}
453
Jean-Paul Calderonea6edbf82008-03-25 15:19:11 -0400454\begin{methoddesc}[X509Name]{der}{}
455Return a string giving the DER representation of the name.
456\end{methoddesc}
457
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400458\begin{methoddesc}[X509Name]{get_components}{}
459Return a list of two-tuples of strings giving the components of the name.
460\end{methoddesc}
461
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500462X509Name objects have the following members:
463
464\begin{memberdesc}[X509Name]{countryName}
465The country of the entity. \code{C} may be used as an alias for
466\code{countryName}.
467\end{memberdesc}
468
469\begin{memberdesc}[X509Name]{stateOrProvinceName}
470The state or province of the entity. \code{ST} may be used as an alias for
471\code{stateOrProvinceName}·
472\end{memberdesc}
473
474\begin{memberdesc}[X509Name]{localityName}
475The locality of the entity. \code{L} may be used as an alias for
476\code{localityName}.
477\end{memberdesc}
478
479\begin{memberdesc}[X509Name]{organizationName}
480The organization name of the entity. \code{O} may be used as an alias for
481\code{organizationName}.
482\end{memberdesc}
483
484\begin{memberdesc}[X509Name]{organizationalUnitName}
485The organizational unit of the entity. \code{OU} may be used as an alias for
486\code{organizationalUnitName}.
487\end{memberdesc}
488
489\begin{memberdesc}[X509Name]{commonName}
490The common name of the entity. \code{CN} may be used as an alias for
491\code{commonName}.
492\end{memberdesc}
493
494\begin{memberdesc}[X509Name]{emailAddress}
495The e-mail address of the entity.
496\end{memberdesc}
497
498\subsubsection{X509Req objects \label{openssl-x509req}}
499
500X509Req objects have the following methods:
501
502\begin{methoddesc}[X509Req]{get_pubkey}{}
503Return a PKey object representing the public key of the certificate request.
504\end{methoddesc}
505
506\begin{methoddesc}[X509Req]{get_subject}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500507Return an X509Name object representing the subject of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500508\end{methoddesc}
509
510\begin{methoddesc}[X509Req]{set_pubkey}{pkey}
511Set the public key of the certificate request to \var{pkey}.
512\end{methoddesc}
513
514\begin{methoddesc}[X509Req]{sign}{pkey, digest}
515Sign the certificate request, using the key \var{pkey} and the message digest
516algorithm identified by the string \var{digest}.
517\end{methoddesc}
518
519\begin{methoddesc}[X509Req]{verify}{pkey}
520Verify a certificate request using the public key \var{pkey}.
521\end{methoddesc}
522
Jean-Paul Calderone8dd19b82008-12-28 20:41:16 -0500523\begin{methoddesc}[X509Req]{set_version}{version}
524Set the version (RFC 2459, 4.1.2.1) of the certificate request to
525\var{version}.
526\end{methoddesc}
527
528\begin{methoddesc}[X509Req]{get_version}{}
529Get the version (RFC 2459, 4.1.2.1) of the certificate request.
530\end{methoddesc}
531
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500532\subsubsection{X509Store objects \label{openssl-x509store}}
533
534The X509Store object has currently just one method:
535
536\begin{methoddesc}[X509Store]{add_cert}{cert}
537Add the certificate \var{cert} to the certificate store.
538\end{methoddesc}
539
540\subsubsection{PKey objects \label{openssl-pkey}}
541
542The PKey object has the following methods:
543
544\begin{methoddesc}[PKey]{bits}{}
545Return the number of bits of the key.
546\end{methoddesc}
547
548\begin{methoddesc}[PKey]{generate_key}{type, bits}
549Generate a public/private key pair of the type \var{type} (one of
550\constant{TYPE_RSA} and \constant{TYPE_DSA}) with the size \var{bits}.
551\end{methoddesc}
552
553\begin{methoddesc}[PKey]{type}{}
554Return the type of the key.
555\end{methoddesc}
556
557\subsubsection{PKCS7 objects \label{openssl-pkcs7}}
558
559PKCS7 objects have the following methods:
560
561\begin{methoddesc}[PKCS7]{type_is_signed}{}
562FIXME
563\end{methoddesc}
564
565\begin{methoddesc}[PKCS7]{type_is_enveloped}{}
566FIXME
567\end{methoddesc}
568
569\begin{methoddesc}[PKCS7]{type_is_signedAndEnveloped}{}
570FIXME
571\end{methoddesc}
572
573\begin{methoddesc}[PKCS7]{type_is_data}{}
574FIXME
575\end{methoddesc}
576
577\begin{methoddesc}[PKCS7]{get_type_name}{}
578Get the type name of the PKCS7.
579\end{methoddesc}
580
581\subsubsection{PKCS12 objects \label{openssl-pkcs12}}
582
583PKCS12 objects have the following methods:
584
Rick Dean42d69e12009-07-20 11:36:08 -0500585\begin{methoddesc}[PKCS12]{export}{\optional{passphrase=None}\optional{, iter=2048}\optional{, maciter=1}}
Rick Deane182f482009-07-17 14:49:48 -0500586Returns a PKCS12 object as a string.
587
588The optional \var{passphrase} must be a string not a callback.
589
590See also the man page for the C function \function{PKCS12_create}.
591\end{methoddesc}
592
593\begin{methoddesc}[PKCS12]{get_ca_certificates}{}
594Return CA certificates within the PKCS12 object as a tuple. Returns
595\constant{None} if no CA certificates are present.
596\end{methoddesc}
597
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500598\begin{methoddesc}[PKCS12]{get_certificate}{}
599Return certificate portion of the PKCS12 structure.
600\end{methoddesc}
601
Rick Dean42d69e12009-07-20 11:36:08 -0500602\begin{methoddesc}[PKCS12]{get_friendlyname}{}
603Return friendlyName portion of the PKCS12 structure.
604\end{methoddesc}
605
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500606\begin{methoddesc}[PKCS12]{get_privatekey}{}
607Return private key portion of the PKCS12 structure
608\end{methoddesc}
609
Rick Deane182f482009-07-17 14:49:48 -0500610\begin{methoddesc}[PKCS12]{set_ca_certificates}{cacerts}
611Replace or set the CA certificates within the PKCS12 object with the sequence \var{cacerts}.
612
613Set \var{cacerts} to \constant{None} to remove all CA certificates.
614\end{methoddesc}
615
616\begin{methoddesc}[PKCS12]{set_certificate}{cert}
617Replace or set the certificate portion of the PKCS12 structure.
618\end{methoddesc}
619
Rick Dean42d69e12009-07-20 11:36:08 -0500620\begin{methoddesc}[PKCS12]{set_friendlyname}{name}
621Replace or set the friendlyName portion of the PKCS12 structure.
622\end{methoddesc}
623
Rick Deane182f482009-07-17 14:49:48 -0500624\begin{methoddesc}[PKCS12]{set_privatekey}{pkey}
625Replace or set private key portion of the PKCS12 structure
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500626\end{methoddesc}
627
628\subsubsection{X509Extension objects \label{openssl-509ext}}
629
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -0500630X509Extension objects have several methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500631
632\begin{methoddesc}[X509Extension]{get_critical}{}
633Return the critical field of the extension object.
634\end{methoddesc}
635
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -0500636\begin{methoddesc}[X509Extension]{get_short_name}{}
Jean-Paul Calderone0d53d0b2011-04-15 12:25:10 -0400637Retrieve the short descriptive name for this extension.
638
639The result is a byte string like \code{``basicConstraints''}.
640\versionadded{0.12}
641\end{methoddesc}
642
643\begin{methoddesc}[X509Extension]{get_data}{}
644Retrieve the data for this extension.
645
646The result is the ASN.1 encoded form of the extension data as a byte string.
647\versionadded{0.12}
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -0500648\end{methoddesc}
649
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500650\subsubsection{NetscapeSPKI objects \label{openssl-netscape-spki}}
651
652NetscapeSPKI objects have the following methods:
653
654\begin{methoddesc}[NetscapeSPKI]{b64_encode}{}
655Return a base64-encoded string representation of the object.
656\end{methoddesc}
657
658\begin{methoddesc}[NetscapeSPKI]{get_pubkey}{}
659Return the public key of object.
660\end{methoddesc}
661
662\begin{methoddesc}[NetscapeSPKI]{set_pubkey}{key}
663Set the public key of the object to \var{key}.
664\end{methoddesc}
665
666\begin{methoddesc}[NetscapeSPKI]{sign}{key, digest_name}
Jean-Paul Calderoneb6f0d602008-12-28 21:20:01 -0500667Sign the NetscapeSPKI object using the given \var{key} and
668\var{digest_name}. \var{digest_name} must be a string describing a digest
669algorithm supported by OpenSSL (by EVP_get_digestbyname, specifically). For
670example, \constant{"md5"} or \constant{"sha1"}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500671\end{methoddesc}
672
673\begin{methoddesc}[NetscapeSPKI]{verify}{key}
674Verify the NetscapeSPKI object using the given \var{key}.
675\end{methoddesc}
676
Rick Dean536ba022009-07-24 23:57:27 -0500677\subsubsection{CRL objects \label{crl}}
678
679CRL objects have the following methods:
680
681\begin{methoddesc}[CRL]{add_revoked}{revoked}
682Add a Revoked object to the CRL, by value not reference.
683\end{methoddesc}
684
685\begin{methoddesc}[CRL]{export}{cert, key\optional{, type=FILETYPE_PEM}\optional{, days=100}}
686Use \var{cert} and \var{key} to sign the CRL and return the CRL as a string.
687\var{days} is the number of days before the next CRL is due.
688\end{methoddesc}
689
690\begin{methoddesc}[CRL]{get_revoked}{}
691Return a tuple of Revoked objects, by value not reference.
692\end{methoddesc}
693
694\subsubsection{Revoked objects \label{revoked}}
695
696Revoked objects have the following methods:
697
Rick Dean6385faf2009-07-26 00:07:47 -0500698\begin{methoddesc}[Revoked]{all_reasons}{}
699Return a list of all supported reasons.
700\end{methoddesc}
701
702\begin{methoddesc}[Revoked]{get_reason}{}
703Return the revocation reason as a str. Can be
704None, which differs from "Unspecified".
705\end{methoddesc}
706
Rick Dean536ba022009-07-24 23:57:27 -0500707\begin{methoddesc}[Revoked]{get_rev_date}{}
708Return the revocation date as a str.
709The string is formatted as an ASN1 GENERALIZEDTIME.
710\end{methoddesc}
711
712\begin{methoddesc}[Revoked]{get_serial}{}
713Return a str containing a hex number of the serial of the revoked certificate.
714\end{methoddesc}
715
Rick Dean6385faf2009-07-26 00:07:47 -0500716\begin{methoddesc}[Revoked]{set_reason}{reason}
717Set the revocation reason. \var{reason} must
718be None or a string, but the values are limited.
719Spaces and case are ignored. See \method{all_reasons}.
720\end{methoddesc}
721
Rick Dean536ba022009-07-24 23:57:27 -0500722\begin{methoddesc}[Revoked]{set_rev_date}{date}
723Set the revocation date.
724The string is formatted as an ASN1 GENERALIZEDTIME.
725\end{methoddesc}
726
727\begin{methoddesc}[Revoked]{set_serial}{serial}
728\var{serial} is a string containing a hex number of the serial of the revoked certificate.
729\end{methoddesc}
730
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500731
732% % % rand module
733
734\subsection{\module{rand} --- An interface to the OpenSSL pseudo random number generator \label{openssl-rand}}
735
736\declaremodule{extension}{rand}
737\modulesynopsis{An interface to the OpenSSL pseudo random number generator}
738
739This module handles the OpenSSL pseudo random number generator (PRNG) and
740declares the following:
741
742\begin{funcdesc}{add}{string, entropy}
743Mix bytes from \var{string} into the PRNG state. The \var{entropy} argument is
744(the lower bound of) an estimate of how much randomness is contained in
745\var{string}, measured in bytes. For more information, see e.g. \rfc{1750}.
746\end{funcdesc}
747
Rick Dean4fd5a4e2009-07-08 12:06:10 -0500748\begin{funcdesc}{bytes}{num_bytes}
Jean-Paul Calderonea45e2b92009-07-08 13:29:58 -0400749Get some random bytes from the PRNG as a string.
750
751This is a wrapper for the C function \function{RAND_bytes}.
Rick Dean4fd5a4e2009-07-08 12:06:10 -0500752\end{funcdesc}
753
Rick Dean433dc642009-07-07 13:11:55 -0500754\begin{funcdesc}{cleanup}{}
Jean-Paul Calderone427c0b32009-07-07 15:43:27 -0400755Erase the memory used by the PRNG.
756
757This is a wrapper for the C function \function{RAND_cleanup}.
Rick Dean433dc642009-07-07 13:11:55 -0500758\end{funcdesc}
759
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500760\begin{funcdesc}{egd}{path\optional{, bytes}}
761Query the Entropy Gathering Daemon\footnote{See
762\url{http://www.lothar.com/tech/crypto/}} on socket \var{path} for \var{bytes}
763bytes of random data and and uses \function{add} to seed the PRNG. The default
764value of \var{bytes} is 255.
765\end{funcdesc}
766
767\begin{funcdesc}{load_file}{path\optional{, bytes}}
768Read \var{bytes} bytes (or all of it, if \var{bytes} is negative) of data from
769the file \var{path} to seed the PRNG. The default value of \var{bytes} is -1.
770\end{funcdesc}
771
772\begin{funcdesc}{screen}{}
773Add the current contents of the screen to the PRNG state.
774Availability: Windows.
775\end{funcdesc}
776
777\begin{funcdesc}{seed}{string}
778This is equivalent to calling \function{add} with \var{entropy} as the length
779of the string.
780\end{funcdesc}
781
782\begin{funcdesc}{status}{}
783Returns true if the PRNG has been seeded with enough data, and false otherwise.
784\end{funcdesc}
785
786\begin{funcdesc}{write_file}{path}
787Write a number of random bytes (currently 1024) to the file \var{path}. This
788file can then be used with \function{load_file} to seed the PRNG again.
789\end{funcdesc}
790
Rick Deanfc69c812009-07-08 11:03:47 -0500791\begin{excdesc}{Error}
Ziga Seilnacht444e7cb2009-09-01 13:31:36 +0200792If the current RAND method supports any errors, this is raised when needed.
Rick Deanfc69c812009-07-08 11:03:47 -0500793The default method does not raise this when the entropy pool is depleted.
794
795Whenever this exception is raised directly, it has a list of error messages
796from the OpenSSL error queue, where each item is a tuple \code{(\var{lib},
797\var{function}, \var{reason})}. Here \var{lib}, \var{function} and \var{reason}
798are all strings, describing where and what the problem is. See \manpage{err}{3}
799for more information.
800\end{excdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500801
802
803% % % SSL module
804
805\subsection{\module{SSL} --- An interface to the SSL-specific parts of OpenSSL \label{openssl-ssl}}
806
807\declaremodule{extension}{SSL}
808\modulesynopsis{An interface to the SSL-specific parts of OpenSSL}
809
810This module handles things specific to SSL. There are two objects defined:
811Context, Connection.
812
813\begin{datadesc}{SSLv2_METHOD}
814\dataline{SSLv3_METHOD}
815\dataline{SSLv23_METHOD}
816\dataline{TLSv1_METHOD}
817These constants represent the different SSL methods to use when creating a
818context object.
819\end{datadesc}
820
821\begin{datadesc}{VERIFY_NONE}
822\dataline{VERIFY_PEER}
823\dataline{VERIFY_FAIL_IF_NO_PEER_CERT}
824These constants represent the verification mode used by the Context
825object's \method{set_verify} method.
826\end{datadesc}
827
828\begin{datadesc}{FILETYPE_PEM}
829\dataline{FILETYPE_ASN1}
830File type constants used with the \method{use_certificate_file} and
831\method{use_privatekey_file} methods of Context objects.
832\end{datadesc}
833
834\begin{datadesc}{OP_SINGLE_DH_USE}
835\dataline{OP_EPHEMERAL_RSA}
836\dataline{OP_NO_SSLv2}
837\dataline{OP_NO_SSLv3}
838\dataline{OP_NO_TLSv1}
839Constants used with \method{set_options} of Context objects.
840\constant{OP_SINGLE_DH_USE} means to always create a new key when using ephemeral
841Diffie-Hellman. \constant{OP_EPHEMERAL_RSA} means to always use ephemeral RSA keys
842when doing RSA operations. \constant{OP_NO_SSLv2}, \constant{OP_NO_SSLv3} and
843\constant{OP_NO_TLSv1} means to disable those specific protocols. This is
844interesting if you're using e.g. \constant{SSLv23_METHOD} to get an SSLv2-compatible
845handshake, but don't want to use SSLv2.
846\end{datadesc}
847
Jean-Paul Calderone2309cd62011-04-15 12:17:53 -0400848\begin{datadesc}{SSLEAY_VERSION}
849\dataline{SSLEAY_CFLAGS}
850\dataline{SSLEAY_BUILT_ON}
851\dataline{SSLEAY_PLATFORM}
852\dataline{SSLEAY_DIR}
853Constants used with \method{SSLeay_version} to specify what OpenSSL version
854information to retrieve. See the man page for the \function{SSLeay_version} C
855API for details.
856\end{datadesc}
857
858\begin{datadesc}{OPENSSL_VERSION_NUMBER}
859An integer giving the version number of the OpenSSL library used to build this
860version of pyOpenSSL. See the man page for the \function{SSLeay_version} C API
861for details.
862\end{datadesc}
863
864\begin{funcdesc}{SSLeay_version}{type}
865Retrieve a string describing some aspect of the underlying OpenSSL version. The
866type passed in should be one of the \constant{SSLEAY_*} constants defined in
867this module.
868\end{funcdesc}
869
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500870\begin{datadesc}{ContextType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400871See \class{Context}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500872\end{datadesc}
873
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400874\begin{classdesc}{Context}{method}
875A class representing SSL contexts. Contexts define the parameters of one or
876more SSL connections.
877
878\var{method} should be \constant{SSLv2_METHOD}, \constant{SSLv3_METHOD},
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500879\constant{SSLv23_METHOD} or \constant{TLSv1_METHOD}.
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400880\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500881
882\begin{datadesc}{ConnectionType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400883See \class{Connection}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500884\end{datadesc}
885
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400886\begin{classdesc}{Connection}{context, socket}
887A class representing SSL connections.
888
889\var{context} should be an instance of \class{Context} and \var{socket}
890should be a socket \footnote{Actually, all that is required is an object
891that \emph{behaves} like a socket, you could even use files, even though
892it'd be tricky to get the handshakes right!} object. \var{socket} may be
893\var{None}; in this case, the Connection is created with a memory BIO: see
894the \method{bio_read}, \method{bio_write}, and \method{bio_shutdown}
895methods.
896\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500897
898\begin{excdesc}{Error}
899This exception is used as a base class for the other SSL-related
900exceptions, but may also be raised directly.
901
902Whenever this exception is raised directly, it has a list of error messages
903from the OpenSSL error queue, where each item is a tuple \code{(\var{lib},
904\var{function}, \var{reason})}. Here \var{lib}, \var{function} and \var{reason}
905are all strings, describing where and what the problem is. See \manpage{err}{3}
906for more information.
907\end{excdesc}
908
909\begin{excdesc}{ZeroReturnError}
910This exception matches the error return code \code{SSL_ERROR_ZERO_RETURN}, and
911is raised when the SSL Connection has been closed. In SSL 3.0 and TLS 1.0, this
912only occurs if a closure alert has occurred in the protocol, i.e. the
913connection has been closed cleanly. Note that this does not necessarily
914mean that the transport layer (e.g. a socket) has been closed.
915
916It may seem a little strange that this is an exception, but it does match an
917\code{SSL_ERROR} code, and is very convenient.
918\end{excdesc}
919
920\begin{excdesc}{WantReadError}
921The operation did not complete; the same I/O method should be called again
922later, with the same arguments. Any I/O method can lead to this since new
923handshakes can occur at any time.
Rick Dean71fa0962009-07-09 23:56:39 -0500924
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400925The wanted read is for \emph{dirty} data sent over the network, not the
926\emph{clean} data inside the tunnel. For a socket based SSL connection,
927\emph{read} means data coming at us over the network. Until that read
928succeeds, the attempted \method{OpenSSL.SSL.Connection.recv},
Rick Dean71fa0962009-07-09 23:56:39 -0500929\method{OpenSSL.SSL.Connection.send}, or
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400930\method{OpenSSL.SSL.Connection.do_handshake} is prevented or incomplete. You
931probably want to \method{select()} on the socket before trying again.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500932\end{excdesc}
933
934\begin{excdesc}{WantWriteError}
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400935See \exception{WantReadError}. The socket send buffer may be too full to
Rick Dean71fa0962009-07-09 23:56:39 -0500936write more data.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500937\end{excdesc}
938
939\begin{excdesc}{WantX509LookupError}
940The operation did not complete because an application callback has asked to be
941called again. The I/O method should be called again later, with the same
942arguments. Note: This won't occur in this version, as there are no such
943callbacks in this version.
944\end{excdesc}
945
946\begin{excdesc}{SysCallError}
947The \exception{SysCallError} occurs when there's an I/O error and OpenSSL's
948error queue does not contain any information. This can mean two things: An
949error in the transport protocol, or an end of file that violates the protocol.
950The parameter to the exception is always a pair \code{(\var{errnum},
951\var{errstr})}.
952\end{excdesc}
953
954
955\subsubsection{Context objects \label{openssl-context}}
956
957Context objects have the following methods:
958
959\begin{methoddesc}[Context]{check_privatekey}{}
960Check if the private key (loaded with \method{use_privatekey\optional{_file}})
961matches the certificate (loaded with \method{use_certificate\optional{_file}}).
Jean-Paul Calderonef05fbbe2008-03-06 21:52:35 -0500962Returns \code{None} if they match, raises \exception{Error} otherwise.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500963\end{methoddesc}
964
965\begin{methoddesc}[Context]{get_app_data}{}
966Retrieve application data as set by \method{set_app_data}.
967\end{methoddesc}
968
969\begin{methoddesc}[Context]{get_cert_store}{}
970Retrieve the certificate store (a X509Store object) that the context uses.
971This can be used to add "trusted" certificates without using the.
972\method{load_verify_locations()} method.
973\end{methoddesc}
974
975\begin{methoddesc}[Context]{get_timeout}{}
976Retrieve session timeout, as set by \method{set_timeout}. The default is 300
977seconds.
978\end{methoddesc}
979
980\begin{methoddesc}[Context]{get_verify_depth}{}
981Retrieve the Context object's verify depth, as set by
982\method{set_verify_depth}.
983\end{methoddesc}
984
985\begin{methoddesc}[Context]{get_verify_mode}{}
Jean-Paul Calderoneae4238d2008-12-28 21:13:50 -0500986Retrieve the Context object's verify mode, as set by \method{set_verify}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500987\end{methoddesc}
988
989\begin{methoddesc}[Context]{load_client_ca}{pemfile}
990Read a file with PEM-formatted certificates that will be sent to the client
991when requesting a client certificate.
992\end{methoddesc}
993
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200994\begin{methoddesc}[Context]{set_client_ca_list}{certificate_authorities}
Ziga Seilnacht9d831fb2009-10-23 09:19:57 +0200995Replace the current list of preferred certificate signers that would be
996sent to the client when requesting a client certificate with the
997\var{certificate_authorities} sequence of \class{OpenSSL.crypto.X509Name}s.
Ziga Seilnacht444e7cb2009-09-01 13:31:36 +0200998
999\versionadded{0.10}
1000\end{methoddesc}
1001
Ziga Seilnachtf93bf102009-10-23 09:51:07 +02001002\begin{methoddesc}[Context]{add_client_ca}{certificate_authority}
Ziga Seilnacht444e7cb2009-09-01 13:31:36 +02001003Extract a \class{OpenSSL.crypto.X509Name} from the \var{certificate_authority}
1004\class{OpenSSL.crypto.X509} certificate and add it to the list of preferred
1005certificate signers sent to the client when requesting a client certificate.
Ziga Seilnacht444e7cb2009-09-01 13:31:36 +02001006
1007\versionadded{0.10}
1008\end{methoddesc}
1009
Jean-Paul Calderone5601c242008-09-07 21:06:52 -04001010\begin{methoddesc}[Context]{load_verify_locations}{pemfile, capath}
1011Specify where CA certificates for verification purposes are located. These
1012are trusted certificates. Note that the certificates have to be in PEM
1013format. If capath is passed, it must be a directory prepared using the
1014\code{c_rehash} tool included with OpenSSL. Either, but not both, of
1015\var{pemfile} or \var{capath} may be \code{None}.
1016\end{methoddesc}
1017
1018\begin{methoddesc}[Context]{set_default_verify_paths}{}
1019Specify that the platform provided CA certificates are to be used for
Jean-Paul Calderone1d287e52009-03-07 09:09:07 -05001020verification purposes. This method may not work properly on OS X.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001021\end{methoddesc}
1022
1023\begin{methoddesc}[Context]{load_tmp_dh}{dhfile}
1024Load parameters for Ephemeral Diffie-Hellman from \var{dhfile}.
1025\end{methoddesc}
1026
1027\begin{methoddesc}[Context]{set_app_data}{data}
1028Associate \var{data} with this Context object. \var{data} can be retrieved
1029later using the \method{get_app_data} method.
1030\end{methoddesc}
1031
1032\begin{methoddesc}[Context]{set_cipher_list}{ciphers}
1033Set the list of ciphers to be used in this context. See the OpenSSL manual for
1034more information (e.g. ciphers(1))
1035\end{methoddesc}
1036
1037\begin{methoddesc}[Context]{set_info_callback}{callback}
1038Set the information callback to \var{callback}. This function will be called
1039from time to time during SSL handshakes.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001040\var{callback} should take three arguments: a Connection object and two
1041integers. The first integer specifies where in the SSL handshake the function
1042was called, and the other the return code from a (possibly failed) internal
1043function call.
1044\end{methoddesc}
1045
1046\begin{methoddesc}[Context]{set_options}{options}
1047Add SSL options. Options you have set before are not cleared!
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001048This method should be used with the \constant{OP_*} constants.
1049\end{methoddesc}
1050
1051\begin{methoddesc}[Context]{set_passwd_cb}{callback\optional{, userdata}}
1052Set the passphrase callback to \var{callback}. This function will be called
Jean-Paul Calderone1eeb29e2008-10-19 11:50:53 -04001053when a private key with a passphrase is loaded. \var{callback} must accept
1054three positional arguments. First, an integer giving the maximum length of
1055the passphrase it may return. If the returned passphrase is longer than
1056this, it will be truncated. Second, a boolean value which will be true if
1057the user should be prompted for the passphrase twice and the callback should
1058verify that the two values supplied are equal. Third, the value given as the
1059\var{userdata} parameter to \method{set_passwd_cb}. If an error occurs,
1060\var{callback} should return a false value (e.g. an empty string).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001061\end{methoddesc}
1062
1063\begin{methoddesc}[Context]{set_session_id}{name}
1064Set the context \var{name} within which a session can be reused for this
1065Context object. This is needed when doing session resumption, because there is
1066no way for a stored session to know which Context object it is associated with.
1067\var{name} may be any binary data.
1068\end{methoddesc}
1069
1070\begin{methoddesc}[Context]{set_timeout}{timeout}
1071Set the timeout for newly created sessions for this Context object to
1072\var{timeout}. \var{timeout} must be given in (whole) seconds. The default
1073value is 300 seconds. See the OpenSSL manual for more information (e.g.
1074SSL_CTX_set_timeout(3)).
1075\end{methoddesc}
1076
1077\begin{methoddesc}[Context]{set_verify}{mode, callback}
1078Set the verification flags for this Context object to \var{mode} and specify
1079that \var{callback} should be used for verification callbacks. \var{mode}
1080should be one of \constant{VERIFY_NONE} and \constant{VERIFY_PEER}. If
1081\constant{VERIFY_PEER} is used, \var{mode} can be OR:ed with
1082\constant{VERIFY_FAIL_IF_NO_PEER_CERT} and \constant{VERIFY_CLIENT_ONCE} to
1083further control the behaviour.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001084\var{callback} should take five arguments: A Connection object, an X509 object,
1085and three integer variables, which are in turn potential error number, error
1086depth and return code. \var{callback} should return true if verification passes
1087and false otherwise.
1088\end{methoddesc}
1089
1090\begin{methoddesc}[Context]{set_verify_depth}{depth}
1091Set the maximum depth for the certificate chain verification that shall be
1092allowed for this Context object.
1093\end{methoddesc}
1094
1095\begin{methoddesc}[Context]{use_certificate}{cert}
1096Use the certificate \var{cert} which has to be a X509 object.
1097\end{methoddesc}
1098
Jean-Paul Calderone87b40602008-02-19 21:13:25 -05001099\begin{methoddesc}[Context]{add_extra_chain_cert}{cert}
1100Adds the certificate \var{cert}, which has to be a X509 object, to the
1101certificate chain presented together with the certificate.
1102\end{methoddesc}
1103
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001104\begin{methoddesc}[Context]{use_certificate_chain_file}{file}
1105Load a certificate chain from \var{file} which must be PEM encoded.
1106\end{methoddesc}
1107
1108\begin{methoddesc}[Context]{use_privatekey}{pkey}
1109Use the private key \var{pkey} which has to be a PKey object.
1110\end{methoddesc}
1111
1112\begin{methoddesc}[Context]{use_certificate_file}{file\optional{, format}}
1113Load the first certificate found in \var{file}. The certificate must be in the
1114format specified by \var{format}, which is either \constant{FILETYPE_PEM} or
1115\constant{FILETYPE_ASN1}. The default is \constant{FILETYPE_PEM}.
1116\end{methoddesc}
1117
1118\begin{methoddesc}[Context]{use_privatekey_file}{file\optional{, format}}
1119Load the first private key found in \var{file}. The private key must be in the
1120format specified by \var{format}, which is either \constant{FILETYPE_PEM} or
1121\constant{FILETYPE_ASN1}. The default is \constant{FILETYPE_PEM}.
1122\end{methoddesc}
1123
1124
1125\subsubsection{Connection objects \label{openssl-connection}}
1126
1127Connection objects have the following methods:
1128
1129\begin{methoddesc}[Connection]{accept}{}
1130Call the \method{accept} method of the underlying socket and set up SSL on the
1131returned socket, using the Context object supplied to this Connection object at
1132creation. Returns a pair \code{(\var{conn}, \var{address})}. where \var{conn}
1133is the new Connection object created, and \var{address} is as returned by the
1134socket's \method{accept}.
1135\end{methoddesc}
1136
1137\begin{methoddesc}[Connection]{bind}{address}
1138Call the \method{bind} method of the underlying socket.
1139\end{methoddesc}
1140
1141\begin{methoddesc}[Connection]{close}{}
1142Call the \method{close} method of the underlying socket. Note: If you want
1143correct SSL closure, you need to call the \method{shutdown} method first.
1144\end{methoddesc}
1145
1146\begin{methoddesc}[Connection]{connect}{address}
1147Call the \method{connect} method of the underlying socket and set up SSL on the
1148socket, using the Context object supplied to this Connection object at
1149creation.
1150\end{methoddesc}
1151
1152\begin{methoddesc}[Connection]{connect_ex}{address}
1153Call the \method{connect_ex} method of the underlying socket and set up SSL on
1154the socket, using the Context object supplied to this Connection object at
1155creation. Note that if the \method{connect_ex} method of the socket doesn't
1156return 0, SSL won't be initialized.
1157\end{methoddesc}
1158
1159\begin{methoddesc}[Connection]{do_handshake}{}
1160Perform an SSL handshake (usually called after \method{renegotiate} or one of
1161\method{set_accept_state} or \method{set_accept_state}). This can raise the
1162same exceptions as \method{send} and \method{recv}.
1163\end{methoddesc}
1164
1165\begin{methoddesc}[Connection]{fileno}{}
1166Retrieve the file descriptor number for the underlying socket.
1167\end{methoddesc}
1168
1169\begin{methoddesc}[Connection]{listen}{backlog}
1170Call the \method{listen} method of the underlying socket.
1171\end{methoddesc}
1172
1173\begin{methoddesc}[Connection]{get_app_data}{}
1174Retrieve application data as set by \method{set_app_data}.
1175\end{methoddesc}
1176
1177\begin{methoddesc}[Connection]{get_cipher_list}{}
1178Retrieve the list of ciphers used by the Connection object. WARNING: This API
1179has changed. It used to take an optional parameter and just return a string,
1180but not it returns the entire list in one go.
1181\end{methoddesc}
1182
Ziga Seilnachtf93bf102009-10-23 09:51:07 +02001183\begin{methoddesc}[Connection]{get_client_ca_list}{}
Ziga Seilnacht444e7cb2009-09-01 13:31:36 +02001184Retrieve the list of preferred client certificate issuers sent by the server
1185as \class{OpenSSL.crypto.X509Name} objects.
1186
1187If this is a client \class{Connection}, the list will be empty until the
1188connection with the server is established.
1189
1190If this is a server \class{Connection}, return the list of certificate
1191authorities that will be sent or has been sent to the client, as controlled
1192by this \class{Connection}'s \class{Context}.
1193
1194\versionadded{0.10}
1195\end{methoddesc}
1196
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001197\begin{methoddesc}[Connection]{get_context}{}
1198Retrieve the Context object associated with this Connection.
1199\end{methoddesc}
1200
1201\begin{methoddesc}[Connection]{get_peer_certificate}{}
1202Retrieve the other side's certificate (if any)
1203\end{methoddesc}
1204
1205\begin{methoddesc}[Connection]{getpeername}{}
1206Call the \method{getpeername} method of the underlying socket.
1207\end{methoddesc}
1208
1209\begin{methoddesc}[Connection]{getsockname}{}
1210Call the \method{getsockname} method of the underlying socket.
1211\end{methoddesc}
1212
1213\begin{methoddesc}[Connection]{getsockopt}{level, optname\optional{, buflen}}
1214Call the \method{getsockopt} method of the underlying socket.
1215\end{methoddesc}
1216
1217\begin{methoddesc}[Connection]{pending}{}
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001218Retrieve the number of bytes that can be safely read from the SSL buffer
1219(\emph{not} the underlying transport buffer).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001220\end{methoddesc}
1221
1222\begin{methoddesc}[Connection]{recv}{bufsize}
1223Receive data from the Connection. The return value is a string representing the
1224data received. The maximum amount of data to be received at once, is specified
1225by \var{bufsize}.
1226\end{methoddesc}
1227
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001228\begin{methoddesc}[Connection]{bio_write}{bytes}
1229If the Connection was created with a memory BIO, this method can be used to add
1230bytes to the read end of that memory BIO. The Connection can then read the
1231bytes (for example, in response to a call to \method{recv}).
1232\end{methoddesc}
1233
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001234\begin{methoddesc}[Connection]{renegotiate}{}
1235Renegotiate the SSL session. Call this if you wish to change cipher suites or
1236anything like that.
1237\end{methoddesc}
1238
1239\begin{methoddesc}[Connection]{send}{string}
1240Send the \var{string} data to the Connection.
1241\end{methoddesc}
1242
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001243\begin{methoddesc}[Connection]{bio_read}{bufsize}
1244If the Connection was created with a memory BIO, this method can be used to
1245read bytes from the write end of that memory BIO. Many Connection methods will
1246add bytes which must be read in this manner or the buffer will eventually fill
1247up and the Connection will be able to take no further actions.
1248\end{methoddesc}
1249
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001250\begin{methoddesc}[Connection]{sendall}{string}
1251Send all of the \var{string} data to the Connection. This calls \method{send}
1252repeatedly until all data is sent. If an error occurs, it's impossible to tell
1253how much data has been sent.
1254\end{methoddesc}
1255
1256\begin{methoddesc}[Connection]{set_accept_state}{}
1257Set the connection to work in server mode. The handshake will be handled
1258automatically by read/write.
1259\end{methoddesc}
1260
1261\begin{methoddesc}[Connection]{set_app_data}{data}
1262Associate \var{data} with this Connection object. \var{data} can be retrieved
1263later using the \method{get_app_data} method.
1264\end{methoddesc}
1265
1266\begin{methoddesc}[Connection]{set_connect_state}{}
1267Set the connection to work in client mode. The handshake will be handled
1268automatically by read/write.
1269\end{methoddesc}
1270
1271\begin{methoddesc}[Connection]{setblocking}{flag}
1272Call the \method{setblocking} method of the underlying socket.
1273\end{methoddesc}
1274
1275\begin{methoddesc}[Connection]{setsockopt}{level, optname, value}
1276Call the \method{setsockopt} method of the underlying socket.
1277\end{methoddesc}
1278
1279\begin{methoddesc}[Connection]{shutdown}{}
1280Send the shutdown message to the Connection. Returns true if the shutdown
1281message exchange is completed and false otherwise (in which case you call
1282\method{recv()} or \method{send()} when the connection becomes
1283readable/writeable.
1284\end{methoddesc}
1285
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001286\begin{methoddesc}[Connection]{get_shutdown}{}
1287Get the shutdown state of the Connection. Returns a bitvector of either or
1288both of \var{SENT_SHUTDOWN} and \var{RECEIVED_SHUTDOWN}.
1289\end{methoddesc}
1290
1291\begin{methoddesc}[Connection]{set_shutdown}{state}
1292Set the shutdown state of the Connection. \var{state} is a bitvector of
1293either or both of \var{SENT_SHUTDOWN} and \var{RECEIVED_SHUTDOWN}.
1294\end{methoddesc}
1295
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001296\begin{methoddesc}[Connection]{sock_shutdown}{how}
1297Call the \method{shutdown} method of the underlying socket.
1298\end{methoddesc}
1299
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001300\begin{methoddesc}[Connection]{bio_shutdown}{}
1301If the Connection was created with a memory BIO, this method can be used to
1302indicate that ``end of file'' has been reached on the read end of that memory
1303BIO.
1304\end{methoddesc}
1305
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001306\begin{methoddesc}[Connection]{state_string}{}
1307Retrieve a verbose string detailing the state of the Connection.
1308\end{methoddesc}
1309
Jean-Paul Calderonefd236f32009-05-03 19:45:07 -04001310\begin{methoddesc}[Connection]{client_random}{}
1311Retrieve the random value used with the client hello message.
1312\end{methoddesc}
1313
1314\begin{methoddesc}[Connection]{server_random}{}
1315Retrieve the random value used with the server hello message.
1316\end{methoddesc}
1317
1318\begin{methoddesc}[Connection]{master_key}{}
1319Retrieve the value of the master key for this session.
1320\end{methoddesc}
1321
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001322\begin{methoddesc}[Connection]{want_read}{}
1323Checks if more data has to be read from the transport layer to complete an
1324operation.
1325\end{methoddesc}
1326
1327\begin{methoddesc}[Connection]{want_write}{}
1328Checks if there is data to write to the transport layer to complete an
1329operation.
1330\end{methoddesc}
1331
1332
1333
1334\section{Internals \label{internals}}
1335
1336We ran into three main problems developing this: Exceptions, callbacks and
1337accessing socket methods. This is what this chapter is about.
1338
1339\subsection{Exceptions \label{exceptions}}
1340
1341We realized early that most of the exceptions would be raised by the I/O
1342functions of OpenSSL, so it felt natural to mimic OpenSSL's error code system,
1343translating them into Python exceptions. This naturally gives us the exceptions
1344\exception{SSL.ZeroReturnError}, \exception{SSL.WantReadError},
1345\exception{SSL.WantWriteError}, \exception{SSL.WantX509LookupError} and
1346\exception{SSL.SysCallError}.
1347
1348For more information about this, see section \ref{openssl-ssl}.
1349
1350
1351\subsection{Callbacks \label{callbacks}}
1352
1353There are a number of problems with callbacks. First of all, OpenSSL is written
1354as a C library, it's not meant to have Python callbacks, so a way around that
1355is needed. Another problem is thread support. A lot of the OpenSSL I/O
1356functions can block if the socket is in blocking mode, and then you want other
1357Python threads to be able to do other things. The real trouble is if you've
Jean-Paul Calderoneb7d6db22008-09-21 18:57:56 -04001358released the global CPython interpreter lock to do a potentially blocking
1359operation, and the operation calls a callback. Then we must take the GIL back,
1360since calling Python APIs without holding it is not allowed.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001361
1362There are two solutions to the first problem, both of which are necessary. The
1363first solution to use is if the C callback allows ''userdata'' to be passed to
1364it (an arbitrary pointer normally). This is great! We can set our Python
1365function object as the real userdata and emulate userdata for the Python
1366function in another way. The other solution can be used if an object with an
1367''app_data'' system always is passed to the callback. For example, the SSL
1368object in OpenSSL has app_data functions and in e.g. the verification
1369callbacks, you can retrieve the related SSL object. What we do is to set our
1370wrapper \class{Connection} object as app_data for the SSL object, and we can
1371easily find the Python callback.
1372
Jean-Paul Calderoneb7d6db22008-09-21 18:57:56 -04001373The other problem is solved using thread local variables. Whenever the GIL is
1374released before calling into an OpenSSL API, the PyThreadState pointer returned
1375by \cfunction{PyEval_SaveState} is stored in a global thread local variable
1376(using Python's own TLS API, \cfunction{PyThread_set_key_value}). When it is
1377necessary to re-acquire the GIL, either after the OpenSSL API returns or in a C
1378callback invoked by that OpenSSL API, the value of the thread local variable is
1379retrieved (\cfunction{PyThread_get_key_value}) and used to re-acquire the GIL.
1380This allows Python threads to execute while OpenSSL APIs are running and allows
1381use of any particular pyOpenSSL object from any Python thread, since there is
1382no per-thread state associated with any of these objects and since OpenSSL is
1383threadsafe (as long as properly initialized, as pyOpenSSL initializes it).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001384
1385
1386\subsection{Acessing Socket Methods \label{socket-methods}}
1387
1388We quickly saw the benefit of wrapping socket methods in the
1389\class{SSL.Connection} class, for an easy transition into using SSL. The
1390problem here is that the \module{socket} module lacks a C API, and all the
1391methods are declared static. One approach would be to have \module{OpenSSL} as
1392a submodule to the \module{socket} module, placing all the code in
1393\file{socketmodule.c}, but this is obviously not a good solution, since you
1394might not want to import tonnes of extra stuff you're not going to use when
1395importing the \module{socket} module. The other approach is to somehow get a
1396pointer to the method to be called, either the C function, or a callable Python
1397object. This is not really a good solution either, since there's a lot of
1398lookups involved.
1399
1400The way it works is that you have to supply a ``\class{socket}-like'' transport
1401object to the \class{SSL.Connection}. The only requirement of this object is
1402that it has a \method{fileno()} method that returns a file descriptor that's
1403valid at the C level (i.e. you can use the system calls read and write). If you
1404want to use the \method{connect()} or \method{accept()} methods of the
1405\class{SSL.Connection} object, the transport object has to supply such
1406methods too. Apart from them, any method lookups in the \class{SSL.Connection}
1407object that fail are passed on to the underlying transport object.
1408
1409Future changes might be to allow Python-level transport objects, that instead
1410of having \method{fileno()} methods, have \method{read()} and \method{write()}
1411methods, so more advanced features of Python can be used. This would probably
1412entail some sort of OpenSSL ``BIOs'', but converting Python strings back and
1413forth is expensive, so this shouldn't be used unless necessary. Other nice
1414things would be to be able to pass in different transport objects for reading
1415and writing, but then the \method{fileno()} method of \class{SSL.Connection}
1416becomes virtually useless. Also, should the method resolution be used on the
1417read-transport or the write-transport?
1418
1419
1420\end{document}