2
0

srp.c 20 KB

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