2 * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
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
10 * Originally written by Christophe Renou and Peter Sylvester,
11 * for the EdelKey project.
14 #include <openssl/opensslconf.h>
16 NON_EMPTY_TRANSLATION_UNIT
22 # include <openssl/conf.h>
23 # include <openssl/bio.h>
24 # include <openssl/err.h>
25 # include <openssl/txt_db.h>
26 # include <openssl/buffer.h>
27 # include <openssl/srp.h>
31 # define BASE_SECTION "srp"
32 # define CONFIG_FILE "openssl.cnf"
35 # define ENV_DATABASE "srpvfile"
36 # define ENV_DEFAULT_SRP "default_srp"
38 static int get_index(CA_DB *db, char *id, char type)
44 if (type == DB_SRP_INDEX) {
45 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
46 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
47 if (pp[DB_srptype][0] == DB_SRP_INDEX
48 && strcmp(id, pp[DB_srpid]) == 0)
52 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
53 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
55 if (pp[DB_srptype][0] != DB_SRP_INDEX
56 && strcmp(id, pp[DB_srpid]) == 0)
64 static void print_entry(CA_DB *db, int indx, int verbose, char *s)
66 if (indx >= 0 && verbose) {
68 char **pp = sk_OPENSSL_PSTRING_value(db->db->data, indx);
69 BIO_printf(bio_err, "%s \"%s\"\n", s, pp[DB_srpid]);
70 for (j = 0; j < DB_NUMBER; j++) {
71 BIO_printf(bio_err, " %d = \"%s\"\n", j, pp[j]);
76 static void print_index(CA_DB *db, int indexindex, int verbose)
78 print_entry(db, indexindex, verbose, "g N entry");
81 static void print_user(CA_DB *db, int userindex, int verbose)
84 char **pp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);
86 if (pp[DB_srptype][0] != 'I') {
87 print_entry(db, userindex, verbose, "User entry");
88 print_entry(db, get_index(db, pp[DB_srpgN], 'I'), verbose,
95 static int update_index(CA_DB *db, char **row)
100 irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row pointers");
101 for (i = 0; i < DB_NUMBER; i++)
103 irow[DB_NUMBER] = NULL;
105 if (!TXT_DB_insert(db->db, irow)) {
106 BIO_printf(bio_err, "failed to update srpvfile\n");
107 BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
114 static char *lookup_conf(const CONF *conf, const char *section, const char *tag)
116 char *entry = NCONF_get_string(conf, section, tag);
118 BIO_printf(bio_err, "variable lookup failed for %s::%s\n", section, tag);
122 static char *srp_verify_user(const char *user, const char *srp_verifier,
123 char *srp_usersalt, const char *g, const char *N,
124 const char *passin, int verbose)
128 char *verifier = NULL;
132 cb_tmp.prompt_info = user;
133 cb_tmp.password = passin;
135 len = password_callback(password, sizeof(password)-1, 0, &cb_tmp);
140 "Validating\n user=\"%s\"\n srp_verifier=\"%s\"\n srp_usersalt=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
141 user, srp_verifier, srp_usersalt, g, N);
143 BIO_printf(bio_err, "Pass %s\n", password);
145 OPENSSL_assert(srp_usersalt != NULL);
146 if ((gNid = SRP_create_verifier(user, password, &srp_usersalt,
147 &verifier, N, g)) == NULL) {
148 BIO_printf(bio_err, "Internal error validating SRP verifier\n");
150 if (strcmp(verifier, srp_verifier))
152 OPENSSL_free(verifier);
154 OPENSSL_cleanse(password, len);
159 static char *srp_create_user(char *user, char **srp_verifier,
160 char **srp_usersalt, char *g, char *N,
161 char *passout, int verbose)
168 cb_tmp.prompt_info = user;
169 cb_tmp.password = passout;
171 len = password_callback(password, sizeof(password)-1, 1, &cb_tmp);
175 BIO_printf(bio_err, "Creating\n user=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
177 if ((gNid = SRP_create_verifier(user, password, &salt,
178 srp_verifier, N, g)) == NULL) {
179 BIO_printf(bio_err, "Internal error creating SRP verifier\n");
181 *srp_usersalt = salt;
183 OPENSSL_cleanse(password, len);
185 BIO_printf(bio_err, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n",
186 gNid, salt, *srp_verifier);
192 typedef enum OPTION_choice {
193 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
194 OPT_VERBOSE, OPT_CONFIG, OPT_NAME, OPT_SRPVFILE, OPT_ADD,
195 OPT_DELETE, OPT_MODIFY, OPT_LIST, OPT_GN, OPT_USERINFO,
196 OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE, OPT_R_ENUM
199 const OPTIONS srp_options[] = {
200 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [user...]\n"},
202 OPT_SECTION("General"),
203 {"help", OPT_HELP, '-', "Display this summary"},
204 {"verbose", OPT_VERBOSE, '-', "Talk a lot while doing things"},
205 {"config", OPT_CONFIG, '<', "A config file"},
206 {"name", OPT_NAME, 's', "The particular srp definition to use"},
207 # ifndef OPENSSL_NO_ENGINE
208 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
211 OPT_SECTION("Action"),
212 {"add", OPT_ADD, '-', "Add a user and srp verifier"},
213 {"modify", OPT_MODIFY, '-', "Modify the srp verifier of an existing user"},
214 {"delete", OPT_DELETE, '-', "Delete user from verifier file"},
215 {"list", OPT_LIST, '-', "List users"},
217 OPT_SECTION("Configuration"),
218 {"srpvfile", OPT_SRPVFILE, '<', "The srp verifier file name"},
219 {"gn", OPT_GN, 's', "Set g and N values to be used for new verifier"},
220 {"userinfo", OPT_USERINFO, 's', "Additional info to be set for user"},
221 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
222 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
227 {"user", 0, 0, "Username(s) to process (optional)"},
231 int srp_main(int argc, char **argv)
236 int gNindex = -1, maxgN = -1, ret = 1, errors = 0, verbose = 0, i;
237 int doupdatedb = 0, mode = OPT_ERR;
238 char *user = NULL, *passinarg = NULL, *passoutarg = NULL;
239 char *passin = NULL, *passout = NULL, *gN = NULL, *userinfo = NULL;
240 char *section = NULL;
241 char **gNrow = NULL, *configfile = NULL;
242 char *srpvfile = NULL, **pp, *prog;
245 prog = opt_init(argc, argv, srp_options);
246 while ((o = opt_next()) != OPT_EOF) {
251 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
254 opt_help(srp_options);
261 configfile = opt_arg();
267 srpvfile = opt_arg();
273 if (mode != OPT_ERR) {
275 "%s: Only one of -add/-delete/-modify/-list\n",
285 userinfo = opt_arg();
288 passinarg = opt_arg();
291 passoutarg = opt_arg();
294 e = setup_engine(opt_arg(), 0);
302 argc = opt_num_rest();
305 if (srpvfile != NULL && configfile != NULL) {
307 "-srpvfile and -configfile cannot be specified together.\n");
310 if (mode == OPT_ERR) {
312 "Exactly one of the options -add, -delete, -modify -list must be specified.\n");
315 if (mode == OPT_DELETE || mode == OPT_MODIFY || mode == OPT_ADD) {
317 BIO_printf(bio_err, "Need at least one user.\n");
322 if ((passinarg != NULL || passoutarg != NULL) && argc != 1) {
324 "-passin, -passout arguments only valid with one user.\n");
328 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
329 BIO_printf(bio_err, "Error getting passwords\n");
333 if (srpvfile == NULL) {
334 if (configfile == NULL)
335 configfile = default_config_file;
338 BIO_printf(bio_err, "Using configuration from %s\n",
340 conf = app_load_config(configfile);
343 if (configfile != default_config_file && !app_load_modules(conf))
346 /* Lets get the config section we are using */
347 if (section == NULL) {
350 "trying to read " ENV_DEFAULT_SRP
351 " in " BASE_SECTION "\n");
353 section = lookup_conf(conf, BASE_SECTION, ENV_DEFAULT_SRP);
358 app_RAND_load_conf(conf, BASE_SECTION);
362 "trying to read " ENV_DATABASE " in section \"%s\"\n",
365 srpvfile = lookup_conf(conf, section, ENV_DATABASE);
366 if (srpvfile == NULL)
371 BIO_printf(bio_err, "Trying to read SRP verifier file \"%s\"\n",
374 db = load_index(srpvfile, NULL);
378 /* Lets check some fields */
379 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
380 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
382 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
384 if ((gNindex < 0) && (gN != NULL) && strcmp(gN, pp[DB_srpid]) == 0)
387 print_index(db, i, verbose > 1);
392 BIO_printf(bio_err, "Database initialised\n");
395 gNrow = sk_OPENSSL_PSTRING_value(db->db->data, gNindex);
396 print_entry(db, gNindex, verbose > 1, "Default g and N");
397 } else if (maxgN > 0 && !SRP_get_default_gN(gN)) {
398 BIO_printf(bio_err, "No g and N value for index \"%s\"\n", gN);
402 BIO_printf(bio_err, "Database has no g N information.\n");
407 BIO_printf(bio_err, "Starting user processing\n");
409 while (mode == OPT_LIST || user != NULL) {
412 if (user != NULL && verbose > 1)
413 BIO_printf(bio_err, "Processing user \"%s\"\n", user);
414 if ((userindex = get_index(db, user, 'U')) >= 0)
415 print_user(db, userindex, (verbose > 0) || mode == OPT_LIST);
417 if (mode == OPT_LIST) {
419 BIO_printf(bio_err, "List all users\n");
421 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++)
422 print_user(db, i, 1);
423 } else if (userindex < 0) {
425 "user \"%s\" does not exist, ignored. t\n", user);
428 } else if (mode == OPT_ADD) {
429 if (userindex >= 0) {
430 /* reactivation of a new user */
432 sk_OPENSSL_PSTRING_value(db->db->data, userindex);
433 BIO_printf(bio_err, "user \"%s\" reactivated.\n", user);
434 row[DB_srptype][0] = 'V';
438 char *row[DB_NUMBER];
440 row[DB_srpverifier] = NULL;
441 row[DB_srpsalt] = NULL;
442 row[DB_srpinfo] = NULL;
445 srp_create_user(user, &(row[DB_srpverifier]),
447 gNrow ? gNrow[DB_srpsalt] : gN,
448 gNrow ? gNrow[DB_srpverifier] : NULL,
449 passout, verbose))) {
451 "Cannot create srp verifier for user \"%s\", operation abandoned .\n",
456 row[DB_srpid] = OPENSSL_strdup(user);
457 row[DB_srptype] = OPENSSL_strdup("v");
458 row[DB_srpgN] = OPENSSL_strdup(gNid);
460 if ((row[DB_srpid] == NULL)
461 || (row[DB_srpgN] == NULL)
462 || (row[DB_srptype] == NULL)
463 || (row[DB_srpverifier] == NULL)
464 || (row[DB_srpsalt] == NULL)
466 && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo)) == NULL))
467 || !update_index(db, row)) {
468 OPENSSL_free(row[DB_srpid]);
469 OPENSSL_free(row[DB_srpgN]);
470 OPENSSL_free(row[DB_srpinfo]);
471 OPENSSL_free(row[DB_srptype]);
472 OPENSSL_free(row[DB_srpverifier]);
473 OPENSSL_free(row[DB_srpsalt]);
478 } else if (mode == OPT_MODIFY) {
481 "user \"%s\" does not exist, operation ignored.\n",
487 sk_OPENSSL_PSTRING_value(db->db->data, userindex);
488 char type = row[DB_srptype][0];
491 "user \"%s\" already updated, operation ignored.\n",
497 if (row[DB_srptype][0] == 'V') {
502 "Verifying password for user \"%s\"\n",
505 get_index(db, row[DB_srpgN], DB_SRP_INDEX)) >= 0)
507 sk_OPENSSL_PSTRING_value(db->db->data,
511 (user, row[DB_srpverifier], row[DB_srpsalt],
512 irow ? irow[DB_srpsalt] : row[DB_srpgN],
513 irow ? irow[DB_srpverifier] : NULL, passin,
516 "Invalid password for user \"%s\", operation abandoned.\n",
523 BIO_printf(bio_err, "Password for user \"%s\" ok.\n",
528 srp_create_user(user, &(row[DB_srpverifier]),
530 gNrow ? gNrow[DB_srpsalt] : NULL,
531 gNrow ? gNrow[DB_srpverifier] : NULL,
532 passout, verbose))) {
534 "Cannot create srp verifier for user \"%s\", operation abandoned.\n",
540 row[DB_srptype][0] = 'v';
541 row[DB_srpgN] = OPENSSL_strdup(gNid);
543 if (row[DB_srpid] == NULL
544 || row[DB_srpgN] == NULL
545 || row[DB_srptype] == NULL
546 || row[DB_srpverifier] == NULL
547 || row[DB_srpsalt] == NULL
549 && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo))
556 } else if (mode == OPT_DELETE) {
559 "user \"%s\" does not exist, operation ignored. t\n",
563 char **xpp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);
565 BIO_printf(bio_err, "user \"%s\" revoked. t\n", user);
566 xpp[DB_srptype][0] = 'R';
572 /* no more processing in any mode if no users left */
578 BIO_printf(bio_err, "User procession done.\n");
581 /* Lets check some fields */
582 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
583 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
585 if (pp[DB_srptype][0] == 'v') {
586 pp[DB_srptype][0] = 'V';
587 print_user(db, i, verbose);
592 BIO_printf(bio_err, "Trying to update srpvfile.\n");
593 if (!save_index(srpvfile, "new", db))
597 BIO_printf(bio_err, "Temporary srpvfile created.\n");
598 if (!rotate_index(srpvfile, "new", "old"))
602 BIO_printf(bio_err, "srpvfile updated.\n");
609 BIO_printf(bio_err, "User errors %d.\n", errors);
612 BIO_printf(bio_err, "SRP terminating with code %d.\n", ret);
614 OPENSSL_free(passin);
615 OPENSSL_free(passout);
617 ERR_print_errors(bio_err);