| 1 |
try: |
| 2 |
import unittest2 as unittest |
| 3 |
except ImportError: |
| 4 |
import unittest |
| 5 |
|
| 6 |
import base64 |
| 7 |
import hashlib |
| 8 |
import hmac |
| 9 |
import kerberos |
| 10 |
|
| 11 |
from mock import patch |
| 12 |
import six |
| 13 |
import struct |
| 14 |
|
| 15 |
from puresasl import SASLProtocolException, QOP |
| 16 |
from puresasl.client import SASLClient |
| 17 |
from puresasl.mechanisms import AnonymousMechanism, PlainMechanism, GSSAPIMechanism, DigestMD5Mechanism, CramMD5Mechanism, ExternalMechanism |
| 18 |
|
| 19 |
|
| 20 |
class _BaseMechanismTests(unittest.TestCase): |
| 21 |
|
| 22 |
mechanism_class = AnonymousMechanism |
| 23 |
sasl_kwargs = {} |
| 24 |
|
| 25 |
def setUp(self): |
| 26 |
self.sasl = SASLClient('localhost', mechanism=self.mechanism_class.name, **self.sasl_kwargs) |
| 27 |
self.mechanism = self.sasl._chosen_mech |
| 28 |
|
| 29 |
def test_init_basic(self, *args): |
| 30 |
sasl = SASLClient('localhost', mechanism=self.mechanism_class.name, **self.sasl_kwargs) |
| 31 |
mech = sasl._chosen_mech |
| 32 |
self.assertIs(mech.sasl, sasl) |
| 33 |
self.assertIsInstance(mech, self.mechanism_class) |
| 34 |
|
| 35 |
def test_process_basic(self, *args): |
| 36 |
self.assertIsInstance(self.sasl.process(six.b('string')), six.binary_type) |
| 37 |
self.assertIsInstance(self.sasl.process(six.b('string')), six.binary_type) |
| 38 |
|
| 39 |
def test_dispose_basic(self, *args): |
| 40 |
self.sasl.dispose() |
| 41 |
|
| 42 |
def test_wrap_unwrap(self, *args): |
| 43 |
self.assertRaises(NotImplementedError, self.sasl.wrap, 'msg') |
| 44 |
self.assertRaises(NotImplementedError, self.sasl.unwrap, 'msg') |
| 45 |
|
| 46 |
def test__pick_qop(self, *args): |
| 47 |
self.assertRaises(SASLProtocolException, self.sasl._chosen_mech._pick_qop, set()) |
| 48 |
self.sasl._chosen_mech._pick_qop(set(QOP.all)) |
| 49 |
|
| 50 |
|
| 51 |
class AnonymousMechanismTest(_BaseMechanismTests): |
| 52 |
|
| 53 |
mechanism_class = AnonymousMechanism |
| 54 |
|
| 55 |
|
| 56 |
class PlainTextMechanismTest(_BaseMechanismTests): |
| 57 |
|
| 58 |
mechanism_class = PlainMechanism |
| 59 |
username = 'user' |
| 60 |
password = 'pass' |
| 61 |
sasl_kwargs = {'username': username, 'password': password} |
| 62 |
|
| 63 |
def test_process(self): |
| 64 |
for challenge in (None, '', b'asdf', u"\U0001F44D"): |
| 65 |
response = self.sasl.process(challenge) |
| 66 |
self.assertEqual(response, six.b('\x00{0}\x00{1}'.format(self.username, self.password))) |
| 67 |
self.assertIsInstance(response, six.binary_type) |
| 68 |
|
| 69 |
def test_process_with_authorization_id_or_identity(self): |
| 70 |
challenge = u"\U0001F44D" |
| 71 |
identity = 'user2' |
| 72 |
|
| 73 |
|
| 74 |
sasl_kwargs = self.sasl_kwargs.copy() |
| 75 |
sasl_kwargs.update({'identity': identity}) |
| 76 |
sasl = SASLClient('localhost', mechanism=self.mechanism_class.name, **sasl_kwargs) |
| 77 |
response = sasl.process(challenge) |
| 78 |
self.assertEqual(response, six.b('{0}\x00{1}\x00{2}'.format(identity, self.username, self.password))) |
| 79 |
self.assertIsInstance(response, six.binary_type) |
| 80 |
self.assertTrue(sasl.complete) |
| 81 |
|
| 82 |
|
| 83 |
auth_id = 'user3' |
| 84 |
sasl_kwargs.update({'authorization_id': auth_id}) |
| 85 |
sasl = SASLClient('localhost', mechanism=self.mechanism_class.name, **sasl_kwargs) |
| 86 |
response = sasl.process(challenge) |
| 87 |
self.assertEqual(response, six.b('{0}\x00{1}\x00{2}'.format(auth_id, self.username, self.password))) |
| 88 |
self.assertIsInstance(response, six.binary_type) |
| 89 |
self.assertTrue(sasl.complete) |
| 90 |
|
| 91 |
def test_wrap_unwrap(self): |
| 92 |
msg = 'msg' |
| 93 |
self.assertIs(self.sasl.wrap(msg), msg) |
| 94 |
self.assertIs(self.sasl.unwrap(msg), msg) |
| 95 |
|
| 96 |
|
| 97 |
class ExternalMechanismTest(_BaseMechanismTests): |
| 98 |
|
| 99 |
mechanism_class = ExternalMechanism |
| 100 |
|
| 101 |
def test_process(self): |
| 102 |
self.assertIs(self.sasl.process(), b'') |
| 103 |
|
| 104 |
def test_wrap_unwrap(self): |
| 105 |
msg = 'msg' |
| 106 |
self.assertIs(self.sasl.wrap(msg), msg) |
| 107 |
self.assertIs(self.sasl.unwrap(msg), msg) |
| 108 |
|
| 109 |
|
| 110 |
@patch('puresasl.mechanisms.kerberos.authGSSClientStep') |
| 111 |
@patch('puresasl.mechanisms.kerberos.authGSSClientResponse', return_value=base64.b64encode(six.b('some\x00 response'))) |
| 112 |
class GSSAPIMechanismTest(_BaseMechanismTests): |
| 113 |
|
| 114 |
mechanism_class = GSSAPIMechanism |
| 115 |
service = 'GSSAPI' |
| 116 |
sasl_kwargs = {'service': service} |
| 117 |
|
| 118 |
@patch('puresasl.mechanisms.kerberos.authGSSClientWrap') |
| 119 |
@patch('puresasl.mechanisms.kerberos.authGSSClientUnwrap') |
| 120 |
def test_wrap_unwrap(self, _inner1, _inner2, authGSSClientResponse, *args): |
| 121 |
|
| 122 |
self.mechanism.qop = QOP.AUTH |
| 123 |
msg = b'msg' |
| 124 |
self.assertIs(self.sasl.wrap(msg), msg) |
| 125 |
self.assertIs(self.sasl.unwrap(msg), msg) |
| 126 |
|
| 127 |
for qop in (QOP.AUTH_INT, QOP.AUTH_CONF): |
| 128 |
self.mechanism.qop = qop |
| 129 |
with patch('puresasl.mechanisms.kerberos.authGSSClientResponseConf', return_value=1): |
| 130 |
self.assertEqual(self.sasl.wrap(msg), base64.b64decode(authGSSClientResponse.return_value)) |
| 131 |
self.assertEqual(self.sasl.unwrap(msg), base64.b64decode(authGSSClientResponse.return_value)) |
| 132 |
if qop == QOP.AUTH_CONF: |
| 133 |
with patch('puresasl.mechanisms.kerberos.authGSSClientResponseConf', return_value=0): |
| 134 |
self.assertRaises(Exception, self.sasl.unwrap, msg) |
| 135 |
|
| 136 |
def test_process_no_user(self, authGSSClientResponse, *args): |
| 137 |
msg = six.b('whatever') |
| 138 |
|
| 139 |
|
| 140 |
self.assertEqual(self.sasl.process(msg), base64.b64decode(authGSSClientResponse.return_value)) |
| 141 |
with patch('puresasl.mechanisms.kerberos.authGSSClientResponse', return_value=''): |
| 142 |
self.assertEqual(self.sasl.process(msg), six.b('')) |
| 143 |
|
| 144 |
username = 'username' |
| 145 |
|
| 146 |
with patch('puresasl.mechanisms.kerberos.authGSSClientStep', return_value=kerberos.AUTH_GSS_COMPLETE): |
| 147 |
with patch('puresasl.mechanisms.kerberos.authGSSClientUserName', return_value=six.b(username)): |
| 148 |
self.assertEqual(self.sasl.process(msg), six.b('')) |
| 149 |
self.assertEqual(self.mechanism.user, six.b(username)) |
| 150 |
|
| 151 |
@patch('puresasl.mechanisms.kerberos.authGSSClientUnwrap') |
| 152 |
def test_process_qop(self, *args): |
| 153 |
self.mechanism._have_negotiated_details = True |
| 154 |
self.mechanism.user = 'user' |
| 155 |
msg = six.b('msg') |
| 156 |
|
| 157 |
self.assertRaises(SASLProtocolException, self.sasl.process, msg) |
| 158 |
|
| 159 |
max_len = 100 |
| 160 |
self.assertLess(max_len, self.sasl.max_buffer) |
| 161 |
for i, qop in QOP.bit_map.items(): |
| 162 |
qop_size = struct.pack('!i', i << 24 | max_len) |
| 163 |
response = base64.b64encode(qop_size) |
| 164 |
with patch('puresasl.mechanisms.kerberos.authGSSClientResponse', return_value=response): |
| 165 |
with patch('puresasl.mechanisms.kerberos.authGSSClientWrap') as authGSSClientWrap: |
| 166 |
self.mechanism.complete = False |
| 167 |
self.assertEqual(self.sasl.process(msg), qop_size) |
| 168 |
self.assertTrue(self.mechanism.complete) |
| 169 |
self.assertEqual(self.mechanism.qop, qop) |
| 170 |
self.assertEqual(self.mechanism.max_buffer, max_len) |
| 171 |
|
| 172 |
args = authGSSClientWrap.call_args[0] |
| 173 |
out_data = args[1] |
| 174 |
out = base64.b64decode(out_data) |
| 175 |
self.assertEqual(out[:4], qop_size) |
| 176 |
self.assertEqual(out[4:], six.b(self.mechanism.user)) |
| 177 |
|
| 178 |
@patch('puresasl.mechanisms.kerberos.authGSSClientClean') |
| 179 |
def test_dispose_basic(self, authGSSClientUnwrap, *args): |
| 180 |
self.sasl.dispose() |
| 181 |
authGSSClientUnwrap.assert_called_once_with(self.mechanism.context) |
| 182 |
|
| 183 |
|
| 184 |
class CramMD5MechanismTest(_BaseMechanismTests): |
| 185 |
|
| 186 |
mechanism_class = CramMD5Mechanism |
| 187 |
username = 'user' |
| 188 |
password = 'pass' |
| 189 |
sasl_kwargs = {'username': username, 'password': password} |
| 190 |
|
| 191 |
def test_process(self): |
| 192 |
self.assertIsNone(self.sasl.process(None)) |
| 193 |
challenge = six.b('msg') |
| 194 |
hash = hmac.HMAC(key=six.b(self.password), digestmod=hashlib.md5) |
| 195 |
hash.update(challenge) |
| 196 |
response = self.sasl.process(challenge) |
| 197 |
self.assertIn(six.b(self.username), response) |
| 198 |
self.assertIn(six.b(hash.hexdigest()), response) |
| 199 |
self.assertIsInstance(response, six.binary_type) |
| 200 |
self.assertTrue(self.sasl.complete) |
| 201 |
|
| 202 |
def test_wrap_unwrap(self): |
| 203 |
msg = 'msg' |
| 204 |
self.assertIs(self.sasl.wrap(msg), msg) |
| 205 |
self.assertIs(self.sasl.unwrap(msg), msg) |
| 206 |
|
| 207 |
|
| 208 |
class DigestMD5MechanismTest(_BaseMechanismTests): |
| 209 |
|
| 210 |
mechanism_class = DigestMD5Mechanism |
| 211 |
username = 'user' |
| 212 |
password = 'pass' |
| 213 |
sasl_kwargs = {'username': username, 'password': password} |
| 214 |
|
| 215 |
def test_wrap_unwrap(self): |
| 216 |
msg = 'msg' |
| 217 |
self.assertIs(self.sasl.wrap(msg), msg) |
| 218 |
self.assertIs(self.sasl.unwrap(msg), msg) |
| 219 |
|
| 220 |
def test_process_basic(self, *args): |
| 221 |
pass |
| 222 |
|
| 223 |
def test_process(self): |
| 224 |
testChallenge = ( |
| 225 |
b'nonce="rmD6R8aMYVWH+/ih9HGBr3xNGAR6o2DUxpKlgDz6gUQ=",r' |
| 226 |
b'ealm="example.org",qop="auth,auth-int,auth-conf",cipher="rc4-40,rc' |
| 227 |
b'4-56,rc4,des,3des",maxbuf=65536,charset=utf-8,algorithm=md5-sess' |
| 228 |
) |
| 229 |
self.sasl.process(testChallenge) |
| 230 |
|
| 231 |
def test_process_server_answer(self): |
| 232 |
sasl_kwargs = {'username': "chris", 'password': "secret"} |
| 233 |
sasl = SASLClient('elwood.innosoft.com', |
| 234 |
service="imap", |
| 235 |
mechanism=self.mechanism_class.name, |
| 236 |
mutual_auth=True, |
| 237 |
**sasl_kwargs) |
| 238 |
testChallenge = ( |
| 239 |
b'utf-8,username="chris",realm="elwood.innosoft.com",' |
| 240 |
b'nonce="OA6MG9tEQGm2hh",nc=00000001,cnonce="OA6MHXh6VqTrRk",' |
| 241 |
b'digest-uri="imap/elwood.innosoft.com",' |
| 242 |
b'response=d388dad90d4bbd760a152321f2143af7,qop=auth' |
| 243 |
) |
| 244 |
sasl.process(testChallenge) |
| 245 |
|
| 246 |
|
| 247 |
sasl._chosen_mech.cnonce = b"OA6MHXh6VqTrRk" |
| 248 |
|
| 249 |
serverResponse = ( |
| 250 |
b'rspauth=ea40f60335c427b5527b84dbabcdfffd' |
| 251 |
) |
| 252 |
sasl.process(serverResponse) |
| 253 |
|
| 254 |
def test__pick_qop(self): |
| 255 |
|
| 256 |
|
| 257 |
self.assertEqual(self.sasl.qop, QOP.AUTH) |