]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/srp.c
f7edfa99305b4d232fd6375332fba276e380d9c1
2 * Copyright 2004-2020 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>
19 #include <openssl/conf.h>
20 #include <openssl/bio.h>
21 #include <openssl/err.h>
22 #include <openssl/txt_db.h>
23 #include <openssl/buffer.h>
24 #include <openssl/srp.h>
28 #define BASE_SECTION "srp"
29 #define CONFIG_FILE "openssl.cnf"
32 #define ENV_DATABASE "srpvfile"
33 #define ENV_DEFAULT_SRP "default_srp"
35 static int get_index(CA_DB
*db
, char *id
, char type
)
41 if (type
== DB_SRP_INDEX
) {
42 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
43 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
44 if (pp
[DB_srptype
][0] == DB_SRP_INDEX
45 && strcmp(id
, pp
[DB_srpid
]) == 0)
49 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
50 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
52 if (pp
[DB_srptype
][0] != DB_SRP_INDEX
53 && strcmp(id
, pp
[DB_srpid
]) == 0)
61 static void print_entry(CA_DB
*db
, int indx
, int verbose
, char *s
)
63 if (indx
>= 0 && verbose
) {
65 char **pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, indx
);
66 BIO_printf(bio_err
, "%s \"%s\"\n", s
, pp
[DB_srpid
]);
67 for (j
= 0; j
< DB_NUMBER
; j
++) {
68 BIO_printf(bio_err
, " %d = \"%s\"\n", j
, pp
[j
]);
73 static void print_index(CA_DB
*db
, int indexindex
, int verbose
)
75 print_entry(db
, indexindex
, verbose
, "g N entry");
78 static void print_user(CA_DB
*db
, int userindex
, int verbose
)
81 char **pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
83 if (pp
[DB_srptype
][0] != 'I') {
84 print_entry(db
, userindex
, verbose
, "User entry");
85 print_entry(db
, get_index(db
, pp
[DB_srpgN
], 'I'), verbose
,
92 static int update_index(CA_DB
*db
, char **row
)
97 irow
= app_malloc(sizeof(*irow
) * (DB_NUMBER
+ 1), "row pointers");
98 for (i
= 0; i
< DB_NUMBER
; i
++)
100 irow
[DB_NUMBER
] = NULL
;
102 if (!TXT_DB_insert(db
->db
, irow
)) {
103 BIO_printf(bio_err
, "failed to update srpvfile\n");
104 BIO_printf(bio_err
, "TXT_DB error number %ld\n", db
->db
->error
);
111 static char *lookup_conf(const CONF
*conf
, const char *section
, const char *tag
)
113 char *entry
= NCONF_get_string(conf
, section
, tag
);
115 BIO_printf(bio_err
, "variable lookup failed for %s::%s\n", section
, tag
);
119 static char *srp_verify_user(const char *user
, const char *srp_verifier
,
120 char *srp_usersalt
, const char *g
, const char *N
,
121 const char *passin
, int verbose
)
125 char *verifier
= NULL
;
129 cb_tmp
.prompt_info
= user
;
130 cb_tmp
.password
= passin
;
132 len
= password_callback(password
, sizeof(password
)-1, 0, &cb_tmp
);
137 "Validating\n user=\"%s\"\n srp_verifier=\"%s\"\n srp_usersalt=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
138 user
, srp_verifier
, srp_usersalt
, g
, N
);
140 BIO_printf(bio_err
, "Pass %s\n", password
);
142 OPENSSL_assert(srp_usersalt
!= NULL
);
143 if ((gNid
= SRP_create_verifier(user
, password
, &srp_usersalt
,
144 &verifier
, N
, g
)) == NULL
) {
145 BIO_printf(bio_err
, "Internal error validating SRP verifier\n");
147 if (strcmp(verifier
, srp_verifier
))
149 OPENSSL_free(verifier
);
151 OPENSSL_cleanse(password
, len
);
156 static char *srp_create_user(char *user
, char **srp_verifier
,
157 char **srp_usersalt
, char *g
, char *N
,
158 char *passout
, int verbose
)
165 cb_tmp
.prompt_info
= user
;
166 cb_tmp
.password
= passout
;
168 len
= password_callback(password
, sizeof(password
)-1, 1, &cb_tmp
);
172 BIO_printf(bio_err
, "Creating\n user=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
174 if ((gNid
= SRP_create_verifier(user
, password
, &salt
,
175 srp_verifier
, N
, g
)) == NULL
) {
176 BIO_printf(bio_err
, "Internal error creating SRP verifier\n");
178 *srp_usersalt
= salt
;
180 OPENSSL_cleanse(password
, len
);
182 BIO_printf(bio_err
, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n",
183 gNid
, salt
, *srp_verifier
);
189 typedef enum OPTION_choice
{
190 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
191 OPT_VERBOSE
, OPT_CONFIG
, OPT_NAME
, OPT_SRPVFILE
, OPT_ADD
,
192 OPT_DELETE
, OPT_MODIFY
, OPT_LIST
, OPT_GN
, OPT_USERINFO
,
193 OPT_PASSIN
, OPT_PASSOUT
, OPT_ENGINE
, OPT_R_ENUM
, OPT_PROV_ENUM
196 const OPTIONS srp_options
[] = {
197 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] [user...]\n"},
199 OPT_SECTION("General"),
200 {"help", OPT_HELP
, '-', "Display this summary"},
201 {"verbose", OPT_VERBOSE
, '-', "Talk a lot while doing things"},
202 {"config", OPT_CONFIG
, '<', "A config file"},
203 {"name", OPT_NAME
, 's', "The particular srp definition to use"},
204 #ifndef OPENSSL_NO_ENGINE
205 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
208 OPT_SECTION("Action"),
209 {"add", OPT_ADD
, '-', "Add a user and srp verifier"},
210 {"modify", OPT_MODIFY
, '-', "Modify the srp verifier of an existing user"},
211 {"delete", OPT_DELETE
, '-', "Delete user from verifier file"},
212 {"list", OPT_LIST
, '-', "List users"},
214 OPT_SECTION("Configuration"),
215 {"srpvfile", OPT_SRPVFILE
, '<', "The srp verifier file name"},
216 {"gn", OPT_GN
, 's', "Set g and N values to be used for new verifier"},
217 {"userinfo", OPT_USERINFO
, 's', "Additional info to be set for user"},
218 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
219 {"passout", OPT_PASSOUT
, 's', "Output file pass phrase source"},
225 {"user", 0, 0, "Username(s) to process (optional)"},
229 int srp_main(int argc
, char **argv
)
234 int gNindex
= -1, maxgN
= -1, ret
= 1, errors
= 0, verbose
= 0, i
;
235 int doupdatedb
= 0, mode
= OPT_ERR
;
236 char *user
= NULL
, *passinarg
= NULL
, *passoutarg
= NULL
;
237 char *passin
= NULL
, *passout
= NULL
, *gN
= NULL
, *userinfo
= NULL
;
238 char *section
= NULL
;
239 char **gNrow
= NULL
, *configfile
= NULL
;
240 char *srpvfile
= NULL
, **pp
, *prog
;
243 prog
= opt_init(argc
, argv
, srp_options
);
244 while ((o
= opt_next()) != OPT_EOF
) {
249 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
252 opt_help(srp_options
);
259 configfile
= opt_arg();
265 srpvfile
= opt_arg();
271 if (mode
!= OPT_ERR
) {
273 "%s: Only one of -add/-delete/-modify/-list\n",
283 userinfo
= opt_arg();
286 passinarg
= opt_arg();
289 passoutarg
= opt_arg();
292 e
= setup_engine(opt_arg(), 0);
299 if (!opt_provider(o
))
305 /* Optional parameters are usernames. */
306 argc
= opt_num_rest();
309 if (srpvfile
!= NULL
&& configfile
!= NULL
) {
311 "-srpvfile and -configfile cannot be specified together.\n");
314 if (mode
== OPT_ERR
) {
316 "Exactly one of the options -add, -delete, -modify -list must be specified.\n");
319 if (mode
== OPT_DELETE
|| mode
== OPT_MODIFY
|| mode
== OPT_ADD
) {
321 BIO_printf(bio_err
, "Need at least one user.\n");
326 if ((passinarg
!= NULL
|| passoutarg
!= NULL
) && argc
!= 1) {
328 "-passin, -passout arguments only valid with one user.\n");
332 if (!app_passwd(passinarg
, passoutarg
, &passin
, &passout
)) {
333 BIO_printf(bio_err
, "Error getting passwords\n");
337 if (srpvfile
== NULL
) {
338 if (configfile
== NULL
)
339 configfile
= default_config_file
;
341 conf
= app_load_config_verbose(configfile
, verbose
);
344 if (configfile
!= default_config_file
&& !app_load_modules(conf
))
347 /* Lets get the config section we are using */
348 if (section
== NULL
) {
351 "trying to read " ENV_DEFAULT_SRP
352 " in " BASE_SECTION
"\n");
354 section
= lookup_conf(conf
, BASE_SECTION
, ENV_DEFAULT_SRP
);
359 app_RAND_load_conf(conf
, BASE_SECTION
);
363 "trying to read " ENV_DATABASE
" in section \"%s\"\n",
366 srpvfile
= lookup_conf(conf
, section
, ENV_DATABASE
);
367 if (srpvfile
== NULL
)
372 BIO_printf(bio_err
, "Trying to read SRP verifier file \"%s\"\n",
375 db
= load_index(srpvfile
, NULL
);
379 /* Lets check some fields */
380 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
381 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
383 if (pp
[DB_srptype
][0] == DB_SRP_INDEX
) {
385 if ((gNindex
< 0) && (gN
!= NULL
) && strcmp(gN
, pp
[DB_srpid
]) == 0)
388 print_index(db
, i
, verbose
> 1);
393 BIO_printf(bio_err
, "Database initialised\n");
396 gNrow
= sk_OPENSSL_PSTRING_value(db
->db
->data
, gNindex
);
397 print_entry(db
, gNindex
, verbose
> 1, "Default g and N");
398 } else if (maxgN
> 0 && !SRP_get_default_gN(gN
)) {
399 BIO_printf(bio_err
, "No g and N value for index \"%s\"\n", gN
);
403 BIO_printf(bio_err
, "Database has no g N information.\n");
408 BIO_printf(bio_err
, "Starting user processing\n");
410 while (mode
== OPT_LIST
|| user
!= NULL
) {
413 if (user
!= NULL
&& verbose
> 1)
414 BIO_printf(bio_err
, "Processing user \"%s\"\n", user
);
415 if ((userindex
= get_index(db
, user
, 'U')) >= 0)
416 print_user(db
, userindex
, (verbose
> 0) || mode
== OPT_LIST
);
418 if (mode
== OPT_LIST
) {
420 BIO_printf(bio_err
, "List all users\n");
422 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++)
423 print_user(db
, i
, 1);
424 } else if (userindex
< 0) {
426 "user \"%s\" does not exist, ignored. t\n", user
);
429 } else if (mode
== OPT_ADD
) {
430 if (userindex
>= 0) {
431 /* reactivation of a new user */
433 sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
434 BIO_printf(bio_err
, "user \"%s\" reactivated.\n", user
);
435 row
[DB_srptype
][0] = 'V';
439 char *row
[DB_NUMBER
];
441 row
[DB_srpverifier
] = NULL
;
442 row
[DB_srpsalt
] = NULL
;
443 row
[DB_srpinfo
] = NULL
;
446 srp_create_user(user
, &(row
[DB_srpverifier
]),
448 gNrow
? gNrow
[DB_srpsalt
] : gN
,
449 gNrow
? gNrow
[DB_srpverifier
] : NULL
,
450 passout
, verbose
))) {
452 "Cannot create srp verifier for user \"%s\", operation abandoned .\n",
457 row
[DB_srpid
] = OPENSSL_strdup(user
);
458 row
[DB_srptype
] = OPENSSL_strdup("v");
459 row
[DB_srpgN
] = OPENSSL_strdup(gNid
);
461 if ((row
[DB_srpid
] == NULL
)
462 || (row
[DB_srpgN
] == NULL
)
463 || (row
[DB_srptype
] == NULL
)
464 || (row
[DB_srpverifier
] == NULL
)
465 || (row
[DB_srpsalt
] == NULL
)
467 && ((row
[DB_srpinfo
] = OPENSSL_strdup(userinfo
)) == NULL
))
468 || !update_index(db
, row
)) {
469 OPENSSL_free(row
[DB_srpid
]);
470 OPENSSL_free(row
[DB_srpgN
]);
471 OPENSSL_free(row
[DB_srpinfo
]);
472 OPENSSL_free(row
[DB_srptype
]);
473 OPENSSL_free(row
[DB_srpverifier
]);
474 OPENSSL_free(row
[DB_srpsalt
]);
479 } else if (mode
== OPT_MODIFY
) {
482 "user \"%s\" does not exist, operation ignored.\n",
488 sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
489 char type
= row
[DB_srptype
][0];
492 "user \"%s\" already updated, operation ignored.\n",
498 if (row
[DB_srptype
][0] == 'V') {
503 "Verifying password for user \"%s\"\n",
506 get_index(db
, row
[DB_srpgN
], DB_SRP_INDEX
)) >= 0)
508 sk_OPENSSL_PSTRING_value(db
->db
->data
,
512 (user
, row
[DB_srpverifier
], row
[DB_srpsalt
],
513 irow
? irow
[DB_srpsalt
] : row
[DB_srpgN
],
514 irow
? irow
[DB_srpverifier
] : NULL
, passin
,
517 "Invalid password for user \"%s\", operation abandoned.\n",
524 BIO_printf(bio_err
, "Password for user \"%s\" ok.\n",
529 srp_create_user(user
, &(row
[DB_srpverifier
]),
531 gNrow
? gNrow
[DB_srpsalt
] : NULL
,
532 gNrow
? gNrow
[DB_srpverifier
] : NULL
,
533 passout
, verbose
))) {
535 "Cannot create srp verifier for user \"%s\", operation abandoned.\n",
541 row
[DB_srptype
][0] = 'v';
542 row
[DB_srpgN
] = OPENSSL_strdup(gNid
);
544 if (row
[DB_srpid
] == NULL
545 || row
[DB_srpgN
] == NULL
546 || row
[DB_srptype
] == NULL
547 || row
[DB_srpverifier
] == NULL
548 || row
[DB_srpsalt
] == NULL
550 && ((row
[DB_srpinfo
] = OPENSSL_strdup(userinfo
))
557 } else if (mode
== OPT_DELETE
) {
560 "user \"%s\" does not exist, operation ignored. t\n",
564 char **xpp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
566 BIO_printf(bio_err
, "user \"%s\" revoked. t\n", user
);
567 xpp
[DB_srptype
][0] = 'R';
573 /* no more processing in any mode if no users left */
579 BIO_printf(bio_err
, "User procession done.\n");
582 /* Lets check some fields */
583 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
584 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
586 if (pp
[DB_srptype
][0] == 'v') {
587 pp
[DB_srptype
][0] = 'V';
588 print_user(db
, i
, verbose
);
593 BIO_printf(bio_err
, "Trying to update srpvfile.\n");
594 if (!save_index(srpvfile
, "new", db
))
598 BIO_printf(bio_err
, "Temporary srpvfile created.\n");
599 if (!rotate_index(srpvfile
, "new", "old"))
603 BIO_printf(bio_err
, "srpvfile updated.\n");
610 BIO_printf(bio_err
, "User errors %d.\n", errors
);
613 BIO_printf(bio_err
, "SRP terminating with code %d.\n", ret
);
615 OPENSSL_free(passin
);
616 OPENSSL_free(passout
);
618 ERR_print_errors(bio_err
);