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