srp_vfy.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (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 "internal/evp_int.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. 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. 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. int SRP_user_pwd_set1_ids(SRP_user_pwd *vinfo, const char *id,
  185. const char *info)
  186. {
  187. OPENSSL_free(vinfo->id);
  188. OPENSSL_free(vinfo->info);
  189. if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id)))
  190. return 0;
  191. return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info)));
  192. }
  193. static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
  194. const char *v)
  195. {
  196. unsigned char tmp[MAX_LEN];
  197. int len;
  198. vinfo->v = NULL;
  199. vinfo->s = NULL;
  200. len = t_fromb64(tmp, sizeof(tmp), v);
  201. if (len < 0)
  202. return 0;
  203. if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
  204. return 0;
  205. len = t_fromb64(tmp, sizeof(tmp), s);
  206. if (len < 0)
  207. goto err;
  208. vinfo->s = BN_bin2bn(tmp, len, NULL);
  209. if (vinfo->s == NULL)
  210. goto err;
  211. return 1;
  212. err:
  213. BN_free(vinfo->v);
  214. vinfo->v = NULL;
  215. return 0;
  216. }
  217. int SRP_user_pwd_set0_sv(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
  218. {
  219. BN_free(vinfo->s);
  220. BN_clear_free(vinfo->v);
  221. vinfo->v = v;
  222. vinfo->s = s;
  223. return (vinfo->s != NULL && vinfo->v != NULL);
  224. }
  225. static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
  226. {
  227. SRP_user_pwd *ret;
  228. if (src == NULL)
  229. return NULL;
  230. if ((ret = SRP_user_pwd_new()) == NULL)
  231. return NULL;
  232. SRP_user_pwd_set_gN(ret, src->g, src->N);
  233. if (!SRP_user_pwd_set1_ids(ret, src->id, src->info)
  234. || !SRP_user_pwd_set0_sv(ret, BN_dup(src->s), BN_dup(src->v))) {
  235. SRP_user_pwd_free(ret);
  236. return NULL;
  237. }
  238. return ret;
  239. }
  240. SRP_VBASE *SRP_VBASE_new(char *seed_key)
  241. {
  242. SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb));
  243. if (vb == NULL)
  244. return NULL;
  245. if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL
  246. || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) {
  247. OPENSSL_free(vb);
  248. return NULL;
  249. }
  250. vb->default_g = NULL;
  251. vb->default_N = NULL;
  252. vb->seed_key = NULL;
  253. if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) {
  254. sk_SRP_user_pwd_free(vb->users_pwd);
  255. sk_SRP_gN_cache_free(vb->gN_cache);
  256. OPENSSL_free(vb);
  257. return NULL;
  258. }
  259. return vb;
  260. }
  261. void SRP_VBASE_free(SRP_VBASE *vb)
  262. {
  263. if (!vb)
  264. return;
  265. sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
  266. sk_SRP_gN_cache_free(vb->gN_cache);
  267. OPENSSL_free(vb->seed_key);
  268. OPENSSL_free(vb);
  269. }
  270. static SRP_gN_cache *SRP_gN_new_init(const char *ch)
  271. {
  272. unsigned char tmp[MAX_LEN];
  273. int len;
  274. SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN));
  275. if (newgN == NULL)
  276. return NULL;
  277. len = t_fromb64(tmp, sizeof(tmp), ch);
  278. if (len < 0)
  279. goto err;
  280. if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL)
  281. goto err;
  282. if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
  283. return newgN;
  284. OPENSSL_free(newgN->b64_bn);
  285. err:
  286. OPENSSL_free(newgN);
  287. return NULL;
  288. }
  289. static void SRP_gN_free(SRP_gN_cache *gN_cache)
  290. {
  291. if (gN_cache == NULL)
  292. return;
  293. OPENSSL_free(gN_cache->b64_bn);
  294. BN_free(gN_cache->bn);
  295. OPENSSL_free(gN_cache);
  296. }
  297. static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
  298. {
  299. int i;
  300. SRP_gN *gN;
  301. if (gN_tab != NULL) {
  302. for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
  303. gN = sk_SRP_gN_value(gN_tab, i);
  304. if (gN && (id == NULL || strcmp(gN->id, id) == 0))
  305. return gN;
  306. }
  307. }
  308. return SRP_get_default_gN(id);
  309. }
  310. static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
  311. {
  312. int i;
  313. if (gN_cache == NULL)
  314. return NULL;
  315. /* search if we have already one... */
  316. for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
  317. SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
  318. if (strcmp(cache->b64_bn, ch) == 0)
  319. return cache->bn;
  320. }
  321. { /* it is the first time that we find it */
  322. SRP_gN_cache *newgN = SRP_gN_new_init(ch);
  323. if (newgN) {
  324. if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
  325. return newgN->bn;
  326. SRP_gN_free(newgN);
  327. }
  328. }
  329. return NULL;
  330. }
  331. /*
  332. * This function parses the verifier file generated by the srp app.
  333. * The format for each entry is:
  334. * V base64(verifier) base64(salt) username gNid userinfo(optional)
  335. * or
  336. * I base64(N) base64(g)
  337. * Note that base64 is the SRP variant of base64 encoding described
  338. * in t_fromb64().
  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 = OPENSSL_malloc(sizeof(*gN))) == NULL)
  368. goto err;
  369. if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL
  370. || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
  371. == NULL
  372. || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
  373. == NULL
  374. || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
  375. goto err;
  376. gN = NULL;
  377. if (vb->seed_key != NULL) {
  378. last_index = pp[DB_srpid];
  379. }
  380. } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
  381. /* it is a user .... */
  382. const SRP_gN *lgN;
  383. if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
  384. error_code = SRP_ERR_MEMORY;
  385. if ((user_pwd = SRP_user_pwd_new()) == NULL)
  386. goto err;
  387. SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
  388. if (!SRP_user_pwd_set1_ids
  389. (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
  390. goto err;
  391. error_code = SRP_ERR_VBASE_BN_LIB;
  392. if (!SRP_user_pwd_set_sv
  393. (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
  394. goto err;
  395. if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
  396. goto err;
  397. user_pwd = NULL; /* abandon responsibility */
  398. }
  399. }
  400. }
  401. if (last_index != NULL) {
  402. /* this means that we want to simulate a default user */
  403. if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
  404. error_code = SRP_ERR_VBASE_BN_LIB;
  405. goto err;
  406. }
  407. vb->default_g = gN->g;
  408. vb->default_N = gN->N;
  409. gN = NULL;
  410. }
  411. error_code = SRP_NO_ERROR;
  412. err:
  413. /*
  414. * there may be still some leaks to fix, if this fails, the application
  415. * terminates most likely
  416. */
  417. if (gN != NULL) {
  418. OPENSSL_free(gN->id);
  419. OPENSSL_free(gN);
  420. }
  421. SRP_user_pwd_free(user_pwd);
  422. TXT_DB_free(tmpdb);
  423. BIO_free_all(in);
  424. sk_SRP_gN_free(SRP_gN_tab);
  425. return error_code;
  426. }
  427. static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
  428. {
  429. int i;
  430. SRP_user_pwd *user;
  431. if (vb == NULL)
  432. return NULL;
  433. for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
  434. user = sk_SRP_user_pwd_value(vb->users_pwd, i);
  435. if (strcmp(user->id, username) == 0)
  436. return user;
  437. }
  438. return NULL;
  439. }
  440. int SRP_VBASE_add0_user(SRP_VBASE *vb, SRP_user_pwd *user_pwd)
  441. {
  442. if (sk_SRP_user_pwd_push(vb->users_pwd, user_pwd) <= 0)
  443. return 0;
  444. return 1;
  445. }
  446. # if !OPENSSL_API_1_1_0
  447. /*
  448. * DEPRECATED: use SRP_VBASE_get1_by_user instead.
  449. * This method ignores the configured seed and fails for an unknown user.
  450. * Ownership of the returned pointer is not released to the caller.
  451. * In other words, caller must not free the result.
  452. */
  453. SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
  454. {
  455. return find_user(vb, username);
  456. }
  457. # endif
  458. /*
  459. * Ownership of the returned pointer is released to the caller.
  460. * In other words, caller must free the result once done.
  461. */
  462. SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
  463. {
  464. SRP_user_pwd *user;
  465. unsigned char digv[SHA_DIGEST_LENGTH];
  466. unsigned char digs[SHA_DIGEST_LENGTH];
  467. EVP_MD_CTX *ctxt = NULL;
  468. if (vb == NULL)
  469. return NULL;
  470. if ((user = find_user(vb, username)) != NULL)
  471. return srp_user_pwd_dup(user);
  472. if ((vb->seed_key == NULL) ||
  473. (vb->default_g == NULL) || (vb->default_N == NULL))
  474. return NULL;
  475. /* if the user is unknown we set parameters as well if we have a seed_key */
  476. if ((user = SRP_user_pwd_new()) == NULL)
  477. return NULL;
  478. SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
  479. if (!SRP_user_pwd_set1_ids(user, username, NULL))
  480. goto err;
  481. if (RAND_priv_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
  482. goto err;
  483. ctxt = EVP_MD_CTX_new();
  484. if (ctxt == NULL
  485. || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
  486. || !EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key))
  487. || !EVP_DigestUpdate(ctxt, username, strlen(username))
  488. || !EVP_DigestFinal_ex(ctxt, digs, NULL))
  489. goto err;
  490. EVP_MD_CTX_free(ctxt);
  491. ctxt = NULL;
  492. if (SRP_user_pwd_set0_sv(user,
  493. BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
  494. BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
  495. return user;
  496. err:
  497. EVP_MD_CTX_free(ctxt);
  498. SRP_user_pwd_free(user);
  499. return NULL;
  500. }
  501. /*
  502. * create a verifier (*salt,*verifier,g and N are in base64)
  503. */
  504. char *SRP_create_verifier(const char *user, const char *pass, char **salt,
  505. char **verifier, const char *N, const char *g)
  506. {
  507. int len;
  508. char *result = NULL, *vf = NULL;
  509. const BIGNUM *N_bn = NULL, *g_bn = NULL;
  510. BIGNUM *N_bn_alloc = NULL, *g_bn_alloc = NULL, *s = NULL, *v = NULL;
  511. unsigned char tmp[MAX_LEN];
  512. unsigned char tmp2[MAX_LEN];
  513. char *defgNid = NULL;
  514. int vfsize = 0;
  515. if ((user == NULL) ||
  516. (pass == NULL) || (salt == NULL) || (verifier == NULL))
  517. goto err;
  518. if (N) {
  519. if ((len = t_fromb64(tmp, sizeof(tmp), N)) <= 0)
  520. goto err;
  521. N_bn_alloc = BN_bin2bn(tmp, len, NULL);
  522. if (N_bn_alloc == NULL)
  523. goto err;
  524. N_bn = N_bn_alloc;
  525. if ((len = t_fromb64(tmp, sizeof(tmp) ,g)) <= 0)
  526. goto err;
  527. g_bn_alloc = BN_bin2bn(tmp, len, NULL);
  528. if (g_bn_alloc == NULL)
  529. goto err;
  530. g_bn = g_bn_alloc;
  531. defgNid = "*";
  532. } else {
  533. SRP_gN *gN = SRP_get_default_gN(g);
  534. if (gN == NULL)
  535. goto err;
  536. N_bn = gN->N;
  537. g_bn = gN->g;
  538. defgNid = gN->id;
  539. }
  540. if (*salt == NULL) {
  541. if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
  542. goto err;
  543. s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
  544. } else {
  545. if ((len = t_fromb64(tmp2, sizeof(tmp2), *salt)) <= 0)
  546. goto err;
  547. s = BN_bin2bn(tmp2, len, NULL);
  548. }
  549. if (s == NULL)
  550. goto err;
  551. if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))
  552. goto err;
  553. if (BN_bn2bin(v, tmp) < 0)
  554. goto err;
  555. vfsize = BN_num_bytes(v) * 2;
  556. if (((vf = OPENSSL_malloc(vfsize)) == NULL))
  557. goto err;
  558. if (!t_tob64(vf, tmp, BN_num_bytes(v)))
  559. goto err;
  560. if (*salt == NULL) {
  561. char *tmp_salt;
  562. if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
  563. goto err;
  564. }
  565. if (!t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN)) {
  566. OPENSSL_free(tmp_salt);
  567. goto err;
  568. }
  569. *salt = tmp_salt;
  570. }
  571. *verifier = vf;
  572. vf = NULL;
  573. result = defgNid;
  574. err:
  575. BN_free(N_bn_alloc);
  576. BN_free(g_bn_alloc);
  577. OPENSSL_clear_free(vf, vfsize);
  578. BN_clear_free(s);
  579. BN_clear_free(v);
  580. return result;
  581. }
  582. /*
  583. * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
  584. * then the provided salt will be used. On successful exit *verifier will point
  585. * to a newly allocated BIGNUM containing the verifier and (if a salt was not
  586. * provided) *salt will be populated with a newly allocated BIGNUM containing a
  587. * random salt.
  588. * The caller is responsible for freeing the allocated *salt and *verifier
  589. * BIGNUMS.
  590. */
  591. int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
  592. BIGNUM **verifier, const BIGNUM *N,
  593. const BIGNUM *g)
  594. {
  595. int result = 0;
  596. BIGNUM *x = NULL;
  597. BN_CTX *bn_ctx = BN_CTX_new();
  598. unsigned char tmp2[MAX_LEN];
  599. BIGNUM *salttmp = NULL;
  600. if ((user == NULL) ||
  601. (pass == NULL) ||
  602. (salt == NULL) ||
  603. (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
  604. goto err;
  605. if (*salt == NULL) {
  606. if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
  607. goto err;
  608. salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
  609. if (salttmp == NULL)
  610. goto err;
  611. } else {
  612. salttmp = *salt;
  613. }
  614. x = SRP_Calc_x(salttmp, user, pass);
  615. if (x == NULL)
  616. goto err;
  617. *verifier = BN_new();
  618. if (*verifier == NULL)
  619. goto err;
  620. if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
  621. BN_clear_free(*verifier);
  622. goto err;
  623. }
  624. result = 1;
  625. *salt = salttmp;
  626. err:
  627. if (salt != NULL && *salt != salttmp)
  628. BN_clear_free(salttmp);
  629. BN_clear_free(x);
  630. BN_CTX_free(bn_ctx);
  631. return result;
  632. }
  633. #endif