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