2 * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/opensslconf.h>
12 NON_EMPTY_TRANSLATION_UNIT
18 # include <openssl/conf.h>
19 # include <openssl/bio.h>
20 # include <openssl/err.h>
21 # include <openssl/txt_db.h>
22 # include <openssl/buffer.h>
23 # include <openssl/srp.h>
27 # define BASE_SECTION "srp"
28 # define CONFIG_FILE "openssl.cnf"
31 # define ENV_DATABASE "srpvfile"
32 # define ENV_DEFAULT_SRP "default_srp"
34 static int get_index(CA_DB *db, char *id, char type)
40 if (type == DB_SRP_INDEX) {
41 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
42 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
43 if (pp[DB_srptype][0] == DB_SRP_INDEX
44 && strcmp(id, pp[DB_srpid]) == 0)
48 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
49 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
51 if (pp[DB_srptype][0] != DB_SRP_INDEX
52 && strcmp(id, pp[DB_srpid]) == 0)
60 static void print_entry(CA_DB *db, int indx, int verbose, char *s)
62 if (indx >= 0 && verbose) {
64 char **pp = sk_OPENSSL_PSTRING_value(db->db->data, indx);
65 BIO_printf(bio_err, "%s \"%s\"\n", s, pp[DB_srpid]);
66 for (j = 0; j < DB_NUMBER; j++) {
67 BIO_printf(bio_err, " %d = \"%s\"\n", j, pp[j]);
72 static void print_index(CA_DB *db, int indexindex, int verbose)
74 print_entry(db, indexindex, verbose, "g N entry");
77 static void print_user(CA_DB *db, int userindex, int verbose)
80 char **pp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);
82 if (pp[DB_srptype][0] != 'I') {
83 print_entry(db, userindex, verbose, "User entry");
84 print_entry(db, get_index(db, pp[DB_srpgN], 'I'), verbose,
91 static int update_index(CA_DB *db, char **row)
96 irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row pointers");
97 for (i = 0; i < DB_NUMBER; i++)
99 irow[DB_NUMBER] = NULL;
101 if (!TXT_DB_insert(db->db, irow)) {
102 BIO_printf(bio_err, "failed to update srpvfile\n");
103 BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
110 static char *lookup_conf(const CONF *conf, const char *section, const char *tag)
112 char *entry = NCONF_get_string(conf, section, tag);
114 BIO_printf(bio_err, "variable lookup failed for %s::%s\n", section, tag);
118 static char *srp_verify_user(const char *user, const char *srp_verifier,
119 char *srp_usersalt, const char *g, const char *N,
120 const char *passin, int verbose)
124 char *verifier = NULL;
128 cb_tmp.prompt_info = user;
129 cb_tmp.password = passin;
131 len = password_callback(password, sizeof(password)-1, 0, &cb_tmp);
136 "Validating\n user=\"%s\"\n srp_verifier=\"%s\"\n srp_usersalt=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
137 user, srp_verifier, srp_usersalt, g, N);
139 BIO_printf(bio_err, "Pass %s\n", password);
141 OPENSSL_assert(srp_usersalt != NULL);
142 if ((gNid = SRP_create_verifier(user, password, &srp_usersalt,
143 &verifier, N, g)) == NULL) {
144 BIO_printf(bio_err, "Internal error validating SRP verifier\n");
146 if (strcmp(verifier, srp_verifier))
148 OPENSSL_free(verifier);
150 OPENSSL_cleanse(password, len);
155 static char *srp_create_user(char *user, char **srp_verifier,
156 char **srp_usersalt, char *g, char *N,
157 char *passout, int verbose)
164 cb_tmp.prompt_info = user;
165 cb_tmp.password = passout;
167 len = password_callback(password, sizeof(password)-1, 1, &cb_tmp);
171 BIO_printf(bio_err, "Creating\n user=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
173 if ((gNid = SRP_create_verifier(user, password, &salt,
174 srp_verifier, N, g)) == NULL) {
175 BIO_printf(bio_err, "Internal error creating SRP verifier\n");
177 *srp_usersalt = salt;
179 OPENSSL_cleanse(password, len);
181 BIO_printf(bio_err, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n",
182 gNid, salt, *srp_verifier);
188 typedef enum OPTION_choice {
189 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
190 OPT_VERBOSE, OPT_CONFIG, OPT_NAME, OPT_SRPVFILE, OPT_ADD,
191 OPT_DELETE, OPT_MODIFY, OPT_LIST, OPT_GN, OPT_USERINFO,
192 OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE, OPT_R_ENUM
195 const OPTIONS srp_options[] = {
196 {"help", OPT_HELP, '-', "Display this summary"},
197 {"verbose", OPT_VERBOSE, '-', "Talk a lot while doing things"},
198 {"config", OPT_CONFIG, '<', "A config file"},
199 {"name", OPT_NAME, 's', "The particular srp definition to use"},
200 {"srpvfile", OPT_SRPVFILE, '<', "The srp verifier file name"},
201 {"add", OPT_ADD, '-', "Add a user and srp verifier"},
202 {"modify", OPT_MODIFY, '-',
203 "Modify the srp verifier of an existing user"},
204 {"delete", OPT_DELETE, '-', "Delete user from verifier file"},
205 {"list", OPT_LIST, '-', "List users"},
206 {"gn", OPT_GN, 's', "Set g and N values to be used for new verifier"},
207 {"userinfo", OPT_USERINFO, 's', "Additional info to be set for user"},
208 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
209 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
211 # ifndef OPENSSL_NO_ENGINE
212 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
217 int srp_main(int argc, char **argv)
222 int gNindex = -1, maxgN = -1, ret = 1, errors = 0, verbose = 0, i;
223 int doupdatedb = 0, mode = OPT_ERR;
224 char *user = NULL, *passinarg = NULL, *passoutarg = NULL;
225 char *passin = NULL, *passout = NULL, *gN = NULL, *userinfo = NULL;
226 char *section = NULL;
227 char **gNrow = NULL, *configfile = NULL;
228 char *srpvfile = NULL, **pp, *prog;
231 prog = opt_init(argc, argv, srp_options);
232 while ((o = opt_next()) != OPT_EOF) {
237 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
240 opt_help(srp_options);
247 configfile = opt_arg();
253 srpvfile = opt_arg();
259 if (mode != OPT_ERR) {
261 "%s: Only one of -add/-delete/-modify/-list\n",
271 userinfo = opt_arg();
274 passinarg = opt_arg();
277 passoutarg = opt_arg();
280 e = setup_engine(opt_arg(), 0);
288 argc = opt_num_rest();
291 if (srpvfile != NULL && configfile != NULL) {
293 "-srpvfile and -configfile cannot be specified together.\n");
296 if (mode == OPT_ERR) {
298 "Exactly one of the options -add, -delete, -modify -list must be specified.\n");
301 if (mode == OPT_DELETE || mode == OPT_MODIFY || mode == OPT_ADD) {
303 BIO_printf(bio_err, "Need at least one user.\n");
308 if ((passinarg != NULL || passoutarg != NULL) && argc != 1) {
310 "-passin, -passout arguments only valid with one user.\n");
314 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
315 BIO_printf(bio_err, "Error getting passwords\n");
319 if (srpvfile == NULL) {
320 if (configfile == NULL)
321 configfile = default_config_file;
324 BIO_printf(bio_err, "Using configuration from %s\n",
326 conf = app_load_config(configfile);
329 if (configfile != default_config_file && !app_load_modules(conf))
332 /* Lets get the config section we are using */
333 if (section == NULL) {
336 "trying to read " ENV_DEFAULT_SRP
337 " in " BASE_SECTION "\n");
339 section = lookup_conf(conf, BASE_SECTION, ENV_DEFAULT_SRP);
344 app_RAND_load_conf(conf, BASE_SECTION);
348 "trying to read " ENV_DATABASE " in section \"%s\"\n",
351 srpvfile = lookup_conf(conf, section, ENV_DATABASE);
352 if (srpvfile == NULL)
357 BIO_printf(bio_err, "Trying to read SRP verifier file \"%s\"\n",
360 db = load_index(srpvfile, NULL);
364 /* Lets check some fields */
365 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
366 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
368 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
370 if ((gNindex < 0) && (gN != NULL) && strcmp(gN, pp[DB_srpid]) == 0)
373 print_index(db, i, verbose > 1);
378 BIO_printf(bio_err, "Database initialised\n");
381 gNrow = sk_OPENSSL_PSTRING_value(db->db->data, gNindex);
382 print_entry(db, gNindex, verbose > 1, "Default g and N");
383 } else if (maxgN > 0 && !SRP_get_default_gN(gN)) {
384 BIO_printf(bio_err, "No g and N value for index \"%s\"\n", gN);
388 BIO_printf(bio_err, "Database has no g N information.\n");
393 BIO_printf(bio_err, "Starting user processing\n");
395 while (mode == OPT_LIST || user != NULL) {
398 if (user != NULL && verbose > 1)
399 BIO_printf(bio_err, "Processing user \"%s\"\n", user);
400 if ((userindex = get_index(db, user, 'U')) >= 0)
401 print_user(db, userindex, (verbose > 0) || mode == OPT_LIST);
403 if (mode == OPT_LIST) {
405 BIO_printf(bio_err, "List all users\n");
407 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++)
408 print_user(db, i, 1);
409 } else if (userindex < 0) {
411 "user \"%s\" does not exist, ignored. t\n", user);
414 } else if (mode == OPT_ADD) {
415 if (userindex >= 0) {
416 /* reactivation of a new user */
418 sk_OPENSSL_PSTRING_value(db->db->data, userindex);
419 BIO_printf(bio_err, "user \"%s\" reactivated.\n", user);
420 row[DB_srptype][0] = 'V';
424 char *row[DB_NUMBER];
426 row[DB_srpverifier] = NULL;
427 row[DB_srpsalt] = NULL;
428 row[DB_srpinfo] = NULL;
431 srp_create_user(user, &(row[DB_srpverifier]),
433 gNrow ? gNrow[DB_srpsalt] : gN,
434 gNrow ? gNrow[DB_srpverifier] : NULL,
435 passout, verbose))) {
437 "Cannot create srp verifier for user \"%s\", operation abandoned .\n",
442 row[DB_srpid] = OPENSSL_strdup(user);
443 row[DB_srptype] = OPENSSL_strdup("v");
444 row[DB_srpgN] = OPENSSL_strdup(gNid);
446 if ((row[DB_srpid] == NULL)
447 || (row[DB_srpgN] == NULL)
448 || (row[DB_srptype] == NULL)
449 || (row[DB_srpverifier] == NULL)
450 || (row[DB_srpsalt] == NULL)
452 && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo)) == NULL))
453 || !update_index(db, row)) {
454 OPENSSL_free(row[DB_srpid]);
455 OPENSSL_free(row[DB_srpgN]);
456 OPENSSL_free(row[DB_srpinfo]);
457 OPENSSL_free(row[DB_srptype]);
458 OPENSSL_free(row[DB_srpverifier]);
459 OPENSSL_free(row[DB_srpsalt]);
464 } else if (mode == OPT_MODIFY) {
467 "user \"%s\" does not exist, operation ignored.\n",
473 sk_OPENSSL_PSTRING_value(db->db->data, userindex);
474 char type = row[DB_srptype][0];
477 "user \"%s\" already updated, operation ignored.\n",
483 if (row[DB_srptype][0] == 'V') {
488 "Verifying password for user \"%s\"\n",
491 get_index(db, row[DB_srpgN], DB_SRP_INDEX)) >= 0)
493 sk_OPENSSL_PSTRING_value(db->db->data,
497 (user, row[DB_srpverifier], row[DB_srpsalt],
498 irow ? irow[DB_srpsalt] : row[DB_srpgN],
499 irow ? irow[DB_srpverifier] : NULL, passin,
502 "Invalid password for user \"%s\", operation abandoned.\n",
509 BIO_printf(bio_err, "Password for user \"%s\" ok.\n",
514 srp_create_user(user, &(row[DB_srpverifier]),
516 gNrow ? gNrow[DB_srpsalt] : NULL,
517 gNrow ? gNrow[DB_srpverifier] : NULL,
518 passout, verbose))) {
520 "Cannot create srp verifier for user \"%s\", operation abandoned.\n",
526 row[DB_srptype][0] = 'v';
527 row[DB_srpgN] = OPENSSL_strdup(gNid);
529 if (row[DB_srpid] == NULL
530 || row[DB_srpgN] == NULL
531 || row[DB_srptype] == NULL
532 || row[DB_srpverifier] == NULL
533 || row[DB_srpsalt] == NULL
535 && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo))
542 } else if (mode == OPT_DELETE) {
545 "user \"%s\" does not exist, operation ignored. t\n",
549 char **xpp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);
551 BIO_printf(bio_err, "user \"%s\" revoked. t\n", user);
552 xpp[DB_srptype][0] = 'R';
558 /* no more processing in any mode if no users left */
564 BIO_printf(bio_err, "User procession done.\n");
567 /* Lets check some fields */
568 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
569 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
571 if (pp[DB_srptype][0] == 'v') {
572 pp[DB_srptype][0] = 'V';
573 print_user(db, i, verbose);
578 BIO_printf(bio_err, "Trying to update srpvfile.\n");
579 if (!save_index(srpvfile, "new", db))
583 BIO_printf(bio_err, "Temporary srpvfile created.\n");
584 if (!rotate_index(srpvfile, "new", "old"))
588 BIO_printf(bio_err, "srpvfile updated.\n");
595 BIO_printf(bio_err, "User errors %d.\n", errors);
598 BIO_printf(bio_err, "SRP terminating with code %d.\n", ret);
600 OPENSSL_free(passin);
601 OPENSSL_free(passout);
603 ERR_print_errors(bio_err);