srp_vfy.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. * Copyright 2004-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. *
  10. * Originally written by Christophe Renou and Peter Sylvester,
  11. * for the EdelKey project.
  12. */
  13. #ifndef OPENSSL_NO_SRP
  14. # include "internal/cryptlib.h"
  15. # include "crypto/evp.h"
  16. # include <openssl/sha.h>
  17. # include <openssl/srp.h>
  18. # include <openssl/evp.h>
  19. # include <openssl/buffer.h>
  20. # include <openssl/rand.h>
  21. # include <openssl/txt_db.h>
  22. # include <openssl/err.h>
  23. # define SRP_RANDOM_SALT_LEN 20
  24. # define MAX_LEN 2500
  25. /*
  26. * Note that SRP uses its own variant of base 64 encoding. A different base64
  27. * alphabet is used and no padding '=' characters are added. Instead we pad to
  28. * the front with 0 bytes and subsequently strip off leading encoded padding.
  29. * This variant is used for compatibility with other SRP implementations -
  30. * notably libsrp, but also others. It is also required for backwards
  31. * compatibility in order to load verifier files from other OpenSSL versions.
  32. */
  33. /*
  34. * Convert a base64 string into raw byte array representation.
  35. * Returns the length of the decoded data, or -1 on error.
  36. */
  37. static int t_fromb64(unsigned char *a, size_t alen, const char *src)
  38. {
  39. EVP_ENCODE_CTX *ctx;
  40. int outl = 0, outl2 = 0;
  41. size_t size, padsize;
  42. const unsigned char *pad = (const unsigned char *)"00";
  43. while (*src == ' ' || *src == '\t' || *src == '\n')
  44. ++src;
  45. size = strlen(src);
  46. padsize = 4 - (size & 3);
  47. padsize &= 3;
  48. /* Four bytes in src become three bytes output. */
  49. if (size > INT_MAX || ((size + padsize) / 4) * 3 > alen)
  50. return -1;
  51. ctx = EVP_ENCODE_CTX_new();
  52. if (ctx == NULL)
  53. return -1;
  54. /*
  55. * This should never occur because 1 byte of data always requires 2 bytes of
  56. * encoding, i.e.
  57. * 0 bytes unencoded = 0 bytes encoded
  58. * 1 byte unencoded = 2 bytes encoded
  59. * 2 bytes unencoded = 3 bytes encoded
  60. * 3 bytes unencoded = 4 bytes encoded
  61. * 4 bytes unencoded = 6 bytes encoded
  62. * etc
  63. */
  64. if (padsize == 3) {
  65. outl = -1;
  66. goto err;
  67. }
  68. /* Valid padsize values are now 0, 1 or 2 */
  69. EVP_DecodeInit(ctx);
  70. evp_encode_ctx_set_flags(ctx, EVP_ENCODE_CTX_USE_SRP_ALPHABET);
  71. /* Add any encoded padding that is required */
  72. if (padsize != 0
  73. && EVP_DecodeUpdate(ctx, a, &outl, pad, padsize) < 0) {
  74. outl = -1;
  75. goto err;
  76. }
  77. if (EVP_DecodeUpdate(ctx, a, &outl2, (const unsigned char *)src, size) < 0) {
  78. outl = -1;
  79. goto err;
  80. }
  81. outl += outl2;
  82. EVP_DecodeFinal(ctx, a + outl, &outl2);
  83. outl += outl2;
  84. /* Strip off the leading padding */
  85. if (padsize != 0) {
  86. if ((int)padsize >= outl) {
  87. outl = -1;
  88. goto err;
  89. }
  90. /*
  91. * If we added 1 byte of padding prior to encoding then we have 2 bytes
  92. * of "real" data which gets spread across 4 encoded bytes like this:
  93. * (6 bits pad)(2 bits pad | 4 bits data)(6 bits data)(6 bits data)
  94. * So 1 byte of pre-encoding padding results in 1 full byte of encoded
  95. * padding.
  96. * If we added 2 bytes of padding prior to encoding this gets encoded
  97. * as:
  98. * (6 bits pad)(6 bits pad)(4 bits pad | 2 bits data)(6 bits data)
  99. * So 2 bytes of pre-encoding padding results in 2 full bytes of encoded
  100. * padding, i.e. we have to strip the same number of bytes of padding
  101. * from the encoded data as we added to the pre-encoded data.
  102. */
  103. memmove(a, a + padsize, outl - padsize);
  104. outl -= padsize;
  105. }
  106. err:
  107. EVP_ENCODE_CTX_free(ctx);
  108. return outl;
  109. }
  110. /*
  111. * Convert a raw byte string into a null-terminated base64 ASCII string.
  112. * Returns 1 on success or 0 on error.
  113. */
  114. static int t_tob64(char *dst, const unsigned char *src, int size)
  115. {
  116. EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
  117. int outl = 0, outl2 = 0;
  118. unsigned char pad[2] = {0, 0};
  119. size_t leadz = 0;
  120. if (ctx == NULL)
  121. return 0;
  122. EVP_EncodeInit(ctx);
  123. evp_encode_ctx_set_flags(ctx, EVP_ENCODE_CTX_NO_NEWLINES
  124. | EVP_ENCODE_CTX_USE_SRP_ALPHABET);
  125. /*
  126. * We pad at the front with zero bytes until the length is a multiple of 3
  127. * so that EVP_EncodeUpdate/EVP_EncodeFinal does not add any of its own "="
  128. * padding
  129. */
  130. leadz = 3 - (size % 3);
  131. if (leadz != 3
  132. && !EVP_EncodeUpdate(ctx, (unsigned char *)dst, &outl, pad,
  133. leadz)) {
  134. EVP_ENCODE_CTX_free(ctx);
  135. return 0;
  136. }
  137. if (!EVP_EncodeUpdate(ctx, (unsigned char *)dst + outl, &outl2, src,
  138. size)) {
  139. EVP_ENCODE_CTX_free(ctx);
  140. return 0;
  141. }
  142. outl += outl2;
  143. EVP_EncodeFinal(ctx, (unsigned char *)dst + outl, &outl2);
  144. outl += outl2;
  145. /* Strip the encoded padding at the front */
  146. if (leadz != 3) {
  147. memmove(dst, dst + leadz, outl - leadz);
  148. dst[outl - leadz] = '\0';
  149. }
  150. EVP_ENCODE_CTX_free(ctx);
  151. return 1;
  152. }
  153. void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
  154. {
  155. if (user_pwd == NULL)
  156. return;
  157. BN_free(user_pwd->s);
  158. BN_clear_free(user_pwd->v);
  159. OPENSSL_free(user_pwd->id);
  160. OPENSSL_free(user_pwd->info);
  161. OPENSSL_free(user_pwd);
  162. }
  163. static SRP_user_pwd *SRP_user_pwd_new(void)
  164. {
  165. SRP_user_pwd *ret;
  166. if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
  167. /* SRPerr(SRP_F_SRP_USER_PWD_NEW, ERR_R_MALLOC_FAILURE); */ /*ckerr_ignore*/
  168. return NULL;
  169. }
  170. ret->N = NULL;
  171. ret->g = NULL;
  172. ret->s = NULL;
  173. ret->v = NULL;
  174. ret->id = NULL;
  175. ret->info = NULL;
  176. return ret;
  177. }
  178. static void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
  179. const BIGNUM *N)
  180. {
  181. vinfo->N = N;
  182. vinfo->g = g;
  183. }
  184. static int SRP_user_pwd_set_ids(SRP_user_pwd *vinfo, const char *id,
  185. const char *info)
  186. {
  187. if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id)))
  188. return 0;
  189. return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info)));
  190. }
  191. static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
  192. const char *v)
  193. {
  194. unsigned char tmp[MAX_LEN];
  195. int len;
  196. vinfo->v = NULL;
  197. vinfo->s = NULL;
  198. len = t_fromb64(tmp, sizeof(tmp), v);
  199. if (len < 0)
  200. return 0;
  201. if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
  202. return 0;
  203. len = t_fromb64(tmp, sizeof(tmp), s);
  204. if (len < 0)
  205. goto err;
  206. vinfo->s = BN_bin2bn(tmp, len, NULL);
  207. if (vinfo->s == NULL)
  208. goto err;
  209. return 1;
  210. err:
  211. BN_free(vinfo->v);
  212. vinfo->v = NULL;
  213. return 0;
  214. }
  215. static int SRP_user_pwd_set_sv_BN(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
  216. {
  217. vinfo->v = v;
  218. vinfo->s = s;
  219. return (vinfo->s != NULL && vinfo->v != NULL);
  220. }
  221. static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
  222. {
  223. SRP_user_pwd *ret;
  224. if (src == NULL)
  225. return NULL;
  226. if ((ret = SRP_user_pwd_new()) == NULL)
  227. return NULL;
  228. SRP_user_pwd_set_gN(ret, src->g, src->N);
  229. if (!SRP_user_pwd_set_ids(ret, src->id, src->info)
  230. || !SRP_user_pwd_set_sv_BN(ret, BN_dup(src->s), BN_dup(src->v))) {
  231. SRP_user_pwd_free(ret);
  232. return NULL;
  233. }
  234. return ret;
  235. }
  236. SRP_VBASE *SRP_VBASE_new(char *seed_key)
  237. {
  238. SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb));
  239. if (vb == NULL)
  240. return NULL;
  241. if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL
  242. || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) {
  243. OPENSSL_free(vb);
  244. return NULL;
  245. }
  246. vb->default_g = NULL;
  247. vb->default_N = NULL;
  248. vb->seed_key = NULL;
  249. if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) {
  250. sk_SRP_user_pwd_free(vb->users_pwd);
  251. sk_SRP_gN_cache_free(vb->gN_cache);
  252. OPENSSL_free(vb);
  253. return NULL;
  254. }
  255. return vb;
  256. }
  257. void SRP_VBASE_free(SRP_VBASE *vb)
  258. {
  259. if (!vb)
  260. return;
  261. sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
  262. sk_SRP_gN_cache_free(vb->gN_cache);
  263. OPENSSL_free(vb->seed_key);
  264. OPENSSL_free(vb);
  265. }
  266. static SRP_gN_cache *SRP_gN_new_init(const char *ch)
  267. {
  268. unsigned char tmp[MAX_LEN];
  269. int len;
  270. SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN));
  271. if (newgN == NULL)
  272. return NULL;
  273. len = t_fromb64(tmp, sizeof(tmp), ch);
  274. if (len < 0)
  275. goto err;
  276. if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL)
  277. goto err;
  278. if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
  279. return newgN;
  280. OPENSSL_free(newgN->b64_bn);
  281. err:
  282. OPENSSL_free(newgN);
  283. return NULL;
  284. }
  285. static void SRP_gN_free(SRP_gN_cache *gN_cache)
  286. {
  287. if (gN_cache == NULL)
  288. return;
  289. OPENSSL_free(gN_cache->b64_bn);
  290. BN_free(gN_cache->bn);
  291. OPENSSL_free(gN_cache);
  292. }
  293. static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
  294. {
  295. int i;
  296. SRP_gN *gN;
  297. if (gN_tab != NULL)
  298. for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
  299. gN = sk_SRP_gN_value(gN_tab, i);
  300. if (gN && (id == NULL || strcmp(gN->id, id) == 0))
  301. return gN;
  302. }
  303. return SRP_get_default_gN(id);
  304. }
  305. static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
  306. {
  307. int i;
  308. if (gN_cache == NULL)
  309. return NULL;
  310. /* search if we have already one... */
  311. for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
  312. SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
  313. if (strcmp(cache->b64_bn, ch) == 0)
  314. return cache->bn;
  315. }
  316. { /* it is the first time that we find it */
  317. SRP_gN_cache *newgN = SRP_gN_new_init(ch);
  318. if (newgN) {
  319. if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
  320. return newgN->bn;
  321. SRP_gN_free(newgN);
  322. }
  323. }
  324. return NULL;
  325. }
  326. /*
  327. * this function parses verifier file. Format is:
  328. * string(index):base64(N):base64(g):0
  329. * string(username):base64(v):base64(salt):int(index)
  330. */
  331. int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
  332. {
  333. int error_code;
  334. STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
  335. char *last_index = NULL;
  336. int i;
  337. char **pp;
  338. SRP_gN *gN = NULL;
  339. SRP_user_pwd *user_pwd = NULL;
  340. TXT_DB *tmpdb = NULL;
  341. BIO *in = BIO_new(BIO_s_file());
  342. error_code = SRP_ERR_OPEN_FILE;
  343. if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
  344. goto err;
  345. error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
  346. if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
  347. goto err;
  348. error_code = SRP_ERR_MEMORY;
  349. if (vb->seed_key) {
  350. last_index = SRP_get_default_gN(NULL)->id;
  351. }
  352. for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
  353. pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
  354. if (pp[DB_srptype][0] == DB_SRP_INDEX) {
  355. /*
  356. * we add this couple in the internal Stack
  357. */
  358. if ((gN = OPENSSL_malloc(sizeof(*gN))) == NULL)
  359. goto err;
  360. if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL
  361. || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
  362. == NULL
  363. || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
  364. == NULL
  365. || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
  366. goto err;
  367. gN = NULL;
  368. if (vb->seed_key != NULL) {
  369. last_index = pp[DB_srpid];
  370. }
  371. } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
  372. /* it is a user .... */
  373. const SRP_gN *lgN;
  374. if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
  375. error_code = SRP_ERR_MEMORY;
  376. if ((user_pwd = SRP_user_pwd_new()) == NULL)
  377. goto err;
  378. SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
  379. if (!SRP_user_pwd_set_ids
  380. (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
  381. goto err;
  382. error_code = SRP_ERR_VBASE_BN_LIB;
  383. if (!SRP_user_pwd_set_sv
  384. (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
  385. goto err;
  386. if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
  387. goto err;
  388. user_pwd = NULL; /* abandon responsibility */
  389. }
  390. }
  391. }
  392. if (last_index != NULL) {
  393. /* this means that we want to simulate a default user */
  394. if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
  395. error_code = SRP_ERR_VBASE_BN_LIB;
  396. goto err;
  397. }
  398. vb->default_g = gN->g;
  399. vb->default_N = gN->N;
  400. gN = NULL;
  401. }
  402. error_code = SRP_NO_ERROR;
  403. err:
  404. /*
  405. * there may be still some leaks to fix, if this fails, the application
  406. * terminates most likely
  407. */
  408. if (gN != NULL) {
  409. OPENSSL_free(gN->id);
  410. OPENSSL_free(gN);
  411. }
  412. SRP_user_pwd_free(user_pwd);
  413. TXT_DB_free(tmpdb);
  414. BIO_free_all(in);
  415. sk_SRP_gN_free(SRP_gN_tab);
  416. return error_code;
  417. }
  418. static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
  419. {
  420. int i;
  421. SRP_user_pwd *user;
  422. if (vb == NULL)
  423. return NULL;
  424. for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
  425. user = sk_SRP_user_pwd_value(vb->users_pwd, i);
  426. if (strcmp(user->id, username) == 0)
  427. return user;
  428. }
  429. return NULL;
  430. }
  431. # if OPENSSL_API_COMPAT < 0x10100000L
  432. /*
  433. * DEPRECATED: use SRP_VBASE_get1_by_user instead.
  434. * This method ignores the configured seed and fails for an unknown user.
  435. * Ownership of the returned pointer is not released to the caller.
  436. * In other words, caller must not free the result.
  437. */
  438. SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
  439. {
  440. return find_user(vb, username);
  441. }
  442. # endif
  443. /*
  444. * Ownership of the returned pointer is released to the caller.
  445. * In other words, caller must free the result once done.
  446. */
  447. SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
  448. {
  449. SRP_user_pwd *user;
  450. unsigned char digv[SHA_DIGEST_LENGTH];
  451. unsigned char digs[SHA_DIGEST_LENGTH];
  452. EVP_MD_CTX *ctxt = NULL;
  453. if (vb == NULL)
  454. return NULL;
  455. if ((user = find_user(vb, username)) != NULL)
  456. return srp_user_pwd_dup(user);
  457. if ((vb->seed_key == NULL) ||
  458. (vb->default_g == NULL) || (vb->default_N == NULL))
  459. return NULL;
  460. /* if the user is unknown we set parameters as well if we have a seed_key */
  461. if ((user = SRP_user_pwd_new()) == NULL)
  462. return NULL;
  463. SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
  464. if (!SRP_user_pwd_set_ids(user, username, NULL))
  465. goto err;
  466. if (RAND_priv_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
  467. goto err;
  468. ctxt = EVP_MD_CTX_new();
  469. if (ctxt == NULL
  470. || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
  471. || !EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key))
  472. || !EVP_DigestUpdate(ctxt, username, strlen(username))
  473. || !EVP_DigestFinal_ex(ctxt, digs, NULL))
  474. goto err;
  475. EVP_MD_CTX_free(ctxt);
  476. ctxt = NULL;
  477. if (SRP_user_pwd_set_sv_BN(user,
  478. BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
  479. BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
  480. return user;
  481. err:
  482. EVP_MD_CTX_free(ctxt);
  483. SRP_user_pwd_free(user);
  484. return NULL;
  485. }
  486. /*
  487. * create a verifier (*salt,*verifier,g and N are in base64)
  488. */
  489. char *SRP_create_verifier(const char *user, const char *pass, char **salt,
  490. char **verifier, const char *N, const char *g)
  491. {
  492. int len;
  493. char *result = NULL, *vf = NULL;
  494. const BIGNUM *N_bn = NULL, *g_bn = NULL;
  495. BIGNUM *N_bn_alloc = NULL, *g_bn_alloc = NULL, *s = NULL, *v = NULL;
  496. unsigned char tmp[MAX_LEN];
  497. unsigned char tmp2[MAX_LEN];
  498. char *defgNid = NULL;
  499. int vfsize = 0;
  500. if ((user == NULL) ||
  501. (pass == NULL) || (salt == NULL) || (verifier == NULL))
  502. goto err;
  503. if (N) {
  504. if ((len = t_fromb64(tmp, sizeof(tmp), N)) <= 0)
  505. goto err;
  506. N_bn_alloc = BN_bin2bn(tmp, len, NULL);
  507. if (N_bn_alloc == NULL)
  508. goto err;
  509. N_bn = N_bn_alloc;
  510. if ((len = t_fromb64(tmp, sizeof(tmp) ,g)) <= 0)
  511. goto err;
  512. g_bn_alloc = BN_bin2bn(tmp, len, NULL);
  513. if (g_bn_alloc == NULL)
  514. goto err;
  515. g_bn = g_bn_alloc;
  516. defgNid = "*";
  517. } else {
  518. SRP_gN *gN = SRP_get_gN_by_id(g, NULL);
  519. if (gN == NULL)
  520. goto err;
  521. N_bn = gN->N;
  522. g_bn = gN->g;
  523. defgNid = gN->id;
  524. }
  525. if (*salt == NULL) {
  526. if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
  527. goto err;
  528. s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
  529. } else {
  530. if ((len = t_fromb64(tmp2, sizeof(tmp2), *salt)) <= 0)
  531. goto err;
  532. s = BN_bin2bn(tmp2, len, NULL);
  533. }
  534. if (s == NULL)
  535. goto err;
  536. if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))
  537. goto err;
  538. if (BN_bn2bin(v, tmp) < 0)
  539. goto err;
  540. vfsize = BN_num_bytes(v) * 2;
  541. if (((vf = OPENSSL_malloc(vfsize)) == NULL))
  542. goto err;
  543. if (!t_tob64(vf, tmp, BN_num_bytes(v)))
  544. goto err;
  545. if (*salt == NULL) {
  546. char *tmp_salt;
  547. if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
  548. goto err;
  549. }
  550. if (!t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN)) {
  551. OPENSSL_free(tmp_salt);
  552. goto err;
  553. }
  554. *salt = tmp_salt;
  555. }
  556. *verifier = vf;
  557. vf = NULL;
  558. result = defgNid;
  559. err:
  560. BN_free(N_bn_alloc);
  561. BN_free(g_bn_alloc);
  562. OPENSSL_clear_free(vf, vfsize);
  563. BN_clear_free(s);
  564. BN_clear_free(v);
  565. return result;
  566. }
  567. /*
  568. * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
  569. * then the provided salt will be used. On successful exit *verifier will point
  570. * to a newly allocated BIGNUM containing the verifier and (if a salt was not
  571. * provided) *salt will be populated with a newly allocated BIGNUM containing a
  572. * random salt.
  573. * The caller is responsible for freeing the allocated *salt and *verifier
  574. * BIGNUMS.
  575. */
  576. int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
  577. BIGNUM **verifier, const BIGNUM *N,
  578. const BIGNUM *g)
  579. {
  580. int result = 0;
  581. BIGNUM *x = NULL;
  582. BN_CTX *bn_ctx = BN_CTX_new();
  583. unsigned char tmp2[MAX_LEN];
  584. BIGNUM *salttmp = NULL, *verif;
  585. if ((user == NULL) ||
  586. (pass == NULL) ||
  587. (salt == NULL) ||
  588. (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
  589. goto err;
  590. if (*salt == NULL) {
  591. if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
  592. goto err;
  593. salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
  594. if (salttmp == NULL)
  595. goto err;
  596. } else {
  597. salttmp = *salt;
  598. }
  599. x = SRP_Calc_x(salttmp, user, pass);
  600. if (x == NULL)
  601. goto err;
  602. verif = BN_new();
  603. if (verif == NULL)
  604. goto err;
  605. if (!BN_mod_exp(verif, g, x, N, bn_ctx)) {
  606. BN_clear_free(verif);
  607. goto err;
  608. }
  609. result = 1;
  610. *salt = salttmp;
  611. *verifier = verif;
  612. err:
  613. if (salt != NULL && *salt != salttmp)
  614. BN_clear_free(salttmp);
  615. BN_clear_free(x);
  616. BN_CTX_free(bn_ctx);
  617. return result;
  618. }
  619. #endif