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