OLD | NEW |
| 1 # Authors: |
| 2 # Trevor Perrin |
| 3 # Kees Bos - Added ignoreAbruptClose parameter |
| 4 # Dimitris Moraitis - Anon ciphersuites |
| 5 # Martin von Loewis - python 3 port |
| 6 # |
| 7 # See the LICENSE file for legal information regarding use of this file. |
| 8 |
1 """TLS Lite + httplib.""" | 9 """TLS Lite + httplib.""" |
2 | 10 |
3 import socket | 11 import socket |
4 import httplib | 12 try: |
| 13 import httplib |
| 14 except ImportError: |
| 15 # Python 3 |
| 16 from http import client as httplib |
5 from tlslite.tlsconnection import TLSConnection | 17 from tlslite.tlsconnection import TLSConnection |
6 from tlslite.integration.clienthelper import ClientHelper | 18 from tlslite.integration.clienthelper import ClientHelper |
7 | 19 |
8 | 20 |
9 class HTTPBaseTLSConnection(httplib.HTTPConnection): | 21 class HTTPTLSConnection(httplib.HTTPConnection, ClientHelper): |
10 """This abstract class provides a framework for adding TLS support | 22 """This class extends L{httplib.HTTPConnection} to support TLS.""" |
11 to httplib.""" | |
12 | 23 |
13 default_port = 443 | 24 def __init__(self, host, port=None, strict=None, |
14 | 25 timeout=socket._GLOBAL_DEFAULT_TIMEOUT, |
15 def __init__(self, host, port=None, strict=None): | 26 source_address=None, |
16 if strict == None: | 27 username=None, password=None, |
17 #Python 2.2 doesn't support strict | 28 certChain=None, privateKey=None, |
18 httplib.HTTPConnection.__init__(self, host, port) | 29 checker=None, |
19 else: | 30 settings=None, |
20 httplib.HTTPConnection.__init__(self, host, port, strict) | 31 ignoreAbruptClose=False, |
21 | 32 anon=False): |
22 def connect(self): | |
23 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
24 if hasattr(sock, 'settimeout'): | |
25 sock.settimeout(10) | |
26 sock.connect((self.host, self.port)) | |
27 | |
28 #Use a TLSConnection to emulate a socket | |
29 self.sock = TLSConnection(sock) | |
30 | |
31 #When httplib closes this, close the socket | |
32 self.sock.closeSocket = True | |
33 self._handshake(self.sock) | |
34 | |
35 def _handshake(self, tlsConnection): | |
36 """Called to perform some sort of handshake. | |
37 | |
38 This method must be overridden in a subclass to do some type of | |
39 handshake. This method will be called after the socket has | |
40 been connected but before any data has been sent. If this | |
41 method does not raise an exception, the TLS connection will be | |
42 considered valid. | |
43 | |
44 This method may (or may not) be called every time an HTTP | |
45 request is performed, depending on whether the underlying HTTP | |
46 connection is persistent. | |
47 | |
48 @type tlsConnection: L{tlslite.TLSConnection.TLSConnection} | |
49 @param tlsConnection: The connection to perform the handshake | |
50 on. | |
51 """ | |
52 raise NotImplementedError() | |
53 | |
54 | |
55 class HTTPTLSConnection(HTTPBaseTLSConnection, ClientHelper): | |
56 """This class extends L{HTTPBaseTLSConnection} to support the | |
57 common types of handshaking.""" | |
58 | |
59 def __init__(self, host, port=None, | |
60 username=None, password=None, sharedKey=None, | |
61 certChain=None, privateKey=None, | |
62 cryptoID=None, protocol=None, | |
63 x509Fingerprint=None, | |
64 x509TrustList=None, x509CommonName=None, | |
65 settings = None): | |
66 """Create a new HTTPTLSConnection. | 33 """Create a new HTTPTLSConnection. |
67 | 34 |
68 For client authentication, use one of these argument | 35 For client authentication, use one of these argument |
69 combinations: | 36 combinations: |
70 - username, password (SRP) | 37 - username, password (SRP) |
71 - username, sharedKey (shared-key) | |
72 - certChain, privateKey (certificate) | 38 - certChain, privateKey (certificate) |
73 | 39 |
74 For server authentication, you can either rely on the | 40 For server authentication, you can either rely on the |
75 implicit mutual authentication performed by SRP or | 41 implicit mutual authentication performed by SRP |
76 shared-keys, or you can do certificate-based server | 42 or you can do certificate-based server |
77 authentication with one of these argument combinations: | 43 authentication with one of these argument combinations: |
78 - cryptoID[, protocol] (requires cryptoIDlib) | |
79 - x509Fingerprint | 44 - x509Fingerprint |
80 - x509TrustList[, x509CommonName] (requires cryptlib_py) | |
81 | 45 |
82 Certificate-based server authentication is compatible with | 46 Certificate-based server authentication is compatible with |
83 SRP or certificate-based client authentication. It is | 47 SRP or certificate-based client authentication. |
84 not compatible with shared-keys. | |
85 | 48 |
86 The constructor does not perform the TLS handshake itself, but | 49 The constructor does not perform the TLS handshake itself, but |
87 simply stores these arguments for later. The handshake is | 50 simply stores these arguments for later. The handshake is |
88 performed only when this class needs to connect with the | 51 performed only when this class needs to connect with the |
89 server. Thus you should be prepared to handle TLS-specific | 52 server. Thus you should be prepared to handle TLS-specific |
90 exceptions when calling methods inherited from | 53 exceptions when calling methods inherited from |
91 L{httplib.HTTPConnection} such as request(), connect(), and | 54 L{httplib.HTTPConnection} such as request(), connect(), and |
92 send(). See the client handshake functions in | 55 send(). See the client handshake functions in |
93 L{tlslite.TLSConnection.TLSConnection} for details on which | 56 L{tlslite.TLSConnection.TLSConnection} for details on which |
94 exceptions might be raised. | 57 exceptions might be raised. |
95 | 58 |
96 @type host: str | 59 @type host: str |
97 @param host: Server to connect to. | 60 @param host: Server to connect to. |
98 | 61 |
99 @type port: int | 62 @type port: int |
100 @param port: Port to connect to. | 63 @param port: Port to connect to. |
101 | 64 |
102 @type username: str | 65 @type username: str |
103 @param username: SRP or shared-key username. Requires the | 66 @param username: SRP username. Requires the |
104 'password' or 'sharedKey' argument. | 67 'password' argument. |
105 | 68 |
106 @type password: str | 69 @type password: str |
107 @param password: SRP password for mutual authentication. | 70 @param password: SRP password for mutual authentication. |
108 Requires the 'username' argument. | 71 Requires the 'username' argument. |
109 | 72 |
110 @type sharedKey: str | 73 @type certChain: L{tlslite.x509certchain.X509CertChain} or |
111 @param sharedKey: Shared key for mutual authentication. | 74 @param certChain: Certificate chain for client authentication. |
112 Requires the 'username' argument. | 75 Requires the 'privateKey' argument. Excludes the SRP arguments. |
| 76 |
| 77 @type privateKey: L{tlslite.utils.rsakey.RSAKey} |
| 78 @param privateKey: Private key for client authentication. |
| 79 Requires the 'certChain' argument. Excludes the SRP arguments. |
| 80 |
| 81 @type checker: L{tlslite.checker.Checker} |
| 82 @param checker: Callable object called after handshaking to |
| 83 evaluate the connection and raise an Exception if necessary. |
113 | 84 |
114 @type certChain: L{tlslite.X509CertChain.X509CertChain} or | 85 @type settings: L{tlslite.handshakesettings.HandshakeSettings} |
115 L{cryptoIDlib.CertChain.CertChain} | |
116 @param certChain: Certificate chain for client authentication. | |
117 Requires the 'privateKey' argument. Excludes the SRP or | |
118 shared-key related arguments. | |
119 | |
120 @type privateKey: L{tlslite.utils.RSAKey.RSAKey} | |
121 @param privateKey: Private key for client authentication. | |
122 Requires the 'certChain' argument. Excludes the SRP or | |
123 shared-key related arguments. | |
124 | |
125 @type cryptoID: str | |
126 @param cryptoID: cryptoID for server authentication. Mutually | |
127 exclusive with the 'x509...' arguments. | |
128 | |
129 @type protocol: str | |
130 @param protocol: cryptoID protocol URI for server | |
131 authentication. Requires the 'cryptoID' argument. | |
132 | |
133 @type x509Fingerprint: str | |
134 @param x509Fingerprint: Hex-encoded X.509 fingerprint for | |
135 server authentication. Mutually exclusive with the 'cryptoID' | |
136 and 'x509TrustList' arguments. | |
137 | |
138 @type x509TrustList: list of L{tlslite.X509.X509} | |
139 @param x509TrustList: A list of trusted root certificates. The | |
140 other party must present a certificate chain which extends to | |
141 one of these root certificates. The cryptlib_py module must be | |
142 installed to use this parameter. Mutually exclusive with the | |
143 'cryptoID' and 'x509Fingerprint' arguments. | |
144 | |
145 @type x509CommonName: str | |
146 @param x509CommonName: The end-entity certificate's 'CN' field | |
147 must match this value. For a web server, this is typically a | |
148 server name such as 'www.amazon.com'. Mutually exclusive with | |
149 the 'cryptoID' and 'x509Fingerprint' arguments. Requires the | |
150 'x509TrustList' argument. | |
151 | |
152 @type settings: L{tlslite.HandshakeSettings.HandshakeSettings} | |
153 @param settings: Various settings which can be used to control | 86 @param settings: Various settings which can be used to control |
154 the ciphersuites, certificate types, and SSL/TLS versions | 87 the ciphersuites, certificate types, and SSL/TLS versions |
155 offered by the client. | 88 offered by the client. |
| 89 |
| 90 @type ignoreAbruptClose: bool |
| 91 @param ignoreAbruptClose: ignore the TLSAbruptCloseError on |
| 92 unexpected hangup. |
156 """ | 93 """ |
| 94 if source_address: |
| 95 httplib.HTTPConnection.__init__(self, host, port, strict, |
| 96 timeout, source_address) |
| 97 if not source_address: |
| 98 httplib.HTTPConnection.__init__(self, host, port, strict, |
| 99 timeout) |
| 100 self.ignoreAbruptClose = ignoreAbruptClose |
| 101 ClientHelper.__init__(self, |
| 102 username, password, |
| 103 certChain, privateKey, |
| 104 checker, |
| 105 settings, |
| 106 anon) |
157 | 107 |
158 HTTPBaseTLSConnection.__init__(self, host, port) | 108 def connect(self): |
159 | 109 httplib.HTTPConnection.connect(self) |
160 ClientHelper.__init__(self, | 110 self.sock = TLSConnection(self.sock) |
161 username, password, sharedKey, | 111 self.sock.ignoreAbruptClose = self.ignoreAbruptClose |
162 certChain, privateKey, | 112 ClientHelper._handshake(self, self.sock) |
163 cryptoID, protocol, | |
164 x509Fingerprint, | |
165 x509TrustList, x509CommonName, | |
166 settings) | |
167 | |
168 def _handshake(self, tlsConnection): | |
169 ClientHelper._handshake(self, tlsConnection) | |
OLD | NEW |