blob: d40ce5271ec05c430ef31049444a35e1ef9eeea9 [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
7\author{Martin Sjögren}
8\authoraddress{\email{martin@strakt.com}}
9
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}
141A Python type object representing the X509 object type.
142\end{datadesc}
143
144\begin{funcdesc}{X509}{}
145Factory function that creates an X509 object.
146\end{funcdesc}
147
148\begin{datadesc}{X509NameType}
149A Python type object representing the X509Name object type.
150\end{datadesc}
151
152\begin{funcdesc}{X509Name}{x509name}
153Factory function that creates a copy of \var{x509name}.
154\end{funcdesc}
155
156\begin{datadesc}{X509ReqType}
157A Python type object representing the X509Req object type.
158\end{datadesc}
159
160\begin{funcdesc}{X509Req}{}
161Factory function that creates an X509Req object.
162\end{funcdesc}
163
164\begin{datadesc}{X509StoreType}
165A Python type object representing the X509Store object type.
166\end{datadesc}
167
168\begin{datadesc}{PKeyType}
169A Python type object representing the PKey object type.
170\end{datadesc}
171
172\begin{funcdesc}{PKey}{}
173Factory function that creates a PKey object.
174\end{funcdesc}
175
176\begin{datadesc}{PKCS7Type}
177A Python type object representing the PKCS7 object type.
178\end{datadesc}
179
180\begin{datadesc}{PKCS12Type}
181A Python type object representing the PKCS12 object type.
182\end{datadesc}
183
184\begin{datadesc}{X509ExtensionType}
185A Python type object representing the X509Extension object type.
186\end{datadesc}
187
188\begin{funcdesc}{X509Extension}{typename, critical, value}
189Factory function that creates a X509Extension object.
190\end{funcdesc}
191
192\begin{datadesc}{NetscapeSPKIType}
193A Python type object representing the NetscapeSPKI object type.
194\end{datadesc}
195
196\begin{funcdesc}{NetscapeSPKI}{\optional{enc}}
197Factory function that creates a NetscapeSPKI object. If the \var{enc} argument
198is present, it should be a base64-encoded string representing a NetscapeSPKI
199object, as returned by the \method{b64_encode} method.
200\end{funcdesc}
201
202\begin{datadesc}{FILETYPE_PEM}
203\dataline{FILETYPE_ASN1}
204File type constants.
205\end{datadesc}
206
207\begin{datadesc}{TYPE_RSA}
208\dataline{TYPE_DSA}
209Key type constants.
210\end{datadesc}
211
212\begin{excdesc}{Error}
213Generic exception used in the \module{crypto} module.
214\end{excdesc}
215
216\begin{funcdesc}{dump_certificate}{type, cert}
217Dump the certificate \var{cert} into a buffer string encoded with the type
218\var{type}.
219\end{funcdesc}
220
221\begin{funcdesc}{dump_certificate_request}{type, req}
222Dump the certificate request \var{req} into a buffer string encoded with the
223type \var{type}.
224\end{funcdesc}
225
226\begin{funcdesc}{dump_privatekey}{type, pkey\optional{, cipher, passphrase}}
227Dump the private key \var{pkey} into a buffer string encoded with the type
228\var{type}, optionally (if \var{type} is \constant{FILETYPE_PEM}) encrypting it
229using \var{cipher} and \var{passphrase}.
230
231\var{passphrase} must be either a string or a callback for providing the
232pass phrase.
233\end{funcdesc}
234
235\begin{funcdesc}{load_certificate}{type, buffer}
236Load a certificate (X509) from the string \var{buffer} encoded with the
237type \var{type}.
238\end{funcdesc}
239
240\begin{funcdesc}{load_certificate_request}{type, buffer}
241Load a certificate request (X509Req) from the string \var{buffer} encoded with
242the type \var{type}.
243\end{funcdesc}
244
245\begin{funcdesc}{load_privatekey}{type, buffer\optional{, passphrase}}
246Load a private key (PKey) from the string \var{buffer} encoded with
247the type \var{type} (must be one of \constant{FILETYPE_PEM} and
248\constant{FILETYPE_ASN1}).
249
250\var{passphrase} must be either a string or a callback for providing the
251pass phrase.
252\end{funcdesc}
253
254\begin{funcdesc}{load_pkcs7_data}{type, buffer}
255Load pkcs7 data from the string \var{buffer} encoded with the type \var{type}.
256\end{funcdesc}
257
258\begin{funcdesc}{load_pkcs12}{buffer\optional{, passphrase}}
259Load pkcs12 data from the string \var{buffer}. If the pkcs12 structure is
260encrypted, a \var{passphrase} must be included.
261\end{funcdesc}
262
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500263\subsubsection{X509 objects \label{openssl-x509}}
264
265X509 objects have the following methods:
266
267\begin{methoddesc}[X509]{get_issuer}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500268Return an X509Name object representing the issuer of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500269\end{methoddesc}
270
271\begin{methoddesc}[X509]{get_pubkey}{}
272Return a PKey object representing the public key of the certificate.
273\end{methoddesc}
274
275\begin{methoddesc}[X509]{get_serial_number}{}
276Return the certificate serial number.
277\end{methoddesc}
278
279\begin{methoddesc}[X509]{get_subject}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500280Return an X509Name object representing the subject of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500281\end{methoddesc}
282
283\begin{methoddesc}[X509]{get_version}{}
284Return the certificate version.
285\end{methoddesc}
286
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400287\begin{methoddesc}[X509]{get_notBefore}{}
288Return a string giving the time before which the certificate is not valid. The
289string is formatted as an ASN1 GENERALIZEDTIME:
290\begin{verbatim}
291 YYYYMMDDhhmmssZ
292 YYYYMMDDhhmmss+hhmm
293 YYYYMMDDhhmmss-hhmm
294\end{verbatim}
Jean-Paul Calderonee0615b52008-03-09 21:44:46 -0400295If no value exists for this field, \code{None} is returned.
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400296\end{methoddesc}
297
298\begin{methoddesc}[X509]{get_notAfter}{}
299Return a string giving the time after 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]{set_notBefore}{when}
310Change the time before which the certificate is not valid. \var{when} is a
311string formatted as an ASN1 GENERALIZEDTIME:
312\begin{verbatim}
313 YYYYMMDDhhmmssZ
314 YYYYMMDDhhmmss+hhmm
315 YYYYMMDDhhmmss-hhmm
316\end{verbatim}
317\end{methoddesc}
318
319\begin{methoddesc}[X509]{set_notAfter}{when}
320Change the time after which the certificate is not valid. \var{when} is a
321string formatted as an ASN1 GENERALIZEDTIME:
322\begin{verbatim}
323 YYYYMMDDhhmmssZ
324 YYYYMMDDhhmmss+hhmm
325 YYYYMMDDhhmmss-hhmm
326\end{verbatim}
327\end{methoddesc}
328
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500329\begin{methoddesc}[X509]{gmtime_adj_notBefore}{time}
330Adjust the timestamp (in GMT) when the certificate starts being valid.
331\end{methoddesc}
332
333\begin{methoddesc}[X509]{gmtime_adj_notAfter}{time}
334Adjust the timestamp (in GMT) when the certificate stops being valid.
335\end{methoddesc}
336
337\begin{methoddesc}[X509]{has_expired}{}
338Checks the certificate's time stamp against current time. Returns true if the
339certificate has expired and false otherwise.
340\end{methoddesc}
341
342\begin{methoddesc}[X509]{set_issuer}{issuer}
343Set the issuer of the certificate to \var{issuer}.
344\end{methoddesc}
345
346\begin{methoddesc}[X509]{set_pubkey}{pkey}
347Set the public key of the certificate to \var{pkey}.
348\end{methoddesc}
349
350\begin{methoddesc}[X509]{set_serial_number}{serialno}
351Set the serial number of the certificate to \var{serialno}.
352\end{methoddesc}
353
354\begin{methoddesc}[X509]{set_subject}{subject}
355Set the subject of the certificate to \var{subject}.
356\end{methoddesc}
357
358\begin{methoddesc}[X509]{set_version}{version}
359Set the certificate version to \var{version}.
360\end{methoddesc}
361
362\begin{methoddesc}[X509]{sign}{pkey, digest}
363Sign the certificate, using the key \var{pkey} and the message digest algorithm
364identified by the string \var{digest}.
365\end{methoddesc}
366
367\begin{methoddesc}[X509]{subject_name_hash}{}
368Return the hash of the certificate subject.
369\end{methoddesc}
370
371\begin{methoddesc}[X509]{digest}{digest_name}
372Return a digest of the certificate, using the \var{digest_name} method.
Jean-Paul Calderoneb6f0d602008-12-28 21:20:01 -0500373\var{digest_name} must be a string describing a digest algorithm supported
374by OpenSSL (by EVP_get_digestbyname, specifically). For example,
375\constant{"md5"} or \constant{"sha1"}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500376\end{methoddesc}
377
378\begin{methoddesc}[X509]{add_extensions}{extensions}
379Add the extensions in the sequence \var{extensions} to the certificate.
380\end{methoddesc}
381
382\subsubsection{X509Name objects \label{openssl-x509name}}
383
Jean-Paul Calderone2dd8ff52008-03-24 17:43:58 -0400384X509Name objects have the following methods:
385
386\begin{methoddesc}[X509Name]{hash}{}
387Return an integer giving the first four bytes of the MD5 digest of the DER
388representation of the name.
389\end{methoddesc}
390
Jean-Paul Calderonea6edbf82008-03-25 15:19:11 -0400391\begin{methoddesc}[X509Name]{der}{}
392Return a string giving the DER representation of the name.
393\end{methoddesc}
394
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400395\begin{methoddesc}[X509Name]{get_components}{}
396Return a list of two-tuples of strings giving the components of the name.
397\end{methoddesc}
398
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500399X509Name objects have the following members:
400
401\begin{memberdesc}[X509Name]{countryName}
402The country of the entity. \code{C} may be used as an alias for
403\code{countryName}.
404\end{memberdesc}
405
406\begin{memberdesc}[X509Name]{stateOrProvinceName}
407The state or province of the entity. \code{ST} may be used as an alias for
408\code{stateOrProvinceName}·
409\end{memberdesc}
410
411\begin{memberdesc}[X509Name]{localityName}
412The locality of the entity. \code{L} may be used as an alias for
413\code{localityName}.
414\end{memberdesc}
415
416\begin{memberdesc}[X509Name]{organizationName}
417The organization name of the entity. \code{O} may be used as an alias for
418\code{organizationName}.
419\end{memberdesc}
420
421\begin{memberdesc}[X509Name]{organizationalUnitName}
422The organizational unit of the entity. \code{OU} may be used as an alias for
423\code{organizationalUnitName}.
424\end{memberdesc}
425
426\begin{memberdesc}[X509Name]{commonName}
427The common name of the entity. \code{CN} may be used as an alias for
428\code{commonName}.
429\end{memberdesc}
430
431\begin{memberdesc}[X509Name]{emailAddress}
432The e-mail address of the entity.
433\end{memberdesc}
434
435\subsubsection{X509Req objects \label{openssl-x509req}}
436
437X509Req objects have the following methods:
438
439\begin{methoddesc}[X509Req]{get_pubkey}{}
440Return a PKey object representing the public key of the certificate request.
441\end{methoddesc}
442
443\begin{methoddesc}[X509Req]{get_subject}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500444Return an X509Name object representing the subject of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500445\end{methoddesc}
446
447\begin{methoddesc}[X509Req]{set_pubkey}{pkey}
448Set the public key of the certificate request to \var{pkey}.
449\end{methoddesc}
450
451\begin{methoddesc}[X509Req]{sign}{pkey, digest}
452Sign the certificate request, using the key \var{pkey} and the message digest
453algorithm identified by the string \var{digest}.
454\end{methoddesc}
455
456\begin{methoddesc}[X509Req]{verify}{pkey}
457Verify a certificate request using the public key \var{pkey}.
458\end{methoddesc}
459
Jean-Paul Calderone8dd19b82008-12-28 20:41:16 -0500460\begin{methoddesc}[X509Req]{set_version}{version}
461Set the version (RFC 2459, 4.1.2.1) of the certificate request to
462\var{version}.
463\end{methoddesc}
464
465\begin{methoddesc}[X509Req]{get_version}{}
466Get the version (RFC 2459, 4.1.2.1) of the certificate request.
467\end{methoddesc}
468
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500469\subsubsection{X509Store objects \label{openssl-x509store}}
470
471The X509Store object has currently just one method:
472
473\begin{methoddesc}[X509Store]{add_cert}{cert}
474Add the certificate \var{cert} to the certificate store.
475\end{methoddesc}
476
477\subsubsection{PKey objects \label{openssl-pkey}}
478
479The PKey object has the following methods:
480
481\begin{methoddesc}[PKey]{bits}{}
482Return the number of bits of the key.
483\end{methoddesc}
484
485\begin{methoddesc}[PKey]{generate_key}{type, bits}
486Generate a public/private key pair of the type \var{type} (one of
487\constant{TYPE_RSA} and \constant{TYPE_DSA}) with the size \var{bits}.
488\end{methoddesc}
489
490\begin{methoddesc}[PKey]{type}{}
491Return the type of the key.
492\end{methoddesc}
493
494\subsubsection{PKCS7 objects \label{openssl-pkcs7}}
495
496PKCS7 objects have the following methods:
497
498\begin{methoddesc}[PKCS7]{type_is_signed}{}
499FIXME
500\end{methoddesc}
501
502\begin{methoddesc}[PKCS7]{type_is_enveloped}{}
503FIXME
504\end{methoddesc}
505
506\begin{methoddesc}[PKCS7]{type_is_signedAndEnveloped}{}
507FIXME
508\end{methoddesc}
509
510\begin{methoddesc}[PKCS7]{type_is_data}{}
511FIXME
512\end{methoddesc}
513
514\begin{methoddesc}[PKCS7]{get_type_name}{}
515Get the type name of the PKCS7.
516\end{methoddesc}
517
518\subsubsection{PKCS12 objects \label{openssl-pkcs12}}
519
520PKCS12 objects have the following methods:
521
522\begin{methoddesc}[PKCS12]{get_certificate}{}
523Return certificate portion of the PKCS12 structure.
524\end{methoddesc}
525
526\begin{methoddesc}[PKCS12]{get_privatekey}{}
527Return private key portion of the PKCS12 structure
528\end{methoddesc}
529
530\begin{methoddesc}[PKCS12]{get_ca_certificates}{}
531Return CA certificates within the PKCS12 object as a tuple. Returns
532None if no CA certificates are present.
533\end{methoddesc}
534
535\subsubsection{X509Extension objects \label{openssl-509ext}}
536
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -0500537X509Extension objects have several methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500538
539\begin{methoddesc}[X509Extension]{get_critical}{}
540Return the critical field of the extension object.
541\end{methoddesc}
542
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -0500543\begin{methoddesc}[X509Extension]{get_short_name}{}
544Return the short type name of the extension object.
545\end{methoddesc}
546
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500547\subsubsection{NetscapeSPKI objects \label{openssl-netscape-spki}}
548
549NetscapeSPKI objects have the following methods:
550
551\begin{methoddesc}[NetscapeSPKI]{b64_encode}{}
552Return a base64-encoded string representation of the object.
553\end{methoddesc}
554
555\begin{methoddesc}[NetscapeSPKI]{get_pubkey}{}
556Return the public key of object.
557\end{methoddesc}
558
559\begin{methoddesc}[NetscapeSPKI]{set_pubkey}{key}
560Set the public key of the object to \var{key}.
561\end{methoddesc}
562
563\begin{methoddesc}[NetscapeSPKI]{sign}{key, digest_name}
Jean-Paul Calderoneb6f0d602008-12-28 21:20:01 -0500564Sign the NetscapeSPKI object using the given \var{key} and
565\var{digest_name}. \var{digest_name} must be a string describing a digest
566algorithm supported by OpenSSL (by EVP_get_digestbyname, specifically). For
567example, \constant{"md5"} or \constant{"sha1"}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500568\end{methoddesc}
569
570\begin{methoddesc}[NetscapeSPKI]{verify}{key}
571Verify the NetscapeSPKI object using the given \var{key}.
572\end{methoddesc}
573
574
575% % % rand module
576
577\subsection{\module{rand} --- An interface to the OpenSSL pseudo random number generator \label{openssl-rand}}
578
579\declaremodule{extension}{rand}
580\modulesynopsis{An interface to the OpenSSL pseudo random number generator}
581
582This module handles the OpenSSL pseudo random number generator (PRNG) and
583declares the following:
584
585\begin{funcdesc}{add}{string, entropy}
586Mix bytes from \var{string} into the PRNG state. The \var{entropy} argument is
587(the lower bound of) an estimate of how much randomness is contained in
588\var{string}, measured in bytes. For more information, see e.g. \rfc{1750}.
589\end{funcdesc}
590
591\begin{funcdesc}{egd}{path\optional{, bytes}}
592Query the Entropy Gathering Daemon\footnote{See
593\url{http://www.lothar.com/tech/crypto/}} on socket \var{path} for \var{bytes}
594bytes of random data and and uses \function{add} to seed the PRNG. The default
595value of \var{bytes} is 255.
596\end{funcdesc}
597
598\begin{funcdesc}{load_file}{path\optional{, bytes}}
599Read \var{bytes} bytes (or all of it, if \var{bytes} is negative) of data from
600the file \var{path} to seed the PRNG. The default value of \var{bytes} is -1.
601\end{funcdesc}
602
603\begin{funcdesc}{screen}{}
604Add the current contents of the screen to the PRNG state.
605Availability: Windows.
606\end{funcdesc}
607
608\begin{funcdesc}{seed}{string}
609This is equivalent to calling \function{add} with \var{entropy} as the length
610of the string.
611\end{funcdesc}
612
613\begin{funcdesc}{status}{}
614Returns true if the PRNG has been seeded with enough data, and false otherwise.
615\end{funcdesc}
616
617\begin{funcdesc}{write_file}{path}
618Write a number of random bytes (currently 1024) to the file \var{path}. This
619file can then be used with \function{load_file} to seed the PRNG again.
620\end{funcdesc}
621
622
623
624% % % SSL module
625
626\subsection{\module{SSL} --- An interface to the SSL-specific parts of OpenSSL \label{openssl-ssl}}
627
628\declaremodule{extension}{SSL}
629\modulesynopsis{An interface to the SSL-specific parts of OpenSSL}
630
631This module handles things specific to SSL. There are two objects defined:
632Context, Connection.
633
634\begin{datadesc}{SSLv2_METHOD}
635\dataline{SSLv3_METHOD}
636\dataline{SSLv23_METHOD}
637\dataline{TLSv1_METHOD}
638These constants represent the different SSL methods to use when creating a
639context object.
640\end{datadesc}
641
642\begin{datadesc}{VERIFY_NONE}
643\dataline{VERIFY_PEER}
644\dataline{VERIFY_FAIL_IF_NO_PEER_CERT}
645These constants represent the verification mode used by the Context
646object's \method{set_verify} method.
647\end{datadesc}
648
649\begin{datadesc}{FILETYPE_PEM}
650\dataline{FILETYPE_ASN1}
651File type constants used with the \method{use_certificate_file} and
652\method{use_privatekey_file} methods of Context objects.
653\end{datadesc}
654
655\begin{datadesc}{OP_SINGLE_DH_USE}
656\dataline{OP_EPHEMERAL_RSA}
657\dataline{OP_NO_SSLv2}
658\dataline{OP_NO_SSLv3}
659\dataline{OP_NO_TLSv1}
660Constants used with \method{set_options} of Context objects.
661\constant{OP_SINGLE_DH_USE} means to always create a new key when using ephemeral
662Diffie-Hellman. \constant{OP_EPHEMERAL_RSA} means to always use ephemeral RSA keys
663when doing RSA operations. \constant{OP_NO_SSLv2}, \constant{OP_NO_SSLv3} and
664\constant{OP_NO_TLSv1} means to disable those specific protocols. This is
665interesting if you're using e.g. \constant{SSLv23_METHOD} to get an SSLv2-compatible
666handshake, but don't want to use SSLv2.
667\end{datadesc}
668
669\begin{datadesc}{ContextType}
670A Python type object representing the Context object type.
671\end{datadesc}
672
673\begin{funcdesc}{Context}{method}
674Factory function that creates a new Context object given an SSL method. The
675method should be \constant{SSLv2_METHOD}, \constant{SSLv3_METHOD},
676\constant{SSLv23_METHOD} or \constant{TLSv1_METHOD}.
677\end{funcdesc}
678
679\begin{datadesc}{ConnectionType}
680A Python type object representing the Connection object type.
681\end{datadesc}
682
683\begin{funcdesc}{Connection}{context, socket}
684Factory fucnction that creates a new Connection object given an SSL context and
685a socket \footnote{Actually, all that is required is an object that
686\emph{behaves} like a socket, you could even use files, even though it'd be
687tricky to get the handshakes right!} object.
688\end{funcdesc}
689
690\begin{excdesc}{Error}
691This exception is used as a base class for the other SSL-related
692exceptions, but may also be raised directly.
693
694Whenever this exception is raised directly, it has a list of error messages
695from the OpenSSL error queue, where each item is a tuple \code{(\var{lib},
696\var{function}, \var{reason})}. Here \var{lib}, \var{function} and \var{reason}
697are all strings, describing where and what the problem is. See \manpage{err}{3}
698for more information.
699\end{excdesc}
700
701\begin{excdesc}{ZeroReturnError}
702This exception matches the error return code \code{SSL_ERROR_ZERO_RETURN}, and
703is raised when the SSL Connection has been closed. In SSL 3.0 and TLS 1.0, this
704only occurs if a closure alert has occurred in the protocol, i.e. the
705connection has been closed cleanly. Note that this does not necessarily
706mean that the transport layer (e.g. a socket) has been closed.
707
708It may seem a little strange that this is an exception, but it does match an
709\code{SSL_ERROR} code, and is very convenient.
710\end{excdesc}
711
712\begin{excdesc}{WantReadError}
713The operation did not complete; the same I/O method should be called again
714later, with the same arguments. Any I/O method can lead to this since new
715handshakes can occur at any time.
716\end{excdesc}
717
718\begin{excdesc}{WantWriteError}
719See \exception{WantReadError}.
720\end{excdesc}
721
722\begin{excdesc}{WantX509LookupError}
723The operation did not complete because an application callback has asked to be
724called again. The I/O method should be called again later, with the same
725arguments. Note: This won't occur in this version, as there are no such
726callbacks in this version.
727\end{excdesc}
728
729\begin{excdesc}{SysCallError}
730The \exception{SysCallError} occurs when there's an I/O error and OpenSSL's
731error queue does not contain any information. This can mean two things: An
732error in the transport protocol, or an end of file that violates the protocol.
733The parameter to the exception is always a pair \code{(\var{errnum},
734\var{errstr})}.
735\end{excdesc}
736
737
738\subsubsection{Context objects \label{openssl-context}}
739
740Context objects have the following methods:
741
742\begin{methoddesc}[Context]{check_privatekey}{}
743Check if the private key (loaded with \method{use_privatekey\optional{_file}})
744matches the certificate (loaded with \method{use_certificate\optional{_file}}).
Jean-Paul Calderonef05fbbe2008-03-06 21:52:35 -0500745Returns \code{None} if they match, raises \exception{Error} otherwise.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500746\end{methoddesc}
747
748\begin{methoddesc}[Context]{get_app_data}{}
749Retrieve application data as set by \method{set_app_data}.
750\end{methoddesc}
751
752\begin{methoddesc}[Context]{get_cert_store}{}
753Retrieve the certificate store (a X509Store object) that the context uses.
754This can be used to add "trusted" certificates without using the.
755\method{load_verify_locations()} method.
756\end{methoddesc}
757
758\begin{methoddesc}[Context]{get_timeout}{}
759Retrieve session timeout, as set by \method{set_timeout}. The default is 300
760seconds.
761\end{methoddesc}
762
763\begin{methoddesc}[Context]{get_verify_depth}{}
764Retrieve the Context object's verify depth, as set by
765\method{set_verify_depth}.
766\end{methoddesc}
767
768\begin{methoddesc}[Context]{get_verify_mode}{}
Jean-Paul Calderoneae4238d2008-12-28 21:13:50 -0500769Retrieve the Context object's verify mode, as set by \method{set_verify}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500770\end{methoddesc}
771
772\begin{methoddesc}[Context]{load_client_ca}{pemfile}
773Read a file with PEM-formatted certificates that will be sent to the client
774when requesting a client certificate.
775\end{methoddesc}
776
Jean-Paul Calderone5601c242008-09-07 21:06:52 -0400777\begin{methoddesc}[Context]{load_verify_locations}{pemfile, capath}
778Specify where CA certificates for verification purposes are located. These
779are trusted certificates. Note that the certificates have to be in PEM
780format. If capath is passed, it must be a directory prepared using the
781\code{c_rehash} tool included with OpenSSL. Either, but not both, of
782\var{pemfile} or \var{capath} may be \code{None}.
783\end{methoddesc}
784
785\begin{methoddesc}[Context]{set_default_verify_paths}{}
786Specify that the platform provided CA certificates are to be used for
Jean-Paul Calderone1d287e52009-03-07 09:09:07 -0500787verification purposes. This method may not work properly on OS X.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500788\end{methoddesc}
789
790\begin{methoddesc}[Context]{load_tmp_dh}{dhfile}
791Load parameters for Ephemeral Diffie-Hellman from \var{dhfile}.
792\end{methoddesc}
793
794\begin{methoddesc}[Context]{set_app_data}{data}
795Associate \var{data} with this Context object. \var{data} can be retrieved
796later using the \method{get_app_data} method.
797\end{methoddesc}
798
799\begin{methoddesc}[Context]{set_cipher_list}{ciphers}
800Set the list of ciphers to be used in this context. See the OpenSSL manual for
801more information (e.g. ciphers(1))
802\end{methoddesc}
803
804\begin{methoddesc}[Context]{set_info_callback}{callback}
805Set the information callback to \var{callback}. This function will be called
806from time to time during SSL handshakes.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500807\var{callback} should take three arguments: a Connection object and two
808integers. The first integer specifies where in the SSL handshake the function
809was called, and the other the return code from a (possibly failed) internal
810function call.
811\end{methoddesc}
812
813\begin{methoddesc}[Context]{set_options}{options}
814Add SSL options. Options you have set before are not cleared!
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500815This method should be used with the \constant{OP_*} constants.
816\end{methoddesc}
817
818\begin{methoddesc}[Context]{set_passwd_cb}{callback\optional{, userdata}}
819Set the passphrase callback to \var{callback}. This function will be called
Jean-Paul Calderone1eeb29e2008-10-19 11:50:53 -0400820when a private key with a passphrase is loaded. \var{callback} must accept
821three positional arguments. First, an integer giving the maximum length of
822the passphrase it may return. If the returned passphrase is longer than
823this, it will be truncated. Second, a boolean value which will be true if
824the user should be prompted for the passphrase twice and the callback should
825verify that the two values supplied are equal. Third, the value given as the
826\var{userdata} parameter to \method{set_passwd_cb}. If an error occurs,
827\var{callback} should return a false value (e.g. an empty string).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500828\end{methoddesc}
829
830\begin{methoddesc}[Context]{set_session_id}{name}
831Set the context \var{name} within which a session can be reused for this
832Context object. This is needed when doing session resumption, because there is
833no way for a stored session to know which Context object it is associated with.
834\var{name} may be any binary data.
835\end{methoddesc}
836
837\begin{methoddesc}[Context]{set_timeout}{timeout}
838Set the timeout for newly created sessions for this Context object to
839\var{timeout}. \var{timeout} must be given in (whole) seconds. The default
840value is 300 seconds. See the OpenSSL manual for more information (e.g.
841SSL_CTX_set_timeout(3)).
842\end{methoddesc}
843
844\begin{methoddesc}[Context]{set_verify}{mode, callback}
845Set the verification flags for this Context object to \var{mode} and specify
846that \var{callback} should be used for verification callbacks. \var{mode}
847should be one of \constant{VERIFY_NONE} and \constant{VERIFY_PEER}. If
848\constant{VERIFY_PEER} is used, \var{mode} can be OR:ed with
849\constant{VERIFY_FAIL_IF_NO_PEER_CERT} and \constant{VERIFY_CLIENT_ONCE} to
850further control the behaviour.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500851\var{callback} should take five arguments: A Connection object, an X509 object,
852and three integer variables, which are in turn potential error number, error
853depth and return code. \var{callback} should return true if verification passes
854and false otherwise.
855\end{methoddesc}
856
857\begin{methoddesc}[Context]{set_verify_depth}{depth}
858Set the maximum depth for the certificate chain verification that shall be
859allowed for this Context object.
860\end{methoddesc}
861
862\begin{methoddesc}[Context]{use_certificate}{cert}
863Use the certificate \var{cert} which has to be a X509 object.
864\end{methoddesc}
865
Jean-Paul Calderone87b40602008-02-19 21:13:25 -0500866\begin{methoddesc}[Context]{add_extra_chain_cert}{cert}
867Adds the certificate \var{cert}, which has to be a X509 object, to the
868certificate chain presented together with the certificate.
869\end{methoddesc}
870
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500871\begin{methoddesc}[Context]{use_certificate_chain_file}{file}
872Load a certificate chain from \var{file} which must be PEM encoded.
873\end{methoddesc}
874
875\begin{methoddesc}[Context]{use_privatekey}{pkey}
876Use the private key \var{pkey} which has to be a PKey object.
877\end{methoddesc}
878
879\begin{methoddesc}[Context]{use_certificate_file}{file\optional{, format}}
880Load the first certificate found in \var{file}. The certificate must be in the
881format specified by \var{format}, which is either \constant{FILETYPE_PEM} or
882\constant{FILETYPE_ASN1}. The default is \constant{FILETYPE_PEM}.
883\end{methoddesc}
884
885\begin{methoddesc}[Context]{use_privatekey_file}{file\optional{, format}}
886Load the first private key found in \var{file}. The private key must be in the
887format specified by \var{format}, which is either \constant{FILETYPE_PEM} or
888\constant{FILETYPE_ASN1}. The default is \constant{FILETYPE_PEM}.
889\end{methoddesc}
890
891
892\subsubsection{Connection objects \label{openssl-connection}}
893
894Connection objects have the following methods:
895
896\begin{methoddesc}[Connection]{accept}{}
897Call the \method{accept} method of the underlying socket and set up SSL on the
898returned socket, using the Context object supplied to this Connection object at
899creation. Returns a pair \code{(\var{conn}, \var{address})}. where \var{conn}
900is the new Connection object created, and \var{address} is as returned by the
901socket's \method{accept}.
902\end{methoddesc}
903
904\begin{methoddesc}[Connection]{bind}{address}
905Call the \method{bind} method of the underlying socket.
906\end{methoddesc}
907
908\begin{methoddesc}[Connection]{close}{}
909Call the \method{close} method of the underlying socket. Note: If you want
910correct SSL closure, you need to call the \method{shutdown} method first.
911\end{methoddesc}
912
913\begin{methoddesc}[Connection]{connect}{address}
914Call the \method{connect} method of the underlying socket and set up SSL on the
915socket, using the Context object supplied to this Connection object at
916creation.
917\end{methoddesc}
918
919\begin{methoddesc}[Connection]{connect_ex}{address}
920Call the \method{connect_ex} method of the underlying socket and set up SSL on
921the socket, using the Context object supplied to this Connection object at
922creation. Note that if the \method{connect_ex} method of the socket doesn't
923return 0, SSL won't be initialized.
924\end{methoddesc}
925
926\begin{methoddesc}[Connection]{do_handshake}{}
927Perform an SSL handshake (usually called after \method{renegotiate} or one of
928\method{set_accept_state} or \method{set_accept_state}). This can raise the
929same exceptions as \method{send} and \method{recv}.
930\end{methoddesc}
931
932\begin{methoddesc}[Connection]{fileno}{}
933Retrieve the file descriptor number for the underlying socket.
934\end{methoddesc}
935
936\begin{methoddesc}[Connection]{listen}{backlog}
937Call the \method{listen} method of the underlying socket.
938\end{methoddesc}
939
940\begin{methoddesc}[Connection]{get_app_data}{}
941Retrieve application data as set by \method{set_app_data}.
942\end{methoddesc}
943
944\begin{methoddesc}[Connection]{get_cipher_list}{}
945Retrieve the list of ciphers used by the Connection object. WARNING: This API
946has changed. It used to take an optional parameter and just return a string,
947but not it returns the entire list in one go.
948\end{methoddesc}
949
950\begin{methoddesc}[Connection]{get_context}{}
951Retrieve the Context object associated with this Connection.
952\end{methoddesc}
953
954\begin{methoddesc}[Connection]{get_peer_certificate}{}
955Retrieve the other side's certificate (if any)
956\end{methoddesc}
957
958\begin{methoddesc}[Connection]{getpeername}{}
959Call the \method{getpeername} method of the underlying socket.
960\end{methoddesc}
961
962\begin{methoddesc}[Connection]{getsockname}{}
963Call the \method{getsockname} method of the underlying socket.
964\end{methoddesc}
965
966\begin{methoddesc}[Connection]{getsockopt}{level, optname\optional{, buflen}}
967Call the \method{getsockopt} method of the underlying socket.
968\end{methoddesc}
969
970\begin{methoddesc}[Connection]{pending}{}
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -0500971Retrieve the number of bytes that can be safely read from the SSL buffer
972(\emph{not} the underlying transport buffer).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500973\end{methoddesc}
974
975\begin{methoddesc}[Connection]{recv}{bufsize}
976Receive data from the Connection. The return value is a string representing the
977data received. The maximum amount of data to be received at once, is specified
978by \var{bufsize}.
979\end{methoddesc}
980
981\begin{methoddesc}[Connection]{renegotiate}{}
982Renegotiate the SSL session. Call this if you wish to change cipher suites or
983anything like that.
984\end{methoddesc}
985
986\begin{methoddesc}[Connection]{send}{string}
987Send the \var{string} data to the Connection.
988\end{methoddesc}
989
990\begin{methoddesc}[Connection]{sendall}{string}
991Send all of the \var{string} data to the Connection. This calls \method{send}
992repeatedly until all data is sent. If an error occurs, it's impossible to tell
993how much data has been sent.
994\end{methoddesc}
995
996\begin{methoddesc}[Connection]{set_accept_state}{}
997Set the connection to work in server mode. The handshake will be handled
998automatically by read/write.
999\end{methoddesc}
1000
1001\begin{methoddesc}[Connection]{set_app_data}{data}
1002Associate \var{data} with this Connection object. \var{data} can be retrieved
1003later using the \method{get_app_data} method.
1004\end{methoddesc}
1005
1006\begin{methoddesc}[Connection]{set_connect_state}{}
1007Set the connection to work in client mode. The handshake will be handled
1008automatically by read/write.
1009\end{methoddesc}
1010
1011\begin{methoddesc}[Connection]{setblocking}{flag}
1012Call the \method{setblocking} method of the underlying socket.
1013\end{methoddesc}
1014
1015\begin{methoddesc}[Connection]{setsockopt}{level, optname, value}
1016Call the \method{setsockopt} method of the underlying socket.
1017\end{methoddesc}
1018
1019\begin{methoddesc}[Connection]{shutdown}{}
1020Send the shutdown message to the Connection. Returns true if the shutdown
1021message exchange is completed and false otherwise (in which case you call
1022\method{recv()} or \method{send()} when the connection becomes
1023readable/writeable.
1024\end{methoddesc}
1025
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001026\begin{methoddesc}[Connection]{get_shutdown}{}
1027Get the shutdown state of the Connection. Returns a bitvector of either or
1028both of \var{SENT_SHUTDOWN} and \var{RECEIVED_SHUTDOWN}.
1029\end{methoddesc}
1030
1031\begin{methoddesc}[Connection]{set_shutdown}{state}
1032Set the shutdown state of the Connection. \var{state} is a bitvector of
1033either or both of \var{SENT_SHUTDOWN} and \var{RECEIVED_SHUTDOWN}.
1034\end{methoddesc}
1035
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001036\begin{methoddesc}[Connection]{sock_shutdown}{how}
1037Call the \method{shutdown} method of the underlying socket.
1038\end{methoddesc}
1039
1040\begin{methoddesc}[Connection]{state_string}{}
1041Retrieve a verbose string detailing the state of the Connection.
1042\end{methoddesc}
1043
1044\begin{methoddesc}[Connection]{want_read}{}
1045Checks if more data has to be read from the transport layer to complete an
1046operation.
1047\end{methoddesc}
1048
1049\begin{methoddesc}[Connection]{want_write}{}
1050Checks if there is data to write to the transport layer to complete an
1051operation.
1052\end{methoddesc}
1053
1054
1055
1056\section{Internals \label{internals}}
1057
1058We ran into three main problems developing this: Exceptions, callbacks and
1059accessing socket methods. This is what this chapter is about.
1060
1061\subsection{Exceptions \label{exceptions}}
1062
1063We realized early that most of the exceptions would be raised by the I/O
1064functions of OpenSSL, so it felt natural to mimic OpenSSL's error code system,
1065translating them into Python exceptions. This naturally gives us the exceptions
1066\exception{SSL.ZeroReturnError}, \exception{SSL.WantReadError},
1067\exception{SSL.WantWriteError}, \exception{SSL.WantX509LookupError} and
1068\exception{SSL.SysCallError}.
1069
1070For more information about this, see section \ref{openssl-ssl}.
1071
1072
1073\subsection{Callbacks \label{callbacks}}
1074
1075There are a number of problems with callbacks. First of all, OpenSSL is written
1076as a C library, it's not meant to have Python callbacks, so a way around that
1077is needed. Another problem is thread support. A lot of the OpenSSL I/O
1078functions can block if the socket is in blocking mode, and then you want other
1079Python threads to be able to do other things. The real trouble is if you've
Jean-Paul Calderoneb7d6db22008-09-21 18:57:56 -04001080released the global CPython interpreter lock to do a potentially blocking
1081operation, and the operation calls a callback. Then we must take the GIL back,
1082since calling Python APIs without holding it is not allowed.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001083
1084There are two solutions to the first problem, both of which are necessary. The
1085first solution to use is if the C callback allows ''userdata'' to be passed to
1086it (an arbitrary pointer normally). This is great! We can set our Python
1087function object as the real userdata and emulate userdata for the Python
1088function in another way. The other solution can be used if an object with an
1089''app_data'' system always is passed to the callback. For example, the SSL
1090object in OpenSSL has app_data functions and in e.g. the verification
1091callbacks, you can retrieve the related SSL object. What we do is to set our
1092wrapper \class{Connection} object as app_data for the SSL object, and we can
1093easily find the Python callback.
1094
Jean-Paul Calderoneb7d6db22008-09-21 18:57:56 -04001095The other problem is solved using thread local variables. Whenever the GIL is
1096released before calling into an OpenSSL API, the PyThreadState pointer returned
1097by \cfunction{PyEval_SaveState} is stored in a global thread local variable
1098(using Python's own TLS API, \cfunction{PyThread_set_key_value}). When it is
1099necessary to re-acquire the GIL, either after the OpenSSL API returns or in a C
1100callback invoked by that OpenSSL API, the value of the thread local variable is
1101retrieved (\cfunction{PyThread_get_key_value}) and used to re-acquire the GIL.
1102This allows Python threads to execute while OpenSSL APIs are running and allows
1103use of any particular pyOpenSSL object from any Python thread, since there is
1104no per-thread state associated with any of these objects and since OpenSSL is
1105threadsafe (as long as properly initialized, as pyOpenSSL initializes it).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001106
1107
1108\subsection{Acessing Socket Methods \label{socket-methods}}
1109
1110We quickly saw the benefit of wrapping socket methods in the
1111\class{SSL.Connection} class, for an easy transition into using SSL. The
1112problem here is that the \module{socket} module lacks a C API, and all the
1113methods are declared static. One approach would be to have \module{OpenSSL} as
1114a submodule to the \module{socket} module, placing all the code in
1115\file{socketmodule.c}, but this is obviously not a good solution, since you
1116might not want to import tonnes of extra stuff you're not going to use when
1117importing the \module{socket} module. The other approach is to somehow get a
1118pointer to the method to be called, either the C function, or a callable Python
1119object. This is not really a good solution either, since there's a lot of
1120lookups involved.
1121
1122The way it works is that you have to supply a ``\class{socket}-like'' transport
1123object to the \class{SSL.Connection}. The only requirement of this object is
1124that it has a \method{fileno()} method that returns a file descriptor that's
1125valid at the C level (i.e. you can use the system calls read and write). If you
1126want to use the \method{connect()} or \method{accept()} methods of the
1127\class{SSL.Connection} object, the transport object has to supply such
1128methods too. Apart from them, any method lookups in the \class{SSL.Connection}
1129object that fail are passed on to the underlying transport object.
1130
1131Future changes might be to allow Python-level transport objects, that instead
1132of having \method{fileno()} methods, have \method{read()} and \method{write()}
1133methods, so more advanced features of Python can be used. This would probably
1134entail some sort of OpenSSL ``BIOs'', but converting Python strings back and
1135forth is expensive, so this shouldn't be used unless necessary. Other nice
1136things would be to be able to pass in different transport objects for reading
1137and writing, but then the \method{fileno()} method of \class{SSL.Connection}
1138becomes virtually useless. Also, should the method resolution be used on the
1139read-transport or the write-transport?
1140
1141
1142\end{document}