]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/srp.c
Add fuzzing!
[thirdparty/openssl.git] / apps / srp.c
1 /*
2 * Written by Peter Sylvester (peter.sylvester@edelweb.fr) for the EdelKey
3 * project and contributed to the OpenSSL project 2004.
4 */
5 /* ====================================================================
6 * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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
18 * distribution.
19 *
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/)"
24 *
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.
29 *
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.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
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 * ====================================================================
52 *
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).
56 *
57 */
58
59 #include <openssl/opensslconf.h>
60 #ifdef OPENSSL_NO_SRP
61 NON_EMPTY_TRANSLATION_UNIT
62 #else
63
64 # include <stdio.h>
65 # include <stdlib.h>
66 # include <string.h>
67 # include <openssl/conf.h>
68 # include <openssl/bio.h>
69 # include <openssl/err.h>
70 # include <openssl/txt_db.h>
71 # include <openssl/buffer.h>
72 # include <openssl/srp.h>
73 # include "apps.h"
74
75 # define BASE_SECTION "srp"
76 # define CONFIG_FILE "openssl.cnf"
77
78 # define ENV_RANDFILE "RANDFILE"
79
80 # define ENV_DATABASE "srpvfile"
81 # define ENV_DEFAULT_SRP "default_srp"
82
83 static int get_index(CA_DB *db, char *id, char type)
84 {
85 char **pp;
86 int i;
87 if (id == NULL)
88 return -1;
89 if (type == DB_SRP_INDEX)
90 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
91 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
92 if (pp[DB_srptype][0] == DB_SRP_INDEX
93 && strcmp(id, pp[DB_srpid]) == 0)
94 return i;
95 } else
96 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
97 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
98
99 if (pp[DB_srptype][0] != DB_SRP_INDEX
100 && strcmp(id, pp[DB_srpid]) == 0)
101 return i;
102 }
103
104 return -1;
105 }
106
107 static void print_entry(CA_DB *db, int indx, int verbose, char *s)
108 {
109 if (indx >= 0 && verbose) {
110 int j;
111 char **pp = sk_OPENSSL_PSTRING_value(db->db->data, indx);
112 BIO_printf(bio_err, "%s \"%s\"\n", s, pp[DB_srpid]);
113 for (j = 0; j < DB_NUMBER; j++) {
114 BIO_printf(bio_err, " %d = \"%s\"\n", j, pp[j]);
115 }
116 }
117 }
118
119 static void print_index(CA_DB *db, int indexindex, int verbose)
120 {
121 print_entry(db, indexindex, verbose, "g N entry");
122 }
123
124 static void print_user(CA_DB *db, int userindex, int verbose)
125 {
126 if (verbose > 0) {
127 char **pp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);
128
129 if (pp[DB_srptype][0] != 'I') {
130 print_entry(db, userindex, verbose, "User entry");
131 print_entry(db, get_index(db, pp[DB_srpgN], 'I'), verbose,
132 "g N entry");
133 }
134
135 }
136 }
137
138 static int update_index(CA_DB *db, char **row)
139 {
140 char **irow;
141 int i;
142
143 irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row pointers");
144 for (i = 0; i < DB_NUMBER; i++) {
145 irow[i] = row[i];
146 row[i] = NULL;
147 }
148 irow[DB_NUMBER] = NULL;
149
150 if (!TXT_DB_insert(db->db, irow)) {
151 BIO_printf(bio_err, "failed to update srpvfile\n");
152 BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
153 OPENSSL_free(irow);
154 return 0;
155 }
156 return 1;
157 }
158
159 static void lookup_fail(const char *name, const char *tag)
160 {
161 BIO_printf(bio_err, "variable lookup failed for %s::%s\n", name, tag);
162 }
163
164 static char *srp_verify_user(const char *user, const char *srp_verifier,
165 char *srp_usersalt, const char *g, const char *N,
166 const char *passin, int verbose)
167 {
168 char password[1024];
169 PW_CB_DATA cb_tmp;
170 char *verifier = NULL;
171 char *gNid = NULL;
172
173 cb_tmp.prompt_info = user;
174 cb_tmp.password = passin;
175
176 if (password_callback(password, 1024, 0, &cb_tmp) > 0) {
177 if (verbose)
178 BIO_printf(bio_err,
179 "Validating\n user=\"%s\"\n srp_verifier=\"%s\"\n srp_usersalt=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
180 user, srp_verifier, srp_usersalt, g, N);
181 BIO_printf(bio_err, "Pass %s\n", password);
182
183 OPENSSL_assert(srp_usersalt != NULL);
184 if (!
185 (gNid =
186 SRP_create_verifier(user, password, &srp_usersalt, &verifier, N,
187 g))) {
188 BIO_printf(bio_err, "Internal error validating SRP verifier\n");
189 } else {
190 if (strcmp(verifier, srp_verifier))
191 gNid = NULL;
192 OPENSSL_free(verifier);
193 }
194 }
195 return gNid;
196 }
197
198 static char *srp_create_user(char *user, char **srp_verifier,
199 char **srp_usersalt, char *g, char *N,
200 char *passout, int verbose)
201 {
202 char password[1024];
203 PW_CB_DATA cb_tmp;
204 char *gNid = NULL;
205 char *salt = NULL;
206 cb_tmp.prompt_info = user;
207 cb_tmp.password = passout;
208
209 if (password_callback(password, 1024, 1, &cb_tmp) > 0) {
210 if (verbose)
211 BIO_printf(bio_err, "Creating\n user=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
212 user, g, N);
213 if (!
214 (gNid =
215 SRP_create_verifier(user, password, &salt, srp_verifier, N,
216 g))) {
217 BIO_printf(bio_err, "Internal error creating SRP verifier\n");
218 } else
219 *srp_usersalt = salt;
220 if (verbose > 1)
221 BIO_printf(bio_err, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n", gNid,
222 salt, *srp_verifier);
223
224 }
225 return gNid;
226 }
227
228 typedef enum OPTION_choice {
229 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
230 OPT_VERBOSE, OPT_CONFIG, OPT_NAME, OPT_SRPVFILE, OPT_ADD,
231 OPT_DELETE, OPT_MODIFY, OPT_LIST, OPT_GN, OPT_USERINFO,
232 OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE
233 } OPTION_CHOICE;
234
235 OPTIONS srp_options[] = {
236 {"help", OPT_HELP, '-', "Display this summary"},
237 {"verbose", OPT_VERBOSE, '-', "Talk a lot while doing things"},
238 {"config", OPT_CONFIG, '<', "A config file"},
239 {"name", OPT_NAME, 's', "The particular srp definition to use"},
240 {"srpvfile", OPT_SRPVFILE, '<', "The srp verifier file name"},
241 {"add", OPT_ADD, '-', "Add a user and srp verifier"},
242 {"modify", OPT_MODIFY, '-',
243 "Modify the srp verifier of an existing user"},
244 {"delete", OPT_DELETE, '-', "Delete user from verifier file"},
245 {"list", OPT_LIST, '-', "List users"},
246 {"gn", OPT_GN, 's', "Set g and N values to be used for new verifier"},
247 {"userinfo", OPT_USERINFO, 's', "Additional info to be set for user"},
248 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
249 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
250 # ifndef OPENSSL_NO_ENGINE
251 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
252 # endif
253 {NULL}
254 };
255
256 int srp_main(int argc, char **argv)
257 {
258 CA_DB *db = NULL;
259 CONF *conf = NULL;
260 int gNindex = -1, maxgN = -1, ret = 1, errors = 0, verbose = 0, i;
261 int doupdatedb = 0, mode = OPT_ERR;
262 char *user = NULL, *passinarg = NULL, *passoutarg = NULL;
263 char *passin = NULL, *passout = NULL, *gN = NULL, *userinfo = NULL;
264 char *randfile = NULL, *tofree = NULL, *section = NULL;
265 char **gNrow = NULL, *configfile = NULL;
266 char *srpvfile = NULL, **pp, *prog;
267 OPTION_CHOICE o;
268
269 prog = opt_init(argc, argv, srp_options);
270 while ((o = opt_next()) != OPT_EOF) {
271 switch (o) {
272 case OPT_EOF:
273 case OPT_ERR:
274 opthelp:
275 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
276 goto end;
277 case OPT_HELP:
278 opt_help(srp_options);
279 ret = 0;
280 goto end;
281 case OPT_VERBOSE:
282 verbose++;
283 break;
284 case OPT_CONFIG:
285 configfile = opt_arg();
286 break;
287 case OPT_NAME:
288 section = opt_arg();
289 break;
290 case OPT_SRPVFILE:
291 srpvfile = opt_arg();
292 break;
293 case OPT_ADD:
294 case OPT_DELETE:
295 case OPT_MODIFY:
296 case OPT_LIST:
297 if (mode != OPT_ERR) {
298 BIO_printf(bio_err,
299 "%s: Only one of -add/delete-modify/-list\n",
300 prog);
301 goto opthelp;
302 }
303 mode = o;
304 break;
305 case OPT_GN:
306 gN = opt_arg();
307 break;
308 case OPT_USERINFO:
309 userinfo = opt_arg();
310 break;
311 case OPT_PASSIN:
312 passinarg = opt_arg();
313 break;
314 case OPT_PASSOUT:
315 passoutarg = opt_arg();
316 break;
317 case OPT_ENGINE:
318 (void)setup_engine(opt_arg(), 0);
319 break;
320 }
321 }
322 argc = opt_num_rest();
323 argv = opt_rest();
324
325 if (srpvfile && configfile) {
326 BIO_printf(bio_err,
327 "-srpvfile and -configfile cannot be specified together.\n");
328 goto end;
329 }
330 if (mode == OPT_ERR) {
331 BIO_printf(bio_err,
332 "Exactly one of the options -add, -delete, -modify -list must be specified.\n");
333 goto opthelp;
334 }
335 if ((mode == OPT_DELETE || mode == OPT_MODIFY || mode == OPT_ADD)
336 && argc < 1) {
337 BIO_printf(bio_err,
338 "Need at least one user for options -add, -delete, -modify. \n");
339 goto opthelp;
340 }
341 if ((passin || passout) && argc != 1) {
342 BIO_printf(bio_err,
343 "-passin, -passout arguments only valid with one user.\n");
344 goto opthelp;
345 }
346
347 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
348 BIO_printf(bio_err, "Error getting passwords\n");
349 goto end;
350 }
351
352 if (!srpvfile) {
353 if (!configfile)
354 configfile = default_config_file;
355
356 if (verbose)
357 BIO_printf(bio_err, "Using configuration from %s\n",
358 configfile);
359 conf = app_load_config(configfile);
360 if (conf == NULL)
361 goto end;
362 if (!app_load_modules(conf))
363 goto end;
364
365 /* Lets get the config section we are using */
366 if (section == NULL) {
367 if (verbose)
368 BIO_printf(bio_err,
369 "trying to read " ENV_DEFAULT_SRP
370 " in " BASE_SECTION "\n");
371
372 section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_SRP);
373 if (section == NULL) {
374 lookup_fail(BASE_SECTION, ENV_DEFAULT_SRP);
375 goto end;
376 }
377 }
378
379 if (randfile == NULL && conf)
380 randfile = NCONF_get_string(conf, BASE_SECTION, "RANDFILE");
381
382 if (verbose)
383 BIO_printf(bio_err,
384 "trying to read " ENV_DATABASE " in section \"%s\"\n",
385 section);
386
387 if ((srpvfile = NCONF_get_string(conf, section, ENV_DATABASE))
388 == NULL) {
389 lookup_fail(section, ENV_DATABASE);
390 goto end;
391 }
392
393 }
394 if (randfile == NULL)
395 ERR_clear_error();
396 else
397 app_RAND_load_file(randfile, 0);
398
399 if (verbose)
400 BIO_printf(bio_err, "Trying to read SRP verifier file \"%s\"\n",
401 srpvfile);
402
403 db = load_index(srpvfile, NULL);
404 if (db == NULL)
405 goto end;
406
407 /* Lets check some fields */
408 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
409 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
410
411 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
412 maxgN = i;
413 if ((gNindex < 0) && (gN != NULL) && strcmp(gN, pp[DB_srpid]) == 0)
414 gNindex = i;
415
416 print_index(db, i, verbose > 1);
417 }
418 }
419
420 if (verbose)
421 BIO_printf(bio_err, "Database initialised\n");
422
423 if (gNindex >= 0) {
424 gNrow = sk_OPENSSL_PSTRING_value(db->db->data, gNindex);
425 print_entry(db, gNindex, verbose > 1, "Default g and N");
426 } else if (maxgN > 0 && !SRP_get_default_gN(gN)) {
427 BIO_printf(bio_err, "No g and N value for index \"%s\"\n", gN);
428 goto end;
429 } else {
430 if (verbose)
431 BIO_printf(bio_err, "Database has no g N information.\n");
432 gNrow = NULL;
433 }
434
435 if (verbose > 1)
436 BIO_printf(bio_err, "Starting user processing\n");
437
438 if (argc > 0)
439 user = *(argv++);
440
441 while (mode == OPT_LIST || user) {
442 int userindex = -1;
443 if (user)
444 if (verbose > 1)
445 BIO_printf(bio_err, "Processing user \"%s\"\n", user);
446 if ((userindex = get_index(db, user, 'U')) >= 0) {
447 print_user(db, userindex, (verbose > 0)
448 || mode == OPT_LIST);
449 }
450
451 if (mode == OPT_LIST) {
452 if (user == NULL) {
453 BIO_printf(bio_err, "List all users\n");
454
455 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
456 print_user(db, i, 1);
457 }
458 } else if (userindex < 0) {
459 BIO_printf(bio_err,
460 "user \"%s\" does not exist, ignored. t\n", user);
461 errors++;
462 }
463 } else if (mode == OPT_ADD) {
464 if (userindex >= 0) {
465 /* reactivation of a new user */
466 char **row =
467 sk_OPENSSL_PSTRING_value(db->db->data, userindex);
468 BIO_printf(bio_err, "user \"%s\" reactivated.\n", user);
469 row[DB_srptype][0] = 'V';
470
471 doupdatedb = 1;
472 } else {
473 char *row[DB_NUMBER];
474 char *gNid;
475 row[DB_srpverifier] = NULL;
476 row[DB_srpsalt] = NULL;
477 row[DB_srpinfo] = NULL;
478 if (!
479 (gNid =
480 srp_create_user(user, &(row[DB_srpverifier]),
481 &(row[DB_srpsalt]),
482 gNrow ? gNrow[DB_srpsalt] : gN,
483 gNrow ? gNrow[DB_srpverifier] : NULL,
484 passout, verbose))) {
485 BIO_printf(bio_err,
486 "Cannot create srp verifier for user \"%s\", operation abandoned .\n",
487 user);
488 errors++;
489 goto end;
490 }
491 row[DB_srpid] = OPENSSL_strdup(user);
492 row[DB_srptype] = OPENSSL_strdup("v");
493 row[DB_srpgN] = OPENSSL_strdup(gNid);
494
495 if ((row[DB_srpid] == NULL)
496 || (row[DB_srpgN] == NULL)
497 || (row[DB_srptype] == NULL)
498 || (row[DB_srpverifier] == NULL)
499 || (row[DB_srpsalt] == NULL)
500 || (userinfo
501 && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo)) == NULL))
502 || !update_index(db, row)) {
503 OPENSSL_free(row[DB_srpid]);
504 OPENSSL_free(row[DB_srpgN]);
505 OPENSSL_free(row[DB_srpinfo]);
506 OPENSSL_free(row[DB_srptype]);
507 OPENSSL_free(row[DB_srpverifier]);
508 OPENSSL_free(row[DB_srpsalt]);
509 goto end;
510 }
511 doupdatedb = 1;
512 }
513 } else if (mode == OPT_MODIFY) {
514 if (userindex < 0) {
515 BIO_printf(bio_err,
516 "user \"%s\" does not exist, operation ignored.\n",
517 user);
518 errors++;
519 } else {
520
521 char **row =
522 sk_OPENSSL_PSTRING_value(db->db->data, userindex);
523 char type = row[DB_srptype][0];
524 if (type == 'v') {
525 BIO_printf(bio_err,
526 "user \"%s\" already updated, operation ignored.\n",
527 user);
528 errors++;
529 } else {
530 char *gNid;
531
532 if (row[DB_srptype][0] == 'V') {
533 int user_gN;
534 char **irow = NULL;
535 if (verbose)
536 BIO_printf(bio_err,
537 "Verifying password for user \"%s\"\n",
538 user);
539 if ((user_gN =
540 get_index(db, row[DB_srpgN], DB_SRP_INDEX)) >= 0)
541 irow =
542 sk_OPENSSL_PSTRING_value(db->db->data,
543 userindex);
544
545 if (!srp_verify_user
546 (user, row[DB_srpverifier], row[DB_srpsalt],
547 irow ? irow[DB_srpsalt] : row[DB_srpgN],
548 irow ? irow[DB_srpverifier] : NULL, passin,
549 verbose)) {
550 BIO_printf(bio_err,
551 "Invalid password for user \"%s\", operation abandoned.\n",
552 user);
553 errors++;
554 goto end;
555 }
556 }
557 if (verbose)
558 BIO_printf(bio_err, "Password for user \"%s\" ok.\n",
559 user);
560
561 if (!
562 (gNid =
563 srp_create_user(user, &(row[DB_srpverifier]),
564 &(row[DB_srpsalt]),
565 gNrow ? gNrow[DB_srpsalt] : NULL,
566 gNrow ? gNrow[DB_srpverifier] : NULL,
567 passout, verbose))) {
568 BIO_printf(bio_err,
569 "Cannot create srp verifier for user \"%s\", operation abandoned.\n",
570 user);
571 errors++;
572 goto end;
573 }
574
575 row[DB_srptype][0] = 'v';
576 row[DB_srpgN] = OPENSSL_strdup(gNid);
577
578 if (row[DB_srpid] == NULL
579 || row[DB_srpgN] == NULL
580 || row[DB_srptype] == NULL
581 || row[DB_srpverifier] == NULL
582 || row[DB_srpsalt] == NULL
583 || (userinfo
584 && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo))
585 == NULL)))
586 goto end;
587
588 doupdatedb = 1;
589 }
590 }
591 } else if (mode == OPT_DELETE) {
592 if (userindex < 0) {
593 BIO_printf(bio_err,
594 "user \"%s\" does not exist, operation ignored. t\n",
595 user);
596 errors++;
597 } else {
598 char **xpp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);
599
600 BIO_printf(bio_err, "user \"%s\" revoked. t\n", user);
601 xpp[DB_srptype][0] = 'R';
602 doupdatedb = 1;
603 }
604 }
605 if (--argc > 0)
606 user = *(argv++);
607 else {
608 user = NULL;
609 }
610 }
611
612 if (verbose)
613 BIO_printf(bio_err, "User procession done.\n");
614
615 if (doupdatedb) {
616 /* Lets check some fields */
617 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
618 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
619
620 if (pp[DB_srptype][0] == 'v') {
621 pp[DB_srptype][0] = 'V';
622 print_user(db, i, verbose);
623 }
624 }
625
626 if (verbose)
627 BIO_printf(bio_err, "Trying to update srpvfile.\n");
628 if (!save_index(srpvfile, "new", db))
629 goto end;
630
631 if (verbose)
632 BIO_printf(bio_err, "Temporary srpvfile created.\n");
633 if (!rotate_index(srpvfile, "new", "old"))
634 goto end;
635
636 if (verbose)
637 BIO_printf(bio_err, "srpvfile updated.\n");
638 }
639
640 ret = (errors != 0);
641 end:
642 if (errors != 0)
643 if (verbose)
644 BIO_printf(bio_err, "User errors %d.\n", errors);
645
646 if (verbose)
647 BIO_printf(bio_err, "SRP terminating with code %d.\n", ret);
648 OPENSSL_free(tofree);
649 if (ret)
650 ERR_print_errors(bio_err);
651 if (randfile)
652 app_RAND_write_file(randfile);
653 NCONF_free(conf);
654 free_index(db);
655 return (ret);
656 }
657 #endif