blob: 968f9a0075e456ab6e10f0eb61dccd453a7ce903 [file] [log] [blame]
Thomas Woutersed03b412007-08-28 21:37:11 +00001# Wrapper module for _ssl, providing some additional facilities
2# implemented in Python. Written by Bill Janssen.
3
Guido van Rossum5b8b1552007-11-16 00:06:11 +00004"""This module provides some more Pythonic support for SSL.
Thomas Woutersed03b412007-08-28 21:37:11 +00005
6Object types:
7
Thomas Wouters1b7f8912007-09-19 03:06:30 +00008 SSLSocket -- subtype of socket.socket which does SSL over the socket
Thomas Woutersed03b412007-08-28 21:37:11 +00009
10Exceptions:
11
Thomas Wouters1b7f8912007-09-19 03:06:30 +000012 SSLError -- exception raised for I/O errors
Thomas Woutersed03b412007-08-28 21:37:11 +000013
14Functions:
15
16 cert_time_to_seconds -- convert time string used for certificate
17 notBefore and notAfter functions to integer
18 seconds past the Epoch (the time values
19 returned from time.time())
20
21 fetch_server_certificate (HOST, PORT) -- fetch the certificate provided
22 by the server running on HOST at port PORT. No
23 validation of the certificate is performed.
24
25Integer constants:
26
27SSL_ERROR_ZERO_RETURN
28SSL_ERROR_WANT_READ
29SSL_ERROR_WANT_WRITE
30SSL_ERROR_WANT_X509_LOOKUP
31SSL_ERROR_SYSCALL
32SSL_ERROR_SSL
33SSL_ERROR_WANT_CONNECT
34
35SSL_ERROR_EOF
36SSL_ERROR_INVALID_ERROR_CODE
37
38The following group define certificate requirements that one side is
39allowing/requiring from the other side:
40
41CERT_NONE - no certificates from the other side are required (or will
42 be looked at if provided)
43CERT_OPTIONAL - certificates are not required, but if provided will be
44 validated, and if validation fails, the connection will
45 also fail
46CERT_REQUIRED - certificates are required, and will be validated, and
47 if validation fails, the connection will also fail
48
49The following constants identify various SSL protocol variants:
50
51PROTOCOL_SSLv2
52PROTOCOL_SSLv3
53PROTOCOL_SSLv23
54PROTOCOL_TLSv1
55"""
56
Christian Heimes05e8be12008-02-23 18:30:17 +000057import textwrap
Thomas Woutersed03b412007-08-28 21:37:11 +000058
59import _ssl # if we can't import it, let the error propagate
Thomas Wouters1b7f8912007-09-19 03:06:30 +000060
61from _ssl import SSLError
Thomas Woutersed03b412007-08-28 21:37:11 +000062from _ssl import CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED
Guido van Rossum5b8b1552007-11-16 00:06:11 +000063from _ssl import (PROTOCOL_SSLv2, PROTOCOL_SSLv3, PROTOCOL_SSLv23,
64 PROTOCOL_TLSv1)
Thomas Wouters1b7f8912007-09-19 03:06:30 +000065from _ssl import RAND_status, RAND_egd, RAND_add
Guido van Rossum5b8b1552007-11-16 00:06:11 +000066from _ssl import (
67 SSL_ERROR_ZERO_RETURN,
68 SSL_ERROR_WANT_READ,
69 SSL_ERROR_WANT_WRITE,
70 SSL_ERROR_WANT_X509_LOOKUP,
71 SSL_ERROR_SYSCALL,
72 SSL_ERROR_SSL,
73 SSL_ERROR_WANT_CONNECT,
74 SSL_ERROR_EOF,
75 SSL_ERROR_INVALID_ERROR_CODE,
76 )
Thomas Woutersed03b412007-08-28 21:37:11 +000077
Bill Janssen6e027db2007-11-15 22:23:56 +000078from socket import socket, AF_INET, SOCK_STREAM, error
Thomas Wouters47b49bf2007-08-30 22:15:33 +000079from socket import getnameinfo as _getnameinfo
Bill Janssen6e027db2007-11-15 22:23:56 +000080from socket import error as socket_error
Guido van Rossum39eb8fa2007-11-16 01:24:05 +000081from socket import dup as _dup
Thomas Wouters1b7f8912007-09-19 03:06:30 +000082import base64 # for DER-to-PEM translation
Bill Janssen54cc54c2007-12-14 22:08:56 +000083import traceback
Thomas Wouters47b49bf2007-08-30 22:15:33 +000084
Guido van Rossum5b8b1552007-11-16 00:06:11 +000085class SSLSocket(socket):
Thomas Woutersed03b412007-08-28 21:37:11 +000086
Thomas Wouters47b49bf2007-08-30 22:15:33 +000087 """This class implements a subtype of socket.socket that wraps
88 the underlying OS socket in an SSL context when necessary, and
89 provides read and write methods over that channel."""
90
Bill Janssen6e027db2007-11-15 22:23:56 +000091 def __init__(self, sock=None, keyfile=None, certfile=None,
Thomas Woutersed03b412007-08-28 21:37:11 +000092 server_side=False, cert_reqs=CERT_NONE,
Bill Janssen6e027db2007-11-15 22:23:56 +000093 ssl_version=PROTOCOL_SSLv23, ca_certs=None,
94 do_handshake_on_connect=True,
95 family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None,
96 suppress_ragged_eofs=True):
97
Bill Janssen6e027db2007-11-15 22:23:56 +000098 if sock is not None:
Bill Janssen54cc54c2007-12-14 22:08:56 +000099 socket.__init__(self,
100 family=sock.family,
101 type=sock.type,
102 proto=sock.proto,
103 fileno=_dup(sock.fileno()))
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000104 sock.close()
Bill Janssen6e027db2007-11-15 22:23:56 +0000105 elif fileno is not None:
106 socket.__init__(self, fileno=fileno)
107 else:
108 socket.__init__(self, family=family, type=type, proto=proto)
109
110 self._closed = False
111
Thomas Woutersed03b412007-08-28 21:37:11 +0000112 if certfile and not keyfile:
113 keyfile = certfile
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000114 # see if it's connected
115 try:
116 socket.getpeername(self)
117 except:
118 # no, no connection yet
119 self._sslobj = None
Thomas Woutersed03b412007-08-28 21:37:11 +0000120 else:
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000121 # yes, create the SSL object
Bill Janssen6e027db2007-11-15 22:23:56 +0000122 try:
123 self._sslobj = _ssl.sslwrap(self, server_side,
124 keyfile, certfile,
125 cert_reqs, ssl_version, ca_certs)
126 if do_handshake_on_connect:
Bill Janssen48dc27c2007-12-05 03:38:10 +0000127 timeout = self.gettimeout()
128 if timeout == 0.0:
129 # non-blocking
130 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
Bill Janssen6e027db2007-11-15 22:23:56 +0000131 self.do_handshake()
Bill Janssen48dc27c2007-12-05 03:38:10 +0000132
Bill Janssen6e027db2007-11-15 22:23:56 +0000133 except socket_error as x:
134 self.close()
135 raise x
136
Thomas Woutersed03b412007-08-28 21:37:11 +0000137 self.keyfile = keyfile
138 self.certfile = certfile
139 self.cert_reqs = cert_reqs
140 self.ssl_version = ssl_version
141 self.ca_certs = ca_certs
Bill Janssen6e027db2007-11-15 22:23:56 +0000142 self.do_handshake_on_connect = do_handshake_on_connect
143 self.suppress_ragged_eofs = suppress_ragged_eofs
Thomas Woutersed03b412007-08-28 21:37:11 +0000144
Guido van Rossumb7b030e2007-11-16 01:28:45 +0000145 def dup(self):
146 raise NotImplemented("Can't dup() %s instances" %
147 self.__class__.__name__)
148
Bill Janssen6e027db2007-11-15 22:23:56 +0000149 def _checkClosed(self, msg=None):
150 # raise an exception here if you wish to check for spurious closes
151 pass
152
Bill Janssen54cc54c2007-12-14 22:08:56 +0000153 def read(self, len=0, buffer=None):
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000154 """Read up to LEN bytes and return them.
155 Return zero-length string on EOF."""
156
Bill Janssen6e027db2007-11-15 22:23:56 +0000157 self._checkClosed()
158 try:
159 if buffer:
Bill Janssen54cc54c2007-12-14 22:08:56 +0000160 v = self._sslobj.read(buffer, len)
Bill Janssen6e027db2007-11-15 22:23:56 +0000161 else:
Bill Janssen54cc54c2007-12-14 22:08:56 +0000162 v = self._sslobj.read(len or 1024)
163 return v
Bill Janssen6e027db2007-11-15 22:23:56 +0000164 except SSLError as x:
165 if x.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
Bill Janssen54cc54c2007-12-14 22:08:56 +0000166 if buffer:
167 return 0
168 else:
169 return b''
Bill Janssen6e027db2007-11-15 22:23:56 +0000170 else:
171 raise
Thomas Woutersed03b412007-08-28 21:37:11 +0000172
173 def write(self, data):
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000174 """Write DATA to the underlying SSL channel. Returns
175 number of bytes of DATA actually transmitted."""
176
Bill Janssen6e027db2007-11-15 22:23:56 +0000177 self._checkClosed()
Thomas Woutersed03b412007-08-28 21:37:11 +0000178 return self._sslobj.write(data)
179
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000180 def getpeercert(self, binary_form=False):
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000181 """Returns a formatted version of the data in the
182 certificate provided by the other end of the SSL channel.
183 Return None if no certificate was provided, {} if a
184 certificate was provided, but not validated."""
185
Bill Janssen6e027db2007-11-15 22:23:56 +0000186 self._checkClosed()
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000187 return self._sslobj.peer_certificate(binary_form)
188
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000189 def cipher(self):
Bill Janssen6e027db2007-11-15 22:23:56 +0000190 self._checkClosed()
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000191 if not self._sslobj:
192 return None
193 else:
194 return self._sslobj.cipher()
Thomas Woutersed03b412007-08-28 21:37:11 +0000195
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000196 def send(self, data, flags=0):
Bill Janssen6e027db2007-11-15 22:23:56 +0000197 self._checkClosed()
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000198 if self._sslobj:
199 if flags != 0:
200 raise ValueError(
201 "non-zero flags not allowed in calls to send() on %s" %
202 self.__class__)
Bill Janssen6e027db2007-11-15 22:23:56 +0000203 while True:
204 try:
205 v = self._sslobj.write(data)
206 except SSLError as x:
207 if x.args[0] == SSL_ERROR_WANT_READ:
208 return 0
209 elif x.args[0] == SSL_ERROR_WANT_WRITE:
210 return 0
211 else:
212 raise
213 else:
214 return v
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000215 else:
216 return socket.send(self, data, flags)
Thomas Woutersed03b412007-08-28 21:37:11 +0000217
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000218 def send_to(self, data, addr, flags=0):
Bill Janssen6e027db2007-11-15 22:23:56 +0000219 self._checkClosed()
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000220 if self._sslobj:
221 raise ValueError("send_to not allowed on instances of %s" %
222 self.__class__)
223 else:
224 return socket.send_to(self, data, addr, flags)
Thomas Woutersed03b412007-08-28 21:37:11 +0000225
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000226 def sendall(self, data, flags=0):
Bill Janssen6e027db2007-11-15 22:23:56 +0000227 self._checkClosed()
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000228 if self._sslobj:
Bill Janssen6e027db2007-11-15 22:23:56 +0000229 amount = len(data)
230 count = 0
231 while (count < amount):
232 v = self.send(data[count:])
233 count += v
234 return amount
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000235 else:
236 return socket.sendall(self, data, flags)
Thomas Woutersed03b412007-08-28 21:37:11 +0000237
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000238 def recv(self, buflen=1024, flags=0):
Bill Janssen6e027db2007-11-15 22:23:56 +0000239 self._checkClosed()
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000240 if self._sslobj:
241 if flags != 0:
242 raise ValueError(
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000243 "non-zero flags not allowed in calls to recv_into() on %s" %
244 self.__class__)
Bill Janssen6e027db2007-11-15 22:23:56 +0000245 while True:
246 try:
247 return self.read(buflen)
248 except SSLError as x:
249 if x.args[0] == SSL_ERROR_WANT_READ:
250 continue
251 else:
252 raise x
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000253 else:
254 return socket.recv(self, buflen, flags)
Thomas Woutersed03b412007-08-28 21:37:11 +0000255
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000256 def recv_into(self, buffer, nbytes=None, flags=0):
Bill Janssen6e027db2007-11-15 22:23:56 +0000257 self._checkClosed()
258 if buffer and (nbytes is None):
259 nbytes = len(buffer)
260 elif nbytes is None:
261 nbytes = 1024
262 if self._sslobj:
263 if flags != 0:
264 raise ValueError(
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000265 "non-zero flags not allowed in calls to recv_into() on %s" %
266 self.__class__)
Bill Janssen6e027db2007-11-15 22:23:56 +0000267 while True:
268 try:
269 v = self.read(nbytes, buffer)
Bill Janssen6e027db2007-11-15 22:23:56 +0000270 return v
271 except SSLError as x:
272 if x.args[0] == SSL_ERROR_WANT_READ:
273 continue
274 else:
275 raise x
276 else:
277 return socket.recv_into(self, buffer, nbytes, flags)
278
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000279 def recv_from(self, addr, buflen=1024, flags=0):
Bill Janssen6e027db2007-11-15 22:23:56 +0000280 self._checkClosed()
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000281 if self._sslobj:
282 raise ValueError("recv_from not allowed on instances of %s" %
283 self.__class__)
284 else:
285 return socket.recv_from(self, addr, buflen, flags)
Thomas Woutersed03b412007-08-28 21:37:11 +0000286
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000287 def pending(self):
Bill Janssen6e027db2007-11-15 22:23:56 +0000288 self._checkClosed()
289 if self._sslobj:
290 return self._sslobj.pending()
291 else:
292 return 0
293
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000294 def shutdown(self, how):
Bill Janssen6e027db2007-11-15 22:23:56 +0000295 self._checkClosed()
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000296 self._sslobj = None
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000297 socket.shutdown(self, how)
Thomas Woutersed03b412007-08-28 21:37:11 +0000298
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000299 def _real_close(self):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000300 self._sslobj = None
Bill Janssen6e027db2007-11-15 22:23:56 +0000301 # self._closed = True
Bill Janssen54cc54c2007-12-14 22:08:56 +0000302 socket._real_close(self)
Bill Janssen6e027db2007-11-15 22:23:56 +0000303
Bill Janssen48dc27c2007-12-05 03:38:10 +0000304 def do_handshake(self, block=False):
Bill Janssen6e027db2007-11-15 22:23:56 +0000305 """Perform a TLS/SSL handshake."""
306
Bill Janssen48dc27c2007-12-05 03:38:10 +0000307 timeout = self.gettimeout()
Bill Janssen6e027db2007-11-15 22:23:56 +0000308 try:
Bill Janssen48dc27c2007-12-05 03:38:10 +0000309 if timeout == 0.0 and block:
310 self.settimeout(None)
Bill Janssen6e027db2007-11-15 22:23:56 +0000311 self._sslobj.do_handshake()
Bill Janssen48dc27c2007-12-05 03:38:10 +0000312 finally:
313 self.settimeout(timeout)
Thomas Woutersed03b412007-08-28 21:37:11 +0000314
315 def connect(self, addr):
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000316 """Connects to remote ADDR, and then wraps the connection in
317 an SSL channel."""
318
Thomas Woutersed03b412007-08-28 21:37:11 +0000319 # Here we assume that the socket is client-side, and not
320 # connected at the time of the call. We connect it, then wrap it.
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000321 if self._sslobj:
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000322 raise ValueError("attempt to connect already-connected SSLSocket!")
Thomas Woutersed03b412007-08-28 21:37:11 +0000323 socket.connect(self, addr)
Bill Janssen6e027db2007-11-15 22:23:56 +0000324 self._sslobj = _ssl.sslwrap(self, False, self.keyfile, self.certfile,
Thomas Woutersed03b412007-08-28 21:37:11 +0000325 self.cert_reqs, self.ssl_version,
326 self.ca_certs)
Bill Janssen54cc54c2007-12-14 22:08:56 +0000327 try:
328 if self.do_handshake_on_connect:
329 self.do_handshake()
330 except:
331 self._sslobj = None
332 raise
Thomas Woutersed03b412007-08-28 21:37:11 +0000333
334 def accept(self):
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000335 """Accepts a new connection from a remote client, and returns
336 a tuple containing that new connection wrapped with a server-side
337 SSL channel, and the address of the remote client."""
338
339 newsock, addr = socket.accept(self)
Bill Janssen6e027db2007-11-15 22:23:56 +0000340 return (SSLSocket(sock=newsock,
341 keyfile=self.keyfile, certfile=self.certfile,
342 server_side=True,
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000343 cert_reqs=self.cert_reqs,
344 ssl_version=self.ssl_version,
Bill Janssen6e027db2007-11-15 22:23:56 +0000345 ca_certs=self.ca_certs,
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000346 do_handshake_on_connect=
347 self.do_handshake_on_connect),
Bill Janssen6e027db2007-11-15 22:23:56 +0000348 addr)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000349
Guido van Rossume6650f92007-12-06 19:05:55 +0000350 def __del__(self):
Bill Janssen54cc54c2007-12-14 22:08:56 +0000351 # sys.stderr.write("__del__ on %s\n" % repr(self))
Guido van Rossume6650f92007-12-06 19:05:55 +0000352 self._real_close()
353
Bill Janssen54cc54c2007-12-14 22:08:56 +0000354
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000355def wrap_socket(sock, keyfile=None, certfile=None,
356 server_side=False, cert_reqs=CERT_NONE,
Bill Janssen6e027db2007-11-15 22:23:56 +0000357 ssl_version=PROTOCOL_SSLv23, ca_certs=None,
Bill Janssen48dc27c2007-12-05 03:38:10 +0000358 do_handshake_on_connect=True,
359 suppress_ragged_eofs=True):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000360
Bill Janssen6e027db2007-11-15 22:23:56 +0000361 return SSLSocket(sock=sock, keyfile=keyfile, certfile=certfile,
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000362 server_side=server_side, cert_reqs=cert_reqs,
Bill Janssen6e027db2007-11-15 22:23:56 +0000363 ssl_version=ssl_version, ca_certs=ca_certs,
Bill Janssen48dc27c2007-12-05 03:38:10 +0000364 do_handshake_on_connect=do_handshake_on_connect,
365 suppress_ragged_eofs=suppress_ragged_eofs)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000366
Thomas Woutersed03b412007-08-28 21:37:11 +0000367# some utility functions
368
369def cert_time_to_seconds(cert_time):
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000370 """Takes a date-time string in standard ASN1_print form
371 ("MON DAY 24HOUR:MINUTE:SEC YEAR TIMEZONE") and return
372 a Python time value in seconds past the epoch."""
373
Thomas Woutersed03b412007-08-28 21:37:11 +0000374 import time
375 return time.mktime(time.strptime(cert_time, "%b %d %H:%M:%S %Y GMT"))
376
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000377PEM_HEADER = "-----BEGIN CERTIFICATE-----"
378PEM_FOOTER = "-----END CERTIFICATE-----"
379
380def DER_cert_to_PEM_cert(der_cert_bytes):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000381 """Takes a certificate in binary DER format and returns the
382 PEM version of it as a string."""
383
Bill Janssen6e027db2007-11-15 22:23:56 +0000384 f = str(base64.standard_b64encode(der_cert_bytes), 'ASCII', 'strict')
385 return (PEM_HEADER + '\n' +
386 textwrap.fill(f, 64) + '\n' +
387 PEM_FOOTER + '\n')
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000388
389def PEM_cert_to_DER_cert(pem_cert_string):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000390 """Takes a certificate in ASCII PEM format and returns the
391 DER-encoded version of it as a byte sequence"""
392
393 if not pem_cert_string.startswith(PEM_HEADER):
394 raise ValueError("Invalid PEM encoding; must start with %s"
395 % PEM_HEADER)
396 if not pem_cert_string.strip().endswith(PEM_FOOTER):
397 raise ValueError("Invalid PEM encoding; must end with %s"
398 % PEM_FOOTER)
399 d = pem_cert_string.strip()[len(PEM_HEADER):-len(PEM_FOOTER)]
Bill Janssen6e027db2007-11-15 22:23:56 +0000400 return base64.decodestring(d.encode('ASCII', 'strict'))
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000401
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000402def get_server_certificate(addr, ssl_version=PROTOCOL_SSLv3, ca_certs=None):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000403 """Retrieve the certificate from the server at the specified address,
404 and return it as a PEM-encoded string.
405 If 'ca_certs' is specified, validate the server cert against it.
406 If 'ssl_version' is specified, use it in the connection attempt."""
407
408 host, port = addr
409 if (ca_certs is not None):
410 cert_reqs = CERT_REQUIRED
411 else:
412 cert_reqs = CERT_NONE
413 s = wrap_socket(socket(), ssl_version=ssl_version,
414 cert_reqs=cert_reqs, ca_certs=ca_certs)
415 s.connect(addr)
416 dercert = s.getpeercert(True)
417 s.close()
418 return DER_cert_to_PEM_cert(dercert)
419
Guido van Rossum5b8b1552007-11-16 00:06:11 +0000420def get_protocol_name(protocol_code):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000421 if protocol_code == PROTOCOL_TLSv1:
422 return "TLSv1"
423 elif protocol_code == PROTOCOL_SSLv23:
424 return "SSLv23"
425 elif protocol_code == PROTOCOL_SSLv2:
426 return "SSLv2"
427 elif protocol_code == PROTOCOL_SSLv3:
428 return "SSLv3"
429 else:
430 return "<unknown>"