srp_vfy.c 21 KB

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