Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 1 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 2 | :mod:`ssl` --- SSL wrapper for socket objects |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 3 | ==================================================================== |
| 4 | |
| 5 | .. module:: ssl |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 6 | :synopsis: SSL wrapper for socket objects |
| 7 | |
| 8 | .. moduleauthor:: Bill Janssen <bill.janssen@gmail.com> |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 9 | |
| 10 | .. versionadded:: 2.6 |
| 11 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 12 | .. sectionauthor:: Bill Janssen <bill.janssen@gmail.com> |
| 13 | |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 14 | |
| 15 | This module provides access to Transport Layer Security (often known |
| 16 | as "Secure Sockets Layer") encryption and peer authentication |
| 17 | facilities for network sockets, both client-side and server-side. |
| 18 | This module uses the OpenSSL library. It is available on all modern |
| 19 | Unix systems, Windows, Mac OS X, and probably additional |
| 20 | platforms, as long as OpenSSL is installed on that platform. |
| 21 | |
| 22 | .. note:: |
| 23 | |
| 24 | Some behavior may be platform dependent, since calls are made to the operating |
| 25 | system socket APIs. |
| 26 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 27 | This section documents the objects and functions in the ``ssl`` module; |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 28 | for more general information about TLS, SSL, and certificates, the |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 29 | reader is referred to the documents in the :ref:`ssl-references` section. |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 30 | |
| 31 | This module defines a class, :class:`ssl.sslsocket`, which is |
| 32 | derived from the :class:`socket.socket` type, and supports additional |
| 33 | :meth:`read` and :meth:`write` methods, along with a method, :meth:`getpeercert`, |
| 34 | to retrieve the certificate of the other side of the connection. |
| 35 | |
| 36 | This module defines the following functions, exceptions, and constants: |
| 37 | |
| 38 | .. function:: cert_time_to_seconds(timestring) |
| 39 | |
| 40 | Returns a floating-point value containing a normal seconds-after-the-epoch time |
| 41 | value, given the time-string representing the "notBefore" or "notAfter" date |
| 42 | from a certificate. |
| 43 | |
| 44 | Here's an example:: |
| 45 | |
| 46 | >>> import ssl |
| 47 | >>> ssl.cert_time_to_seconds("May 9 00:00:00 2007 GMT") |
| 48 | 1178694000.0 |
| 49 | >>> import time |
| 50 | >>> time.ctime(ssl.cert_time_to_seconds("May 9 00:00:00 2007 GMT")) |
| 51 | 'Wed May 9 00:00:00 2007' |
| 52 | >>> |
| 53 | |
| 54 | .. exception:: sslerror |
| 55 | |
| 56 | Raised to signal an error from the underlying SSL implementation. This |
| 57 | signifies some problem in the higher-level |
| 58 | encryption and authentication layer that's superimposed on the underlying |
| 59 | network connection. |
| 60 | |
| 61 | .. data:: CERT_NONE |
| 62 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 63 | Value to pass to the ``cert_reqs`` parameter to :func:`sslobject` |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 64 | when no certificates will be required or validated from the other |
| 65 | side of the socket connection. |
| 66 | |
| 67 | .. data:: CERT_OPTIONAL |
| 68 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 69 | Value to pass to the ``cert_reqs`` parameter to :func:`sslobject` |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 70 | when no certificates will be required from the other side of the |
| 71 | socket connection, but if they are provided, will be validated. |
| 72 | Note that use of this setting requires a valid certificate |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 73 | validation file also be passed as a value of the ``ca_certs`` |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 74 | parameter. |
| 75 | |
| 76 | .. data:: CERT_REQUIRED |
| 77 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 78 | Value to pass to the ``cert_reqs`` parameter to :func:`sslobject` |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 79 | when certificates will be required from the other side of the |
| 80 | socket connection. Note that use of this setting requires a valid certificate |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 81 | validation file also be passed as a value of the ``ca_certs`` |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 82 | parameter. |
| 83 | |
| 84 | .. data:: PROTOCOL_SSLv2 |
| 85 | |
| 86 | Selects SSL version 2 as the channel encryption protocol. |
| 87 | |
| 88 | .. data:: PROTOCOL_SSLv23 |
| 89 | |
| 90 | Selects SSL version 2 or 3 as the channel encryption protocol. This is a setting to use for maximum compatibility |
| 91 | with the other end of an SSL connection, but it may cause the specific ciphers chosen for the encryption to be |
| 92 | of fairly low quality. |
| 93 | |
| 94 | .. data:: PROTOCOL_SSLv3 |
| 95 | |
| 96 | Selects SSL version 3 as the channel encryption protocol. |
| 97 | |
| 98 | .. data:: PROTOCOL_TLSv1 |
| 99 | |
| 100 | Selects SSL version 2 as the channel encryption protocol. This is |
| 101 | the most modern version, and probably the best choice for maximum |
| 102 | protection, if both sides can speak it. |
| 103 | |
| 104 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 105 | .. _ssl-certificates: |
| 106 | |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 107 | Certificates |
| 108 | ------------ |
| 109 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 110 | Certificates in general are part of a public-key / private-key system. In this system, each *principal*, |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 111 | (which may be a machine, or a person, or an organization) is assigned a unique two-part encryption key. |
| 112 | One part of the key is public, and is called the *public key*; the other part is kept secret, and is called |
| 113 | the *private key*. The two parts are related, in that if you encrypt a message with one of the parts, you can |
| 114 | decrypt it with the other part, and **only** with the other part. |
| 115 | |
| 116 | A certificate contains information about two principals. It contains |
| 117 | the name of a *subject*, and the subject's public key. It also |
| 118 | contains a statement by a second principal, the *issuer*, that the |
| 119 | subject is who he claims to be, and that this is indeed the subject's |
| 120 | public key. The issuer's statement is signed with the issuer's |
| 121 | private key, which only the issuer knows. However, anyone can verify |
| 122 | the issuer's statement by finding the issuer's public key, decrypting |
| 123 | the statement with it, and comparing it to the other information in |
| 124 | the certificate. The certificate also contains information about the |
| 125 | time period over which it is valid. This is expressed as two fields, |
| 126 | called "notBefore" and "notAfter". |
| 127 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 128 | In the Python use of certificates, a client or server |
| 129 | can use a certificate to prove who they are. The other |
| 130 | side of a network connection can also be required to produce a certificate, |
| 131 | and that certificate can be validated to the satisfaction |
| 132 | of the client or server that requires such validation. |
| 133 | The connection can be set to fail automatically if such |
| 134 | validation is not achieved. |
| 135 | |
| 136 | Python uses files to contain certificates. They should be formatted |
| 137 | as "PEM" (see :rfc:`1422`), which is a base-64 encoded form wrapped |
| 138 | with a header line and a footer line:: |
| 139 | |
| 140 | -----BEGIN CERTIFICATE----- |
| 141 | ... (certificate in base64 PEM encoding) ... |
| 142 | -----END CERTIFICATE----- |
| 143 | |
| 144 | The Python files which contain certificates can contain a sequence |
| 145 | of certificates, sometimes called a *certificate chain*. This chain |
| 146 | should start with the specific certificate for the principal who "is" |
| 147 | the client or server, and then the certificate for the issuer of that |
| 148 | certificate, and then the certificate for the issuer of *that* certificate, |
| 149 | and so on up the chain till you get to a certificate which is *self-signed*, |
| 150 | that is, a certificate which has the same subject and issuer, |
| 151 | sometimes called a *root certificate*. The certificates should just |
| 152 | be concatenated together in the certificate file. For example, suppose |
| 153 | we had a three certificate chain, from our server certificate to the |
| 154 | certificate of the certification authority that signed our server certificate, |
| 155 | to the root certificate of the agency which issued the certification authority's |
| 156 | certificate:: |
| 157 | |
| 158 | -----BEGIN CERTIFICATE----- |
| 159 | ... (certificate for your server)... |
| 160 | -----END CERTIFICATE----- |
| 161 | -----BEGIN CERTIFICATE----- |
| 162 | ... (the certificate for the CA)... |
| 163 | -----END CERTIFICATE----- |
| 164 | -----BEGIN CERTIFICATE----- |
| 165 | ... (the root certificate for the CA's issuer)... |
| 166 | -----END CERTIFICATE----- |
| 167 | |
| 168 | If you are going to require validation of the other side of the connection's |
| 169 | certificate, you need to provide a "CA certs" file, filled with the certificate |
| 170 | chains for each issuer you are willing to trust. Again, this file just |
| 171 | contains these chains concatenated together. For validation, Python will |
| 172 | use the first chain it finds in the file which matches. |
| 173 | Some "standard" root certificates are available at |
| 174 | http://www.thawte.com/roots/ (for Thawte roots) and |
| 175 | http://www.verisign.com/support/roots.html (for Verisign roots). |
Bill Janssen | ffe576d | 2007-09-05 00:46:27 +0000 | [diff] [blame^] | 176 | See also :rfc:`4158` for more discussion of the way in which |
| 177 | certification chains can be built. |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 178 | |
| 179 | |
| 180 | sslsocket Objects |
| 181 | ----------------- |
| 182 | |
| 183 | .. class:: sslsocket(sock [, keyfile=None, certfile=None, server_side=False, cert_reqs=CERT_NONE, ssl_version=PROTOCOL_SSLv23, ca_certs=None]) |
| 184 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 185 | Takes an instance ``sock`` of :class:`socket.socket`, and returns an instance of a subtype |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 186 | of :class:`socket.socket` which wraps the underlying socket in an SSL context. |
| 187 | For client-side sockets, the context construction is lazy; if the underlying socket isn't |
| 188 | connected yet, the context construction will be performed after :meth:`connect` is called |
| 189 | on the socket. |
| 190 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 191 | The ``keyfile`` and ``certfile`` parameters specify optional files which contain a certificate |
| 192 | to be used to identify the local side of the connection. See the above discussion of :ref:`ssl-certificates` |
| 193 | for more information on how the certificate is stored in the ``certfile``. |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 194 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 195 | Often the private key is stored |
| 196 | in the same file as the certificate; in this case, only the ``certfile`` parameter need be |
| 197 | passed. If the private key is stored in a separate file, both parameters must be used. |
| 198 | If the private key is stored in the ``certfile``, it should come before the first certificate |
| 199 | in the certificate chain:: |
| 200 | |
| 201 | -----BEGIN RSA PRIVATE KEY----- |
| 202 | ... (private key in base64 encoding) ... |
| 203 | -----END RSA PRIVATE KEY----- |
| 204 | -----BEGIN CERTIFICATE----- |
| 205 | ... (certificate in base64 PEM encoding) ... |
| 206 | -----END CERTIFICATE----- |
| 207 | |
| 208 | The parameter ``server_side`` is a boolean which identifies whether server-side or client-side |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 209 | behavior is desired from this socket. |
| 210 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 211 | The parameter ``cert_reqs`` specifies whether a certificate is |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 212 | required from the other side of the connection, and whether it will |
| 213 | be validated if provided. It must be one of the three values |
| 214 | :const:`CERT_NONE` (certificates ignored), :const:`CERT_OPTIONAL` (not required, |
| 215 | but validated if provided), or :const:`CERT_REQUIRED` (required and |
| 216 | validated). If the value of this parameter is not :const:`CERT_NONE`, then |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 217 | the ``ca_certs`` parameter must point to a file of CA certificates. |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 218 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 219 | The parameter ``ssl_version`` specifies which version of the SSL protocol to use. Typically, |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 220 | the server specifies this, and a client connecting to it must use the same protocol. An |
| 221 | SSL server using :const:`PROTOCOL_SSLv23` can understand a client connecting via SSL2, SSL3, or TLS1, |
| 222 | but a client using :const:`PROTOCOL_SSLv23` can only connect to an SSL2 server. |
| 223 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 224 | The ``ca_certs`` file contains a set of concatenated "certification authority" certificates, |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 225 | which are used to validate certificates passed from the other end of the connection. |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 226 | See the above discussion of :ref:`ssl-certificates` for more information about how to arrange |
| 227 | the certificates in this file. |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 228 | |
| 229 | .. method:: sslsocket.read([nbytes]) |
| 230 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 231 | Reads up to ``nbytes`` bytes from the SSL-encrypted channel and returns them. |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 232 | |
| 233 | .. method:: sslsocket.write(data) |
| 234 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 235 | Writes the ``data`` to the other side of the connection, using the SSL channel to encrypt. Returns the number |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 236 | of bytes written. |
| 237 | |
| 238 | .. method:: sslsocket.getpeercert() |
| 239 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 240 | If there is no certificate for the peer on the other end of the connection, returns ``None``. |
| 241 | If a certificate was received from the peer, but not validated, returns an empty ``dict`` instance. |
| 242 | If a certificate was received and validated, returns a ``dict`` instance with the fields |
| 243 | ``subject`` (the principal for which the certificate was issued), ``issuer`` (the signer of |
| 244 | the certificate), ``notBefore`` (the time before which the certificate should not be trusted), |
| 245 | and ``notAfter`` (the time after which the certificate should not be trusted) filled in. |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 246 | |
Bill Janssen | ffe576d | 2007-09-05 00:46:27 +0000 | [diff] [blame^] | 247 | The "subject" and "issuer" fields are tuples containing the name-value fields |
| 248 | given in the certificate's data structure for each principal:: |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 249 | |
Bill Janssen | ffe576d | 2007-09-05 00:46:27 +0000 | [diff] [blame^] | 250 | {'issuer': (('countryName', u'US'), |
| 251 | ('stateOrProvinceName', u'Delaware'), |
| 252 | ('localityName', u'Wilmington'), |
| 253 | ('organizationName', u'Python Software Foundation'), |
| 254 | ('organizationalUnitName', u'SSL'), |
| 255 | ('commonName', u'somemachine.python.org')), |
| 256 | 'notAfter': 'Feb 16 16:54:50 2013 GMT', |
| 257 | 'notBefore': 'Aug 27 16:54:50 2007 GMT', |
| 258 | 'subject': (('countryName', u'US'), |
| 259 | ('stateOrProvinceName', u'Delaware'), |
| 260 | ('localityName', u'Wilmington'), |
| 261 | ('organizationName', u'Python Software Foundation'), |
| 262 | ('organizationalUnitName', u'SSL'), |
| 263 | ('commonName', u'somemachine.python.org')), |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 264 | 'version': 2} |
| 265 | |
| 266 | This certificate is said to be *self-signed*, because the subject |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 267 | and issuer are the same entity. The *version* field refers to the X509 version |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 268 | that's used for the certificate. |
| 269 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 270 | .. method:: sslsocket.ssl_shutdown() |
| 271 | |
| 272 | Closes the SSL context (if any) over the socket, but leaves the socket connection |
| 273 | open for further use, if both sides are willing. This is different from :meth:`socket.socket.shutdown`, |
| 274 | which will close the connection, but leave the local socket available for further use. |
| 275 | |
| 276 | |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 277 | Examples |
| 278 | -------- |
| 279 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 280 | Testing for SSL support |
| 281 | ^^^^^^^^^^^^^^^^^^^^^^^ |
| 282 | |
| 283 | To test for the presence of SSL support in a Python installation, user code should use the following idiom:: |
| 284 | |
| 285 | try: |
| 286 | import ssl |
| 287 | except ImportError: |
| 288 | pass |
| 289 | else: |
| 290 | [ do something that requires SSL support ] |
| 291 | |
| 292 | Client-side operation |
| 293 | ^^^^^^^^^^^^^^^^^^^^^ |
| 294 | |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 295 | This example connects to an SSL server, prints the server's address and certificate, |
| 296 | sends some bytes, and reads part of the response:: |
| 297 | |
| 298 | import socket, ssl, pprint |
| 299 | |
| 300 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 301 | ssl_sock = ssl.sslsocket(s, ca_certs="/etc/ca_certs_file", cert_reqs=ssl.CERT_REQUIRED) |
| 302 | |
| 303 | ssl_sock.connect(('www.verisign.com', 443)) |
| 304 | |
| 305 | print repr(ssl_sock.getpeername()) |
| 306 | print pprint.pformat(ssl_sock.getpeercert()) |
| 307 | |
| 308 | # Set a simple HTTP request -- use httplib in actual code. |
| 309 | ssl_sock.write("""GET / HTTP/1.0\r |
| 310 | Host: www.verisign.com\r\n\r\n""") |
| 311 | |
| 312 | # Read a chunk of data. Will not necessarily |
| 313 | # read all the data returned by the server. |
| 314 | data = ssl_sock.read() |
| 315 | |
| 316 | # note that closing the sslsocket will also close the underlying socket |
| 317 | ssl_sock.close() |
| 318 | |
Bill Janssen | ffe576d | 2007-09-05 00:46:27 +0000 | [diff] [blame^] | 319 | As of September 4, 2007, the certificate printed by this program |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 320 | looked like this:: |
| 321 | |
Bill Janssen | ffe576d | 2007-09-05 00:46:27 +0000 | [diff] [blame^] | 322 | {'issuer': (('countryName', u'US'), |
| 323 | ('organizationName', u'VeriSign, Inc.'), |
| 324 | ('organizationalUnitName', u'VeriSign Trust Network'), |
| 325 | ('organizationalUnitName', |
| 326 | u'Terms of use at https://www.verisign.com/rpa (c)06'), |
| 327 | ('commonName', |
| 328 | u'VeriSign Class 3 Extended Validation SSL SGC CA')), |
| 329 | 'notAfter': 'May 8 23:59:59 2009 GMT', |
| 330 | 'notBefore': 'May 9 00:00:00 2007 GMT', |
| 331 | 'subject': (('serialNumber', u'2497886'), |
| 332 | ('1.3.6.1.4.1.311.60.2.1.3', u'US'), |
| 333 | ('1.3.6.1.4.1.311.60.2.1.2', u'Delaware'), |
| 334 | ('countryName', u'US'), |
| 335 | ('postalCode', u'94043'), |
| 336 | ('stateOrProvinceName', u'California'), |
| 337 | ('localityName', u'Mountain View'), |
| 338 | ('streetAddress', u'487 East Middlefield Road'), |
| 339 | ('organizationName', u'VeriSign, Inc.'), |
| 340 | ('organizationalUnitName', u'Production Security Services'), |
| 341 | ('organizationalUnitName', |
| 342 | u'Terms of use at www.verisign.com/rpa (c)06'), |
| 343 | ('commonName', u'www.verisign.com')), |
| 344 | 'version': 2} |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 345 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 346 | Server-side operation |
| 347 | ^^^^^^^^^^^^^^^^^^^^^ |
| 348 | |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 349 | For server operation, typically you'd need to have a server certificate, and private key, each in a file. |
| 350 | You'd open a socket, bind it to a port, call :meth:`listen` on it, then start waiting for clients |
| 351 | to connect:: |
| 352 | |
| 353 | import socket, ssl |
| 354 | |
| 355 | bindsocket = socket.socket() |
| 356 | bindsocket.bind(('myaddr.mydomain.com', 10023)) |
| 357 | bindsocket.listen(5) |
| 358 | |
| 359 | When one did, you'd call :meth:`accept` on the socket to get the new socket from the other |
| 360 | end, and use :func:`sslsocket` to create a server-side SSL context for it:: |
| 361 | |
| 362 | while True: |
| 363 | newsocket, fromaddr = bindsocket.accept() |
| 364 | connstream = ssl.sslsocket(newsocket, server_side=True, certfile="mycertfile", |
| 365 | keyfile="mykeyfile", ssl_protocol=ssl.PROTOCOL_TLSv1) |
| 366 | deal_with_client(connstream) |
| 367 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 368 | Then you'd read data from the ``connstream`` and do something with it till you are finished with the client (or the client is finished with you):: |
Guido van Rossum | 8ee23bb | 2007-08-27 19:11:11 +0000 | [diff] [blame] | 369 | |
| 370 | def deal_with_client(connstream): |
| 371 | |
| 372 | data = connstream.read() |
| 373 | # null data means the client is finished with us |
| 374 | while data: |
| 375 | if not do_something(connstream, data): |
| 376 | # we'll assume do_something returns False when we're finished with client |
| 377 | break |
| 378 | data = connstream.read() |
| 379 | # finished with client |
| 380 | connstream.close() |
| 381 | |
| 382 | And go back to listening for new client connections. |
| 383 | |
| 384 | |
Bill Janssen | 426ea0a | 2007-08-29 22:35:05 +0000 | [diff] [blame] | 385 | .. _ssl-references: |
| 386 | |
| 387 | References |
| 388 | ---------- |
| 389 | |
| 390 | Class :class:`socket.socket` |
| 391 | Documentation of underlying :mod:`socket` class |
| 392 | |
| 393 | `Introducing SSL and Certificates using OpenSSL <http://old.pseudonym.org/ssl/wwwj-index.html>`_, by Frederick J. Hirsch |
| 394 | |
| 395 | `Privacy Enhancement for Internet Electronic Mail: Part II: Certificate-Based Key Management`, :rfc:`1422`, by Steve Kent |
Bill Janssen | ffe576d | 2007-09-05 00:46:27 +0000 | [diff] [blame^] | 396 | |
| 397 | `Internet X.509 Public Key Infrastructure Certificate and CRL Profile`, :rfc:`3280`, Housley et. al. |