]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/apps.c
We have AES support in openssl speed
[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>
117#define NON_MAIN
118#include "apps.h"
119#undef NON_MAIN
90ae4673
RL
120#include <openssl/err.h>
121#include <openssl/x509.h>
535d79da 122#include <openssl/x509v3.h>
90ae4673
RL
123#include <openssl/pem.h>
124#include <openssl/pkcs12.h>
2fe5adc3 125#include <openssl/ui.h>
90ae4673 126#include <openssl/safestack.h>
1372965e 127#include <openssl/engine.h>
d02b48c6 128
19f21921
RL
129#ifdef OPENSSL_SYS_WINDOWS
130#define strcasecmp _stricmp
131#else
132#include <strings.h>
133#endif
134
bc36ee62 135#ifdef OPENSSL_SYS_WINDOWS
58964a49 136# include "bss_file.c"
d02b48c6
RE
137#endif
138
8ca533e3
DSH
139typedef struct {
140 char *name;
141 unsigned long flag;
142 unsigned long mask;
143} NAME_EX_TBL;
144
2fe5adc3
RL
145static UI_METHOD *ui_method = NULL;
146
8ca533e3 147static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
535d79da 148static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
8ca533e3 149
80bb905d
RL
150#ifndef OPENSSL_NO_RC4
151/* Looks like this stuff is worth moving into separate function */
152static EVP_PKEY *
153load_netscape_key(BIO *err, BIO *key, const char *file,
154 const char *key_descrip, int format);
155#endif
156
d02b48c6 157int app_init(long mesgwin);
d02b48c6 158#ifdef undef /* never finished - probably never will be :-) */
6b691a5c 159int args_from_file(char *file, int *argc, char **argv[])
d02b48c6
RE
160 {
161 FILE *fp;
162 int num,i;
163 unsigned int len;
164 static char *buf=NULL;
165 static char **arg=NULL;
166 char *p;
167 struct stat stbuf;
168
169 if (stat(file,&stbuf) < 0) return(0);
170
171 fp=fopen(file,"r");
172 if (fp == NULL)
173 return(0);
174
175 *argc=0;
176 *argv=NULL;
177
178 len=(unsigned int)stbuf.st_size;
26a3a48d
RL
179 if (buf != NULL) OPENSSL_free(buf);
180 buf=(char *)OPENSSL_malloc(len+1);
d02b48c6
RE
181 if (buf == NULL) return(0);
182
183 len=fread(buf,1,len,fp);
184 if (len <= 1) return(0);
185 buf[len]='\0';
186
187 i=0;
188 for (p=buf; *p; p++)
189 if (*p == '\n') i++;
26a3a48d
RL
190 if (arg != NULL) OPENSSL_free(arg);
191 arg=(char **)OPENSSL_malloc(sizeof(char *)*(i*2));
d02b48c6
RE
192
193 *argv=arg;
194 num=0;
195 p=buf;
196 for (;;)
197 {
198 if (!*p) break;
199 if (*p == '#') /* comment line */
200 {
201 while (*p && (*p != '\n')) p++;
202 continue;
203 }
204 /* else we have a line */
205 *(arg++)=p;
206 num++;
207 while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n')))
208 p++;
209 if (!*p) break;
210 if (*p == '\n')
211 {
212 *(p++)='\0';
213 continue;
214 }
215 /* else it is a tab or space */
216 p++;
217 while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
218 p++;
219 if (!*p) break;
220 if (*p == '\n')
221 {
222 p++;
223 continue;
224 }
225 *(arg++)=p++;
226 num++;
227 while (*p && (*p != '\n')) p++;
228 if (!*p) break;
229 /* else *p == '\n' */
230 *(p++)='\0';
231 }
232 *argc=num;
233 return(1);
234 }
235#endif
236
6b691a5c 237int str2fmt(char *s)
d02b48c6
RE
238 {
239 if ((*s == 'D') || (*s == 'd'))
240 return(FORMAT_ASN1);
241 else if ((*s == 'T') || (*s == 't'))
242 return(FORMAT_TEXT);
243 else if ((*s == 'P') || (*s == 'p'))
244 return(FORMAT_PEM);
245 else if ((*s == 'N') || (*s == 'n'))
246 return(FORMAT_NETSCAPE);
094fe66d
DSH
247 else if ((*s == 'S') || (*s == 's'))
248 return(FORMAT_SMIME);
90ae4673
RL
249 else if ((*s == '1')
250 || (strcmp(s,"PKCS12") == 0) || (strcmp(s,"pkcs12") == 0)
251 || (strcmp(s,"P12") == 0) || (strcmp(s,"p12") == 0))
252 return(FORMAT_PKCS12);
5270e702
RL
253 else if ((*s == 'E') || (*s == 'e'))
254 return(FORMAT_ENGINE);
d02b48c6
RE
255 else
256 return(FORMAT_UNDEF);
257 }
258
bc36ee62 259#if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
6b691a5c 260void program_name(char *in, char *out, int size)
d02b48c6
RE
261 {
262 int i,n;
263 char *p=NULL;
264
265 n=strlen(in);
266 /* find the last '/', '\' or ':' */
267 for (i=n-1; i>0; i--)
268 {
269 if ((in[i] == '/') || (in[i] == '\\') || (in[i] == ':'))
270 {
271 p= &(in[i+1]);
272 break;
273 }
274 }
275 if (p == NULL)
276 p=in;
277 n=strlen(p);
278 /* strip off trailing .exe if present. */
279 if ((n > 4) && (p[n-4] == '.') &&
280 ((p[n-3] == 'e') || (p[n-3] == 'E')) &&
281 ((p[n-2] == 'x') || (p[n-2] == 'X')) &&
282 ((p[n-1] == 'e') || (p[n-1] == 'E')))
283 n-=4;
284 if (n > size-1)
285 n=size-1;
286
287 for (i=0; i<n; i++)
288 {
289 if ((p[i] >= 'A') && (p[i] <= 'Z'))
290 out[i]=p[i]-'A'+'a';
291 else
292 out[i]=p[i];
293 }
294 out[n]='\0';
295 }
296#else
bc36ee62 297#ifdef OPENSSL_SYS_VMS
7d7d2cbc
UM
298void program_name(char *in, char *out, int size)
299 {
300 char *p=in, *q;
301 char *chars=":]>";
302
303 while(*chars != '\0')
304 {
305 q=strrchr(p,*chars);
306 if (q > p)
307 p = q + 1;
308 chars++;
309 }
310
311 q=strrchr(p,'.');
312 if (q == NULL)
313 q = in+size;
314 strncpy(out,p,q-p);
315 out[q-p]='\0';
316 }
317#else
6b691a5c 318void program_name(char *in, char *out, int size)
d02b48c6
RE
319 {
320 char *p;
321
322 p=strrchr(in,'/');
323 if (p != NULL)
324 p++;
325 else
326 p=in;
327 strncpy(out,p,size-1);
328 out[size-1]='\0';
329 }
330#endif
7d7d2cbc 331#endif
d02b48c6 332
bc36ee62 333#ifdef OPENSSL_SYS_WIN32
6b691a5c 334int WIN32_rename(char *from, char *to)
d02b48c6 335 {
bc36ee62 336#ifdef OPENSSL_SYS_WINNT
d02b48c6 337 int ret;
a5ab0532 338/* Note: MoveFileEx() doesn't work under Win95, Win98 */
d02b48c6
RE
339
340 ret=MoveFileEx(from,to,MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED);
341 return(ret?0:-1);
a5ab0532 342#else
b1c4fe36
BM
343 unlink(to);
344 return MoveFile(from, to);
a5ab0532 345#endif
d02b48c6
RE
346 }
347#endif
348
6b691a5c 349int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[])
d02b48c6
RE
350 {
351 int num,len,i;
352 char *p;
353
354 *argc=0;
355 *argv=NULL;
356
357 len=strlen(buf);
358 i=0;
359 if (arg->count == 0)
360 {
361 arg->count=20;
26a3a48d 362 arg->data=(char **)OPENSSL_malloc(sizeof(char *)*arg->count);
d02b48c6
RE
363 }
364 for (i=0; i<arg->count; i++)
365 arg->data[i]=NULL;
366
367 num=0;
368 p=buf;
369 for (;;)
370 {
371 /* first scan over white space */
372 if (!*p) break;
373 while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
374 p++;
375 if (!*p) break;
376
377 /* The start of something good :-) */
378 if (num >= arg->count)
379 {
380 arg->count+=20;
26a3a48d 381 arg->data=(char **)OPENSSL_realloc(arg->data,
d02b48c6
RE
382 sizeof(char *)*arg->count);
383 if (argc == 0) return(0);
384 }
385 arg->data[num++]=p;
386
387 /* now look for the end of this */
388 if ((*p == '\'') || (*p == '\"')) /* scan for closing quote */
389 {
390 i= *(p++);
391 arg->data[num-1]++; /* jump over quote */
392 while (*p && (*p != i))
393 p++;
394 *p='\0';
395 }
396 else
397 {
398 while (*p && ((*p != ' ') &&
399 (*p != '\t') && (*p != '\n')))
400 p++;
401
402 if (*p == '\0')
403 p--;
404 else
405 *p='\0';
406 }
407 p++;
408 }
409 *argc=num;
410 *argv=arg->data;
411 return(1);
412 }
413
414#ifndef APP_INIT
6b691a5c 415int app_init(long mesgwin)
d02b48c6
RE
416 {
417 return(1);
418 }
419#endif
53b1899e 420
a3fe382e 421
954ef7ef
DSH
422int dump_cert_text (BIO *out, X509 *x)
423{
424 char buf[256];
425 X509_NAME_oneline(X509_get_subject_name(x),buf,256);
426 BIO_puts(out,"subject=");
427 BIO_puts(out,buf);
428
429 X509_NAME_oneline(X509_get_issuer_name(x),buf,256);
430 BIO_puts(out,"\nissuer= ");
431 BIO_puts(out,buf);
432 BIO_puts(out,"\n");
433 return 0;
434}
a3fe382e 435
2fe5adc3
RL
436static int ui_open(UI *ui)
437 {
438 return UI_method_get_opener(UI_OpenSSL())(ui);
439 }
440static int ui_read(UI *ui, UI_STRING *uis)
441 {
442 if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
443 && UI_get0_user_data(ui))
444 {
445 switch(UI_get_string_type(uis))
446 {
447 case UIT_PROMPT:
448 case UIT_VERIFY:
449 {
450 const char *password =
451 ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
452 if (password[0] != '\0')
453 {
4f272c17 454 UI_set_result(ui, uis, password);
2fe5adc3
RL
455 return 1;
456 }
457 }
458 default:
459 break;
460 }
461 }
462 return UI_method_get_reader(UI_OpenSSL())(ui, uis);
463 }
464static int ui_write(UI *ui, UI_STRING *uis)
465 {
466 if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
467 && UI_get0_user_data(ui))
468 {
469 switch(UI_get_string_type(uis))
470 {
471 case UIT_PROMPT:
472 case UIT_VERIFY:
473 {
474 const char *password =
475 ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
476 if (password[0] != '\0')
477 return 1;
478 }
479 default:
480 break;
481 }
482 }
483 return UI_method_get_writer(UI_OpenSSL())(ui, uis);
484 }
485static int ui_close(UI *ui)
486 {
487 return UI_method_get_closer(UI_OpenSSL())(ui);
488 }
489int setup_ui_method()
490 {
491 ui_method = UI_create_method("OpenSSL application user interface");
492 UI_method_set_opener(ui_method, ui_open);
493 UI_method_set_reader(ui_method, ui_read);
494 UI_method_set_writer(ui_method, ui_write);
495 UI_method_set_closer(ui_method, ui_close);
496 return 0;
497 }
2c7bc88d
DSH
498void destroy_ui_method()
499 {
500 if(ui_method)
501 {
502 UI_destroy_method(ui_method);
503 ui_method = NULL;
504 }
505 }
30b4c272 506int password_callback(char *buf, int bufsiz, int verify,
2fe5adc3 507 PW_CB_DATA *cb_tmp)
30b4c272 508 {
2fe5adc3
RL
509 UI *ui = NULL;
510 int res = 0;
30b4c272
RL
511 const char *prompt_info = NULL;
512 const char *password = NULL;
2fe5adc3 513 PW_CB_DATA *cb_data = (PW_CB_DATA *)cb_tmp;
30b4c272
RL
514
515 if (cb_data)
516 {
517 if (cb_data->password)
518 password = cb_data->password;
519 if (cb_data->prompt_info)
520 prompt_info = cb_data->prompt_info;
521 }
522
b65f8513
DSH
523 if (password)
524 {
525 res = strlen(password);
526 if (res > bufsiz)
527 res = bufsiz;
528 memcpy(buf, password, res);
529 return res;
530 }
531
2fe5adc3
RL
532 ui = UI_new_method(ui_method);
533 if (ui)
534 {
2fe5adc3
RL
535 int ok = 0;
536 char *buff = NULL;
537 int ui_flags = 0;
538 char *prompt = NULL;
30b4c272 539
2fe5adc3
RL
540 prompt = UI_construct_prompt(ui, "pass phrase",
541 cb_data->prompt_info);
30b4c272 542
2fe5adc3 543 ui_flags |= UI_INPUT_FLAG_DEFAULT_PWD;
4f272c17 544 UI_ctrl(ui, UI_CTRL_PRINT_ERRORS, 1, 0, 0);
2fe5adc3
RL
545
546 if (ok >= 0)
4f272c17
RL
547 ok = UI_add_input_string(ui,prompt,ui_flags,buf,
548 PW_MIN_LENGTH,BUFSIZ-1);
2fe5adc3
RL
549 if (ok >= 0 && verify)
550 {
551 buff = (char *)OPENSSL_malloc(bufsiz);
4f272c17
RL
552 ok = UI_add_verify_string(ui,prompt,ui_flags,buff,
553 PW_MIN_LENGTH,BUFSIZ-1, buf);
2fe5adc3
RL
554 }
555 if (ok >= 0)
4f272c17 556 do
2fe5adc3 557 {
4f272c17 558 ok = UI_process(ui);
2fe5adc3 559 }
4f272c17
RL
560 while (ok < 0 && UI_ctrl(ui, UI_CTRL_IS_REDOABLE, 0, 0, 0));
561
2fe5adc3 562 if (buff)
30b4c272 563 {
4f272c17 564 memset(buff,0,(unsigned int)bufsiz);
2fe5adc3 565 OPENSSL_free(buff);
30b4c272 566 }
2fe5adc3 567
4f272c17
RL
568 if (ok >= 0)
569 res = strlen(buf);
2fe5adc3 570 if (ok == -1)
30b4c272 571 {
2fe5adc3
RL
572 BIO_printf(bio_err, "User interface error\n");
573 ERR_print_errors(bio_err);
574 memset(buf,0,(unsigned int)bufsiz);
575 res = 0;
30b4c272 576 }
2fe5adc3
RL
577 if (ok == -2)
578 {
579 BIO_printf(bio_err,"aborted!\n");
580 memset(buf,0,(unsigned int)bufsiz);
581 res = 0;
582 }
583 UI_free(ui);
4f272c17 584 OPENSSL_free(prompt);
30b4c272 585 }
2fe5adc3 586 return res;
30b4c272
RL
587 }
588
a3fe382e
DSH
589static char *app_get_pass(BIO *err, char *arg, int keepbio);
590
591int app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2)
592{
593 int same;
594 if(!arg2 || !arg1 || strcmp(arg1, arg2)) same = 0;
595 else same = 1;
596 if(arg1) {
597 *pass1 = app_get_pass(err, arg1, same);
598 if(!*pass1) return 0;
599 } else if(pass1) *pass1 = NULL;
600 if(arg2) {
601 *pass2 = app_get_pass(err, arg2, same ? 2 : 0);
602 if(!*pass2) return 0;
603 } else if(pass2) *pass2 = NULL;
604 return 1;
605}
606
607static char *app_get_pass(BIO *err, char *arg, int keepbio)
608{
609 char *tmp, tpass[APP_PASS_LEN];
610 static BIO *pwdbio = NULL;
611 int i;
612 if(!strncmp(arg, "pass:", 5)) return BUF_strdup(arg + 5);
613 if(!strncmp(arg, "env:", 4)) {
614 tmp = getenv(arg + 4);
615 if(!tmp) {
616 BIO_printf(err, "Can't read environment variable %s\n", arg + 4);
617 return NULL;
618 }
619 return BUF_strdup(tmp);
620 }
621 if(!keepbio || !pwdbio) {
622 if(!strncmp(arg, "file:", 5)) {
623 pwdbio = BIO_new_file(arg + 5, "r");
624 if(!pwdbio) {
625 BIO_printf(err, "Can't open file %s\n", arg + 5);
626 return NULL;
627 }
628 } else if(!strncmp(arg, "fd:", 3)) {
629 BIO *btmp;
630 i = atoi(arg + 3);
631 if(i >= 0) pwdbio = BIO_new_fd(i, BIO_NOCLOSE);
632 if((i < 0) || !pwdbio) {
633 BIO_printf(err, "Can't access file descriptor %s\n", arg + 3);
634 return NULL;
635 }
636 /* Can't do BIO_gets on an fd BIO so add a buffering BIO */
637 btmp = BIO_new(BIO_f_buffer());
638 pwdbio = BIO_push(btmp, pwdbio);
639 } else if(!strcmp(arg, "stdin")) {
640 pwdbio = BIO_new_fp(stdin, BIO_NOCLOSE);
641 if(!pwdbio) {
642 BIO_printf(err, "Can't open BIO for stdin\n");
643 return NULL;
644 }
645 } else {
646 BIO_printf(err, "Invalid password argument \"%s\"\n", arg);
647 return NULL;
648 }
649 }
650 i = BIO_gets(pwdbio, tpass, APP_PASS_LEN);
651 if(keepbio != 1) {
652 BIO_free_all(pwdbio);
653 pwdbio = NULL;
654 }
655 if(i <= 0) {
656 BIO_printf(err, "Error reading password from BIO\n");
657 return NULL;
658 }
659 tmp = strchr(tpass, '\n');
660 if(tmp) *tmp = 0;
661 return BUF_strdup(tpass);
662}
90ae4673 663
b7a26e6d 664int add_oid_section(BIO *err, CONF *conf)
431b0cce
RL
665{
666 char *p;
667 STACK_OF(CONF_VALUE) *sktmp;
668 CONF_VALUE *cnf;
669 int i;
b7a26e6d 670 if(!(p=NCONF_get_string(conf,NULL,"oid_section")))
2c0d1012
BM
671 {
672 ERR_clear_error();
673 return 1;
674 }
b7a26e6d 675 if(!(sktmp = NCONF_get_section(conf, p))) {
431b0cce
RL
676 BIO_printf(err, "problem loading oid section %s\n", p);
677 return 0;
678 }
679 for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
680 cnf = sk_CONF_VALUE_value(sktmp, i);
681 if(OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
682 BIO_printf(err, "problem creating object %s=%s\n",
683 cnf->name, cnf->value);
684 return 0;
685 }
686 }
687 return 1;
688}
689
30b4c272
RL
690X509 *load_cert(BIO *err, const char *file, int format,
691 const char *pass, ENGINE *e, const char *cert_descrip)
90ae4673
RL
692 {
693 ASN1_HEADER *ah=NULL;
694 BUF_MEM *buf=NULL;
695 X509 *x=NULL;
696 BIO *cert;
697
698 if ((cert=BIO_new(BIO_s_file())) == NULL)
699 {
431b0cce 700 ERR_print_errors(err);
90ae4673
RL
701 goto end;
702 }
703
704 if (file == NULL)
620cea37
BM
705 {
706 setvbuf(stdin, NULL, _IONBF, 0);
90ae4673 707 BIO_set_fp(cert,stdin,BIO_NOCLOSE);
620cea37 708 }
90ae4673
RL
709 else
710 {
711 if (BIO_read_filename(cert,file) <= 0)
712 {
30b4c272
RL
713 BIO_printf(err, "Error opening %s %s\n",
714 cert_descrip, file);
715 ERR_print_errors(err);
90ae4673
RL
716 goto end;
717 }
718 }
719
720 if (format == FORMAT_ASN1)
721 x=d2i_X509_bio(cert,NULL);
722 else if (format == FORMAT_NETSCAPE)
723 {
724 unsigned char *p,*op;
725 int size=0,i;
726
727 /* We sort of have to do it this way because it is sort of nice
728 * to read the header first and check it, then
729 * try to read the certificate */
730 buf=BUF_MEM_new();
731 for (;;)
732 {
733 if ((buf == NULL) || (!BUF_MEM_grow(buf,size+1024*10)))
734 goto end;
735 i=BIO_read(cert,&(buf->data[size]),1024*10);
736 size+=i;
737 if (i == 0) break;
738 if (i < 0)
739 {
740 perror("reading certificate");
741 goto end;
742 }
743 }
744 p=(unsigned char *)buf->data;
745 op=p;
746
747 /* First load the header */
748 if ((ah=d2i_ASN1_HEADER(NULL,&p,(long)size)) == NULL)
749 goto end;
750 if ((ah->header == NULL) || (ah->header->data == NULL) ||
751 (strncmp(NETSCAPE_CERT_HDR,(char *)ah->header->data,
752 ah->header->length) != 0))
753 {
431b0cce 754 BIO_printf(err,"Error reading header on certificate\n");
90ae4673
RL
755 goto end;
756 }
757 /* header is ok, so now read the object */
758 p=op;
759 ah->meth=X509_asn1_meth();
760 if ((ah=d2i_ASN1_HEADER(&ah,&p,(long)size)) == NULL)
761 goto end;
762 x=(X509 *)ah->data;
763 ah->data=NULL;
764 }
765 else if (format == FORMAT_PEM)
30b4c272
RL
766 x=PEM_read_bio_X509_AUX(cert,NULL,
767 (pem_password_cb *)password_callback, NULL);
90ae4673
RL
768 else if (format == FORMAT_PKCS12)
769 {
770 PKCS12 *p12 = d2i_PKCS12_bio(cert, NULL);
771
772 PKCS12_parse(p12, NULL, NULL, &x, NULL);
773 PKCS12_free(p12);
774 p12 = NULL;
775 }
776 else {
30b4c272
RL
777 BIO_printf(err,"bad input format specified for %s\n",
778 cert_descrip);
90ae4673
RL
779 goto end;
780 }
781end:
782 if (x == NULL)
783 {
431b0cce
RL
784 BIO_printf(err,"unable to load certificate\n");
785 ERR_print_errors(err);
90ae4673
RL
786 }
787 if (ah != NULL) ASN1_HEADER_free(ah);
788 if (cert != NULL) BIO_free(cert);
789 if (buf != NULL) BUF_MEM_free(buf);
790 return(x);
791 }
792
30b4c272
RL
793EVP_PKEY *load_key(BIO *err, const char *file, int format,
794 const char *pass, ENGINE *e, const char *key_descrip)
90ae4673
RL
795 {
796 BIO *key=NULL;
797 EVP_PKEY *pkey=NULL;
30b4c272
RL
798 PW_CB_DATA cb_data;
799
800 cb_data.password = pass;
801 cb_data.prompt_info = file;
90ae4673
RL
802
803 if (file == NULL)
804 {
431b0cce 805 BIO_printf(err,"no keyfile specified\n");
90ae4673
RL
806 goto end;
807 }
32d862ed
RL
808 if (format == FORMAT_ENGINE)
809 {
810 if (!e)
811 BIO_printf(bio_err,"no engine specified\n");
812 else
30b4c272 813 pkey = ENGINE_load_private_key(e, file,
2fe5adc3 814 ui_method, &cb_data);
32d862ed
RL
815 goto end;
816 }
90ae4673
RL
817 key=BIO_new(BIO_s_file());
818 if (key == NULL)
819 {
431b0cce 820 ERR_print_errors(err);
90ae4673
RL
821 goto end;
822 }
823 if (BIO_read_filename(key,file) <= 0)
824 {
30b4c272
RL
825 BIO_printf(err, "Error opening %s %s\n", key_descrip, file);
826 ERR_print_errors(err);
90ae4673
RL
827 goto end;
828 }
829 if (format == FORMAT_ASN1)
830 {
831 pkey=d2i_PrivateKey_bio(key, NULL);
832 }
833 else if (format == FORMAT_PEM)
834 {
30b4c272
RL
835 pkey=PEM_read_bio_PrivateKey(key,NULL,
836 (pem_password_cb *)password_callback, &cb_data);
90ae4673 837 }
80bb905d
RL
838#ifndef OPENSSL_NO_RC4
839 else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
840 pkey = load_netscape_key(err, key, file, key_descrip, format);
841#endif
90ae4673
RL
842 else if (format == FORMAT_PKCS12)
843 {
844 PKCS12 *p12 = d2i_PKCS12_bio(key, NULL);
845
846 PKCS12_parse(p12, pass, &pkey, NULL, NULL);
847 PKCS12_free(p12);
848 p12 = NULL;
849 }
850 else
851 {
30b4c272 852 BIO_printf(err,"bad input format specified for key file\n");
90ae4673
RL
853 goto end;
854 }
855 end:
856 if (key != NULL) BIO_free(key);
857 if (pkey == NULL)
30b4c272 858 BIO_printf(err,"unable to load %s\n", key_descrip);
90ae4673
RL
859 return(pkey);
860 }
861
30b4c272
RL
862EVP_PKEY *load_pubkey(BIO *err, const char *file, int format,
863 const char *pass, ENGINE *e, const char *key_descrip)
bd08a2bd
DSH
864 {
865 BIO *key=NULL;
866 EVP_PKEY *pkey=NULL;
30b4c272
RL
867 PW_CB_DATA cb_data;
868
869 cb_data.password = pass;
870 cb_data.prompt_info = file;
bd08a2bd
DSH
871
872 if (file == NULL)
873 {
874 BIO_printf(err,"no keyfile specified\n");
875 goto end;
876 }
32d862ed
RL
877 if (format == FORMAT_ENGINE)
878 {
879 if (!e)
880 BIO_printf(bio_err,"no engine specified\n");
881 else
30b4c272 882 pkey = ENGINE_load_public_key(e, file,
2fe5adc3 883 ui_method, &cb_data);
32d862ed
RL
884 goto end;
885 }
bd08a2bd
DSH
886 key=BIO_new(BIO_s_file());
887 if (key == NULL)
888 {
889 ERR_print_errors(err);
890 goto end;
891 }
892 if (BIO_read_filename(key,file) <= 0)
893 {
30b4c272
RL
894 BIO_printf(err, "Error opening %s %s\n", key_descrip, file);
895 ERR_print_errors(err);
bd08a2bd
DSH
896 goto end;
897 }
898 if (format == FORMAT_ASN1)
899 {
900 pkey=d2i_PUBKEY_bio(key, NULL);
901 }
902 else if (format == FORMAT_PEM)
903 {
30b4c272
RL
904 pkey=PEM_read_bio_PUBKEY(key,NULL,
905 (pem_password_cb *)password_callback, &cb_data);
bd08a2bd 906 }
80bb905d
RL
907#ifndef OPENSSL_NO_RC4
908 else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
909 pkey = load_netscape_key(err, key, file, key_descrip, format);
910#endif
bd08a2bd
DSH
911 else
912 {
30b4c272 913 BIO_printf(err,"bad input format specified for key file\n");
bd08a2bd
DSH
914 goto end;
915 }
916 end:
917 if (key != NULL) BIO_free(key);
918 if (pkey == NULL)
30b4c272 919 BIO_printf(err,"unable to load %s\n", key_descrip);
bd08a2bd
DSH
920 return(pkey);
921 }
922
80bb905d
RL
923#ifndef OPENSSL_NO_RC4
924EVP_PKEY *
925load_netscape_key(BIO *err, BIO *key, const char *file,
926 const char *key_descrip, int format)
927 {
928 EVP_PKEY *pkey;
929 BUF_MEM *buf;
930 RSA *rsa;
931 const unsigned char *p;
932 int size, i;
933
934 buf=BUF_MEM_new();
935 pkey = EVP_PKEY_new();
936 size = 0;
937 if (buf == NULL || pkey == NULL)
938 goto error;
939 for (;;)
940 {
941 if (!BUF_MEM_grow(buf,size+1024*10))
942 goto error;
943 i = BIO_read(key, &(buf->data[size]), 1024*10);
944 size += i;
945 if (i == 0)
946 break;
947 if (i < 0)
948 {
949 BIO_printf(err, "Error reading %s %s",
950 key_descrip, file);
951 goto error;
952 }
953 }
954 p=(unsigned char *)buf->data;
955 rsa = d2i_RSA_NET(NULL,&p,(long)size,NULL,
956 (format == FORMAT_IISSGC ? 1 : 0));
957 if (rsa == NULL)
958 goto error;
959 BUF_MEM_free(buf);
960 EVP_PKEY_set1_RSA(pkey, rsa);
961 return pkey;
962error:
963 BUF_MEM_free(buf);
964 EVP_PKEY_free(pkey);
965 return NULL;
966 }
967#endif /* ndef OPENSSL_NO_RC4 */
968
30b4c272
RL
969STACK_OF(X509) *load_certs(BIO *err, const char *file, int format,
970 const char *pass, ENGINE *e, const char *cert_descrip)
90ae4673
RL
971 {
972 BIO *certs;
973 int i;
974 STACK_OF(X509) *othercerts = NULL;
975 STACK_OF(X509_INFO) *allcerts = NULL;
976 X509_INFO *xi;
30b4c272
RL
977 PW_CB_DATA cb_data;
978
979 cb_data.password = pass;
980 cb_data.prompt_info = file;
90ae4673
RL
981
982 if((certs = BIO_new(BIO_s_file())) == NULL)
983 {
431b0cce 984 ERR_print_errors(err);
90ae4673
RL
985 goto end;
986 }
987
988 if (file == NULL)
989 BIO_set_fp(certs,stdin,BIO_NOCLOSE);
990 else
991 {
992 if (BIO_read_filename(certs,file) <= 0)
993 {
30b4c272
RL
994 BIO_printf(err, "Error opening %s %s\n",
995 cert_descrip, file);
996 ERR_print_errors(err);
90ae4673
RL
997 goto end;
998 }
999 }
1000
1001 if (format == FORMAT_PEM)
1002 {
62324627 1003 othercerts = sk_X509_new_null();
90ae4673
RL
1004 if(!othercerts)
1005 {
1006 sk_X509_free(othercerts);
1007 othercerts = NULL;
1008 goto end;
1009 }
30b4c272
RL
1010 allcerts = PEM_X509_INFO_read_bio(certs, NULL,
1011 (pem_password_cb *)password_callback, &cb_data);
90ae4673
RL
1012 for(i = 0; i < sk_X509_INFO_num(allcerts); i++)
1013 {
1014 xi = sk_X509_INFO_value (allcerts, i);
1015 if (xi->x509)
1016 {
1017 sk_X509_push(othercerts, xi->x509);
1018 xi->x509 = NULL;
1019 }
1020 }
1021 goto end;
1022 }
1023 else {
30b4c272
RL
1024 BIO_printf(err,"bad input format specified for %s\n",
1025 cert_descrip);
90ae4673
RL
1026 goto end;
1027 }
1028end:
1029 if (othercerts == NULL)
1030 {
431b0cce
RL
1031 BIO_printf(err,"unable to load certificates\n");
1032 ERR_print_errors(err);
90ae4673
RL
1033 }
1034 if (allcerts) sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
1035 if (certs != NULL) BIO_free(certs);
1036 return(othercerts);
1037 }
1038
8ca533e3
DSH
1039
1040#define X509V3_EXT_UNKNOWN_MASK (0xfL << 16)
1041/* Return error for unknown extensions */
1042#define X509V3_EXT_DEFAULT 0
1043/* Print error for unknown extensions */
1044#define X509V3_EXT_ERROR_UNKNOWN (1L << 16)
1045/* ASN1 parse unknown extensions */
1046#define X509V3_EXT_PARSE_UNKNOWN (2L << 16)
1047/* BIO_dump unknown extensions */
1048#define X509V3_EXT_DUMP_UNKNOWN (3L << 16)
1049
535d79da
DSH
1050#define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \
1051 X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION)
1052
8ca533e3
DSH
1053int set_cert_ex(unsigned long *flags, const char *arg)
1054{
1055 static const NAME_EX_TBL cert_tbl[] = {
1056 { "compatible", X509_FLAG_COMPAT, 0xffffffffl},
535d79da 1057 { "ca_default", X509_FLAG_CA, 0xffffffffl},
8ca533e3
DSH
1058 { "no_header", X509_FLAG_NO_HEADER, 0},
1059 { "no_version", X509_FLAG_NO_VERSION, 0},
1060 { "no_serial", X509_FLAG_NO_SERIAL, 0},
1061 { "no_signame", X509_FLAG_NO_SIGNAME, 0},
1062 { "no_validity", X509_FLAG_NO_VALIDITY, 0},
1063 { "no_subject", X509_FLAG_NO_SUBJECT, 0},
0a3ea5d3 1064 { "no_issuer", X509_FLAG_NO_ISSUER, 0},
8ca533e3
DSH
1065 { "no_pubkey", X509_FLAG_NO_PUBKEY, 0},
1066 { "no_extensions", X509_FLAG_NO_EXTENSIONS, 0},
1067 { "no_sigdump", X509_FLAG_NO_SIGDUMP, 0},
1068 { "no_aux", X509_FLAG_NO_AUX, 0},
1069 { "ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK},
1070 { "ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
1071 { "ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
1072 { "ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
1073 { NULL, 0, 0}
1074 };
535d79da 1075 return set_multi_opts(flags, arg, cert_tbl);
8ca533e3 1076}
a657546f
DSH
1077
1078int set_name_ex(unsigned long *flags, const char *arg)
1079{
8ca533e3 1080 static const NAME_EX_TBL ex_tbl[] = {
a657546f
DSH
1081 { "esc_2253", ASN1_STRFLGS_ESC_2253, 0},
1082 { "esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0},
1083 { "esc_msb", ASN1_STRFLGS_ESC_MSB, 0},
1084 { "use_quote", ASN1_STRFLGS_ESC_QUOTE, 0},
1085 { "utf8", ASN1_STRFLGS_UTF8_CONVERT, 0},
bd4e1527
DSH
1086 { "ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0},
1087 { "show_type", ASN1_STRFLGS_SHOW_TYPE, 0},
a657546f
DSH
1088 { "dump_all", ASN1_STRFLGS_DUMP_ALL, 0},
1089 { "dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0},
1090 { "dump_der", ASN1_STRFLGS_DUMP_DER, 0},
1091 { "compat", XN_FLAG_COMPAT, 0xffffffffL},
1092 { "sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK},
1093 { "sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK},
1094 { "sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK},
1095 { "sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK},
1096 { "dn_rev", XN_FLAG_DN_REV, 0},
1097 { "nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK},
1098 { "sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK},
1099 { "lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK},
e890dcdb 1100 { "align", XN_FLAG_FN_ALIGN, 0},
a657546f
DSH
1101 { "oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK},
1102 { "space_eq", XN_FLAG_SPC_EQ, 0},
1103 { "dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0},
1104 { "RFC2253", XN_FLAG_RFC2253, 0xffffffffL},
1105 { "oneline", XN_FLAG_ONELINE, 0xffffffffL},
1106 { "multiline", XN_FLAG_MULTILINE, 0xffffffffL},
535d79da 1107 { "ca_default", XN_FLAG_MULTILINE, 0xffffffffL},
a657546f
DSH
1108 { NULL, 0, 0}
1109 };
535d79da
DSH
1110 return set_multi_opts(flags, arg, ex_tbl);
1111}
1112
791bd0cd
DSH
1113int set_ext_copy(int *copy_type, const char *arg)
1114{
1115 if (!strcasecmp(arg, "none"))
1116 *copy_type = EXT_COPY_NONE;
1117 else if (!strcasecmp(arg, "copy"))
1118 *copy_type = EXT_COPY_ADD;
1119 else if (!strcasecmp(arg, "copyall"))
1120 *copy_type = EXT_COPY_ALL;
1121 else
1122 return 0;
1123 return 1;
1124}
1125
1126int copy_extensions(X509 *x, X509_REQ *req, int copy_type)
1127{
1128 STACK_OF(X509_EXTENSION) *exts = NULL;
1129 X509_EXTENSION *ext, *tmpext;
1130 ASN1_OBJECT *obj;
1131 int i, idx, ret = 0;
1132 if (!x || !req || (copy_type == EXT_COPY_NONE))
1133 return 1;
1134 exts = X509_REQ_get_extensions(req);
1135
1136 for(i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
1137 ext = sk_X509_EXTENSION_value(exts, i);
1138 obj = X509_EXTENSION_get_object(ext);
1139 idx = X509_get_ext_by_OBJ(x, obj, -1);
1140 /* Does extension exist? */
1141 if (idx != -1) {
1142 /* If normal copy don't override existing extension */
1143 if (copy_type == EXT_COPY_ADD)
1144 continue;
1145 /* Delete all extensions of same type */
1146 do {
1147 tmpext = X509_get_ext(x, idx);
1148 X509_delete_ext(x, idx);
1149 X509_EXTENSION_free(tmpext);
1150 idx = X509_get_ext_by_OBJ(x, obj, -1);
1151 } while (idx != -1);
1152 }
1153 if (!X509_add_ext(x, ext, -1))
1154 goto end;
1155 }
1156
1157 ret = 1;
1158
1159 end:
1160
1161 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
1162
1163 return ret;
1164}
1165
1166
1167
1168
535d79da
DSH
1169static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl)
1170{
1171 STACK_OF(CONF_VALUE) *vals;
1172 CONF_VALUE *val;
1173 int i, ret = 1;
1174 if(!arg) return 0;
1175 vals = X509V3_parse_list(arg);
1176 for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
1177 val = sk_CONF_VALUE_value(vals, i);
1178 if (!set_table_opts(flags, val->name, in_tbl))
1179 ret = 0;
1180 }
1181 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
1182 return ret;
8ca533e3 1183}
a657546f 1184
8ca533e3
DSH
1185static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl)
1186{
1187 char c;
1188 const NAME_EX_TBL *ptbl;
a657546f
DSH
1189 c = arg[0];
1190
1191 if(c == '-') {
1192 c = 0;
1193 arg++;
1194 } else if (c == '+') {
1195 c = 1;
1196 arg++;
1197 } else c = 1;
1198
8ca533e3 1199 for(ptbl = in_tbl; ptbl->name; ptbl++) {
c15e0363 1200 if(!strcasecmp(arg, ptbl->name)) {
a657546f
DSH
1201 *flags &= ~ptbl->mask;
1202 if(c) *flags |= ptbl->flag;
1203 else *flags &= ~ptbl->flag;
1204 return 1;
1205 }
1206 }
1207 return 0;
1208}
1209
1210void print_name(BIO *out, char *title, X509_NAME *nm, unsigned long lflags)
1211{
1212 char buf[256];
1213 char mline = 0;
1214 int indent = 0;
1215 if(title) BIO_puts(out, title);
1216 if((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
1217 mline = 1;
1218 indent = 4;
1219 }
1220 if(lflags == XN_FLAG_COMPAT) {
1221 X509_NAME_oneline(nm,buf,256);
1222 BIO_puts(out,buf);
1223 BIO_puts(out, "\n");
1224 } else {
1225 if(mline) BIO_puts(out, "\n");
1226 X509_NAME_print_ex(out, nm, indent, lflags);
1227 BIO_puts(out, "\n");
1228 }
1229}
1230
81f169e9
DSH
1231X509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath)
1232{
1233 X509_STORE *store;
1234 X509_LOOKUP *lookup;
1235 if(!(store = X509_STORE_new())) goto end;
1236 lookup=X509_STORE_add_lookup(store,X509_LOOKUP_file());
1237 if (lookup == NULL) goto end;
1238 if (CAfile) {
1239 if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) {
1240 BIO_printf(bp, "Error loading file %s\n", CAfile);
1241 goto end;
1242 }
1243 } else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
1244
1245 lookup=X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
1246 if (lookup == NULL) goto end;
1247 if (CApath) {
1248 if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) {
1249 BIO_printf(bp, "Error loading directory %s\n", CApath);
1250 goto end;
1251 }
1252 } else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
1253
1254 ERR_clear_error();
1255 return store;
1256 end:
1257 X509_STORE_free(store);
1258 return NULL;
1259}
531d630b 1260
e1a00d7d
RL
1261/* Try to load an engine in a shareable library */
1262ENGINE *try_load_engine(BIO *err, const char *engine, int debug)
1263 {
1264 ENGINE *e = ENGINE_by_id("dynamic");
1265 if (e)
1266 {
1267 if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0)
1268 || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
1269 {
1270 ENGINE_free(e);
1271 e = NULL;
1272 }
1273 }
1274 return e;
1275 }
1276
531d630b
RL
1277ENGINE *setup_engine(BIO *err, const char *engine, int debug)
1278 {
1279 ENGINE *e = NULL;
1280
1281 if (engine)
1282 {
34c66925
GT
1283 if(strcmp(engine, "auto") == 0)
1284 {
1285 BIO_printf(err,"enabling auto ENGINE support\n");
1286 ENGINE_register_all_complete();
1287 return NULL;
1288 }
e1a00d7d
RL
1289 if((e = ENGINE_by_id(engine)) == NULL
1290 && (e = try_load_engine(err, engine, debug)) == NULL)
531d630b
RL
1291 {
1292 BIO_printf(err,"invalid engine \"%s\"\n", engine);
e1a00d7d 1293 ERR_print_errors(err);
531d630b
RL
1294 return NULL;
1295 }
1296 if (debug)
1297 {
1298 ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM,
1299 0, err, 0);
1300 }
2fe5adc3 1301 ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, ui_method, 0, 1);
531d630b
RL
1302 if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
1303 {
1304 BIO_printf(err,"can't use that engine\n");
e1a00d7d 1305 ERR_print_errors(err);
308f028e 1306 ENGINE_free(e);
531d630b
RL
1307 return NULL;
1308 }
354c3ace 1309
e1a00d7d 1310 BIO_printf(err,"engine \"%s\" set.\n", ENGINE_get_id(e));
354c3ace 1311
531d630b
RL
1312 /* Free our "structural" reference. */
1313 ENGINE_free(e);
1314 }
1315 return e;
1316 }