srp_vfy.c 19 KB

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