]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/srp.c
3 * Written by Peter Sylvester (peter.sylvester@edelweb.fr) for the EdelKey
4 * project and contributed to the OpenSSL project 2004.
6 /* ====================================================================
7 * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
59 #include <openssl/opensslconf.h>
61 #ifndef OPENSSL_NO_SRP
65 # include <openssl/conf.h>
66 # include <openssl/bio.h>
67 # include <openssl/err.h>
68 # include <openssl/txt_db.h>
69 # include <openssl/buffer.h>
70 # include <openssl/srp.h>
75 # define PROG srp_main
77 # define BASE_SECTION "srp"
78 # define CONFIG_FILE "openssl.cnf"
80 # define ENV_RANDFILE "RANDFILE"
82 # define ENV_DATABASE "srpvfile"
83 # define ENV_DEFAULT_SRP "default_srp"
85 static char *srp_usage
[] = {
86 "usage: srp [args] [user] \n",
88 " -verbose Talk a lot while doing things\n",
89 " -config file A config file\n",
90 " -name arg The particular srp definition to use\n",
91 " -srpvfile arg The srp verifier file name\n",
92 " -add add an user and srp verifier\n",
93 " -modify modify the srp verifier of an existing user\n",
94 " -delete delete user from verifier file\n",
96 " -gn arg g and N values to be used for new verifier\n",
97 " -userinfo arg additional info to be set for user\n",
98 " -passin arg input file pass phrase source\n",
99 " -passout arg output file pass phrase source\n",
100 # ifndef OPENSSL_NO_ENGINE
101 " -engine e - use engine e, possibly a hardware device.\n",
107 extern int EF_PROTECT_FREE
;
108 extern int EF_PROTECT_BELOW
;
109 extern int EF_ALIGNMENT
;
112 static CONF
*conf
= NULL
;
113 static char *section
= NULL
;
115 # define VERBOSE if (verbose)
116 # define VVERBOSE if (verbose>1)
118 int MAIN(int, char **);
120 static int get_index(CA_DB
*db
, char *id
, char type
)
126 if (type
== DB_SRP_INDEX
)
127 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
128 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
129 if (pp
[DB_srptype
][0] == DB_SRP_INDEX
130 && !strcmp(id
, pp
[DB_srpid
]))
133 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
134 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
136 if (pp
[DB_srptype
][0] != DB_SRP_INDEX
137 && !strcmp(id
, pp
[DB_srpid
]))
144 static void print_entry(CA_DB
*db
, BIO
*bio
, int indx
, int verbose
, char *s
)
146 if (indx
>= 0 && verbose
) {
148 char **pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, indx
);
149 BIO_printf(bio
, "%s \"%s\"\n", s
, pp
[DB_srpid
]);
150 for (j
= 0; j
< DB_NUMBER
; j
++) {
151 BIO_printf(bio_err
, " %d = \"%s\"\n", j
, pp
[j
]);
156 static void print_index(CA_DB
*db
, BIO
*bio
, int indexindex
, int verbose
)
158 print_entry(db
, bio
, indexindex
, verbose
, "g N entry");
161 static void print_user(CA_DB
*db
, BIO
*bio
, int userindex
, int verbose
)
164 char **pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
166 if (pp
[DB_srptype
][0] != 'I') {
167 print_entry(db
, bio
, userindex
, verbose
, "User entry");
168 print_entry(db
, bio
, get_index(db
, pp
[DB_srpgN
], 'I'), verbose
,
175 static int update_index(CA_DB
*db
, BIO
*bio
, char **row
)
181 (char **)OPENSSL_malloc(sizeof(char *) * (DB_NUMBER
+ 1))) == NULL
) {
182 BIO_printf(bio_err
, "Memory allocation failure\n");
186 for (i
= 0; i
< DB_NUMBER
; i
++) {
190 irow
[DB_NUMBER
] = NULL
;
192 if (!TXT_DB_insert(db
->db
, irow
)) {
193 BIO_printf(bio
, "failed to update srpvfile\n");
194 BIO_printf(bio
, "TXT_DB error number %ld\n", db
->db
->error
);
201 static void lookup_fail(const char *name
, const char *tag
)
203 BIO_printf(bio_err
, "variable lookup failed for %s::%s\n", name
, tag
);
206 static char *srp_verify_user(const char *user
, const char *srp_verifier
,
207 char *srp_usersalt
, const char *g
, const char *N
,
208 const char *passin
, BIO
*bio
, int verbose
)
212 char *verifier
= NULL
;
215 cb_tmp
.prompt_info
= user
;
216 cb_tmp
.password
= passin
;
218 if (password_callback(password
, 1024, 0, &cb_tmp
) > 0) {
219 VERBOSE
BIO_printf(bio
,
222 " srp_verifier=\"%s\"\n"
223 " srp_usersalt=\"%s\"\n"
224 " g=\"%s\"\n N=\"%s\"\n",
225 user
, srp_verifier
, srp_usersalt
, g
, N
);
226 BIO_printf(bio
, "Pass %s\n", password
);
228 OPENSSL_assert(srp_usersalt
!= NULL
);
229 if (!(gNid
= SRP_create_verifier(user
, password
, &srp_usersalt
,
231 BIO_printf(bio
, "Internal error validating SRP verifier\n");
233 if (strcmp(verifier
, srp_verifier
))
235 OPENSSL_free(verifier
);
241 static char *srp_create_user(char *user
, char **srp_verifier
,
242 char **srp_usersalt
, char *g
, char *N
,
243 char *passout
, BIO
*bio
, int verbose
)
249 cb_tmp
.prompt_info
= user
;
250 cb_tmp
.password
= passout
;
252 if (password_callback(password
, 1024, 1, &cb_tmp
) > 0) {
253 VERBOSE
BIO_printf(bio
,
256 " g=\"%s\"\n" " N=\"%s\"\n", user
, g
, N
);
257 if (!(gNid
= SRP_create_verifier(user
, password
, &salt
,
258 srp_verifier
, N
, g
))) {
259 BIO_printf(bio
, "Internal error creating SRP verifier\n");
261 *srp_usersalt
= salt
;
262 VVERBOSE
BIO_printf(bio
, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n",
263 gNid
, salt
, *srp_verifier
);
269 int MAIN(int argc
, char **argv
)
277 char *passargin
= NULL
, *passargout
= NULL
;
278 char *passin
= NULL
, *passout
= NULL
;
284 char *userinfo
= NULL
;
291 char *configfile
= NULL
;
297 char *randfile
= NULL
;
298 # ifndef OPENSSL_NO_ENGINE
306 EF_PROTECT_BELOW
= 1;
316 if ((bio_err
= BIO_new(BIO_s_file())) != NULL
)
317 BIO_set_fp(bio_err
, stderr
, BIO_NOCLOSE
| BIO_FP_TEXT
);
321 while (argc
>= 1 && badops
== 0) {
322 if (strcmp(*argv
, "-verbose") == 0)
324 else if (strcmp(*argv
, "-config") == 0) {
327 configfile
= *(++argv
);
328 } else if (strcmp(*argv
, "-name") == 0) {
332 } else if (strcmp(*argv
, "-srpvfile") == 0) {
336 } else if (strcmp(*argv
, "-add") == 0)
338 else if (strcmp(*argv
, "-delete") == 0)
340 else if (strcmp(*argv
, "-modify") == 0)
342 else if (strcmp(*argv
, "-list") == 0)
344 else if (strcmp(*argv
, "-gn") == 0) {
348 } else if (strcmp(*argv
, "-userinfo") == 0) {
351 userinfo
= *(++argv
);
352 } else if (strcmp(*argv
, "-passin") == 0) {
355 passargin
= *(++argv
);
356 } else if (strcmp(*argv
, "-passout") == 0) {
359 passargout
= *(++argv
);
361 # ifndef OPENSSL_NO_ENGINE
362 else if (strcmp(*argv
, "-engine") == 0) {
369 else if (**argv
== '-') {
371 BIO_printf(bio_err
, "unknown option %s\n", *argv
);
381 if (dbfile
&& configfile
) {
383 "-dbfile and -configfile cannot be specified together.\n");
386 if (add_user
+ delete_user
+ modify_user
+ list_user
!= 1) {
387 BIO_printf(bio_err
, "Exactly one of the options "
388 "-add, -delete, -modify -list must be specified.\n");
391 if (delete_user
+ modify_user
+ delete_user
== 1 && argc
<= 0) {
392 BIO_printf(bio_err
, "Need at least one user for options "
393 "-add, -delete, -modify. \n");
396 if ((passin
|| passout
) && argc
!= 1) {
398 "-passin, -passout arguments only valid with one user.\n");
403 for (pp
= srp_usage
; (*pp
!= NULL
); pp
++)
404 BIO_printf(bio_err
, "%s", *pp
);
406 BIO_printf(bio_err
, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR
,
407 LIST_SEPARATOR_CHAR
);
409 " load the file (or the files in the directory) into\n");
410 BIO_printf(bio_err
, " the random number generator\n");
414 ERR_load_crypto_strings();
416 # ifndef OPENSSL_NO_ENGINE
417 setup_engine(bio_err
, engine
, 0);
420 if (!app_passwd(bio_err
, passargin
, passargout
, &passin
, &passout
)) {
421 BIO_printf(bio_err
, "Error getting passwords\n");
427 /*****************************************************************/
429 if (configfile
== NULL
)
430 configfile
= getenv("OPENSSL_CONF");
431 if (configfile
== NULL
)
432 configfile
= getenv("SSLEAY_CONF");
433 if (configfile
== NULL
) {
434 const char *s
= X509_get_default_cert_area();
437 # ifdef OPENSSL_SYS_VMS
438 len
= strlen(s
) + sizeof(CONFIG_FILE
);
439 tofree
= OPENSSL_malloc(len
);
441 BIO_printf(bio_err
, "Out of memory\n");
446 len
= strlen(s
) + sizeof(CONFIG_FILE
) + 1;
447 tofree
= OPENSSL_malloc(len
);
449 BIO_printf(bio_err
, "Out of memory\n");
452 BUF_strlcpy(tofree
, s
, len
);
453 BUF_strlcat(tofree
, "/", len
);
455 BUF_strlcat(tofree
, CONFIG_FILE
, len
);
459 VERBOSE
BIO_printf(bio_err
, "Using configuration from %s\n",
461 conf
= NCONF_new(NULL
);
462 if (NCONF_load(conf
, configfile
, &errorline
) <= 0) {
464 BIO_printf(bio_err
, "error loading the config file '%s'\n",
467 BIO_printf(bio_err
, "error on line %ld of config file '%s'\n",
468 errorline
, configfile
);
472 OPENSSL_free(tofree
);
476 if (!load_config(bio_err
, conf
))
479 /* Lets get the config section we are using */
480 if (section
== NULL
) {
481 VERBOSE
BIO_printf(bio_err
,
482 "trying to read " ENV_DEFAULT_SRP
483 " in \" BASE_SECTION \"\n");
485 section
= NCONF_get_string(conf
, BASE_SECTION
, ENV_DEFAULT_SRP
);
486 if (section
== NULL
) {
487 lookup_fail(BASE_SECTION
, ENV_DEFAULT_SRP
);
492 if (randfile
== NULL
&& conf
)
493 randfile
= NCONF_get_string(conf
, BASE_SECTION
, "RANDFILE");
495 VERBOSE
BIO_printf(bio_err
,
496 "trying to read " ENV_DATABASE
497 " in section \"%s\"\n", section
);
499 if ((dbfile
= NCONF_get_string(conf
, section
, ENV_DATABASE
)) == NULL
) {
500 lookup_fail(section
, ENV_DATABASE
);
505 if (randfile
== NULL
)
508 app_RAND_load_file(randfile
, bio_err
, 0);
510 VERBOSE
BIO_printf(bio_err
, "Trying to read SRP verifier file \"%s\"\n",
513 db
= load_index(dbfile
, &db_attr
);
517 /* Lets check some fields */
518 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
519 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
521 if (pp
[DB_srptype
][0] == DB_SRP_INDEX
) {
523 if (gNindex
< 0 && gN
!= NULL
&& !strcmp(gN
, pp
[DB_srpid
]))
526 print_index(db
, bio_err
, i
, verbose
> 1);
530 VERBOSE
BIO_printf(bio_err
, "Database initialised\n");
533 gNrow
= sk_OPENSSL_PSTRING_value(db
->db
->data
, gNindex
);
534 print_entry(db
, bio_err
, gNindex
, verbose
> 1, "Default g and N");
535 } else if (maxgN
> 0 && !SRP_get_default_gN(gN
)) {
536 BIO_printf(bio_err
, "No g and N value for index \"%s\"\n", gN
);
539 VERBOSE
BIO_printf(bio_err
, "Database has no g N information.\n");
543 VVERBOSE
BIO_printf(bio_err
, "Starting user processing\n");
548 while (list_user
|| user
) {
551 VVERBOSE
BIO_printf(bio_err
, "Processing user \"%s\"\n", user
);
552 if ((userindex
= get_index(db
, user
, 'U')) >= 0) {
553 print_user(db
, bio_err
, userindex
, (verbose
> 0) || list_user
);
558 BIO_printf(bio_err
, "List all users\n");
560 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
561 print_user(db
, bio_err
, i
, 1);
564 } else if (userindex
< 0) {
566 "user \"%s\" does not exist, ignored. t\n", user
);
569 } else if (add_user
) {
570 if (userindex
>= 0) {
571 /* reactivation of a new user */
573 sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
574 BIO_printf(bio_err
, "user \"%s\" reactivated.\n", user
);
575 row
[DB_srptype
][0] = 'V';
579 char *row
[DB_NUMBER
];
581 row
[DB_srpverifier
] = NULL
;
582 row
[DB_srpsalt
] = NULL
;
583 row
[DB_srpinfo
] = NULL
;
584 if (!(gNid
= srp_create_user(user
, &(row
[DB_srpverifier
]),
586 gNrow
? gNrow
[DB_srpsalt
] : gN
,
587 gNrow
? gNrow
[DB_srpverifier
] :
588 NULL
, passout
, bio_err
,
591 "Cannot create srp verifier for user \"%s\","
592 " operation abandoned .\n", user
);
596 row
[DB_srpid
] = BUF_strdup(user
);
597 row
[DB_srptype
] = BUF_strdup("v");
598 row
[DB_srpgN
] = BUF_strdup(gNid
);
600 if (!row
[DB_srpid
] || !row
[DB_srpgN
] || !row
[DB_srptype
]
601 || !row
[DB_srpverifier
] || !row
[DB_srpsalt
]
603 && (!(row
[DB_srpinfo
] = BUF_strdup(userinfo
))))
604 || !update_index(db
, bio_err
, row
)) {
606 OPENSSL_free(row
[DB_srpid
]);
608 OPENSSL_free(row
[DB_srpgN
]);
610 OPENSSL_free(row
[DB_srpinfo
]);
612 OPENSSL_free(row
[DB_srptype
]);
613 if (row
[DB_srpverifier
])
614 OPENSSL_free(row
[DB_srpverifier
]);
616 OPENSSL_free(row
[DB_srpsalt
]);
621 } else if (modify_user
) {
624 "user \"%s\" does not exist, operation ignored.\n",
630 sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
631 char type
= row
[DB_srptype
][0];
634 "user \"%s\" already updated, operation ignored.\n",
640 if (row
[DB_srptype
][0] == 'V') {
643 VERBOSE
BIO_printf(bio_err
,
644 "Verifying password for user \"%s\"\n",
647 get_index(db
, row
[DB_srpgN
], DB_SRP_INDEX
)) >= 0)
649 sk_OPENSSL_PSTRING_value(db
->db
->data
,
653 (user
, row
[DB_srpverifier
], row
[DB_srpsalt
],
654 irow
? irow
[DB_srpsalt
] : row
[DB_srpgN
],
655 irow
? irow
[DB_srpverifier
] : NULL
, passin
,
658 "Invalid password for user \"%s\", operation abandoned.\n",
664 VERBOSE
BIO_printf(bio_err
,
665 "Password for user \"%s\" ok.\n",
668 if (!(gNid
= srp_create_user(user
, &(row
[DB_srpverifier
]),
670 gNrow
? gNrow
[DB_srpsalt
] :
672 gNrow
? gNrow
[DB_srpverifier
]
673 : NULL
, passout
, bio_err
,
676 "Cannot create srp verifier for user \"%s\","
677 " operation abandoned.\n", user
);
682 row
[DB_srptype
][0] = 'v';
683 row
[DB_srpgN
] = BUF_strdup(gNid
);
685 if (!row
[DB_srpid
] || !row
[DB_srpgN
] || !row
[DB_srptype
]
686 || !row
[DB_srpverifier
] || !row
[DB_srpsalt
]
688 && (!(row
[DB_srpinfo
] = BUF_strdup(userinfo
)))))
694 } else if (delete_user
) {
697 "user \"%s\" does not exist, operation ignored. t\n",
702 sk_OPENSSL_PSTRING_value(db
->db
->data
, userindex
);
703 BIO_printf(bio_err
, "user \"%s\" revoked. t\n", user
);
705 xpp
[DB_srptype
][0] = 'R';
718 VERBOSE
BIO_printf(bio_err
, "User procession done.\n");
721 /* Lets check some fields */
722 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
723 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
725 if (pp
[DB_srptype
][0] == 'v') {
726 pp
[DB_srptype
][0] = 'V';
727 print_user(db
, bio_err
, i
, verbose
);
731 VERBOSE
BIO_printf(bio_err
, "Trying to update srpvfile.\n");
732 if (!save_index(dbfile
, "new", db
))
735 VERBOSE
BIO_printf(bio_err
, "Temporary srpvfile created.\n");
736 if (!rotate_index(dbfile
, "new", "old"))
739 VERBOSE
BIO_printf(bio_err
, "srpvfile updated.\n");
745 VERBOSE
BIO_printf(bio_err
, "User errors %d.\n", errors
);
747 VERBOSE
BIO_printf(bio_err
, "SRP terminating with code %d.\n", ret
);
749 OPENSSL_free(tofree
);
751 ERR_print_errors(bio_err
);
753 app_RAND_write_file(randfile
, bio_err
);