2 * Copyright 2004-2016 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>
26 # define BASE_SECTION "srp"
27 # define CONFIG_FILE "openssl.cnf"
29 # define ENV_RANDFILE "RANDFILE"
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)
47 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
48 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
50 if (pp
[DB_srptype
][0] != DB_SRP_INDEX
51 && strcmp(id
, pp
[DB_srpid
]) == 0)
58 static void print_entry(CA_DB
*db
, int indx
, int verbose
, char *s
)
60 if (indx
>= 0 && verbose
) {
62 char **pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, indx
);
63 BIO_printf(bio_err
, "%s \"%s\"\n", s
, pp
[DB_srpid
]);
64 for (j
= 0; j
< DB_NUMBER
; j
++) {
65 BIO_printf(bio_err
, " %d = \"%s\"\n", j
, pp
[j
]);
70 static void print_index(CA_DB
*db
, int indexindex
, int verbose
)
72 print_entry(db
, indexindex
, verbose
, "g N entry");
75 static void print_user(CA_DB
*db
, int userindex
, int verbose
)
78 char **pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
80 if (pp
[DB_srptype
][0] != 'I') {
81 print_entry(db
, userindex
, verbose
, "User entry");
82 print_entry(db
, get_index(db
, pp
[DB_srpgN
], 'I'), verbose
,
89 static int update_index(CA_DB
*db
, char **row
)
94 irow
= app_malloc(sizeof(*irow
) * (DB_NUMBER
+ 1), "row pointers");
95 for (i
= 0; i
< DB_NUMBER
; i
++)
97 irow
[DB_NUMBER
] = NULL
;
99 if (!TXT_DB_insert(db
->db
, irow
)) {
100 BIO_printf(bio_err
, "failed to update srpvfile\n");
101 BIO_printf(bio_err
, "TXT_DB error number %ld\n", db
->db
->error
);
108 static char *lookup_conf(const CONF
*conf
, const char *section
, const char *tag
)
110 char *entry
= NCONF_get_string(conf
, section
, tag
);
112 BIO_printf(bio_err
, "variable lookup failed for %s::%s\n", section
, tag
);
116 static char *srp_verify_user(const char *user
, const char *srp_verifier
,
117 char *srp_usersalt
, const char *g
, const char *N
,
118 const char *passin
, int verbose
)
122 char *verifier
= NULL
;
125 cb_tmp
.prompt_info
= user
;
126 cb_tmp
.password
= passin
;
128 if (password_callback(password
, sizeof(password
), 0, &cb_tmp
) > 0) {
131 "Validating\n user=\"%s\"\n srp_verifier=\"%s\"\n srp_usersalt=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
132 user
, srp_verifier
, srp_usersalt
, g
, N
);
133 BIO_printf(bio_err
, "Pass %s\n", password
);
135 OPENSSL_assert(srp_usersalt
!= NULL
);
138 SRP_create_verifier(user
, password
, &srp_usersalt
, &verifier
, N
,
140 BIO_printf(bio_err
, "Internal error validating SRP verifier\n");
142 if (strcmp(verifier
, srp_verifier
))
144 OPENSSL_free(verifier
);
150 static char *srp_create_user(char *user
, char **srp_verifier
,
151 char **srp_usersalt
, char *g
, char *N
,
152 char *passout
, int verbose
)
158 cb_tmp
.prompt_info
= user
;
159 cb_tmp
.password
= passout
;
161 if (password_callback(password
, sizeof(password
), 1, &cb_tmp
) > 0) {
163 BIO_printf(bio_err
, "Creating\n user=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
167 SRP_create_verifier(user
, password
, &salt
, srp_verifier
, N
,
169 BIO_printf(bio_err
, "Internal error creating SRP verifier\n");
171 *srp_usersalt
= salt
;
173 BIO_printf(bio_err
, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n", gNid
,
174 salt
, *srp_verifier
);
180 typedef enum OPTION_choice
{
181 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
182 OPT_VERBOSE
, OPT_CONFIG
, OPT_NAME
, OPT_SRPVFILE
, OPT_ADD
,
183 OPT_DELETE
, OPT_MODIFY
, OPT_LIST
, OPT_GN
, OPT_USERINFO
,
184 OPT_PASSIN
, OPT_PASSOUT
, OPT_ENGINE
187 const OPTIONS srp_options
[] = {
188 {"help", OPT_HELP
, '-', "Display this summary"},
189 {"verbose", OPT_VERBOSE
, '-', "Talk a lot while doing things"},
190 {"config", OPT_CONFIG
, '<', "A config file"},
191 {"name", OPT_NAME
, 's', "The particular srp definition to use"},
192 {"srpvfile", OPT_SRPVFILE
, '<', "The srp verifier file name"},
193 {"add", OPT_ADD
, '-', "Add a user and srp verifier"},
194 {"modify", OPT_MODIFY
, '-',
195 "Modify the srp verifier of an existing user"},
196 {"delete", OPT_DELETE
, '-', "Delete user from verifier file"},
197 {"list", OPT_LIST
, '-', "List users"},
198 {"gn", OPT_GN
, 's', "Set g and N values to be used for new verifier"},
199 {"userinfo", OPT_USERINFO
, 's', "Additional info to be set for user"},
200 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
201 {"passout", OPT_PASSOUT
, 's', "Output file pass phrase source"},
202 # ifndef OPENSSL_NO_ENGINE
203 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
208 int srp_main(int argc
, char **argv
)
213 int gNindex
= -1, maxgN
= -1, ret
= 1, errors
= 0, verbose
= 0, i
;
214 int doupdatedb
= 0, mode
= OPT_ERR
;
215 char *user
= NULL
, *passinarg
= NULL
, *passoutarg
= NULL
;
216 char *passin
= NULL
, *passout
= NULL
, *gN
= NULL
, *userinfo
= NULL
;
217 char *randfile
= NULL
, *section
= NULL
;
218 char **gNrow
= NULL
, *configfile
= NULL
;
219 char *srpvfile
= NULL
, **pp
, *prog
;
222 prog
= opt_init(argc
, argv
, srp_options
);
223 while ((o
= opt_next()) != OPT_EOF
) {
228 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
231 opt_help(srp_options
);
238 configfile
= opt_arg();
244 srpvfile
= opt_arg();
250 if (mode
!= OPT_ERR
) {
252 "%s: Only one of -add/delete-modify/-list\n",
262 userinfo
= opt_arg();
265 passinarg
= opt_arg();
268 passoutarg
= opt_arg();
271 e
= setup_engine(opt_arg(), 0);
275 argc
= opt_num_rest();
278 if (srpvfile
&& configfile
) {
280 "-srpvfile and -configfile cannot be specified together.\n");
283 if (mode
== OPT_ERR
) {
285 "Exactly one of the options -add, -delete, -modify -list must be specified.\n");
288 if ((mode
== OPT_DELETE
|| mode
== OPT_MODIFY
|| mode
== OPT_ADD
)
291 "Need at least one user for options -add, -delete, -modify. \n");
294 if ((passin
|| passout
) && argc
!= 1) {
296 "-passin, -passout arguments only valid with one user.\n");
300 if (!app_passwd(passinarg
, passoutarg
, &passin
, &passout
)) {
301 BIO_printf(bio_err
, "Error getting passwords\n");
307 configfile
= default_config_file
;
310 BIO_printf(bio_err
, "Using configuration from %s\n",
312 conf
= app_load_config(configfile
);
315 if (configfile
!= default_config_file
&& !app_load_modules(conf
))
318 /* Lets get the config section we are using */
319 if (section
== NULL
) {
322 "trying to read " ENV_DEFAULT_SRP
323 " in " BASE_SECTION
"\n");
325 section
= lookup_conf(conf
, BASE_SECTION
, ENV_DEFAULT_SRP
);
330 if (randfile
== NULL
)
331 randfile
= NCONF_get_string(conf
, BASE_SECTION
, "RANDFILE");
335 "trying to read " ENV_DATABASE
" in section \"%s\"\n",
338 srpvfile
= lookup_conf(conf
, section
, ENV_DATABASE
);
339 if (srpvfile
== NULL
)
342 if (randfile
== NULL
)
345 app_RAND_load_file(randfile
, 0);
348 BIO_printf(bio_err
, "Trying to read SRP verifier file \"%s\"\n",
351 db
= load_index(srpvfile
, NULL
);
355 /* Lets check some fields */
356 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
357 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
359 if (pp
[DB_srptype
][0] == DB_SRP_INDEX
) {
361 if ((gNindex
< 0) && (gN
!= NULL
) && strcmp(gN
, pp
[DB_srpid
]) == 0)
364 print_index(db
, i
, verbose
> 1);
369 BIO_printf(bio_err
, "Database initialised\n");
372 gNrow
= sk_OPENSSL_PSTRING_value(db
->db
->data
, gNindex
);
373 print_entry(db
, gNindex
, verbose
> 1, "Default g and N");
374 } else if (maxgN
> 0 && !SRP_get_default_gN(gN
)) {
375 BIO_printf(bio_err
, "No g and N value for index \"%s\"\n", gN
);
379 BIO_printf(bio_err
, "Database has no g N information.\n");
384 BIO_printf(bio_err
, "Starting user processing\n");
389 while (mode
== OPT_LIST
|| user
) {
392 if (user
!= NULL
&& verbose
> 1)
393 BIO_printf(bio_err
, "Processing user \"%s\"\n", user
);
394 if ((userindex
= get_index(db
, user
, 'U')) >= 0) {
395 print_user(db
, userindex
, (verbose
> 0) || mode
== OPT_LIST
);
398 if (mode
== OPT_LIST
) {
400 BIO_printf(bio_err
, "List all users\n");
402 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
403 print_user(db
, i
, 1);
405 } else if (userindex
< 0) {
407 "user \"%s\" does not exist, ignored. t\n", user
);
410 } else if (mode
== OPT_ADD
) {
411 if (userindex
>= 0) {
412 /* reactivation of a new user */
414 sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
415 BIO_printf(bio_err
, "user \"%s\" reactivated.\n", user
);
416 row
[DB_srptype
][0] = 'V';
420 char *row
[DB_NUMBER
];
422 row
[DB_srpverifier
] = NULL
;
423 row
[DB_srpsalt
] = NULL
;
424 row
[DB_srpinfo
] = NULL
;
427 srp_create_user(user
, &(row
[DB_srpverifier
]),
429 gNrow
? gNrow
[DB_srpsalt
] : gN
,
430 gNrow
? gNrow
[DB_srpverifier
] : NULL
,
431 passout
, verbose
))) {
433 "Cannot create srp verifier for user \"%s\", operation abandoned .\n",
438 row
[DB_srpid
] = OPENSSL_strdup(user
);
439 row
[DB_srptype
] = OPENSSL_strdup("v");
440 row
[DB_srpgN
] = OPENSSL_strdup(gNid
);
442 if ((row
[DB_srpid
] == NULL
)
443 || (row
[DB_srpgN
] == NULL
)
444 || (row
[DB_srptype
] == NULL
)
445 || (row
[DB_srpverifier
] == NULL
)
446 || (row
[DB_srpsalt
] == NULL
)
448 && ((row
[DB_srpinfo
] = OPENSSL_strdup(userinfo
)) == NULL
))
449 || !update_index(db
, row
)) {
450 OPENSSL_free(row
[DB_srpid
]);
451 OPENSSL_free(row
[DB_srpgN
]);
452 OPENSSL_free(row
[DB_srpinfo
]);
453 OPENSSL_free(row
[DB_srptype
]);
454 OPENSSL_free(row
[DB_srpverifier
]);
455 OPENSSL_free(row
[DB_srpsalt
]);
460 } else if (mode
== OPT_MODIFY
) {
463 "user \"%s\" does not exist, operation ignored.\n",
469 sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
470 char type
= row
[DB_srptype
][0];
473 "user \"%s\" already updated, operation ignored.\n",
479 if (row
[DB_srptype
][0] == 'V') {
484 "Verifying password for user \"%s\"\n",
487 get_index(db
, row
[DB_srpgN
], DB_SRP_INDEX
)) >= 0)
489 sk_OPENSSL_PSTRING_value(db
->db
->data
,
493 (user
, row
[DB_srpverifier
], row
[DB_srpsalt
],
494 irow
? irow
[DB_srpsalt
] : row
[DB_srpgN
],
495 irow
? irow
[DB_srpverifier
] : NULL
, passin
,
498 "Invalid password for user \"%s\", operation abandoned.\n",
505 BIO_printf(bio_err
, "Password for user \"%s\" ok.\n",
510 srp_create_user(user
, &(row
[DB_srpverifier
]),
512 gNrow
? gNrow
[DB_srpsalt
] : NULL
,
513 gNrow
? gNrow
[DB_srpverifier
] : NULL
,
514 passout
, verbose
))) {
516 "Cannot create srp verifier for user \"%s\", operation abandoned.\n",
522 row
[DB_srptype
][0] = 'v';
523 row
[DB_srpgN
] = OPENSSL_strdup(gNid
);
525 if (row
[DB_srpid
] == NULL
526 || row
[DB_srpgN
] == NULL
527 || row
[DB_srptype
] == NULL
528 || row
[DB_srpverifier
] == NULL
529 || row
[DB_srpsalt
] == NULL
531 && ((row
[DB_srpinfo
] = OPENSSL_strdup(userinfo
))
538 } else if (mode
== OPT_DELETE
) {
541 "user \"%s\" does not exist, operation ignored. t\n",
545 char **xpp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
547 BIO_printf(bio_err
, "user \"%s\" revoked. t\n", user
);
548 xpp
[DB_srptype
][0] = 'R';
560 BIO_printf(bio_err
, "User procession done.\n");
563 /* Lets check some fields */
564 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
565 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
567 if (pp
[DB_srptype
][0] == 'v') {
568 pp
[DB_srptype
][0] = 'V';
569 print_user(db
, i
, verbose
);
574 BIO_printf(bio_err
, "Trying to update srpvfile.\n");
575 if (!save_index(srpvfile
, "new", db
))
579 BIO_printf(bio_err
, "Temporary srpvfile created.\n");
580 if (!rotate_index(srpvfile
, "new", "old"))
584 BIO_printf(bio_err
, "srpvfile updated.\n");
591 BIO_printf(bio_err
, "User errors %d.\n", errors
);
594 BIO_printf(bio_err
, "SRP terminating with code %d.\n", ret
);
596 OPENSSL_free(passin
);
597 OPENSSL_free(passout
);
599 ERR_print_errors(bio_err
);
601 app_RAND_write_file(randfile
);