]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/apps.c
There's a slight possibility that a is 0 in BN_sub_word(), and might
[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 */
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 77int app_init(long mesgwin);
d02b48c6 78#ifdef undef /* never finished - probably never will be :-) */
6b691a5c 79int 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 157int 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 178void 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
216void 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 236void 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 252int 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 267int 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 333int app_init(long mesgwin)
d02b48c6
RE
334 {
335 return(1);
336 }
337#endif
53b1899e 338
a3fe382e 339
954ef7ef
DSH
340int 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
354static char *app_get_pass(BIO *err, char *arg, int keepbio);
355
356int 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
372static 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
429int 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
451X509 *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 }
534end:
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 546EVP_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
431b0cce 595STACK_OF(X509) *load_certs(BIO *err, char *file, int format)
90ae4673
RL
596 {
597 BIO *certs;
598 int i;
599 STACK_OF(X509) *othercerts = NULL;
600 STACK_OF(X509_INFO) *allcerts = NULL;
601 X509_INFO *xi;
602
603 if((certs = BIO_new(BIO_s_file())) == NULL)
604 {
431b0cce 605 ERR_print_errors(err);
90ae4673
RL
606 goto end;
607 }
608
609 if (file == NULL)
610 BIO_set_fp(certs,stdin,BIO_NOCLOSE);
611 else
612 {
613 if (BIO_read_filename(certs,file) <= 0)
614 {
615 perror(file);
616 goto end;
617 }
618 }
619
620 if (format == FORMAT_PEM)
621 {
622 othercerts = sk_X509_new(NULL);
623 if(!othercerts)
624 {
625 sk_X509_free(othercerts);
626 othercerts = NULL;
627 goto end;
628 }
629 allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
630 for(i = 0; i < sk_X509_INFO_num(allcerts); i++)
631 {
632 xi = sk_X509_INFO_value (allcerts, i);
633 if (xi->x509)
634 {
635 sk_X509_push(othercerts, xi->x509);
636 xi->x509 = NULL;
637 }
638 }
639 goto end;
640 }
641 else {
431b0cce 642 BIO_printf(err,"bad input format specified for input cert\n");
90ae4673
RL
643 goto end;
644 }
645end:
646 if (othercerts == NULL)
647 {
431b0cce
RL
648 BIO_printf(err,"unable to load certificates\n");
649 ERR_print_errors(err);
90ae4673
RL
650 }
651 if (allcerts) sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
652 if (certs != NULL) BIO_free(certs);
653 return(othercerts);
654 }
655