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