blob: c7d3a6c21d076ecf65e443d9c05077fc3bc30861 [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001\documentclass{howto}
2
3\title{Python OpenSSL Manual}
4
Jean-Paul Calderone71ff3682009-04-25 08:30:11 -04005\release{0.9}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05006
Jean-Paul Calderone0ebe45a2009-04-25 10:40:31 -04007\author{Jean-Paul Calderone}
8\authoraddress{\email{exarkun@twistedmatrix.com}}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05009
10\usepackage[english]{babel}
11\usepackage[T1]{fontenc}
12
13\begin{document}
14
15\maketitle
16
17\begin{abstract}
18\noindent
19This module is a rather thin wrapper around (a subset of) the OpenSSL library.
20With thin wrapper I mean that a lot of the object methods do nothing more than
21calling a corresponding function in the OpenSSL library.
22\end{abstract}
23
24\tableofcontents
25
26
27\section{Introduction \label{intro}}
28
Jean-Paul Calderone9450d5b2008-09-01 12:04:20 -040029The reason pyOpenSSL was created is that the SSL support in the socket module
30in Python 2.1 (the contemporary version of Python when the pyOpenSSL project
31was begun) was severely limited. Other OpenSSL wrappers for Python at the time
32were also limited, though in different ways. Unfortunately, Python's standard
33library SSL support has remained weak, although other packages (such as
34M2Crypto\footnote{See \url{http://chandlerproject.org/Projects/MeTooCrypto}})
35have made great advances and now equal or exceed pyOpenSSL's functionality.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050036
Jean-Paul Calderone9450d5b2008-09-01 12:04:20 -040037The reason pyOpenSSL continues to be maintained is that there is a significant
38user community around it, as well as a large amount of software which depends
39on it. It is a great benefit to many people for pyOpenSSL to continue to exist
40and advance.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050041
42\section{Building and Installing \label{building}}
43
44These instructions can also be found in the file \verb|INSTALL|.
45
46I have tested this on Debian Linux systems (woody and sid), Solaris 2.6 and
472.7. Others have successfully compiled it on Windows and NT.
48
49\subsection{Building the Module on a Unix System \label{building-unix}}
50
51pyOpenSSL uses distutils, so there really shouldn't be any problems. To build
52the library:
53\begin{verbatim}
54python setup.py build
55\end{verbatim}
56
57If your OpenSSL header files aren't in \verb|/usr/include|, you may need to
58supply the \verb|-I| flag to let the setup script know where to look. The same
59goes for the libraries of course, use the \verb|-L| flag. Note that
60\verb|build| won't accept these flags, so you have to run first
61\verb|build_ext| and then \verb|build|! Example:
62\begin{verbatim}
63python setup.py build_ext -I/usr/local/ssl/include -L/usr/local/ssl/lib
64python setup.py build
65\end{verbatim}
66
67Now you should have a directory called \verb|OpenSSL| that contains e.g.
68\verb|SSL.so| and \verb|__init__.py| somewhere in the build dicrectory,
69so just:
70\begin{verbatim}
71python setup.py install
72\end{verbatim}
73
74If you, for some arcane reason, don't want the module to appear in the
75\verb|site-packages| directory, use the \verb|--prefix| option.
76
77You can, of course, do
78\begin{verbatim}
79python setup.py --help
80\end{verbatim}
81
82to find out more about how to use the script.
83
84\subsection{Building the Module on a Windows System \label{building-windows}}
85
86Big thanks to Itamar Shtull-Trauring and Oleg Orlov for their help with
87Windows build instructions. Same as for Unix systems, we have to separate
88the \verb|build_ext| and the \verb|build|.
89
90Building the library:
91
92\begin{verbatim}
93setup.py build_ext -I ...\openssl\inc32 -L ...\openssl\out32dll
94setup.py build
95\end{verbatim}
96
97Where \verb|...\openssl| is of course the location of your OpenSSL installation.
98
99Installation is the same as for Unix systems:
100\begin{verbatim}
101setup.py install
102\end{verbatim}
103
104And similarily, you can do
105\begin{verbatim}
106setup.py --help
107\end{verbatim}
108
109to get more information.
110
111
112\section{\module{OpenSSL} --- Python interface to OpenSSL \label{openssl}}
113
114\declaremodule{extension}{OpenSSL}
115\modulesynopsis{Python interface to OpenSSL}
116
117This package provides a high-level interface to the functions in the
118OpenSSL library. The following modules are defined:
119
120\begin{datadesc}{crypto}
121Generic cryptographic module. Note that if anything is incomplete, this module is!
122\end{datadesc}
123
124\begin{datadesc}{rand}
125An interface to the OpenSSL pseudo random number generator.
126\end{datadesc}
127
128\begin{datadesc}{SSL}
129An interface to the SSL-specific parts of OpenSSL.
130\end{datadesc}
131
132
133% % % crypto moduleOpenSSL
134
135\subsection{\module{crypto} --- Generic cryptographic module \label{openssl-crypto}}
136
137\declaremodule{extension}{crypto}
138\modulesynopsis{Generic cryptographic module}
139
140\begin{datadesc}{X509Type}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400141See \class{X509}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500142\end{datadesc}
143
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400144\begin{classdesc}{X509}{}
145A class representing X.509 certificates.
146\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500147
148\begin{datadesc}{X509NameType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400149See \class{X509Name}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500150\end{datadesc}
151
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400152\begin{classdesc}{X509Name}{x509name}
153A class representing X.509 Distinguished Names.
154
155This constructor creates a copy of \var{x509name} which should be an
156instance of \class{X509Name}.
157\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500158
159\begin{datadesc}{X509ReqType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400160See \class{X509Req}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500161\end{datadesc}
162
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400163\begin{classdesc}{X509Req}{}
164A class representing X.509 certificate requests.
165\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500166
167\begin{datadesc}{X509StoreType}
168A Python type object representing the X509Store object type.
169\end{datadesc}
170
171\begin{datadesc}{PKeyType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400172See \class{PKey}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500173\end{datadesc}
174
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400175\begin{classdesc}{PKey}{}
176A class representing DSA or RSA keys.
177\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500178
179\begin{datadesc}{PKCS7Type}
180A Python type object representing the PKCS7 object type.
181\end{datadesc}
182
183\begin{datadesc}{PKCS12Type}
184A Python type object representing the PKCS12 object type.
185\end{datadesc}
186
187\begin{datadesc}{X509ExtensionType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400188See \class{X509Extension}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500189\end{datadesc}
190
Rick Dean47262da2009-07-08 16:17:17 -0500191\begin{classdesc}{X509Extension}{typename, critical, value\optional{, subject}\optional{, issuer}}
192A class representing an X.509 v3 certificate extensions.
193See \url{http://openssl.org/docs/apps/x509v3_config.html\#STANDARD_EXTENSIONS}
194for \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}.
272\var{buffer} encoded with the type \var{type}. The type \var{type}
273must 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
282encrypted, a \var{passphrase} must be included.
283\end{funcdesc}
284
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500285\subsubsection{X509 objects \label{openssl-x509}}
286
287X509 objects have the following methods:
288
289\begin{methoddesc}[X509]{get_issuer}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500290Return an X509Name object representing the issuer of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500291\end{methoddesc}
292
293\begin{methoddesc}[X509]{get_pubkey}{}
294Return a PKey object representing the public key of the certificate.
295\end{methoddesc}
296
297\begin{methoddesc}[X509]{get_serial_number}{}
298Return the certificate serial number.
299\end{methoddesc}
300
301\begin{methoddesc}[X509]{get_subject}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500302Return an X509Name object representing the subject of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500303\end{methoddesc}
304
305\begin{methoddesc}[X509]{get_version}{}
306Return the certificate version.
307\end{methoddesc}
308
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400309\begin{methoddesc}[X509]{get_notBefore}{}
310Return a string giving the time before which the certificate is not valid. The
311string is formatted as an ASN1 GENERALIZEDTIME:
312\begin{verbatim}
313 YYYYMMDDhhmmssZ
314 YYYYMMDDhhmmss+hhmm
315 YYYYMMDDhhmmss-hhmm
316\end{verbatim}
Jean-Paul Calderonee0615b52008-03-09 21:44:46 -0400317If no value exists for this field, \code{None} is returned.
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400318\end{methoddesc}
319
320\begin{methoddesc}[X509]{get_notAfter}{}
321Return a string giving the time after which the certificate is not valid. The
322string is formatted as an ASN1 GENERALIZEDTIME:
323\begin{verbatim}
324 YYYYMMDDhhmmssZ
325 YYYYMMDDhhmmss+hhmm
326 YYYYMMDDhhmmss-hhmm
327\end{verbatim}
Jean-Paul Calderonee0615b52008-03-09 21:44:46 -0400328If no value exists for this field, \code{None} is returned.
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400329\end{methoddesc}
330
331\begin{methoddesc}[X509]{set_notBefore}{when}
332Change the time before which the certificate is not valid. \var{when} is a
333string formatted as an ASN1 GENERALIZEDTIME:
334\begin{verbatim}
335 YYYYMMDDhhmmssZ
336 YYYYMMDDhhmmss+hhmm
337 YYYYMMDDhhmmss-hhmm
338\end{verbatim}
339\end{methoddesc}
340
341\begin{methoddesc}[X509]{set_notAfter}{when}
342Change the time after which the certificate is not valid. \var{when} is a
343string formatted as an ASN1 GENERALIZEDTIME:
344\begin{verbatim}
345 YYYYMMDDhhmmssZ
346 YYYYMMDDhhmmss+hhmm
347 YYYYMMDDhhmmss-hhmm
348\end{verbatim}
349\end{methoddesc}
350
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500351\begin{methoddesc}[X509]{gmtime_adj_notBefore}{time}
352Adjust the timestamp (in GMT) when the certificate starts being valid.
353\end{methoddesc}
354
355\begin{methoddesc}[X509]{gmtime_adj_notAfter}{time}
356Adjust the timestamp (in GMT) when the certificate stops being valid.
357\end{methoddesc}
358
359\begin{methoddesc}[X509]{has_expired}{}
360Checks the certificate's time stamp against current time. Returns true if the
361certificate has expired and false otherwise.
362\end{methoddesc}
363
364\begin{methoddesc}[X509]{set_issuer}{issuer}
365Set the issuer of the certificate to \var{issuer}.
366\end{methoddesc}
367
368\begin{methoddesc}[X509]{set_pubkey}{pkey}
369Set the public key of the certificate to \var{pkey}.
370\end{methoddesc}
371
372\begin{methoddesc}[X509]{set_serial_number}{serialno}
373Set the serial number of the certificate to \var{serialno}.
374\end{methoddesc}
375
376\begin{methoddesc}[X509]{set_subject}{subject}
377Set the subject of the certificate to \var{subject}.
378\end{methoddesc}
379
380\begin{methoddesc}[X509]{set_version}{version}
381Set the certificate version to \var{version}.
382\end{methoddesc}
383
384\begin{methoddesc}[X509]{sign}{pkey, digest}
385Sign the certificate, using the key \var{pkey} and the message digest algorithm
386identified by the string \var{digest}.
387\end{methoddesc}
388
389\begin{methoddesc}[X509]{subject_name_hash}{}
390Return the hash of the certificate subject.
391\end{methoddesc}
392
393\begin{methoddesc}[X509]{digest}{digest_name}
394Return a digest of the certificate, using the \var{digest_name} method.
Jean-Paul Calderoneb6f0d602008-12-28 21:20:01 -0500395\var{digest_name} must be a string describing a digest algorithm supported
396by OpenSSL (by EVP_get_digestbyname, specifically). For example,
397\constant{"md5"} or \constant{"sha1"}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500398\end{methoddesc}
399
400\begin{methoddesc}[X509]{add_extensions}{extensions}
401Add the extensions in the sequence \var{extensions} to the certificate.
402\end{methoddesc}
403
404\subsubsection{X509Name objects \label{openssl-x509name}}
405
Jean-Paul Calderone2dd8ff52008-03-24 17:43:58 -0400406X509Name objects have the following methods:
407
408\begin{methoddesc}[X509Name]{hash}{}
409Return an integer giving the first four bytes of the MD5 digest of the DER
410representation of the name.
411\end{methoddesc}
412
Jean-Paul Calderonea6edbf82008-03-25 15:19:11 -0400413\begin{methoddesc}[X509Name]{der}{}
414Return a string giving the DER representation of the name.
415\end{methoddesc}
416
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400417\begin{methoddesc}[X509Name]{get_components}{}
418Return a list of two-tuples of strings giving the components of the name.
419\end{methoddesc}
420
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500421X509Name objects have the following members:
422
423\begin{memberdesc}[X509Name]{countryName}
424The country of the entity. \code{C} may be used as an alias for
425\code{countryName}.
426\end{memberdesc}
427
428\begin{memberdesc}[X509Name]{stateOrProvinceName}
429The state or province of the entity. \code{ST} may be used as an alias for
430\code{stateOrProvinceName}·
431\end{memberdesc}
432
433\begin{memberdesc}[X509Name]{localityName}
434The locality of the entity. \code{L} may be used as an alias for
435\code{localityName}.
436\end{memberdesc}
437
438\begin{memberdesc}[X509Name]{organizationName}
439The organization name of the entity. \code{O} may be used as an alias for
440\code{organizationName}.
441\end{memberdesc}
442
443\begin{memberdesc}[X509Name]{organizationalUnitName}
444The organizational unit of the entity. \code{OU} may be used as an alias for
445\code{organizationalUnitName}.
446\end{memberdesc}
447
448\begin{memberdesc}[X509Name]{commonName}
449The common name of the entity. \code{CN} may be used as an alias for
450\code{commonName}.
451\end{memberdesc}
452
453\begin{memberdesc}[X509Name]{emailAddress}
454The e-mail address of the entity.
455\end{memberdesc}
456
457\subsubsection{X509Req objects \label{openssl-x509req}}
458
459X509Req objects have the following methods:
460
461\begin{methoddesc}[X509Req]{get_pubkey}{}
462Return a PKey object representing the public key of the certificate request.
463\end{methoddesc}
464
465\begin{methoddesc}[X509Req]{get_subject}{}
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500466Return an X509Name object representing the subject of the certificate.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500467\end{methoddesc}
468
469\begin{methoddesc}[X509Req]{set_pubkey}{pkey}
470Set the public key of the certificate request to \var{pkey}.
471\end{methoddesc}
472
473\begin{methoddesc}[X509Req]{sign}{pkey, digest}
474Sign the certificate request, using the key \var{pkey} and the message digest
475algorithm identified by the string \var{digest}.
476\end{methoddesc}
477
478\begin{methoddesc}[X509Req]{verify}{pkey}
479Verify a certificate request using the public key \var{pkey}.
480\end{methoddesc}
481
Jean-Paul Calderone8dd19b82008-12-28 20:41:16 -0500482\begin{methoddesc}[X509Req]{set_version}{version}
483Set the version (RFC 2459, 4.1.2.1) of the certificate request to
484\var{version}.
485\end{methoddesc}
486
487\begin{methoddesc}[X509Req]{get_version}{}
488Get the version (RFC 2459, 4.1.2.1) of the certificate request.
489\end{methoddesc}
490
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500491\subsubsection{X509Store objects \label{openssl-x509store}}
492
493The X509Store object has currently just one method:
494
495\begin{methoddesc}[X509Store]{add_cert}{cert}
496Add the certificate \var{cert} to the certificate store.
497\end{methoddesc}
498
499\subsubsection{PKey objects \label{openssl-pkey}}
500
501The PKey object has the following methods:
502
503\begin{methoddesc}[PKey]{bits}{}
504Return the number of bits of the key.
505\end{methoddesc}
506
507\begin{methoddesc}[PKey]{generate_key}{type, bits}
508Generate a public/private key pair of the type \var{type} (one of
509\constant{TYPE_RSA} and \constant{TYPE_DSA}) with the size \var{bits}.
510\end{methoddesc}
511
512\begin{methoddesc}[PKey]{type}{}
513Return the type of the key.
514\end{methoddesc}
515
516\subsubsection{PKCS7 objects \label{openssl-pkcs7}}
517
518PKCS7 objects have the following methods:
519
520\begin{methoddesc}[PKCS7]{type_is_signed}{}
521FIXME
522\end{methoddesc}
523
524\begin{methoddesc}[PKCS7]{type_is_enveloped}{}
525FIXME
526\end{methoddesc}
527
528\begin{methoddesc}[PKCS7]{type_is_signedAndEnveloped}{}
529FIXME
530\end{methoddesc}
531
532\begin{methoddesc}[PKCS7]{type_is_data}{}
533FIXME
534\end{methoddesc}
535
536\begin{methoddesc}[PKCS7]{get_type_name}{}
537Get the type name of the PKCS7.
538\end{methoddesc}
539
540\subsubsection{PKCS12 objects \label{openssl-pkcs12}}
541
542PKCS12 objects have the following methods:
543
544\begin{methoddesc}[PKCS12]{get_certificate}{}
545Return certificate portion of the PKCS12 structure.
546\end{methoddesc}
547
548\begin{methoddesc}[PKCS12]{get_privatekey}{}
549Return private key portion of the PKCS12 structure
550\end{methoddesc}
551
552\begin{methoddesc}[PKCS12]{get_ca_certificates}{}
553Return CA certificates within the PKCS12 object as a tuple. Returns
554None if no CA certificates are present.
555\end{methoddesc}
556
557\subsubsection{X509Extension objects \label{openssl-509ext}}
558
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -0500559X509Extension objects have several methods:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500560
561\begin{methoddesc}[X509Extension]{get_critical}{}
562Return the critical field of the extension object.
563\end{methoddesc}
564
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -0500565\begin{methoddesc}[X509Extension]{get_short_name}{}
566Return the short type name of the extension object.
567\end{methoddesc}
568
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500569\subsubsection{NetscapeSPKI objects \label{openssl-netscape-spki}}
570
571NetscapeSPKI objects have the following methods:
572
573\begin{methoddesc}[NetscapeSPKI]{b64_encode}{}
574Return a base64-encoded string representation of the object.
575\end{methoddesc}
576
577\begin{methoddesc}[NetscapeSPKI]{get_pubkey}{}
578Return the public key of object.
579\end{methoddesc}
580
581\begin{methoddesc}[NetscapeSPKI]{set_pubkey}{key}
582Set the public key of the object to \var{key}.
583\end{methoddesc}
584
585\begin{methoddesc}[NetscapeSPKI]{sign}{key, digest_name}
Jean-Paul Calderoneb6f0d602008-12-28 21:20:01 -0500586Sign the NetscapeSPKI object using the given \var{key} and
587\var{digest_name}. \var{digest_name} must be a string describing a digest
588algorithm supported by OpenSSL (by EVP_get_digestbyname, specifically). For
589example, \constant{"md5"} or \constant{"sha1"}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500590\end{methoddesc}
591
592\begin{methoddesc}[NetscapeSPKI]{verify}{key}
593Verify the NetscapeSPKI object using the given \var{key}.
594\end{methoddesc}
595
Rick Dean536ba022009-07-24 23:57:27 -0500596\subsubsection{CRL objects \label{crl}}
597
598CRL objects have the following methods:
599
600\begin{methoddesc}[CRL]{add_revoked}{revoked}
601Add a Revoked object to the CRL, by value not reference.
602\end{methoddesc}
603
604\begin{methoddesc}[CRL]{export}{cert, key\optional{, type=FILETYPE_PEM}\optional{, days=100}}
605Use \var{cert} and \var{key} to sign the CRL and return the CRL as a string.
606\var{days} is the number of days before the next CRL is due.
607\end{methoddesc}
608
609\begin{methoddesc}[CRL]{get_revoked}{}
610Return a tuple of Revoked objects, by value not reference.
611\end{methoddesc}
612
613\subsubsection{Revoked objects \label{revoked}}
614
615Revoked objects have the following methods:
616
Rick Dean6385faf2009-07-26 00:07:47 -0500617\begin{methoddesc}[Revoked]{all_reasons}{}
618Return a list of all supported reasons.
619\end{methoddesc}
620
621\begin{methoddesc}[Revoked]{get_reason}{}
622Return the revocation reason as a str. Can be
623None, which differs from "Unspecified".
624\end{methoddesc}
625
Rick Dean536ba022009-07-24 23:57:27 -0500626\begin{methoddesc}[Revoked]{get_rev_date}{}
627Return the revocation date as a str.
628The string is formatted as an ASN1 GENERALIZEDTIME.
629\end{methoddesc}
630
631\begin{methoddesc}[Revoked]{get_serial}{}
632Return a str containing a hex number of the serial of the revoked certificate.
633\end{methoddesc}
634
Rick Dean6385faf2009-07-26 00:07:47 -0500635\begin{methoddesc}[Revoked]{set_reason}{reason}
636Set the revocation reason. \var{reason} must
637be None or a string, but the values are limited.
638Spaces and case are ignored. See \method{all_reasons}.
639\end{methoddesc}
640
Rick Dean536ba022009-07-24 23:57:27 -0500641\begin{methoddesc}[Revoked]{set_rev_date}{date}
642Set the revocation date.
643The string is formatted as an ASN1 GENERALIZEDTIME.
644\end{methoddesc}
645
646\begin{methoddesc}[Revoked]{set_serial}{serial}
647\var{serial} is a string containing a hex number of the serial of the revoked certificate.
648\end{methoddesc}
649
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500650
651% % % rand module
652
653\subsection{\module{rand} --- An interface to the OpenSSL pseudo random number generator \label{openssl-rand}}
654
655\declaremodule{extension}{rand}
656\modulesynopsis{An interface to the OpenSSL pseudo random number generator}
657
658This module handles the OpenSSL pseudo random number generator (PRNG) and
659declares the following:
660
661\begin{funcdesc}{add}{string, entropy}
662Mix bytes from \var{string} into the PRNG state. The \var{entropy} argument is
663(the lower bound of) an estimate of how much randomness is contained in
664\var{string}, measured in bytes. For more information, see e.g. \rfc{1750}.
665\end{funcdesc}
666
Rick Dean4fd5a4e2009-07-08 12:06:10 -0500667\begin{funcdesc}{bytes}{num_bytes}
Jean-Paul Calderonea45e2b92009-07-08 13:29:58 -0400668Get some random bytes from the PRNG as a string.
669
670This is a wrapper for the C function \function{RAND_bytes}.
Rick Dean4fd5a4e2009-07-08 12:06:10 -0500671\end{funcdesc}
672
Rick Dean433dc642009-07-07 13:11:55 -0500673\begin{funcdesc}{cleanup}{}
Jean-Paul Calderone427c0b32009-07-07 15:43:27 -0400674Erase the memory used by the PRNG.
675
676This is a wrapper for the C function \function{RAND_cleanup}.
Rick Dean433dc642009-07-07 13:11:55 -0500677\end{funcdesc}
678
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500679\begin{funcdesc}{egd}{path\optional{, bytes}}
680Query the Entropy Gathering Daemon\footnote{See
681\url{http://www.lothar.com/tech/crypto/}} on socket \var{path} for \var{bytes}
682bytes of random data and and uses \function{add} to seed the PRNG. The default
683value of \var{bytes} is 255.
684\end{funcdesc}
685
686\begin{funcdesc}{load_file}{path\optional{, bytes}}
687Read \var{bytes} bytes (or all of it, if \var{bytes} is negative) of data from
688the file \var{path} to seed the PRNG. The default value of \var{bytes} is -1.
689\end{funcdesc}
690
691\begin{funcdesc}{screen}{}
692Add the current contents of the screen to the PRNG state.
693Availability: Windows.
694\end{funcdesc}
695
696\begin{funcdesc}{seed}{string}
697This is equivalent to calling \function{add} with \var{entropy} as the length
698of the string.
699\end{funcdesc}
700
701\begin{funcdesc}{status}{}
702Returns true if the PRNG has been seeded with enough data, and false otherwise.
703\end{funcdesc}
704
705\begin{funcdesc}{write_file}{path}
706Write a number of random bytes (currently 1024) to the file \var{path}. This
707file can then be used with \function{load_file} to seed the PRNG again.
708\end{funcdesc}
709
Rick Deanfc69c812009-07-08 11:03:47 -0500710\begin{excdesc}{Error}
711If the current RAND method supports any errors, this is raised when needed.
712The default method does not raise this when the entropy pool is depleted.
713
714Whenever this exception is raised directly, it has a list of error messages
715from the OpenSSL error queue, where each item is a tuple \code{(\var{lib},
716\var{function}, \var{reason})}. Here \var{lib}, \var{function} and \var{reason}
717are all strings, describing where and what the problem is. See \manpage{err}{3}
718for more information.
719\end{excdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500720
721
722% % % SSL module
723
724\subsection{\module{SSL} --- An interface to the SSL-specific parts of OpenSSL \label{openssl-ssl}}
725
726\declaremodule{extension}{SSL}
727\modulesynopsis{An interface to the SSL-specific parts of OpenSSL}
728
729This module handles things specific to SSL. There are two objects defined:
730Context, Connection.
731
732\begin{datadesc}{SSLv2_METHOD}
733\dataline{SSLv3_METHOD}
734\dataline{SSLv23_METHOD}
735\dataline{TLSv1_METHOD}
736These constants represent the different SSL methods to use when creating a
737context object.
738\end{datadesc}
739
740\begin{datadesc}{VERIFY_NONE}
741\dataline{VERIFY_PEER}
742\dataline{VERIFY_FAIL_IF_NO_PEER_CERT}
743These constants represent the verification mode used by the Context
744object's \method{set_verify} method.
745\end{datadesc}
746
747\begin{datadesc}{FILETYPE_PEM}
748\dataline{FILETYPE_ASN1}
749File type constants used with the \method{use_certificate_file} and
750\method{use_privatekey_file} methods of Context objects.
751\end{datadesc}
752
753\begin{datadesc}{OP_SINGLE_DH_USE}
754\dataline{OP_EPHEMERAL_RSA}
755\dataline{OP_NO_SSLv2}
756\dataline{OP_NO_SSLv3}
757\dataline{OP_NO_TLSv1}
758Constants used with \method{set_options} of Context objects.
759\constant{OP_SINGLE_DH_USE} means to always create a new key when using ephemeral
760Diffie-Hellman. \constant{OP_EPHEMERAL_RSA} means to always use ephemeral RSA keys
761when doing RSA operations. \constant{OP_NO_SSLv2}, \constant{OP_NO_SSLv3} and
762\constant{OP_NO_TLSv1} means to disable those specific protocols. This is
763interesting if you're using e.g. \constant{SSLv23_METHOD} to get an SSLv2-compatible
764handshake, but don't want to use SSLv2.
765\end{datadesc}
766
767\begin{datadesc}{ContextType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400768See \class{Context}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500769\end{datadesc}
770
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400771\begin{classdesc}{Context}{method}
772A class representing SSL contexts. Contexts define the parameters of one or
773more SSL connections.
774
775\var{method} should be \constant{SSLv2_METHOD}, \constant{SSLv3_METHOD},
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500776\constant{SSLv23_METHOD} or \constant{TLSv1_METHOD}.
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400777\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500778
779\begin{datadesc}{ConnectionType}
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400780See \class{Connection}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500781\end{datadesc}
782
Jean-Paul Calderonefc4abd02009-07-04 16:15:36 -0400783\begin{classdesc}{Connection}{context, socket}
784A class representing SSL connections.
785
786\var{context} should be an instance of \class{Context} and \var{socket}
787should be a socket \footnote{Actually, all that is required is an object
788that \emph{behaves} like a socket, you could even use files, even though
789it'd be tricky to get the handshakes right!} object. \var{socket} may be
790\var{None}; in this case, the Connection is created with a memory BIO: see
791the \method{bio_read}, \method{bio_write}, and \method{bio_shutdown}
792methods.
793\end{classdesc}
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500794
795\begin{excdesc}{Error}
796This exception is used as a base class for the other SSL-related
797exceptions, but may also be raised directly.
798
799Whenever this exception is raised directly, it has a list of error messages
800from the OpenSSL error queue, where each item is a tuple \code{(\var{lib},
801\var{function}, \var{reason})}. Here \var{lib}, \var{function} and \var{reason}
802are all strings, describing where and what the problem is. See \manpage{err}{3}
803for more information.
804\end{excdesc}
805
806\begin{excdesc}{ZeroReturnError}
807This exception matches the error return code \code{SSL_ERROR_ZERO_RETURN}, and
808is raised when the SSL Connection has been closed. In SSL 3.0 and TLS 1.0, this
809only occurs if a closure alert has occurred in the protocol, i.e. the
810connection has been closed cleanly. Note that this does not necessarily
811mean that the transport layer (e.g. a socket) has been closed.
812
813It may seem a little strange that this is an exception, but it does match an
814\code{SSL_ERROR} code, and is very convenient.
815\end{excdesc}
816
817\begin{excdesc}{WantReadError}
818The operation did not complete; the same I/O method should be called again
819later, with the same arguments. Any I/O method can lead to this since new
820handshakes can occur at any time.
Rick Dean71fa0962009-07-09 23:56:39 -0500821
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400822The wanted read is for \emph{dirty} data sent over the network, not the
823\emph{clean} data inside the tunnel. For a socket based SSL connection,
824\emph{read} means data coming at us over the network. Until that read
825succeeds, the attempted \method{OpenSSL.SSL.Connection.recv},
Rick Dean71fa0962009-07-09 23:56:39 -0500826\method{OpenSSL.SSL.Connection.send}, or
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400827\method{OpenSSL.SSL.Connection.do_handshake} is prevented or incomplete. You
828probably want to \method{select()} on the socket before trying again.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500829\end{excdesc}
830
831\begin{excdesc}{WantWriteError}
Jean-Paul Calderonecc787d52009-07-16 12:08:41 -0400832See \exception{WantReadError}. The socket send buffer may be too full to
Rick Dean71fa0962009-07-09 23:56:39 -0500833write more data.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500834\end{excdesc}
835
836\begin{excdesc}{WantX509LookupError}
837The operation did not complete because an application callback has asked to be
838called again. The I/O method should be called again later, with the same
839arguments. Note: This won't occur in this version, as there are no such
840callbacks in this version.
841\end{excdesc}
842
843\begin{excdesc}{SysCallError}
844The \exception{SysCallError} occurs when there's an I/O error and OpenSSL's
845error queue does not contain any information. This can mean two things: An
846error in the transport protocol, or an end of file that violates the protocol.
847The parameter to the exception is always a pair \code{(\var{errnum},
848\var{errstr})}.
849\end{excdesc}
850
851
852\subsubsection{Context objects \label{openssl-context}}
853
854Context objects have the following methods:
855
856\begin{methoddesc}[Context]{check_privatekey}{}
857Check if the private key (loaded with \method{use_privatekey\optional{_file}})
858matches the certificate (loaded with \method{use_certificate\optional{_file}}).
Jean-Paul Calderonef05fbbe2008-03-06 21:52:35 -0500859Returns \code{None} if they match, raises \exception{Error} otherwise.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500860\end{methoddesc}
861
862\begin{methoddesc}[Context]{get_app_data}{}
863Retrieve application data as set by \method{set_app_data}.
864\end{methoddesc}
865
866\begin{methoddesc}[Context]{get_cert_store}{}
867Retrieve the certificate store (a X509Store object) that the context uses.
868This can be used to add "trusted" certificates without using the.
869\method{load_verify_locations()} method.
870\end{methoddesc}
871
872\begin{methoddesc}[Context]{get_timeout}{}
873Retrieve session timeout, as set by \method{set_timeout}. The default is 300
874seconds.
875\end{methoddesc}
876
877\begin{methoddesc}[Context]{get_verify_depth}{}
878Retrieve the Context object's verify depth, as set by
879\method{set_verify_depth}.
880\end{methoddesc}
881
882\begin{methoddesc}[Context]{get_verify_mode}{}
Jean-Paul Calderoneae4238d2008-12-28 21:13:50 -0500883Retrieve the Context object's verify mode, as set by \method{set_verify}.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500884\end{methoddesc}
885
886\begin{methoddesc}[Context]{load_client_ca}{pemfile}
887Read a file with PEM-formatted certificates that will be sent to the client
888when requesting a client certificate.
889\end{methoddesc}
890
Jean-Paul Calderone5601c242008-09-07 21:06:52 -0400891\begin{methoddesc}[Context]{load_verify_locations}{pemfile, capath}
892Specify where CA certificates for verification purposes are located. These
893are trusted certificates. Note that the certificates have to be in PEM
894format. If capath is passed, it must be a directory prepared using the
895\code{c_rehash} tool included with OpenSSL. Either, but not both, of
896\var{pemfile} or \var{capath} may be \code{None}.
897\end{methoddesc}
898
899\begin{methoddesc}[Context]{set_default_verify_paths}{}
900Specify that the platform provided CA certificates are to be used for
Jean-Paul Calderone1d287e52009-03-07 09:09:07 -0500901verification purposes. This method may not work properly on OS X.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500902\end{methoddesc}
903
904\begin{methoddesc}[Context]{load_tmp_dh}{dhfile}
905Load parameters for Ephemeral Diffie-Hellman from \var{dhfile}.
906\end{methoddesc}
907
908\begin{methoddesc}[Context]{set_app_data}{data}
909Associate \var{data} with this Context object. \var{data} can be retrieved
910later using the \method{get_app_data} method.
911\end{methoddesc}
912
913\begin{methoddesc}[Context]{set_cipher_list}{ciphers}
914Set the list of ciphers to be used in this context. See the OpenSSL manual for
915more information (e.g. ciphers(1))
916\end{methoddesc}
917
918\begin{methoddesc}[Context]{set_info_callback}{callback}
919Set the information callback to \var{callback}. This function will be called
920from time to time during SSL handshakes.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500921\var{callback} should take three arguments: a Connection object and two
922integers. The first integer specifies where in the SSL handshake the function
923was called, and the other the return code from a (possibly failed) internal
924function call.
925\end{methoddesc}
926
927\begin{methoddesc}[Context]{set_options}{options}
928Add SSL options. Options you have set before are not cleared!
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500929This method should be used with the \constant{OP_*} constants.
930\end{methoddesc}
931
932\begin{methoddesc}[Context]{set_passwd_cb}{callback\optional{, userdata}}
933Set the passphrase callback to \var{callback}. This function will be called
Jean-Paul Calderone1eeb29e2008-10-19 11:50:53 -0400934when a private key with a passphrase is loaded. \var{callback} must accept
935three positional arguments. First, an integer giving the maximum length of
936the passphrase it may return. If the returned passphrase is longer than
937this, it will be truncated. Second, a boolean value which will be true if
938the user should be prompted for the passphrase twice and the callback should
939verify that the two values supplied are equal. Third, the value given as the
940\var{userdata} parameter to \method{set_passwd_cb}. If an error occurs,
941\var{callback} should return a false value (e.g. an empty string).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500942\end{methoddesc}
943
944\begin{methoddesc}[Context]{set_session_id}{name}
945Set the context \var{name} within which a session can be reused for this
946Context object. This is needed when doing session resumption, because there is
947no way for a stored session to know which Context object it is associated with.
948\var{name} may be any binary data.
949\end{methoddesc}
950
951\begin{methoddesc}[Context]{set_timeout}{timeout}
952Set the timeout for newly created sessions for this Context object to
953\var{timeout}. \var{timeout} must be given in (whole) seconds. The default
954value is 300 seconds. See the OpenSSL manual for more information (e.g.
955SSL_CTX_set_timeout(3)).
956\end{methoddesc}
957
958\begin{methoddesc}[Context]{set_verify}{mode, callback}
959Set the verification flags for this Context object to \var{mode} and specify
960that \var{callback} should be used for verification callbacks. \var{mode}
961should be one of \constant{VERIFY_NONE} and \constant{VERIFY_PEER}. If
962\constant{VERIFY_PEER} is used, \var{mode} can be OR:ed with
963\constant{VERIFY_FAIL_IF_NO_PEER_CERT} and \constant{VERIFY_CLIENT_ONCE} to
964further control the behaviour.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500965\var{callback} should take five arguments: A Connection object, an X509 object,
966and three integer variables, which are in turn potential error number, error
967depth and return code. \var{callback} should return true if verification passes
968and false otherwise.
969\end{methoddesc}
970
971\begin{methoddesc}[Context]{set_verify_depth}{depth}
972Set the maximum depth for the certificate chain verification that shall be
973allowed for this Context object.
974\end{methoddesc}
975
976\begin{methoddesc}[Context]{use_certificate}{cert}
977Use the certificate \var{cert} which has to be a X509 object.
978\end{methoddesc}
979
Jean-Paul Calderone87b40602008-02-19 21:13:25 -0500980\begin{methoddesc}[Context]{add_extra_chain_cert}{cert}
981Adds the certificate \var{cert}, which has to be a X509 object, to the
982certificate chain presented together with the certificate.
983\end{methoddesc}
984
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500985\begin{methoddesc}[Context]{use_certificate_chain_file}{file}
986Load a certificate chain from \var{file} which must be PEM encoded.
987\end{methoddesc}
988
989\begin{methoddesc}[Context]{use_privatekey}{pkey}
990Use the private key \var{pkey} which has to be a PKey object.
991\end{methoddesc}
992
993\begin{methoddesc}[Context]{use_certificate_file}{file\optional{, format}}
994Load the first certificate found in \var{file}. The certificate must be in the
995format specified by \var{format}, which is either \constant{FILETYPE_PEM} or
996\constant{FILETYPE_ASN1}. The default is \constant{FILETYPE_PEM}.
997\end{methoddesc}
998
999\begin{methoddesc}[Context]{use_privatekey_file}{file\optional{, format}}
1000Load the first private key found in \var{file}. The private key must be in the
1001format specified by \var{format}, which is either \constant{FILETYPE_PEM} or
1002\constant{FILETYPE_ASN1}. The default is \constant{FILETYPE_PEM}.
1003\end{methoddesc}
1004
1005
1006\subsubsection{Connection objects \label{openssl-connection}}
1007
1008Connection objects have the following methods:
1009
1010\begin{methoddesc}[Connection]{accept}{}
1011Call the \method{accept} method of the underlying socket and set up SSL on the
1012returned socket, using the Context object supplied to this Connection object at
1013creation. Returns a pair \code{(\var{conn}, \var{address})}. where \var{conn}
1014is the new Connection object created, and \var{address} is as returned by the
1015socket's \method{accept}.
1016\end{methoddesc}
1017
1018\begin{methoddesc}[Connection]{bind}{address}
1019Call the \method{bind} method of the underlying socket.
1020\end{methoddesc}
1021
1022\begin{methoddesc}[Connection]{close}{}
1023Call the \method{close} method of the underlying socket. Note: If you want
1024correct SSL closure, you need to call the \method{shutdown} method first.
1025\end{methoddesc}
1026
1027\begin{methoddesc}[Connection]{connect}{address}
1028Call the \method{connect} method of the underlying socket and set up SSL on the
1029socket, using the Context object supplied to this Connection object at
1030creation.
1031\end{methoddesc}
1032
1033\begin{methoddesc}[Connection]{connect_ex}{address}
1034Call the \method{connect_ex} method of the underlying socket and set up SSL on
1035the socket, using the Context object supplied to this Connection object at
1036creation. Note that if the \method{connect_ex} method of the socket doesn't
1037return 0, SSL won't be initialized.
1038\end{methoddesc}
1039
1040\begin{methoddesc}[Connection]{do_handshake}{}
1041Perform an SSL handshake (usually called after \method{renegotiate} or one of
1042\method{set_accept_state} or \method{set_accept_state}). This can raise the
1043same exceptions as \method{send} and \method{recv}.
1044\end{methoddesc}
1045
1046\begin{methoddesc}[Connection]{fileno}{}
1047Retrieve the file descriptor number for the underlying socket.
1048\end{methoddesc}
1049
1050\begin{methoddesc}[Connection]{listen}{backlog}
1051Call the \method{listen} method of the underlying socket.
1052\end{methoddesc}
1053
1054\begin{methoddesc}[Connection]{get_app_data}{}
1055Retrieve application data as set by \method{set_app_data}.
1056\end{methoddesc}
1057
1058\begin{methoddesc}[Connection]{get_cipher_list}{}
1059Retrieve the list of ciphers used by the Connection object. WARNING: This API
1060has changed. It used to take an optional parameter and just return a string,
1061but not it returns the entire list in one go.
1062\end{methoddesc}
1063
1064\begin{methoddesc}[Connection]{get_context}{}
1065Retrieve the Context object associated with this Connection.
1066\end{methoddesc}
1067
1068\begin{methoddesc}[Connection]{get_peer_certificate}{}
1069Retrieve the other side's certificate (if any)
1070\end{methoddesc}
1071
1072\begin{methoddesc}[Connection]{getpeername}{}
1073Call the \method{getpeername} method of the underlying socket.
1074\end{methoddesc}
1075
1076\begin{methoddesc}[Connection]{getsockname}{}
1077Call the \method{getsockname} method of the underlying socket.
1078\end{methoddesc}
1079
1080\begin{methoddesc}[Connection]{getsockopt}{level, optname\optional{, buflen}}
1081Call the \method{getsockopt} method of the underlying socket.
1082\end{methoddesc}
1083
1084\begin{methoddesc}[Connection]{pending}{}
Jean-Paul Calderoneb6f57be2008-03-06 21:22:16 -05001085Retrieve the number of bytes that can be safely read from the SSL buffer
1086(\emph{not} the underlying transport buffer).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001087\end{methoddesc}
1088
1089\begin{methoddesc}[Connection]{recv}{bufsize}
1090Receive data from the Connection. The return value is a string representing the
1091data received. The maximum amount of data to be received at once, is specified
1092by \var{bufsize}.
1093\end{methoddesc}
1094
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001095\begin{methoddesc}[Connection]{bio_write}{bytes}
1096If the Connection was created with a memory BIO, this method can be used to add
1097bytes to the read end of that memory BIO. The Connection can then read the
1098bytes (for example, in response to a call to \method{recv}).
1099\end{methoddesc}
1100
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001101\begin{methoddesc}[Connection]{renegotiate}{}
1102Renegotiate the SSL session. Call this if you wish to change cipher suites or
1103anything like that.
1104\end{methoddesc}
1105
1106\begin{methoddesc}[Connection]{send}{string}
1107Send the \var{string} data to the Connection.
1108\end{methoddesc}
1109
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001110\begin{methoddesc}[Connection]{bio_read}{bufsize}
1111If the Connection was created with a memory BIO, this method can be used to
1112read bytes from the write end of that memory BIO. Many Connection methods will
1113add bytes which must be read in this manner or the buffer will eventually fill
1114up and the Connection will be able to take no further actions.
1115\end{methoddesc}
1116
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001117\begin{methoddesc}[Connection]{sendall}{string}
1118Send all of the \var{string} data to the Connection. This calls \method{send}
1119repeatedly until all data is sent. If an error occurs, it's impossible to tell
1120how much data has been sent.
1121\end{methoddesc}
1122
1123\begin{methoddesc}[Connection]{set_accept_state}{}
1124Set the connection to work in server mode. The handshake will be handled
1125automatically by read/write.
1126\end{methoddesc}
1127
1128\begin{methoddesc}[Connection]{set_app_data}{data}
1129Associate \var{data} with this Connection object. \var{data} can be retrieved
1130later using the \method{get_app_data} method.
1131\end{methoddesc}
1132
1133\begin{methoddesc}[Connection]{set_connect_state}{}
1134Set the connection to work in client mode. The handshake will be handled
1135automatically by read/write.
1136\end{methoddesc}
1137
1138\begin{methoddesc}[Connection]{setblocking}{flag}
1139Call the \method{setblocking} method of the underlying socket.
1140\end{methoddesc}
1141
1142\begin{methoddesc}[Connection]{setsockopt}{level, optname, value}
1143Call the \method{setsockopt} method of the underlying socket.
1144\end{methoddesc}
1145
1146\begin{methoddesc}[Connection]{shutdown}{}
1147Send the shutdown message to the Connection. Returns true if the shutdown
1148message exchange is completed and false otherwise (in which case you call
1149\method{recv()} or \method{send()} when the connection becomes
1150readable/writeable.
1151\end{methoddesc}
1152
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001153\begin{methoddesc}[Connection]{get_shutdown}{}
1154Get the shutdown state of the Connection. Returns a bitvector of either or
1155both of \var{SENT_SHUTDOWN} and \var{RECEIVED_SHUTDOWN}.
1156\end{methoddesc}
1157
1158\begin{methoddesc}[Connection]{set_shutdown}{state}
1159Set the shutdown state of the Connection. \var{state} is a bitvector of
1160either or both of \var{SENT_SHUTDOWN} and \var{RECEIVED_SHUTDOWN}.
1161\end{methoddesc}
1162
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001163\begin{methoddesc}[Connection]{sock_shutdown}{how}
1164Call the \method{shutdown} method of the underlying socket.
1165\end{methoddesc}
1166
Jean-Paul Calderoneb6b17122009-05-01 16:36:11 -04001167\begin{methoddesc}[Connection]{bio_shutdown}{}
1168If the Connection was created with a memory BIO, this method can be used to
1169indicate that ``end of file'' has been reached on the read end of that memory
1170BIO.
1171\end{methoddesc}
1172
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001173\begin{methoddesc}[Connection]{state_string}{}
1174Retrieve a verbose string detailing the state of the Connection.
1175\end{methoddesc}
1176
Jean-Paul Calderonefd236f32009-05-03 19:45:07 -04001177\begin{methoddesc}[Connection]{client_random}{}
1178Retrieve the random value used with the client hello message.
1179\end{methoddesc}
1180
1181\begin{methoddesc}[Connection]{server_random}{}
1182Retrieve the random value used with the server hello message.
1183\end{methoddesc}
1184
1185\begin{methoddesc}[Connection]{master_key}{}
1186Retrieve the value of the master key for this session.
1187\end{methoddesc}
1188
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001189\begin{methoddesc}[Connection]{want_read}{}
1190Checks if more data has to be read from the transport layer to complete an
1191operation.
1192\end{methoddesc}
1193
1194\begin{methoddesc}[Connection]{want_write}{}
1195Checks if there is data to write to the transport layer to complete an
1196operation.
1197\end{methoddesc}
1198
1199
1200
1201\section{Internals \label{internals}}
1202
1203We ran into three main problems developing this: Exceptions, callbacks and
1204accessing socket methods. This is what this chapter is about.
1205
1206\subsection{Exceptions \label{exceptions}}
1207
1208We realized early that most of the exceptions would be raised by the I/O
1209functions of OpenSSL, so it felt natural to mimic OpenSSL's error code system,
1210translating them into Python exceptions. This naturally gives us the exceptions
1211\exception{SSL.ZeroReturnError}, \exception{SSL.WantReadError},
1212\exception{SSL.WantWriteError}, \exception{SSL.WantX509LookupError} and
1213\exception{SSL.SysCallError}.
1214
1215For more information about this, see section \ref{openssl-ssl}.
1216
1217
1218\subsection{Callbacks \label{callbacks}}
1219
1220There are a number of problems with callbacks. First of all, OpenSSL is written
1221as a C library, it's not meant to have Python callbacks, so a way around that
1222is needed. Another problem is thread support. A lot of the OpenSSL I/O
1223functions can block if the socket is in blocking mode, and then you want other
1224Python threads to be able to do other things. The real trouble is if you've
Jean-Paul Calderoneb7d6db22008-09-21 18:57:56 -04001225released the global CPython interpreter lock to do a potentially blocking
1226operation, and the operation calls a callback. Then we must take the GIL back,
1227since calling Python APIs without holding it is not allowed.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001228
1229There are two solutions to the first problem, both of which are necessary. The
1230first solution to use is if the C callback allows ''userdata'' to be passed to
1231it (an arbitrary pointer normally). This is great! We can set our Python
1232function object as the real userdata and emulate userdata for the Python
1233function in another way. The other solution can be used if an object with an
1234''app_data'' system always is passed to the callback. For example, the SSL
1235object in OpenSSL has app_data functions and in e.g. the verification
1236callbacks, you can retrieve the related SSL object. What we do is to set our
1237wrapper \class{Connection} object as app_data for the SSL object, and we can
1238easily find the Python callback.
1239
Jean-Paul Calderoneb7d6db22008-09-21 18:57:56 -04001240The other problem is solved using thread local variables. Whenever the GIL is
1241released before calling into an OpenSSL API, the PyThreadState pointer returned
1242by \cfunction{PyEval_SaveState} is stored in a global thread local variable
1243(using Python's own TLS API, \cfunction{PyThread_set_key_value}). When it is
1244necessary to re-acquire the GIL, either after the OpenSSL API returns or in a C
1245callback invoked by that OpenSSL API, the value of the thread local variable is
1246retrieved (\cfunction{PyThread_get_key_value}) and used to re-acquire the GIL.
1247This allows Python threads to execute while OpenSSL APIs are running and allows
1248use of any particular pyOpenSSL object from any Python thread, since there is
1249no per-thread state associated with any of these objects and since OpenSSL is
1250threadsafe (as long as properly initialized, as pyOpenSSL initializes it).
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001251
1252
1253\subsection{Acessing Socket Methods \label{socket-methods}}
1254
1255We quickly saw the benefit of wrapping socket methods in the
1256\class{SSL.Connection} class, for an easy transition into using SSL. The
1257problem here is that the \module{socket} module lacks a C API, and all the
1258methods are declared static. One approach would be to have \module{OpenSSL} as
1259a submodule to the \module{socket} module, placing all the code in
1260\file{socketmodule.c}, but this is obviously not a good solution, since you
1261might not want to import tonnes of extra stuff you're not going to use when
1262importing the \module{socket} module. The other approach is to somehow get a
1263pointer to the method to be called, either the C function, or a callable Python
1264object. This is not really a good solution either, since there's a lot of
1265lookups involved.
1266
1267The way it works is that you have to supply a ``\class{socket}-like'' transport
1268object to the \class{SSL.Connection}. The only requirement of this object is
1269that it has a \method{fileno()} method that returns a file descriptor that's
1270valid at the C level (i.e. you can use the system calls read and write). If you
1271want to use the \method{connect()} or \method{accept()} methods of the
1272\class{SSL.Connection} object, the transport object has to supply such
1273methods too. Apart from them, any method lookups in the \class{SSL.Connection}
1274object that fail are passed on to the underlying transport object.
1275
1276Future changes might be to allow Python-level transport objects, that instead
1277of having \method{fileno()} methods, have \method{read()} and \method{write()}
1278methods, so more advanced features of Python can be used. This would probably
1279entail some sort of OpenSSL ``BIOs'', but converting Python strings back and
1280forth is expensive, so this shouldn't be used unless necessary. Other nice
1281things would be to be able to pass in different transport objects for reading
1282and writing, but then the \method{fileno()} method of \class{SSL.Connection}
1283becomes virtually useless. Also, should the method resolution be used on the
1284read-transport or the write-transport?
1285
1286
1287\end{document}