srp_vfy.c 19 KB

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