srp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /* apps/srp.c */
  2. /*
  3. * Written by Peter Sylvester (peter.sylvester@edelweb.fr) for the EdelKey
  4. * project and contributed to the OpenSSL project 2004.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <openssl/opensslconf.h>
  60. #ifndef OPENSSL_NO_SRP
  61. # include <stdio.h>
  62. # include <stdlib.h>
  63. # include <string.h>
  64. # include <openssl/conf.h>
  65. # include <openssl/bio.h>
  66. # include <openssl/err.h>
  67. # include <openssl/txt_db.h>
  68. # include <openssl/buffer.h>
  69. # include <openssl/srp.h>
  70. # include "apps.h"
  71. # undef PROG
  72. # define PROG srp_main
  73. # define BASE_SECTION "srp"
  74. # define CONFIG_FILE "openssl.cnf"
  75. # define ENV_RANDFILE "RANDFILE"
  76. # define ENV_DATABASE "srpvfile"
  77. # define ENV_DEFAULT_SRP "default_srp"
  78. static char *srp_usage[] = {
  79. "usage: srp [args] [user] \n",
  80. "\n",
  81. " -verbose Talk alot while doing things\n",
  82. " -config file A config file\n",
  83. " -name arg The particular srp definition to use\n",
  84. " -srpvfile arg The srp verifier file name\n",
  85. " -add add an user and srp verifier\n",
  86. " -modify modify the srp verifier of an existing user\n",
  87. " -delete delete user from verifier file\n",
  88. " -list list user\n",
  89. " -gn arg g and N values to be used for new verifier\n",
  90. " -userinfo arg additional info to be set for user\n",
  91. " -passin arg input file pass phrase source\n",
  92. " -passout arg output file pass phrase source\n",
  93. # ifndef OPENSSL_NO_ENGINE
  94. " -engine e - use engine e, possibly a hardware device.\n",
  95. # endif
  96. NULL
  97. };
  98. # ifdef EFENCE
  99. extern int EF_PROTECT_FREE;
  100. extern int EF_PROTECT_BELOW;
  101. extern int EF_ALIGNMENT;
  102. # endif
  103. static CONF *conf = NULL;
  104. static char *section = NULL;
  105. # define VERBOSE if (verbose)
  106. # define VVERBOSE if (verbose>1)
  107. int MAIN(int, char **);
  108. static int get_index(CA_DB *db, char *id, char type)
  109. {
  110. char **pp;
  111. int i;
  112. if (id == NULL)
  113. return -1;
  114. if (type == DB_SRP_INDEX) {
  115. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  116. pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
  117. if (pp[DB_srptype][0] == DB_SRP_INDEX
  118. && !strcmp(id, pp[DB_srpid]))
  119. return i;
  120. }
  121. } else {
  122. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  123. pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
  124. if (pp[DB_srptype][0] != DB_SRP_INDEX
  125. && !strcmp(id, pp[DB_srpid]))
  126. return i;
  127. }
  128. }
  129. return -1;
  130. }
  131. static void print_entry(CA_DB *db, BIO *bio, int indx, int verbose, char *s)
  132. {
  133. if (indx >= 0 && verbose) {
  134. int j;
  135. char **pp = sk_OPENSSL_PSTRING_value(db->db->data, indx);
  136. BIO_printf(bio, "%s \"%s\"\n", s, pp[DB_srpid]);
  137. for (j = 0; j < DB_NUMBER; j++) {
  138. BIO_printf(bio_err, " %d = \"%s\"\n", j, pp[j]);
  139. }
  140. }
  141. }
  142. static void print_index(CA_DB *db, BIO *bio, int indexindex, int verbose)
  143. {
  144. print_entry(db, bio, indexindex, verbose, "g N entry");
  145. }
  146. static void print_user(CA_DB *db, BIO *bio, int userindex, int verbose)
  147. {
  148. if (verbose > 0) {
  149. char **pp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);
  150. if (pp[DB_srptype][0] != 'I') {
  151. print_entry(db, bio, userindex, verbose, "User entry");
  152. print_entry(db, bio, get_index(db, pp[DB_srpgN], 'I'), verbose,
  153. "g N entry");
  154. }
  155. }
  156. }
  157. static int update_index(CA_DB *db, BIO *bio, char **row)
  158. {
  159. char **irow;
  160. int i;
  161. irow = (char **)OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1));
  162. if (irow == NULL) {
  163. BIO_printf(bio_err, "Memory allocation failure\n");
  164. return 0;
  165. }
  166. for (i = 0; i < DB_NUMBER; i++)
  167. irow[i] = row[i];
  168. irow[DB_NUMBER] = NULL;
  169. if (!TXT_DB_insert(db->db, irow)) {
  170. BIO_printf(bio, "failed to update srpvfile\n");
  171. BIO_printf(bio, "TXT_DB error number %ld\n", db->db->error);
  172. OPENSSL_free(irow);
  173. return 0;
  174. }
  175. return 1;
  176. }
  177. static void lookup_fail(const char *name, char *tag)
  178. {
  179. BIO_printf(bio_err, "variable lookup failed for %s::%s\n", name, tag);
  180. }
  181. static char *srp_verify_user(const char *user, const char *srp_verifier,
  182. char *srp_usersalt, const char *g, const char *N,
  183. const char *passin, BIO *bio, int verbose)
  184. {
  185. char password[1025];
  186. PW_CB_DATA cb_tmp;
  187. char *verifier = NULL;
  188. char *gNid = NULL;
  189. int len;
  190. cb_tmp.prompt_info = user;
  191. cb_tmp.password = passin;
  192. len = password_callback(password, sizeof(password)-1, 0, &cb_tmp);
  193. if (len > 0) {
  194. password[len] = 0;
  195. VERBOSE BIO_printf(bio,
  196. "Validating\n user=\"%s\"\n srp_verifier=\"%s\"\n srp_usersalt=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
  197. user, srp_verifier, srp_usersalt, g, N);
  198. VVERBOSE BIO_printf(bio, "Pass %s\n", password);
  199. if (!(gNid = SRP_create_verifier(user, password, &srp_usersalt,
  200. &verifier, N, g))) {
  201. BIO_printf(bio, "Internal error validating SRP verifier\n");
  202. } else {
  203. if (strcmp(verifier, srp_verifier))
  204. gNid = NULL;
  205. OPENSSL_free(verifier);
  206. }
  207. OPENSSL_cleanse(password, len);
  208. }
  209. return gNid;
  210. }
  211. static char *srp_create_user(char *user, char **srp_verifier,
  212. char **srp_usersalt, char *g, char *N,
  213. char *passout, BIO *bio, int verbose)
  214. {
  215. char password[1025];
  216. PW_CB_DATA cb_tmp;
  217. char *gNid = NULL;
  218. char *salt = NULL;
  219. int len;
  220. cb_tmp.prompt_info = user;
  221. cb_tmp.password = passout;
  222. len = password_callback(password, sizeof(password)-1, 1, &cb_tmp);
  223. if (len > 0) {
  224. password[len] = 0;
  225. VERBOSE BIO_printf(bio,
  226. "Creating\n user=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
  227. user, g, N);
  228. if (!(gNid = SRP_create_verifier(user, password, &salt,
  229. srp_verifier, N, g))) {
  230. BIO_printf(bio, "Internal error creating SRP verifier\n");
  231. } else {
  232. *srp_usersalt = salt;
  233. }
  234. OPENSSL_cleanse(password, len);
  235. VVERBOSE BIO_printf(bio, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n",
  236. gNid, salt, *srp_verifier);
  237. }
  238. return gNid;
  239. }
  240. int MAIN(int argc, char **argv)
  241. {
  242. int add_user = 0;
  243. int list_user = 0;
  244. int delete_user = 0;
  245. int modify_user = 0;
  246. char *user = NULL;
  247. char *passargin = NULL, *passargout = NULL;
  248. char *passin = NULL, *passout = NULL;
  249. char *gN = NULL;
  250. int gNindex = -1;
  251. char **gNrow = NULL;
  252. int maxgN = -1;
  253. char *userinfo = NULL;
  254. int badops = 0;
  255. int ret = 1;
  256. int errors = 0;
  257. int verbose = 0;
  258. int doupdatedb = 0;
  259. char *configfile = NULL;
  260. char *dbfile = NULL;
  261. CA_DB *db = NULL;
  262. char **pp;
  263. int i;
  264. long errorline = -1;
  265. char *randfile = NULL;
  266. ENGINE *e = NULL;
  267. char *engine = NULL;
  268. char *tofree = NULL;
  269. DB_ATTR db_attr;
  270. # ifdef EFENCE
  271. EF_PROTECT_FREE = 1;
  272. EF_PROTECT_BELOW = 1;
  273. EF_ALIGNMENT = 0;
  274. # endif
  275. apps_startup();
  276. conf = NULL;
  277. section = NULL;
  278. if (bio_err == NULL)
  279. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  280. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  281. argc--;
  282. argv++;
  283. while (argc >= 1 && badops == 0) {
  284. if (strcmp(*argv, "-verbose") == 0) {
  285. verbose++;
  286. } else if (strcmp(*argv, "-config") == 0) {
  287. if (--argc < 1)
  288. goto bad;
  289. configfile = *(++argv);
  290. } else if (strcmp(*argv, "-name") == 0) {
  291. if (--argc < 1)
  292. goto bad;
  293. section = *(++argv);
  294. } else if (strcmp(*argv, "-srpvfile") == 0) {
  295. if (--argc < 1)
  296. goto bad;
  297. dbfile = *(++argv);
  298. } else if (strcmp(*argv, "-add") == 0) {
  299. add_user = 1;
  300. } else if (strcmp(*argv, "-delete") == 0) {
  301. delete_user = 1;
  302. } else if (strcmp(*argv, "-modify") == 0) {
  303. modify_user = 1;
  304. } else if (strcmp(*argv, "-list") == 0) {
  305. list_user = 1;
  306. } else if (strcmp(*argv, "-gn") == 0) {
  307. if (--argc < 1)
  308. goto bad;
  309. gN = *(++argv);
  310. } else if (strcmp(*argv, "-userinfo") == 0) {
  311. if (--argc < 1)
  312. goto bad;
  313. userinfo = *(++argv);
  314. } else if (strcmp(*argv, "-passin") == 0) {
  315. if (--argc < 1)
  316. goto bad;
  317. passargin = *(++argv);
  318. } else if (strcmp(*argv, "-passout") == 0) {
  319. if (--argc < 1)
  320. goto bad;
  321. passargout = *(++argv);
  322. }
  323. # ifndef OPENSSL_NO_ENGINE
  324. else if (strcmp(*argv, "-engine") == 0) {
  325. if (--argc < 1)
  326. goto bad;
  327. engine = *(++argv);
  328. }
  329. # endif
  330. else if (**argv == '-') {
  331. bad:
  332. BIO_printf(bio_err, "unknown option %s\n", *argv);
  333. badops = 1;
  334. break;
  335. } else {
  336. break;
  337. }
  338. argc--;
  339. argv++;
  340. }
  341. if (dbfile && configfile) {
  342. BIO_printf(bio_err,
  343. "-dbfile and -configfile cannot be specified together.\n");
  344. badops = 1;
  345. }
  346. if (add_user + delete_user + modify_user + list_user != 1) {
  347. BIO_printf(bio_err,
  348. "Exactly one of the options -add, -delete, -modify -list must be specified.\n");
  349. badops = 1;
  350. }
  351. if (delete_user + modify_user + delete_user == 1 && argc <= 0) {
  352. BIO_printf(bio_err,
  353. "Need at least one user for options -add, -delete, -modify. \n");
  354. badops = 1;
  355. }
  356. if ((passargin || passargout) && argc != 1) {
  357. BIO_printf(bio_err,
  358. "-passin, -passout arguments only valid with one user.\n");
  359. badops = 1;
  360. }
  361. if (badops) {
  362. for (pp = srp_usage; (*pp != NULL); pp++)
  363. BIO_printf(bio_err, "%s", *pp);
  364. BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
  365. LIST_SEPARATOR_CHAR);
  366. BIO_printf(bio_err,
  367. " load the file (or the files in the directory) into\n");
  368. BIO_printf(bio_err, " the random number generator\n");
  369. goto err;
  370. }
  371. ERR_load_crypto_strings();
  372. e = setup_engine(bio_err, engine, 0);
  373. if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
  374. BIO_printf(bio_err, "Error getting passwords\n");
  375. goto err;
  376. }
  377. if (!dbfile) {
  378. /*****************************************************************/
  379. tofree = NULL;
  380. if (configfile == NULL)
  381. configfile = getenv("OPENSSL_CONF");
  382. if (configfile == NULL)
  383. configfile = getenv("SSLEAY_CONF");
  384. if (configfile == NULL) {
  385. const char *s = X509_get_default_cert_area();
  386. size_t len;
  387. # ifdef OPENSSL_SYS_VMS
  388. len = strlen(s) + sizeof(CONFIG_FILE);
  389. tofree = OPENSSL_malloc(len);
  390. if (!tofree) {
  391. BIO_printf(bio_err, "Out of memory\n");
  392. goto err;
  393. }
  394. strcpy(tofree, s);
  395. # else
  396. len = strlen(s) + sizeof(CONFIG_FILE) + 1;
  397. tofree = OPENSSL_malloc(len);
  398. if (!tofree) {
  399. BIO_printf(bio_err, "Out of memory\n");
  400. goto err;
  401. }
  402. BUF_strlcpy(tofree, s, len);
  403. BUF_strlcat(tofree, "/", len);
  404. # endif
  405. BUF_strlcat(tofree, CONFIG_FILE, len);
  406. configfile = tofree;
  407. }
  408. VERBOSE BIO_printf(bio_err, "Using configuration from %s\n",
  409. configfile);
  410. conf = NCONF_new(NULL);
  411. if (NCONF_load(conf, configfile, &errorline) <= 0) {
  412. if (errorline <= 0)
  413. BIO_printf(bio_err, "error loading the config file '%s'\n",
  414. configfile);
  415. else
  416. BIO_printf(bio_err, "error on line %ld of config file '%s'\n",
  417. errorline, configfile);
  418. goto err;
  419. }
  420. if (tofree) {
  421. OPENSSL_free(tofree);
  422. tofree = NULL;
  423. }
  424. if (!load_config(bio_err, conf))
  425. goto err;
  426. /* Lets get the config section we are using */
  427. if (section == NULL) {
  428. VERBOSE BIO_printf(bio_err,
  429. "trying to read " ENV_DEFAULT_SRP
  430. " in \" BASE_SECTION \"\n");
  431. section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_SRP);
  432. if (section == NULL) {
  433. lookup_fail(BASE_SECTION, ENV_DEFAULT_SRP);
  434. goto err;
  435. }
  436. }
  437. if (randfile == NULL && conf)
  438. randfile = NCONF_get_string(conf, BASE_SECTION, "RANDFILE");
  439. VERBOSE BIO_printf(bio_err,
  440. "trying to read " ENV_DATABASE
  441. " in section \"%s\"\n", section);
  442. if ((dbfile = NCONF_get_string(conf, section, ENV_DATABASE)) == NULL) {
  443. lookup_fail(section, ENV_DATABASE);
  444. goto err;
  445. }
  446. }
  447. if (randfile == NULL)
  448. ERR_clear_error();
  449. else
  450. app_RAND_load_file(randfile, bio_err, 0);
  451. VERBOSE BIO_printf(bio_err, "Trying to read SRP verifier file \"%s\"\n",
  452. dbfile);
  453. db = load_index(dbfile, &db_attr);
  454. if (db == NULL)
  455. goto err;
  456. /* Lets check some fields */
  457. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  458. pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
  459. if (pp[DB_srptype][0] == DB_SRP_INDEX) {
  460. maxgN = i;
  461. if (gNindex < 0 && gN != NULL && !strcmp(gN, pp[DB_srpid]))
  462. gNindex = i;
  463. print_index(db, bio_err, i, verbose > 1);
  464. }
  465. }
  466. VERBOSE BIO_printf(bio_err, "Database initialised\n");
  467. if (gNindex >= 0) {
  468. gNrow = sk_OPENSSL_PSTRING_value(db->db->data, gNindex);
  469. print_entry(db, bio_err, gNindex, verbose > 1, "Default g and N");
  470. } else if (maxgN > 0 && !SRP_get_default_gN(gN)) {
  471. BIO_printf(bio_err, "No g and N value for index \"%s\"\n", gN);
  472. goto err;
  473. } else {
  474. VERBOSE BIO_printf(bio_err, "Database has no g N information.\n");
  475. gNrow = NULL;
  476. }
  477. VVERBOSE BIO_printf(bio_err, "Starting user processing\n");
  478. if (argc > 0)
  479. user = *(argv++);
  480. while (list_user || user) {
  481. int userindex = -1;
  482. if (user)
  483. VVERBOSE BIO_printf(bio_err, "Processing user \"%s\"\n", user);
  484. if ((userindex = get_index(db, user, 'U')) >= 0) {
  485. print_user(db, bio_err, userindex, (verbose > 0) || list_user);
  486. }
  487. if (list_user) {
  488. if (user == NULL) {
  489. BIO_printf(bio_err, "List all users\n");
  490. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  491. print_user(db, bio_err, i, 1);
  492. }
  493. list_user = 0;
  494. } else if (userindex < 0) {
  495. BIO_printf(bio_err,
  496. "user \"%s\" does not exist, ignored. t\n", user);
  497. errors++;
  498. }
  499. } else if (add_user) {
  500. if (userindex >= 0) {
  501. /* reactivation of a new user */
  502. char **row =
  503. sk_OPENSSL_PSTRING_value(db->db->data, userindex);
  504. BIO_printf(bio_err, "user \"%s\" reactivated.\n", user);
  505. row[DB_srptype][0] = 'V';
  506. doupdatedb = 1;
  507. } else {
  508. char *row[DB_NUMBER];
  509. char *gNid;
  510. row[DB_srpverifier] = NULL;
  511. row[DB_srpsalt] = NULL;
  512. row[DB_srpinfo] = NULL;
  513. if (!
  514. (gNid =
  515. srp_create_user(user, &(row[DB_srpverifier]),
  516. &(row[DB_srpsalt]),
  517. gNrow ? gNrow[DB_srpsalt] : gN,
  518. gNrow ? gNrow[DB_srpverifier] : NULL,
  519. passout, bio_err, verbose))) {
  520. BIO_printf(bio_err,
  521. "Cannot create srp verifier for user \"%s\", operation abandoned .\n",
  522. user);
  523. errors++;
  524. goto err;
  525. }
  526. row[DB_srpid] = BUF_strdup(user);
  527. row[DB_srptype] = BUF_strdup("v");
  528. row[DB_srpgN] = BUF_strdup(gNid);
  529. if (!row[DB_srpid] || !row[DB_srpgN] || !row[DB_srptype]
  530. || !row[DB_srpverifier] || !row[DB_srpsalt] || (userinfo
  531. &&
  532. (!(row
  533. [DB_srpinfo]
  534. =
  535. BUF_strdup
  536. (userinfo))))
  537. || !update_index(db, bio_err, row)) {
  538. if (row[DB_srpid])
  539. OPENSSL_free(row[DB_srpid]);
  540. if (row[DB_srpgN])
  541. OPENSSL_free(row[DB_srpgN]);
  542. if (row[DB_srpinfo])
  543. OPENSSL_free(row[DB_srpinfo]);
  544. if (row[DB_srptype])
  545. OPENSSL_free(row[DB_srptype]);
  546. if (row[DB_srpverifier])
  547. OPENSSL_free(row[DB_srpverifier]);
  548. if (row[DB_srpsalt])
  549. OPENSSL_free(row[DB_srpsalt]);
  550. goto err;
  551. }
  552. doupdatedb = 1;
  553. }
  554. } else if (modify_user) {
  555. if (userindex < 0) {
  556. BIO_printf(bio_err,
  557. "user \"%s\" does not exist, operation ignored.\n",
  558. user);
  559. errors++;
  560. } else {
  561. char **row =
  562. sk_OPENSSL_PSTRING_value(db->db->data, userindex);
  563. char type = row[DB_srptype][0];
  564. if (type == 'v') {
  565. BIO_printf(bio_err,
  566. "user \"%s\" already updated, operation ignored.\n",
  567. user);
  568. errors++;
  569. } else {
  570. char *gNid;
  571. if (row[DB_srptype][0] == 'V') {
  572. int user_gN;
  573. char **irow = NULL;
  574. VERBOSE BIO_printf(bio_err,
  575. "Verifying password for user \"%s\"\n",
  576. user);
  577. if ((user_gN =
  578. get_index(db, row[DB_srpgN], DB_SRP_INDEX)) >= 0)
  579. irow =
  580. (char **)sk_OPENSSL_PSTRING_value(db->
  581. db->data,
  582. userindex);
  583. if (!srp_verify_user
  584. (user, row[DB_srpverifier], row[DB_srpsalt],
  585. irow ? irow[DB_srpsalt] : row[DB_srpgN],
  586. irow ? irow[DB_srpverifier] : NULL, passin,
  587. bio_err, verbose)) {
  588. BIO_printf(bio_err,
  589. "Invalid password for user \"%s\", operation abandoned.\n",
  590. user);
  591. errors++;
  592. goto err;
  593. }
  594. }
  595. VERBOSE BIO_printf(bio_err,
  596. "Password for user \"%s\" ok.\n",
  597. user);
  598. if (!
  599. (gNid =
  600. srp_create_user(user, &(row[DB_srpverifier]),
  601. &(row[DB_srpsalt]),
  602. gNrow ? gNrow[DB_srpsalt] : NULL,
  603. gNrow ? gNrow[DB_srpverifier] : NULL,
  604. passout, bio_err, verbose))) {
  605. BIO_printf(bio_err,
  606. "Cannot create srp verifier for user \"%s\", operation abandoned.\n",
  607. user);
  608. errors++;
  609. goto err;
  610. }
  611. row[DB_srptype][0] = 'v';
  612. row[DB_srpgN] = BUF_strdup(gNid);
  613. if (!row[DB_srpid] || !row[DB_srpgN] || !row[DB_srptype]
  614. || !row[DB_srpverifier] || !row[DB_srpsalt]
  615. || (userinfo
  616. && (!(row[DB_srpinfo] = BUF_strdup(userinfo)))))
  617. goto err;
  618. doupdatedb = 1;
  619. }
  620. }
  621. } else if (delete_user) {
  622. if (userindex < 0) {
  623. BIO_printf(bio_err,
  624. "user \"%s\" does not exist, operation ignored. t\n",
  625. user);
  626. errors++;
  627. } else {
  628. char **xpp =
  629. sk_OPENSSL_PSTRING_value(db->db->data, userindex);
  630. BIO_printf(bio_err, "user \"%s\" revoked. t\n", user);
  631. xpp[DB_srptype][0] = 'R';
  632. doupdatedb = 1;
  633. }
  634. }
  635. if (--argc > 0) {
  636. user = *(argv++);
  637. } else {
  638. user = NULL;
  639. list_user = 0;
  640. }
  641. }
  642. VERBOSE BIO_printf(bio_err, "User procession done.\n");
  643. if (doupdatedb) {
  644. /* Lets check some fields */
  645. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  646. pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
  647. if (pp[DB_srptype][0] == 'v') {
  648. pp[DB_srptype][0] = 'V';
  649. print_user(db, bio_err, i, verbose);
  650. }
  651. }
  652. VERBOSE BIO_printf(bio_err, "Trying to update srpvfile.\n");
  653. if (!save_index(dbfile, "new", db))
  654. goto err;
  655. VERBOSE BIO_printf(bio_err, "Temporary srpvfile created.\n");
  656. if (!rotate_index(dbfile, "new", "old"))
  657. goto err;
  658. VERBOSE BIO_printf(bio_err, "srpvfile updated.\n");
  659. }
  660. ret = (errors != 0);
  661. err:
  662. if (errors != 0)
  663. VERBOSE BIO_printf(bio_err, "User errors %d.\n", errors);
  664. VERBOSE BIO_printf(bio_err, "SRP terminating with code %d.\n", ret);
  665. if (tofree)
  666. OPENSSL_free(tofree);
  667. if (ret)
  668. ERR_print_errors(bio_err);
  669. if (randfile)
  670. app_RAND_write_file(randfile, bio_err);
  671. if (conf)
  672. NCONF_free(conf);
  673. if (db)
  674. free_index(db);
  675. release_engine(e);
  676. OBJ_cleanup();
  677. apps_shutdown();
  678. OPENSSL_EXIT(ret);
  679. }
  680. #else
  681. static void *dummy = &dummy;
  682. #endif