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