OLD | NEW |
| (Empty) |
1 """TLS Lite + httplib.""" | |
2 | |
3 import socket | |
4 import httplib | |
5 from tlslite.TLSConnection import TLSConnection | |
6 from tlslite.integration.ClientHelper import ClientHelper | |
7 | |
8 | |
9 class HTTPBaseTLSConnection(httplib.HTTPConnection): | |
10 """This abstract class provides a framework for adding TLS support | |
11 to httplib.""" | |
12 | |
13 default_port = 443 | |
14 | |
15 def __init__(self, host, port=None, strict=None): | |
16 if strict == None: | |
17 #Python 2.2 doesn't support strict | |
18 httplib.HTTPConnection.__init__(self, host, port) | |
19 else: | |
20 httplib.HTTPConnection.__init__(self, host, port, strict) | |
21 | |
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. | |
67 | |
68 For client authentication, use one of these argument | |
69 combinations: | |
70 - username, password (SRP) | |
71 - username, sharedKey (shared-key) | |
72 - certChain, privateKey (certificate) | |
73 | |
74 For server authentication, you can either rely on the | |
75 implicit mutual authentication performed by SRP or | |
76 shared-keys, or you can do certificate-based server | |
77 authentication with one of these argument combinations: | |
78 - cryptoID[, protocol] (requires cryptoIDlib) | |
79 - x509Fingerprint | |
80 - x509TrustList[, x509CommonName] (requires cryptlib_py) | |
81 | |
82 Certificate-based server authentication is compatible with | |
83 SRP or certificate-based client authentication. It is | |
84 not compatible with shared-keys. | |
85 | |
86 The constructor does not perform the TLS handshake itself, but | |
87 simply stores these arguments for later. The handshake is | |
88 performed only when this class needs to connect with the | |
89 server. Thus you should be prepared to handle TLS-specific | |
90 exceptions when calling methods inherited from | |
91 L{httplib.HTTPConnection} such as request(), connect(), and | |
92 send(). See the client handshake functions in | |
93 L{tlslite.TLSConnection.TLSConnection} for details on which | |
94 exceptions might be raised. | |
95 | |
96 @type host: str | |
97 @param host: Server to connect to. | |
98 | |
99 @type port: int | |
100 @param port: Port to connect to. | |
101 | |
102 @type username: str | |
103 @param username: SRP or shared-key username. Requires the | |
104 'password' or 'sharedKey' argument. | |
105 | |
106 @type password: str | |
107 @param password: SRP password for mutual authentication. | |
108 Requires the 'username' argument. | |
109 | |
110 @type sharedKey: str | |
111 @param sharedKey: Shared key for mutual authentication. | |
112 Requires the 'username' argument. | |
113 | |
114 @type certChain: L{tlslite.X509CertChain.X509CertChain} or | |
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 | |
154 the ciphersuites, certificate types, and SSL/TLS versions | |
155 offered by the client. | |
156 """ | |
157 | |
158 HTTPBaseTLSConnection.__init__(self, host, port) | |
159 | |
160 ClientHelper.__init__(self, | |
161 username, password, sharedKey, | |
162 certChain, privateKey, | |
163 cryptoID, protocol, | |
164 x509Fingerprint, | |
165 x509TrustList, x509CommonName, | |
166 settings) | |
167 | |
168 def _handshake(self, tlsConnection): | |
169 ClientHelper._handshake(self, tlsConnection) | |
OLD | NEW |