2 * Written by Peter Sylvester (peter.sylvester@edelweb.fr) for the EdelKey
3 * project and contributed to the OpenSSL project 2004.
5 /* ====================================================================
6 * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
58 #include <openssl/opensslconf.h>
60 #ifndef OPENSSL_NO_SRP
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>
73 # define BASE_SECTION "srp"
74 # define CONFIG_FILE "openssl.cnf"
76 # define ENV_RANDFILE "RANDFILE"
78 # define ENV_DATABASE "srpvfile"
79 # define ENV_DEFAULT_SRP "default_srp"
81 static int get_index(CA_DB *db, char *id, char type)
87 if (type == DB_SRP_INDEX)
88 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
89 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
90 if (pp[DB_srptype][0] == DB_SRP_INDEX
91 && !strcmp(id, pp[DB_srpid]))
94 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
95 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
97 if (pp[DB_srptype][0] != DB_SRP_INDEX
98 && !strcmp(id, pp[DB_srpid]))
105 static void print_entry(CA_DB *db, BIO *bio, int indx, int verbose, char *s)
107 if (indx >= 0 && verbose) {
109 char **pp = sk_OPENSSL_PSTRING_value(db->db->data, indx);
110 BIO_printf(bio, "%s \"%s\"\n", s, pp[DB_srpid]);
111 for (j = 0; j < DB_NUMBER; j++) {
112 BIO_printf(bio_err, " %d = \"%s\"\n", j, pp[j]);
117 static void print_index(CA_DB *db, BIO *bio, int indexindex, int verbose)
119 print_entry(db, bio, indexindex, verbose, "g N entry");
122 static void print_user(CA_DB *db, BIO *bio, int userindex, int verbose)
125 char **pp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);
127 if (pp[DB_srptype][0] != 'I') {
128 print_entry(db, bio, userindex, verbose, "User entry");
129 print_entry(db, bio, get_index(db, pp[DB_srpgN], 'I'), verbose,
136 static int update_index(CA_DB *db, BIO *bio, char **row)
142 (char **)OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) == NULL) {
143 BIO_printf(bio_err, "Memory allocation failure\n");
147 for (i = 0; i < DB_NUMBER; i++) {
151 irow[DB_NUMBER] = NULL;
153 if (!TXT_DB_insert(db->db, irow)) {
154 BIO_printf(bio, "failed to update srpvfile\n");
155 BIO_printf(bio, "TXT_DB error number %ld\n", db->db->error);
162 static void lookup_fail(const char *name, const char *tag)
164 BIO_printf(bio_err, "variable lookup failed for %s::%s\n", name, tag);
167 static char *srp_verify_user(const char *user, const char *srp_verifier,
168 char *srp_usersalt, const char *g, const char *N,
169 const char *passin, BIO *bio, int verbose)
173 char *verifier = NULL;
176 cb_tmp.prompt_info = user;
177 cb_tmp.password = passin;
179 if (password_callback(password, 1024, 0, &cb_tmp) > 0) {
182 "Validating\n user=\"%s\"\n srp_verifier=\"%s\"\n srp_usersalt=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
183 user, srp_verifier, srp_usersalt, g, N);
184 BIO_printf(bio, "Pass %s\n", password);
186 OPENSSL_assert(srp_usersalt != NULL);
189 SRP_create_verifier(user, password, &srp_usersalt, &verifier, N,
191 BIO_printf(bio, "Internal error validating SRP verifier\n");
193 if (strcmp(verifier, srp_verifier))
195 OPENSSL_free(verifier);
201 static char *srp_create_user(char *user, char **srp_verifier,
202 char **srp_usersalt, char *g, char *N,
203 char *passout, BIO *bio, int verbose)
209 cb_tmp.prompt_info = user;
210 cb_tmp.password = passout;
212 if (password_callback(password, 1024, 1, &cb_tmp) > 0) {
214 BIO_printf(bio, "Creating\n user=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
218 SRP_create_verifier(user, password, &salt, srp_verifier, N,
220 BIO_printf(bio, "Internal error creating SRP verifier\n");
222 *srp_usersalt = salt;
224 BIO_printf(bio, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n", gNid,
225 salt, *srp_verifier);
231 typedef enum OPTION_choice {
232 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
233 OPT_VERBOSE, OPT_CONFIG, OPT_NAME, OPT_SRPVFILE, OPT_ADD,
234 OPT_DELETE, OPT_MODIFY, OPT_LIST, OPT_GN, OPT_USERINFO,
235 OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE
238 OPTIONS srp_options[] = {
239 {"help", OPT_HELP, '-', "Display this summary"},
240 {"verbose", OPT_VERBOSE, '-', "Talk a lot while doing things"},
241 {"config", OPT_CONFIG, '<', "A config file"},
242 {"name", OPT_NAME, 's', "The particular srp definition to use"},
243 {"srpvfile", OPT_SRPVFILE, '<', "The srp verifier file name"},
244 {"add", OPT_ADD, '-', "Add a user and srp verifier"},
245 {"modify", OPT_MODIFY, '-',
246 "Modify the srp verifier of an existing user"},
247 {"delete", OPT_DELETE, '-', "Delete user from verifier file"},
248 {"list", OPT_LIST, '-', "List users"},
249 {"gn", OPT_GN, 's', "Set g and N values to be used for new verifier"},
250 {"userinfo", OPT_USERINFO, 's', "Additional info to be set for user"},
251 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
252 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
253 # ifndef OPENSSL_NO_ENGINE
254 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
259 int srp_main(int argc, char **argv)
264 int gNindex = -1, maxgN = -1, ret = 1, errors = 0, verbose =
265 0, i, doupdatedb = 0;
267 char *user = NULL, *passinarg = NULL, *passoutarg = NULL;
268 char *passin = NULL, *passout = NULL, *gN = NULL, *userinfo = NULL;
269 char *randfile = NULL, *tofree = NULL, *section = NULL;
270 char **gNrow = NULL, *configfile = NULL, *dbfile = NULL, **pp, *prog;
274 prog = opt_init(argc, argv, srp_options);
275 while ((o = opt_next()) != OPT_EOF) {
280 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
283 opt_help(srp_options);
290 configfile = opt_arg();
302 if (mode != OPT_ERR) {
304 "%s: Only one of -add/delete-modify/-list\n",
314 userinfo = opt_arg();
317 passinarg = opt_arg();
320 passoutarg = opt_arg();
323 (void)setup_engine(opt_arg(), 0);
327 argc = opt_num_rest();
330 if (dbfile && configfile) {
332 "-dbfile and -configfile cannot be specified together.\n");
335 if (mode == OPT_ERR) {
337 "Exactly one of the options -add, -delete, -modify -list must be specified.\n");
340 if ((mode == OPT_DELETE || mode == OPT_MODIFY || OPT_ADD) && argc < 1) {
342 "Need at least one user for options -add, -delete, -modify. \n");
345 if ((passin || passout) && argc != 1) {
347 "-passin, -passout arguments only valid with one user.\n");
351 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
352 BIO_printf(bio_err, "Error getting passwords\n");
358 /*****************************************************************/
360 if (configfile == NULL)
361 configfile = getenv("OPENSSL_CONF");
362 if (configfile == NULL)
363 configfile = getenv("SSLEAY_CONF");
364 if (configfile == NULL) {
365 const char *s = X509_get_default_cert_area();
368 # ifdef OPENSSL_SYS_VMS
369 len = strlen(s) + sizeof(CONFIG_FILE);
370 tofree = OPENSSL_malloc(len);
372 BIO_printf(bio_err, "Out of memory\n");
377 len = strlen(s) + sizeof(CONFIG_FILE) + 1;
378 tofree = OPENSSL_malloc(len);
380 BIO_printf(bio_err, "Out of memory\n");
383 BUF_strlcpy(tofree, s, len);
384 BUF_strlcat(tofree, "/", len);
386 BUF_strlcat(tofree, CONFIG_FILE, len);
391 BIO_printf(bio_err, "Using configuration from %s\n", configfile);
392 conf = NCONF_new(NULL);
393 if (NCONF_load(conf, configfile, &errorline) <= 0) {
395 BIO_printf(bio_err, "error loading the config file '%s'\n",
398 BIO_printf(bio_err, "error on line %ld of config file '%s'\n",
399 errorline, configfile);
403 OPENSSL_free(tofree);
407 /* Lets get the config section we are using */
408 if (section == NULL) {
411 "trying to read " ENV_DEFAULT_SRP
412 " in \" BASE_SECTION \"\n");
414 section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_SRP);
415 if (section == NULL) {
416 lookup_fail(BASE_SECTION, ENV_DEFAULT_SRP);
421 if (randfile == NULL && conf)
422 randfile = NCONF_get_string(conf, BASE_SECTION, "RANDFILE");
426 "trying to read " ENV_DATABASE " in section \"%s\"\n",
429 if ((dbfile = NCONF_get_string(conf, section, ENV_DATABASE)) == NULL) {
430 lookup_fail(section, ENV_DATABASE);
435 if (randfile == NULL)
438 app_RAND_load_file(randfile, 0);
441 BIO_printf(bio_err, "Trying to read SRP verifier file \"%s\"\n",
444 db = load_index(dbfile, &db_attr);
448 /* Lets check some fields */
449 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
450 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
452 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
454 if (gNindex < 0 && gN != NULL && !strcmp(gN, pp[DB_srpid]))
457 print_index(db, bio_err, i, verbose > 1);
462 BIO_printf(bio_err, "Database initialised\n");
465 gNrow = sk_OPENSSL_PSTRING_value(db->db->data, gNindex);
466 print_entry(db, bio_err, gNindex, verbose > 1, "Default g and N");
467 } else if (maxgN > 0 && !SRP_get_default_gN(gN)) {
468 BIO_printf(bio_err, "No g and N value for index \"%s\"\n", gN);
472 BIO_printf(bio_err, "Database has no g N information.\n");
477 BIO_printf(bio_err, "Starting user processing\n");
482 while (mode == OPT_LIST || user) {
486 BIO_printf(bio_err, "Processing user \"%s\"\n", user);
487 if ((userindex = get_index(db, user, 'U')) >= 0) {
488 print_user(db, bio_err, userindex, (verbose > 0)
489 || mode == OPT_LIST);
492 if (mode == OPT_LIST) {
494 BIO_printf(bio_err, "List all users\n");
496 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
497 print_user(db, bio_err, i, 1);
499 } else if (userindex < 0) {
501 "user \"%s\" does not exist, ignored. t\n", user);
504 } else if (mode == OPT_ADD) {
505 if (userindex >= 0) {
506 /* reactivation of a new user */
508 sk_OPENSSL_PSTRING_value(db->db->data, userindex);
509 BIO_printf(bio_err, "user \"%s\" reactivated.\n", user);
510 row[DB_srptype][0] = 'V';
514 char *row[DB_NUMBER];
516 row[DB_srpverifier] = NULL;
517 row[DB_srpsalt] = NULL;
518 row[DB_srpinfo] = NULL;
521 srp_create_user(user, &(row[DB_srpverifier]),
523 gNrow ? gNrow[DB_srpsalt] : gN,
524 gNrow ? gNrow[DB_srpverifier] : NULL,
525 passout, bio_err, verbose))) {
527 "Cannot create srp verifier for user \"%s\", operation abandoned .\n",
532 row[DB_srpid] = BUF_strdup(user);
533 row[DB_srptype] = BUF_strdup("v");
534 row[DB_srpgN] = BUF_strdup(gNid);
536 if (!row[DB_srpid] || !row[DB_srpgN] || !row[DB_srptype]
537 || !row[DB_srpverifier] || !row[DB_srpsalt] || (userinfo
544 || !update_index(db, bio_err, row)) {
546 OPENSSL_free(row[DB_srpid]);
548 OPENSSL_free(row[DB_srpgN]);
550 OPENSSL_free(row[DB_srpinfo]);
552 OPENSSL_free(row[DB_srptype]);
553 if (row[DB_srpverifier])
554 OPENSSL_free(row[DB_srpverifier]);
556 OPENSSL_free(row[DB_srpsalt]);
561 } else if (mode == OPT_MODIFY) {
564 "user \"%s\" does not exist, operation ignored.\n",
570 sk_OPENSSL_PSTRING_value(db->db->data, userindex);
571 char type = row[DB_srptype][0];
574 "user \"%s\" already updated, operation ignored.\n",
580 if (row[DB_srptype][0] == 'V') {
585 "Verifying password for user \"%s\"\n",
588 get_index(db, row[DB_srpgN], DB_SRP_INDEX)) >= 0)
590 sk_OPENSSL_PSTRING_value(db->db->data,
594 (user, row[DB_srpverifier], row[DB_srpsalt],
595 irow ? irow[DB_srpsalt] : row[DB_srpgN],
596 irow ? irow[DB_srpverifier] : NULL, passin,
599 "Invalid password for user \"%s\", operation abandoned.\n",
606 BIO_printf(bio_err, "Password for user \"%s\" ok.\n",
611 srp_create_user(user, &(row[DB_srpverifier]),
613 gNrow ? gNrow[DB_srpsalt] : NULL,
614 gNrow ? gNrow[DB_srpverifier] : NULL,
615 passout, bio_err, verbose))) {
617 "Cannot create srp verifier for user \"%s\", operation abandoned.\n",
623 row[DB_srptype][0] = 'v';
624 row[DB_srpgN] = BUF_strdup(gNid);
626 if (!row[DB_srpid] || !row[DB_srpgN] || !row[DB_srptype]
627 || !row[DB_srpverifier] || !row[DB_srpsalt]
629 && (!(row[DB_srpinfo] = BUF_strdup(userinfo)))))
635 } else if (mode == OPT_DELETE) {
638 "user \"%s\" does not exist, operation ignored. t\n",
643 sk_OPENSSL_PSTRING_value(db->db->data, userindex);
644 BIO_printf(bio_err, "user \"%s\" revoked. t\n", user);
646 xpp[DB_srptype][0] = 'R';
659 BIO_printf(bio_err, "User procession done.\n");
662 /* Lets check some fields */
663 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
664 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
666 if (pp[DB_srptype][0] == 'v') {
667 pp[DB_srptype][0] = 'V';
668 print_user(db, bio_err, i, verbose);
673 BIO_printf(bio_err, "Trying to update srpvfile.\n");
674 if (!save_index(dbfile, "new", db))
678 BIO_printf(bio_err, "Temporary srpvfile created.\n");
679 if (!rotate_index(dbfile, "new", "old"))
683 BIO_printf(bio_err, "srpvfile updated.\n");
690 BIO_printf(bio_err, "User errors %d.\n", errors);
693 BIO_printf(bio_err, "SRP terminating with code %d.\n", ret);
695 OPENSSL_free(tofree);
697 ERR_print_errors(bio_err);
699 app_RAND_write_file(randfile);