srp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 OpenSSL license (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. #include <openssl/opensslconf.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <openssl/conf.h>
  18. #include <openssl/bio.h>
  19. #include <openssl/err.h>
  20. #include <openssl/txt_db.h>
  21. #include <openssl/buffer.h>
  22. #include <openssl/srp.h>
  23. #include "apps.h"
  24. #include "progs.h"
  25. #define BASE_SECTION "srp"
  26. #define CONFIG_FILE "openssl.cnf"
  27. #define ENV_DATABASE "srpvfile"
  28. #define ENV_DEFAULT_SRP "default_srp"
  29. static int get_index(CA_DB *db, char *id, char type)
  30. {
  31. char **pp;
  32. int i;
  33. if (id == NULL)
  34. return -1;
  35. if (type == DB_SRP_INDEX) {
  36. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  37. pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
  38. if (pp[DB_srptype][0] == DB_SRP_INDEX
  39. && strcmp(id, pp[DB_srpid]) == 0)
  40. return i;
  41. }
  42. } else {
  43. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  44. pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
  45. if (pp[DB_srptype][0] != DB_SRP_INDEX
  46. && strcmp(id, pp[DB_srpid]) == 0)
  47. return i;
  48. }
  49. }
  50. return -1;
  51. }
  52. static void print_entry(CA_DB *db, int indx, int verbose, char *s)
  53. {
  54. if (indx >= 0 && verbose) {
  55. int j;
  56. char **pp = sk_OPENSSL_PSTRING_value(db->db->data, indx);
  57. BIO_printf(bio_err, "%s \"%s\"\n", s, pp[DB_srpid]);
  58. for (j = 0; j < DB_NUMBER; j++) {
  59. BIO_printf(bio_err, " %d = \"%s\"\n", j, pp[j]);
  60. }
  61. }
  62. }
  63. static void print_index(CA_DB *db, int indexindex, int verbose)
  64. {
  65. print_entry(db, indexindex, verbose, "g N entry");
  66. }
  67. static void print_user(CA_DB *db, int userindex, int verbose)
  68. {
  69. if (verbose > 0) {
  70. char **pp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);
  71. if (pp[DB_srptype][0] != 'I') {
  72. print_entry(db, userindex, verbose, "User entry");
  73. print_entry(db, get_index(db, pp[DB_srpgN], 'I'), verbose,
  74. "g N entry");
  75. }
  76. }
  77. }
  78. static int update_index(CA_DB *db, char **row)
  79. {
  80. char **irow;
  81. int i;
  82. irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row pointers");
  83. for (i = 0; i < DB_NUMBER; i++)
  84. irow[i] = row[i];
  85. irow[DB_NUMBER] = NULL;
  86. if (!TXT_DB_insert(db->db, irow)) {
  87. BIO_printf(bio_err, "failed to update srpvfile\n");
  88. BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
  89. OPENSSL_free(irow);
  90. return 0;
  91. }
  92. return 1;
  93. }
  94. static char *lookup_conf(const CONF *conf, const char *section, const char *tag)
  95. {
  96. char *entry = NCONF_get_string(conf, section, tag);
  97. if (entry == NULL)
  98. BIO_printf(bio_err, "variable lookup failed for %s::%s\n", section, tag);
  99. return entry;
  100. }
  101. static char *srp_verify_user(const char *user, const char *srp_verifier,
  102. char *srp_usersalt, const char *g, const char *N,
  103. const char *passin, int verbose)
  104. {
  105. char password[1025];
  106. PW_CB_DATA cb_tmp;
  107. char *verifier = NULL;
  108. char *gNid = NULL;
  109. int len;
  110. cb_tmp.prompt_info = user;
  111. cb_tmp.password = passin;
  112. len = password_callback(password, sizeof(password)-1, 0, &cb_tmp);
  113. if (len > 0) {
  114. password[len] = 0;
  115. if (verbose)
  116. BIO_printf(bio_err,
  117. "Validating\n user=\"%s\"\n srp_verifier=\"%s\"\n srp_usersalt=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
  118. user, srp_verifier, srp_usersalt, g, N);
  119. if (verbose > 1)
  120. BIO_printf(bio_err, "Pass %s\n", password);
  121. OPENSSL_assert(srp_usersalt != NULL);
  122. if ((gNid = SRP_create_verifier(user, password, &srp_usersalt,
  123. &verifier, N, g)) == NULL) {
  124. BIO_printf(bio_err, "Internal error validating SRP verifier\n");
  125. } else {
  126. if (strcmp(verifier, srp_verifier))
  127. gNid = NULL;
  128. OPENSSL_free(verifier);
  129. }
  130. OPENSSL_cleanse(password, len);
  131. }
  132. return gNid;
  133. }
  134. static char *srp_create_user(char *user, char **srp_verifier,
  135. char **srp_usersalt, char *g, char *N,
  136. char *passout, int verbose)
  137. {
  138. char password[1025];
  139. PW_CB_DATA cb_tmp;
  140. char *gNid = NULL;
  141. char *salt = NULL;
  142. int len;
  143. cb_tmp.prompt_info = user;
  144. cb_tmp.password = passout;
  145. len = password_callback(password, sizeof(password)-1, 1, &cb_tmp);
  146. if (len > 0) {
  147. password[len] = 0;
  148. if (verbose)
  149. BIO_printf(bio_err, "Creating\n user=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
  150. user, g, N);
  151. if ((gNid = SRP_create_verifier(user, password, &salt,
  152. srp_verifier, N, g)) == NULL) {
  153. BIO_printf(bio_err, "Internal error creating SRP verifier\n");
  154. } else {
  155. *srp_usersalt = salt;
  156. }
  157. OPENSSL_cleanse(password, len);
  158. if (verbose > 1)
  159. BIO_printf(bio_err, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n",
  160. gNid, salt, *srp_verifier);
  161. }
  162. return gNid;
  163. }
  164. typedef enum OPTION_choice {
  165. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  166. OPT_VERBOSE, OPT_CONFIG, OPT_NAME, OPT_SRPVFILE, OPT_ADD,
  167. OPT_DELETE, OPT_MODIFY, OPT_LIST, OPT_GN, OPT_USERINFO,
  168. OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE, OPT_R_ENUM
  169. } OPTION_CHOICE;
  170. const OPTIONS srp_options[] = {
  171. {"help", OPT_HELP, '-', "Display this summary"},
  172. {"verbose", OPT_VERBOSE, '-', "Talk a lot while doing things"},
  173. {"config", OPT_CONFIG, '<', "A config file"},
  174. {"name", OPT_NAME, 's', "The particular srp definition to use"},
  175. {"srpvfile", OPT_SRPVFILE, '<', "The srp verifier file name"},
  176. {"add", OPT_ADD, '-', "Add a user and srp verifier"},
  177. {"modify", OPT_MODIFY, '-',
  178. "Modify the srp verifier of an existing user"},
  179. {"delete", OPT_DELETE, '-', "Delete user from verifier file"},
  180. {"list", OPT_LIST, '-', "List users"},
  181. {"gn", OPT_GN, 's', "Set g and N values to be used for new verifier"},
  182. {"userinfo", OPT_USERINFO, 's', "Additional info to be set for user"},
  183. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  184. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  185. OPT_R_OPTIONS,
  186. #ifndef OPENSSL_NO_ENGINE
  187. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  188. #endif
  189. {NULL}
  190. };
  191. int srp_main(int argc, char **argv)
  192. {
  193. ENGINE *e = NULL;
  194. CA_DB *db = NULL;
  195. CONF *conf = NULL;
  196. int gNindex = -1, maxgN = -1, ret = 1, errors = 0, verbose = 0, i;
  197. int doupdatedb = 0, mode = OPT_ERR;
  198. char *user = NULL, *passinarg = NULL, *passoutarg = NULL;
  199. char *passin = NULL, *passout = NULL, *gN = NULL, *userinfo = NULL;
  200. char *section = NULL;
  201. char **gNrow = NULL, *configfile = NULL;
  202. char *srpvfile = NULL, **pp, *prog;
  203. OPTION_CHOICE o;
  204. prog = opt_init(argc, argv, srp_options);
  205. while ((o = opt_next()) != OPT_EOF) {
  206. switch (o) {
  207. case OPT_EOF:
  208. case OPT_ERR:
  209. opthelp:
  210. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  211. goto end;
  212. case OPT_HELP:
  213. opt_help(srp_options);
  214. ret = 0;
  215. goto end;
  216. case OPT_VERBOSE:
  217. verbose++;
  218. break;
  219. case OPT_CONFIG:
  220. configfile = opt_arg();
  221. break;
  222. case OPT_NAME:
  223. section = opt_arg();
  224. break;
  225. case OPT_SRPVFILE:
  226. srpvfile = opt_arg();
  227. break;
  228. case OPT_ADD:
  229. case OPT_DELETE:
  230. case OPT_MODIFY:
  231. case OPT_LIST:
  232. if (mode != OPT_ERR) {
  233. BIO_printf(bio_err,
  234. "%s: Only one of -add/-delete/-modify/-list\n",
  235. prog);
  236. goto opthelp;
  237. }
  238. mode = o;
  239. break;
  240. case OPT_GN:
  241. gN = opt_arg();
  242. break;
  243. case OPT_USERINFO:
  244. userinfo = opt_arg();
  245. break;
  246. case OPT_PASSIN:
  247. passinarg = opt_arg();
  248. break;
  249. case OPT_PASSOUT:
  250. passoutarg = opt_arg();
  251. break;
  252. case OPT_ENGINE:
  253. e = setup_engine(opt_arg(), 0);
  254. break;
  255. case OPT_R_CASES:
  256. if (!opt_rand(o))
  257. goto end;
  258. break;
  259. }
  260. }
  261. argc = opt_num_rest();
  262. argv = opt_rest();
  263. if (srpvfile != NULL && configfile != NULL) {
  264. BIO_printf(bio_err,
  265. "-srpvfile and -configfile cannot be specified together.\n");
  266. goto end;
  267. }
  268. if (mode == OPT_ERR) {
  269. BIO_printf(bio_err,
  270. "Exactly one of the options -add, -delete, -modify -list must be specified.\n");
  271. goto opthelp;
  272. }
  273. if (mode == OPT_DELETE || mode == OPT_MODIFY || mode == OPT_ADD) {
  274. if (argc == 0) {
  275. BIO_printf(bio_err, "Need at least one user.\n");
  276. goto opthelp;
  277. }
  278. user = *argv++;
  279. }
  280. if ((passinarg != NULL || passoutarg != NULL) && argc != 1) {
  281. BIO_printf(bio_err,
  282. "-passin, -passout arguments only valid with one user.\n");
  283. goto opthelp;
  284. }
  285. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  286. BIO_printf(bio_err, "Error getting passwords\n");
  287. goto end;
  288. }
  289. if (srpvfile == NULL) {
  290. if (configfile == NULL)
  291. configfile = default_config_file;
  292. if (verbose)
  293. BIO_printf(bio_err, "Using configuration from %s\n",
  294. configfile);
  295. conf = app_load_config(configfile);
  296. if (conf == NULL)
  297. goto end;
  298. if (configfile != default_config_file && !app_load_modules(conf))
  299. goto end;
  300. /* Lets get the config section we are using */
  301. if (section == NULL) {
  302. if (verbose)
  303. BIO_printf(bio_err,
  304. "trying to read " ENV_DEFAULT_SRP
  305. " in " BASE_SECTION "\n");
  306. section = lookup_conf(conf, BASE_SECTION, ENV_DEFAULT_SRP);
  307. if (section == NULL)
  308. goto end;
  309. }
  310. app_RAND_load_conf(conf, BASE_SECTION);
  311. if (verbose)
  312. BIO_printf(bio_err,
  313. "trying to read " ENV_DATABASE " in section \"%s\"\n",
  314. section);
  315. srpvfile = lookup_conf(conf, section, ENV_DATABASE);
  316. if (srpvfile == NULL)
  317. goto end;
  318. }
  319. if (verbose)
  320. BIO_printf(bio_err, "Trying to read SRP verifier file \"%s\"\n",
  321. srpvfile);
  322. db = load_index(srpvfile, NULL);
  323. if (db == NULL)
  324. goto end;
  325. /* Lets check some fields */
  326. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  327. pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
  328. if (pp[DB_srptype][0] == DB_SRP_INDEX) {
  329. maxgN = i;
  330. if ((gNindex < 0) && (gN != NULL) && strcmp(gN, pp[DB_srpid]) == 0)
  331. gNindex = i;
  332. print_index(db, i, verbose > 1);
  333. }
  334. }
  335. if (verbose)
  336. BIO_printf(bio_err, "Database initialised\n");
  337. if (gNindex >= 0) {
  338. gNrow = sk_OPENSSL_PSTRING_value(db->db->data, gNindex);
  339. print_entry(db, gNindex, verbose > 1, "Default g and N");
  340. } else if (maxgN > 0 && !SRP_get_default_gN(gN)) {
  341. BIO_printf(bio_err, "No g and N value for index \"%s\"\n", gN);
  342. goto end;
  343. } else {
  344. if (verbose)
  345. BIO_printf(bio_err, "Database has no g N information.\n");
  346. gNrow = NULL;
  347. }
  348. if (verbose > 1)
  349. BIO_printf(bio_err, "Starting user processing\n");
  350. while (mode == OPT_LIST || user != NULL) {
  351. int userindex = -1;
  352. if (user != NULL && verbose > 1)
  353. BIO_printf(bio_err, "Processing user \"%s\"\n", user);
  354. if ((userindex = get_index(db, user, 'U')) >= 0)
  355. print_user(db, userindex, (verbose > 0) || mode == OPT_LIST);
  356. if (mode == OPT_LIST) {
  357. if (user == NULL) {
  358. BIO_printf(bio_err, "List all users\n");
  359. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++)
  360. print_user(db, i, 1);
  361. } else if (userindex < 0) {
  362. BIO_printf(bio_err,
  363. "user \"%s\" does not exist, ignored. t\n", user);
  364. errors++;
  365. }
  366. } else if (mode == OPT_ADD) {
  367. if (userindex >= 0) {
  368. /* reactivation of a new user */
  369. char **row =
  370. sk_OPENSSL_PSTRING_value(db->db->data, userindex);
  371. BIO_printf(bio_err, "user \"%s\" reactivated.\n", user);
  372. row[DB_srptype][0] = 'V';
  373. doupdatedb = 1;
  374. } else {
  375. char *row[DB_NUMBER];
  376. char *gNid;
  377. row[DB_srpverifier] = NULL;
  378. row[DB_srpsalt] = NULL;
  379. row[DB_srpinfo] = NULL;
  380. if (!
  381. (gNid =
  382. srp_create_user(user, &(row[DB_srpverifier]),
  383. &(row[DB_srpsalt]),
  384. gNrow ? gNrow[DB_srpsalt] : gN,
  385. gNrow ? gNrow[DB_srpverifier] : NULL,
  386. passout, verbose))) {
  387. BIO_printf(bio_err,
  388. "Cannot create srp verifier for user \"%s\", operation abandoned .\n",
  389. user);
  390. errors++;
  391. goto end;
  392. }
  393. row[DB_srpid] = OPENSSL_strdup(user);
  394. row[DB_srptype] = OPENSSL_strdup("v");
  395. row[DB_srpgN] = OPENSSL_strdup(gNid);
  396. if ((row[DB_srpid] == NULL)
  397. || (row[DB_srpgN] == NULL)
  398. || (row[DB_srptype] == NULL)
  399. || (row[DB_srpverifier] == NULL)
  400. || (row[DB_srpsalt] == NULL)
  401. || (userinfo
  402. && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo)) == NULL))
  403. || !update_index(db, row)) {
  404. OPENSSL_free(row[DB_srpid]);
  405. OPENSSL_free(row[DB_srpgN]);
  406. OPENSSL_free(row[DB_srpinfo]);
  407. OPENSSL_free(row[DB_srptype]);
  408. OPENSSL_free(row[DB_srpverifier]);
  409. OPENSSL_free(row[DB_srpsalt]);
  410. goto end;
  411. }
  412. doupdatedb = 1;
  413. }
  414. } else if (mode == OPT_MODIFY) {
  415. if (userindex < 0) {
  416. BIO_printf(bio_err,
  417. "user \"%s\" does not exist, operation ignored.\n",
  418. user);
  419. errors++;
  420. } else {
  421. char **row =
  422. sk_OPENSSL_PSTRING_value(db->db->data, userindex);
  423. char type = row[DB_srptype][0];
  424. if (type == 'v') {
  425. BIO_printf(bio_err,
  426. "user \"%s\" already updated, operation ignored.\n",
  427. user);
  428. errors++;
  429. } else {
  430. char *gNid;
  431. if (row[DB_srptype][0] == 'V') {
  432. int user_gN;
  433. char **irow = NULL;
  434. if (verbose)
  435. BIO_printf(bio_err,
  436. "Verifying password for user \"%s\"\n",
  437. user);
  438. if ((user_gN =
  439. get_index(db, row[DB_srpgN], DB_SRP_INDEX)) >= 0)
  440. irow =
  441. sk_OPENSSL_PSTRING_value(db->db->data,
  442. userindex);
  443. if (!srp_verify_user
  444. (user, row[DB_srpverifier], row[DB_srpsalt],
  445. irow ? irow[DB_srpsalt] : row[DB_srpgN],
  446. irow ? irow[DB_srpverifier] : NULL, passin,
  447. verbose)) {
  448. BIO_printf(bio_err,
  449. "Invalid password for user \"%s\", operation abandoned.\n",
  450. user);
  451. errors++;
  452. goto end;
  453. }
  454. }
  455. if (verbose)
  456. BIO_printf(bio_err, "Password for user \"%s\" ok.\n",
  457. user);
  458. if (!
  459. (gNid =
  460. srp_create_user(user, &(row[DB_srpverifier]),
  461. &(row[DB_srpsalt]),
  462. gNrow ? gNrow[DB_srpsalt] : NULL,
  463. gNrow ? gNrow[DB_srpverifier] : NULL,
  464. passout, verbose))) {
  465. BIO_printf(bio_err,
  466. "Cannot create srp verifier for user \"%s\", operation abandoned.\n",
  467. user);
  468. errors++;
  469. goto end;
  470. }
  471. row[DB_srptype][0] = 'v';
  472. row[DB_srpgN] = OPENSSL_strdup(gNid);
  473. if (row[DB_srpid] == NULL
  474. || row[DB_srpgN] == NULL
  475. || row[DB_srptype] == NULL
  476. || row[DB_srpverifier] == NULL
  477. || row[DB_srpsalt] == NULL
  478. || (userinfo
  479. && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo))
  480. == NULL)))
  481. goto end;
  482. doupdatedb = 1;
  483. }
  484. }
  485. } else if (mode == OPT_DELETE) {
  486. if (userindex < 0) {
  487. BIO_printf(bio_err,
  488. "user \"%s\" does not exist, operation ignored. t\n",
  489. user);
  490. errors++;
  491. } else {
  492. char **xpp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);
  493. BIO_printf(bio_err, "user \"%s\" revoked. t\n", user);
  494. xpp[DB_srptype][0] = 'R';
  495. doupdatedb = 1;
  496. }
  497. }
  498. user = *argv++;
  499. if (user == NULL) {
  500. /* no more processing in any mode if no users left */
  501. break;
  502. }
  503. }
  504. if (verbose)
  505. BIO_printf(bio_err, "User procession done.\n");
  506. if (doupdatedb) {
  507. /* Lets check some fields */
  508. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  509. pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
  510. if (pp[DB_srptype][0] == 'v') {
  511. pp[DB_srptype][0] = 'V';
  512. print_user(db, i, verbose);
  513. }
  514. }
  515. if (verbose)
  516. BIO_printf(bio_err, "Trying to update srpvfile.\n");
  517. if (!save_index(srpvfile, "new", db))
  518. goto end;
  519. if (verbose)
  520. BIO_printf(bio_err, "Temporary srpvfile created.\n");
  521. if (!rotate_index(srpvfile, "new", "old"))
  522. goto end;
  523. if (verbose)
  524. BIO_printf(bio_err, "srpvfile updated.\n");
  525. }
  526. ret = (errors != 0);
  527. end:
  528. if (errors != 0)
  529. if (verbose)
  530. BIO_printf(bio_err, "User errors %d.\n", errors);
  531. if (verbose)
  532. BIO_printf(bio_err, "SRP terminating with code %d.\n", ret);
  533. OPENSSL_free(passin);
  534. OPENSSL_free(passout);
  535. if (ret)
  536. ERR_print_errors(bio_err);
  537. NCONF_free(conf);
  538. free_index(db);
  539. release_engine(e);
  540. return ret;
  541. }