]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/srp.c
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>
72 # define BASE_SECTION "srp"
73 # define CONFIG_FILE "openssl.cnf"
75 # define ENV_RANDFILE "RANDFILE"
77 # define ENV_DATABASE "srpvfile"
78 # define ENV_DEFAULT_SRP "default_srp"
80 static int get_index(CA_DB
*db
, char *id
, char type
)
86 if (type
== DB_SRP_INDEX
)
87 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
88 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
89 if (pp
[DB_srptype
][0] == DB_SRP_INDEX
90 && strcmp(id
, pp
[DB_srpid
]) == 0)
93 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
94 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
96 if (pp
[DB_srptype
][0] != DB_SRP_INDEX
97 && strcmp(id
, pp
[DB_srpid
]) == 0)
104 static void print_entry(CA_DB
*db
, int indx
, int verbose
, char *s
)
106 if (indx
>= 0 && verbose
) {
108 char **pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, indx
);
109 BIO_printf(bio_err
, "%s \"%s\"\n", s
, pp
[DB_srpid
]);
110 for (j
= 0; j
< DB_NUMBER
; j
++) {
111 BIO_printf(bio_err
, " %d = \"%s\"\n", j
, pp
[j
]);
116 static void print_index(CA_DB
*db
, int indexindex
, int verbose
)
118 print_entry(db
, indexindex
, verbose
, "g N entry");
121 static void print_user(CA_DB
*db
, int userindex
, int verbose
)
124 char **pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
126 if (pp
[DB_srptype
][0] != 'I') {
127 print_entry(db
, userindex
, verbose
, "User entry");
128 print_entry(db
, get_index(db
, pp
[DB_srpgN
], 'I'), verbose
,
135 static int update_index(CA_DB
*db
, char **row
)
140 irow
= app_malloc(sizeof(*irow
) * (DB_NUMBER
+ 1), "row pointers");
141 for (i
= 0; i
< DB_NUMBER
; i
++) {
145 irow
[DB_NUMBER
] = NULL
;
147 if (!TXT_DB_insert(db
->db
, irow
)) {
148 BIO_printf(bio_err
, "failed to update srpvfile\n");
149 BIO_printf(bio_err
, "TXT_DB error number %ld\n", db
->db
->error
);
156 static void lookup_fail(const char *name
, const char *tag
)
158 BIO_printf(bio_err
, "variable lookup failed for %s::%s\n", name
, tag
);
161 static char *srp_verify_user(const char *user
, const char *srp_verifier
,
162 char *srp_usersalt
, const char *g
, const char *N
,
163 const char *passin
, int verbose
)
167 char *verifier
= NULL
;
170 cb_tmp
.prompt_info
= user
;
171 cb_tmp
.password
= passin
;
173 if (password_callback(password
, 1024, 0, &cb_tmp
) > 0) {
176 "Validating\n user=\"%s\"\n srp_verifier=\"%s\"\n srp_usersalt=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
177 user
, srp_verifier
, srp_usersalt
, g
, N
);
178 BIO_printf(bio_err
, "Pass %s\n", password
);
180 OPENSSL_assert(srp_usersalt
!= NULL
);
183 SRP_create_verifier(user
, password
, &srp_usersalt
, &verifier
, N
,
185 BIO_printf(bio_err
, "Internal error validating SRP verifier\n");
187 if (strcmp(verifier
, srp_verifier
))
189 OPENSSL_free(verifier
);
195 static char *srp_create_user(char *user
, char **srp_verifier
,
196 char **srp_usersalt
, char *g
, char *N
,
197 char *passout
, int verbose
)
203 cb_tmp
.prompt_info
= user
;
204 cb_tmp
.password
= passout
;
206 if (password_callback(password
, 1024, 1, &cb_tmp
) > 0) {
208 BIO_printf(bio_err
, "Creating\n user=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
212 SRP_create_verifier(user
, password
, &salt
, srp_verifier
, N
,
214 BIO_printf(bio_err
, "Internal error creating SRP verifier\n");
216 *srp_usersalt
= salt
;
218 BIO_printf(bio_err
, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n", gNid
,
219 salt
, *srp_verifier
);
225 typedef enum OPTION_choice
{
226 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
227 OPT_VERBOSE
, OPT_CONFIG
, OPT_NAME
, OPT_SRPVFILE
, OPT_ADD
,
228 OPT_DELETE
, OPT_MODIFY
, OPT_LIST
, OPT_GN
, OPT_USERINFO
,
229 OPT_PASSIN
, OPT_PASSOUT
, OPT_ENGINE
232 OPTIONS srp_options
[] = {
233 {"help", OPT_HELP
, '-', "Display this summary"},
234 {"verbose", OPT_VERBOSE
, '-', "Talk a lot while doing things"},
235 {"config", OPT_CONFIG
, '<', "A config file"},
236 {"name", OPT_NAME
, 's', "The particular srp definition to use"},
237 {"srpvfile", OPT_SRPVFILE
, '<', "The srp verifier file name"},
238 {"add", OPT_ADD
, '-', "Add a user and srp verifier"},
239 {"modify", OPT_MODIFY
, '-',
240 "Modify the srp verifier of an existing user"},
241 {"delete", OPT_DELETE
, '-', "Delete user from verifier file"},
242 {"list", OPT_LIST
, '-', "List users"},
243 {"gn", OPT_GN
, 's', "Set g and N values to be used for new verifier"},
244 {"userinfo", OPT_USERINFO
, 's', "Additional info to be set for user"},
245 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
246 {"passout", OPT_PASSOUT
, 's', "Output file pass phrase source"},
247 # ifndef OPENSSL_NO_ENGINE
248 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
253 int srp_main(int argc
, char **argv
)
258 int gNindex
= -1, maxgN
= -1, ret
= 1, errors
= 0, verbose
= 0, i
;
259 int doupdatedb
= 0, mode
= OPT_ERR
;
260 char *user
= NULL
, *passinarg
= NULL
, *passoutarg
= NULL
;
261 char *passin
= NULL
, *passout
= NULL
, *gN
= NULL
, *userinfo
= NULL
;
262 char *randfile
= NULL
, *tofree
= NULL
, *section
= NULL
;
263 char **gNrow
= NULL
, *configfile
= NULL
;
264 char *srpvfile
= NULL
, **pp
, *prog
;
267 prog
= opt_init(argc
, argv
, srp_options
);
268 while ((o
= opt_next()) != OPT_EOF
) {
273 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
276 opt_help(srp_options
);
283 configfile
= opt_arg();
289 srpvfile
= opt_arg();
295 if (mode
!= OPT_ERR
) {
297 "%s: Only one of -add/delete-modify/-list\n",
307 userinfo
= opt_arg();
310 passinarg
= opt_arg();
313 passoutarg
= opt_arg();
316 (void)setup_engine(opt_arg(), 0);
320 argc
= opt_num_rest();
323 if (srpvfile
&& configfile
) {
325 "-srpvfile and -configfile cannot be specified together.\n");
328 if (mode
== OPT_ERR
) {
330 "Exactly one of the options -add, -delete, -modify -list must be specified.\n");
333 if ((mode
== OPT_DELETE
|| mode
== OPT_MODIFY
|| mode
== OPT_ADD
)
336 "Need at least one user for options -add, -delete, -modify. \n");
339 if ((passin
|| passout
) && argc
!= 1) {
341 "-passin, -passout arguments only valid with one user.\n");
345 if (!app_passwd(passinarg
, passoutarg
, &passin
, &passout
)) {
346 BIO_printf(bio_err
, "Error getting passwords\n");
352 configfile
= default_config_file
;
355 BIO_printf(bio_err
, "Using configuration from %s\n",
357 conf
= app_load_config(configfile
);
360 if (!app_load_modules(conf
))
363 /* Lets get the config section we are using */
364 if (section
== NULL
) {
367 "trying to read " ENV_DEFAULT_SRP
368 " in \" BASE_SECTION \"\n");
370 section
= NCONF_get_string(conf
, BASE_SECTION
, ENV_DEFAULT_SRP
);
371 if (section
== NULL
) {
372 lookup_fail(BASE_SECTION
, ENV_DEFAULT_SRP
);
377 if (randfile
== NULL
&& conf
)
378 randfile
= NCONF_get_string(conf
, BASE_SECTION
, "RANDFILE");
382 "trying to read " ENV_DATABASE
" in section \"%s\"\n",
385 if ((srpvfile
= NCONF_get_string(conf
, section
, ENV_DATABASE
))
387 lookup_fail(section
, ENV_DATABASE
);
392 if (randfile
== NULL
)
395 app_RAND_load_file(randfile
, 0);
398 BIO_printf(bio_err
, "Trying to read SRP verifier file \"%s\"\n",
401 db
= load_index(srpvfile
, &db_attr
);
405 /* Lets check some fields */
406 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
407 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
409 if (pp
[DB_srptype
][0] == DB_SRP_INDEX
) {
411 if ((gNindex
< 0) && (gN
!= NULL
) && strcmp(gN
, pp
[DB_srpid
]) == 0)
414 print_index(db
, i
, verbose
> 1);
419 BIO_printf(bio_err
, "Database initialised\n");
422 gNrow
= sk_OPENSSL_PSTRING_value(db
->db
->data
, gNindex
);
423 print_entry(db
, gNindex
, verbose
> 1, "Default g and N");
424 } else if (maxgN
> 0 && !SRP_get_default_gN(gN
)) {
425 BIO_printf(bio_err
, "No g and N value for index \"%s\"\n", gN
);
429 BIO_printf(bio_err
, "Database has no g N information.\n");
434 BIO_printf(bio_err
, "Starting user processing\n");
439 while (mode
== OPT_LIST
|| user
) {
443 BIO_printf(bio_err
, "Processing user \"%s\"\n", user
);
444 if ((userindex
= get_index(db
, user
, 'U')) >= 0) {
445 print_user(db
, userindex
, (verbose
> 0)
446 || mode
== OPT_LIST
);
449 if (mode
== OPT_LIST
) {
451 BIO_printf(bio_err
, "List all users\n");
453 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
454 print_user(db
, i
, 1);
456 } else if (userindex
< 0) {
458 "user \"%s\" does not exist, ignored. t\n", user
);
461 } else if (mode
== OPT_ADD
) {
462 if (userindex
>= 0) {
463 /* reactivation of a new user */
465 sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
466 BIO_printf(bio_err
, "user \"%s\" reactivated.\n", user
);
467 row
[DB_srptype
][0] = 'V';
471 char *row
[DB_NUMBER
];
473 row
[DB_srpverifier
] = NULL
;
474 row
[DB_srpsalt
] = NULL
;
475 row
[DB_srpinfo
] = NULL
;
478 srp_create_user(user
, &(row
[DB_srpverifier
]),
480 gNrow
? gNrow
[DB_srpsalt
] : gN
,
481 gNrow
? gNrow
[DB_srpverifier
] : NULL
,
482 passout
, verbose
))) {
484 "Cannot create srp verifier for user \"%s\", operation abandoned .\n",
489 row
[DB_srpid
] = OPENSSL_strdup(user
);
490 row
[DB_srptype
] = OPENSSL_strdup("v");
491 row
[DB_srpgN
] = OPENSSL_strdup(gNid
);
493 if ((row
[DB_srpid
] == NULL
)
494 || (row
[DB_srpgN
] == NULL
)
495 || (row
[DB_srptype
] == NULL
)
496 || (row
[DB_srpverifier
] == NULL
)
497 || (row
[DB_srpsalt
] == NULL
)
499 && ((row
[DB_srpinfo
] = OPENSSL_strdup(userinfo
)) == NULL
))
500 || !update_index(db
, row
)) {
501 OPENSSL_free(row
[DB_srpid
]);
502 OPENSSL_free(row
[DB_srpgN
]);
503 OPENSSL_free(row
[DB_srpinfo
]);
504 OPENSSL_free(row
[DB_srptype
]);
505 OPENSSL_free(row
[DB_srpverifier
]);
506 OPENSSL_free(row
[DB_srpsalt
]);
511 } else if (mode
== OPT_MODIFY
) {
514 "user \"%s\" does not exist, operation ignored.\n",
520 sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
521 char type
= row
[DB_srptype
][0];
524 "user \"%s\" already updated, operation ignored.\n",
530 if (row
[DB_srptype
][0] == 'V') {
535 "Verifying password for user \"%s\"\n",
538 get_index(db
, row
[DB_srpgN
], DB_SRP_INDEX
)) >= 0)
540 sk_OPENSSL_PSTRING_value(db
->db
->data
,
544 (user
, row
[DB_srpverifier
], row
[DB_srpsalt
],
545 irow
? irow
[DB_srpsalt
] : row
[DB_srpgN
],
546 irow
? irow
[DB_srpverifier
] : NULL
, passin
,
549 "Invalid password for user \"%s\", operation abandoned.\n",
556 BIO_printf(bio_err
, "Password for user \"%s\" ok.\n",
561 srp_create_user(user
, &(row
[DB_srpverifier
]),
563 gNrow
? gNrow
[DB_srpsalt
] : NULL
,
564 gNrow
? gNrow
[DB_srpverifier
] : NULL
,
565 passout
, verbose
))) {
567 "Cannot create srp verifier for user \"%s\", operation abandoned.\n",
573 row
[DB_srptype
][0] = 'v';
574 row
[DB_srpgN
] = OPENSSL_strdup(gNid
);
576 if (row
[DB_srpid
] == NULL
577 || row
[DB_srpgN
] == NULL
578 || row
[DB_srptype
] == NULL
579 || row
[DB_srpverifier
] == NULL
580 || row
[DB_srpsalt
] == NULL
582 && ((row
[DB_srpinfo
] = OPENSSL_strdup(userinfo
))
589 } else if (mode
== OPT_DELETE
) {
592 "user \"%s\" does not exist, operation ignored. t\n",
596 char **xpp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
598 BIO_printf(bio_err
, "user \"%s\" revoked. t\n", user
);
599 xpp
[DB_srptype
][0] = 'R';
611 BIO_printf(bio_err
, "User procession done.\n");
614 /* Lets check some fields */
615 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
616 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
618 if (pp
[DB_srptype
][0] == 'v') {
619 pp
[DB_srptype
][0] = 'V';
620 print_user(db
, i
, verbose
);
625 BIO_printf(bio_err
, "Trying to update srpvfile.\n");
626 if (!save_index(srpvfile
, "new", db
))
630 BIO_printf(bio_err
, "Temporary srpvfile created.\n");
631 if (!rotate_index(srpvfile
, "new", "old"))
635 BIO_printf(bio_err
, "srpvfile updated.\n");
642 BIO_printf(bio_err
, "User errors %d.\n", errors
);
645 BIO_printf(bio_err
, "SRP terminating with code %d.\n", ret
);
646 OPENSSL_free(tofree
);
648 ERR_print_errors(bio_err
);
650 app_RAND_write_file(randfile
);
660 static void *dummy
= &dummy
;