]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/apps.c
add missing entries for "-multivalue-rdn" and "-utf8" in ca.pod and req.pod
[thirdparty/openssl.git] / apps / apps.c
CommitLineData
d02b48c6 1/* apps/apps.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
a661b653
BM
58/* ====================================================================
59 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
d02b48c6
RE
111
112#include <stdio.h>
113#include <stdlib.h>
114#include <string.h>
115#include <sys/types.h>
116#include <sys/stat.h>
d652a095 117#include <ctype.h>
90ae4673
RL
118#include <openssl/err.h>
119#include <openssl/x509.h>
535d79da 120#include <openssl/x509v3.h>
90ae4673
RL
121#include <openssl/pem.h>
122#include <openssl/pkcs12.h>
2fe5adc3 123#include <openssl/ui.h>
90ae4673 124#include <openssl/safestack.h>
0b13e9f0 125#ifndef OPENSSL_NO_ENGINE
1372965e 126#include <openssl/engine.h>
0b13e9f0 127#endif
823a67b0 128#include <openssl/rsa.h>
f0eae953 129#include <openssl/bn.h>
d02b48c6 130
d610d27f
RL
131#define NON_MAIN
132#include "apps.h"
133#undef NON_MAIN
134
8ca533e3 135typedef struct {
7d727231 136 const char *name;
8ca533e3
DSH
137 unsigned long flag;
138 unsigned long mask;
139} NAME_EX_TBL;
140
2fe5adc3
RL
141static UI_METHOD *ui_method = NULL;
142
8ca533e3 143static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
535d79da 144static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
8ca533e3 145
690ecff7 146#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
80bb905d
RL
147/* Looks like this stuff is worth moving into separate function */
148static EVP_PKEY *
149load_netscape_key(BIO *err, BIO *key, const char *file,
150 const char *key_descrip, int format);
151#endif
152
d02b48c6 153int app_init(long mesgwin);
d02b48c6 154#ifdef undef /* never finished - probably never will be :-) */
6b691a5c 155int args_from_file(char *file, int *argc, char **argv[])
d02b48c6
RE
156 {
157 FILE *fp;
158 int num,i;
159 unsigned int len;
160 static char *buf=NULL;
161 static char **arg=NULL;
162 char *p;
163 struct stat stbuf;
164
165 if (stat(file,&stbuf) < 0) return(0);
166
167 fp=fopen(file,"r");
168 if (fp == NULL)
169 return(0);
170
171 *argc=0;
172 *argv=NULL;
173
174 len=(unsigned int)stbuf.st_size;
26a3a48d
RL
175 if (buf != NULL) OPENSSL_free(buf);
176 buf=(char *)OPENSSL_malloc(len+1);
d02b48c6
RE
177 if (buf == NULL) return(0);
178
179 len=fread(buf,1,len,fp);
180 if (len <= 1) return(0);
181 buf[len]='\0';
182
183 i=0;
184 for (p=buf; *p; p++)
185 if (*p == '\n') i++;
26a3a48d
RL
186 if (arg != NULL) OPENSSL_free(arg);
187 arg=(char **)OPENSSL_malloc(sizeof(char *)*(i*2));
d02b48c6
RE
188
189 *argv=arg;
190 num=0;
191 p=buf;
192 for (;;)
193 {
194 if (!*p) break;
195 if (*p == '#') /* comment line */
196 {
197 while (*p && (*p != '\n')) p++;
198 continue;
199 }
200 /* else we have a line */
201 *(arg++)=p;
202 num++;
203 while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n')))
204 p++;
205 if (!*p) break;
206 if (*p == '\n')
207 {
208 *(p++)='\0';
209 continue;
210 }
211 /* else it is a tab or space */
212 p++;
213 while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
214 p++;
215 if (!*p) break;
216 if (*p == '\n')
217 {
218 p++;
219 continue;
220 }
221 *(arg++)=p++;
222 num++;
223 while (*p && (*p != '\n')) p++;
224 if (!*p) break;
225 /* else *p == '\n' */
226 *(p++)='\0';
227 }
228 *argc=num;
229 return(1);
230 }
231#endif
232
6b691a5c 233int str2fmt(char *s)
d02b48c6
RE
234 {
235 if ((*s == 'D') || (*s == 'd'))
236 return(FORMAT_ASN1);
237 else if ((*s == 'T') || (*s == 't'))
238 return(FORMAT_TEXT);
239 else if ((*s == 'P') || (*s == 'p'))
240 return(FORMAT_PEM);
241 else if ((*s == 'N') || (*s == 'n'))
242 return(FORMAT_NETSCAPE);
094fe66d
DSH
243 else if ((*s == 'S') || (*s == 's'))
244 return(FORMAT_SMIME);
90ae4673
RL
245 else if ((*s == '1')
246 || (strcmp(s,"PKCS12") == 0) || (strcmp(s,"pkcs12") == 0)
247 || (strcmp(s,"P12") == 0) || (strcmp(s,"p12") == 0))
248 return(FORMAT_PKCS12);
5270e702
RL
249 else if ((*s == 'E') || (*s == 'e'))
250 return(FORMAT_ENGINE);
d02b48c6
RE
251 else
252 return(FORMAT_UNDEF);
253 }
254
4d8743f4 255#if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) || defined(OPENSSL_SYS_NETWARE)
6b691a5c 256void program_name(char *in, char *out, int size)
d02b48c6
RE
257 {
258 int i,n;
259 char *p=NULL;
260
261 n=strlen(in);
262 /* find the last '/', '\' or ':' */
263 for (i=n-1; i>0; i--)
264 {
265 if ((in[i] == '/') || (in[i] == '\\') || (in[i] == ':'))
266 {
267 p= &(in[i+1]);
268 break;
269 }
270 }
271 if (p == NULL)
272 p=in;
273 n=strlen(p);
4d8743f4
RL
274
275#if defined(OPENSSL_SYS_NETWARE)
276 /* strip off trailing .nlm if present. */
277 if ((n > 4) && (p[n-4] == '.') &&
278 ((p[n-3] == 'n') || (p[n-3] == 'N')) &&
279 ((p[n-2] == 'l') || (p[n-2] == 'L')) &&
280 ((p[n-1] == 'm') || (p[n-1] == 'M')))
281 n-=4;
282#else
d02b48c6
RE
283 /* strip off trailing .exe if present. */
284 if ((n > 4) && (p[n-4] == '.') &&
285 ((p[n-3] == 'e') || (p[n-3] == 'E')) &&
286 ((p[n-2] == 'x') || (p[n-2] == 'X')) &&
287 ((p[n-1] == 'e') || (p[n-1] == 'E')))
288 n-=4;
4d8743f4
RL
289#endif
290
d02b48c6
RE
291 if (n > size-1)
292 n=size-1;
293
294 for (i=0; i<n; i++)
295 {
296 if ((p[i] >= 'A') && (p[i] <= 'Z'))
297 out[i]=p[i]-'A'+'a';
298 else
299 out[i]=p[i];
300 }
301 out[n]='\0';
302 }
303#else
bc36ee62 304#ifdef OPENSSL_SYS_VMS
7d7d2cbc
UM
305void program_name(char *in, char *out, int size)
306 {
307 char *p=in, *q;
308 char *chars=":]>";
309
310 while(*chars != '\0')
311 {
312 q=strrchr(p,*chars);
313 if (q > p)
314 p = q + 1;
315 chars++;
316 }
317
318 q=strrchr(p,'.');
319 if (q == NULL)
6298bf90
RL
320 q = p + strlen(p);
321 strncpy(out,p,size-1);
322 if (q-p >= size)
323 {
324 out[size-1]='\0';
325 }
326 else
327 {
328 out[q-p]='\0';
329 }
7d7d2cbc
UM
330 }
331#else
6b691a5c 332void program_name(char *in, char *out, int size)
d02b48c6
RE
333 {
334 char *p;
335
336 p=strrchr(in,'/');
337 if (p != NULL)
338 p++;
339 else
340 p=in;
54a656ef 341 BUF_strlcpy(out,p,size);
d02b48c6
RE
342 }
343#endif
7d7d2cbc 344#endif
d02b48c6 345
6b691a5c 346int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[])
d02b48c6
RE
347 {
348 int num,len,i;
349 char *p;
350
351 *argc=0;
352 *argv=NULL;
353
354 len=strlen(buf);
355 i=0;
356 if (arg->count == 0)
357 {
358 arg->count=20;
26a3a48d 359 arg->data=(char **)OPENSSL_malloc(sizeof(char *)*arg->count);
d02b48c6
RE
360 }
361 for (i=0; i<arg->count; i++)
362 arg->data[i]=NULL;
363
364 num=0;
365 p=buf;
366 for (;;)
367 {
368 /* first scan over white space */
369 if (!*p) break;
370 while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
371 p++;
372 if (!*p) break;
373
374 /* The start of something good :-) */
375 if (num >= arg->count)
376 {
9e1a1123
NL
377 char **tmp_p;
378 int tlen = arg->count + 20;
379 tmp_p = (char **)OPENSSL_realloc(arg->data,
380 sizeof(char *)*tlen);
381 if (tmp_p == NULL)
382 return 0;
383 arg->data = tmp_p;
384 arg->count = tlen;
385 /* initialize newly allocated data */
386 for (i = num; i < arg->count; i++)
387 arg->data[i] = NULL;
d02b48c6
RE
388 }
389 arg->data[num++]=p;
390
391 /* now look for the end of this */
392 if ((*p == '\'') || (*p == '\"')) /* scan for closing quote */
393 {
394 i= *(p++);
395 arg->data[num-1]++; /* jump over quote */
396 while (*p && (*p != i))
397 p++;
398 *p='\0';
399 }
400 else
401 {
402 while (*p && ((*p != ' ') &&
403 (*p != '\t') && (*p != '\n')))
404 p++;
405
406 if (*p == '\0')
407 p--;
408 else
409 *p='\0';
410 }
411 p++;
412 }
413 *argc=num;
414 *argv=arg->data;
415 return(1);
416 }
417
418#ifndef APP_INIT
6b691a5c 419int app_init(long mesgwin)
d02b48c6
RE
420 {
421 return(1);
422 }
423#endif
53b1899e 424
a3fe382e 425
954ef7ef
DSH
426int dump_cert_text (BIO *out, X509 *x)
427{
54a656ef
BL
428 char *p;
429
430 p=X509_NAME_oneline(X509_get_subject_name(x),NULL,0);
954ef7ef 431 BIO_puts(out,"subject=");
54a656ef
BL
432 BIO_puts(out,p);
433 OPENSSL_free(p);
954ef7ef 434
54a656ef
BL
435 p=X509_NAME_oneline(X509_get_issuer_name(x),NULL,0);
436 BIO_puts(out,"\nissuer=");
437 BIO_puts(out,p);
954ef7ef 438 BIO_puts(out,"\n");
54a656ef
BL
439 OPENSSL_free(p);
440
441 return 0;
954ef7ef 442}
a3fe382e 443
2fe5adc3
RL
444static int ui_open(UI *ui)
445 {
446 return UI_method_get_opener(UI_OpenSSL())(ui);
447 }
448static int ui_read(UI *ui, UI_STRING *uis)
449 {
450 if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
451 && UI_get0_user_data(ui))
452 {
453 switch(UI_get_string_type(uis))
454 {
455 case UIT_PROMPT:
456 case UIT_VERIFY:
457 {
458 const char *password =
459 ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
f35232e6 460 if (password && password[0] != '\0')
2fe5adc3 461 {
4f272c17 462 UI_set_result(ui, uis, password);
2fe5adc3
RL
463 return 1;
464 }
465 }
466 default:
467 break;
468 }
469 }
470 return UI_method_get_reader(UI_OpenSSL())(ui, uis);
471 }
472static int ui_write(UI *ui, UI_STRING *uis)
473 {
474 if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
475 && UI_get0_user_data(ui))
476 {
477 switch(UI_get_string_type(uis))
478 {
479 case UIT_PROMPT:
480 case UIT_VERIFY:
481 {
482 const char *password =
483 ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
f35232e6 484 if (password && password[0] != '\0')
2fe5adc3
RL
485 return 1;
486 }
487 default:
488 break;
489 }
490 }
491 return UI_method_get_writer(UI_OpenSSL())(ui, uis);
492 }
493static int ui_close(UI *ui)
494 {
495 return UI_method_get_closer(UI_OpenSSL())(ui);
496 }
40889b9c 497int setup_ui_method(void)
2fe5adc3
RL
498 {
499 ui_method = UI_create_method("OpenSSL application user interface");
500 UI_method_set_opener(ui_method, ui_open);
501 UI_method_set_reader(ui_method, ui_read);
502 UI_method_set_writer(ui_method, ui_write);
503 UI_method_set_closer(ui_method, ui_close);
504 return 0;
505 }
40889b9c 506void destroy_ui_method(void)
2c7bc88d
DSH
507 {
508 if(ui_method)
509 {
510 UI_destroy_method(ui_method);
511 ui_method = NULL;
512 }
513 }
30b4c272 514int password_callback(char *buf, int bufsiz, int verify,
2fe5adc3 515 PW_CB_DATA *cb_tmp)
30b4c272 516 {
2fe5adc3
RL
517 UI *ui = NULL;
518 int res = 0;
30b4c272
RL
519 const char *prompt_info = NULL;
520 const char *password = NULL;
2fe5adc3 521 PW_CB_DATA *cb_data = (PW_CB_DATA *)cb_tmp;
30b4c272
RL
522
523 if (cb_data)
524 {
525 if (cb_data->password)
526 password = cb_data->password;
527 if (cb_data->prompt_info)
528 prompt_info = cb_data->prompt_info;
529 }
530
b65f8513
DSH
531 if (password)
532 {
533 res = strlen(password);
534 if (res > bufsiz)
535 res = bufsiz;
536 memcpy(buf, password, res);
537 return res;
538 }
539
2fe5adc3
RL
540 ui = UI_new_method(ui_method);
541 if (ui)
542 {
2fe5adc3
RL
543 int ok = 0;
544 char *buff = NULL;
545 int ui_flags = 0;
546 char *prompt = NULL;
30b4c272 547
2fe5adc3 548 prompt = UI_construct_prompt(ui, "pass phrase",
151368cc 549 prompt_info);
30b4c272 550
2fe5adc3 551 ui_flags |= UI_INPUT_FLAG_DEFAULT_PWD;
4f272c17 552 UI_ctrl(ui, UI_CTRL_PRINT_ERRORS, 1, 0, 0);
2fe5adc3
RL
553
554 if (ok >= 0)
4f272c17
RL
555 ok = UI_add_input_string(ui,prompt,ui_flags,buf,
556 PW_MIN_LENGTH,BUFSIZ-1);
2fe5adc3
RL
557 if (ok >= 0 && verify)
558 {
559 buff = (char *)OPENSSL_malloc(bufsiz);
4f272c17
RL
560 ok = UI_add_verify_string(ui,prompt,ui_flags,buff,
561 PW_MIN_LENGTH,BUFSIZ-1, buf);
2fe5adc3
RL
562 }
563 if (ok >= 0)
4f272c17 564 do
2fe5adc3 565 {
4f272c17 566 ok = UI_process(ui);
2fe5adc3 567 }
4f272c17
RL
568 while (ok < 0 && UI_ctrl(ui, UI_CTRL_IS_REDOABLE, 0, 0, 0));
569
2fe5adc3 570 if (buff)
30b4c272 571 {
4579924b 572 OPENSSL_cleanse(buff,(unsigned int)bufsiz);
2fe5adc3 573 OPENSSL_free(buff);
30b4c272 574 }
2fe5adc3 575
4f272c17
RL
576 if (ok >= 0)
577 res = strlen(buf);
2fe5adc3 578 if (ok == -1)
30b4c272 579 {
2fe5adc3
RL
580 BIO_printf(bio_err, "User interface error\n");
581 ERR_print_errors(bio_err);
4579924b 582 OPENSSL_cleanse(buf,(unsigned int)bufsiz);
2fe5adc3 583 res = 0;
30b4c272 584 }
2fe5adc3
RL
585 if (ok == -2)
586 {
587 BIO_printf(bio_err,"aborted!\n");
4579924b 588 OPENSSL_cleanse(buf,(unsigned int)bufsiz);
2fe5adc3
RL
589 res = 0;
590 }
591 UI_free(ui);
4f272c17 592 OPENSSL_free(prompt);
30b4c272 593 }
2fe5adc3 594 return res;
30b4c272
RL
595 }
596
a3fe382e
DSH
597static char *app_get_pass(BIO *err, char *arg, int keepbio);
598
599int app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2)
600{
601 int same;
602 if(!arg2 || !arg1 || strcmp(arg1, arg2)) same = 0;
603 else same = 1;
604 if(arg1) {
605 *pass1 = app_get_pass(err, arg1, same);
606 if(!*pass1) return 0;
607 } else if(pass1) *pass1 = NULL;
608 if(arg2) {
609 *pass2 = app_get_pass(err, arg2, same ? 2 : 0);
610 if(!*pass2) return 0;
611 } else if(pass2) *pass2 = NULL;
612 return 1;
613}
614
615static char *app_get_pass(BIO *err, char *arg, int keepbio)
616{
617 char *tmp, tpass[APP_PASS_LEN];
618 static BIO *pwdbio = NULL;
619 int i;
620 if(!strncmp(arg, "pass:", 5)) return BUF_strdup(arg + 5);
621 if(!strncmp(arg, "env:", 4)) {
622 tmp = getenv(arg + 4);
623 if(!tmp) {
624 BIO_printf(err, "Can't read environment variable %s\n", arg + 4);
625 return NULL;
626 }
627 return BUF_strdup(tmp);
628 }
629 if(!keepbio || !pwdbio) {
630 if(!strncmp(arg, "file:", 5)) {
631 pwdbio = BIO_new_file(arg + 5, "r");
632 if(!pwdbio) {
633 BIO_printf(err, "Can't open file %s\n", arg + 5);
634 return NULL;
635 }
636 } else if(!strncmp(arg, "fd:", 3)) {
637 BIO *btmp;
638 i = atoi(arg + 3);
639 if(i >= 0) pwdbio = BIO_new_fd(i, BIO_NOCLOSE);
640 if((i < 0) || !pwdbio) {
641 BIO_printf(err, "Can't access file descriptor %s\n", arg + 3);
642 return NULL;
643 }
644 /* Can't do BIO_gets on an fd BIO so add a buffering BIO */
645 btmp = BIO_new(BIO_f_buffer());
646 pwdbio = BIO_push(btmp, pwdbio);
647 } else if(!strcmp(arg, "stdin")) {
648 pwdbio = BIO_new_fp(stdin, BIO_NOCLOSE);
649 if(!pwdbio) {
650 BIO_printf(err, "Can't open BIO for stdin\n");
651 return NULL;
652 }
653 } else {
654 BIO_printf(err, "Invalid password argument \"%s\"\n", arg);
655 return NULL;
656 }
657 }
658 i = BIO_gets(pwdbio, tpass, APP_PASS_LEN);
659 if(keepbio != 1) {
660 BIO_free_all(pwdbio);
661 pwdbio = NULL;
662 }
663 if(i <= 0) {
664 BIO_printf(err, "Error reading password from BIO\n");
665 return NULL;
666 }
667 tmp = strchr(tpass, '\n');
668 if(tmp) *tmp = 0;
669 return BUF_strdup(tpass);
670}
90ae4673 671
b7a26e6d 672int add_oid_section(BIO *err, CONF *conf)
431b0cce
RL
673{
674 char *p;
675 STACK_OF(CONF_VALUE) *sktmp;
676 CONF_VALUE *cnf;
677 int i;
b7a26e6d 678 if(!(p=NCONF_get_string(conf,NULL,"oid_section")))
2c0d1012
BM
679 {
680 ERR_clear_error();
681 return 1;
682 }
b7a26e6d 683 if(!(sktmp = NCONF_get_section(conf, p))) {
431b0cce
RL
684 BIO_printf(err, "problem loading oid section %s\n", p);
685 return 0;
686 }
687 for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
688 cnf = sk_CONF_VALUE_value(sktmp, i);
689 if(OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
690 BIO_printf(err, "problem creating object %s=%s\n",
691 cnf->name, cnf->value);
692 return 0;
693 }
694 }
695 return 1;
696}
697
e90fadda
DSH
698static int load_pkcs12(BIO *err, BIO *in, const char *desc,
699 pem_password_cb *pem_cb, void *cb_data,
700 EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca)
701 {
702 const char *pass;
703 char tpass[PEM_BUFSIZE];
704 int len, ret = 0;
705 PKCS12 *p12;
706 p12 = d2i_PKCS12_bio(in, NULL);
707 if (p12 == NULL)
708 {
709 BIO_printf(err, "Error loading PKCS12 file for %s\n", desc);
3ffb8d42 710 goto die;
e90fadda
DSH
711 }
712 /* See if an empty password will do */
713 if (PKCS12_verify_mac(p12, "", 0) || PKCS12_verify_mac(p12, NULL, 0))
714 pass = "";
715 else
716 {
717 if (!pem_cb)
718 pem_cb = (pem_password_cb *)password_callback;
719 len = pem_cb(tpass, PEM_BUFSIZE, 0, cb_data);
720 if (len < 0)
721 {
722 BIO_printf(err, "Passpharse callback error for %s\n",
723 desc);
3ffb8d42 724 goto die;
e90fadda
DSH
725 }
726 if (len < PEM_BUFSIZE)
727 tpass[len] = 0;
728 if (!PKCS12_verify_mac(p12, tpass, len))
729 {
730 BIO_printf(err,
731 "Mac verify error (wrong password?) in PKCS12 file for %s\n", desc);
3ffb8d42 732 goto die;
e90fadda
DSH
733 }
734 pass = tpass;
735 }
736 ret = PKCS12_parse(p12, pass, pkey, cert, ca);
3ffb8d42 737 die:
e90fadda
DSH
738 if (p12)
739 PKCS12_free(p12);
740 return ret;
741 }
742
30b4c272
RL
743X509 *load_cert(BIO *err, const char *file, int format,
744 const char *pass, ENGINE *e, const char *cert_descrip)
90ae4673
RL
745 {
746 ASN1_HEADER *ah=NULL;
747 BUF_MEM *buf=NULL;
748 X509 *x=NULL;
749 BIO *cert;
750
751 if ((cert=BIO_new(BIO_s_file())) == NULL)
752 {
431b0cce 753 ERR_print_errors(err);
90ae4673
RL
754 goto end;
755 }
756
757 if (file == NULL)
620cea37
BM
758 {
759 setvbuf(stdin, NULL, _IONBF, 0);
90ae4673 760 BIO_set_fp(cert,stdin,BIO_NOCLOSE);
620cea37 761 }
90ae4673
RL
762 else
763 {
764 if (BIO_read_filename(cert,file) <= 0)
765 {
30b4c272
RL
766 BIO_printf(err, "Error opening %s %s\n",
767 cert_descrip, file);
768 ERR_print_errors(err);
90ae4673
RL
769 goto end;
770 }
771 }
772
773 if (format == FORMAT_ASN1)
774 x=d2i_X509_bio(cert,NULL);
775 else if (format == FORMAT_NETSCAPE)
776 {
875a644a 777 const unsigned char *p,*op;
90ae4673
RL
778 int size=0,i;
779
780 /* We sort of have to do it this way because it is sort of nice
781 * to read the header first and check it, then
782 * try to read the certificate */
783 buf=BUF_MEM_new();
784 for (;;)
785 {
786 if ((buf == NULL) || (!BUF_MEM_grow(buf,size+1024*10)))
787 goto end;
788 i=BIO_read(cert,&(buf->data[size]),1024*10);
789 size+=i;
790 if (i == 0) break;
791 if (i < 0)
792 {
793 perror("reading certificate");
794 goto end;
795 }
796 }
797 p=(unsigned char *)buf->data;
798 op=p;
799
800 /* First load the header */
801 if ((ah=d2i_ASN1_HEADER(NULL,&p,(long)size)) == NULL)
802 goto end;
803 if ((ah->header == NULL) || (ah->header->data == NULL) ||
804 (strncmp(NETSCAPE_CERT_HDR,(char *)ah->header->data,
805 ah->header->length) != 0))
806 {
431b0cce 807 BIO_printf(err,"Error reading header on certificate\n");
90ae4673
RL
808 goto end;
809 }
810 /* header is ok, so now read the object */
811 p=op;
812 ah->meth=X509_asn1_meth();
813 if ((ah=d2i_ASN1_HEADER(&ah,&p,(long)size)) == NULL)
814 goto end;
815 x=(X509 *)ah->data;
816 ah->data=NULL;
817 }
818 else if (format == FORMAT_PEM)
30b4c272
RL
819 x=PEM_read_bio_X509_AUX(cert,NULL,
820 (pem_password_cb *)password_callback, NULL);
90ae4673
RL
821 else if (format == FORMAT_PKCS12)
822 {
e90fadda
DSH
823 if (!load_pkcs12(err, cert,cert_descrip, NULL, NULL,
824 NULL, &x, NULL))
825 goto end;
90ae4673
RL
826 }
827 else {
30b4c272
RL
828 BIO_printf(err,"bad input format specified for %s\n",
829 cert_descrip);
90ae4673
RL
830 goto end;
831 }
832end:
833 if (x == NULL)
834 {
431b0cce
RL
835 BIO_printf(err,"unable to load certificate\n");
836 ERR_print_errors(err);
90ae4673
RL
837 }
838 if (ah != NULL) ASN1_HEADER_free(ah);
839 if (cert != NULL) BIO_free(cert);
840 if (buf != NULL) BUF_MEM_free(buf);
841 return(x);
842 }
843
da9b9724 844EVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin,
30b4c272 845 const char *pass, ENGINE *e, const char *key_descrip)
90ae4673
RL
846 {
847 BIO *key=NULL;
848 EVP_PKEY *pkey=NULL;
30b4c272
RL
849 PW_CB_DATA cb_data;
850
851 cb_data.password = pass;
852 cb_data.prompt_info = file;
90ae4673 853
da9b9724 854 if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
90ae4673 855 {
431b0cce 856 BIO_printf(err,"no keyfile specified\n");
90ae4673
RL
857 goto end;
858 }
0b13e9f0 859#ifndef OPENSSL_NO_ENGINE
32d862ed
RL
860 if (format == FORMAT_ENGINE)
861 {
862 if (!e)
863 BIO_printf(bio_err,"no engine specified\n");
864 else
30b4c272 865 pkey = ENGINE_load_private_key(e, file,
2fe5adc3 866 ui_method, &cb_data);
32d862ed
RL
867 goto end;
868 }
0b13e9f0 869#endif
90ae4673
RL
870 key=BIO_new(BIO_s_file());
871 if (key == NULL)
872 {
431b0cce 873 ERR_print_errors(err);
90ae4673
RL
874 goto end;
875 }
da9b9724 876 if (file == NULL && maybe_stdin)
90ae4673 877 {
da9b9724
RL
878 setvbuf(stdin, NULL, _IONBF, 0);
879 BIO_set_fp(key,stdin,BIO_NOCLOSE);
90ae4673 880 }
da9b9724
RL
881 else
882 if (BIO_read_filename(key,file) <= 0)
883 {
884 BIO_printf(err, "Error opening %s %s\n",
885 key_descrip, file);
886 ERR_print_errors(err);
887 goto end;
888 }
90ae4673
RL
889 if (format == FORMAT_ASN1)
890 {
891 pkey=d2i_PrivateKey_bio(key, NULL);
892 }
893 else if (format == FORMAT_PEM)
894 {
30b4c272
RL
895 pkey=PEM_read_bio_PrivateKey(key,NULL,
896 (pem_password_cb *)password_callback, &cb_data);
90ae4673 897 }
690ecff7 898#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
80bb905d
RL
899 else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
900 pkey = load_netscape_key(err, key, file, key_descrip, format);
901#endif
90ae4673
RL
902 else if (format == FORMAT_PKCS12)
903 {
e90fadda
DSH
904 if (!load_pkcs12(err, key, key_descrip,
905 (pem_password_cb *)password_callback, &cb_data,
906 &pkey, NULL, NULL))
907 goto end;
90ae4673
RL
908 }
909 else
910 {
30b4c272 911 BIO_printf(err,"bad input format specified for key file\n");
90ae4673
RL
912 goto end;
913 }
914 end:
915 if (key != NULL) BIO_free(key);
916 if (pkey == NULL)
30b4c272 917 BIO_printf(err,"unable to load %s\n", key_descrip);
90ae4673
RL
918 return(pkey);
919 }
920
da9b9724 921EVP_PKEY *load_pubkey(BIO *err, const char *file, int format, int maybe_stdin,
30b4c272 922 const char *pass, ENGINE *e, const char *key_descrip)
bd08a2bd
DSH
923 {
924 BIO *key=NULL;
925 EVP_PKEY *pkey=NULL;
30b4c272
RL
926 PW_CB_DATA cb_data;
927
928 cb_data.password = pass;
929 cb_data.prompt_info = file;
bd08a2bd 930
da9b9724 931 if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
bd08a2bd
DSH
932 {
933 BIO_printf(err,"no keyfile specified\n");
934 goto end;
935 }
0b13e9f0 936#ifndef OPENSSL_NO_ENGINE
32d862ed
RL
937 if (format == FORMAT_ENGINE)
938 {
939 if (!e)
940 BIO_printf(bio_err,"no engine specified\n");
941 else
30b4c272 942 pkey = ENGINE_load_public_key(e, file,
2fe5adc3 943 ui_method, &cb_data);
32d862ed
RL
944 goto end;
945 }
0b13e9f0 946#endif
bd08a2bd
DSH
947 key=BIO_new(BIO_s_file());
948 if (key == NULL)
949 {
950 ERR_print_errors(err);
951 goto end;
952 }
da9b9724 953 if (file == NULL && maybe_stdin)
bd08a2bd 954 {
da9b9724
RL
955 setvbuf(stdin, NULL, _IONBF, 0);
956 BIO_set_fp(key,stdin,BIO_NOCLOSE);
957 }
958 else
959 if (BIO_read_filename(key,file) <= 0)
960 {
961 BIO_printf(err, "Error opening %s %s\n",
962 key_descrip, file);
963 ERR_print_errors(err);
964 goto end;
bd08a2bd
DSH
965 }
966 if (format == FORMAT_ASN1)
967 {
968 pkey=d2i_PUBKEY_bio(key, NULL);
969 }
970 else if (format == FORMAT_PEM)
971 {
30b4c272
RL
972 pkey=PEM_read_bio_PUBKEY(key,NULL,
973 (pem_password_cb *)password_callback, &cb_data);
bd08a2bd 974 }
690ecff7 975#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
80bb905d
RL
976 else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
977 pkey = load_netscape_key(err, key, file, key_descrip, format);
978#endif
bd08a2bd
DSH
979 else
980 {
30b4c272 981 BIO_printf(err,"bad input format specified for key file\n");
bd08a2bd
DSH
982 goto end;
983 }
984 end:
985 if (key != NULL) BIO_free(key);
986 if (pkey == NULL)
30b4c272 987 BIO_printf(err,"unable to load %s\n", key_descrip);
bd08a2bd
DSH
988 return(pkey);
989 }
990
690ecff7 991#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
1c02ca53 992static EVP_PKEY *
80bb905d
RL
993load_netscape_key(BIO *err, BIO *key, const char *file,
994 const char *key_descrip, int format)
995 {
996 EVP_PKEY *pkey;
997 BUF_MEM *buf;
998 RSA *rsa;
999 const unsigned char *p;
1000 int size, i;
1001
1002 buf=BUF_MEM_new();
1003 pkey = EVP_PKEY_new();
1004 size = 0;
1005 if (buf == NULL || pkey == NULL)
1006 goto error;
1007 for (;;)
1008 {
54a656ef 1009 if (!BUF_MEM_grow_clean(buf,size+1024*10))
80bb905d
RL
1010 goto error;
1011 i = BIO_read(key, &(buf->data[size]), 1024*10);
1012 size += i;
1013 if (i == 0)
1014 break;
1015 if (i < 0)
1016 {
1017 BIO_printf(err, "Error reading %s %s",
1018 key_descrip, file);
1019 goto error;
1020 }
1021 }
1022 p=(unsigned char *)buf->data;
1023 rsa = d2i_RSA_NET(NULL,&p,(long)size,NULL,
1024 (format == FORMAT_IISSGC ? 1 : 0));
1025 if (rsa == NULL)
1026 goto error;
1027 BUF_MEM_free(buf);
1028 EVP_PKEY_set1_RSA(pkey, rsa);
1029 return pkey;
1030error:
1031 BUF_MEM_free(buf);
1032 EVP_PKEY_free(pkey);
1033 return NULL;
1034 }
1035#endif /* ndef OPENSSL_NO_RC4 */
1036
30b4c272
RL
1037STACK_OF(X509) *load_certs(BIO *err, const char *file, int format,
1038 const char *pass, ENGINE *e, const char *cert_descrip)
90ae4673
RL
1039 {
1040 BIO *certs;
1041 int i;
1042 STACK_OF(X509) *othercerts = NULL;
1043 STACK_OF(X509_INFO) *allcerts = NULL;
1044 X509_INFO *xi;
30b4c272
RL
1045 PW_CB_DATA cb_data;
1046
1047 cb_data.password = pass;
1048 cb_data.prompt_info = file;
90ae4673
RL
1049
1050 if((certs = BIO_new(BIO_s_file())) == NULL)
1051 {
431b0cce 1052 ERR_print_errors(err);
90ae4673
RL
1053 goto end;
1054 }
1055
1056 if (file == NULL)
1057 BIO_set_fp(certs,stdin,BIO_NOCLOSE);
1058 else
1059 {
1060 if (BIO_read_filename(certs,file) <= 0)
1061 {
30b4c272
RL
1062 BIO_printf(err, "Error opening %s %s\n",
1063 cert_descrip, file);
1064 ERR_print_errors(err);
90ae4673
RL
1065 goto end;
1066 }
1067 }
1068
1069 if (format == FORMAT_PEM)
1070 {
62324627 1071 othercerts = sk_X509_new_null();
90ae4673
RL
1072 if(!othercerts)
1073 {
1074 sk_X509_free(othercerts);
1075 othercerts = NULL;
1076 goto end;
1077 }
30b4c272
RL
1078 allcerts = PEM_X509_INFO_read_bio(certs, NULL,
1079 (pem_password_cb *)password_callback, &cb_data);
90ae4673
RL
1080 for(i = 0; i < sk_X509_INFO_num(allcerts); i++)
1081 {
1082 xi = sk_X509_INFO_value (allcerts, i);
1083 if (xi->x509)
1084 {
1085 sk_X509_push(othercerts, xi->x509);
1086 xi->x509 = NULL;
1087 }
1088 }
1089 goto end;
1090 }
1091 else {
30b4c272
RL
1092 BIO_printf(err,"bad input format specified for %s\n",
1093 cert_descrip);
90ae4673
RL
1094 goto end;
1095 }
1096end:
1097 if (othercerts == NULL)
1098 {
431b0cce
RL
1099 BIO_printf(err,"unable to load certificates\n");
1100 ERR_print_errors(err);
90ae4673
RL
1101 }
1102 if (allcerts) sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
1103 if (certs != NULL) BIO_free(certs);
1104 return(othercerts);
1105 }
1106
8ca533e3
DSH
1107
1108#define X509V3_EXT_UNKNOWN_MASK (0xfL << 16)
1109/* Return error for unknown extensions */
1110#define X509V3_EXT_DEFAULT 0
1111/* Print error for unknown extensions */
1112#define X509V3_EXT_ERROR_UNKNOWN (1L << 16)
1113/* ASN1 parse unknown extensions */
1114#define X509V3_EXT_PARSE_UNKNOWN (2L << 16)
1115/* BIO_dump unknown extensions */
1116#define X509V3_EXT_DUMP_UNKNOWN (3L << 16)
1117
535d79da
DSH
1118#define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \
1119 X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION)
1120
8ca533e3
DSH
1121int set_cert_ex(unsigned long *flags, const char *arg)
1122{
1123 static const NAME_EX_TBL cert_tbl[] = {
1124 { "compatible", X509_FLAG_COMPAT, 0xffffffffl},
535d79da 1125 { "ca_default", X509_FLAG_CA, 0xffffffffl},
8ca533e3
DSH
1126 { "no_header", X509_FLAG_NO_HEADER, 0},
1127 { "no_version", X509_FLAG_NO_VERSION, 0},
1128 { "no_serial", X509_FLAG_NO_SERIAL, 0},
1129 { "no_signame", X509_FLAG_NO_SIGNAME, 0},
1130 { "no_validity", X509_FLAG_NO_VALIDITY, 0},
1131 { "no_subject", X509_FLAG_NO_SUBJECT, 0},
0a3ea5d3 1132 { "no_issuer", X509_FLAG_NO_ISSUER, 0},
8ca533e3
DSH
1133 { "no_pubkey", X509_FLAG_NO_PUBKEY, 0},
1134 { "no_extensions", X509_FLAG_NO_EXTENSIONS, 0},
1135 { "no_sigdump", X509_FLAG_NO_SIGDUMP, 0},
1136 { "no_aux", X509_FLAG_NO_AUX, 0},
fc85ac20 1137 { "no_attributes", X509_FLAG_NO_ATTRIBUTES, 0},
8ca533e3
DSH
1138 { "ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK},
1139 { "ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
1140 { "ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
1141 { "ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
1142 { NULL, 0, 0}
1143 };
535d79da 1144 return set_multi_opts(flags, arg, cert_tbl);
8ca533e3 1145}
a657546f
DSH
1146
1147int set_name_ex(unsigned long *flags, const char *arg)
1148{
8ca533e3 1149 static const NAME_EX_TBL ex_tbl[] = {
a657546f
DSH
1150 { "esc_2253", ASN1_STRFLGS_ESC_2253, 0},
1151 { "esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0},
1152 { "esc_msb", ASN1_STRFLGS_ESC_MSB, 0},
1153 { "use_quote", ASN1_STRFLGS_ESC_QUOTE, 0},
1154 { "utf8", ASN1_STRFLGS_UTF8_CONVERT, 0},
bd4e1527
DSH
1155 { "ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0},
1156 { "show_type", ASN1_STRFLGS_SHOW_TYPE, 0},
a657546f
DSH
1157 { "dump_all", ASN1_STRFLGS_DUMP_ALL, 0},
1158 { "dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0},
1159 { "dump_der", ASN1_STRFLGS_DUMP_DER, 0},
1160 { "compat", XN_FLAG_COMPAT, 0xffffffffL},
1161 { "sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK},
1162 { "sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK},
1163 { "sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK},
1164 { "sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK},
1165 { "dn_rev", XN_FLAG_DN_REV, 0},
1166 { "nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK},
1167 { "sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK},
1168 { "lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK},
e890dcdb 1169 { "align", XN_FLAG_FN_ALIGN, 0},
a657546f
DSH
1170 { "oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK},
1171 { "space_eq", XN_FLAG_SPC_EQ, 0},
1172 { "dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0},
1173 { "RFC2253", XN_FLAG_RFC2253, 0xffffffffL},
1174 { "oneline", XN_FLAG_ONELINE, 0xffffffffL},
1175 { "multiline", XN_FLAG_MULTILINE, 0xffffffffL},
535d79da 1176 { "ca_default", XN_FLAG_MULTILINE, 0xffffffffL},
a657546f
DSH
1177 { NULL, 0, 0}
1178 };
535d79da
DSH
1179 return set_multi_opts(flags, arg, ex_tbl);
1180}
1181
791bd0cd
DSH
1182int set_ext_copy(int *copy_type, const char *arg)
1183{
1184 if (!strcasecmp(arg, "none"))
1185 *copy_type = EXT_COPY_NONE;
1186 else if (!strcasecmp(arg, "copy"))
1187 *copy_type = EXT_COPY_ADD;
1188 else if (!strcasecmp(arg, "copyall"))
1189 *copy_type = EXT_COPY_ALL;
1190 else
1191 return 0;
1192 return 1;
1193}
1194
1195int copy_extensions(X509 *x, X509_REQ *req, int copy_type)
1196{
1197 STACK_OF(X509_EXTENSION) *exts = NULL;
1198 X509_EXTENSION *ext, *tmpext;
1199 ASN1_OBJECT *obj;
1200 int i, idx, ret = 0;
1201 if (!x || !req || (copy_type == EXT_COPY_NONE))
1202 return 1;
1203 exts = X509_REQ_get_extensions(req);
1204
1205 for(i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
1206 ext = sk_X509_EXTENSION_value(exts, i);
1207 obj = X509_EXTENSION_get_object(ext);
1208 idx = X509_get_ext_by_OBJ(x, obj, -1);
1209 /* Does extension exist? */
1210 if (idx != -1) {
1211 /* If normal copy don't override existing extension */
1212 if (copy_type == EXT_COPY_ADD)
1213 continue;
1214 /* Delete all extensions of same type */
1215 do {
1216 tmpext = X509_get_ext(x, idx);
1217 X509_delete_ext(x, idx);
1218 X509_EXTENSION_free(tmpext);
1219 idx = X509_get_ext_by_OBJ(x, obj, -1);
1220 } while (idx != -1);
1221 }
1222 if (!X509_add_ext(x, ext, -1))
1223 goto end;
1224 }
1225
1226 ret = 1;
1227
1228 end:
1229
1230 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
1231
1232 return ret;
1233}
1234
1235
1236
1237
535d79da
DSH
1238static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl)
1239{
1240 STACK_OF(CONF_VALUE) *vals;
1241 CONF_VALUE *val;
1242 int i, ret = 1;
1243 if(!arg) return 0;
1244 vals = X509V3_parse_list(arg);
1245 for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
1246 val = sk_CONF_VALUE_value(vals, i);
1247 if (!set_table_opts(flags, val->name, in_tbl))
1248 ret = 0;
1249 }
1250 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
1251 return ret;
8ca533e3 1252}
a657546f 1253
8ca533e3
DSH
1254static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl)
1255{
1256 char c;
1257 const NAME_EX_TBL *ptbl;
a657546f
DSH
1258 c = arg[0];
1259
1260 if(c == '-') {
1261 c = 0;
1262 arg++;
1263 } else if (c == '+') {
1264 c = 1;
1265 arg++;
1266 } else c = 1;
1267
8ca533e3 1268 for(ptbl = in_tbl; ptbl->name; ptbl++) {
c15e0363 1269 if(!strcasecmp(arg, ptbl->name)) {
a657546f
DSH
1270 *flags &= ~ptbl->mask;
1271 if(c) *flags |= ptbl->flag;
1272 else *flags &= ~ptbl->flag;
1273 return 1;
1274 }
1275 }
1276 return 0;
1277}
1278
7d727231 1279void print_name(BIO *out, const char *title, X509_NAME *nm, unsigned long lflags)
a657546f 1280{
402bcde8 1281 char *buf;
a657546f
DSH
1282 char mline = 0;
1283 int indent = 0;
54a656ef 1284
a657546f
DSH
1285 if(title) BIO_puts(out, title);
1286 if((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
1287 mline = 1;
1288 indent = 4;
1289 }
1290 if(lflags == XN_FLAG_COMPAT) {
402bcde8
RL
1291 buf = X509_NAME_oneline(nm, 0, 0);
1292 BIO_puts(out, buf);
a657546f 1293 BIO_puts(out, "\n");
402bcde8 1294 OPENSSL_free(buf);
a657546f
DSH
1295 } else {
1296 if(mline) BIO_puts(out, "\n");
1297 X509_NAME_print_ex(out, nm, indent, lflags);
1298 BIO_puts(out, "\n");
1299 }
1300}
1301
81f169e9
DSH
1302X509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath)
1303{
1304 X509_STORE *store;
1305 X509_LOOKUP *lookup;
1306 if(!(store = X509_STORE_new())) goto end;
1307 lookup=X509_STORE_add_lookup(store,X509_LOOKUP_file());
1308 if (lookup == NULL) goto end;
1309 if (CAfile) {
1310 if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) {
1311 BIO_printf(bp, "Error loading file %s\n", CAfile);
1312 goto end;
1313 }
1314 } else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
1315
1316 lookup=X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
1317 if (lookup == NULL) goto end;
1318 if (CApath) {
1319 if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) {
1320 BIO_printf(bp, "Error loading directory %s\n", CApath);
1321 goto end;
1322 }
1323 } else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
1324
1325 ERR_clear_error();
1326 return store;
1327 end:
1328 X509_STORE_free(store);
1329 return NULL;
1330}
531d630b 1331
0b13e9f0 1332#ifndef OPENSSL_NO_ENGINE
e1a00d7d 1333/* Try to load an engine in a shareable library */
40889b9c 1334static ENGINE *try_load_engine(BIO *err, const char *engine, int debug)
e1a00d7d
RL
1335 {
1336 ENGINE *e = ENGINE_by_id("dynamic");
1337 if (e)
1338 {
1339 if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0)
1340 || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
1341 {
1342 ENGINE_free(e);
1343 e = NULL;
1344 }
1345 }
1346 return e;
1347 }
1348
531d630b
RL
1349ENGINE *setup_engine(BIO *err, const char *engine, int debug)
1350 {
1351 ENGINE *e = NULL;
1352
1353 if (engine)
1354 {
34c66925
GT
1355 if(strcmp(engine, "auto") == 0)
1356 {
1357 BIO_printf(err,"enabling auto ENGINE support\n");
1358 ENGINE_register_all_complete();
1359 return NULL;
1360 }
e1a00d7d
RL
1361 if((e = ENGINE_by_id(engine)) == NULL
1362 && (e = try_load_engine(err, engine, debug)) == NULL)
531d630b
RL
1363 {
1364 BIO_printf(err,"invalid engine \"%s\"\n", engine);
e1a00d7d 1365 ERR_print_errors(err);
531d630b
RL
1366 return NULL;
1367 }
1368 if (debug)
1369 {
1370 ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM,
1371 0, err, 0);
1372 }
2fe5adc3 1373 ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, ui_method, 0, 1);
531d630b
RL
1374 if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
1375 {
1376 BIO_printf(err,"can't use that engine\n");
e1a00d7d 1377 ERR_print_errors(err);
308f028e 1378 ENGINE_free(e);
531d630b
RL
1379 return NULL;
1380 }
354c3ace 1381
e1a00d7d 1382 BIO_printf(err,"engine \"%s\" set.\n", ENGINE_get_id(e));
354c3ace 1383
531d630b
RL
1384 /* Free our "structural" reference. */
1385 ENGINE_free(e);
1386 }
1387 return e;
1388 }
0b13e9f0 1389#endif
3647bee2
DSH
1390
1391int load_config(BIO *err, CONF *cnf)
1392 {
1393 if (!cnf)
1394 cnf = config;
1395 if (!cnf)
1396 return 1;
1397
1398 OPENSSL_load_builtin_modules();
1399
1400 if (CONF_modules_load(cnf, NULL, 0) <= 0)
1401 {
1402 BIO_printf(err, "Error configuring OpenSSL\n");
1403 ERR_print_errors(err);
1404 return 0;
1405 }
1406 return 1;
1407 }
54a656ef
BL
1408
1409char *make_config_name()
1410 {
1411 const char *t=X509_get_default_cert_area();
d420ac2c 1412 size_t len;
54a656ef
BL
1413 char *p;
1414
d420ac2c
RL
1415 len=strlen(t)+strlen(OPENSSL_CONF)+2;
1416 p=OPENSSL_malloc(len);
1417 BUF_strlcpy(p,t,len);
54a656ef 1418#ifndef OPENSSL_SYS_VMS
d420ac2c 1419 BUF_strlcat(p,"/",len);
54a656ef 1420#endif
d420ac2c 1421 BUF_strlcat(p,OPENSSL_CONF,len);
54a656ef
BL
1422
1423 return p;
1424 }
f85b68cd
RL
1425
1426static unsigned long index_serial_hash(const char **a)
1427 {
1428 const char *n;
1429
1430 n=a[DB_serial];
1431 while (*n == '0') n++;
1432 return(lh_strhash(n));
1433 }
1434
1435static int index_serial_cmp(const char **a, const char **b)
1436 {
1437 const char *aa,*bb;
1438
1439 for (aa=a[DB_serial]; *aa == '0'; aa++);
1440 for (bb=b[DB_serial]; *bb == '0'; bb++);
1441 return(strcmp(aa,bb));
1442 }
1443
1444static int index_name_qual(char **a)
1445 { return(a[0][0] == 'V'); }
1446
1447static unsigned long index_name_hash(const char **a)
1448 { return(lh_strhash(a[DB_name])); }
1449
1450int index_name_cmp(const char **a, const char **b)
1451 { return(strcmp(a[DB_name],
1452 b[DB_name])); }
1453
1454static IMPLEMENT_LHASH_HASH_FN(index_serial_hash,const char **)
1455static IMPLEMENT_LHASH_COMP_FN(index_serial_cmp,const char **)
1456static IMPLEMENT_LHASH_HASH_FN(index_name_hash,const char **)
1457static IMPLEMENT_LHASH_COMP_FN(index_name_cmp,const char **)
1458
1459#undef BSIZE
1460#define BSIZE 256
1461
1462BIGNUM *load_serial(char *serialfile, int create, ASN1_INTEGER **retai)
1463 {
1464 BIO *in=NULL;
1465 BIGNUM *ret=NULL;
1466 MS_STATIC char buf[1024];
1467 ASN1_INTEGER *ai=NULL;
1468
1469 ai=ASN1_INTEGER_new();
1470 if (ai == NULL) goto err;
1471
1472 if ((in=BIO_new(BIO_s_file())) == NULL)
1473 {
1474 ERR_print_errors(bio_err);
1475 goto err;
1476 }
1477
1478 if (BIO_read_filename(in,serialfile) <= 0)
1479 {
1480 if (!create)
1481 {
1482 perror(serialfile);
1483 goto err;
1484 }
1485 else
1486 {
f85b68cd 1487 ret=BN_new();
64674bcc 1488 if (ret == NULL || !rand_serial(ret, ai))
f85b68cd 1489 BIO_printf(bio_err, "Out of memory\n");
f85b68cd
RL
1490 }
1491 }
1492 else
1493 {
1494 if (!a2i_ASN1_INTEGER(in,ai,buf,1024))
1495 {
1496 BIO_printf(bio_err,"unable to load number from %s\n",
1497 serialfile);
1498 goto err;
1499 }
1500 ret=ASN1_INTEGER_to_BN(ai,NULL);
1501 if (ret == NULL)
1502 {
1503 BIO_printf(bio_err,"error converting number from bin to BIGNUM\n");
1504 goto err;
1505 }
1506 }
1507
1508 if (ret && retai)
1509 {
1510 *retai = ai;
1511 ai = NULL;
1512 }
1513 err:
1514 if (in != NULL) BIO_free(in);
1515 if (ai != NULL) ASN1_INTEGER_free(ai);
1516 return(ret);
1517 }
1518
4c771796 1519int save_serial(char *serialfile, char *suffix, BIGNUM *serial, ASN1_INTEGER **retai)
f85b68cd 1520 {
4c771796
RL
1521 char buf[1][BSIZE];
1522 BIO *out = NULL;
f85b68cd
RL
1523 int ret=0;
1524 ASN1_INTEGER *ai=NULL;
4c771796
RL
1525 int j;
1526
1527 if (suffix == NULL)
1528 j = strlen(serialfile);
1529 else
1530 j = strlen(serialfile) + strlen(suffix) + 1;
1531 if (j >= BSIZE)
1532 {
1533 BIO_printf(bio_err,"file name too long\n");
1534 goto err;
1535 }
f85b68cd 1536
4c771796
RL
1537 if (suffix == NULL)
1538 BUF_strlcpy(buf[0], serialfile, BSIZE);
1539 else
1540 {
1541#ifndef OPENSSL_SYS_VMS
1542 j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", serialfile, suffix);
1543#else
1544 j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", serialfile, suffix);
1545#endif
1546 }
1547#ifdef RL_DEBUG
1548 BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]);
1549#endif
f85b68cd
RL
1550 out=BIO_new(BIO_s_file());
1551 if (out == NULL)
1552 {
1553 ERR_print_errors(bio_err);
1554 goto err;
1555 }
4c771796 1556 if (BIO_write_filename(out,buf[0]) <= 0)
f85b68cd
RL
1557 {
1558 perror(serialfile);
1559 goto err;
1560 }
1561
1562 if ((ai=BN_to_ASN1_INTEGER(serial,NULL)) == NULL)
1563 {
1564 BIO_printf(bio_err,"error converting serial to ASN.1 format\n");
1565 goto err;
1566 }
1567 i2a_ASN1_INTEGER(out,ai);
1568 BIO_puts(out,"\n");
1569 ret=1;
1570 if (retai)
1571 {
1572 *retai = ai;
1573 ai = NULL;
1574 }
1575err:
1576 if (out != NULL) BIO_free_all(out);
1577 if (ai != NULL) ASN1_INTEGER_free(ai);
1578 return(ret);
1579 }
1580
4c771796
RL
1581int rotate_serial(char *serialfile, char *new_suffix, char *old_suffix)
1582 {
1583 char buf[5][BSIZE];
1584 int i,j;
1585 struct stat sb;
1586
1587 i = strlen(serialfile) + strlen(old_suffix);
1588 j = strlen(serialfile) + strlen(new_suffix);
1589 if (i > j) j = i;
1590 if (j + 1 >= BSIZE)
1591 {
1592 BIO_printf(bio_err,"file name too long\n");
1593 goto err;
1594 }
1595
1596#ifndef OPENSSL_SYS_VMS
1597 j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s",
1598 serialfile, new_suffix);
1599#else
1600 j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s",
1601 serialfile, new_suffix);
1602#endif
1603#ifndef OPENSSL_SYS_VMS
1604 j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s",
1605 serialfile, old_suffix);
1606#else
1607 j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s",
1608 serialfile, old_suffix);
1609#endif
1610 if (stat(serialfile,&sb) < 0)
1611 {
1612 if (errno != ENOENT
1613#ifdef ENOTDIR
53bb3bee 1614 && errno != ENOTDIR
4c771796 1615#endif
53bb3bee 1616 )
4c771796
RL
1617 goto err;
1618 }
1619 else
1620 {
1621#ifdef RL_DEBUG
1622 BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
1623 serialfile, buf[1]);
1624#endif
1625 if (rename(serialfile,buf[1]) < 0)
1626 {
1627 BIO_printf(bio_err,
1628 "unable to rename %s to %s\n",
1629 serialfile, buf[1]);
1630 perror("reason");
1631 goto err;
1632 }
1633 }
1634#ifdef RL_DEBUG
1635 BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
1636 buf[0],serialfile);
1637#endif
1638 if (rename(buf[0],serialfile) < 0)
1639 {
1640 BIO_printf(bio_err,
1641 "unable to rename %s to %s\n",
1642 buf[0],serialfile);
1643 perror("reason");
1644 rename(buf[1],serialfile);
1645 goto err;
1646 }
1647 return 1;
1648 err:
1649 return 0;
1650 }
1651
64674bcc
DSH
1652int rand_serial(BIGNUM *b, ASN1_INTEGER *ai)
1653 {
1654 BIGNUM *btmp;
1655 int ret = 0;
1656 if (b)
1657 btmp = b;
1658 else
1659 btmp = BN_new();
1660
1661 if (!btmp)
1662 return 0;
1663
1664 if (!BN_pseudo_rand(btmp, SERIAL_RAND_BITS, 0, 0))
1665 goto error;
1666 if (ai && !BN_to_ASN1_INTEGER(btmp, ai))
1667 goto error;
1668
1669 ret = 1;
1670
1671 error:
1672
1673 if (!b)
1674 BN_free(btmp);
1675
1676 return ret;
1677 }
1678
f85b68cd
RL
1679CA_DB *load_index(char *dbfile, DB_ATTR *db_attr)
1680 {
1681 CA_DB *retdb = NULL;
1682 TXT_DB *tmpdb = NULL;
1683 BIO *in = BIO_new(BIO_s_file());
1684 CONF *dbattr_conf = NULL;
1685 char buf[1][BSIZE];
1686 long errorline= -1;
1687
1688 if (in == NULL)
1689 {
1690 ERR_print_errors(bio_err);
1691 goto err;
1692 }
1693 if (BIO_read_filename(in,dbfile) <= 0)
1694 {
1695 perror(dbfile);
1696 BIO_printf(bio_err,"unable to open '%s'\n",dbfile);
1697 goto err;
1698 }
1699 if ((tmpdb = TXT_DB_read(in,DB_NUMBER)) == NULL)
1700 {
1701 if (tmpdb != NULL) TXT_DB_free(tmpdb);
1702 goto err;
1703 }
1704
1705#ifndef OPENSSL_SYS_VMS
1706 BIO_snprintf(buf[0], sizeof buf[0], "%s.attr", dbfile);
1707#else
1708 BIO_snprintf(buf[0], sizeof buf[0], "%s-attr", dbfile);
1709#endif
1710 dbattr_conf = NCONF_new(NULL);
1711 if (NCONF_load(dbattr_conf,buf[0],&errorline) <= 0)
1712 {
1713 if (errorline > 0)
1714 {
1715 BIO_printf(bio_err,
1716 "error on line %ld of db attribute file '%s'\n"
1717 ,errorline,buf[0]);
1718 goto err;
1719 }
1720 else
1721 {
1722 NCONF_free(dbattr_conf);
1723 dbattr_conf = NULL;
1724 }
1725 }
1726
1727 if ((retdb = OPENSSL_malloc(sizeof(CA_DB))) == NULL)
1728 {
1729 fprintf(stderr, "Out of memory\n");
1730 goto err;
1731 }
1732
1733 retdb->db = tmpdb;
1734 tmpdb = NULL;
1735 if (db_attr)
1736 retdb->attributes = *db_attr;
1737 else
1738 {
1739 retdb->attributes.unique_subject = 1;
1740 }
1741
1742 if (dbattr_conf)
1743 {
1744 char *p = NCONF_get_string(dbattr_conf,NULL,"unique_subject");
1745 if (p)
1746 {
83b23ed9 1747#ifdef RL_DEBUG
f85b68cd 1748 BIO_printf(bio_err, "DEBUG[load_index]: unique_subject = \"%s\"\n", p);
83b23ed9 1749#endif
03ddbdd9 1750 retdb->attributes.unique_subject = parse_yesno(p,1);
f85b68cd
RL
1751 }
1752 }
1753
1754 err:
1755 if (dbattr_conf) NCONF_free(dbattr_conf);
1756 if (tmpdb) TXT_DB_free(tmpdb);
1757 if (in) BIO_free_all(in);
1758 return retdb;
1759 }
1760
1761int index_index(CA_DB *db)
1762 {
1763 if (!TXT_DB_create_index(db->db, DB_serial, NULL,
1764 LHASH_HASH_FN(index_serial_hash),
1765 LHASH_COMP_FN(index_serial_cmp)))
1766 {
1767 BIO_printf(bio_err,
1768 "error creating serial number index:(%ld,%ld,%ld)\n",
1769 db->db->error,db->db->arg1,db->db->arg2);
1770 return 0;
1771 }
1772
1773 if (db->attributes.unique_subject
1774 && !TXT_DB_create_index(db->db, DB_name, index_name_qual,
1775 LHASH_HASH_FN(index_name_hash),
1776 LHASH_COMP_FN(index_name_cmp)))
1777 {
1778 BIO_printf(bio_err,"error creating name index:(%ld,%ld,%ld)\n",
1779 db->db->error,db->db->arg1,db->db->arg2);
1780 return 0;
1781 }
1782 return 1;
1783 }
1784
7d727231 1785int save_index(const char *dbfile, const char *suffix, CA_DB *db)
f85b68cd
RL
1786 {
1787 char buf[3][BSIZE];
1788 BIO *out = BIO_new(BIO_s_file());
1789 int j;
1790
1791 if (out == NULL)
1792 {
1793 ERR_print_errors(bio_err);
1794 goto err;
1795 }
1796
1797 j = strlen(dbfile) + strlen(suffix);
1798 if (j + 6 >= BSIZE)
1799 {
1800 BIO_printf(bio_err,"file name too long\n");
1801 goto err;
1802 }
1803
1804#ifndef OPENSSL_SYS_VMS
1805 j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr", dbfile);
1806#else
1807 j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr", dbfile);
1808#endif
1809#ifndef OPENSSL_SYS_VMS
1810 j = BIO_snprintf(buf[1], sizeof buf[1], "%s.attr.%s", dbfile, suffix);
1811#else
1812 j = BIO_snprintf(buf[1], sizeof buf[1], "%s-attr-%s", dbfile, suffix);
1813#endif
1814#ifndef OPENSSL_SYS_VMS
1815 j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", dbfile, suffix);
1816#else
1817 j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", dbfile, suffix);
1818#endif
63b6fe2b 1819#ifdef RL_DEBUG
f85b68cd 1820 BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]);
63b6fe2b 1821#endif
f85b68cd
RL
1822 if (BIO_write_filename(out,buf[0]) <= 0)
1823 {
1824 perror(dbfile);
1825 BIO_printf(bio_err,"unable to open '%s'\n", dbfile);
1826 goto err;
1827 }
1828 j=TXT_DB_write(out,db->db);
1829 if (j <= 0) goto err;
1830
1831 BIO_free(out);
1832
1833 out = BIO_new(BIO_s_file());
63b6fe2b 1834#ifdef RL_DEBUG
f85b68cd 1835 BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[1]);
63b6fe2b 1836#endif
f85b68cd
RL
1837 if (BIO_write_filename(out,buf[1]) <= 0)
1838 {
1839 perror(buf[2]);
1840 BIO_printf(bio_err,"unable to open '%s'\n", buf[2]);
1841 goto err;
1842 }
1843 BIO_printf(out,"unique_subject = %s\n",
1844 db->attributes.unique_subject ? "yes" : "no");
1845 BIO_free(out);
1846
1847 return 1;
1848 err:
1849 return 0;
1850 }
1851
7d727231 1852int rotate_index(const char *dbfile, const char *new_suffix, const char *old_suffix)
f85b68cd
RL
1853 {
1854 char buf[5][BSIZE];
1855 int i,j;
1856 struct stat sb;
1857
1858 i = strlen(dbfile) + strlen(old_suffix);
1859 j = strlen(dbfile) + strlen(new_suffix);
1860 if (i > j) j = i;
1861 if (j + 6 >= BSIZE)
1862 {
1863 BIO_printf(bio_err,"file name too long\n");
1864 goto err;
1865 }
1866
1867#ifndef OPENSSL_SYS_VMS
1868 j = BIO_snprintf(buf[4], sizeof buf[4], "%s.attr", dbfile);
1869#else
1870 j = BIO_snprintf(buf[4], sizeof buf[4], "%s-attr", dbfile);
1871#endif
1872#ifndef OPENSSL_SYS_VMS
1873 j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr.%s",
1874 dbfile, new_suffix);
1875#else
1876 j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr-%s",
1877 dbfile, new_suffix);
1878#endif
1879#ifndef OPENSSL_SYS_VMS
1880 j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s",
1881 dbfile, new_suffix);
1882#else
1883 j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s",
1884 dbfile, new_suffix);
1885#endif
1886#ifndef OPENSSL_SYS_VMS
1887 j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s",
1888 dbfile, old_suffix);
1889#else
1890 j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s",
1891 dbfile, old_suffix);
1892#endif
1893#ifndef OPENSSL_SYS_VMS
1894 j = BIO_snprintf(buf[3], sizeof buf[3], "%s.attr.%s",
1895 dbfile, old_suffix);
1896#else
1897 j = BIO_snprintf(buf[3], sizeof buf[3], "%s-attr-%s",
1898 dbfile, old_suffix);
1899#endif
1900 if (stat(dbfile,&sb) < 0)
1901 {
1902 if (errno != ENOENT
1903#ifdef ENOTDIR
53bb3bee 1904 && errno != ENOTDIR
f85b68cd 1905#endif
53bb3bee 1906 )
f85b68cd
RL
1907 goto err;
1908 }
1909 else
1910 {
63b6fe2b 1911#ifdef RL_DEBUG
f85b68cd
RL
1912 BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
1913 dbfile, buf[1]);
63b6fe2b 1914#endif
f85b68cd
RL
1915 if (rename(dbfile,buf[1]) < 0)
1916 {
1917 BIO_printf(bio_err,
1918 "unable to rename %s to %s\n",
1919 dbfile, buf[1]);
1920 perror("reason");
1921 goto err;
1922 }
1923 }
63b6fe2b 1924#ifdef RL_DEBUG
f85b68cd
RL
1925 BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
1926 buf[0],dbfile);
63b6fe2b 1927#endif
f85b68cd
RL
1928 if (rename(buf[0],dbfile) < 0)
1929 {
1930 BIO_printf(bio_err,
1931 "unable to rename %s to %s\n",
1932 buf[0],dbfile);
1933 perror("reason");
1934 rename(buf[1],dbfile);
1935 goto err;
1936 }
1937 if (stat(buf[4],&sb) < 0)
1938 {
1939 if (errno != ENOENT
1940#ifdef ENOTDIR
53bb3bee 1941 && errno != ENOTDIR
f85b68cd 1942#endif
53bb3bee 1943 )
f85b68cd
RL
1944 goto err;
1945 }
1946 else
1947 {
63b6fe2b 1948#ifdef RL_DEBUG
f85b68cd
RL
1949 BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
1950 buf[4],buf[3]);
63b6fe2b 1951#endif
f85b68cd
RL
1952 if (rename(buf[4],buf[3]) < 0)
1953 {
1954 BIO_printf(bio_err,
1955 "unable to rename %s to %s\n",
1956 buf[4], buf[3]);
1957 perror("reason");
1958 rename(dbfile,buf[0]);
1959 rename(buf[1],dbfile);
1960 goto err;
1961 }
1962 }
63b6fe2b 1963#ifdef RL_DEBUG
f85b68cd
RL
1964 BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
1965 buf[2],buf[4]);
63b6fe2b 1966#endif
f85b68cd
RL
1967 if (rename(buf[2],buf[4]) < 0)
1968 {
1969 BIO_printf(bio_err,
1970 "unable to rename %s to %s\n",
1971 buf[2],buf[4]);
1972 perror("reason");
1973 rename(buf[3],buf[4]);
1974 rename(dbfile,buf[0]);
1975 rename(buf[1],dbfile);
1976 goto err;
1977 }
1978 return 1;
1979 err:
1980 return 0;
1981 }
1982
1983void free_index(CA_DB *db)
1984 {
db598fbc
RL
1985 if (db)
1986 {
1987 if (db->db) TXT_DB_free(db->db);
1988 OPENSSL_free(db);
1989 }
f85b68cd 1990 }
6d5ffb59 1991
ff990440 1992int parse_yesno(const char *str, int def)
03ddbdd9
RL
1993 {
1994 int ret = def;
1995 if (str)
1996 {
1997 switch (*str)
1998 {
1999 case 'f': /* false */
2000 case 'F': /* FALSE */
2001 case 'n': /* no */
2002 case 'N': /* NO */
2003 case '0': /* 0 */
2004 ret = 0;
2005 break;
2006 case 't': /* true */
2007 case 'T': /* TRUE */
2008 case 'y': /* yes */
2009 case 'Y': /* YES */
2010 case '1': /* 1 */
2011 ret = 0;
2012 break;
2013 default:
2014 ret = def;
2015 break;
2016 }
2017 }
2018 return ret;
2019 }
2020
6d5ffb59
RL
2021/*
2022 * subject is expected to be in the format /type0=value0/type1=value1/type2=...
2023 * where characters may be escaped by \
2024 */
2025X509_NAME *parse_name(char *subject, long chtype, int multirdn)
2026 {
2027 size_t buflen = strlen(subject)+1; /* to copy the types and values into. due to escaping, the copy can only become shorter */
2028 char *buf = OPENSSL_malloc(buflen);
2029 size_t max_ne = buflen / 2 + 1; /* maximum number of name elements */
2030 char **ne_types = OPENSSL_malloc(max_ne * sizeof (char *));
2031 char **ne_values = OPENSSL_malloc(max_ne * sizeof (char *));
2032 int *mval = OPENSSL_malloc (max_ne * sizeof (int));
2033
2034 char *sp = subject, *bp = buf;
2035 int i, ne_num = 0;
2036
2037 X509_NAME *n = NULL;
2038 int nid;
2039
2040 if (!buf || !ne_types || !ne_values)
2041 {
2042 BIO_printf(bio_err, "malloc error\n");
2043 goto error;
2044 }
2045
2046 if (*subject != '/')
2047 {
2048 BIO_printf(bio_err, "Subject does not start with '/'.\n");
2049 goto error;
2050 }
2051 sp++; /* skip leading / */
2052
2053 /* no multivalued RDN by default */
2054 mval[ne_num] = 0;
2055
2056 while (*sp)
2057 {
2058 /* collect type */
2059 ne_types[ne_num] = bp;
2060 while (*sp)
2061 {
2062 if (*sp == '\\') /* is there anything to escape in the type...? */
2063 {
2064 if (*++sp)
2065 *bp++ = *sp++;
2066 else
2067 {
2068 BIO_printf(bio_err, "escape character at end of string\n");
2069 goto error;
2070 }
2071 }
2072 else if (*sp == '=')
2073 {
2074 sp++;
2075 *bp++ = '\0';
2076 break;
2077 }
2078 else
2079 *bp++ = *sp++;
2080 }
2081 if (!*sp)
2082 {
2083 BIO_printf(bio_err, "end of string encountered while processing type of subject name element #%d\n", ne_num);
2084 goto error;
2085 }
2086 ne_values[ne_num] = bp;
2087 while (*sp)
2088 {
2089 if (*sp == '\\')
2090 {
2091 if (*++sp)
2092 *bp++ = *sp++;
2093 else
2094 {
2095 BIO_printf(bio_err, "escape character at end of string\n");
2096 goto error;
2097 }
2098 }
2099 else if (*sp == '/')
2100 {
2101 sp++;
2102 /* no multivalued RDN by default */
2103 mval[ne_num+1] = 0;
2104 break;
2105 }
2106 else if (*sp == '+' && multirdn)
2107 {
2108 /* a not escaped + signals a mutlivalued RDN */
2109 sp++;
2110 mval[ne_num+1] = -1;
2111 break;
2112 }
2113 else
2114 *bp++ = *sp++;
2115 }
2116 *bp++ = '\0';
2117 ne_num++;
2118 }
2119
2120 if (!(n = X509_NAME_new()))
2121 goto error;
2122
2123 for (i = 0; i < ne_num; i++)
2124 {
2125 if ((nid=OBJ_txt2nid(ne_types[i])) == NID_undef)
2126 {
2127 BIO_printf(bio_err, "Subject Attribute %s has no known NID, skipped\n", ne_types[i]);
2128 continue;
2129 }
2130
2131 if (!*ne_values[i])
2132 {
2133 BIO_printf(bio_err, "No value provided for Subject Attribute %s, skipped\n", ne_types[i]);
2134 continue;
2135 }
2136
2137 if (!X509_NAME_add_entry_by_NID(n, nid, chtype, (unsigned char*)ne_values[i], -1,-1,mval[i]))
2138 goto error;
2139 }
2140
2141 OPENSSL_free(ne_values);
2142 OPENSSL_free(ne_types);
2143 OPENSSL_free(buf);
2144 return n;
2145
2146error:
2147 X509_NAME_free(n);
2148 if (ne_values)
2149 OPENSSL_free(ne_values);
2150 if (ne_types)
2151 OPENSSL_free(ne_types);
2152 if (buf)
2153 OPENSSL_free(buf);
2154 return NULL;
2155}
2156
d530017c
RL
2157/* This code MUST COME AFTER anything that uses rename() */
2158#ifdef OPENSSL_SYS_WIN32
f68854b4 2159int WIN32_rename(const char *from, const char *to)
d530017c
RL
2160 {
2161#ifndef OPENSSL_SYS_WINCE
2162 /* Windows rename gives an error if 'to' exists, so delete it
2163 * first and ignore file not found errror
2164 */
2165 if((remove(to) != 0) && (errno != ENOENT))
2166 return -1;
2167#undef rename
2168 return rename(from, to);
2169#else
2170 /* convert strings to UNICODE */
2171 {
2172 BOOL result = FALSE;
2173 WCHAR* wfrom;
2174 WCHAR* wto;
2175 int i;
2176 wfrom = malloc((strlen(from)+1)*2);
2177 wto = malloc((strlen(to)+1)*2);
2178 if (wfrom != NULL && wto != NULL)
2179 {
2180 for (i=0; i<(int)strlen(from)+1; i++)
2181 wfrom[i] = (short)from[i];
2182 for (i=0; i<(int)strlen(to)+1; i++)
2183 wto[i] = (short)to[i];
2184 result = MoveFile(wfrom, wto);
2185 }
2186 if (wfrom != NULL)
2187 free(wfrom);
2188 if (wto != NULL)
2189 free(wto);
2190 return result;
2191 }
2192#endif
2193 }
2194#endif
5d7c222d 2195
c431798e
DSH
2196int args_verify(char ***pargs, int *pargc,
2197 int *badarg, BIO *err, X509_VERIFY_PARAM **pm)
5d7c222d
DSH
2198 {
2199 ASN1_OBJECT *otmp = NULL;
2200 unsigned long flags = 0;
c431798e
DSH
2201 int i;
2202 int purpose = 0;
2203 char **oldargs = *pargs;
5d7c222d
DSH
2204 char *arg = **pargs, *argn = (*pargs)[1];
2205 if (!strcmp(arg, "-policy"))
2206 {
2207 if (!argn)
2208 *badarg = 1;
2209 else
2210 {
2211 otmp = OBJ_txt2obj(argn, 0);
2212 if (!otmp)
2213 {
2214 BIO_printf(err, "Invalid Policy \"%s\"\n",
2215 argn);
2216 *badarg = 1;
2217 }
2218 }
2219 (*pargs)++;
2220 }
c431798e
DSH
2221 else if (strcmp(arg,"-purpose") == 0)
2222 {
2223 X509_PURPOSE *xptmp;
2224 if (!argn)
2225 *badarg = 1;
2226 else
2227 {
2228 i = X509_PURPOSE_get_by_sname(argn);
2229 if(i < 0)
2230 {
2231 BIO_printf(err, "unrecognized purpose\n");
2232 *badarg = 1;
2233 }
2234 else
2235 {
2236 xptmp = X509_PURPOSE_get0(i);
2237 purpose = X509_PURPOSE_get_id(xptmp);
2238 }
2239 }
2240 (*pargs)++;
2241 }
5d7c222d
DSH
2242 else if (!strcmp(arg, "-ignore_critical"))
2243 flags |= X509_V_FLAG_IGNORE_CRITICAL;
2244 else if (!strcmp(arg, "-issuer_checks"))
2245 flags |= X509_V_FLAG_CB_ISSUER_CHECK;
2246 else if (!strcmp(arg, "-crl_check"))
2247 flags |= X509_V_FLAG_CRL_CHECK;
2248 else if (!strcmp(arg, "-crl_check_all"))
2249 flags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
2250 else if (!strcmp(arg, "-policy_check"))
2251 flags |= X509_V_FLAG_POLICY_CHECK;
2252 else if (!strcmp(arg, "-explicit_policy"))
2253 flags |= X509_V_FLAG_EXPLICIT_POLICY;
2254 else if (!strcmp(arg, "-x509_strict"))
2255 flags |= X509_V_FLAG_X509_STRICT;
2256 else if (!strcmp(arg, "-policy_print"))
2257 flags |= X509_V_FLAG_NOTIFY_POLICY;
2258 else
2259 return 0;
2260
2261 if (*badarg)
2262 {
2263 if (*pm)
2264 X509_VERIFY_PARAM_free(*pm);
2265 *pm = NULL;
c431798e 2266 goto end;
5d7c222d
DSH
2267 }
2268
2269 if (!*pm && !(*pm = X509_VERIFY_PARAM_new()))
2270 {
2271 *badarg = 1;
c431798e 2272 goto end;
5d7c222d
DSH
2273 }
2274
2275 if (otmp)
2276 X509_VERIFY_PARAM_add0_policy(*pm, otmp);
2277 if (flags)
2278 X509_VERIFY_PARAM_set_flags(*pm, flags);
2279
c431798e
DSH
2280 if (purpose)
2281 X509_VERIFY_PARAM_set_purpose(*pm, purpose);
2282
2283 end:
2284
5d7c222d
DSH
2285 (*pargs)++;
2286
c431798e
DSH
2287 if (pargc)
2288 *pargc -= *pargs - oldargs;
2289
5d7c222d
DSH
2290 return 1;
2291
2292 }
c431798e 2293
7d727231
NL
2294static void nodes_print(BIO *out, const char *name,
2295 STACK_OF(X509_POLICY_NODE) *nodes)
c431798e
DSH
2296 {
2297 X509_POLICY_NODE *node;
2298 int i;
2299 BIO_printf(out, "%s Policies:", name);
2300 if (nodes)
2301 {
2302 BIO_puts(out, "\n");
2303 for (i = 0; i < sk_X509_POLICY_NODE_num(nodes); i++)
2304 {
2305 node = sk_X509_POLICY_NODE_value(nodes, i);
2306 X509_POLICY_NODE_print(out, node, 2);
2307 }
2308 }
2309 else
2310 BIO_puts(out, " <empty>\n");
2311 }
2312
2313void policies_print(BIO *out, X509_STORE_CTX *ctx)
2314 {
2315 X509_POLICY_TREE *tree;
2316 int explicit_policy;
2317 int free_out = 0;
2318 if (out == NULL)
2319 {
2320 out = BIO_new_fp(stderr, BIO_NOCLOSE);
2321 free_out = 1;
2322 }
2323 tree = X509_STORE_CTX_get0_policy_tree(ctx);
2324 explicit_policy = X509_STORE_CTX_get_explicit_policy(ctx);
2325
2326 BIO_printf(out, "Require explicit Policy: %s\n",
2327 explicit_policy ? "True" : "False");
2328
2329 nodes_print(out, "Authority", X509_policy_tree_get0_policies(tree));
2330 nodes_print(out, "User", X509_policy_tree_get0_user_policies(tree));
2331 if (free_out)
2332 BIO_free(out);
2333 }