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