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