OLD | NEW |
| 1 # Authors: |
| 2 # Trevor Perrin |
| 3 # Kees Bos - Fixes for compatibility with different Python versions |
| 4 # Martin von Loewis - python 3 port |
| 5 # |
| 6 # See the LICENSE file for legal information regarding use of this file. |
| 7 |
| 8 |
1 """TLS Lite + xmlrpclib.""" | 9 """TLS Lite + xmlrpclib.""" |
2 | 10 |
3 import xmlrpclib | 11 try: |
4 import httplib | 12 import xmlrpclib |
| 13 import httplib |
| 14 except ImportError: |
| 15 # Python 3 |
| 16 from xmlrpc import client as xmlrpclib |
| 17 from http import client as httplib |
5 from tlslite.integration.httptlsconnection import HTTPTLSConnection | 18 from tlslite.integration.httptlsconnection import HTTPTLSConnection |
6 from tlslite.integration.clienthelper import ClientHelper | 19 from tlslite.integration.clienthelper import ClientHelper |
| 20 import tlslite.errors |
7 | 21 |
8 | 22 |
9 class XMLRPCTransport(xmlrpclib.Transport, ClientHelper): | 23 class XMLRPCTransport(xmlrpclib.Transport, ClientHelper): |
10 """Handles an HTTPS transaction to an XML-RPC server.""" | 24 """Handles an HTTPS transaction to an XML-RPC server.""" |
11 | 25 |
12 def __init__(self, | 26 # Pre python 2.7, the make_connection returns a HTTP class |
13 username=None, password=None, sharedKey=None, | 27 transport = xmlrpclib.Transport() |
| 28 conn_class_is_http = not hasattr(transport, '_connection') |
| 29 del(transport) |
| 30 |
| 31 def __init__(self, use_datetime=0, |
| 32 username=None, password=None, |
14 certChain=None, privateKey=None, | 33 certChain=None, privateKey=None, |
15 cryptoID=None, protocol=None, | 34 checker=None, |
16 x509Fingerprint=None, | 35 settings=None, |
17 x509TrustList=None, x509CommonName=None, | 36 ignoreAbruptClose=False): |
18 settings=None): | |
19 """Create a new XMLRPCTransport. | 37 """Create a new XMLRPCTransport. |
20 | 38 |
21 An instance of this class can be passed to L{xmlrpclib.ServerProxy} | 39 An instance of this class can be passed to L{xmlrpclib.ServerProxy} |
22 to use TLS with XML-RPC calls:: | 40 to use TLS with XML-RPC calls:: |
23 | 41 |
24 from tlslite.api import XMLRPCTransport | 42 from tlslite import XMLRPCTransport |
25 from xmlrpclib import ServerProxy | 43 from xmlrpclib import ServerProxy |
26 | 44 |
27 transport = XMLRPCTransport(user="alice", password="abra123") | 45 transport = XMLRPCTransport(user="alice", password="abra123") |
28 server = ServerProxy("https://localhost", transport) | 46 server = ServerProxy("https://localhost", transport) |
29 | 47 |
30 For client authentication, use one of these argument | 48 For client authentication, use one of these argument |
31 combinations: | 49 combinations: |
32 - username, password (SRP) | 50 - username, password (SRP) |
33 - username, sharedKey (shared-key) | |
34 - certChain, privateKey (certificate) | 51 - certChain, privateKey (certificate) |
35 | 52 |
36 For server authentication, you can either rely on the | 53 For server authentication, you can either rely on the |
37 implicit mutual authentication performed by SRP or | 54 implicit mutual authentication performed by SRP or |
38 shared-keys, or you can do certificate-based server | 55 you can do certificate-based server |
39 authentication with one of these argument combinations: | 56 authentication with one of these argument combinations: |
40 - cryptoID[, protocol] (requires cryptoIDlib) | |
41 - x509Fingerprint | 57 - x509Fingerprint |
42 - x509TrustList[, x509CommonName] (requires cryptlib_py) | |
43 | 58 |
44 Certificate-based server authentication is compatible with | 59 Certificate-based server authentication is compatible with |
45 SRP or certificate-based client authentication. It is | 60 SRP or certificate-based client authentication. |
46 not compatible with shared-keys. | |
47 | 61 |
48 The constructor does not perform the TLS handshake itself, but | 62 The constructor does not perform the TLS handshake itself, but |
49 simply stores these arguments for later. The handshake is | 63 simply stores these arguments for later. The handshake is |
50 performed only when this class needs to connect with the | 64 performed only when this class needs to connect with the |
51 server. Thus you should be prepared to handle TLS-specific | 65 server. Thus you should be prepared to handle TLS-specific |
52 exceptions when calling methods of L{xmlrpclib.ServerProxy}. See the | 66 exceptions when calling methods of L{xmlrpclib.ServerProxy}. See the |
53 client handshake functions in | 67 client handshake functions in |
54 L{tlslite.TLSConnection.TLSConnection} for details on which | 68 L{tlslite.TLSConnection.TLSConnection} for details on which |
55 exceptions might be raised. | 69 exceptions might be raised. |
56 | 70 |
57 @type username: str | 71 @type username: str |
58 @param username: SRP or shared-key username. Requires the | 72 @param username: SRP username. Requires the |
59 'password' or 'sharedKey' argument. | 73 'password' argument. |
60 | 74 |
61 @type password: str | 75 @type password: str |
62 @param password: SRP password for mutual authentication. | 76 @param password: SRP password for mutual authentication. |
63 Requires the 'username' argument. | 77 Requires the 'username' argument. |
64 | 78 |
65 @type sharedKey: str | 79 @type certChain: L{tlslite.x509certchain.X509CertChain} |
66 @param sharedKey: Shared key for mutual authentication. | 80 @param certChain: Certificate chain for client authentication. |
67 Requires the 'username' argument. | 81 Requires the 'privateKey' argument. Excludes the SRP arguments. |
68 | 82 |
69 @type certChain: L{tlslite.X509CertChain.X509CertChain} or | 83 @type privateKey: L{tlslite.utils.rsakey.RSAKey} |
70 L{cryptoIDlib.CertChain.CertChain} | 84 @param privateKey: Private key for client authentication. |
71 @param certChain: Certificate chain for client authentication. | 85 Requires the 'certChain' argument. Excludes the SRP arguments. |
72 Requires the 'privateKey' argument. Excludes the SRP or | |
73 shared-key related arguments. | |
74 | 86 |
75 @type privateKey: L{tlslite.utils.RSAKey.RSAKey} | 87 @type checker: L{tlslite.checker.Checker} |
76 @param privateKey: Private key for client authentication. | 88 @param checker: Callable object called after handshaking to |
77 Requires the 'certChain' argument. Excludes the SRP or | 89 evaluate the connection and raise an Exception if necessary. |
78 shared-key related arguments. | |
79 | 90 |
80 @type cryptoID: str | 91 @type settings: L{tlslite.handshakesettings.HandshakeSettings} |
81 @param cryptoID: cryptoID for server authentication. Mutually | |
82 exclusive with the 'x509...' arguments. | |
83 | |
84 @type protocol: str | |
85 @param protocol: cryptoID protocol URI for server | |
86 authentication. Requires the 'cryptoID' argument. | |
87 | |
88 @type x509Fingerprint: str | |
89 @param x509Fingerprint: Hex-encoded X.509 fingerprint for | |
90 server authentication. Mutually exclusive with the 'cryptoID' | |
91 and 'x509TrustList' arguments. | |
92 | |
93 @type x509TrustList: list of L{tlslite.X509.X509} | |
94 @param x509TrustList: A list of trusted root certificates. The | |
95 other party must present a certificate chain which extends to | |
96 one of these root certificates. The cryptlib_py module must be | |
97 installed to use this parameter. Mutually exclusive with the | |
98 'cryptoID' and 'x509Fingerprint' arguments. | |
99 | |
100 @type x509CommonName: str | |
101 @param x509CommonName: The end-entity certificate's 'CN' field | |
102 must match this value. For a web server, this is typically a | |
103 server name such as 'www.amazon.com'. Mutually exclusive with | |
104 the 'cryptoID' and 'x509Fingerprint' arguments. Requires the | |
105 'x509TrustList' argument. | |
106 | |
107 @type settings: L{tlslite.HandshakeSettings.HandshakeSettings} | |
108 @param settings: Various settings which can be used to control | 92 @param settings: Various settings which can be used to control |
109 the ciphersuites, certificate types, and SSL/TLS versions | 93 the ciphersuites, certificate types, and SSL/TLS versions |
110 offered by the client. | 94 offered by the client. |
| 95 |
| 96 @type ignoreAbruptClose: bool |
| 97 @param ignoreAbruptClose: ignore the TLSAbruptCloseError on |
| 98 unexpected hangup. |
111 """ | 99 """ |
112 | 100 |
| 101 # self._connection is new in python 2.7, since we're using it here, |
| 102 # we'll add this ourselves too, just in case we're pre-2.7 |
| 103 self._connection = (None, None) |
| 104 xmlrpclib.Transport.__init__(self, use_datetime) |
| 105 self.ignoreAbruptClose = ignoreAbruptClose |
113 ClientHelper.__init__(self, | 106 ClientHelper.__init__(self, |
114 username, password, sharedKey, | 107 username, password, |
115 certChain, privateKey, | 108 certChain, privateKey, |
116 cryptoID, protocol, | 109 checker, |
117 x509Fingerprint, | |
118 x509TrustList, x509CommonName, | |
119 settings) | 110 settings) |
120 | 111 |
| 112 def make_connection(self, host): |
| 113 # return an existing connection if possible. This allows |
| 114 # HTTP/1.1 keep-alive. |
| 115 if self._connection and host == self._connection[0]: |
| 116 http = self._connection[1] |
| 117 else: |
| 118 # create a HTTPS connection object from a host descriptor |
| 119 chost, extra_headers, x509 = self.get_host_info(host) |
121 | 120 |
122 def make_connection(self, host): | 121 http = HTTPTLSConnection(chost, None, |
123 # create a HTTPS connection object from a host descriptor | 122 username=self.username, password=self.passw
ord, |
124 host, extra_headers, x509 = self.get_host_info(host) | 123 certChain=self.certChain, privateKey=self.p
rivateKey, |
125 http = HTTPTLSConnection(host, None, | 124 checker=self.checker, |
126 self.username, self.password, | 125 settings=self.settings, |
127 self.sharedKey, | 126 ignoreAbruptClose=self.ignoreAbruptClose) |
128 self.certChain, self.privateKey, | 127 # store the host argument along with the connection object |
129 self.checker.cryptoID, | 128 self._connection = host, http |
130 self.checker.protocol, | 129 if not self.conn_class_is_http: |
131 self.checker.x509Fingerprint, | 130 return http |
132 self.checker.x509TrustList, | |
133 self.checker.x509CommonName, | |
134 self.settings) | |
135 http2 = httplib.HTTP() | 131 http2 = httplib.HTTP() |
136 http2._setup(http) | 132 http2._setup(http) |
137 return http2 | 133 return http2 |
OLD | NEW |