]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/srp.c
f7edfa99305b4d232fd6375332fba276e380d9c1
[thirdparty/openssl.git] / apps / srp.c
1 /*
2 * Copyright 2004-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
4 *
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
9 *
10 * Originally written by Christophe Renou and Peter Sylvester,
11 * for the EdelKey project.
12 */
13
14 #include <openssl/opensslconf.h>
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.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>
25 #include "apps.h"
26 #include "progs.h"
27
28 #define BASE_SECTION "srp"
29 #define CONFIG_FILE "openssl.cnf"
30
31
32 #define ENV_DATABASE "srpvfile"
33 #define ENV_DEFAULT_SRP "default_srp"
34
35 static int get_index(CA_DB *db, char *id, char type)
36 {
37 char **pp;
38 int i;
39 if (id == NULL)
40 return -1;
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)
46 return i;
47 }
48 } else {
49 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
50 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
51
52 if (pp[DB_srptype][0] != DB_SRP_INDEX
53 && strcmp(id, pp[DB_srpid]) == 0)
54 return i;
55 }
56 }
57
58 return -1;
59 }
60
61 static void print_entry(CA_DB *db, int indx, int verbose, char *s)
62 {
63 if (indx >= 0 && verbose) {
64 int j;
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]);
69 }
70 }
71 }
72
73 static void print_index(CA_DB *db, int indexindex, int verbose)
74 {
75 print_entry(db, indexindex, verbose, "g N entry");
76 }
77
78 static void print_user(CA_DB *db, int userindex, int verbose)
79 {
80 if (verbose > 0) {
81 char **pp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);
82
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,
86 "g N entry");
87 }
88
89 }
90 }
91
92 static int update_index(CA_DB *db, char **row)
93 {
94 char **irow;
95 int i;
96
97 irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row pointers");
98 for (i = 0; i < DB_NUMBER; i++)
99 irow[i] = row[i];
100 irow[DB_NUMBER] = NULL;
101
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);
105 OPENSSL_free(irow);
106 return 0;
107 }
108 return 1;
109 }
110
111 static char *lookup_conf(const CONF *conf, const char *section, const char *tag)
112 {
113 char *entry = NCONF_get_string(conf, section, tag);
114 if (entry == NULL)
115 BIO_printf(bio_err, "variable lookup failed for %s::%s\n", section, tag);
116 return entry;
117 }
118
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)
122 {
123 char password[1025];
124 PW_CB_DATA cb_tmp;
125 char *verifier = NULL;
126 char *gNid = NULL;
127 int len;
128
129 cb_tmp.prompt_info = user;
130 cb_tmp.password = passin;
131
132 len = password_callback(password, sizeof(password)-1, 0, &cb_tmp);
133 if (len > 0) {
134 password[len] = 0;
135 if (verbose)
136 BIO_printf(bio_err,
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);
139 if (verbose > 1)
140 BIO_printf(bio_err, "Pass %s\n", password);
141
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");
146 } else {
147 if (strcmp(verifier, srp_verifier))
148 gNid = NULL;
149 OPENSSL_free(verifier);
150 }
151 OPENSSL_cleanse(password, len);
152 }
153 return gNid;
154 }
155
156 static char *srp_create_user(char *user, char **srp_verifier,
157 char **srp_usersalt, char *g, char *N,
158 char *passout, int verbose)
159 {
160 char password[1025];
161 PW_CB_DATA cb_tmp;
162 char *gNid = NULL;
163 char *salt = NULL;
164 int len;
165 cb_tmp.prompt_info = user;
166 cb_tmp.password = passout;
167
168 len = password_callback(password, sizeof(password)-1, 1, &cb_tmp);
169 if (len > 0) {
170 password[len] = 0;
171 if (verbose)
172 BIO_printf(bio_err, "Creating\n user=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
173 user, g, 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");
177 } else {
178 *srp_usersalt = salt;
179 }
180 OPENSSL_cleanse(password, len);
181 if (verbose > 1)
182 BIO_printf(bio_err, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n",
183 gNid, salt, *srp_verifier);
184
185 }
186 return gNid;
187 }
188
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
194 } OPTION_CHOICE;
195
196 const OPTIONS srp_options[] = {
197 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [user...]\n"},
198
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"},
206 #endif
207
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"},
213
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"},
220
221 OPT_R_OPTIONS,
222 OPT_PROV_OPTIONS,
223
224 OPT_PARAMETERS(),
225 {"user", 0, 0, "Username(s) to process (optional)"},
226 {NULL}
227 };
228
229 int srp_main(int argc, char **argv)
230 {
231 ENGINE *e = NULL;
232 CA_DB *db = NULL;
233 CONF *conf = NULL;
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;
241 OPTION_CHOICE o;
242
243 prog = opt_init(argc, argv, srp_options);
244 while ((o = opt_next()) != OPT_EOF) {
245 switch (o) {
246 case OPT_EOF:
247 case OPT_ERR:
248 opthelp:
249 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
250 goto end;
251 case OPT_HELP:
252 opt_help(srp_options);
253 ret = 0;
254 goto end;
255 case OPT_VERBOSE:
256 verbose++;
257 break;
258 case OPT_CONFIG:
259 configfile = opt_arg();
260 break;
261 case OPT_NAME:
262 section = opt_arg();
263 break;
264 case OPT_SRPVFILE:
265 srpvfile = opt_arg();
266 break;
267 case OPT_ADD:
268 case OPT_DELETE:
269 case OPT_MODIFY:
270 case OPT_LIST:
271 if (mode != OPT_ERR) {
272 BIO_printf(bio_err,
273 "%s: Only one of -add/-delete/-modify/-list\n",
274 prog);
275 goto opthelp;
276 }
277 mode = o;
278 break;
279 case OPT_GN:
280 gN = opt_arg();
281 break;
282 case OPT_USERINFO:
283 userinfo = opt_arg();
284 break;
285 case OPT_PASSIN:
286 passinarg = opt_arg();
287 break;
288 case OPT_PASSOUT:
289 passoutarg = opt_arg();
290 break;
291 case OPT_ENGINE:
292 e = setup_engine(opt_arg(), 0);
293 break;
294 case OPT_R_CASES:
295 if (!opt_rand(o))
296 goto end;
297 break;
298 case OPT_PROV_CASES:
299 if (!opt_provider(o))
300 goto end;
301 break;
302 }
303 }
304
305 /* Optional parameters are usernames. */
306 argc = opt_num_rest();
307 argv = opt_rest();
308
309 if (srpvfile != NULL && configfile != NULL) {
310 BIO_printf(bio_err,
311 "-srpvfile and -configfile cannot be specified together.\n");
312 goto end;
313 }
314 if (mode == OPT_ERR) {
315 BIO_printf(bio_err,
316 "Exactly one of the options -add, -delete, -modify -list must be specified.\n");
317 goto opthelp;
318 }
319 if (mode == OPT_DELETE || mode == OPT_MODIFY || mode == OPT_ADD) {
320 if (argc == 0) {
321 BIO_printf(bio_err, "Need at least one user.\n");
322 goto opthelp;
323 }
324 user = *argv++;
325 }
326 if ((passinarg != NULL || passoutarg != NULL) && argc != 1) {
327 BIO_printf(bio_err,
328 "-passin, -passout arguments only valid with one user.\n");
329 goto opthelp;
330 }
331
332 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
333 BIO_printf(bio_err, "Error getting passwords\n");
334 goto end;
335 }
336
337 if (srpvfile == NULL) {
338 if (configfile == NULL)
339 configfile = default_config_file;
340
341 conf = app_load_config_verbose(configfile, verbose);
342 if (conf == NULL)
343 goto end;
344 if (configfile != default_config_file && !app_load_modules(conf))
345 goto end;
346
347 /* Lets get the config section we are using */
348 if (section == NULL) {
349 if (verbose)
350 BIO_printf(bio_err,
351 "trying to read " ENV_DEFAULT_SRP
352 " in " BASE_SECTION "\n");
353
354 section = lookup_conf(conf, BASE_SECTION, ENV_DEFAULT_SRP);
355 if (section == NULL)
356 goto end;
357 }
358
359 app_RAND_load_conf(conf, BASE_SECTION);
360
361 if (verbose)
362 BIO_printf(bio_err,
363 "trying to read " ENV_DATABASE " in section \"%s\"\n",
364 section);
365
366 srpvfile = lookup_conf(conf, section, ENV_DATABASE);
367 if (srpvfile == NULL)
368 goto end;
369 }
370
371 if (verbose)
372 BIO_printf(bio_err, "Trying to read SRP verifier file \"%s\"\n",
373 srpvfile);
374
375 db = load_index(srpvfile, NULL);
376 if (db == NULL)
377 goto end;
378
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);
382
383 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
384 maxgN = i;
385 if ((gNindex < 0) && (gN != NULL) && strcmp(gN, pp[DB_srpid]) == 0)
386 gNindex = i;
387
388 print_index(db, i, verbose > 1);
389 }
390 }
391
392 if (verbose)
393 BIO_printf(bio_err, "Database initialised\n");
394
395 if (gNindex >= 0) {
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);
400 goto end;
401 } else {
402 if (verbose)
403 BIO_printf(bio_err, "Database has no g N information.\n");
404 gNrow = NULL;
405 }
406
407 if (verbose > 1)
408 BIO_printf(bio_err, "Starting user processing\n");
409
410 while (mode == OPT_LIST || user != NULL) {
411 int userindex = -1;
412
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);
417
418 if (mode == OPT_LIST) {
419 if (user == NULL) {
420 BIO_printf(bio_err, "List all users\n");
421
422 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++)
423 print_user(db, i, 1);
424 } else if (userindex < 0) {
425 BIO_printf(bio_err,
426 "user \"%s\" does not exist, ignored. t\n", user);
427 errors++;
428 }
429 } else if (mode == OPT_ADD) {
430 if (userindex >= 0) {
431 /* reactivation of a new user */
432 char **row =
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';
436
437 doupdatedb = 1;
438 } else {
439 char *row[DB_NUMBER];
440 char *gNid;
441 row[DB_srpverifier] = NULL;
442 row[DB_srpsalt] = NULL;
443 row[DB_srpinfo] = NULL;
444 if (!
445 (gNid =
446 srp_create_user(user, &(row[DB_srpverifier]),
447 &(row[DB_srpsalt]),
448 gNrow ? gNrow[DB_srpsalt] : gN,
449 gNrow ? gNrow[DB_srpverifier] : NULL,
450 passout, verbose))) {
451 BIO_printf(bio_err,
452 "Cannot create srp verifier for user \"%s\", operation abandoned .\n",
453 user);
454 errors++;
455 goto end;
456 }
457 row[DB_srpid] = OPENSSL_strdup(user);
458 row[DB_srptype] = OPENSSL_strdup("v");
459 row[DB_srpgN] = OPENSSL_strdup(gNid);
460
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)
466 || (userinfo
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]);
475 goto end;
476 }
477 doupdatedb = 1;
478 }
479 } else if (mode == OPT_MODIFY) {
480 if (userindex < 0) {
481 BIO_printf(bio_err,
482 "user \"%s\" does not exist, operation ignored.\n",
483 user);
484 errors++;
485 } else {
486
487 char **row =
488 sk_OPENSSL_PSTRING_value(db->db->data, userindex);
489 char type = row[DB_srptype][0];
490 if (type == 'v') {
491 BIO_printf(bio_err,
492 "user \"%s\" already updated, operation ignored.\n",
493 user);
494 errors++;
495 } else {
496 char *gNid;
497
498 if (row[DB_srptype][0] == 'V') {
499 int user_gN;
500 char **irow = NULL;
501 if (verbose)
502 BIO_printf(bio_err,
503 "Verifying password for user \"%s\"\n",
504 user);
505 if ((user_gN =
506 get_index(db, row[DB_srpgN], DB_SRP_INDEX)) >= 0)
507 irow =
508 sk_OPENSSL_PSTRING_value(db->db->data,
509 userindex);
510
511 if (!srp_verify_user
512 (user, row[DB_srpverifier], row[DB_srpsalt],
513 irow ? irow[DB_srpsalt] : row[DB_srpgN],
514 irow ? irow[DB_srpverifier] : NULL, passin,
515 verbose)) {
516 BIO_printf(bio_err,
517 "Invalid password for user \"%s\", operation abandoned.\n",
518 user);
519 errors++;
520 goto end;
521 }
522 }
523 if (verbose)
524 BIO_printf(bio_err, "Password for user \"%s\" ok.\n",
525 user);
526
527 if (!
528 (gNid =
529 srp_create_user(user, &(row[DB_srpverifier]),
530 &(row[DB_srpsalt]),
531 gNrow ? gNrow[DB_srpsalt] : NULL,
532 gNrow ? gNrow[DB_srpverifier] : NULL,
533 passout, verbose))) {
534 BIO_printf(bio_err,
535 "Cannot create srp verifier for user \"%s\", operation abandoned.\n",
536 user);
537 errors++;
538 goto end;
539 }
540
541 row[DB_srptype][0] = 'v';
542 row[DB_srpgN] = OPENSSL_strdup(gNid);
543
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
549 || (userinfo
550 && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo))
551 == NULL)))
552 goto end;
553
554 doupdatedb = 1;
555 }
556 }
557 } else if (mode == OPT_DELETE) {
558 if (userindex < 0) {
559 BIO_printf(bio_err,
560 "user \"%s\" does not exist, operation ignored. t\n",
561 user);
562 errors++;
563 } else {
564 char **xpp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);
565
566 BIO_printf(bio_err, "user \"%s\" revoked. t\n", user);
567 xpp[DB_srptype][0] = 'R';
568 doupdatedb = 1;
569 }
570 }
571 user = *argv++;
572 if (user == NULL) {
573 /* no more processing in any mode if no users left */
574 break;
575 }
576 }
577
578 if (verbose)
579 BIO_printf(bio_err, "User procession done.\n");
580
581 if (doupdatedb) {
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);
585
586 if (pp[DB_srptype][0] == 'v') {
587 pp[DB_srptype][0] = 'V';
588 print_user(db, i, verbose);
589 }
590 }
591
592 if (verbose)
593 BIO_printf(bio_err, "Trying to update srpvfile.\n");
594 if (!save_index(srpvfile, "new", db))
595 goto end;
596
597 if (verbose)
598 BIO_printf(bio_err, "Temporary srpvfile created.\n");
599 if (!rotate_index(srpvfile, "new", "old"))
600 goto end;
601
602 if (verbose)
603 BIO_printf(bio_err, "srpvfile updated.\n");
604 }
605
606 ret = (errors != 0);
607 end:
608 if (errors != 0)
609 if (verbose)
610 BIO_printf(bio_err, "User errors %d.\n", errors);
611
612 if (verbose)
613 BIO_printf(bio_err, "SRP terminating with code %d.\n", ret);
614
615 OPENSSL_free(passin);
616 OPENSSL_free(passout);
617 if (ret)
618 ERR_print_errors(bio_err);
619 NCONF_free(conf);
620 free_index(db);
621 release_engine(e);
622 return ret;
623 }