srp_vfy.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. * Written by Christophe Renou (christophe.renou@edelweb.fr) with the
  3. * precious help of Peter Sylvester (peter.sylvester@edelweb.fr) for the
  4. * EdelKey project and contributed to the OpenSSL project 2004.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #ifndef OPENSSL_NO_SRP
  60. # include "internal/cryptlib.h"
  61. # include <openssl/sha.h>
  62. # include <openssl/srp.h>
  63. # include <openssl/evp.h>
  64. # include <openssl/buffer.h>
  65. # include <openssl/rand.h>
  66. # include <openssl/txt_db.h>
  67. # define SRP_RANDOM_SALT_LEN 20
  68. # define MAX_LEN 2500
  69. static char b64table[] =
  70. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
  71. /*
  72. * the following two conversion routines have been inspired by code from
  73. * Stanford
  74. */
  75. /*
  76. * Convert a base64 string into raw byte array representation.
  77. */
  78. static int t_fromb64(unsigned char *a, const char *src)
  79. {
  80. char *loc;
  81. int i, j;
  82. int size;
  83. while (*src && (*src == ' ' || *src == '\t' || *src == '\n'))
  84. ++src;
  85. size = strlen(src);
  86. i = 0;
  87. while (i < size) {
  88. loc = strchr(b64table, src[i]);
  89. if (loc == (char *)0)
  90. break;
  91. else
  92. a[i] = loc - b64table;
  93. ++i;
  94. }
  95. /* if nothing valid to process we have a zero length response */
  96. if (i == 0)
  97. return 0;
  98. size = i;
  99. i = size - 1;
  100. j = size;
  101. while (1) {
  102. a[j] = a[i];
  103. if (--i < 0)
  104. break;
  105. a[j] |= (a[i] & 3) << 6;
  106. --j;
  107. a[j] = (unsigned char)((a[i] & 0x3c) >> 2);
  108. if (--i < 0)
  109. break;
  110. a[j] |= (a[i] & 0xf) << 4;
  111. --j;
  112. a[j] = (unsigned char)((a[i] & 0x30) >> 4);
  113. if (--i < 0)
  114. break;
  115. a[j] |= (a[i] << 2);
  116. a[--j] = 0;
  117. if (--i < 0)
  118. break;
  119. }
  120. while (a[j] == 0 && j <= size)
  121. ++j;
  122. i = 0;
  123. while (j <= size)
  124. a[i++] = a[j++];
  125. return i;
  126. }
  127. /*
  128. * Convert a raw byte string into a null-terminated base64 ASCII string.
  129. */
  130. static char *t_tob64(char *dst, const unsigned char *src, int size)
  131. {
  132. int c, pos = size % 3;
  133. unsigned char b0 = 0, b1 = 0, b2 = 0, notleading = 0;
  134. char *olddst = dst;
  135. switch (pos) {
  136. case 1:
  137. b2 = src[0];
  138. break;
  139. case 2:
  140. b1 = src[0];
  141. b2 = src[1];
  142. break;
  143. }
  144. while (1) {
  145. c = (b0 & 0xfc) >> 2;
  146. if (notleading || c != 0) {
  147. *dst++ = b64table[c];
  148. notleading = 1;
  149. }
  150. c = ((b0 & 3) << 4) | ((b1 & 0xf0) >> 4);
  151. if (notleading || c != 0) {
  152. *dst++ = b64table[c];
  153. notleading = 1;
  154. }
  155. c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >> 6);
  156. if (notleading || c != 0) {
  157. *dst++ = b64table[c];
  158. notleading = 1;
  159. }
  160. c = b2 & 0x3f;
  161. if (notleading || c != 0) {
  162. *dst++ = b64table[c];
  163. notleading = 1;
  164. }
  165. if (pos >= size)
  166. break;
  167. else {
  168. b0 = src[pos++];
  169. b1 = src[pos++];
  170. b2 = src[pos++];
  171. }
  172. }
  173. *dst++ = '\0';
  174. return olddst;
  175. }
  176. static void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
  177. {
  178. if (user_pwd == NULL)
  179. return;
  180. BN_free(user_pwd->s);
  181. BN_clear_free(user_pwd->v);
  182. OPENSSL_free(user_pwd->id);
  183. OPENSSL_free(user_pwd->info);
  184. OPENSSL_free(user_pwd);
  185. }
  186. static SRP_user_pwd *SRP_user_pwd_new(void)
  187. {
  188. SRP_user_pwd *ret = OPENSSL_malloc(sizeof(*ret));
  189. if (ret == NULL)
  190. return NULL;
  191. ret->N = NULL;
  192. ret->g = NULL;
  193. ret->s = NULL;
  194. ret->v = NULL;
  195. ret->id = NULL;
  196. ret->info = NULL;
  197. return ret;
  198. }
  199. static void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
  200. const BIGNUM *N)
  201. {
  202. vinfo->N = N;
  203. vinfo->g = g;
  204. }
  205. static int SRP_user_pwd_set_ids(SRP_user_pwd *vinfo, const char *id,
  206. const char *info)
  207. {
  208. if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id)))
  209. return 0;
  210. return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info)));
  211. }
  212. static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
  213. const char *v)
  214. {
  215. unsigned char tmp[MAX_LEN];
  216. int len;
  217. if (strlen(s) > MAX_LEN || strlen(v) > MAX_LEN)
  218. return 0;
  219. len = t_fromb64(tmp, v);
  220. if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
  221. return 0;
  222. len = t_fromb64(tmp, s);
  223. return ((vinfo->s = BN_bin2bn(tmp, len, NULL)) != NULL);
  224. }
  225. static int SRP_user_pwd_set_sv_BN(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
  226. {
  227. vinfo->v = v;
  228. vinfo->s = s;
  229. return (vinfo->s != NULL && vinfo->v != NULL);
  230. }
  231. SRP_VBASE *SRP_VBASE_new(char *seed_key)
  232. {
  233. SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb));
  234. if (vb == NULL)
  235. return NULL;
  236. if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL
  237. || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) {
  238. OPENSSL_free(vb);
  239. return NULL;
  240. }
  241. vb->default_g = NULL;
  242. vb->default_N = NULL;
  243. vb->seed_key = NULL;
  244. if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) {
  245. sk_SRP_user_pwd_free(vb->users_pwd);
  246. sk_SRP_gN_cache_free(vb->gN_cache);
  247. OPENSSL_free(vb);
  248. return NULL;
  249. }
  250. return vb;
  251. }
  252. void SRP_VBASE_free(SRP_VBASE *vb)
  253. {
  254. if (!vb)
  255. return;
  256. sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
  257. sk_SRP_gN_cache_free(vb->gN_cache);
  258. OPENSSL_free(vb->seed_key);
  259. OPENSSL_free(vb);
  260. }
  261. static SRP_gN_cache *SRP_gN_new_init(const char *ch)
  262. {
  263. unsigned char tmp[MAX_LEN];
  264. int len;
  265. SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN));
  266. if (newgN == NULL)
  267. return NULL;
  268. if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL)
  269. goto err;
  270. len = t_fromb64(tmp, ch);
  271. if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
  272. return newgN;
  273. OPENSSL_free(newgN->b64_bn);
  274. err:
  275. OPENSSL_free(newgN);
  276. return NULL;
  277. }
  278. static void SRP_gN_free(SRP_gN_cache *gN_cache)
  279. {
  280. if (gN_cache == NULL)
  281. return;
  282. OPENSSL_free(gN_cache->b64_bn);
  283. BN_free(gN_cache->bn);
  284. OPENSSL_free(gN_cache);
  285. }
  286. static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
  287. {
  288. int i;
  289. SRP_gN *gN;
  290. if (gN_tab != NULL)
  291. for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
  292. gN = sk_SRP_gN_value(gN_tab, i);
  293. if (gN && (id == NULL || strcmp(gN->id, id) == 0))
  294. return gN;
  295. }
  296. return SRP_get_default_gN(id);
  297. }
  298. static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
  299. {
  300. int i;
  301. if (gN_cache == NULL)
  302. return NULL;
  303. /* search if we have already one... */
  304. for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
  305. SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
  306. if (strcmp(cache->b64_bn, ch) == 0)
  307. return cache->bn;
  308. }
  309. { /* it is the first time that we find it */
  310. SRP_gN_cache *newgN = SRP_gN_new_init(ch);
  311. if (newgN) {
  312. if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
  313. return newgN->bn;
  314. SRP_gN_free(newgN);
  315. }
  316. }
  317. return NULL;
  318. }
  319. /*
  320. * this function parses verifier file. Format is:
  321. * string(index):base64(N):base64(g):0
  322. * string(username):base64(v):base64(salt):int(index)
  323. */
  324. int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
  325. {
  326. int error_code;
  327. STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
  328. char *last_index = NULL;
  329. int i;
  330. char **pp;
  331. SRP_gN *gN = NULL;
  332. SRP_user_pwd *user_pwd = NULL;
  333. TXT_DB *tmpdb = NULL;
  334. BIO *in = BIO_new(BIO_s_file());
  335. error_code = SRP_ERR_OPEN_FILE;
  336. if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
  337. goto err;
  338. error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
  339. if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
  340. goto err;
  341. error_code = SRP_ERR_MEMORY;
  342. if (vb->seed_key) {
  343. last_index = SRP_get_default_gN(NULL)->id;
  344. }
  345. for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
  346. pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
  347. if (pp[DB_srptype][0] == DB_SRP_INDEX) {
  348. /*
  349. * we add this couple in the internal Stack
  350. */
  351. if ((gN = OPENSSL_malloc(sizeof(*gN))) == NULL)
  352. goto err;
  353. if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL
  354. || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
  355. == NULL
  356. || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
  357. == NULL
  358. || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
  359. goto err;
  360. gN = NULL;
  361. if (vb->seed_key != NULL) {
  362. last_index = pp[DB_srpid];
  363. }
  364. } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
  365. /* it is a user .... */
  366. const SRP_gN *lgN;
  367. if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
  368. error_code = SRP_ERR_MEMORY;
  369. if ((user_pwd = SRP_user_pwd_new()) == NULL)
  370. goto err;
  371. SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
  372. if (!SRP_user_pwd_set_ids
  373. (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
  374. goto err;
  375. error_code = SRP_ERR_VBASE_BN_LIB;
  376. if (!SRP_user_pwd_set_sv
  377. (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
  378. goto err;
  379. if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
  380. goto err;
  381. user_pwd = NULL; /* abandon responsability */
  382. }
  383. }
  384. }
  385. if (last_index != NULL) {
  386. /* this means that we want to simulate a default user */
  387. if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
  388. error_code = SRP_ERR_VBASE_BN_LIB;
  389. goto err;
  390. }
  391. vb->default_g = gN->g;
  392. vb->default_N = gN->N;
  393. gN = NULL;
  394. }
  395. error_code = SRP_NO_ERROR;
  396. err:
  397. /*
  398. * there may be still some leaks to fix, if this fails, the application
  399. * terminates most likely
  400. */
  401. if (gN != NULL) {
  402. OPENSSL_free(gN->id);
  403. OPENSSL_free(gN);
  404. }
  405. SRP_user_pwd_free(user_pwd);
  406. TXT_DB_free(tmpdb);
  407. BIO_free_all(in);
  408. sk_SRP_gN_free(SRP_gN_tab);
  409. return error_code;
  410. }
  411. SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
  412. {
  413. int i;
  414. SRP_user_pwd *user;
  415. unsigned char digv[SHA_DIGEST_LENGTH];
  416. unsigned char digs[SHA_DIGEST_LENGTH];
  417. EVP_MD_CTX *ctxt = NULL;
  418. if (vb == NULL)
  419. return NULL;
  420. for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
  421. user = sk_SRP_user_pwd_value(vb->users_pwd, i);
  422. if (strcmp(user->id, username) == 0)
  423. return user;
  424. }
  425. if ((vb->seed_key == NULL) ||
  426. (vb->default_g == NULL) || (vb->default_N == NULL))
  427. return NULL;
  428. /* if the user is unknown we set parameters as well if we have a seed_key */
  429. if ((user = SRP_user_pwd_new()) == NULL)
  430. return NULL;
  431. SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
  432. if (!SRP_user_pwd_set_ids(user, username, NULL))
  433. goto err;
  434. if (RAND_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
  435. goto err;
  436. ctxt = EVP_MD_CTX_new();
  437. EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL);
  438. EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key));
  439. EVP_DigestUpdate(ctxt, username, strlen(username));
  440. EVP_DigestFinal_ex(ctxt, digs, NULL);
  441. EVP_MD_CTX_free(ctxt);
  442. ctxt = NULL;
  443. if (SRP_user_pwd_set_sv_BN(user,
  444. BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
  445. BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
  446. return user;
  447. err:
  448. EVP_MD_CTX_free(ctxt);
  449. SRP_user_pwd_free(user);
  450. return NULL;
  451. }
  452. /*
  453. * create a verifier (*salt,*verifier,g and N are in base64)
  454. */
  455. char *SRP_create_verifier(const char *user, const char *pass, char **salt,
  456. char **verifier, const char *N, const char *g)
  457. {
  458. int len;
  459. char *result = NULL, *vf = NULL;
  460. BIGNUM *N_bn = NULL, *g_bn = NULL, *s = NULL, *v = NULL;
  461. unsigned char tmp[MAX_LEN];
  462. unsigned char tmp2[MAX_LEN];
  463. char *defgNid = NULL;
  464. int vfsize = 0;
  465. if ((user == NULL) ||
  466. (pass == NULL) || (salt == NULL) || (verifier == NULL))
  467. goto err;
  468. if (N) {
  469. if ((len = t_fromb64(tmp, N)) == 0)
  470. goto err;
  471. N_bn = BN_bin2bn(tmp, len, NULL);
  472. if ((len = t_fromb64(tmp, g)) == 0)
  473. goto err;
  474. g_bn = BN_bin2bn(tmp, len, NULL);
  475. defgNid = "*";
  476. } else {
  477. SRP_gN *gN = SRP_get_gN_by_id(g, NULL);
  478. if (gN == NULL)
  479. goto err;
  480. N_bn = gN->N;
  481. g_bn = gN->g;
  482. defgNid = gN->id;
  483. }
  484. if (*salt == NULL) {
  485. if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
  486. goto err;
  487. s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
  488. } else {
  489. if ((len = t_fromb64(tmp2, *salt)) == 0)
  490. goto err;
  491. s = BN_bin2bn(tmp2, len, NULL);
  492. }
  493. if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))
  494. goto err;
  495. BN_bn2bin(v, tmp);
  496. vfsize = BN_num_bytes(v) * 2;
  497. if (((vf = OPENSSL_malloc(vfsize)) == NULL))
  498. goto err;
  499. t_tob64(vf, tmp, BN_num_bytes(v));
  500. if (*salt == NULL) {
  501. char *tmp_salt;
  502. if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
  503. goto err;
  504. }
  505. t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);
  506. *salt = tmp_salt;
  507. }
  508. *verifier = vf;
  509. vf = NULL;
  510. result = defgNid;
  511. err:
  512. if (N) {
  513. BN_free(N_bn);
  514. BN_free(g_bn);
  515. }
  516. OPENSSL_clear_free(vf, vfsize);
  517. BN_clear_free(s);
  518. BN_clear_free(v);
  519. return result;
  520. }
  521. /*
  522. * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
  523. * then the provided salt will be used. On successful exit *verifier will point
  524. * to a newly allocated BIGNUM containing the verifier and (if a salt was not
  525. * provided) *salt will be populated with a newly allocated BIGNUM containing a
  526. * random salt.
  527. * The caller is responsible for freeing the allocated *salt and *verifier
  528. * BIGNUMS.
  529. */
  530. int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
  531. BIGNUM **verifier, const BIGNUM *N,
  532. const BIGNUM *g)
  533. {
  534. int result = 0;
  535. BIGNUM *x = NULL;
  536. BN_CTX *bn_ctx = BN_CTX_new();
  537. unsigned char tmp2[MAX_LEN];
  538. BIGNUM *salttmp = NULL;
  539. if ((user == NULL) ||
  540. (pass == NULL) ||
  541. (salt == NULL) ||
  542. (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
  543. goto err;
  544. if (*salt == NULL) {
  545. if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
  546. goto err;
  547. salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
  548. } else {
  549. salttmp = *salt;
  550. }
  551. x = SRP_Calc_x(salttmp, user, pass);
  552. *verifier = BN_new();
  553. if (*verifier == NULL)
  554. goto err;
  555. if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
  556. BN_clear_free(*verifier);
  557. goto err;
  558. }
  559. result = 1;
  560. *salt = salttmp;
  561. err:
  562. if (salt != NULL && *salt != salttmp)
  563. BN_clear_free(salttmp);
  564. BN_clear_free(x);
  565. BN_CTX_free(bn_ctx);
  566. return result;
  567. }
  568. #endif