srp_vfy.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * Copyright 2004-2020 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 "crypto/evp.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. DEFINE_STACK_OF(SRP_user_pwd)
  26. DEFINE_STACK_OF(SRP_gN_cache)
  27. DEFINE_STACK_OF(SRP_gN)
  28. /*
  29. * Note that SRP uses its own variant of base 64 encoding. A different base64
  30. * alphabet is used and no padding '=' characters are added. Instead we pad to
  31. * the front with 0 bytes and subsequently strip off leading encoded padding.
  32. * This variant is used for compatibility with other SRP implementations -
  33. * notably libsrp, but also others. It is also required for backwards
  34. * compatibility in order to load verifier files from other OpenSSL versions.
  35. */
  36. /*
  37. * Convert a base64 string into raw byte array representation.
  38. * Returns the length of the decoded data, or -1 on error.
  39. */
  40. static int t_fromb64(unsigned char *a, size_t alen, const char *src)
  41. {
  42. EVP_ENCODE_CTX *ctx;
  43. int outl = 0, outl2 = 0;
  44. size_t size, padsize;
  45. const unsigned char *pad = (const unsigned char *)"00";
  46. while (*src == ' ' || *src == '\t' || *src == '\n')
  47. ++src;
  48. size = strlen(src);
  49. padsize = 4 - (size & 3);
  50. padsize &= 3;
  51. /* Four bytes in src become three bytes output. */
  52. if (size > INT_MAX || ((size + padsize) / 4) * 3 > alen)
  53. return -1;
  54. ctx = EVP_ENCODE_CTX_new();
  55. if (ctx == NULL)
  56. return -1;
  57. /*
  58. * This should never occur because 1 byte of data always requires 2 bytes of
  59. * encoding, i.e.
  60. * 0 bytes unencoded = 0 bytes encoded
  61. * 1 byte unencoded = 2 bytes encoded
  62. * 2 bytes unencoded = 3 bytes encoded
  63. * 3 bytes unencoded = 4 bytes encoded
  64. * 4 bytes unencoded = 6 bytes encoded
  65. * etc
  66. */
  67. if (padsize == 3) {
  68. outl = -1;
  69. goto err;
  70. }
  71. /* Valid padsize values are now 0, 1 or 2 */
  72. EVP_DecodeInit(ctx);
  73. evp_encode_ctx_set_flags(ctx, EVP_ENCODE_CTX_USE_SRP_ALPHABET);
  74. /* Add any encoded padding that is required */
  75. if (padsize != 0
  76. && EVP_DecodeUpdate(ctx, a, &outl, pad, padsize) < 0) {
  77. outl = -1;
  78. goto err;
  79. }
  80. if (EVP_DecodeUpdate(ctx, a, &outl2, (const unsigned char *)src, size) < 0) {
  81. outl = -1;
  82. goto err;
  83. }
  84. outl += outl2;
  85. EVP_DecodeFinal(ctx, a + outl, &outl2);
  86. outl += outl2;
  87. /* Strip off the leading padding */
  88. if (padsize != 0) {
  89. if ((int)padsize >= outl) {
  90. outl = -1;
  91. goto err;
  92. }
  93. /*
  94. * If we added 1 byte of padding prior to encoding then we have 2 bytes
  95. * of "real" data which gets spread across 4 encoded bytes like this:
  96. * (6 bits pad)(2 bits pad | 4 bits data)(6 bits data)(6 bits data)
  97. * So 1 byte of pre-encoding padding results in 1 full byte of encoded
  98. * padding.
  99. * If we added 2 bytes of padding prior to encoding this gets encoded
  100. * as:
  101. * (6 bits pad)(6 bits pad)(4 bits pad | 2 bits data)(6 bits data)
  102. * So 2 bytes of pre-encoding padding results in 2 full bytes of encoded
  103. * padding, i.e. we have to strip the same number of bytes of padding
  104. * from the encoded data as we added to the pre-encoded data.
  105. */
  106. memmove(a, a + padsize, outl - padsize);
  107. outl -= padsize;
  108. }
  109. err:
  110. EVP_ENCODE_CTX_free(ctx);
  111. return outl;
  112. }
  113. /*
  114. * Convert a raw byte string into a null-terminated base64 ASCII string.
  115. * Returns 1 on success or 0 on error.
  116. */
  117. static int t_tob64(char *dst, const unsigned char *src, int size)
  118. {
  119. EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
  120. int outl = 0, outl2 = 0;
  121. unsigned char pad[2] = {0, 0};
  122. size_t leadz = 0;
  123. if (ctx == NULL)
  124. return 0;
  125. EVP_EncodeInit(ctx);
  126. evp_encode_ctx_set_flags(ctx, EVP_ENCODE_CTX_NO_NEWLINES
  127. | EVP_ENCODE_CTX_USE_SRP_ALPHABET);
  128. /*
  129. * We pad at the front with zero bytes until the length is a multiple of 3
  130. * so that EVP_EncodeUpdate/EVP_EncodeFinal does not add any of its own "="
  131. * padding
  132. */
  133. leadz = 3 - (size % 3);
  134. if (leadz != 3
  135. && !EVP_EncodeUpdate(ctx, (unsigned char *)dst, &outl, pad,
  136. leadz)) {
  137. EVP_ENCODE_CTX_free(ctx);
  138. return 0;
  139. }
  140. if (!EVP_EncodeUpdate(ctx, (unsigned char *)dst + outl, &outl2, src,
  141. size)) {
  142. EVP_ENCODE_CTX_free(ctx);
  143. return 0;
  144. }
  145. outl += outl2;
  146. EVP_EncodeFinal(ctx, (unsigned char *)dst + outl, &outl2);
  147. outl += outl2;
  148. /* Strip the encoded padding at the front */
  149. if (leadz != 3) {
  150. memmove(dst, dst + leadz, outl - leadz);
  151. dst[outl - leadz] = '\0';
  152. }
  153. EVP_ENCODE_CTX_free(ctx);
  154. return 1;
  155. }
  156. void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
  157. {
  158. if (user_pwd == NULL)
  159. return;
  160. BN_free(user_pwd->s);
  161. BN_clear_free(user_pwd->v);
  162. OPENSSL_free(user_pwd->id);
  163. OPENSSL_free(user_pwd->info);
  164. OPENSSL_free(user_pwd);
  165. }
  166. SRP_user_pwd *SRP_user_pwd_new(void)
  167. {
  168. SRP_user_pwd *ret;
  169. if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
  170. /* SRPerr(SRP_F_SRP_USER_PWD_NEW, ERR_R_MALLOC_FAILURE); */ /*ckerr_ignore*/
  171. return NULL;
  172. }
  173. ret->N = NULL;
  174. ret->g = NULL;
  175. ret->s = NULL;
  176. ret->v = NULL;
  177. ret->id = NULL;
  178. ret->info = NULL;
  179. return ret;
  180. }
  181. void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
  182. const BIGNUM *N)
  183. {
  184. vinfo->N = N;
  185. vinfo->g = g;
  186. }
  187. int SRP_user_pwd_set1_ids(SRP_user_pwd *vinfo, const char *id,
  188. const char *info)
  189. {
  190. OPENSSL_free(vinfo->id);
  191. OPENSSL_free(vinfo->info);
  192. if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id)))
  193. return 0;
  194. return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info)));
  195. }
  196. static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
  197. const char *v)
  198. {
  199. unsigned char tmp[MAX_LEN];
  200. int len;
  201. vinfo->v = NULL;
  202. vinfo->s = NULL;
  203. len = t_fromb64(tmp, sizeof(tmp), v);
  204. if (len < 0)
  205. return 0;
  206. if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
  207. return 0;
  208. len = t_fromb64(tmp, sizeof(tmp), s);
  209. if (len < 0)
  210. goto err;
  211. vinfo->s = BN_bin2bn(tmp, len, NULL);
  212. if (vinfo->s == NULL)
  213. goto err;
  214. return 1;
  215. err:
  216. BN_free(vinfo->v);
  217. vinfo->v = NULL;
  218. return 0;
  219. }
  220. int SRP_user_pwd_set0_sv(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
  221. {
  222. BN_free(vinfo->s);
  223. BN_clear_free(vinfo->v);
  224. vinfo->v = v;
  225. vinfo->s = s;
  226. return (vinfo->s != NULL && vinfo->v != NULL);
  227. }
  228. static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
  229. {
  230. SRP_user_pwd *ret;
  231. if (src == NULL)
  232. return NULL;
  233. if ((ret = SRP_user_pwd_new()) == NULL)
  234. return NULL;
  235. SRP_user_pwd_set_gN(ret, src->g, src->N);
  236. if (!SRP_user_pwd_set1_ids(ret, src->id, src->info)
  237. || !SRP_user_pwd_set0_sv(ret, BN_dup(src->s), BN_dup(src->v))) {
  238. SRP_user_pwd_free(ret);
  239. return NULL;
  240. }
  241. return ret;
  242. }
  243. SRP_VBASE *SRP_VBASE_new(char *seed_key)
  244. {
  245. SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb));
  246. if (vb == NULL)
  247. return NULL;
  248. if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL
  249. || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) {
  250. OPENSSL_free(vb);
  251. return NULL;
  252. }
  253. vb->default_g = NULL;
  254. vb->default_N = NULL;
  255. vb->seed_key = NULL;
  256. if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) {
  257. sk_SRP_user_pwd_free(vb->users_pwd);
  258. sk_SRP_gN_cache_free(vb->gN_cache);
  259. OPENSSL_free(vb);
  260. return NULL;
  261. }
  262. return vb;
  263. }
  264. void SRP_VBASE_free(SRP_VBASE *vb)
  265. {
  266. if (!vb)
  267. return;
  268. sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
  269. sk_SRP_gN_cache_free(vb->gN_cache);
  270. OPENSSL_free(vb->seed_key);
  271. OPENSSL_free(vb);
  272. }
  273. static SRP_gN_cache *SRP_gN_new_init(const char *ch)
  274. {
  275. unsigned char tmp[MAX_LEN];
  276. int len;
  277. SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN));
  278. if (newgN == NULL)
  279. return NULL;
  280. len = t_fromb64(tmp, sizeof(tmp), ch);
  281. if (len < 0)
  282. goto err;
  283. if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL)
  284. goto err;
  285. if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
  286. return newgN;
  287. OPENSSL_free(newgN->b64_bn);
  288. err:
  289. OPENSSL_free(newgN);
  290. return NULL;
  291. }
  292. static void SRP_gN_free(SRP_gN_cache *gN_cache)
  293. {
  294. if (gN_cache == NULL)
  295. return;
  296. OPENSSL_free(gN_cache->b64_bn);
  297. BN_free(gN_cache->bn);
  298. OPENSSL_free(gN_cache);
  299. }
  300. static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
  301. {
  302. int i;
  303. SRP_gN *gN;
  304. if (gN_tab != NULL) {
  305. for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
  306. gN = sk_SRP_gN_value(gN_tab, i);
  307. if (gN && (id == NULL || strcmp(gN->id, id) == 0))
  308. return gN;
  309. }
  310. }
  311. return SRP_get_default_gN(id);
  312. }
  313. static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
  314. {
  315. int i;
  316. if (gN_cache == NULL)
  317. return NULL;
  318. /* search if we have already one... */
  319. for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
  320. SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
  321. if (strcmp(cache->b64_bn, ch) == 0)
  322. return cache->bn;
  323. }
  324. { /* it is the first time that we find it */
  325. SRP_gN_cache *newgN = SRP_gN_new_init(ch);
  326. if (newgN) {
  327. if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
  328. return newgN->bn;
  329. SRP_gN_free(newgN);
  330. }
  331. }
  332. return NULL;
  333. }
  334. /*
  335. * This function parses the verifier file generated by the srp app.
  336. * The format for each entry is:
  337. * V base64(verifier) base64(salt) username gNid userinfo(optional)
  338. * or
  339. * I base64(N) base64(g)
  340. * Note that base64 is the SRP variant of base64 encoding described
  341. * in t_fromb64().
  342. */
  343. int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
  344. {
  345. int error_code;
  346. STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
  347. char *last_index = NULL;
  348. int i;
  349. char **pp;
  350. SRP_gN *gN = NULL;
  351. SRP_user_pwd *user_pwd = NULL;
  352. TXT_DB *tmpdb = NULL;
  353. BIO *in = BIO_new(BIO_s_file());
  354. error_code = SRP_ERR_OPEN_FILE;
  355. if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
  356. goto err;
  357. error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
  358. if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
  359. goto err;
  360. error_code = SRP_ERR_MEMORY;
  361. if (vb->seed_key) {
  362. last_index = SRP_get_default_gN(NULL)->id;
  363. }
  364. for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
  365. pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
  366. if (pp[DB_srptype][0] == DB_SRP_INDEX) {
  367. /*
  368. * we add this couple in the internal Stack
  369. */
  370. if ((gN = OPENSSL_malloc(sizeof(*gN))) == NULL)
  371. goto err;
  372. if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL
  373. || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
  374. == NULL
  375. || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
  376. == NULL
  377. || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
  378. goto err;
  379. gN = NULL;
  380. if (vb->seed_key != NULL) {
  381. last_index = pp[DB_srpid];
  382. }
  383. } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
  384. /* it is a user .... */
  385. const SRP_gN *lgN;
  386. if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
  387. error_code = SRP_ERR_MEMORY;
  388. if ((user_pwd = SRP_user_pwd_new()) == NULL)
  389. goto err;
  390. SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
  391. if (!SRP_user_pwd_set1_ids
  392. (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
  393. goto err;
  394. error_code = SRP_ERR_VBASE_BN_LIB;
  395. if (!SRP_user_pwd_set_sv
  396. (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
  397. goto err;
  398. if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
  399. goto err;
  400. user_pwd = NULL; /* abandon responsibility */
  401. }
  402. }
  403. }
  404. if (last_index != NULL) {
  405. /* this means that we want to simulate a default user */
  406. if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
  407. error_code = SRP_ERR_VBASE_BN_LIB;
  408. goto err;
  409. }
  410. vb->default_g = gN->g;
  411. vb->default_N = gN->N;
  412. gN = NULL;
  413. }
  414. error_code = SRP_NO_ERROR;
  415. err:
  416. /*
  417. * there may be still some leaks to fix, if this fails, the application
  418. * terminates most likely
  419. */
  420. if (gN != NULL) {
  421. OPENSSL_free(gN->id);
  422. OPENSSL_free(gN);
  423. }
  424. SRP_user_pwd_free(user_pwd);
  425. TXT_DB_free(tmpdb);
  426. BIO_free_all(in);
  427. sk_SRP_gN_free(SRP_gN_tab);
  428. return error_code;
  429. }
  430. static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
  431. {
  432. int i;
  433. SRP_user_pwd *user;
  434. if (vb == NULL)
  435. return NULL;
  436. for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
  437. user = sk_SRP_user_pwd_value(vb->users_pwd, i);
  438. if (strcmp(user->id, username) == 0)
  439. return user;
  440. }
  441. return NULL;
  442. }
  443. int SRP_VBASE_add0_user(SRP_VBASE *vb, SRP_user_pwd *user_pwd)
  444. {
  445. if (sk_SRP_user_pwd_push(vb->users_pwd, user_pwd) <= 0)
  446. return 0;
  447. return 1;
  448. }
  449. # ifndef OPENSSL_NO_DEPRECATED_1_1_0
  450. /*
  451. * DEPRECATED: use SRP_VBASE_get1_by_user instead.
  452. * This method ignores the configured seed and fails for an unknown user.
  453. * Ownership of the returned pointer is not released to the caller.
  454. * In other words, caller must not free the result.
  455. */
  456. SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
  457. {
  458. return find_user(vb, username);
  459. }
  460. # endif
  461. /*
  462. * Ownership of the returned pointer is released to the caller.
  463. * In other words, caller must free the result once done.
  464. */
  465. SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
  466. {
  467. SRP_user_pwd *user;
  468. unsigned char digv[SHA_DIGEST_LENGTH];
  469. unsigned char digs[SHA_DIGEST_LENGTH];
  470. EVP_MD_CTX *ctxt = NULL;
  471. if (vb == NULL)
  472. return NULL;
  473. if ((user = find_user(vb, username)) != NULL)
  474. return srp_user_pwd_dup(user);
  475. if ((vb->seed_key == NULL) ||
  476. (vb->default_g == NULL) || (vb->default_N == NULL))
  477. return NULL;
  478. /* if the user is unknown we set parameters as well if we have a seed_key */
  479. if ((user = SRP_user_pwd_new()) == NULL)
  480. return NULL;
  481. SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
  482. if (!SRP_user_pwd_set1_ids(user, username, NULL))
  483. goto err;
  484. if (RAND_priv_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
  485. goto err;
  486. ctxt = EVP_MD_CTX_new();
  487. if (ctxt == NULL
  488. || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
  489. || !EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key))
  490. || !EVP_DigestUpdate(ctxt, username, strlen(username))
  491. || !EVP_DigestFinal_ex(ctxt, digs, NULL))
  492. goto err;
  493. EVP_MD_CTX_free(ctxt);
  494. ctxt = NULL;
  495. if (SRP_user_pwd_set0_sv(user,
  496. BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
  497. BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
  498. return user;
  499. err:
  500. EVP_MD_CTX_free(ctxt);
  501. SRP_user_pwd_free(user);
  502. return NULL;
  503. }
  504. /*
  505. * create a verifier (*salt,*verifier,g and N are in base64)
  506. */
  507. char *SRP_create_verifier_ex(const char *user, const char *pass, char **salt,
  508. char **verifier, const char *N, const char *g,
  509. OPENSSL_CTX *libctx, const char *propq)
  510. {
  511. int len;
  512. char *result = NULL, *vf = NULL;
  513. const BIGNUM *N_bn = NULL, *g_bn = NULL;
  514. BIGNUM *N_bn_alloc = NULL, *g_bn_alloc = NULL, *s = NULL, *v = NULL;
  515. unsigned char tmp[MAX_LEN];
  516. unsigned char tmp2[MAX_LEN];
  517. char *defgNid = NULL;
  518. int vfsize = 0;
  519. if ((user == NULL) ||
  520. (pass == NULL) || (salt == NULL) || (verifier == NULL))
  521. goto err;
  522. if (N) {
  523. if ((len = t_fromb64(tmp, sizeof(tmp), N)) <= 0)
  524. goto err;
  525. N_bn_alloc = BN_bin2bn(tmp, len, NULL);
  526. if (N_bn_alloc == NULL)
  527. goto err;
  528. N_bn = N_bn_alloc;
  529. if ((len = t_fromb64(tmp, sizeof(tmp) ,g)) <= 0)
  530. goto err;
  531. g_bn_alloc = BN_bin2bn(tmp, len, NULL);
  532. if (g_bn_alloc == NULL)
  533. goto err;
  534. g_bn = g_bn_alloc;
  535. defgNid = "*";
  536. } else {
  537. SRP_gN *gN = SRP_get_default_gN(g);
  538. if (gN == NULL)
  539. goto err;
  540. N_bn = gN->N;
  541. g_bn = gN->g;
  542. defgNid = gN->id;
  543. }
  544. if (*salt == NULL) {
  545. if (RAND_bytes_ex(libctx, tmp2, SRP_RANDOM_SALT_LEN) <= 0)
  546. goto err;
  547. s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
  548. } else {
  549. if ((len = t_fromb64(tmp2, sizeof(tmp2), *salt)) <= 0)
  550. goto err;
  551. s = BN_bin2bn(tmp2, len, NULL);
  552. }
  553. if (s == NULL)
  554. goto err;
  555. if (!SRP_create_verifier_BN_ex(user, pass, &s, &v, N_bn, g_bn, libctx,
  556. propq))
  557. goto err;
  558. if (BN_bn2bin(v, tmp) < 0)
  559. goto err;
  560. vfsize = BN_num_bytes(v) * 2;
  561. if (((vf = OPENSSL_malloc(vfsize)) == NULL))
  562. goto err;
  563. if (!t_tob64(vf, tmp, BN_num_bytes(v)))
  564. goto err;
  565. if (*salt == NULL) {
  566. char *tmp_salt;
  567. if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
  568. goto err;
  569. }
  570. if (!t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN)) {
  571. OPENSSL_free(tmp_salt);
  572. goto err;
  573. }
  574. *salt = tmp_salt;
  575. }
  576. *verifier = vf;
  577. vf = NULL;
  578. result = defgNid;
  579. err:
  580. BN_free(N_bn_alloc);
  581. BN_free(g_bn_alloc);
  582. OPENSSL_clear_free(vf, vfsize);
  583. BN_clear_free(s);
  584. BN_clear_free(v);
  585. return result;
  586. }
  587. char *SRP_create_verifier(const char *user, const char *pass, char **salt,
  588. char **verifier, const char *N, const char *g)
  589. {
  590. return SRP_create_verifier_ex(user, pass, salt, verifier, N, g, NULL, NULL);
  591. }
  592. /*
  593. * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
  594. * then the provided salt will be used. On successful exit *verifier will point
  595. * to a newly allocated BIGNUM containing the verifier and (if a salt was not
  596. * provided) *salt will be populated with a newly allocated BIGNUM containing a
  597. * random salt.
  598. * The caller is responsible for freeing the allocated *salt and *verifier
  599. * BIGNUMS.
  600. */
  601. int SRP_create_verifier_BN_ex(const char *user, const char *pass, BIGNUM **salt,
  602. BIGNUM **verifier, const BIGNUM *N,
  603. const BIGNUM *g, OPENSSL_CTX *libctx,
  604. const char *propq)
  605. {
  606. int result = 0;
  607. BIGNUM *x = NULL;
  608. BN_CTX *bn_ctx = BN_CTX_new_ex(libctx);
  609. unsigned char tmp2[MAX_LEN];
  610. BIGNUM *salttmp = NULL;
  611. if ((user == NULL) ||
  612. (pass == NULL) ||
  613. (salt == NULL) ||
  614. (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
  615. goto err;
  616. if (*salt == NULL) {
  617. if (RAND_bytes_ex(libctx, tmp2, SRP_RANDOM_SALT_LEN) <= 0)
  618. goto err;
  619. salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
  620. if (salttmp == NULL)
  621. goto err;
  622. } else {
  623. salttmp = *salt;
  624. }
  625. x = SRP_Calc_x_ex(salttmp, user, pass, libctx, propq);
  626. if (x == NULL)
  627. goto err;
  628. *verifier = BN_new();
  629. if (*verifier == NULL)
  630. goto err;
  631. if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
  632. BN_clear_free(*verifier);
  633. goto err;
  634. }
  635. result = 1;
  636. *salt = salttmp;
  637. err:
  638. if (salt != NULL && *salt != salttmp)
  639. BN_clear_free(salttmp);
  640. BN_clear_free(x);
  641. BN_CTX_free(bn_ctx);
  642. return result;
  643. }
  644. int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
  645. BIGNUM **verifier, const BIGNUM *N,
  646. const BIGNUM *g)
  647. {
  648. return SRP_create_verifier_BN_ex(user, pass, salt, verifier, N, g, NULL,
  649. NULL);
  650. }
  651. #endif