~exarkun/pyopenssl/trunk

« back to all changes in this revision

Viewing changes to doc/pyOpenSSL.tex

  • Committer: Jean-Paul Calderone
  • Date: 2011-09-11 19:49:43 UTC
  • mfrom: (156.3.22 sphinx-doc)
  • Revision ID: exarkun@divmod.com-20110911194943-ucaan2tzidk7ek5l
Convert the documentation from LaTeX/epytext to Sphinx/ReST

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
\documentclass{howto}
2
 
 
3
 
\title{Python OpenSSL Manual}
4
 
 
5
 
\release{0.13}
6
 
 
7
 
\author{Jean-Paul Calderone}
8
 
\authoraddress{\email{exarkun@twistedmatrix.com}}
9
 
 
10
 
\usepackage[english]{babel}
11
 
\usepackage[T1]{fontenc}
12
 
 
13
 
\begin{document}
14
 
 
15
 
\maketitle
16
 
 
17
 
\begin{abstract}
18
 
\noindent
19
 
This module is a rather thin wrapper around (a subset of) the OpenSSL library.
20
 
With thin wrapper I mean that a lot of the object methods do nothing more than
21
 
calling a corresponding function in the OpenSSL library.
22
 
\end{abstract}
23
 
 
24
 
\tableofcontents
25
 
 
26
 
 
27
 
\section{Introduction \label{intro}}
28
 
 
29
 
The reason pyOpenSSL was created is that the SSL support in the socket module
30
 
in Python 2.1 (the contemporary version of Python when the pyOpenSSL project
31
 
was begun) was severely limited.  Other OpenSSL wrappers for Python at the time
32
 
were also limited, though in different ways.  Unfortunately, Python's standard
33
 
library SSL support has remained weak, although other packages (such as
34
 
M2Crypto\footnote{See \url{http://chandlerproject.org/Projects/MeTooCrypto}})
35
 
have made great advances and now equal or exceed pyOpenSSL's functionality.
36
 
 
37
 
The reason pyOpenSSL continues to be maintained is that there is a significant
38
 
user community around it, as well as a large amount of software which depends
39
 
on it.  It is a great benefit to many people for pyOpenSSL to continue to exist
40
 
and advance.
41
 
 
42
 
\section{Building and Installing \label{building}}
43
 
 
44
 
These instructions can also be found in the file \verb|INSTALL|.
45
 
 
46
 
I have tested this on Debian Linux systems (woody and sid), Solaris 2.6 and
47
 
2.7. Others have successfully compiled it on Windows and NT.
48
 
 
49
 
\subsection{Building the Module on a Unix System \label{building-unix}}
50
 
 
51
 
pyOpenSSL uses distutils, so there really shouldn't be any problems. To build
52
 
the library:
53
 
\begin{verbatim}
54
 
python setup.py build
55
 
\end{verbatim}
56
 
 
57
 
If your OpenSSL header files aren't in \verb|/usr/include|, you may need to
58
 
supply the \verb|-I| flag to let the setup script know where to look. The same
59
 
goes 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}
63
 
python setup.py build_ext -I/usr/local/ssl/include -L/usr/local/ssl/lib
64
 
python setup.py build
65
 
\end{verbatim}
66
 
 
67
 
Now 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,
69
 
so just:
70
 
\begin{verbatim}
71
 
python setup.py install
72
 
\end{verbatim}
73
 
 
74
 
If 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
 
 
77
 
You can, of course, do
78
 
\begin{verbatim}
79
 
python setup.py --help
80
 
\end{verbatim}
81
 
 
82
 
to find out more about how to use the script.
83
 
 
84
 
\subsection{Building the Module on a Windows System \label{building-windows}}
85
 
 
86
 
Big thanks to Itamar Shtull-Trauring and Oleg Orlov for their help with
87
 
Windows build instructions.  Same as for Unix systems, we have to separate
88
 
the \verb|build_ext| and the \verb|build|.
89
 
 
90
 
Building the library:
91
 
 
92
 
\begin{verbatim}
93
 
setup.py build_ext -I ...\openssl\inc32 -L ...\openssl\out32dll
94
 
setup.py build
95
 
\end{verbatim}
96
 
 
97
 
Where \verb|...\openssl| is of course the location of your OpenSSL installation.
98
 
 
99
 
Installation is the same as for Unix systems:
100
 
\begin{verbatim}
101
 
setup.py install
102
 
\end{verbatim}
103
 
 
104
 
And similarily, you can do
105
 
\begin{verbatim}
106
 
setup.py --help
107
 
\end{verbatim}
108
 
 
109
 
to 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
 
 
117
 
This package provides a high-level interface to the functions in the
118
 
OpenSSL library. The following modules are defined:
119
 
 
120
 
\begin{datadesc}{crypto}
121
 
Generic cryptographic module. Note that if anything is incomplete, this module is!
122
 
\end{datadesc}
123
 
 
124
 
\begin{datadesc}{rand}
125
 
An interface to the OpenSSL pseudo random number generator.
126
 
\end{datadesc}
127
 
 
128
 
\begin{datadesc}{SSL}
129
 
An 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}
141
 
See \class{X509}.
142
 
\end{datadesc}
143
 
 
144
 
\begin{classdesc}{X509}{}
145
 
A class representing X.509 certificates.
146
 
\end{classdesc}
147
 
 
148
 
\begin{datadesc}{X509NameType}
149
 
See \class{X509Name}.
150
 
\end{datadesc}
151
 
 
152
 
\begin{classdesc}{X509Name}{x509name}
153
 
A class representing X.509 Distinguished Names.
154
 
 
155
 
This constructor creates a copy of \var{x509name} which should be an
156
 
instance of \class{X509Name}.
157
 
\end{classdesc}
158
 
 
159
 
\begin{datadesc}{X509ReqType}
160
 
See \class{X509Req}.
161
 
\end{datadesc}
162
 
 
163
 
\begin{classdesc}{X509Req}{}
164
 
A class representing X.509 certificate requests.
165
 
\end{classdesc}
166
 
 
167
 
\begin{datadesc}{X509StoreType}
168
 
A Python type object representing the X509Store object type.
169
 
\end{datadesc}
170
 
 
171
 
\begin{datadesc}{PKeyType}
172
 
See \class{PKey}.
173
 
\end{datadesc}
174
 
 
175
 
\begin{classdesc}{PKey}{}
176
 
A class representing DSA or RSA keys.
177
 
\end{classdesc}
178
 
 
179
 
\begin{datadesc}{PKCS7Type}
180
 
A Python type object representing the PKCS7 object type.
181
 
\end{datadesc}
182
 
 
183
 
\begin{datadesc}{PKCS12Type}
184
 
A Python type object representing the PKCS12 object type.
185
 
\end{datadesc}
186
 
 
187
 
\begin{datadesc}{X509ExtensionType}
188
 
See \class{X509Extension}.
189
 
\end{datadesc}
190
 
 
191
 
\begin{classdesc}{X509Extension}{typename, critical, value\optional{, subject}\optional{, issuer}}
192
 
A class representing an X.509 v3 certificate extensions.
193
 
See \url{http://openssl.org/docs/apps/x509v3_config.html\#STANDARD_EXTENSIONS}
194
 
for \var{typename} strings and their options.
195
 
Optional parameters \var{subject} and \var{issuer} must be X509 objects.
196
 
\end{classdesc}
197
 
 
198
 
\begin{datadesc}{NetscapeSPKIType}
199
 
See \class{NetscapeSPKI}.
200
 
\end{datadesc}
201
 
 
202
 
\begin{classdesc}{NetscapeSPKI}{\optional{enc}}
203
 
A class representing Netscape SPKI objects.
204
 
 
205
 
If the \var{enc} argument is present, it should be a base64-encoded string
206
 
representing a NetscapeSPKI object, as returned by the \method{b64_encode}
207
 
method.
208
 
\end{classdesc}
209
 
 
210
 
\begin{classdesc}{CRL}{}
211
 
A class representing Certifcate Revocation List objects.
212
 
\end{classdesc}
213
 
 
214
 
\begin{classdesc}{Revoked}{}
215
 
A class representing Revocation objects of CRL.
216
 
\end{classdesc}
217
 
 
218
 
\begin{datadesc}{FILETYPE_PEM}
219
 
\dataline{FILETYPE_ASN1}
220
 
File type constants.
221
 
\end{datadesc}
222
 
 
223
 
\begin{datadesc}{TYPE_RSA}
224
 
\dataline{TYPE_DSA}
225
 
Key type constants.
226
 
\end{datadesc}
227
 
 
228
 
\begin{excdesc}{Error}
229
 
Generic exception used in the \module{crypto} module.
230
 
\end{excdesc}
231
 
 
232
 
\begin{funcdesc}{dump_certificate}{type, cert}
233
 
Dump 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}
238
 
Dump the certificate request \var{req} into a buffer string encoded with the
239
 
type \var{type}.
240
 
\end{funcdesc}
241
 
 
242
 
\begin{funcdesc}{dump_privatekey}{type, pkey\optional{, cipher, passphrase}}
243
 
Dump 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
245
 
using \var{cipher} and \var{passphrase}.
246
 
 
247
 
\var{passphrase} must be either a string or a callback for providing the
248
 
pass phrase.
249
 
\end{funcdesc}
250
 
 
251
 
\begin{funcdesc}{load_certificate}{type, buffer}
252
 
Load a certificate (X509) from the string \var{buffer} encoded with the
253
 
type \var{type}.
254
 
\end{funcdesc}
255
 
 
256
 
\begin{funcdesc}{load_certificate_request}{type, buffer}
257
 
Load a certificate request (X509Req) from the string \var{buffer} encoded with
258
 
the type \var{type}.
259
 
\end{funcdesc}
260
 
 
261
 
\begin{funcdesc}{load_privatekey}{type, buffer\optional{, passphrase}}
262
 
Load a private key (PKey) from the string \var{buffer} encoded with
263
 
the 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
267
 
pass phrase.
268
 
\end{funcdesc}
269
 
 
270
 
\begin{funcdesc}{load_crl}{type, buffer}
271
 
Load Certificate Revocation List (CRL) data from a string \var{buffer}.
272
 
\var{buffer} encoded with the type \var{type}.  The type \var{type}
273
 
must either \constant{FILETYPE_PEM} or \constant{FILETYPE_ASN1}).
274
 
\end{funcdesc}
275
 
 
276
 
\begin{funcdesc}{load_pkcs7_data}{type, buffer}
277
 
Load 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}}
281
 
Load pkcs12 data from the string \var{buffer}. If the pkcs12 structure is
282
 
encrypted, a \var{passphrase} must be included.  The MAC is always
283
 
checked and thus required.
284
 
 
285
 
See also the man page for the C function \function{PKCS12_parse}.
286
 
\end{funcdesc}
287
 
 
288
 
\begin{funcdesc}{sign}{key, data, digest}
289
 
Sign a data string using the given key and message digest.
290
 
 
291
 
\var{key} is a \code{PKey} instance.  \var{data} is a \code{str} instance.
292
 
\var{digest} is a \code{str} naming a supported message digest type, for example
293
 
\code{``sha1''}.
294
 
\versionadded{0.11}
295
 
\end{funcdesc}
296
 
 
297
 
\begin{funcdesc}{verify}{certificate, signature, data, digest}
298
 
Verify the signature for a data string.
299
 
 
300
 
\var{certificate} is a \code{X509} instance corresponding to the private key
301
 
which generated the signature.  \var{signature} is a \var{str} instance giving
302
 
the signature itself.  \var{data} is a \var{str} instance giving the data to
303
 
which the signature applies.  \var{digest} is a \var{str} instance naming the
304
 
message digest type of the signature, for example \code{``sha1''}.
305
 
\versionadded{0.11}
306
 
\end{funcdesc}
307
 
 
308
 
\subsubsection{X509 objects \label{openssl-x509}}
309
 
 
310
 
X509 objects have the following methods:
311
 
 
312
 
\begin{methoddesc}[X509]{get_issuer}{}
313
 
Return an X509Name object representing the issuer of the certificate.
314
 
\end{methoddesc}
315
 
 
316
 
\begin{methoddesc}[X509]{get_pubkey}{}
317
 
Return a PKey object representing the public key of the certificate.
318
 
\end{methoddesc}
319
 
 
320
 
\begin{methoddesc}[X509]{get_serial_number}{}
321
 
Return the certificate serial number.
322
 
\end{methoddesc}
323
 
 
324
 
\begin{methoddesc}[X509]{get_signature_algorithm}{}
325
 
Return the signature algorithm used in the certificate.  If the algorithm is
326
 
undefined, raise \code{ValueError}.
327
 
\end{methoddesc}
328
 
 
329
 
\begin{methoddesc}[X509]{get_subject}{}
330
 
Return an X509Name object representing the subject of the certificate.
331
 
\end{methoddesc}
332
 
 
333
 
\begin{methoddesc}[X509]{get_version}{}
334
 
Return the certificate version.
335
 
\end{methoddesc}
336
 
 
337
 
\begin{methoddesc}[X509]{get_notBefore}{}
338
 
Return a string giving the time before which the certificate is not valid.  The
339
 
string is formatted as an ASN1 GENERALIZEDTIME:
340
 
\begin{verbatim}
341
 
                 YYYYMMDDhhmmssZ
342
 
                 YYYYMMDDhhmmss+hhmm
343
 
                 YYYYMMDDhhmmss-hhmm
344
 
\end{verbatim}
345
 
If no value exists for this field, \code{None} is returned.
346
 
\end{methoddesc}
347
 
 
348
 
\begin{methoddesc}[X509]{get_notAfter}{}
349
 
Return a string giving the time after which the certificate is not valid.  The
350
 
string is formatted as an ASN1 GENERALIZEDTIME:
351
 
\begin{verbatim}
352
 
                 YYYYMMDDhhmmssZ
353
 
                 YYYYMMDDhhmmss+hhmm
354
 
                 YYYYMMDDhhmmss-hhmm
355
 
\end{verbatim}
356
 
If no value exists for this field, \code{None} is returned.
357
 
\end{methoddesc}
358
 
 
359
 
\begin{methoddesc}[X509]{set_notBefore}{when}
360
 
Change the time before which the certificate is not valid.  \var{when} is a
361
 
string formatted as an ASN1 GENERALIZEDTIME:
362
 
\begin{verbatim}
363
 
                 YYYYMMDDhhmmssZ
364
 
                 YYYYMMDDhhmmss+hhmm
365
 
                 YYYYMMDDhhmmss-hhmm
366
 
\end{verbatim}
367
 
\end{methoddesc}
368
 
 
369
 
\begin{methoddesc}[X509]{set_notAfter}{when}
370
 
Change the time after which the certificate is not valid.  \var{when} is a
371
 
string formatted as an ASN1 GENERALIZEDTIME:
372
 
\begin{verbatim}
373
 
                 YYYYMMDDhhmmssZ
374
 
                 YYYYMMDDhhmmss+hhmm
375
 
                 YYYYMMDDhhmmss-hhmm
376
 
\end{verbatim}
377
 
\end{methoddesc}
378
 
 
379
 
\begin{methoddesc}[X509]{gmtime_adj_notBefore}{time}
380
 
Adjust the timestamp (in GMT) when the certificate starts being valid.
381
 
\end{methoddesc}
382
 
 
383
 
\begin{methoddesc}[X509]{gmtime_adj_notAfter}{time}
384
 
Adjust the timestamp (in GMT) when the certificate stops being valid.
385
 
\end{methoddesc}
386
 
 
387
 
\begin{methoddesc}[X509]{has_expired}{}
388
 
Checks the certificate's time stamp against current time. Returns true if the
389
 
certificate has expired and false otherwise.
390
 
\end{methoddesc}
391
 
 
392
 
\begin{methoddesc}[X509]{set_issuer}{issuer}
393
 
Set the issuer of the certificate to \var{issuer}.
394
 
\end{methoddesc}
395
 
 
396
 
\begin{methoddesc}[X509]{set_pubkey}{pkey}
397
 
Set the public key of the certificate to \var{pkey}.
398
 
\end{methoddesc}
399
 
 
400
 
\begin{methoddesc}[X509]{set_serial_number}{serialno}
401
 
Set the serial number of the certificate to \var{serialno}.
402
 
\end{methoddesc}
403
 
 
404
 
\begin{methoddesc}[X509]{set_subject}{subject}
405
 
Set the subject of the certificate to \var{subject}.
406
 
\end{methoddesc}
407
 
 
408
 
\begin{methoddesc}[X509]{set_version}{version}
409
 
Set the certificate version to \var{version}.
410
 
\end{methoddesc}
411
 
 
412
 
\begin{methoddesc}[X509]{sign}{pkey, digest}
413
 
Sign the certificate, using the key \var{pkey} and the message digest algorithm
414
 
identified by the string \var{digest}.
415
 
\end{methoddesc}
416
 
 
417
 
\begin{methoddesc}[X509]{subject_name_hash}{}
418
 
Return the hash of the certificate subject.
419
 
\end{methoddesc}
420
 
 
421
 
\begin{methoddesc}[X509]{digest}{digest_name}
422
 
Return a digest of the certificate, using the \var{digest_name} method.
423
 
\var{digest_name} must be a string describing a digest algorithm supported
424
 
by OpenSSL (by EVP_get_digestbyname, specifically).  For example,
425
 
\constant{"md5"} or \constant{"sha1"}.
426
 
\end{methoddesc}
427
 
 
428
 
\begin{methoddesc}[X509]{add_extensions}{extensions}
429
 
Add the extensions in the sequence \var{extensions} to the certificate.
430
 
\end{methoddesc}
431
 
 
432
 
\begin{methoddesc}[X509]{get_extension_count}{}
433
 
Return the number of extensions on this certificate.
434
 
\versionadded{0.12}
435
 
\end{methoddesc}
436
 
 
437
 
\begin{methoddesc}[X509]{get_extension}{index}
438
 
Retrieve the extension on this certificate at the given index.
439
 
 
440
 
Extensions on a certificate are kept in order.  The index parameter selects
441
 
which extension will be returned.  The returned object will be an X509Extension
442
 
instance.
443
 
\versionadded{0.12}
444
 
\end{methoddesc}
445
 
 
446
 
\subsubsection{X509Name objects \label{openssl-x509name}}
447
 
 
448
 
X509Name objects have the following methods:
449
 
 
450
 
\begin{methoddesc}[X509Name]{hash}{}
451
 
Return an integer giving the first four bytes of the MD5 digest of the DER
452
 
representation of the name.
453
 
\end{methoddesc}
454
 
 
455
 
\begin{methoddesc}[X509Name]{der}{}
456
 
Return a string giving the DER representation of the name.
457
 
\end{methoddesc}
458
 
 
459
 
\begin{methoddesc}[X509Name]{get_components}{}
460
 
Return a list of two-tuples of strings giving the components of the name.
461
 
\end{methoddesc}
462
 
 
463
 
X509Name objects have the following members:
464
 
 
465
 
\begin{memberdesc}[X509Name]{countryName}
466
 
The country of the entity. \code{C} may be used as an alias for
467
 
\code{countryName}.
468
 
\end{memberdesc}
469
 
 
470
 
\begin{memberdesc}[X509Name]{stateOrProvinceName}
471
 
The state or province of the entity. \code{ST} may be used as an alias for
472
 
\code{stateOrProvinceName}�
473
 
\end{memberdesc}
474
 
 
475
 
\begin{memberdesc}[X509Name]{localityName}
476
 
The locality of the entity. \code{L} may be used as an alias for
477
 
\code{localityName}.
478
 
\end{memberdesc}
479
 
 
480
 
\begin{memberdesc}[X509Name]{organizationName}
481
 
The organization name of the entity. \code{O} may be used as an alias for
482
 
\code{organizationName}.
483
 
\end{memberdesc}
484
 
 
485
 
\begin{memberdesc}[X509Name]{organizationalUnitName}
486
 
The organizational unit of the entity. \code{OU} may be used as an alias for
487
 
\code{organizationalUnitName}.
488
 
\end{memberdesc}
489
 
 
490
 
\begin{memberdesc}[X509Name]{commonName}
491
 
The common name of the entity. \code{CN} may be used as an alias for
492
 
\code{commonName}.
493
 
\end{memberdesc}
494
 
 
495
 
\begin{memberdesc}[X509Name]{emailAddress}
496
 
The e-mail address of the entity.
497
 
\end{memberdesc}
498
 
 
499
 
\subsubsection{X509Req objects \label{openssl-x509req}}
500
 
 
501
 
X509Req objects have the following methods:
502
 
 
503
 
\begin{methoddesc}[X509Req]{get_pubkey}{}
504
 
Return a PKey object representing the public key of the certificate request.
505
 
\end{methoddesc}
506
 
 
507
 
\begin{methoddesc}[X509Req]{get_subject}{}
508
 
Return an X509Name object representing the subject of the certificate.
509
 
\end{methoddesc}
510
 
 
511
 
\begin{methoddesc}[X509Req]{set_pubkey}{pkey}
512
 
Set the public key of the certificate request to \var{pkey}.
513
 
\end{methoddesc}
514
 
 
515
 
\begin{methoddesc}[X509Req]{sign}{pkey, digest}
516
 
Sign the certificate request, using the key \var{pkey} and the message digest
517
 
algorithm identified by the string \var{digest}.
518
 
\end{methoddesc}
519
 
 
520
 
\begin{methoddesc}[X509Req]{verify}{pkey}
521
 
Verify a certificate request using the public key \var{pkey}.
522
 
\end{methoddesc}
523
 
 
524
 
\begin{methoddesc}[X509Req]{set_version}{version}
525
 
Set the version (RFC 2459, 4.1.2.1) of the certificate request to
526
 
\var{version}.
527
 
\end{methoddesc}
528
 
 
529
 
\begin{methoddesc}[X509Req]{get_version}{}
530
 
Get the version (RFC 2459, 4.1.2.1) of the certificate request.
531
 
\end{methoddesc}
532
 
 
533
 
\subsubsection{X509Store objects \label{openssl-x509store}}
534
 
 
535
 
The X509Store object has currently just one method:
536
 
 
537
 
\begin{methoddesc}[X509Store]{add_cert}{cert}
538
 
Add the certificate \var{cert} to the certificate store.
539
 
\end{methoddesc}
540
 
 
541
 
\subsubsection{PKey objects \label{openssl-pkey}}
542
 
 
543
 
The PKey object has the following methods:
544
 
 
545
 
\begin{methoddesc}[PKey]{bits}{}
546
 
Return the number of bits of the key.
547
 
\end{methoddesc}
548
 
 
549
 
\begin{methoddesc}[PKey]{generate_key}{type, bits}
550
 
Generate a public/private key pair of the type \var{type} (one of
551
 
\constant{TYPE_RSA} and \constant{TYPE_DSA}) with the size \var{bits}.
552
 
\end{methoddesc}
553
 
 
554
 
\begin{methoddesc}[PKey]{type}{}
555
 
Return the type of the key.
556
 
\end{methoddesc}
557
 
 
558
 
\begin{methoddesc}[PKey]{check}{}
559
 
Check the consistency of this key, returning True if it is consistent and
560
 
raising an exception otherwise.  This is only valid for RSA keys.  See the
561
 
OpenSSL RSA_check_key man page for further limitations.
562
 
\end{methoddesc}
563
 
 
564
 
\subsubsection{PKCS7 objects \label{openssl-pkcs7}}
565
 
 
566
 
PKCS7 objects have the following methods:
567
 
 
568
 
\begin{methoddesc}[PKCS7]{type_is_signed}{}
569
 
FIXME
570
 
\end{methoddesc}
571
 
 
572
 
\begin{methoddesc}[PKCS7]{type_is_enveloped}{}
573
 
FIXME
574
 
\end{methoddesc}
575
 
 
576
 
\begin{methoddesc}[PKCS7]{type_is_signedAndEnveloped}{}
577
 
FIXME
578
 
\end{methoddesc}
579
 
 
580
 
\begin{methoddesc}[PKCS7]{type_is_data}{}
581
 
FIXME
582
 
\end{methoddesc}
583
 
 
584
 
\begin{methoddesc}[PKCS7]{get_type_name}{}
585
 
Get the type name of the PKCS7.
586
 
\end{methoddesc}
587
 
 
588
 
\subsubsection{PKCS12 objects \label{openssl-pkcs12}}
589
 
 
590
 
PKCS12 objects have the following methods:
591
 
 
592
 
\begin{methoddesc}[PKCS12]{export}{\optional{passphrase=None}\optional{, iter=2048}\optional{, maciter=1}}
593
 
Returns a PKCS12 object as a string.
594
 
 
595
 
The optional \var{passphrase} must be a string not a callback.
596
 
 
597
 
See also the man page for the C function \function{PKCS12_create}.
598
 
\end{methoddesc}
599
 
 
600
 
\begin{methoddesc}[PKCS12]{get_ca_certificates}{}
601
 
Return CA certificates within the PKCS12 object as a tuple. Returns
602
 
\constant{None} if no CA certificates are present.
603
 
\end{methoddesc}
604
 
 
605
 
\begin{methoddesc}[PKCS12]{get_certificate}{}
606
 
Return certificate portion of the PKCS12 structure.
607
 
\end{methoddesc}
608
 
 
609
 
\begin{methoddesc}[PKCS12]{get_friendlyname}{}
610
 
Return friendlyName portion of the PKCS12 structure.
611
 
\end{methoddesc}
612
 
 
613
 
\begin{methoddesc}[PKCS12]{get_privatekey}{}
614
 
Return private key portion of the PKCS12 structure
615
 
\end{methoddesc}
616
 
 
617
 
\begin{methoddesc}[PKCS12]{set_ca_certificates}{cacerts}
618
 
Replace or set the CA certificates within the PKCS12 object with the sequence \var{cacerts}.
619
 
 
620
 
Set \var{cacerts} to \constant{None} to remove all CA certificates.
621
 
\end{methoddesc}
622
 
 
623
 
\begin{methoddesc}[PKCS12]{set_certificate}{cert}
624
 
Replace or set the certificate portion of the PKCS12 structure.
625
 
\end{methoddesc}
626
 
 
627
 
\begin{methoddesc}[PKCS12]{set_friendlyname}{name}
628
 
Replace or set the friendlyName portion of the PKCS12 structure.
629
 
\end{methoddesc}
630
 
 
631
 
\begin{methoddesc}[PKCS12]{set_privatekey}{pkey}
632
 
Replace or set private key portion of the PKCS12 structure
633
 
\end{methoddesc}
634
 
 
635
 
\subsubsection{X509Extension objects \label{openssl-509ext}}
636
 
 
637
 
X509Extension objects have several methods:
638
 
 
639
 
\begin{methoddesc}[X509Extension]{get_critical}{}
640
 
Return the critical field of the extension object.
641
 
\end{methoddesc}
642
 
 
643
 
\begin{methoddesc}[X509Extension]{get_short_name}{}
644
 
Retrieve the short descriptive name for this extension.
645
 
 
646
 
The result is a byte string like \code{``basicConstraints''}.
647
 
\versionadded{0.12}
648
 
\end{methoddesc}
649
 
 
650
 
\begin{methoddesc}[X509Extension]{get_data}{}
651
 
Retrieve the data for this extension.
652
 
 
653
 
The result is the ASN.1 encoded form of the extension data as a byte string.
654
 
\versionadded{0.12}
655
 
\end{methoddesc}
656
 
 
657
 
\subsubsection{NetscapeSPKI objects \label{openssl-netscape-spki}}
658
 
 
659
 
NetscapeSPKI objects have the following methods:
660
 
 
661
 
\begin{methoddesc}[NetscapeSPKI]{b64_encode}{}
662
 
Return a base64-encoded string representation of the object.
663
 
\end{methoddesc}
664
 
 
665
 
\begin{methoddesc}[NetscapeSPKI]{get_pubkey}{}
666
 
Return the public key of object.
667
 
\end{methoddesc}
668
 
 
669
 
\begin{methoddesc}[NetscapeSPKI]{set_pubkey}{key}
670
 
Set the public key of the object to \var{key}.
671
 
\end{methoddesc}
672
 
 
673
 
\begin{methoddesc}[NetscapeSPKI]{sign}{key, digest_name}
674
 
Sign the NetscapeSPKI object using the given \var{key} and
675
 
\var{digest_name}.  \var{digest_name} must be a string describing a digest
676
 
algorithm supported by OpenSSL (by EVP_get_digestbyname, specifically).  For
677
 
example, \constant{"md5"} or \constant{"sha1"}.
678
 
\end{methoddesc}
679
 
 
680
 
\begin{methoddesc}[NetscapeSPKI]{verify}{key}
681
 
Verify the NetscapeSPKI object using the given \var{key}.
682
 
\end{methoddesc}
683
 
 
684
 
\subsubsection{CRL objects \label{crl}}
685
 
 
686
 
CRL objects have the following methods:
687
 
 
688
 
\begin{methoddesc}[CRL]{add_revoked}{revoked}
689
 
Add a Revoked object to the CRL, by value not reference.
690
 
\end{methoddesc}
691
 
 
692
 
\begin{methoddesc}[CRL]{export}{cert, key\optional{, type=FILETYPE_PEM}\optional{, days=100}}
693
 
Use \var{cert} and \var{key} to sign the CRL and return the CRL as a string.
694
 
\var{days} is the number of days before the next CRL is due.
695
 
\end{methoddesc}
696
 
 
697
 
\begin{methoddesc}[CRL]{get_revoked}{}
698
 
Return a tuple of Revoked objects, by value not reference.
699
 
\end{methoddesc}
700
 
 
701
 
\subsubsection{Revoked objects \label{revoked}}
702
 
 
703
 
Revoked objects have the following methods:
704
 
 
705
 
\begin{methoddesc}[Revoked]{all_reasons}{}
706
 
Return a list of all supported reasons.
707
 
\end{methoddesc}
708
 
 
709
 
\begin{methoddesc}[Revoked]{get_reason}{}
710
 
Return the revocation reason as a str.  Can be
711
 
None, which differs from "Unspecified".
712
 
\end{methoddesc}
713
 
 
714
 
\begin{methoddesc}[Revoked]{get_rev_date}{}
715
 
Return the revocation date as a str.
716
 
The string is formatted as an ASN1 GENERALIZEDTIME.
717
 
\end{methoddesc}
718
 
 
719
 
\begin{methoddesc}[Revoked]{get_serial}{}
720
 
Return a str containing a hex number of the serial of the revoked certificate.
721
 
\end{methoddesc}
722
 
 
723
 
\begin{methoddesc}[Revoked]{set_reason}{reason}
724
 
Set the revocation reason.  \var{reason} must
725
 
be None or a string, but the values are limited.  
726
 
Spaces and case are ignored.  See \method{all_reasons}.
727
 
\end{methoddesc}
728
 
 
729
 
\begin{methoddesc}[Revoked]{set_rev_date}{date}
730
 
Set the revocation date.
731
 
The string is formatted as an ASN1 GENERALIZEDTIME.
732
 
\end{methoddesc}
733
 
 
734
 
\begin{methoddesc}[Revoked]{set_serial}{serial}
735
 
\var{serial} is a string containing a hex number of the serial of the revoked certificate.
736
 
\end{methoddesc}
737
 
 
738
 
 
739
 
% % % rand module
740
 
 
741
 
\subsection{\module{rand} --- An interface to the OpenSSL pseudo random number generator \label{openssl-rand}}
742
 
 
743
 
\declaremodule{extension}{rand}
744
 
\modulesynopsis{An interface to the OpenSSL pseudo random number generator}
745
 
 
746
 
This module handles the OpenSSL pseudo random number generator (PRNG) and
747
 
declares the following:
748
 
 
749
 
\begin{funcdesc}{add}{string, entropy}
750
 
Mix bytes from \var{string} into the PRNG state. The \var{entropy} argument is
751
 
(the lower bound of) an estimate of how much randomness is contained in
752
 
\var{string}, measured in bytes. For more information, see e.g. \rfc{1750}.
753
 
\end{funcdesc}
754
 
 
755
 
\begin{funcdesc}{bytes}{num_bytes}
756
 
Get some random bytes from the PRNG as a string.
757
 
 
758
 
This is a wrapper for the C function \function{RAND_bytes}.
759
 
\end{funcdesc}
760
 
 
761
 
\begin{funcdesc}{cleanup}{}
762
 
Erase the memory used by the PRNG.
763
 
 
764
 
This is a wrapper for the C function \function{RAND_cleanup}.
765
 
\end{funcdesc}
766
 
 
767
 
\begin{funcdesc}{egd}{path\optional{, bytes}}
768
 
Query the Entropy Gathering Daemon\footnote{See
769
 
\url{http://www.lothar.com/tech/crypto/}} on socket \var{path} for \var{bytes}
770
 
bytes of random data and and uses \function{add} to seed the PRNG. The default
771
 
value of \var{bytes} is 255.
772
 
\end{funcdesc}
773
 
 
774
 
\begin{funcdesc}{load_file}{path\optional{, bytes}}
775
 
Read \var{bytes} bytes (or all of it, if \var{bytes} is negative) of data from
776
 
the file \var{path} to seed the PRNG. The default value of \var{bytes} is -1.
777
 
\end{funcdesc}
778
 
 
779
 
\begin{funcdesc}{screen}{}
780
 
Add the current contents of the screen to the PRNG state.
781
 
Availability: Windows.
782
 
\end{funcdesc}
783
 
 
784
 
\begin{funcdesc}{seed}{string}
785
 
This is equivalent to calling \function{add} with \var{entropy} as the length
786
 
of the string.
787
 
\end{funcdesc}
788
 
 
789
 
\begin{funcdesc}{status}{}
790
 
Returns true if the PRNG has been seeded with enough data, and false otherwise.
791
 
\end{funcdesc}
792
 
 
793
 
\begin{funcdesc}{write_file}{path}
794
 
Write a number of random bytes (currently 1024) to the file \var{path}. This
795
 
file can then be used with \function{load_file} to seed the PRNG again.
796
 
\end{funcdesc}
797
 
 
798
 
\begin{excdesc}{Error}
799
 
If the current RAND method supports any errors, this is raised when needed.
800
 
The default method does not raise this when the entropy pool is depleted.
801
 
 
802
 
Whenever this exception is raised directly, it has a list of error messages
803
 
from the OpenSSL error queue, where each item is a tuple \code{(\var{lib},
804
 
\var{function}, \var{reason})}. Here \var{lib}, \var{function} and \var{reason}
805
 
are all strings, describing where and what the problem is. See \manpage{err}{3}
806
 
for more information.
807
 
\end{excdesc}
808
 
 
809
 
 
810
 
% % % SSL module
811
 
 
812
 
\subsection{\module{SSL} --- An interface to the SSL-specific parts of OpenSSL \label{openssl-ssl}}
813
 
 
814
 
\declaremodule{extension}{SSL}
815
 
\modulesynopsis{An interface to the SSL-specific parts of OpenSSL}
816
 
 
817
 
This module handles things specific to SSL. There are two objects defined:
818
 
Context, Connection.
819
 
 
820
 
\begin{datadesc}{SSLv2_METHOD}
821
 
\dataline{SSLv3_METHOD}
822
 
\dataline{SSLv23_METHOD}
823
 
\dataline{TLSv1_METHOD}
824
 
These constants represent the different SSL methods to use when creating a
825
 
context object.
826
 
\end{datadesc}
827
 
 
828
 
\begin{datadesc}{VERIFY_NONE}
829
 
\dataline{VERIFY_PEER}
830
 
\dataline{VERIFY_FAIL_IF_NO_PEER_CERT}
831
 
These constants represent the verification mode used by the Context
832
 
object's \method{set_verify} method.
833
 
\end{datadesc}
834
 
 
835
 
\begin{datadesc}{FILETYPE_PEM}
836
 
\dataline{FILETYPE_ASN1}
837
 
File type constants used with the \method{use_certificate_file} and
838
 
\method{use_privatekey_file} methods of Context objects.
839
 
\end{datadesc}
840
 
 
841
 
\begin{datadesc}{OP_SINGLE_DH_USE}
842
 
\dataline{OP_EPHEMERAL_RSA}
843
 
\dataline{OP_NO_SSLv2}
844
 
\dataline{OP_NO_SSLv3}
845
 
\dataline{OP_NO_TLSv1}
846
 
\dataline{OP_NO_TICKET}
847
 
\dataline{OP_NO_COMPRESSION}
848
 
Constants used with \method{set_options} of Context objects.
849
 
\constant{OP_SINGLE_DH_USE} means to always create a new key when using ephemeral
850
 
Diffie-Hellman. \constant{OP_EPHEMERAL_RSA} means to always use ephemeral RSA keys
851
 
when doing RSA operations. \constant{OP_NO_SSLv2}, \constant{OP_NO_SSLv3} and
852
 
\constant{OP_NO_TLSv1} means to disable those specific protocols. This is
853
 
interesting if you're using e.g. \constant{SSLv23_METHOD} to get an SSLv2-compatible
854
 
handshake, but don't want to use SSLv2.
855
 
\end{datadesc}
856
 
 
857
 
\begin{datadesc}{MODE_NO_COMPRESSION}
858
 
Constant used with \method{set_mode} of Context objects to disable automatic
859
 
compression of application traffic.
860
 
\end{datadesc}
861
 
 
862
 
\begin{datadesc}{SSLEAY_VERSION}
863
 
\dataline{SSLEAY_CFLAGS}
864
 
\dataline{SSLEAY_BUILT_ON}
865
 
\dataline{SSLEAY_PLATFORM}
866
 
\dataline{SSLEAY_DIR}
867
 
Constants used with \method{SSLeay_version} to specify what OpenSSL version
868
 
information to retrieve.  See the man page for the \function{SSLeay_version} C
869
 
API for details.
870
 
\end{datadesc}
871
 
 
872
 
\begin{datadesc}{OPENSSL_VERSION_NUMBER}
873
 
An integer giving the version number of the OpenSSL library used to build this
874
 
version of pyOpenSSL.  See the man page for the \function{SSLeay_version} C API
875
 
for details.
876
 
\end{datadesc}
877
 
 
878
 
\begin{funcdesc}{SSLeay_version}{type}
879
 
Retrieve a string describing some aspect of the underlying OpenSSL version.  The
880
 
type passed in should be one of the \constant{SSLEAY_*} constants defined in
881
 
this module.
882
 
\end{funcdesc}
883
 
 
884
 
\begin{datadesc}{ContextType}
885
 
See \class{Context}.
886
 
\end{datadesc}
887
 
 
888
 
\begin{classdesc}{Context}{method}
889
 
A class representing SSL contexts.  Contexts define the parameters of one or
890
 
more SSL connections.
891
 
 
892
 
\var{method} should be \constant{SSLv2_METHOD}, \constant{SSLv3_METHOD},
893
 
\constant{SSLv23_METHOD} or \constant{TLSv1_METHOD}.
894
 
\end{classdesc}
895
 
 
896
 
\begin{datadesc}{ConnectionType}
897
 
See \class{Connection}.
898
 
\end{datadesc}
899
 
 
900
 
\begin{classdesc}{Connection}{context, socket}
901
 
A class representing SSL connections.
902
 
 
903
 
\var{context} should be an instance of \class{Context} and \var{socket}
904
 
should be a socket \footnote{Actually, all that is required is an object
905
 
that \emph{behaves} like a socket, you could even use files, even though
906
 
it'd be tricky to get the handshakes right!} object.  \var{socket} may be
907
 
\var{None}; in this case, the Connection is created with a memory BIO: see
908
 
the \method{bio_read}, \method{bio_write}, and \method{bio_shutdown}
909
 
methods.
910
 
\end{classdesc}
911
 
 
912
 
\begin{excdesc}{Error}
913
 
This exception is used as a base class for the other SSL-related
914
 
exceptions, but may also be raised directly.
915
 
 
916
 
Whenever this exception is raised directly, it has a list of error messages
917
 
from the OpenSSL error queue, where each item is a tuple \code{(\var{lib},
918
 
\var{function}, \var{reason})}. Here \var{lib}, \var{function} and \var{reason}
919
 
are all strings, describing where and what the problem is. See \manpage{err}{3}
920
 
for more information.
921
 
\end{excdesc}
922
 
 
923
 
\begin{excdesc}{ZeroReturnError}
924
 
This exception matches the error return code \code{SSL_ERROR_ZERO_RETURN}, and
925
 
is raised when the SSL Connection has been closed. In SSL 3.0 and TLS 1.0, this
926
 
only occurs if a closure alert has occurred in the protocol, i.e. the
927
 
connection has been closed cleanly. Note that this does not necessarily
928
 
mean that the transport layer (e.g. a socket) has been closed.
929
 
 
930
 
It may seem a little strange that this is an exception, but it does match an
931
 
\code{SSL_ERROR} code, and is very convenient.
932
 
\end{excdesc}
933
 
 
934
 
\begin{excdesc}{WantReadError}
935
 
The operation did not complete; the same I/O method should be called again
936
 
later, with the same arguments. Any I/O method can lead to this since new
937
 
handshakes can occur at any time.
938
 
 
939
 
The wanted read is for \emph{dirty} data sent over the network, not the
940
 
\emph{clean} data inside the tunnel.  For a socket based SSL connection,
941
 
\emph{read} means data coming at us over the network.  Until that read
942
 
succeeds, the attempted \method{OpenSSL.SSL.Connection.recv},
943
 
\method{OpenSSL.SSL.Connection.send}, or
944
 
\method{OpenSSL.SSL.Connection.do_handshake} is prevented or incomplete. You
945
 
probably want to \method{select()} on the socket before trying again.
946
 
\end{excdesc}
947
 
 
948
 
\begin{excdesc}{WantWriteError}
949
 
See \exception{WantReadError}.  The socket send buffer may be too full to
950
 
write more data.
951
 
\end{excdesc}
952
 
 
953
 
\begin{excdesc}{WantX509LookupError}
954
 
The operation did not complete because an application callback has asked to be
955
 
called again. The I/O method should be called again later, with the same
956
 
arguments. Note: This won't occur in this version, as there are no such
957
 
callbacks in this version.
958
 
\end{excdesc}
959
 
 
960
 
\begin{excdesc}{SysCallError}
961
 
The \exception{SysCallError} occurs when there's an I/O error and OpenSSL's
962
 
error queue does not contain any information. This can mean two things: An
963
 
error in the transport protocol, or an end of file that violates the protocol.
964
 
The parameter to the exception is always a pair \code{(\var{errnum},
965
 
\var{errstr})}.
966
 
\end{excdesc}
967
 
 
968
 
 
969
 
\subsubsection{Context objects \label{openssl-context}}
970
 
 
971
 
Context objects have the following methods:
972
 
 
973
 
\begin{methoddesc}[Context]{check_privatekey}{}
974
 
Check if the private key (loaded with \method{use_privatekey\optional{_file}})
975
 
matches the certificate (loaded with \method{use_certificate\optional{_file}}).
976
 
Returns \code{None} if they match, raises \exception{Error} otherwise.
977
 
\end{methoddesc}
978
 
 
979
 
\begin{methoddesc}[Context]{get_app_data}{}
980
 
Retrieve application data as set by \method{set_app_data}.
981
 
\end{methoddesc}
982
 
 
983
 
\begin{methoddesc}[Context]{get_cert_store}{}
984
 
Retrieve the certificate store (a X509Store object) that the context uses.
985
 
This can be used to add "trusted" certificates without using the.
986
 
\method{load_verify_locations()} method.
987
 
\end{methoddesc}
988
 
 
989
 
\begin{methoddesc}[Context]{get_timeout}{}
990
 
Retrieve session timeout, as set by \method{set_timeout}. The default is 300
991
 
seconds.
992
 
\end{methoddesc}
993
 
 
994
 
\begin{methoddesc}[Context]{get_verify_depth}{}
995
 
Retrieve the Context object's verify depth, as set by
996
 
\method{set_verify_depth}.
997
 
\end{methoddesc}
998
 
 
999
 
\begin{methoddesc}[Context]{get_verify_mode}{}
1000
 
Retrieve the Context object's verify mode, as set by \method{set_verify}.
1001
 
\end{methoddesc}
1002
 
 
1003
 
\begin{methoddesc}[Context]{load_client_ca}{pemfile}
1004
 
Read a file with PEM-formatted certificates that will be sent to the client
1005
 
when requesting a client certificate.
1006
 
\end{methoddesc}
1007
 
 
1008
 
\begin{methoddesc}[Context]{set_client_ca_list}{certificate_authorities}
1009
 
Replace the current list of preferred certificate signers that would be
1010
 
sent to the client when requesting a client certificate with the
1011
 
\var{certificate_authorities} sequence of \class{OpenSSL.crypto.X509Name}s.
1012
 
 
1013
 
\versionadded{0.10}
1014
 
\end{methoddesc}
1015
 
 
1016
 
\begin{methoddesc}[Context]{add_client_ca}{certificate_authority}
1017
 
Extract a \class{OpenSSL.crypto.X509Name} from the \var{certificate_authority}
1018
 
\class{OpenSSL.crypto.X509} certificate and add it to the list of preferred
1019
 
certificate signers sent to the client when requesting a client certificate.
1020
 
 
1021
 
\versionadded{0.10}
1022
 
\end{methoddesc}
1023
 
 
1024
 
\begin{methoddesc}[Context]{load_verify_locations}{pemfile, capath}
1025
 
Specify where CA certificates for verification purposes are located. These
1026
 
are trusted certificates. Note that the certificates have to be in PEM
1027
 
format.  If capath is passed, it must be a directory prepared using the
1028
 
\code{c_rehash} tool included with OpenSSL.  Either, but not both, of
1029
 
\var{pemfile} or \var{capath} may be \code{None}.
1030
 
\end{methoddesc}
1031
 
 
1032
 
\begin{methoddesc}[Context]{set_default_verify_paths}{}
1033
 
Specify that the platform provided CA certificates are to be used for
1034
 
verification purposes.  This method may not work properly on OS X.
1035
 
\end{methoddesc}
1036
 
 
1037
 
\begin{methoddesc}[Context]{load_tmp_dh}{dhfile}
1038
 
Load parameters for Ephemeral Diffie-Hellman from \var{dhfile}.
1039
 
\end{methoddesc}
1040
 
 
1041
 
\begin{methoddesc}[Context]{set_app_data}{data}
1042
 
Associate \var{data} with this Context object. \var{data} can be retrieved
1043
 
later using the \method{get_app_data} method.
1044
 
\end{methoddesc}
1045
 
 
1046
 
\begin{methoddesc}[Context]{set_cipher_list}{ciphers}
1047
 
Set the list of ciphers to be used in this context. See the OpenSSL manual for
1048
 
more information (e.g. ciphers(1))
1049
 
\end{methoddesc}
1050
 
 
1051
 
\begin{methoddesc}[Context]{set_info_callback}{callback}
1052
 
Set the information callback to \var{callback}. This function will be called
1053
 
from time to time during SSL handshakes.
1054
 
\var{callback} should take three arguments: a Connection object and two
1055
 
integers. The first integer specifies where in the SSL handshake the function
1056
 
was called, and the other the return code from a (possibly failed) internal
1057
 
function call.
1058
 
\end{methoddesc}
1059
 
 
1060
 
\begin{methoddesc}[Context]{set_options}{options}
1061
 
Add SSL options. Options you have set before are not cleared!
1062
 
This method should be used with the \constant{OP_*} constants.
1063
 
\end{methoddesc}
1064
 
 
1065
 
\begin{methoddesc}[Context]{set_mode}{mode}
1066
 
Add SSL mode. Modes you have set before are not cleared!
1067
 
This method should be used with the \constant{MODE_*} constants.
1068
 
\end{methoddesc}
1069
 
 
1070
 
\begin{methoddesc}[Context]{set_passwd_cb}{callback\optional{, userdata}}
1071
 
Set the passphrase callback to \var{callback}. This function will be called
1072
 
when a private key with a passphrase is loaded. \var{callback} must accept
1073
 
three positional arguments.  First, an integer giving the maximum length of
1074
 
the passphrase it may return.  If the returned passphrase is longer than
1075
 
this, it will be truncated.  Second, a boolean value which will be true if
1076
 
the user should be prompted for the passphrase twice and the callback should
1077
 
verify that the two values supplied are equal. Third, the value given as the
1078
 
\var{userdata} parameter to \method{set_passwd_cb}.  If an error occurs,
1079
 
\var{callback} should return a false value (e.g. an empty string).
1080
 
\end{methoddesc}
1081
 
 
1082
 
\begin{methoddesc}[Context]{set_session_id}{name}
1083
 
Set the context \var{name} within which a session can be reused for this
1084
 
Context object. This is needed when doing session resumption, because there is
1085
 
no way for a stored session to know which Context object it is associated with.
1086
 
\var{name} may be any binary data.
1087
 
\end{methoddesc}
1088
 
 
1089
 
\begin{methoddesc}[Context]{set_timeout}{timeout}
1090
 
Set the timeout for newly created sessions for this Context object to
1091
 
\var{timeout}. \var{timeout} must be given in (whole) seconds. The default
1092
 
value is 300 seconds. See the OpenSSL manual for more information (e.g.
1093
 
SSL_CTX_set_timeout(3)).
1094
 
\end{methoddesc}
1095
 
 
1096
 
\begin{methoddesc}[Context]{set_verify}{mode, callback}
1097
 
Set the verification flags for this Context object to \var{mode} and specify
1098
 
that \var{callback} should be used for verification callbacks. \var{mode}
1099
 
should be one of \constant{VERIFY_NONE} and \constant{VERIFY_PEER}. If
1100
 
\constant{VERIFY_PEER} is used, \var{mode} can be OR:ed with
1101
 
\constant{VERIFY_FAIL_IF_NO_PEER_CERT} and \constant{VERIFY_CLIENT_ONCE} to
1102
 
further control the behaviour.
1103
 
\var{callback} should take five arguments: A Connection object, an X509 object,
1104
 
and three integer variables, which are in turn potential error number, error
1105
 
depth and return code. \var{callback} should return true if verification passes
1106
 
and false otherwise.
1107
 
\end{methoddesc}
1108
 
 
1109
 
\begin{methoddesc}[Context]{set_verify_depth}{depth}
1110
 
Set the maximum depth for the certificate chain verification that shall be
1111
 
allowed for this Context object.
1112
 
\end{methoddesc}
1113
 
 
1114
 
\begin{methoddesc}[Context]{use_certificate}{cert}
1115
 
Use the certificate \var{cert} which has to be a X509 object.
1116
 
\end{methoddesc}
1117
 
 
1118
 
\begin{methoddesc}[Context]{add_extra_chain_cert}{cert}
1119
 
Adds the certificate \var{cert}, which has to be a X509 object, to the
1120
 
certificate chain presented together with the certificate.
1121
 
\end{methoddesc}
1122
 
 
1123
 
\begin{methoddesc}[Context]{use_certificate_chain_file}{file}
1124
 
Load a certificate chain from \var{file} which must be PEM encoded.
1125
 
\end{methoddesc}
1126
 
 
1127
 
\begin{methoddesc}[Context]{use_privatekey}{pkey}
1128
 
Use the private key \var{pkey} which has to be a PKey object.
1129
 
\end{methoddesc}
1130
 
 
1131
 
\begin{methoddesc}[Context]{use_certificate_file}{file\optional{, format}}
1132
 
Load the first certificate found in \var{file}. The certificate must be in the
1133
 
format specified by \var{format}, which is either \constant{FILETYPE_PEM} or
1134
 
\constant{FILETYPE_ASN1}. The default is \constant{FILETYPE_PEM}.
1135
 
\end{methoddesc}
1136
 
 
1137
 
\begin{methoddesc}[Context]{use_privatekey_file}{file\optional{, format}}
1138
 
Load the first private key found in \var{file}. The private key must be in the
1139
 
format specified by \var{format}, which is either \constant{FILETYPE_PEM} or
1140
 
\constant{FILETYPE_ASN1}. The default is \constant{FILETYPE_PEM}.
1141
 
\end{methoddesc}
1142
 
 
1143
 
\begin{methoddesc}[Context]{set_tlsext_servername_callback}{callback}
1144
 
Specify a one-argument callable to use as the TLS extension server name
1145
 
callback.  When a connection using the server name extension is made using this
1146
 
context, the callback will be invoked with the \code{Connection} instance.
1147
 
\versionadded{0.13}
1148
 
\end{methoddesc}
1149
 
 
1150
 
\subsubsection{Connection objects \label{openssl-connection}}
1151
 
 
1152
 
Connection objects have the following methods:
1153
 
 
1154
 
\begin{methoddesc}[Connection]{accept}{}
1155
 
Call the \method{accept} method of the underlying socket and set up SSL on the
1156
 
returned socket, using the Context object supplied to this Connection object at
1157
 
creation. Returns a pair \code{(\var{conn}, \var{address})}. where \var{conn}
1158
 
is the new Connection object created, and \var{address} is as returned by the
1159
 
socket's \method{accept}.
1160
 
\end{methoddesc}
1161
 
 
1162
 
\begin{methoddesc}[Connection]{bind}{address}
1163
 
Call the \method{bind} method of the underlying socket.
1164
 
\end{methoddesc}
1165
 
 
1166
 
\begin{methoddesc}[Connection]{close}{}
1167
 
Call the \method{close} method of the underlying socket. Note: If you want
1168
 
correct SSL closure, you need to call the \method{shutdown} method first.
1169
 
\end{methoddesc}
1170
 
 
1171
 
\begin{methoddesc}[Connection]{connect}{address}
1172
 
Call the \method{connect} method of the underlying socket and set up SSL on the
1173
 
socket, using the Context object supplied to this Connection object at
1174
 
creation.
1175
 
\end{methoddesc}
1176
 
 
1177
 
\begin{methoddesc}[Connection]{connect_ex}{address}
1178
 
Call the \method{connect_ex} method of the underlying socket and set up SSL on
1179
 
the socket, using the Context object supplied to this Connection object at
1180
 
creation. Note that if the \method{connect_ex} method of the socket doesn't
1181
 
return 0, SSL won't be initialized.
1182
 
\end{methoddesc}
1183
 
 
1184
 
\begin{methoddesc}[Connection]{do_handshake}{}
1185
 
Perform an SSL handshake (usually called after \method{renegotiate} or one of
1186
 
\method{set_accept_state} or \method{set_accept_state}). This can raise the
1187
 
same exceptions as \method{send} and \method{recv}.
1188
 
\end{methoddesc}
1189
 
 
1190
 
\begin{methoddesc}[Connection]{fileno}{}
1191
 
Retrieve the file descriptor number for the underlying socket.
1192
 
\end{methoddesc}
1193
 
 
1194
 
\begin{methoddesc}[Connection]{listen}{backlog}
1195
 
Call the \method{listen} method of the underlying socket.
1196
 
\end{methoddesc}
1197
 
 
1198
 
\begin{methoddesc}[Connection]{get_app_data}{}
1199
 
Retrieve application data as set by \method{set_app_data}.
1200
 
\end{methoddesc}
1201
 
 
1202
 
\begin{methoddesc}[Connection]{get_cipher_list}{}
1203
 
Retrieve the list of ciphers used by the Connection object. WARNING: This API
1204
 
has changed. It used to take an optional parameter and just return a string,
1205
 
but not it returns the entire list in one go.
1206
 
\end{methoddesc}
1207
 
 
1208
 
\begin{methoddesc}[Connection]{get_client_ca_list}{}
1209
 
Retrieve the list of preferred client certificate issuers sent by the server
1210
 
as \class{OpenSSL.crypto.X509Name} objects.
1211
 
 
1212
 
If this is a client \class{Connection}, the list will be empty until the
1213
 
connection with the server is established.
1214
 
 
1215
 
If this is a server \class{Connection}, return the list of certificate
1216
 
authorities that will be sent or has been sent to the client, as controlled
1217
 
by this \class{Connection}'s \class{Context}.
1218
 
 
1219
 
\versionadded{0.10}
1220
 
\end{methoddesc}
1221
 
 
1222
 
\begin{methoddesc}[Connection]{get_context}{}
1223
 
Retrieve the Context object associated with this Connection.
1224
 
\end{methoddesc}
1225
 
 
1226
 
\begin{methoddesc}[Connection]{set_context}{context}
1227
 
Specify a replacement Context object for this Connection.
1228
 
\end{methoddesc}
1229
 
 
1230
 
\begin{methoddesc}[Connection]{get_peer_certificate}{}
1231
 
Retrieve the other side's certificate (if any)
1232
 
\end{methoddesc}
1233
 
 
1234
 
\begin{methoddesc}[Connection]{get_peer_cert_chain}{}
1235
 
Retrieve the tuple of the other side's certificate chain (if any)
1236
 
\end{methoddesc}
1237
 
 
1238
 
\begin{methoddesc}[Connection]{getpeername}{}
1239
 
Call the \method{getpeername} method of the underlying socket.
1240
 
\end{methoddesc}
1241
 
 
1242
 
\begin{methoddesc}[Connection]{getsockname}{}
1243
 
Call the \method{getsockname} method of the underlying socket.
1244
 
\end{methoddesc}
1245
 
 
1246
 
\begin{methoddesc}[Connection]{getsockopt}{level, optname\optional{, buflen}}
1247
 
Call the \method{getsockopt} method of the underlying socket.
1248
 
\end{methoddesc}
1249
 
 
1250
 
\begin{methoddesc}[Connection]{pending}{}
1251
 
Retrieve the number of bytes that can be safely read from the SSL buffer
1252
 
(\emph{not} the underlying transport buffer).
1253
 
\end{methoddesc}
1254
 
 
1255
 
\begin{methoddesc}[Connection]{recv}{bufsize}
1256
 
Receive data from the Connection. The return value is a string representing the
1257
 
data received. The maximum amount of data to be received at once, is specified
1258
 
by \var{bufsize}.
1259
 
\end{methoddesc}
1260
 
 
1261
 
\begin{methoddesc}[Connection]{bio_write}{bytes}
1262
 
If the Connection was created with a memory BIO, this method can be used to add
1263
 
bytes to the read end of that memory BIO.  The Connection can then read the
1264
 
bytes (for example, in response to a call to \method{recv}).
1265
 
\end{methoddesc}
1266
 
 
1267
 
\begin{methoddesc}[Connection]{renegotiate}{}
1268
 
Renegotiate the SSL session. Call this if you wish to change cipher suites or
1269
 
anything like that.
1270
 
\end{methoddesc}
1271
 
 
1272
 
\begin{methoddesc}[Connection]{send}{string}
1273
 
Send the \var{string} data to the Connection.
1274
 
\end{methoddesc}
1275
 
 
1276
 
\begin{methoddesc}[Connection]{bio_read}{bufsize}
1277
 
If the Connection was created with a memory BIO, this method can be used to
1278
 
read bytes from the write end of that memory BIO.  Many Connection methods will
1279
 
add bytes which must be read in this manner or the buffer will eventually fill
1280
 
up and the Connection will be able to take no further actions.
1281
 
\end{methoddesc}
1282
 
 
1283
 
\begin{methoddesc}[Connection]{sendall}{string}
1284
 
Send all of the \var{string} data to the Connection. This calls \method{send}
1285
 
repeatedly until all data is sent. If an error occurs, it's impossible to tell
1286
 
how much data has been sent.
1287
 
\end{methoddesc}
1288
 
 
1289
 
\begin{methoddesc}[Connection]{set_accept_state}{}
1290
 
Set the connection to work in server mode. The handshake will be handled
1291
 
automatically by read/write.
1292
 
\end{methoddesc}
1293
 
 
1294
 
\begin{methoddesc}[Connection]{set_app_data}{data}
1295
 
Associate \var{data} with this Connection object. \var{data} can be retrieved
1296
 
later using the \method{get_app_data} method.
1297
 
\end{methoddesc}
1298
 
 
1299
 
\begin{methoddesc}[Connection]{set_connect_state}{}
1300
 
Set the connection to work in client mode. The handshake will be handled
1301
 
automatically by read/write.
1302
 
\end{methoddesc}
1303
 
 
1304
 
\begin{methoddesc}[Connection]{setblocking}{flag}
1305
 
Call the \method{setblocking} method of the underlying socket.
1306
 
\end{methoddesc}
1307
 
 
1308
 
\begin{methoddesc}[Connection]{setsockopt}{level, optname, value}
1309
 
Call the \method{setsockopt} method of the underlying socket.
1310
 
\end{methoddesc}
1311
 
 
1312
 
\begin{methoddesc}[Connection]{shutdown}{}
1313
 
Send the shutdown message to the Connection. Returns true if the shutdown
1314
 
message exchange is completed and false otherwise (in which case you call
1315
 
\method{recv()} or \method{send()} when the connection becomes
1316
 
readable/writeable.
1317
 
\end{methoddesc}
1318
 
 
1319
 
\begin{methoddesc}[Connection]{get_shutdown}{}
1320
 
Get the shutdown state of the Connection.  Returns a bitvector of either or
1321
 
both of \var{SENT_SHUTDOWN} and \var{RECEIVED_SHUTDOWN}.
1322
 
\end{methoddesc}
1323
 
 
1324
 
\begin{methoddesc}[Connection]{set_shutdown}{state}
1325
 
Set the shutdown state of the Connection.  \var{state} is a bitvector of
1326
 
either or both of \var{SENT_SHUTDOWN} and \var{RECEIVED_SHUTDOWN}.
1327
 
\end{methoddesc}
1328
 
 
1329
 
\begin{methoddesc}[Connection]{sock_shutdown}{how}
1330
 
Call the \method{shutdown} method of the underlying socket.
1331
 
\end{methoddesc}
1332
 
 
1333
 
\begin{methoddesc}[Connection]{bio_shutdown}{}
1334
 
If the Connection was created with a memory BIO, this method can be used to
1335
 
indicate that ``end of file'' has been reached on the read end of that memory
1336
 
BIO.
1337
 
\end{methoddesc}
1338
 
 
1339
 
\begin{methoddesc}[Connection]{state_string}{}
1340
 
Retrieve a verbose string detailing the state of the Connection.
1341
 
\end{methoddesc}
1342
 
 
1343
 
\begin{methoddesc}[Connection]{client_random}{}
1344
 
Retrieve the random value used with the client hello message.
1345
 
\end{methoddesc}
1346
 
 
1347
 
\begin{methoddesc}[Connection]{server_random}{}
1348
 
Retrieve the random value used with the server hello message.
1349
 
\end{methoddesc}
1350
 
 
1351
 
\begin{methoddesc}[Connection]{master_key}{}
1352
 
Retrieve the value of the master key for this session.
1353
 
\end{methoddesc}
1354
 
 
1355
 
\begin{methoddesc}[Connection]{want_read}{}
1356
 
Checks if more data has to be read from the transport layer to complete an
1357
 
operation.
1358
 
\end{methoddesc}
1359
 
 
1360
 
\begin{methoddesc}[Connection]{want_write}{}
1361
 
Checks if there is data to write to the transport layer to complete an
1362
 
operation.
1363
 
\end{methoddesc}
1364
 
 
1365
 
\begin{methoddesc}[Connection]{set_tlsext_host_name}{name}
1366
 
Specify the byte string to send as the server name in the client hello message.
1367
 
\versionadded{0.13}
1368
 
\end{methoddesc}
1369
 
 
1370
 
\begin{methoddesc}[Connection]{get_servername}{}
1371
 
Get the value of the server name received in the client hello message.
1372
 
\versionadded{0.13}
1373
 
\end{methoddesc}
1374
 
 
1375
 
 
1376
 
 
1377
 
\section{Internals \label{internals}}
1378
 
 
1379
 
We ran into three main problems developing this: Exceptions, callbacks and
1380
 
accessing socket methods. This is what this chapter is about.
1381
 
 
1382
 
\subsection{Exceptions \label{exceptions}}
1383
 
 
1384
 
We realized early that most of the exceptions would be raised by the I/O
1385
 
functions of OpenSSL, so it felt natural to mimic OpenSSL's error code system,
1386
 
translating them into Python exceptions. This naturally gives us the exceptions
1387
 
\exception{SSL.ZeroReturnError}, \exception{SSL.WantReadError},
1388
 
\exception{SSL.WantWriteError}, \exception{SSL.WantX509LookupError} and
1389
 
\exception{SSL.SysCallError}.
1390
 
 
1391
 
For more information about this, see section \ref{openssl-ssl}.
1392
 
 
1393
 
 
1394
 
\subsection{Callbacks \label{callbacks}}
1395
 
 
1396
 
There are a number of problems with callbacks. First of all, OpenSSL is written
1397
 
as a C library, it's not meant to have Python callbacks, so a way around that
1398
 
is needed. Another problem is thread support. A lot of the OpenSSL I/O
1399
 
functions can block if the socket is in blocking mode, and then you want other
1400
 
Python threads to be able to do other things. The real trouble is if you've
1401
 
released the global CPython interpreter lock to do a potentially blocking
1402
 
operation, and the operation calls a callback. Then we must take the GIL back,
1403
 
since calling Python APIs without holding it is not allowed.
1404
 
 
1405
 
There are two solutions to the first problem, both of which are necessary. The
1406
 
first solution to use is if the C callback allows ''userdata'' to be passed to
1407
 
it (an arbitrary pointer normally). This is great! We can set our Python
1408
 
function object as the real userdata and emulate userdata for the Python
1409
 
function in another way. The other solution can be used if an object with an
1410
 
''app_data'' system always is passed to the callback. For example, the SSL
1411
 
object in OpenSSL has app_data functions and in e.g. the verification
1412
 
callbacks, you can retrieve the related SSL object. What we do is to set our
1413
 
wrapper \class{Connection} object as app_data for the SSL object, and we can
1414
 
easily find the Python callback.
1415
 
 
1416
 
The other problem is solved using thread local variables.  Whenever the GIL is
1417
 
released before calling into an OpenSSL API, the PyThreadState pointer returned
1418
 
by \cfunction{PyEval_SaveState} is stored in a global thread local variable
1419
 
(using Python's own TLS API, \cfunction{PyThread_set_key_value}).  When it is
1420
 
necessary to re-acquire the GIL, either after the OpenSSL API returns or in a C
1421
 
callback invoked by that OpenSSL API, the value of the thread local variable is
1422
 
retrieved (\cfunction{PyThread_get_key_value}) and used to re-acquire the GIL.
1423
 
This allows Python threads to execute while OpenSSL APIs are running and allows
1424
 
use of any particular pyOpenSSL object from any Python thread, since there is
1425
 
no per-thread state associated with any of these objects and since OpenSSL is
1426
 
threadsafe (as long as properly initialized, as pyOpenSSL initializes it).
1427
 
 
1428
 
 
1429
 
\subsection{Acessing Socket Methods \label{socket-methods}}
1430
 
 
1431
 
We quickly saw the benefit of wrapping socket methods in the
1432
 
\class{SSL.Connection} class, for an easy transition into using SSL. The
1433
 
problem here is that the \module{socket} module lacks a C API, and all the
1434
 
methods are declared static. One approach would be to have \module{OpenSSL} as
1435
 
a submodule to the \module{socket} module, placing all the code in
1436
 
\file{socketmodule.c}, but this is obviously not a good solution, since you
1437
 
might not want to import tonnes of extra stuff you're not going to use when
1438
 
importing the \module{socket} module. The other approach is to somehow get a
1439
 
pointer to the method to be called, either the C function, or a callable Python
1440
 
object. This is not really a good solution either, since there's a lot of
1441
 
lookups involved.
1442
 
 
1443
 
The way it works is that you have to supply a ``\class{socket}-like'' transport
1444
 
object to the \class{SSL.Connection}. The only requirement of this object is
1445
 
that it has a \method{fileno()} method that returns a file descriptor that's
1446
 
valid at the C level (i.e. you can use the system calls read and write). If you
1447
 
want to use the \method{connect()} or \method{accept()} methods of the
1448
 
\class{SSL.Connection} object, the transport object has to supply such
1449
 
methods too. Apart from them, any method lookups in the \class{SSL.Connection}
1450
 
object that fail are passed on to the underlying transport object.
1451
 
 
1452
 
Future changes might be to allow Python-level transport objects, that instead
1453
 
of having \method{fileno()} methods, have \method{read()} and \method{write()}
1454
 
methods, so more advanced features of Python can be used. This would probably
1455
 
entail some sort of OpenSSL ``BIOs'', but converting Python strings back and
1456
 
forth is expensive, so this shouldn't be used unless necessary. Other nice
1457
 
things would be to be able to pass in different transport objects for reading
1458
 
and writing, but then the \method{fileno()} method of \class{SSL.Connection}
1459
 
becomes virtually useless. Also, should the method resolution be used on the
1460
 
read-transport or the write-transport?
1461
 
 
1462
 
 
1463
 
\end{document}