| 1 |
""" |
| 2 |
Copyright (c) 2023 Proton AG |
| 3 |
|
| 4 |
This file is part of Proton. |
| 5 |
|
| 6 |
Proton is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
Proton is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with ProtonVPN. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
""" |
| 19 |
# N A large safe prime (N = 2q+1, where q is prime) |
| 20 |
# All arithmetic is done modulo N. |
| 21 |
# g A generator modulo N |
| 22 |
# k Multiplier parameter (k = H(N, g) in SRP-6a, k = 3 for legacy SRP-6) |
| 23 |
# s User's salt |
| 24 |
# I Username |
| 25 |
# p Cleartext Password |
| 26 |
# H() One-way hash function |
| 27 |
# ^ (Modular) Exponentiation |
| 28 |
# u Random scrambling parameter |
| 29 |
# a,b Secret ephemeral values |
| 30 |
# A,B Public ephemeral values |
| 31 |
# x Private key (derived from p and s) |
| 32 |
# v Password verifier |
| 33 |
|
| 34 |
from __future__ import division |
| 35 |
|
| 36 |
import ctypes |
| 37 |
import sys, os |
| 38 |
|
| 39 |
from .pmhash import pmhash |
| 40 |
from .util import PM_VERSION, SRP_LEN_BYTES, SALT_LEN_BYTES, hash_password |
| 41 |
|
| 42 |
dlls = list() |
| 43 |
|
| 44 |
platform = sys.platform |
| 45 |
if platform == 'darwin': |
| 46 |
dlls.append(ctypes.cdll.LoadLibrary('libssl.dylib')) |
| 47 |
elif 'win' in platform: |
| 48 |
for d in ('libeay32.dll', 'libssl32.dll', 'ssleay32.dll'): |
| 49 |
try: |
| 50 |
dlls.append(ctypes.cdll.LoadLibrary(d)) |
| 51 |
except Exception: #nosec |
| 52 |
pass |
| 53 |
else: |
| 54 |
try: |
| 55 |
dlls.append(ctypes.cdll.LoadLibrary('libssl.so.10')) |
| 56 |
except OSError: |
| 57 |
try: |
| 58 |
dlls.append(ctypes.cdll.LoadLibrary('libssl.so.1.0.0')) |
| 59 |
except OSError: |
| 60 |
dlls.append(ctypes.cdll.LoadLibrary('libssl.so')) |
| 61 |
|
| 62 |
|
| 63 |
class BIGNUM_Struct(ctypes.Structure): |
| 64 |
_fields_ = [("d", ctypes.c_void_p), |
| 65 |
("top", ctypes.c_int), |
| 66 |
("dmax", ctypes.c_int), |
| 67 |
("neg", ctypes.c_int), |
| 68 |
("flags", ctypes.c_int)] |
| 69 |
|
| 70 |
|
| 71 |
class BN_CTX_Struct(ctypes.Structure): |
| 72 |
_fields_ = [("_", ctypes.c_byte)] |
| 73 |
|
| 74 |
|
| 75 |
BIGNUM = ctypes.POINTER(BIGNUM_Struct) |
| 76 |
BN_CTX = ctypes.POINTER(BN_CTX_Struct) |
| 77 |
|
| 78 |
|
| 79 |
def load_func(name, args, returns=ctypes.c_int): |
| 80 |
d = sys.modules[__name__].__dict__ |
| 81 |
f = None |
| 82 |
|
| 83 |
for dll in dlls: |
| 84 |
try: |
| 85 |
f = getattr(dll, name) |
| 86 |
f.argtypes = args |
| 87 |
f.restype = returns |
| 88 |
d[name] = f |
| 89 |
return |
| 90 |
except Exception: #nosec |
| 91 |
pass |
| 92 |
raise ImportError('Unable to load required functions from SSL dlls') |
| 93 |
|
| 94 |
|
| 95 |
load_func('BN_new', [], BIGNUM) |
| 96 |
load_func('BN_free', [BIGNUM], None) |
| 97 |
load_func('BN_clear', [BIGNUM], None) |
| 98 |
load_func('BN_set_flags', [BIGNUM, ctypes.c_int], None) |
| 99 |
|
| 100 |
load_func('BN_CTX_new', [], BN_CTX) |
| 101 |
load_func('BN_CTX_free', [BN_CTX], None) |
| 102 |
|
| 103 |
load_func('BN_cmp', [BIGNUM, BIGNUM], ctypes.c_int) |
| 104 |
|
| 105 |
load_func('BN_num_bits', [BIGNUM], ctypes.c_int) |
| 106 |
|
| 107 |
load_func('BN_add', [BIGNUM, BIGNUM, BIGNUM]) |
| 108 |
load_func('BN_sub', [BIGNUM, BIGNUM, BIGNUM]) |
| 109 |
load_func('BN_mul', [BIGNUM, BIGNUM, BIGNUM, BN_CTX]) |
| 110 |
load_func('BN_div', [BIGNUM, BIGNUM, BIGNUM, BIGNUM, BN_CTX]) |
| 111 |
load_func('BN_mod_exp', [BIGNUM, BIGNUM, BIGNUM, BIGNUM, BN_CTX]) |
| 112 |
|
| 113 |
load_func('BN_rand', [BIGNUM, ctypes.c_int, ctypes.c_int, ctypes.c_int]) |
| 114 |
|
| 115 |
load_func('BN_bn2bin', [BIGNUM, ctypes.c_char_p]) |
| 116 |
load_func('BN_bin2bn', [ctypes.c_char_p, ctypes.c_int, BIGNUM], BIGNUM) |
| 117 |
|
| 118 |
load_func('BN_hex2bn', [ctypes.POINTER(BIGNUM), ctypes.c_char_p]) |
| 119 |
load_func('BN_bn2hex', [BIGNUM], ctypes.c_char_p) |
| 120 |
|
| 121 |
load_func('CRYPTO_free', [ctypes.c_char_p]) |
| 122 |
|
| 123 |
load_func('RAND_seed', [ctypes.c_char_p, ctypes.c_int]) |
| 124 |
|
| 125 |
|
| 126 |
def new_bn(): |
| 127 |
bn = BN_new() |
| 128 |
BN_set_flags(bn, 0x04) # BN_FLAG_CONSTTIME |
| 129 |
return bn |
| 130 |
|
| 131 |
|
| 132 |
def bn_num_bytes(a): |
| 133 |
return ((BN_num_bits(a) + 7) // 8) # noqa |
| 134 |
|
| 135 |
|
| 136 |
def bn_mod(rem, m, d, ctx): |
| 137 |
return BN_div(None, rem, m, d, ctx) # noqa |
| 138 |
|
| 139 |
|
| 140 |
def bn_is_zero(n): |
| 141 |
return n[0].top == 0 |
| 142 |
|
| 143 |
|
| 144 |
def bn_to_bytes(n, num_bytes): |
| 145 |
b = ctypes.create_string_buffer(bn_num_bytes(n)) |
| 146 |
BN_bn2bin(n, b) # noqa |
| 147 |
return b.raw[::-1].ljust(num_bytes, b'\0') |
| 148 |
|
| 149 |
|
| 150 |
def bytes_to_bn(dest_bn, bytes): |
| 151 |
BN_bin2bn(bytes[::-1], len(bytes), dest_bn) # noqa |
| 152 |
|
| 153 |
|
| 154 |
def bn_hash(hash_class, dest, n1, n2): |
| 155 |
h = hash_class() |
| 156 |
h.update(bn_to_bytes(n1, SRP_LEN_BYTES)) |
| 157 |
h.update(bn_to_bytes(n2, SRP_LEN_BYTES)) |
| 158 |
d = h.digest() |
| 159 |
bytes_to_bn(dest, d) |
| 160 |
|
| 161 |
|
| 162 |
def bn_hash_k(hash_class, dest, g, N, width): |
| 163 |
h = hash_class() |
| 164 |
bin1 = ctypes.create_string_buffer(width) |
| 165 |
bin2 = ctypes.create_string_buffer(width) |
| 166 |
BN_bn2bin(g, bin1) # noqa |
| 167 |
BN_bn2bin(N, bin2) # noqa |
| 168 |
h.update(bin1) |
| 169 |
h.update(bin2[::-1]) |
| 170 |
bytes_to_bn(dest, h.digest()) |
| 171 |
|
| 172 |
|
| 173 |
def calculate_x(hash_class, dest, salt, password, modulus, version): |
| 174 |
exp = hash_password( |
| 175 |
hash_class, password, salt, bn_to_bytes(modulus, SRP_LEN_BYTES), version |
| 176 |
) |
| 177 |
bytes_to_bn(dest, exp) |
| 178 |
|
| 179 |
|
| 180 |
def update_hash(h, n): |
| 181 |
h.update(bn_to_bytes(n, SRP_LEN_BYTES)) |
| 182 |
|
| 183 |
|
| 184 |
def calculate_client_challenge(hash_class, A, B, K): |
| 185 |
h = hash_class() |
| 186 |
update_hash(h, A) |
| 187 |
update_hash(h, B) |
| 188 |
h.update(K) |
| 189 |
return h.digest() |
| 190 |
|
| 191 |
|
| 192 |
def calculate_server_challenge(hash_class, A, M, K): |
| 193 |
h = hash_class() |
| 194 |
update_hash(h, A) |
| 195 |
h.update(M) |
| 196 |
h.update(K) |
| 197 |
return h.digest() |
| 198 |
|
| 199 |
|
| 200 |
def get_ngk(hash_class, n_bin, g_hex, ctx): |
| 201 |
N = new_bn() # noqa |
| 202 |
g = new_bn() # noqa |
| 203 |
k = new_bn() # noqa |
| 204 |
|
| 205 |
bytes_to_bn(N, n_bin) |
| 206 |
BN_hex2bn(g, g_hex) # noqa |
| 207 |
bn_hash_k(hash_class, k, g, N, SRP_LEN_BYTES) |
| 208 |
|
| 209 |
return N, g, k |
| 210 |
|
| 211 |
|
| 212 |
class User(object): |
| 213 |
def __init__(self, password, n_bin, g_hex=b"2", bytes_a=None, bytes_A=None): # noqa |
| 214 |
if bytes_a and len(bytes_a) != 32: |
| 215 |
raise ValueError("32 bytes required for bytes_a") |
| 216 |
|
| 217 |
if not isinstance(password, str) or len(password) == 0: |
| 218 |
raise ValueError("Invalid password") |
| 219 |
|
| 220 |
self.password = password.encode() |
| 221 |
self.a = new_bn() # noqa |
| 222 |
self.A = new_bn() # noqa |
| 223 |
self.B = new_bn() # noqa |
| 224 |
self.S = new_bn() # noqa |
| 225 |
self.u = new_bn() # noqa |
| 226 |
self.x = new_bn() # noqa |
| 227 |
self.v = new_bn() # noqa |
| 228 |
self.tmp1 = new_bn() # noqa |
| 229 |
self.tmp2 = new_bn() # noqa |
| 230 |
self.tmp3 = new_bn() # noqa |
| 231 |
self.ctx = BN_CTX_new() # noqa |
| 232 |
self.M = None |
| 233 |
self.K = None |
| 234 |
self.expected_server_proof = None |
| 235 |
self._authenticated = False |
| 236 |
self.bytes_s = None |
| 237 |
|
| 238 |
self.hash_class = pmhash |
| 239 |
self.N, self.g, self.k = get_ngk(self.hash_class, n_bin, g_hex, self.ctx) # noqa |
| 240 |
|
| 241 |
if bytes_a: |
| 242 |
bytes_to_bn(self.a, bytes_a) |
| 243 |
else: |
| 244 |
BN_rand(self.a, 256, 0, 0) # noqa |
| 245 |
|
| 246 |
if bytes_A: |
| 247 |
bytes_to_bn(self.A, bytes_A) |
| 248 |
else: |
| 249 |
BN_mod_exp(self.A, self.g, self.a, self.N, self.ctx) # noqa |
| 250 |
|
| 251 |
def __del__(self): |
| 252 |
if not hasattr(self, 'a'): |
| 253 |
return # __init__ threw exception. no clean up required |
| 254 |
BN_free(self.a) # noqa |
| 255 |
BN_free(self.A) # noqa |
| 256 |
BN_free(self.B) # noqa |
| 257 |
BN_free(self.S) # noqa |
| 258 |
BN_free(self.u) # noqa |
| 259 |
BN_free(self.x) # noqa |
| 260 |
BN_free(self.v) # noqa |
| 261 |
BN_free(self.N) # noqa |
| 262 |
BN_free(self.g) # noqa |
| 263 |
BN_free(self.k) # noqa |
| 264 |
BN_free(self.tmp1) # noqa |
| 265 |
BN_free(self.tmp2) # noqa |
| 266 |
BN_free(self.tmp3) # noqa |
| 267 |
BN_CTX_free(self.ctx) # noqa |
| 268 |
|
| 269 |
def authenticated(self): |
| 270 |
return self._authenticated |
| 271 |
|
| 272 |
def get_ephemeral_secret(self): |
| 273 |
return bn_to_bytes(self.a, SRP_LEN_BYTES) |
| 274 |
|
| 275 |
def get_session_key(self): |
| 276 |
return self.K if self._authenticated else None |
| 277 |
|
| 278 |
def get_challenge(self): |
| 279 |
return bn_to_bytes(self.A, SRP_LEN_BYTES) |
| 280 |
|
| 281 |
# Returns M or None if SRP-6a safety check is violated |
| 282 |
def process_challenge( |
| 283 |
self, bytes_s, bytes_server_challenge, version=PM_VERSION |
| 284 |
): |
| 285 |
self.bytes_s = bytes_s |
| 286 |
bytes_to_bn(self.B, bytes_server_challenge) |
| 287 |
|
| 288 |
# SRP-6a safety check |
| 289 |
if bn_is_zero(self.B): |
| 290 |
return None |
| 291 |
|
| 292 |
bn_hash(self.hash_class, self.u, self.A, self.B) |
| 293 |
|
| 294 |
# SRP-6a safety check |
| 295 |
if bn_is_zero(self.u): |
| 296 |
return None |
| 297 |
|
| 298 |
calculate_x( |
| 299 |
self.hash_class, self.x, self.bytes_s, self.password, self.N, version |
| 300 |
) |
| 301 |
BN_mod_exp(self.v, self.g, self.x, self.N, self.ctx) # noqa |
| 302 |
|
| 303 |
# S = (B - k*(g^x)) ^ (a + ux) |
| 304 |
BN_mul(self.tmp1, self.u, self.x, self.ctx) # noqa |
| 305 |
BN_add(self.tmp2, self.a, self.tmp1) # noqa tmp2 = (a + ux) |
| 306 |
BN_mod_exp(self.tmp1, self.g, self.x, self.N, self.ctx) # noqa |
| 307 |
BN_mul(self.tmp3, self.k, self.tmp1, self.ctx) # noqa tmp3 = k*(g^x) |
| 308 |
BN_sub(self.tmp1, self.B, self.tmp3) # noqa tmp1 = (B - K*(g^x)) |
| 309 |
BN_mod_exp(self.S, self.tmp1, self.tmp2, self.N, self.ctx) # noqa |
| 310 |
|
| 311 |
self.K = bn_to_bytes(self.S, SRP_LEN_BYTES) |
| 312 |
self.M = calculate_client_challenge( |
| 313 |
self.hash_class, self.A, self.B, self.K |
| 314 |
) |
| 315 |
self.expected_server_proof = calculate_server_challenge( |
| 316 |
self.hash_class, self.A, self.M, self.K |
| 317 |
) |
| 318 |
|
| 319 |
return self.M |
| 320 |
|
| 321 |
def verify_session(self, server_proof): |
| 322 |
if self.expected_server_proof == server_proof: |
| 323 |
self._authenticated = True |
| 324 |
|
| 325 |
def compute_v(self, bytes_s=None, version=PM_VERSION): |
| 326 |
if bytes_s is None: |
| 327 |
salt = new_bn() |
| 328 |
BN_rand(salt, 10*8, 0, 0) # noqa |
| 329 |
self.bytes_s = bn_to_bytes(salt, SALT_LEN_BYTES) |
| 330 |
else: |
| 331 |
self.bytes_s = bytes_s |
| 332 |
|
| 333 |
calculate_x( |
| 334 |
self.hash_class, self.x, self.bytes_s, self.password, self.N, version |
| 335 |
) |
| 336 |
BN_mod_exp(self.v, self.g, self.x, self.N, self.ctx) |
| 337 |
return self.bytes_s, bn_to_bytes(self.v, SRP_LEN_BYTES) |
| 338 |
|
| 339 |
# --------------------------------------------------------- |
| 340 |
# Init |
| 341 |
# |
| 342 |
RAND_seed(os.urandom(32), 32) # noqa |