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