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