2 * Copyright 2004-2017 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"
30 # define ENV_DATABASE "srpvfile"
31 # define ENV_DEFAULT_SRP "default_srp"
33 static int get_index(CA_DB
*db
, char *id
, char type
)
39 if (type
== DB_SRP_INDEX
) {
40 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
41 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
42 if (pp
[DB_srptype
][0] == DB_SRP_INDEX
43 && 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)
59 static void print_entry(CA_DB
*db
, int indx
, int verbose
, char *s
)
61 if (indx
>= 0 && verbose
) {
63 char **pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, indx
);
64 BIO_printf(bio_err
, "%s \"%s\"\n", s
, pp
[DB_srpid
]);
65 for (j
= 0; j
< DB_NUMBER
; j
++) {
66 BIO_printf(bio_err
, " %d = \"%s\"\n", j
, pp
[j
]);
71 static void print_index(CA_DB
*db
, int indexindex
, int verbose
)
73 print_entry(db
, indexindex
, verbose
, "g N entry");
76 static void print_user(CA_DB
*db
, int userindex
, int verbose
)
79 char **pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
81 if (pp
[DB_srptype
][0] != 'I') {
82 print_entry(db
, userindex
, verbose
, "User entry");
83 print_entry(db
, get_index(db
, pp
[DB_srpgN
], 'I'), verbose
,
90 static int update_index(CA_DB
*db
, char **row
)
95 irow
= app_malloc(sizeof(*irow
) * (DB_NUMBER
+ 1), "row pointers");
96 for (i
= 0; i
< DB_NUMBER
; i
++)
98 irow
[DB_NUMBER
] = NULL
;
100 if (!TXT_DB_insert(db
->db
, irow
)) {
101 BIO_printf(bio_err
, "failed to update srpvfile\n");
102 BIO_printf(bio_err
, "TXT_DB error number %ld\n", db
->db
->error
);
109 static char *lookup_conf(const CONF
*conf
, const char *section
, const char *tag
)
111 char *entry
= NCONF_get_string(conf
, section
, tag
);
113 BIO_printf(bio_err
, "variable lookup failed for %s::%s\n", section
, tag
);
117 static char *srp_verify_user(const char *user
, const char *srp_verifier
,
118 char *srp_usersalt
, const char *g
, const char *N
,
119 const char *passin
, int verbose
)
123 char *verifier
= NULL
;
127 cb_tmp
.prompt_info
= user
;
128 cb_tmp
.password
= passin
;
130 len
= password_callback(password
, sizeof(password
)-1, 0, &cb_tmp
);
135 "Validating\n user=\"%s\"\n srp_verifier=\"%s\"\n srp_usersalt=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
136 user
, srp_verifier
, srp_usersalt
, g
, N
);
138 BIO_printf(bio_err
, "Pass %s\n", password
);
140 OPENSSL_assert(srp_usersalt
!= NULL
);
141 if ((gNid
= SRP_create_verifier(user
, password
, &srp_usersalt
,
142 &verifier
, N
, g
)) == NULL
) {
143 BIO_printf(bio_err
, "Internal error validating SRP verifier\n");
145 if (strcmp(verifier
, srp_verifier
))
147 OPENSSL_free(verifier
);
149 OPENSSL_cleanse(password
, len
);
154 static char *srp_create_user(char *user
, char **srp_verifier
,
155 char **srp_usersalt
, char *g
, char *N
,
156 char *passout
, int verbose
)
163 cb_tmp
.prompt_info
= user
;
164 cb_tmp
.password
= passout
;
166 len
= password_callback(password
, sizeof(password
)-1, 1, &cb_tmp
);
170 BIO_printf(bio_err
, "Creating\n user=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
172 if ((gNid
= SRP_create_verifier(user
, password
, &salt
,
173 srp_verifier
, N
, g
)) == NULL
) {
174 BIO_printf(bio_err
, "Internal error creating SRP verifier\n");
176 *srp_usersalt
= salt
;
178 OPENSSL_cleanse(password
, len
);
180 BIO_printf(bio_err
, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n",
181 gNid
, salt
, *srp_verifier
);
187 typedef enum OPTION_choice
{
188 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
189 OPT_VERBOSE
, OPT_CONFIG
, OPT_NAME
, OPT_SRPVFILE
, OPT_ADD
,
190 OPT_DELETE
, OPT_MODIFY
, OPT_LIST
, OPT_GN
, OPT_USERINFO
,
191 OPT_PASSIN
, OPT_PASSOUT
, OPT_ENGINE
, OPT_R_ENUM
194 const OPTIONS srp_options
[] = {
195 {"help", OPT_HELP
, '-', "Display this summary"},
196 {"verbose", OPT_VERBOSE
, '-', "Talk a lot while doing things"},
197 {"config", OPT_CONFIG
, '<', "A config file"},
198 {"name", OPT_NAME
, 's', "The particular srp definition to use"},
199 {"srpvfile", OPT_SRPVFILE
, '<', "The srp verifier file name"},
200 {"add", OPT_ADD
, '-', "Add a user and srp verifier"},
201 {"modify", OPT_MODIFY
, '-',
202 "Modify the srp verifier of an existing user"},
203 {"delete", OPT_DELETE
, '-', "Delete user from verifier file"},
204 {"list", OPT_LIST
, '-', "List users"},
205 {"gn", OPT_GN
, 's', "Set g and N values to be used for new verifier"},
206 {"userinfo", OPT_USERINFO
, 's', "Additional info to be set for user"},
207 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
208 {"passout", OPT_PASSOUT
, 's', "Output file pass phrase source"},
210 # ifndef OPENSSL_NO_ENGINE
211 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
216 int srp_main(int argc
, char **argv
)
221 int gNindex
= -1, maxgN
= -1, ret
= 1, errors
= 0, verbose
= 0, i
;
222 int doupdatedb
= 0, mode
= OPT_ERR
;
223 char *user
= NULL
, *passinarg
= NULL
, *passoutarg
= NULL
;
224 char *passin
= NULL
, *passout
= NULL
, *gN
= NULL
, *userinfo
= NULL
;
225 char *section
= NULL
;
226 char **gNrow
= NULL
, *configfile
= NULL
;
227 char *srpvfile
= NULL
, **pp
, *prog
;
230 prog
= opt_init(argc
, argv
, srp_options
);
231 while ((o
= opt_next()) != OPT_EOF
) {
236 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
239 opt_help(srp_options
);
246 configfile
= opt_arg();
252 srpvfile
= opt_arg();
258 if (mode
!= OPT_ERR
) {
260 "%s: Only one of -add/-delete/-modify/-list\n",
270 userinfo
= opt_arg();
273 passinarg
= opt_arg();
276 passoutarg
= opt_arg();
279 e
= setup_engine(opt_arg(), 0);
287 argc
= opt_num_rest();
290 if (srpvfile
!= NULL
&& configfile
!= NULL
) {
292 "-srpvfile and -configfile cannot be specified together.\n");
295 if (mode
== OPT_ERR
) {
297 "Exactly one of the options -add, -delete, -modify -list must be specified.\n");
300 if (mode
== OPT_DELETE
|| mode
== OPT_MODIFY
|| mode
== OPT_ADD
) {
302 BIO_printf(bio_err
, "Need at least one user.\n");
307 if ((passinarg
!= NULL
|| passoutarg
!= NULL
) && argc
!= 1) {
309 "-passin, -passout arguments only valid with one user.\n");
313 if (!app_passwd(passinarg
, passoutarg
, &passin
, &passout
)) {
314 BIO_printf(bio_err
, "Error getting passwords\n");
318 if (srpvfile
== NULL
) {
319 if (configfile
== NULL
)
320 configfile
= default_config_file
;
323 BIO_printf(bio_err
, "Using configuration from %s\n",
325 conf
= app_load_config(configfile
);
328 if (configfile
!= default_config_file
&& !app_load_modules(conf
))
331 /* Lets get the config section we are using */
332 if (section
== NULL
) {
335 "trying to read " ENV_DEFAULT_SRP
336 " in " BASE_SECTION
"\n");
338 section
= lookup_conf(conf
, BASE_SECTION
, ENV_DEFAULT_SRP
);
343 app_RAND_load_conf(conf
, BASE_SECTION
);
347 "trying to read " ENV_DATABASE
" in section \"%s\"\n",
350 srpvfile
= lookup_conf(conf
, section
, ENV_DATABASE
);
351 if (srpvfile
== NULL
)
356 BIO_printf(bio_err
, "Trying to read SRP verifier file \"%s\"\n",
359 db
= load_index(srpvfile
, NULL
);
363 /* Lets check some fields */
364 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
365 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
367 if (pp
[DB_srptype
][0] == DB_SRP_INDEX
) {
369 if ((gNindex
< 0) && (gN
!= NULL
) && strcmp(gN
, pp
[DB_srpid
]) == 0)
372 print_index(db
, i
, verbose
> 1);
377 BIO_printf(bio_err
, "Database initialised\n");
380 gNrow
= sk_OPENSSL_PSTRING_value(db
->db
->data
, gNindex
);
381 print_entry(db
, gNindex
, verbose
> 1, "Default g and N");
382 } else if (maxgN
> 0 && !SRP_get_default_gN(gN
)) {
383 BIO_printf(bio_err
, "No g and N value for index \"%s\"\n", gN
);
387 BIO_printf(bio_err
, "Database has no g N information.\n");
392 BIO_printf(bio_err
, "Starting user processing\n");
394 while (mode
== OPT_LIST
|| user
!= NULL
) {
397 if (user
!= NULL
&& verbose
> 1)
398 BIO_printf(bio_err
, "Processing user \"%s\"\n", user
);
399 if ((userindex
= get_index(db
, user
, 'U')) >= 0)
400 print_user(db
, userindex
, (verbose
> 0) || mode
== OPT_LIST
);
402 if (mode
== OPT_LIST
) {
404 BIO_printf(bio_err
, "List all users\n");
406 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++)
407 print_user(db
, i
, 1);
408 } else if (userindex
< 0) {
410 "user \"%s\" does not exist, ignored. t\n", user
);
413 } else if (mode
== OPT_ADD
) {
414 if (userindex
>= 0) {
415 /* reactivation of a new user */
417 sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
418 BIO_printf(bio_err
, "user \"%s\" reactivated.\n", user
);
419 row
[DB_srptype
][0] = 'V';
423 char *row
[DB_NUMBER
];
425 row
[DB_srpverifier
] = NULL
;
426 row
[DB_srpsalt
] = NULL
;
427 row
[DB_srpinfo
] = NULL
;
430 srp_create_user(user
, &(row
[DB_srpverifier
]),
432 gNrow
? gNrow
[DB_srpsalt
] : gN
,
433 gNrow
? gNrow
[DB_srpverifier
] : NULL
,
434 passout
, verbose
))) {
436 "Cannot create srp verifier for user \"%s\", operation abandoned .\n",
441 row
[DB_srpid
] = OPENSSL_strdup(user
);
442 row
[DB_srptype
] = OPENSSL_strdup("v");
443 row
[DB_srpgN
] = OPENSSL_strdup(gNid
);
445 if ((row
[DB_srpid
] == NULL
)
446 || (row
[DB_srpgN
] == NULL
)
447 || (row
[DB_srptype
] == NULL
)
448 || (row
[DB_srpverifier
] == NULL
)
449 || (row
[DB_srpsalt
] == NULL
)
451 && ((row
[DB_srpinfo
] = OPENSSL_strdup(userinfo
)) == NULL
))
452 || !update_index(db
, row
)) {
453 OPENSSL_free(row
[DB_srpid
]);
454 OPENSSL_free(row
[DB_srpgN
]);
455 OPENSSL_free(row
[DB_srpinfo
]);
456 OPENSSL_free(row
[DB_srptype
]);
457 OPENSSL_free(row
[DB_srpverifier
]);
458 OPENSSL_free(row
[DB_srpsalt
]);
463 } else if (mode
== OPT_MODIFY
) {
466 "user \"%s\" does not exist, operation ignored.\n",
472 sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
473 char type
= row
[DB_srptype
][0];
476 "user \"%s\" already updated, operation ignored.\n",
482 if (row
[DB_srptype
][0] == 'V') {
487 "Verifying password for user \"%s\"\n",
490 get_index(db
, row
[DB_srpgN
], DB_SRP_INDEX
)) >= 0)
492 sk_OPENSSL_PSTRING_value(db
->db
->data
,
496 (user
, row
[DB_srpverifier
], row
[DB_srpsalt
],
497 irow
? irow
[DB_srpsalt
] : row
[DB_srpgN
],
498 irow
? irow
[DB_srpverifier
] : NULL
, passin
,
501 "Invalid password for user \"%s\", operation abandoned.\n",
508 BIO_printf(bio_err
, "Password for user \"%s\" ok.\n",
513 srp_create_user(user
, &(row
[DB_srpverifier
]),
515 gNrow
? gNrow
[DB_srpsalt
] : NULL
,
516 gNrow
? gNrow
[DB_srpverifier
] : NULL
,
517 passout
, verbose
))) {
519 "Cannot create srp verifier for user \"%s\", operation abandoned.\n",
525 row
[DB_srptype
][0] = 'v';
526 row
[DB_srpgN
] = OPENSSL_strdup(gNid
);
528 if (row
[DB_srpid
] == NULL
529 || row
[DB_srpgN
] == NULL
530 || row
[DB_srptype
] == NULL
531 || row
[DB_srpverifier
] == NULL
532 || row
[DB_srpsalt
] == NULL
534 && ((row
[DB_srpinfo
] = OPENSSL_strdup(userinfo
))
541 } else if (mode
== OPT_DELETE
) {
544 "user \"%s\" does not exist, operation ignored. t\n",
548 char **xpp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
550 BIO_printf(bio_err
, "user \"%s\" revoked. t\n", user
);
551 xpp
[DB_srptype
][0] = 'R';
557 /* no more processing in any mode if no users left */
563 BIO_printf(bio_err
, "User procession done.\n");
566 /* Lets check some fields */
567 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
568 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
570 if (pp
[DB_srptype
][0] == 'v') {
571 pp
[DB_srptype
][0] = 'V';
572 print_user(db
, i
, verbose
);
577 BIO_printf(bio_err
, "Trying to update srpvfile.\n");
578 if (!save_index(srpvfile
, "new", db
))
582 BIO_printf(bio_err
, "Temporary srpvfile created.\n");
583 if (!rotate_index(srpvfile
, "new", "old"))
587 BIO_printf(bio_err
, "srpvfile updated.\n");
594 BIO_printf(bio_err
, "User errors %d.\n", errors
);
597 BIO_printf(bio_err
, "SRP terminating with code %d.\n", ret
);
599 OPENSSL_free(passin
);
600 OPENSSL_free(passout
);
602 ERR_print_errors(bio_err
);