]>
Commit | Line | Data |
---|---|---|
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 | */ | |
58 | ||
59 | #include <stdio.h> | |
60 | #include <stdlib.h> | |
61 | #include <string.h> | |
62 | #include <sys/types.h> | |
63 | #include <sys/stat.h> | |
64 | #define NON_MAIN | |
65 | #include "apps.h" | |
66 | #undef NON_MAIN | |
90ae4673 RL |
67 | #include <openssl/err.h> |
68 | #include <openssl/x509.h> | |
69 | #include <openssl/pem.h> | |
70 | #include <openssl/pkcs12.h> | |
71 | #include <openssl/safestack.h> | |
d02b48c6 | 72 | |
58964a49 RE |
73 | #ifdef WINDOWS |
74 | # include "bss_file.c" | |
d02b48c6 RE |
75 | #endif |
76 | ||
d02b48c6 | 77 | int app_init(long mesgwin); |
d02b48c6 | 78 | #ifdef undef /* never finished - probably never will be :-) */ |
6b691a5c | 79 | int args_from_file(char *file, int *argc, char **argv[]) |
d02b48c6 RE |
80 | { |
81 | FILE *fp; | |
82 | int num,i; | |
83 | unsigned int len; | |
84 | static char *buf=NULL; | |
85 | static char **arg=NULL; | |
86 | char *p; | |
87 | struct stat stbuf; | |
88 | ||
89 | if (stat(file,&stbuf) < 0) return(0); | |
90 | ||
91 | fp=fopen(file,"r"); | |
92 | if (fp == NULL) | |
93 | return(0); | |
94 | ||
95 | *argc=0; | |
96 | *argv=NULL; | |
97 | ||
98 | len=(unsigned int)stbuf.st_size; | |
26a3a48d RL |
99 | if (buf != NULL) OPENSSL_free(buf); |
100 | buf=(char *)OPENSSL_malloc(len+1); | |
d02b48c6 RE |
101 | if (buf == NULL) return(0); |
102 | ||
103 | len=fread(buf,1,len,fp); | |
104 | if (len <= 1) return(0); | |
105 | buf[len]='\0'; | |
106 | ||
107 | i=0; | |
108 | for (p=buf; *p; p++) | |
109 | if (*p == '\n') i++; | |
26a3a48d RL |
110 | if (arg != NULL) OPENSSL_free(arg); |
111 | arg=(char **)OPENSSL_malloc(sizeof(char *)*(i*2)); | |
d02b48c6 RE |
112 | |
113 | *argv=arg; | |
114 | num=0; | |
115 | p=buf; | |
116 | for (;;) | |
117 | { | |
118 | if (!*p) break; | |
119 | if (*p == '#') /* comment line */ | |
120 | { | |
121 | while (*p && (*p != '\n')) p++; | |
122 | continue; | |
123 | } | |
124 | /* else we have a line */ | |
125 | *(arg++)=p; | |
126 | num++; | |
127 | while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n'))) | |
128 | p++; | |
129 | if (!*p) break; | |
130 | if (*p == '\n') | |
131 | { | |
132 | *(p++)='\0'; | |
133 | continue; | |
134 | } | |
135 | /* else it is a tab or space */ | |
136 | p++; | |
137 | while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n'))) | |
138 | p++; | |
139 | if (!*p) break; | |
140 | if (*p == '\n') | |
141 | { | |
142 | p++; | |
143 | continue; | |
144 | } | |
145 | *(arg++)=p++; | |
146 | num++; | |
147 | while (*p && (*p != '\n')) p++; | |
148 | if (!*p) break; | |
149 | /* else *p == '\n' */ | |
150 | *(p++)='\0'; | |
151 | } | |
152 | *argc=num; | |
153 | return(1); | |
154 | } | |
155 | #endif | |
156 | ||
6b691a5c | 157 | int str2fmt(char *s) |
d02b48c6 RE |
158 | { |
159 | if ((*s == 'D') || (*s == 'd')) | |
160 | return(FORMAT_ASN1); | |
161 | else if ((*s == 'T') || (*s == 't')) | |
162 | return(FORMAT_TEXT); | |
163 | else if ((*s == 'P') || (*s == 'p')) | |
164 | return(FORMAT_PEM); | |
165 | else if ((*s == 'N') || (*s == 'n')) | |
166 | return(FORMAT_NETSCAPE); | |
094fe66d DSH |
167 | else if ((*s == 'S') || (*s == 's')) |
168 | return(FORMAT_SMIME); | |
90ae4673 RL |
169 | else if ((*s == '1') |
170 | || (strcmp(s,"PKCS12") == 0) || (strcmp(s,"pkcs12") == 0) | |
171 | || (strcmp(s,"P12") == 0) || (strcmp(s,"p12") == 0)) | |
172 | return(FORMAT_PKCS12); | |
d02b48c6 RE |
173 | else |
174 | return(FORMAT_UNDEF); | |
175 | } | |
176 | ||
177 | #if defined(MSDOS) || defined(WIN32) || defined(WIN16) | |
6b691a5c | 178 | void program_name(char *in, char *out, int size) |
d02b48c6 RE |
179 | { |
180 | int i,n; | |
181 | char *p=NULL; | |
182 | ||
183 | n=strlen(in); | |
184 | /* find the last '/', '\' or ':' */ | |
185 | for (i=n-1; i>0; i--) | |
186 | { | |
187 | if ((in[i] == '/') || (in[i] == '\\') || (in[i] == ':')) | |
188 | { | |
189 | p= &(in[i+1]); | |
190 | break; | |
191 | } | |
192 | } | |
193 | if (p == NULL) | |
194 | p=in; | |
195 | n=strlen(p); | |
196 | /* strip off trailing .exe if present. */ | |
197 | if ((n > 4) && (p[n-4] == '.') && | |
198 | ((p[n-3] == 'e') || (p[n-3] == 'E')) && | |
199 | ((p[n-2] == 'x') || (p[n-2] == 'X')) && | |
200 | ((p[n-1] == 'e') || (p[n-1] == 'E'))) | |
201 | n-=4; | |
202 | if (n > size-1) | |
203 | n=size-1; | |
204 | ||
205 | for (i=0; i<n; i++) | |
206 | { | |
207 | if ((p[i] >= 'A') && (p[i] <= 'Z')) | |
208 | out[i]=p[i]-'A'+'a'; | |
209 | else | |
210 | out[i]=p[i]; | |
211 | } | |
212 | out[n]='\0'; | |
213 | } | |
214 | #else | |
7d7d2cbc UM |
215 | #ifdef VMS |
216 | void program_name(char *in, char *out, int size) | |
217 | { | |
218 | char *p=in, *q; | |
219 | char *chars=":]>"; | |
220 | ||
221 | while(*chars != '\0') | |
222 | { | |
223 | q=strrchr(p,*chars); | |
224 | if (q > p) | |
225 | p = q + 1; | |
226 | chars++; | |
227 | } | |
228 | ||
229 | q=strrchr(p,'.'); | |
230 | if (q == NULL) | |
231 | q = in+size; | |
232 | strncpy(out,p,q-p); | |
233 | out[q-p]='\0'; | |
234 | } | |
235 | #else | |
6b691a5c | 236 | void program_name(char *in, char *out, int size) |
d02b48c6 RE |
237 | { |
238 | char *p; | |
239 | ||
240 | p=strrchr(in,'/'); | |
241 | if (p != NULL) | |
242 | p++; | |
243 | else | |
244 | p=in; | |
245 | strncpy(out,p,size-1); | |
246 | out[size-1]='\0'; | |
247 | } | |
248 | #endif | |
7d7d2cbc | 249 | #endif |
d02b48c6 RE |
250 | |
251 | #ifdef WIN32 | |
6b691a5c | 252 | int WIN32_rename(char *from, char *to) |
d02b48c6 | 253 | { |
a5ab0532 | 254 | #ifdef WINNT |
d02b48c6 | 255 | int ret; |
a5ab0532 | 256 | /* Note: MoveFileEx() doesn't work under Win95, Win98 */ |
d02b48c6 RE |
257 | |
258 | ret=MoveFileEx(from,to,MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED); | |
259 | return(ret?0:-1); | |
a5ab0532 | 260 | #else |
b1c4fe36 BM |
261 | unlink(to); |
262 | return MoveFile(from, to); | |
a5ab0532 | 263 | #endif |
d02b48c6 RE |
264 | } |
265 | #endif | |
266 | ||
6b691a5c | 267 | int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[]) |
d02b48c6 RE |
268 | { |
269 | int num,len,i; | |
270 | char *p; | |
271 | ||
272 | *argc=0; | |
273 | *argv=NULL; | |
274 | ||
275 | len=strlen(buf); | |
276 | i=0; | |
277 | if (arg->count == 0) | |
278 | { | |
279 | arg->count=20; | |
26a3a48d | 280 | arg->data=(char **)OPENSSL_malloc(sizeof(char *)*arg->count); |
d02b48c6 RE |
281 | } |
282 | for (i=0; i<arg->count; i++) | |
283 | arg->data[i]=NULL; | |
284 | ||
285 | num=0; | |
286 | p=buf; | |
287 | for (;;) | |
288 | { | |
289 | /* first scan over white space */ | |
290 | if (!*p) break; | |
291 | while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n'))) | |
292 | p++; | |
293 | if (!*p) break; | |
294 | ||
295 | /* The start of something good :-) */ | |
296 | if (num >= arg->count) | |
297 | { | |
298 | arg->count+=20; | |
26a3a48d | 299 | arg->data=(char **)OPENSSL_realloc(arg->data, |
d02b48c6 RE |
300 | sizeof(char *)*arg->count); |
301 | if (argc == 0) return(0); | |
302 | } | |
303 | arg->data[num++]=p; | |
304 | ||
305 | /* now look for the end of this */ | |
306 | if ((*p == '\'') || (*p == '\"')) /* scan for closing quote */ | |
307 | { | |
308 | i= *(p++); | |
309 | arg->data[num-1]++; /* jump over quote */ | |
310 | while (*p && (*p != i)) | |
311 | p++; | |
312 | *p='\0'; | |
313 | } | |
314 | else | |
315 | { | |
316 | while (*p && ((*p != ' ') && | |
317 | (*p != '\t') && (*p != '\n'))) | |
318 | p++; | |
319 | ||
320 | if (*p == '\0') | |
321 | p--; | |
322 | else | |
323 | *p='\0'; | |
324 | } | |
325 | p++; | |
326 | } | |
327 | *argc=num; | |
328 | *argv=arg->data; | |
329 | return(1); | |
330 | } | |
331 | ||
332 | #ifndef APP_INIT | |
6b691a5c | 333 | int app_init(long mesgwin) |
d02b48c6 RE |
334 | { |
335 | return(1); | |
336 | } | |
337 | #endif | |
53b1899e | 338 | |
a3fe382e | 339 | |
954ef7ef DSH |
340 | int dump_cert_text (BIO *out, X509 *x) |
341 | { | |
342 | char buf[256]; | |
343 | X509_NAME_oneline(X509_get_subject_name(x),buf,256); | |
344 | BIO_puts(out,"subject="); | |
345 | BIO_puts(out,buf); | |
346 | ||
347 | X509_NAME_oneline(X509_get_issuer_name(x),buf,256); | |
348 | BIO_puts(out,"\nissuer= "); | |
349 | BIO_puts(out,buf); | |
350 | BIO_puts(out,"\n"); | |
351 | return 0; | |
352 | } | |
a3fe382e DSH |
353 | |
354 | static char *app_get_pass(BIO *err, char *arg, int keepbio); | |
355 | ||
356 | int app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2) | |
357 | { | |
358 | int same; | |
359 | if(!arg2 || !arg1 || strcmp(arg1, arg2)) same = 0; | |
360 | else same = 1; | |
361 | if(arg1) { | |
362 | *pass1 = app_get_pass(err, arg1, same); | |
363 | if(!*pass1) return 0; | |
364 | } else if(pass1) *pass1 = NULL; | |
365 | if(arg2) { | |
366 | *pass2 = app_get_pass(err, arg2, same ? 2 : 0); | |
367 | if(!*pass2) return 0; | |
368 | } else if(pass2) *pass2 = NULL; | |
369 | return 1; | |
370 | } | |
371 | ||
372 | static char *app_get_pass(BIO *err, char *arg, int keepbio) | |
373 | { | |
374 | char *tmp, tpass[APP_PASS_LEN]; | |
375 | static BIO *pwdbio = NULL; | |
376 | int i; | |
377 | if(!strncmp(arg, "pass:", 5)) return BUF_strdup(arg + 5); | |
378 | if(!strncmp(arg, "env:", 4)) { | |
379 | tmp = getenv(arg + 4); | |
380 | if(!tmp) { | |
381 | BIO_printf(err, "Can't read environment variable %s\n", arg + 4); | |
382 | return NULL; | |
383 | } | |
384 | return BUF_strdup(tmp); | |
385 | } | |
386 | if(!keepbio || !pwdbio) { | |
387 | if(!strncmp(arg, "file:", 5)) { | |
388 | pwdbio = BIO_new_file(arg + 5, "r"); | |
389 | if(!pwdbio) { | |
390 | BIO_printf(err, "Can't open file %s\n", arg + 5); | |
391 | return NULL; | |
392 | } | |
393 | } else if(!strncmp(arg, "fd:", 3)) { | |
394 | BIO *btmp; | |
395 | i = atoi(arg + 3); | |
396 | if(i >= 0) pwdbio = BIO_new_fd(i, BIO_NOCLOSE); | |
397 | if((i < 0) || !pwdbio) { | |
398 | BIO_printf(err, "Can't access file descriptor %s\n", arg + 3); | |
399 | return NULL; | |
400 | } | |
401 | /* Can't do BIO_gets on an fd BIO so add a buffering BIO */ | |
402 | btmp = BIO_new(BIO_f_buffer()); | |
403 | pwdbio = BIO_push(btmp, pwdbio); | |
404 | } else if(!strcmp(arg, "stdin")) { | |
405 | pwdbio = BIO_new_fp(stdin, BIO_NOCLOSE); | |
406 | if(!pwdbio) { | |
407 | BIO_printf(err, "Can't open BIO for stdin\n"); | |
408 | return NULL; | |
409 | } | |
410 | } else { | |
411 | BIO_printf(err, "Invalid password argument \"%s\"\n", arg); | |
412 | return NULL; | |
413 | } | |
414 | } | |
415 | i = BIO_gets(pwdbio, tpass, APP_PASS_LEN); | |
416 | if(keepbio != 1) { | |
417 | BIO_free_all(pwdbio); | |
418 | pwdbio = NULL; | |
419 | } | |
420 | if(i <= 0) { | |
421 | BIO_printf(err, "Error reading password from BIO\n"); | |
422 | return NULL; | |
423 | } | |
424 | tmp = strchr(tpass, '\n'); | |
425 | if(tmp) *tmp = 0; | |
426 | return BUF_strdup(tpass); | |
427 | } | |
90ae4673 | 428 | |
431b0cce RL |
429 | int add_oid_section(BIO *err, LHASH *conf) |
430 | { | |
431 | char *p; | |
432 | STACK_OF(CONF_VALUE) *sktmp; | |
433 | CONF_VALUE *cnf; | |
434 | int i; | |
435 | if(!(p=CONF_get_string(conf,NULL,"oid_section"))) return 1; | |
436 | if(!(sktmp = CONF_get_section(conf, p))) { | |
437 | BIO_printf(err, "problem loading oid section %s\n", p); | |
438 | return 0; | |
439 | } | |
440 | for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++) { | |
441 | cnf = sk_CONF_VALUE_value(sktmp, i); | |
442 | if(OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) { | |
443 | BIO_printf(err, "problem creating object %s=%s\n", | |
444 | cnf->name, cnf->value); | |
445 | return 0; | |
446 | } | |
447 | } | |
448 | return 1; | |
449 | } | |
450 | ||
451 | X509 *load_cert(BIO *err, char *file, int format) | |
90ae4673 RL |
452 | { |
453 | ASN1_HEADER *ah=NULL; | |
454 | BUF_MEM *buf=NULL; | |
455 | X509 *x=NULL; | |
456 | BIO *cert; | |
457 | ||
458 | if ((cert=BIO_new(BIO_s_file())) == NULL) | |
459 | { | |
431b0cce | 460 | ERR_print_errors(err); |
90ae4673 RL |
461 | goto end; |
462 | } | |
463 | ||
464 | if (file == NULL) | |
465 | BIO_set_fp(cert,stdin,BIO_NOCLOSE); | |
466 | else | |
467 | { | |
468 | if (BIO_read_filename(cert,file) <= 0) | |
469 | { | |
470 | perror(file); | |
471 | goto end; | |
472 | } | |
473 | } | |
474 | ||
475 | if (format == FORMAT_ASN1) | |
476 | x=d2i_X509_bio(cert,NULL); | |
477 | else if (format == FORMAT_NETSCAPE) | |
478 | { | |
479 | unsigned char *p,*op; | |
480 | int size=0,i; | |
481 | ||
482 | /* We sort of have to do it this way because it is sort of nice | |
483 | * to read the header first and check it, then | |
484 | * try to read the certificate */ | |
485 | buf=BUF_MEM_new(); | |
486 | for (;;) | |
487 | { | |
488 | if ((buf == NULL) || (!BUF_MEM_grow(buf,size+1024*10))) | |
489 | goto end; | |
490 | i=BIO_read(cert,&(buf->data[size]),1024*10); | |
491 | size+=i; | |
492 | if (i == 0) break; | |
493 | if (i < 0) | |
494 | { | |
495 | perror("reading certificate"); | |
496 | goto end; | |
497 | } | |
498 | } | |
499 | p=(unsigned char *)buf->data; | |
500 | op=p; | |
501 | ||
502 | /* First load the header */ | |
503 | if ((ah=d2i_ASN1_HEADER(NULL,&p,(long)size)) == NULL) | |
504 | goto end; | |
505 | if ((ah->header == NULL) || (ah->header->data == NULL) || | |
506 | (strncmp(NETSCAPE_CERT_HDR,(char *)ah->header->data, | |
507 | ah->header->length) != 0)) | |
508 | { | |
431b0cce | 509 | BIO_printf(err,"Error reading header on certificate\n"); |
90ae4673 RL |
510 | goto end; |
511 | } | |
512 | /* header is ok, so now read the object */ | |
513 | p=op; | |
514 | ah->meth=X509_asn1_meth(); | |
515 | if ((ah=d2i_ASN1_HEADER(&ah,&p,(long)size)) == NULL) | |
516 | goto end; | |
517 | x=(X509 *)ah->data; | |
518 | ah->data=NULL; | |
519 | } | |
520 | else if (format == FORMAT_PEM) | |
521 | x=PEM_read_bio_X509_AUX(cert,NULL,NULL,NULL); | |
522 | else if (format == FORMAT_PKCS12) | |
523 | { | |
524 | PKCS12 *p12 = d2i_PKCS12_bio(cert, NULL); | |
525 | ||
526 | PKCS12_parse(p12, NULL, NULL, &x, NULL); | |
527 | PKCS12_free(p12); | |
528 | p12 = NULL; | |
529 | } | |
530 | else { | |
431b0cce | 531 | BIO_printf(err,"bad input format specified for input cert\n"); |
90ae4673 RL |
532 | goto end; |
533 | } | |
534 | end: | |
535 | if (x == NULL) | |
536 | { | |
431b0cce RL |
537 | BIO_printf(err,"unable to load certificate\n"); |
538 | ERR_print_errors(err); | |
90ae4673 RL |
539 | } |
540 | if (ah != NULL) ASN1_HEADER_free(ah); | |
541 | if (cert != NULL) BIO_free(cert); | |
542 | if (buf != NULL) BUF_MEM_free(buf); | |
543 | return(x); | |
544 | } | |
545 | ||
431b0cce | 546 | EVP_PKEY *load_key(BIO *err, char *file, int format, char *pass) |
90ae4673 RL |
547 | { |
548 | BIO *key=NULL; | |
549 | EVP_PKEY *pkey=NULL; | |
550 | ||
551 | if (file == NULL) | |
552 | { | |
431b0cce | 553 | BIO_printf(err,"no keyfile specified\n"); |
90ae4673 RL |
554 | goto end; |
555 | } | |
556 | key=BIO_new(BIO_s_file()); | |
557 | if (key == NULL) | |
558 | { | |
431b0cce | 559 | ERR_print_errors(err); |
90ae4673 RL |
560 | goto end; |
561 | } | |
562 | if (BIO_read_filename(key,file) <= 0) | |
563 | { | |
564 | perror(file); | |
565 | goto end; | |
566 | } | |
567 | if (format == FORMAT_ASN1) | |
568 | { | |
569 | pkey=d2i_PrivateKey_bio(key, NULL); | |
570 | } | |
571 | else if (format == FORMAT_PEM) | |
572 | { | |
573 | pkey=PEM_read_bio_PrivateKey(key,NULL,NULL,pass); | |
574 | } | |
575 | else if (format == FORMAT_PKCS12) | |
576 | { | |
577 | PKCS12 *p12 = d2i_PKCS12_bio(key, NULL); | |
578 | ||
579 | PKCS12_parse(p12, pass, &pkey, NULL, NULL); | |
580 | PKCS12_free(p12); | |
581 | p12 = NULL; | |
582 | } | |
583 | else | |
584 | { | |
431b0cce | 585 | BIO_printf(err,"bad input format specified for key\n"); |
90ae4673 RL |
586 | goto end; |
587 | } | |
588 | end: | |
589 | if (key != NULL) BIO_free(key); | |
590 | if (pkey == NULL) | |
431b0cce | 591 | BIO_printf(err,"unable to load Private Key\n"); |
90ae4673 RL |
592 | return(pkey); |
593 | } | |
594 | ||
bd08a2bd DSH |
595 | EVP_PKEY *load_pubkey(BIO *err, char *file, int format) |
596 | { | |
597 | BIO *key=NULL; | |
598 | EVP_PKEY *pkey=NULL; | |
599 | ||
600 | if (file == NULL) | |
601 | { | |
602 | BIO_printf(err,"no keyfile specified\n"); | |
603 | goto end; | |
604 | } | |
605 | key=BIO_new(BIO_s_file()); | |
606 | if (key == NULL) | |
607 | { | |
608 | ERR_print_errors(err); | |
609 | goto end; | |
610 | } | |
611 | if (BIO_read_filename(key,file) <= 0) | |
612 | { | |
613 | perror(file); | |
614 | goto end; | |
615 | } | |
616 | if (format == FORMAT_ASN1) | |
617 | { | |
618 | pkey=d2i_PUBKEY_bio(key, NULL); | |
619 | } | |
620 | else if (format == FORMAT_PEM) | |
621 | { | |
622 | pkey=PEM_read_bio_PUBKEY(key,NULL,NULL,NULL); | |
623 | } | |
624 | else | |
625 | { | |
626 | BIO_printf(err,"bad input format specified for key\n"); | |
627 | goto end; | |
628 | } | |
629 | end: | |
630 | if (key != NULL) BIO_free(key); | |
631 | if (pkey == NULL) | |
632 | BIO_printf(err,"unable to load Public Key\n"); | |
633 | return(pkey); | |
634 | } | |
635 | ||
431b0cce | 636 | STACK_OF(X509) *load_certs(BIO *err, char *file, int format) |
90ae4673 RL |
637 | { |
638 | BIO *certs; | |
639 | int i; | |
640 | STACK_OF(X509) *othercerts = NULL; | |
641 | STACK_OF(X509_INFO) *allcerts = NULL; | |
642 | X509_INFO *xi; | |
643 | ||
644 | if((certs = BIO_new(BIO_s_file())) == NULL) | |
645 | { | |
431b0cce | 646 | ERR_print_errors(err); |
90ae4673 RL |
647 | goto end; |
648 | } | |
649 | ||
650 | if (file == NULL) | |
651 | BIO_set_fp(certs,stdin,BIO_NOCLOSE); | |
652 | else | |
653 | { | |
654 | if (BIO_read_filename(certs,file) <= 0) | |
655 | { | |
656 | perror(file); | |
657 | goto end; | |
658 | } | |
659 | } | |
660 | ||
661 | if (format == FORMAT_PEM) | |
662 | { | |
663 | othercerts = sk_X509_new(NULL); | |
664 | if(!othercerts) | |
665 | { | |
666 | sk_X509_free(othercerts); | |
667 | othercerts = NULL; | |
668 | goto end; | |
669 | } | |
670 | allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL); | |
671 | for(i = 0; i < sk_X509_INFO_num(allcerts); i++) | |
672 | { | |
673 | xi = sk_X509_INFO_value (allcerts, i); | |
674 | if (xi->x509) | |
675 | { | |
676 | sk_X509_push(othercerts, xi->x509); | |
677 | xi->x509 = NULL; | |
678 | } | |
679 | } | |
680 | goto end; | |
681 | } | |
682 | else { | |
431b0cce | 683 | BIO_printf(err,"bad input format specified for input cert\n"); |
90ae4673 RL |
684 | goto end; |
685 | } | |
686 | end: | |
687 | if (othercerts == NULL) | |
688 | { | |
431b0cce RL |
689 | BIO_printf(err,"unable to load certificates\n"); |
690 | ERR_print_errors(err); | |
90ae4673 RL |
691 | } |
692 | if (allcerts) sk_X509_INFO_pop_free(allcerts, X509_INFO_free); | |
693 | if (certs != NULL) BIO_free(certs); | |
694 | return(othercerts); | |
695 | } | |
696 | ||
a657546f DSH |
697 | typedef struct { |
698 | char *name; | |
699 | unsigned long flag; | |
700 | unsigned long mask; | |
701 | } NAME_EX_TBL; | |
702 | ||
703 | int set_name_ex(unsigned long *flags, const char *arg) | |
704 | { | |
705 | char c; | |
706 | const NAME_EX_TBL *ptbl, ex_tbl[] = { | |
707 | { "esc_2253", ASN1_STRFLGS_ESC_2253, 0}, | |
708 | { "esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0}, | |
709 | { "esc_msb", ASN1_STRFLGS_ESC_MSB, 0}, | |
710 | { "use_quote", ASN1_STRFLGS_ESC_QUOTE, 0}, | |
711 | { "utf8", ASN1_STRFLGS_UTF8_CONVERT, 0}, | |
bd4e1527 DSH |
712 | { "ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0}, |
713 | { "show_type", ASN1_STRFLGS_SHOW_TYPE, 0}, | |
a657546f DSH |
714 | { "dump_all", ASN1_STRFLGS_DUMP_ALL, 0}, |
715 | { "dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0}, | |
716 | { "dump_der", ASN1_STRFLGS_DUMP_DER, 0}, | |
717 | { "compat", XN_FLAG_COMPAT, 0xffffffffL}, | |
718 | { "sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK}, | |
719 | { "sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK}, | |
720 | { "sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK}, | |
721 | { "sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK}, | |
722 | { "dn_rev", XN_FLAG_DN_REV, 0}, | |
723 | { "nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK}, | |
724 | { "sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK}, | |
725 | { "lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK}, | |
726 | { "oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK}, | |
727 | { "space_eq", XN_FLAG_SPC_EQ, 0}, | |
728 | { "dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0}, | |
729 | { "RFC2253", XN_FLAG_RFC2253, 0xffffffffL}, | |
730 | { "oneline", XN_FLAG_ONELINE, 0xffffffffL}, | |
731 | { "multiline", XN_FLAG_MULTILINE, 0xffffffffL}, | |
732 | { NULL, 0, 0} | |
733 | }; | |
734 | ||
735 | c = arg[0]; | |
736 | ||
737 | if(c == '-') { | |
738 | c = 0; | |
739 | arg++; | |
740 | } else if (c == '+') { | |
741 | c = 1; | |
742 | arg++; | |
743 | } else c = 1; | |
744 | ||
745 | for(ptbl = ex_tbl; ptbl->name; ptbl++) { | |
746 | if(!strcmp(arg, ptbl->name)) { | |
747 | *flags &= ~ptbl->mask; | |
748 | if(c) *flags |= ptbl->flag; | |
749 | else *flags &= ~ptbl->flag; | |
750 | return 1; | |
751 | } | |
752 | } | |
753 | return 0; | |
754 | } | |
755 | ||
756 | void print_name(BIO *out, char *title, X509_NAME *nm, unsigned long lflags) | |
757 | { | |
758 | char buf[256]; | |
759 | char mline = 0; | |
760 | int indent = 0; | |
761 | if(title) BIO_puts(out, title); | |
762 | if((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) { | |
763 | mline = 1; | |
764 | indent = 4; | |
765 | } | |
766 | if(lflags == XN_FLAG_COMPAT) { | |
767 | X509_NAME_oneline(nm,buf,256); | |
768 | BIO_puts(out,buf); | |
769 | BIO_puts(out, "\n"); | |
770 | } else { | |
771 | if(mline) BIO_puts(out, "\n"); | |
772 | X509_NAME_print_ex(out, nm, indent, lflags); | |
773 | BIO_puts(out, "\n"); | |
774 | } | |
775 | } | |
776 |