srp_vfy.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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, size_t alen, const char *src)
  80. {
  81. char *loc;
  82. int i, j;
  83. int size;
  84. if (alen == 0 || alen > INT_MAX)
  85. return -1;
  86. while (*src && (*src == ' ' || *src == '\t' || *src == '\n'))
  87. ++src;
  88. size = strlen(src);
  89. if (size < 0 || size >= (int)alen)
  90. return -1;
  91. i = 0;
  92. while (i < size) {
  93. loc = strchr(b64table, src[i]);
  94. if (loc == (char *)0)
  95. break;
  96. else
  97. a[i] = loc - b64table;
  98. ++i;
  99. }
  100. /* if nothing valid to process we have a zero length response */
  101. if (i == 0)
  102. return 0;
  103. size = i;
  104. i = size - 1;
  105. j = size;
  106. while (1) {
  107. a[j] = a[i];
  108. if (--i < 0)
  109. break;
  110. a[j] |= (a[i] & 3) << 6;
  111. --j;
  112. a[j] = (unsigned char)((a[i] & 0x3c) >> 2);
  113. if (--i < 0)
  114. break;
  115. a[j] |= (a[i] & 0xf) << 4;
  116. --j;
  117. a[j] = (unsigned char)((a[i] & 0x30) >> 4);
  118. if (--i < 0)
  119. break;
  120. a[j] |= (a[i] << 2);
  121. a[--j] = 0;
  122. if (--i < 0)
  123. break;
  124. }
  125. while (j <= size && a[j] == 0)
  126. ++j;
  127. i = 0;
  128. while (j <= size)
  129. a[i++] = a[j++];
  130. return i;
  131. }
  132. /*
  133. * Convert a raw byte string into a null-terminated base64 ASCII string.
  134. */
  135. static char *t_tob64(char *dst, const unsigned char *src, int size)
  136. {
  137. int c, pos = size % 3;
  138. unsigned char b0 = 0, b1 = 0, b2 = 0, notleading = 0;
  139. char *olddst = dst;
  140. switch (pos) {
  141. case 1:
  142. b2 = src[0];
  143. break;
  144. case 2:
  145. b1 = src[0];
  146. b2 = src[1];
  147. break;
  148. }
  149. while (1) {
  150. c = (b0 & 0xfc) >> 2;
  151. if (notleading || c != 0) {
  152. *dst++ = b64table[c];
  153. notleading = 1;
  154. }
  155. c = ((b0 & 3) << 4) | ((b1 & 0xf0) >> 4);
  156. if (notleading || c != 0) {
  157. *dst++ = b64table[c];
  158. notleading = 1;
  159. }
  160. c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >> 6);
  161. if (notleading || c != 0) {
  162. *dst++ = b64table[c];
  163. notleading = 1;
  164. }
  165. c = b2 & 0x3f;
  166. if (notleading || c != 0) {
  167. *dst++ = b64table[c];
  168. notleading = 1;
  169. }
  170. if (pos >= size)
  171. break;
  172. else {
  173. b0 = src[pos++];
  174. b1 = src[pos++];
  175. b2 = src[pos++];
  176. }
  177. }
  178. *dst++ = '\0';
  179. return olddst;
  180. }
  181. void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
  182. {
  183. if (user_pwd == NULL)
  184. return;
  185. BN_free(user_pwd->s);
  186. BN_clear_free(user_pwd->v);
  187. OPENSSL_free(user_pwd->id);
  188. OPENSSL_free(user_pwd->info);
  189. OPENSSL_free(user_pwd);
  190. }
  191. static SRP_user_pwd *SRP_user_pwd_new()
  192. {
  193. SRP_user_pwd *ret = OPENSSL_malloc(sizeof(SRP_user_pwd));
  194. if (ret == NULL)
  195. return NULL;
  196. ret->N = NULL;
  197. ret->g = NULL;
  198. ret->s = NULL;
  199. ret->v = NULL;
  200. ret->id = NULL;
  201. ret->info = NULL;
  202. return ret;
  203. }
  204. static void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
  205. const BIGNUM *N)
  206. {
  207. vinfo->N = N;
  208. vinfo->g = g;
  209. }
  210. static int SRP_user_pwd_set_ids(SRP_user_pwd *vinfo, const char *id,
  211. const char *info)
  212. {
  213. if (id != NULL && NULL == (vinfo->id = BUF_strdup(id)))
  214. return 0;
  215. return (info == NULL || NULL != (vinfo->info = BUF_strdup(info)));
  216. }
  217. static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
  218. const char *v)
  219. {
  220. unsigned char tmp[MAX_LEN];
  221. int len;
  222. vinfo->v = NULL;
  223. vinfo->s = NULL;
  224. len = t_fromb64(tmp, sizeof(tmp), v);
  225. if (len < 0)
  226. return 0;
  227. if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
  228. return 0;
  229. len = t_fromb64(tmp, sizeof(tmp), s);
  230. if (len < 0)
  231. goto err;
  232. vinfo->s = BN_bin2bn(tmp, len, NULL);
  233. if (vinfo->s == NULL)
  234. goto err;
  235. return 1;
  236. err:
  237. BN_free(vinfo->v);
  238. vinfo->v = NULL;
  239. return 0;
  240. }
  241. static int SRP_user_pwd_set_sv_BN(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
  242. {
  243. vinfo->v = v;
  244. vinfo->s = s;
  245. return (vinfo->s != NULL && vinfo->v != NULL);
  246. }
  247. static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
  248. {
  249. SRP_user_pwd *ret;
  250. if (src == NULL)
  251. return NULL;
  252. if ((ret = SRP_user_pwd_new()) == NULL)
  253. return NULL;
  254. SRP_user_pwd_set_gN(ret, src->g, src->N);
  255. if (!SRP_user_pwd_set_ids(ret, src->id, src->info)
  256. || !SRP_user_pwd_set_sv_BN(ret, BN_dup(src->s), BN_dup(src->v))) {
  257. SRP_user_pwd_free(ret);
  258. return NULL;
  259. }
  260. return ret;
  261. }
  262. SRP_VBASE *SRP_VBASE_new(char *seed_key)
  263. {
  264. SRP_VBASE *vb = (SRP_VBASE *)OPENSSL_malloc(sizeof(SRP_VBASE));
  265. if (vb == NULL)
  266. return NULL;
  267. if (!(vb->users_pwd = sk_SRP_user_pwd_new_null()) ||
  268. !(vb->gN_cache = sk_SRP_gN_cache_new_null())) {
  269. OPENSSL_free(vb);
  270. return NULL;
  271. }
  272. vb->default_g = NULL;
  273. vb->default_N = NULL;
  274. vb->seed_key = NULL;
  275. if ((seed_key != NULL) && (vb->seed_key = BUF_strdup(seed_key)) == NULL) {
  276. sk_SRP_user_pwd_free(vb->users_pwd);
  277. sk_SRP_gN_cache_free(vb->gN_cache);
  278. OPENSSL_free(vb);
  279. return NULL;
  280. }
  281. return vb;
  282. }
  283. int SRP_VBASE_free(SRP_VBASE *vb)
  284. {
  285. sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
  286. sk_SRP_gN_cache_free(vb->gN_cache);
  287. OPENSSL_free(vb->seed_key);
  288. OPENSSL_free(vb);
  289. return 0;
  290. }
  291. static SRP_gN_cache *SRP_gN_new_init(const char *ch)
  292. {
  293. unsigned char tmp[MAX_LEN];
  294. int len;
  295. SRP_gN_cache *newgN =
  296. (SRP_gN_cache *)OPENSSL_malloc(sizeof(SRP_gN_cache));
  297. if (newgN == NULL)
  298. return NULL;
  299. len = t_fromb64(tmp, sizeof(tmp), ch);
  300. if (len < 0)
  301. goto err;
  302. if ((newgN->b64_bn = BUF_strdup(ch)) == NULL)
  303. goto err;
  304. if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
  305. return newgN;
  306. OPENSSL_free(newgN->b64_bn);
  307. err:
  308. OPENSSL_free(newgN);
  309. return NULL;
  310. }
  311. static void SRP_gN_free(SRP_gN_cache *gN_cache)
  312. {
  313. if (gN_cache == NULL)
  314. return;
  315. OPENSSL_free(gN_cache->b64_bn);
  316. BN_free(gN_cache->bn);
  317. OPENSSL_free(gN_cache);
  318. }
  319. static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
  320. {
  321. int i;
  322. SRP_gN *gN;
  323. if (gN_tab != NULL)
  324. for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
  325. gN = sk_SRP_gN_value(gN_tab, i);
  326. if (gN && (id == NULL || strcmp(gN->id, id) == 0))
  327. return gN;
  328. }
  329. return SRP_get_default_gN(id);
  330. }
  331. static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
  332. {
  333. int i;
  334. if (gN_cache == NULL)
  335. return NULL;
  336. /* search if we have already one... */
  337. for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
  338. SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
  339. if (strcmp(cache->b64_bn, ch) == 0)
  340. return cache->bn;
  341. }
  342. { /* it is the first time that we find it */
  343. SRP_gN_cache *newgN = SRP_gN_new_init(ch);
  344. if (newgN) {
  345. if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
  346. return newgN->bn;
  347. SRP_gN_free(newgN);
  348. }
  349. }
  350. return NULL;
  351. }
  352. /*
  353. * this function parses verifier file. Format is:
  354. * string(index):base64(N):base64(g):0
  355. * string(username):base64(v):base64(salt):int(index)
  356. */
  357. int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
  358. {
  359. int error_code;
  360. STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
  361. char *last_index = NULL;
  362. int i;
  363. char **pp;
  364. SRP_gN *gN = NULL;
  365. SRP_user_pwd *user_pwd = NULL;
  366. TXT_DB *tmpdb = NULL;
  367. BIO *in = BIO_new(BIO_s_file());
  368. error_code = SRP_ERR_OPEN_FILE;
  369. if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
  370. goto err;
  371. error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
  372. if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
  373. goto err;
  374. error_code = SRP_ERR_MEMORY;
  375. if (vb->seed_key) {
  376. last_index = SRP_get_default_gN(NULL)->id;
  377. }
  378. for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
  379. pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
  380. if (pp[DB_srptype][0] == DB_SRP_INDEX) {
  381. /*
  382. * we add this couple in the internal Stack
  383. */
  384. if ((gN = (SRP_gN *) OPENSSL_malloc(sizeof(SRP_gN))) == NULL)
  385. goto err;
  386. if (!(gN->id = BUF_strdup(pp[DB_srpid]))
  387. || !(gN->N =
  388. SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
  389. || !(gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
  390. || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
  391. goto err;
  392. gN = NULL;
  393. if (vb->seed_key != NULL) {
  394. last_index = pp[DB_srpid];
  395. }
  396. } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
  397. /* it is a user .... */
  398. SRP_gN *lgN;
  399. if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
  400. error_code = SRP_ERR_MEMORY;
  401. if ((user_pwd = SRP_user_pwd_new()) == NULL)
  402. goto err;
  403. SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
  404. if (!SRP_user_pwd_set_ids
  405. (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
  406. goto err;
  407. error_code = SRP_ERR_VBASE_BN_LIB;
  408. if (!SRP_user_pwd_set_sv
  409. (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
  410. goto err;
  411. if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
  412. goto err;
  413. user_pwd = NULL; /* abandon responsability */
  414. }
  415. }
  416. }
  417. if (last_index != NULL) {
  418. /* this means that we want to simulate a default user */
  419. if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
  420. error_code = SRP_ERR_VBASE_BN_LIB;
  421. goto err;
  422. }
  423. vb->default_g = gN->g;
  424. vb->default_N = gN->N;
  425. gN = NULL;
  426. }
  427. error_code = SRP_NO_ERROR;
  428. err:
  429. /*
  430. * there may be still some leaks to fix, if this fails, the application
  431. * terminates most likely
  432. */
  433. if (gN != NULL) {
  434. OPENSSL_free(gN->id);
  435. OPENSSL_free(gN);
  436. }
  437. SRP_user_pwd_free(user_pwd);
  438. if (tmpdb)
  439. TXT_DB_free(tmpdb);
  440. if (in)
  441. BIO_free_all(in);
  442. sk_SRP_gN_free(SRP_gN_tab);
  443. return error_code;
  444. }
  445. static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
  446. {
  447. int i;
  448. SRP_user_pwd *user;
  449. if (vb == NULL)
  450. return NULL;
  451. for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
  452. user = sk_SRP_user_pwd_value(vb->users_pwd, i);
  453. if (strcmp(user->id, username) == 0)
  454. return user;
  455. }
  456. return NULL;
  457. }
  458. /*
  459. * This method ignores the configured seed and fails for an unknown user.
  460. * Ownership of the returned pointer is not released to the caller.
  461. * In other words, caller must not free the result.
  462. */
  463. SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
  464. {
  465. return find_user(vb, username);
  466. }
  467. /*
  468. * Ownership of the returned pointer is released to the caller.
  469. * In other words, caller must free the result once done.
  470. */
  471. SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
  472. {
  473. SRP_user_pwd *user;
  474. unsigned char digv[SHA_DIGEST_LENGTH];
  475. unsigned char digs[SHA_DIGEST_LENGTH];
  476. EVP_MD_CTX ctxt;
  477. if (vb == NULL)
  478. return NULL;
  479. if ((user = find_user(vb, username)) != NULL)
  480. return srp_user_pwd_dup(user);
  481. if ((vb->seed_key == NULL) ||
  482. (vb->default_g == NULL) || (vb->default_N == NULL))
  483. return NULL;
  484. /* if the user is unknown we set parameters as well if we have a seed_key */
  485. if ((user = SRP_user_pwd_new()) == NULL)
  486. return NULL;
  487. SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
  488. if (!SRP_user_pwd_set_ids(user, username, NULL))
  489. goto err;
  490. if (RAND_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
  491. goto err;
  492. EVP_MD_CTX_init(&ctxt);
  493. EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
  494. EVP_DigestUpdate(&ctxt, vb->seed_key, strlen(vb->seed_key));
  495. EVP_DigestUpdate(&ctxt, username, strlen(username));
  496. EVP_DigestFinal_ex(&ctxt, digs, NULL);
  497. EVP_MD_CTX_cleanup(&ctxt);
  498. if (SRP_user_pwd_set_sv_BN
  499. (user, BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
  500. BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
  501. return user;
  502. err:SRP_user_pwd_free(user);
  503. return NULL;
  504. }
  505. /*
  506. * create a verifier (*salt,*verifier,g and N are in base64)
  507. */
  508. char *SRP_create_verifier(const char *user, const char *pass, char **salt,
  509. char **verifier, const char *N, const char *g)
  510. {
  511. int len;
  512. char *result = NULL, *vf = NULL;
  513. BIGNUM *N_bn = NULL, *g_bn = NULL, *s = NULL, *v = NULL;
  514. unsigned char tmp[MAX_LEN];
  515. unsigned char tmp2[MAX_LEN];
  516. char *defgNid = NULL;
  517. int vfsize = 0;
  518. if ((user == NULL) ||
  519. (pass == NULL) || (salt == NULL) || (verifier == NULL))
  520. goto err;
  521. if (N) {
  522. if (!(len = t_fromb64(tmp, sizeof(tmp), N)))
  523. goto err;
  524. N_bn = BN_bin2bn(tmp, len, NULL);
  525. if (!(len = t_fromb64(tmp, sizeof(tmp), g)))
  526. goto err;
  527. g_bn = BN_bin2bn(tmp, len, NULL);
  528. defgNid = "*";
  529. } else {
  530. SRP_gN *gN = SRP_get_gN_by_id(g, NULL);
  531. if (gN == NULL)
  532. goto err;
  533. N_bn = gN->N;
  534. g_bn = gN->g;
  535. defgNid = gN->id;
  536. }
  537. if (*salt == NULL) {
  538. if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
  539. goto err;
  540. s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
  541. } else {
  542. if (!(len = t_fromb64(tmp2, sizeof(tmp2), *salt)))
  543. goto err;
  544. s = BN_bin2bn(tmp2, len, NULL);
  545. }
  546. if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))
  547. goto err;
  548. BN_bn2bin(v, tmp);
  549. vfsize = BN_num_bytes(v) * 2;
  550. if (((vf = OPENSSL_malloc(vfsize)) == NULL))
  551. goto err;
  552. t_tob64(vf, tmp, BN_num_bytes(v));
  553. if (*salt == NULL) {
  554. char *tmp_salt;
  555. if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
  556. goto err;
  557. }
  558. t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);
  559. *salt = tmp_salt;
  560. }
  561. *verifier = vf;
  562. vf = NULL;
  563. result = defgNid;
  564. err:
  565. if (N) {
  566. BN_free(N_bn);
  567. BN_free(g_bn);
  568. }
  569. if (vf != NULL)
  570. OPENSSL_cleanse(vf, vfsize);
  571. OPENSSL_free(vf);
  572. BN_clear_free(s);
  573. BN_clear_free(v);
  574. return result;
  575. }
  576. /*
  577. * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
  578. * then the provided salt will be used. On successful exit *verifier will point
  579. * to a newly allocated BIGNUM containing the verifier and (if a salt was not
  580. * provided) *salt will be populated with a newly allocated BIGNUM containing a
  581. * random salt.
  582. * The caller is responsible for freeing the allocated *salt and *verifier
  583. * BIGNUMS.
  584. */
  585. int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
  586. BIGNUM **verifier, BIGNUM *N, BIGNUM *g)
  587. {
  588. int result = 0;
  589. BIGNUM *x = NULL;
  590. BN_CTX *bn_ctx = BN_CTX_new();
  591. unsigned char tmp2[MAX_LEN];
  592. BIGNUM *salttmp = NULL;
  593. if ((user == NULL) ||
  594. (pass == NULL) ||
  595. (salt == NULL) ||
  596. (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
  597. goto err;
  598. srp_bn_print(N);
  599. srp_bn_print(g);
  600. if (*salt == NULL) {
  601. if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
  602. goto err;
  603. salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
  604. } else {
  605. salttmp = *salt;
  606. }
  607. x = SRP_Calc_x(salttmp, user, pass);
  608. *verifier = BN_new();
  609. if (*verifier == NULL)
  610. goto err;
  611. if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
  612. BN_clear_free(*verifier);
  613. goto err;
  614. }
  615. srp_bn_print(*verifier);
  616. result = 1;
  617. *salt = salttmp;
  618. err:
  619. if (*salt != salttmp)
  620. BN_clear_free(salttmp);
  621. BN_clear_free(x);
  622. BN_CTX_free(bn_ctx);
  623. return result;
  624. }
  625. #endif