1 /* $OpenBSD: apps.c,v 1.47 2018/02/07 08:57:25 jsing Exp $ */
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
18 * All rights reserved.
20 * This package is an SSL implementation written
21 * by Eric Young (eay@cryptsoft.com).
22 * The implementation was written so as to conform with Netscapes SSL.
24 * This library is free for commercial and non-commercial use as long as
25 * the following conditions are aheared to. The following conditions
26 * apply to all code found in this distribution, be it the RC4, RSA,
27 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
28 * included with this distribution is covered by the same copyright terms
29 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
31 * Copyright remains Eric Young's, and as such any Copyright notices in
32 * the code are not to be removed.
33 * If this package is used in a product, Eric Young should be given attribution
34 * as the author of the parts of the library used.
35 * This can be in the form of a textual message at program startup or
36 * in documentation (online or textual) provided with the package.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * "This product includes cryptographic software written by
49 * Eric Young (eay@cryptsoft.com)"
50 * The word 'cryptographic' can be left out if the rouines from the library
51 * being used are not cryptographic related :-).
52 * 4. If you include any Windows specific code (or a derivative thereof) from
53 * the apps directory (application code) you must include an acknowledgement:
54 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
56 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * The licence and distribution terms for any publically available version or
69 * derivative of this code cannot be changed. i.e. this code cannot simply be
70 * copied and put under another distribution licence
71 * [including the GNU Public Licence.]
73 /* ====================================================================
74 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
76 * Redistribution and use in source and binary forms, with or without
77 * modification, are permitted provided that the following conditions
80 * 1. Redistributions of source code must retain the above copyright
81 * notice, this list of conditions and the following disclaimer.
83 * 2. Redistributions in binary form must reproduce the above copyright
84 * notice, this list of conditions and the following disclaimer in
85 * the documentation and/or other materials provided with the
88 * 3. All advertising materials mentioning features or use of this
89 * software must display the following acknowledgment:
90 * "This product includes software developed by the OpenSSL Project
91 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
93 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
94 * endorse or promote products derived from this software without
95 * prior written permission. For written permission, please contact
96 * openssl-core@openssl.org.
98 * 5. Products derived from this software may not be called "OpenSSL"
99 * nor may "OpenSSL" appear in their names without prior written
100 * permission of the OpenSSL Project.
102 * 6. Redistributions of any form whatsoever must retain the following
104 * "This product includes software developed by the OpenSSL Project
105 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
107 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
108 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
109 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
110 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
111 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
112 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
113 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
114 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
115 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
116 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
117 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
118 * OF THE POSSIBILITY OF SUCH DAMAGE.
119 * ====================================================================
121 * This product includes cryptographic software written by Eric Young
122 * (eay@cryptsoft.com). This product includes software written by Tim
123 * Hudson (tjh@cryptsoft.com).
127 #include <sys/types.h>
128 #include <sys/stat.h>
140 #include <openssl/bn.h>
141 #include <openssl/err.h>
142 #include <openssl/pem.h>
143 #include <openssl/pkcs12.h>
144 #include <openssl/safestack.h>
145 #include <openssl/x509.h>
146 #include <openssl/x509v3.h>
148 #include <openssl/rsa.h>
156 UI_METHOD
*ui_method
= NULL
;
158 static int set_table_opts(unsigned long *flags
, const char *arg
,
159 const NAME_EX_TBL
*in_tbl
);
160 static int set_multi_opts(unsigned long *flags
, const char *arg
,
161 const NAME_EX_TBL
*in_tbl
);
163 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
164 /* Looks like this stuff is worth moving into separate function */
165 static EVP_PKEY
*load_netscape_key(BIO
*err
, BIO
*key
, const char *file
,
166 const char *key_descrip
, int format
);
174 if ((*s
== 'D') || (*s
== 'd'))
175 return (FORMAT_ASN1
);
176 else if ((*s
== 'T') || (*s
== 't'))
177 return (FORMAT_TEXT
);
178 else if ((*s
== 'N') || (*s
== 'n'))
179 return (FORMAT_NETSCAPE
);
180 else if ((*s
== 'S') || (*s
== 's'))
181 return (FORMAT_SMIME
);
182 else if ((*s
== 'M') || (*s
== 'm'))
183 return (FORMAT_MSBLOB
);
184 else if ((*s
== '1') ||
185 (strcmp(s
, "PKCS12") == 0) || (strcmp(s
, "pkcs12") == 0) ||
186 (strcmp(s
, "P12") == 0) || (strcmp(s
, "p12") == 0))
187 return (FORMAT_PKCS12
);
188 else if ((*s
== 'P') || (*s
== 'p')) {
189 if (s
[1] == 'V' || s
[1] == 'v')
194 return (FORMAT_UNDEF
);
198 program_name(char *in
, char *out
, int size
)
202 p
= strrchr(in
, '/');
207 strlcpy(out
, p
, size
);
211 chopup_args(ARGS
*arg
, char *buf
, int *argc
, char **argv
[])
220 if (arg
->count
== 0) {
222 arg
->data
= reallocarray(NULL
, arg
->count
, sizeof(char *));
223 if (arg
->data
== NULL
)
226 for (i
= 0; i
< arg
->count
; i
++)
232 /* first scan over white space */
235 while (*p
&& ((*p
== ' ') || (*p
== '\t') || (*p
== '\n')))
240 /* The start of something good :-) */
241 if (num
>= arg
->count
) {
243 int tlen
= arg
->count
+ 20;
244 tmp_p
= reallocarray(arg
->data
, tlen
, sizeof(char *));
249 /* initialize newly allocated data */
250 for (i
= num
; i
< arg
->count
; i
++)
253 arg
->data
[num
++] = p
;
255 /* now look for the end of this */
256 if ((*p
== '\'') || (*p
== '\"')) { /* scan for closing
259 arg
->data
[num
- 1]++; /* jump over quote */
260 while (*p
&& (*p
!= i
))
264 while (*p
&& ((*p
!= ' ') &&
265 (*p
!= '\t') && (*p
!= '\n')))
281 dump_cert_text(BIO
*out
, X509
*x
)
285 p
= X509_NAME_oneline(X509_get_subject_name(x
), NULL
, 0);
286 BIO_puts(out
, "subject=");
290 p
= X509_NAME_oneline(X509_get_issuer_name(x
), NULL
, 0);
291 BIO_puts(out
, "\nissuer=");
302 return UI_method_get_opener(UI_OpenSSL()) (ui
);
306 ui_read(UI
*ui
, UI_STRING
*uis
)
308 const char *password
;
311 if (UI_get_input_flags(uis
) & UI_INPUT_FLAG_DEFAULT_PWD
&&
312 UI_get0_user_data(ui
)) {
313 string_type
= UI_get_string_type(uis
);
314 if (string_type
== UIT_PROMPT
|| string_type
== UIT_VERIFY
) {
316 ((PW_CB_DATA
*)UI_get0_user_data(ui
))->password
;
317 if (password
&& password
[0] != '\0') {
318 UI_set_result(ui
, uis
, password
);
323 return UI_method_get_reader(UI_OpenSSL()) (ui
, uis
);
327 ui_write(UI
*ui
, UI_STRING
*uis
)
329 const char *password
;
332 if (UI_get_input_flags(uis
) & UI_INPUT_FLAG_DEFAULT_PWD
&&
333 UI_get0_user_data(ui
)) {
334 string_type
= UI_get_string_type(uis
);
335 if (string_type
== UIT_PROMPT
|| string_type
== UIT_VERIFY
) {
337 ((PW_CB_DATA
*)UI_get0_user_data(ui
))->password
;
338 if (password
&& password
[0] != '\0')
342 return UI_method_get_writer(UI_OpenSSL()) (ui
, uis
);
348 return UI_method_get_closer(UI_OpenSSL()) (ui
);
352 password_callback(char *buf
, int bufsiz
, int verify
, void *arg
)
354 PW_CB_DATA
*cb_tmp
= arg
;
357 const char *prompt_info
= NULL
;
358 const char *password
= NULL
;
359 PW_CB_DATA
*cb_data
= (PW_CB_DATA
*) cb_tmp
;
362 if (cb_data
->password
)
363 password
= cb_data
->password
;
364 if (cb_data
->prompt_info
)
365 prompt_info
= cb_data
->prompt_info
;
368 res
= strlen(password
);
371 memcpy(buf
, password
, res
);
374 ui
= UI_new_method(ui_method
);
381 prompt
= UI_construct_prompt(ui
, "pass phrase", prompt_info
);
383 ui_flags
|= UI_INPUT_FLAG_DEFAULT_PWD
;
384 UI_ctrl(ui
, UI_CTRL_PRINT_ERRORS
, 1, 0, 0);
387 ok
= UI_add_input_string(ui
, prompt
, ui_flags
, buf
,
388 PW_MIN_LENGTH
, bufsiz
- 1);
389 if (ok
>= 0 && verify
) {
390 buff
= malloc(bufsiz
);
391 ok
= UI_add_verify_string(ui
, prompt
, ui_flags
, buff
,
392 PW_MIN_LENGTH
, bufsiz
- 1, buf
);
398 UI_ctrl(ui
, UI_CTRL_IS_REDOABLE
, 0, 0, 0));
400 freezero(buff
, (unsigned int) bufsiz
);
404 BIO_printf(bio_err
, "User interface error\n");
405 ERR_print_errors(bio_err
);
406 explicit_bzero(buf
, (unsigned int) bufsiz
);
410 BIO_printf(bio_err
, "aborted!\n");
411 explicit_bzero(buf
, (unsigned int) bufsiz
);
420 static char *app_get_pass(BIO
*err
, char *arg
, int keepbio
);
423 app_passwd(BIO
*err
, char *arg1
, char *arg2
, char **pass1
, char **pass2
)
427 if (!arg2
|| !arg1
|| strcmp(arg1
, arg2
))
432 *pass1
= app_get_pass(err
, arg1
, same
);
438 *pass2
= app_get_pass(err
, arg2
, same
? 2 : 0);
447 app_get_pass(BIO
*err
, char *arg
, int keepbio
)
449 char *tmp
, tpass
[APP_PASS_LEN
];
450 static BIO
*pwdbio
= NULL
;
451 const char *errstr
= NULL
;
454 if (!strncmp(arg
, "pass:", 5))
455 return strdup(arg
+ 5);
456 if (!strncmp(arg
, "env:", 4)) {
457 tmp
= getenv(arg
+ 4);
459 BIO_printf(err
, "Can't read environment variable %s\n",
465 if (!keepbio
|| !pwdbio
) {
466 if (!strncmp(arg
, "file:", 5)) {
467 pwdbio
= BIO_new_file(arg
+ 5, "r");
469 BIO_printf(err
, "Can't open file %s\n",
473 } else if (!strncmp(arg
, "fd:", 3)) {
475 i
= strtonum(arg
+ 3, 0, INT_MAX
, &errstr
);
478 "Invalid file descriptor %s: %s\n",
482 pwdbio
= BIO_new_fd(i
, BIO_NOCLOSE
);
485 "Can't access file descriptor %s\n",
490 * Can't do BIO_gets on an fd BIO so add a buffering
493 btmp
= BIO_new(BIO_f_buffer());
494 pwdbio
= BIO_push(btmp
, pwdbio
);
495 } else if (!strcmp(arg
, "stdin")) {
496 pwdbio
= BIO_new_fp(stdin
, BIO_NOCLOSE
);
498 BIO_printf(err
, "Can't open BIO for stdin\n");
502 BIO_printf(err
, "Invalid password argument \"%s\"\n",
507 i
= BIO_gets(pwdbio
, tpass
, APP_PASS_LEN
);
509 BIO_free_all(pwdbio
);
513 BIO_printf(err
, "Error reading password from BIO\n");
516 tmp
= strchr(tpass
, '\n');
519 return strdup(tpass
);
523 add_oid_section(BIO
*err
, CONF
*conf
)
526 STACK_OF(CONF_VALUE
) *sktmp
;
530 if (!(p
= NCONF_get_string(conf
, NULL
, "oid_section"))) {
534 if (!(sktmp
= NCONF_get_section(conf
, p
))) {
535 BIO_printf(err
, "problem loading oid section %s\n", p
);
538 for (i
= 0; i
< sk_CONF_VALUE_num(sktmp
); i
++) {
539 cnf
= sk_CONF_VALUE_value(sktmp
, i
);
540 if (OBJ_create(cnf
->value
, cnf
->name
, cnf
->name
) == NID_undef
) {
541 BIO_printf(err
, "problem creating object %s=%s\n",
542 cnf
->name
, cnf
->value
);
550 load_pkcs12(BIO
*err
, BIO
*in
, const char *desc
, pem_password_cb
*pem_cb
,
551 void *cb_data
, EVP_PKEY
**pkey
, X509
**cert
, STACK_OF(X509
) **ca
)
554 char tpass
[PEM_BUFSIZE
];
558 p12
= d2i_PKCS12_bio(in
, NULL
);
560 BIO_printf(err
, "Error loading PKCS12 file for %s\n", desc
);
563 /* See if an empty password will do */
564 if (PKCS12_verify_mac(p12
, "", 0) || PKCS12_verify_mac(p12
, NULL
, 0))
568 pem_cb
= password_callback
;
569 len
= pem_cb(tpass
, PEM_BUFSIZE
, 0, cb_data
);
571 BIO_printf(err
, "Passpharse callback error for %s\n",
575 if (len
< PEM_BUFSIZE
)
577 if (!PKCS12_verify_mac(p12
, tpass
, len
)) {
579 "Mac verify error (wrong password?) in PKCS12 file for %s\n", desc
);
584 ret
= PKCS12_parse(p12
, pass
, pkey
, cert
, ca
);
592 load_cert(BIO
*err
, const char *file
, int format
, const char *pass
,
593 const char *cert_descrip
)
598 if ((cert
= BIO_new(BIO_s_file())) == NULL
) {
599 ERR_print_errors(err
);
603 setvbuf(stdin
, NULL
, _IONBF
, 0);
604 BIO_set_fp(cert
, stdin
, BIO_NOCLOSE
);
606 if (BIO_read_filename(cert
, file
) <= 0) {
607 BIO_printf(err
, "Error opening %s %s\n",
609 ERR_print_errors(err
);
614 if (format
== FORMAT_ASN1
)
615 x
= d2i_X509_bio(cert
, NULL
);
616 else if (format
== FORMAT_NETSCAPE
) {
618 nx
= ASN1_item_d2i_bio(&NETSCAPE_X509_it
,
623 if ((strncmp(NETSCAPE_CERT_HDR
, (char *) nx
->header
->data
,
624 nx
->header
->length
) != 0)) {
625 NETSCAPE_X509_free(nx
);
627 "Error reading header on certificate\n");
632 NETSCAPE_X509_free(nx
);
633 } else if (format
== FORMAT_PEM
)
634 x
= PEM_read_bio_X509_AUX(cert
, NULL
, password_callback
, NULL
);
635 else if (format
== FORMAT_PKCS12
) {
636 if (!load_pkcs12(err
, cert
, cert_descrip
, NULL
, NULL
,
640 BIO_printf(err
, "bad input format specified for %s\n",
647 BIO_printf(err
, "unable to load certificate\n");
648 ERR_print_errors(err
);
655 load_key(BIO
*err
, const char *file
, int format
, int maybe_stdin
,
656 const char *pass
, const char *key_descrip
)
659 EVP_PKEY
*pkey
= NULL
;
662 cb_data
.password
= pass
;
663 cb_data
.prompt_info
= file
;
665 if (file
== NULL
&& (!maybe_stdin
)) {
666 BIO_printf(err
, "no keyfile specified\n");
669 key
= BIO_new(BIO_s_file());
671 ERR_print_errors(err
);
674 if (file
== NULL
&& maybe_stdin
) {
675 setvbuf(stdin
, NULL
, _IONBF
, 0);
676 BIO_set_fp(key
, stdin
, BIO_NOCLOSE
);
677 } else if (BIO_read_filename(key
, file
) <= 0) {
678 BIO_printf(err
, "Error opening %s %s\n",
680 ERR_print_errors(err
);
683 if (format
== FORMAT_ASN1
) {
684 pkey
= d2i_PrivateKey_bio(key
, NULL
);
685 } else if (format
== FORMAT_PEM
) {
686 pkey
= PEM_read_bio_PrivateKey(key
, NULL
, password_callback
, &cb_data
);
688 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
689 else if (format
== FORMAT_NETSCAPE
|| format
== FORMAT_IISSGC
)
690 pkey
= load_netscape_key(err
, key
, file
, key_descrip
, format
);
692 else if (format
== FORMAT_PKCS12
) {
693 if (!load_pkcs12(err
, key
, key_descrip
, password_callback
, &cb_data
,
697 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) && !defined (OPENSSL_NO_RC4)
698 else if (format
== FORMAT_MSBLOB
)
699 pkey
= b2i_PrivateKey_bio(key
);
700 else if (format
== FORMAT_PVK
)
701 pkey
= b2i_PVK_bio(key
, password_callback
,
705 BIO_printf(err
, "bad input format specified for key file\n");
711 BIO_printf(err
, "unable to load %s\n", key_descrip
);
712 ERR_print_errors(err
);
718 load_pubkey(BIO
*err
, const char *file
, int format
, int maybe_stdin
,
719 const char *pass
, const char *key_descrip
)
722 EVP_PKEY
*pkey
= NULL
;
725 cb_data
.password
= pass
;
726 cb_data
.prompt_info
= file
;
728 if (file
== NULL
&& !maybe_stdin
) {
729 BIO_printf(err
, "no keyfile specified\n");
732 key
= BIO_new(BIO_s_file());
734 ERR_print_errors(err
);
737 if (file
== NULL
&& maybe_stdin
) {
738 setvbuf(stdin
, NULL
, _IONBF
, 0);
739 BIO_set_fp(key
, stdin
, BIO_NOCLOSE
);
740 } else if (BIO_read_filename(key
, file
) <= 0) {
741 BIO_printf(err
, "Error opening %s %s\n", key_descrip
, file
);
742 ERR_print_errors(err
);
745 if (format
== FORMAT_ASN1
) {
746 pkey
= d2i_PUBKEY_bio(key
, NULL
);
748 else if (format
== FORMAT_ASN1RSA
) {
750 rsa
= d2i_RSAPublicKey_bio(key
, NULL
);
752 pkey
= EVP_PKEY_new();
754 EVP_PKEY_set1_RSA(pkey
, rsa
);
758 } else if (format
== FORMAT_PEMRSA
) {
760 rsa
= PEM_read_bio_RSAPublicKey(key
, NULL
, password_callback
, &cb_data
);
762 pkey
= EVP_PKEY_new();
764 EVP_PKEY_set1_RSA(pkey
, rsa
);
769 else if (format
== FORMAT_PEM
) {
770 pkey
= PEM_read_bio_PUBKEY(key
, NULL
, password_callback
, &cb_data
);
772 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
773 else if (format
== FORMAT_NETSCAPE
|| format
== FORMAT_IISSGC
)
774 pkey
= load_netscape_key(err
, key
, file
, key_descrip
, format
);
776 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA)
777 else if (format
== FORMAT_MSBLOB
)
778 pkey
= b2i_PublicKey_bio(key
);
781 BIO_printf(err
, "bad input format specified for key file\n");
788 BIO_printf(err
, "unable to load %s\n", key_descrip
);
792 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
794 load_netscape_key(BIO
*err
, BIO
*key
, const char *file
,
795 const char *key_descrip
, int format
)
800 const unsigned char *p
;
804 pkey
= EVP_PKEY_new();
806 if (buf
== NULL
|| pkey
== NULL
)
809 if (!BUF_MEM_grow_clean(buf
, size
+ 1024 * 10))
811 i
= BIO_read(key
, &(buf
->data
[size
]), 1024 * 10);
816 BIO_printf(err
, "Error reading %s %s",
821 p
= (unsigned char *) buf
->data
;
822 rsa
= d2i_RSA_NET(NULL
, &p
, (long) size
, NULL
,
823 (format
== FORMAT_IISSGC
? 1 : 0));
827 EVP_PKEY_set1_RSA(pkey
, rsa
);
835 #endif /* ndef OPENSSL_NO_RC4 */
838 load_certs_crls(BIO
*err
, const char *file
, int format
, const char *pass
,
839 const char *desc
, STACK_OF(X509
) **pcerts
,
840 STACK_OF(X509_CRL
) **pcrls
)
844 STACK_OF(X509_INFO
) *xis
= NULL
;
849 cb_data
.password
= pass
;
850 cb_data
.prompt_info
= file
;
852 if (format
!= FORMAT_PEM
) {
853 BIO_printf(err
, "bad input format specified for %s\n", desc
);
857 bio
= BIO_new_fp(stdin
, BIO_NOCLOSE
);
859 bio
= BIO_new_file(file
, "r");
862 BIO_printf(err
, "Error opening %s %s\n",
863 desc
, file
? file
: "stdin");
864 ERR_print_errors(err
);
867 xis
= PEM_X509_INFO_read_bio(bio
, NULL
, password_callback
, &cb_data
);
872 *pcerts
= sk_X509_new_null();
877 *pcrls
= sk_X509_CRL_new_null();
881 for (i
= 0; i
< sk_X509_INFO_num(xis
); i
++) {
882 xi
= sk_X509_INFO_value(xis
, i
);
883 if (xi
->x509
&& pcerts
) {
884 if (!sk_X509_push(*pcerts
, xi
->x509
))
888 if (xi
->crl
&& pcrls
) {
889 if (!sk_X509_CRL_push(*pcrls
, xi
->crl
))
895 if (pcerts
&& sk_X509_num(*pcerts
) > 0)
898 if (pcrls
&& sk_X509_CRL_num(*pcrls
) > 0)
902 sk_X509_INFO_pop_free(xis
, X509_INFO_free
);
906 sk_X509_pop_free(*pcerts
, X509_free
);
910 sk_X509_CRL_pop_free(*pcrls
, X509_CRL_free
);
913 BIO_printf(err
, "unable to load %s\n",
914 pcerts
? "certificates" : "CRLs");
915 ERR_print_errors(err
);
921 load_certs(BIO
*err
, const char *file
, int format
, const char *pass
,
924 STACK_OF(X509
) *certs
;
926 if (!load_certs_crls(err
, file
, format
, pass
, desc
, &certs
, NULL
))
932 load_crls(BIO
*err
, const char *file
, int format
, const char *pass
,
935 STACK_OF(X509_CRL
) *crls
;
937 if (!load_certs_crls(err
, file
, format
, pass
, desc
, NULL
, &crls
))
942 #define X509V3_EXT_UNKNOWN_MASK (0xfL << 16)
943 /* Return error for unknown extensions */
944 #define X509V3_EXT_DEFAULT 0
945 /* Print error for unknown extensions */
946 #define X509V3_EXT_ERROR_UNKNOWN (1L << 16)
947 /* ASN1 parse unknown extensions */
948 #define X509V3_EXT_PARSE_UNKNOWN (2L << 16)
949 /* BIO_dump unknown extensions */
950 #define X509V3_EXT_DUMP_UNKNOWN (3L << 16)
952 #define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \
953 X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION)
956 set_cert_ex(unsigned long *flags
, const char *arg
)
958 static const NAME_EX_TBL cert_tbl
[] = {
959 {"compatible", X509_FLAG_COMPAT
, 0xffffffffl
},
960 {"ca_default", X509_FLAG_CA
, 0xffffffffl
},
961 {"no_header", X509_FLAG_NO_HEADER
, 0},
962 {"no_version", X509_FLAG_NO_VERSION
, 0},
963 {"no_serial", X509_FLAG_NO_SERIAL
, 0},
964 {"no_signame", X509_FLAG_NO_SIGNAME
, 0},
965 {"no_validity", X509_FLAG_NO_VALIDITY
, 0},
966 {"no_subject", X509_FLAG_NO_SUBJECT
, 0},
967 {"no_issuer", X509_FLAG_NO_ISSUER
, 0},
968 {"no_pubkey", X509_FLAG_NO_PUBKEY
, 0},
969 {"no_extensions", X509_FLAG_NO_EXTENSIONS
, 0},
970 {"no_sigdump", X509_FLAG_NO_SIGDUMP
, 0},
971 {"no_aux", X509_FLAG_NO_AUX
, 0},
972 {"no_attributes", X509_FLAG_NO_ATTRIBUTES
, 0},
973 {"ext_default", X509V3_EXT_DEFAULT
, X509V3_EXT_UNKNOWN_MASK
},
974 {"ext_error", X509V3_EXT_ERROR_UNKNOWN
, X509V3_EXT_UNKNOWN_MASK
},
975 {"ext_parse", X509V3_EXT_PARSE_UNKNOWN
, X509V3_EXT_UNKNOWN_MASK
},
976 {"ext_dump", X509V3_EXT_DUMP_UNKNOWN
, X509V3_EXT_UNKNOWN_MASK
},
979 return set_multi_opts(flags
, arg
, cert_tbl
);
983 set_name_ex(unsigned long *flags
, const char *arg
)
985 static const NAME_EX_TBL ex_tbl
[] = {
986 {"esc_2253", ASN1_STRFLGS_ESC_2253
, 0},
987 {"esc_ctrl", ASN1_STRFLGS_ESC_CTRL
, 0},
988 {"esc_msb", ASN1_STRFLGS_ESC_MSB
, 0},
989 {"use_quote", ASN1_STRFLGS_ESC_QUOTE
, 0},
990 {"utf8", ASN1_STRFLGS_UTF8_CONVERT
, 0},
991 {"ignore_type", ASN1_STRFLGS_IGNORE_TYPE
, 0},
992 {"show_type", ASN1_STRFLGS_SHOW_TYPE
, 0},
993 {"dump_all", ASN1_STRFLGS_DUMP_ALL
, 0},
994 {"dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN
, 0},
995 {"dump_der", ASN1_STRFLGS_DUMP_DER
, 0},
996 {"compat", XN_FLAG_COMPAT
, 0xffffffffL
},
997 {"sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS
, XN_FLAG_SEP_MASK
},
998 {"sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC
, XN_FLAG_SEP_MASK
},
999 {"sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC
, XN_FLAG_SEP_MASK
},
1000 {"sep_multiline", XN_FLAG_SEP_MULTILINE
, XN_FLAG_SEP_MASK
},
1001 {"dn_rev", XN_FLAG_DN_REV
, 0},
1002 {"nofname", XN_FLAG_FN_NONE
, XN_FLAG_FN_MASK
},
1003 {"sname", XN_FLAG_FN_SN
, XN_FLAG_FN_MASK
},
1004 {"lname", XN_FLAG_FN_LN
, XN_FLAG_FN_MASK
},
1005 {"align", XN_FLAG_FN_ALIGN
, 0},
1006 {"oid", XN_FLAG_FN_OID
, XN_FLAG_FN_MASK
},
1007 {"space_eq", XN_FLAG_SPC_EQ
, 0},
1008 {"dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS
, 0},
1009 {"RFC2253", XN_FLAG_RFC2253
, 0xffffffffL
},
1010 {"oneline", XN_FLAG_ONELINE
, 0xffffffffL
},
1011 {"multiline", XN_FLAG_MULTILINE
, 0xffffffffL
},
1012 {"ca_default", XN_FLAG_MULTILINE
, 0xffffffffL
},
1015 return set_multi_opts(flags
, arg
, ex_tbl
);
1019 set_ext_copy(int *copy_type
, const char *arg
)
1021 if (!strcasecmp(arg
, "none"))
1022 *copy_type
= EXT_COPY_NONE
;
1023 else if (!strcasecmp(arg
, "copy"))
1024 *copy_type
= EXT_COPY_ADD
;
1025 else if (!strcasecmp(arg
, "copyall"))
1026 *copy_type
= EXT_COPY_ALL
;
1033 copy_extensions(X509
*x
, X509_REQ
*req
, int copy_type
)
1035 STACK_OF(X509_EXTENSION
) *exts
= NULL
;
1036 X509_EXTENSION
*ext
, *tmpext
;
1038 int i
, idx
, ret
= 0;
1040 if (!x
|| !req
|| (copy_type
== EXT_COPY_NONE
))
1042 exts
= X509_REQ_get_extensions(req
);
1044 for (i
= 0; i
< sk_X509_EXTENSION_num(exts
); i
++) {
1045 ext
= sk_X509_EXTENSION_value(exts
, i
);
1046 obj
= X509_EXTENSION_get_object(ext
);
1047 idx
= X509_get_ext_by_OBJ(x
, obj
, -1);
1048 /* Does extension exist? */
1050 /* If normal copy don't override existing extension */
1051 if (copy_type
== EXT_COPY_ADD
)
1053 /* Delete all extensions of same type */
1055 tmpext
= X509_get_ext(x
, idx
);
1056 X509_delete_ext(x
, idx
);
1057 X509_EXTENSION_free(tmpext
);
1058 idx
= X509_get_ext_by_OBJ(x
, obj
, -1);
1059 } while (idx
!= -1);
1061 if (!X509_add_ext(x
, ext
, -1))
1068 sk_X509_EXTENSION_pop_free(exts
, X509_EXTENSION_free
);
1074 set_multi_opts(unsigned long *flags
, const char *arg
,
1075 const NAME_EX_TBL
*in_tbl
)
1077 STACK_OF(CONF_VALUE
) *vals
;
1083 vals
= X509V3_parse_list(arg
);
1084 for (i
= 0; i
< sk_CONF_VALUE_num(vals
); i
++) {
1085 val
= sk_CONF_VALUE_value(vals
, i
);
1086 if (!set_table_opts(flags
, val
->name
, in_tbl
))
1089 sk_CONF_VALUE_pop_free(vals
, X509V3_conf_free
);
1094 set_table_opts(unsigned long *flags
, const char *arg
,
1095 const NAME_EX_TBL
*in_tbl
)
1098 const NAME_EX_TBL
*ptbl
;
1104 } else if (c
== '+') {
1110 for (ptbl
= in_tbl
; ptbl
->name
; ptbl
++) {
1111 if (!strcasecmp(arg
, ptbl
->name
)) {
1112 *flags
&= ~ptbl
->mask
;
1114 *flags
|= ptbl
->flag
;
1116 *flags
&= ~ptbl
->flag
;
1124 print_name(BIO
*out
, const char *title
, X509_NAME
*nm
, unsigned long lflags
)
1131 BIO_puts(out
, title
);
1132 if ((lflags
& XN_FLAG_SEP_MASK
) == XN_FLAG_SEP_MULTILINE
) {
1136 if (lflags
== XN_FLAG_COMPAT
) {
1137 buf
= X509_NAME_oneline(nm
, 0, 0);
1139 BIO_puts(out
, "\n");
1143 BIO_puts(out
, "\n");
1144 X509_NAME_print_ex(out
, nm
, indent
, lflags
);
1145 BIO_puts(out
, "\n");
1150 setup_verify(BIO
*bp
, char *CAfile
, char *CApath
)
1153 X509_LOOKUP
*lookup
;
1155 if (!(store
= X509_STORE_new()))
1157 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_file());
1161 if (!X509_LOOKUP_load_file(lookup
, CAfile
, X509_FILETYPE_PEM
)) {
1162 BIO_printf(bp
, "Error loading file %s\n", CAfile
);
1166 X509_LOOKUP_load_file(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
1168 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_hash_dir());
1172 if (!X509_LOOKUP_add_dir(lookup
, CApath
, X509_FILETYPE_PEM
)) {
1173 BIO_printf(bp
, "Error loading directory %s\n", CApath
);
1177 X509_LOOKUP_add_dir(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
1183 X509_STORE_free(store
);
1188 load_config(BIO
*err
, CONF
*cnf
)
1190 static int load_config_called
= 0;
1192 if (load_config_called
)
1194 load_config_called
= 1;
1200 OPENSSL_load_builtin_modules();
1202 if (CONF_modules_load(cnf
, NULL
, 0) <= 0) {
1203 BIO_printf(err
, "Error configuring OpenSSL\n");
1204 ERR_print_errors(err
);
1213 const char *t
= X509_get_default_cert_area();
1216 if (asprintf(&p
, "%s/openssl.cnf", t
) == -1)
1221 static unsigned long
1222 index_serial_hash(const OPENSSL_CSTRING
*a
)
1229 return (lh_strhash(n
));
1233 index_serial_cmp(const OPENSSL_CSTRING
*a
, const OPENSSL_CSTRING
*b
)
1235 const char *aa
, *bb
;
1237 for (aa
= a
[DB_serial
]; *aa
== '0'; aa
++)
1239 for (bb
= b
[DB_serial
]; *bb
== '0'; bb
++)
1241 return (strcmp(aa
, bb
));
1245 index_name_qual(char **a
)
1247 return (a
[0][0] == 'V');
1250 static unsigned long
1251 index_name_hash(const OPENSSL_CSTRING
*a
)
1253 return (lh_strhash(a
[DB_name
]));
1257 index_name_cmp(const OPENSSL_CSTRING
*a
, const OPENSSL_CSTRING
*b
)
1259 return (strcmp(a
[DB_name
], b
[DB_name
]));
1262 static IMPLEMENT_LHASH_HASH_FN(index_serial
, OPENSSL_CSTRING
)
1263 static IMPLEMENT_LHASH_COMP_FN(index_serial
, OPENSSL_CSTRING
)
1264 static IMPLEMENT_LHASH_HASH_FN(index_name
, OPENSSL_CSTRING
)
1265 static IMPLEMENT_LHASH_COMP_FN(index_name
, OPENSSL_CSTRING
)
1268 load_serial(char *serialfile
, int create
, ASN1_INTEGER
**retai
)
1273 ASN1_INTEGER
*ai
= NULL
;
1275 ai
= ASN1_INTEGER_new();
1279 if ((in
= BIO_new(BIO_s_file())) == NULL
) {
1280 ERR_print_errors(bio_err
);
1283 if (BIO_read_filename(in
, serialfile
) <= 0) {
1289 if (ret
== NULL
|| !rand_serial(ret
, ai
))
1290 BIO_printf(bio_err
, "Out of memory\n");
1293 if (!a2i_ASN1_INTEGER(in
, ai
, buf
, sizeof buf
)) {
1294 BIO_printf(bio_err
, "unable to load number from %s\n",
1298 ret
= ASN1_INTEGER_to_BN(ai
, NULL
);
1301 "error converting number from bin to BIGNUM\n");
1313 ASN1_INTEGER_free(ai
);
1318 save_serial(char *serialfile
, char *suffix
, BIGNUM
*serial
,
1319 ASN1_INTEGER
**retai
)
1321 char serialpath
[PATH_MAX
];
1324 ASN1_INTEGER
*ai
= NULL
;
1327 n
= strlcpy(serialpath
, serialfile
, sizeof serialpath
);
1329 n
= snprintf(serialpath
, sizeof serialpath
, "%s.%s",
1330 serialfile
, suffix
);
1331 if (n
== -1 || n
>= sizeof(serialpath
)) {
1332 BIO_printf(bio_err
, "serial too long\n");
1335 out
= BIO_new(BIO_s_file());
1337 ERR_print_errors(bio_err
);
1340 if (BIO_write_filename(out
, serialpath
) <= 0) {
1344 if ((ai
= BN_to_ASN1_INTEGER(serial
, NULL
)) == NULL
) {
1346 "error converting serial to ASN.1 format\n");
1349 i2a_ASN1_INTEGER(out
, ai
);
1350 BIO_puts(out
, "\n");
1359 ASN1_INTEGER_free(ai
);
1364 rotate_serial(char *serialfile
, char *new_suffix
, char *old_suffix
)
1366 char opath
[PATH_MAX
], npath
[PATH_MAX
];
1368 if (snprintf(npath
, sizeof npath
, "%s.%s", serialfile
,
1369 new_suffix
) >= sizeof npath
) {
1370 BIO_printf(bio_err
, "file name too long\n");
1374 if (snprintf(opath
, sizeof opath
, "%s.%s", serialfile
,
1375 old_suffix
) >= sizeof opath
) {
1376 BIO_printf(bio_err
, "file name too long\n");
1380 if (rename(serialfile
, opath
) < 0 &&
1381 errno
!= ENOENT
&& errno
!= ENOTDIR
) {
1382 BIO_printf(bio_err
, "unable to rename %s to %s\n",
1389 if (rename(npath
, serialfile
) < 0) {
1390 BIO_printf(bio_err
, "unable to rename %s to %s\n",
1393 if (rename(opath
, serialfile
) < 0) {
1394 BIO_printf(bio_err
, "unable to rename %s to %s\n",
1407 rand_serial(BIGNUM
*b
, ASN1_INTEGER
*ai
)
1420 if (!BN_pseudo_rand(btmp
, SERIAL_RAND_BITS
, 0, 0))
1422 if (ai
&& !BN_to_ASN1_INTEGER(btmp
, ai
))
1435 load_index(char *dbfile
, DB_ATTR
*db_attr
)
1437 CA_DB
*retdb
= NULL
;
1438 TXT_DB
*tmpdb
= NULL
;
1439 BIO
*in
= BIO_new(BIO_s_file());
1440 CONF
*dbattr_conf
= NULL
;
1441 char attrpath
[PATH_MAX
];
1442 long errorline
= -1;
1445 ERR_print_errors(bio_err
);
1448 if (BIO_read_filename(in
, dbfile
) <= 0) {
1450 BIO_printf(bio_err
, "unable to open '%s'\n", dbfile
);
1453 if ((tmpdb
= TXT_DB_read(in
, DB_NUMBER
)) == NULL
)
1456 if (snprintf(attrpath
, sizeof attrpath
, "%s.attr", dbfile
)
1457 >= sizeof attrpath
) {
1458 BIO_printf(bio_err
, "attr filename too long\n");
1462 dbattr_conf
= NCONF_new(NULL
);
1463 if (NCONF_load(dbattr_conf
, attrpath
, &errorline
) <= 0) {
1464 if (errorline
> 0) {
1466 "error on line %ld of db attribute file '%s'\n",
1467 errorline
, attrpath
);
1470 NCONF_free(dbattr_conf
);
1474 if ((retdb
= malloc(sizeof(CA_DB
))) == NULL
) {
1475 fprintf(stderr
, "Out of memory\n");
1481 retdb
->attributes
= *db_attr
;
1483 retdb
->attributes
.unique_subject
= 1;
1487 char *p
= NCONF_get_string(dbattr_conf
, NULL
, "unique_subject");
1489 retdb
->attributes
.unique_subject
= parse_yesno(p
, 1);
1494 NCONF_free(dbattr_conf
);
1501 index_index(CA_DB
*db
)
1503 if (!TXT_DB_create_index(db
->db
, DB_serial
, NULL
,
1504 LHASH_HASH_FN(index_serial
), LHASH_COMP_FN(index_serial
))) {
1506 "error creating serial number index:(%ld,%ld,%ld)\n",
1507 db
->db
->error
, db
->db
->arg1
, db
->db
->arg2
);
1510 if (db
->attributes
.unique_subject
&&
1511 !TXT_DB_create_index(db
->db
, DB_name
, index_name_qual
,
1512 LHASH_HASH_FN(index_name
), LHASH_COMP_FN(index_name
))) {
1513 BIO_printf(bio_err
, "error creating name index:(%ld,%ld,%ld)\n",
1514 db
->db
->error
, db
->db
->arg1
, db
->db
->arg2
);
1521 save_index(const char *file
, const char *suffix
, CA_DB
*db
)
1523 char attrpath
[PATH_MAX
], dbfile
[PATH_MAX
];
1524 BIO
*out
= BIO_new(BIO_s_file());
1528 ERR_print_errors(bio_err
);
1531 if (snprintf(attrpath
, sizeof attrpath
, "%s.attr.%s",
1532 file
, suffix
) >= sizeof attrpath
) {
1533 BIO_printf(bio_err
, "file name too long\n");
1536 if (snprintf(dbfile
, sizeof dbfile
, "%s.%s",
1537 file
, suffix
) >= sizeof dbfile
) {
1538 BIO_printf(bio_err
, "file name too long\n");
1542 if (BIO_write_filename(out
, dbfile
) <= 0) {
1544 BIO_printf(bio_err
, "unable to open '%s'\n", dbfile
);
1547 j
= TXT_DB_write(out
, db
->db
);
1553 out
= BIO_new(BIO_s_file());
1555 if (BIO_write_filename(out
, attrpath
) <= 0) {
1557 BIO_printf(bio_err
, "unable to open '%s'\n", attrpath
);
1560 BIO_printf(out
, "unique_subject = %s\n",
1561 db
->attributes
.unique_subject
? "yes" : "no");
1571 rotate_index(const char *dbfile
, const char *new_suffix
, const char *old_suffix
)
1573 char attrpath
[PATH_MAX
], nattrpath
[PATH_MAX
], oattrpath
[PATH_MAX
];
1574 char dbpath
[PATH_MAX
], odbpath
[PATH_MAX
];
1576 if (snprintf(attrpath
, sizeof attrpath
, "%s.attr",
1577 dbfile
) >= sizeof attrpath
) {
1578 BIO_printf(bio_err
, "file name too long\n");
1581 if (snprintf(nattrpath
, sizeof nattrpath
, "%s.attr.%s",
1582 dbfile
, new_suffix
) >= sizeof nattrpath
) {
1583 BIO_printf(bio_err
, "file name too long\n");
1586 if (snprintf(oattrpath
, sizeof oattrpath
, "%s.attr.%s",
1587 dbfile
, old_suffix
) >= sizeof oattrpath
) {
1588 BIO_printf(bio_err
, "file name too long\n");
1591 if (snprintf(dbpath
, sizeof dbpath
, "%s.%s",
1592 dbfile
, new_suffix
) >= sizeof dbpath
) {
1593 BIO_printf(bio_err
, "file name too long\n");
1596 if (snprintf(odbpath
, sizeof odbpath
, "%s.%s",
1597 dbfile
, old_suffix
) >= sizeof odbpath
) {
1598 BIO_printf(bio_err
, "file name too long\n");
1602 if (rename(dbfile
, odbpath
) < 0 && errno
!= ENOENT
&& errno
!= ENOTDIR
) {
1603 BIO_printf(bio_err
, "unable to rename %s to %s\n",
1609 if (rename(dbpath
, dbfile
) < 0) {
1610 BIO_printf(bio_err
, "unable to rename %s to %s\n",
1613 if (rename(odbpath
, dbfile
) < 0) {
1614 BIO_printf(bio_err
, "unable to rename %s to %s\n",
1621 if (rename(attrpath
, oattrpath
) < 0 && errno
!= ENOENT
&& errno
!= ENOTDIR
) {
1622 BIO_printf(bio_err
, "unable to rename %s to %s\n",
1623 attrpath
, oattrpath
);
1625 if (rename(dbfile
, dbpath
) < 0) {
1626 BIO_printf(bio_err
, "unable to rename %s to %s\n",
1630 if (rename(odbpath
, dbfile
) < 0) {
1631 BIO_printf(bio_err
, "unable to rename %s to %s\n",
1638 if (rename(nattrpath
, attrpath
) < 0) {
1639 BIO_printf(bio_err
, "unable to rename %s to %s\n",
1640 nattrpath
, attrpath
);
1642 if (rename(oattrpath
, attrpath
) < 0) {
1643 BIO_printf(bio_err
, "unable to rename %s to %s\n",
1644 oattrpath
, attrpath
);
1647 if (rename(dbfile
, dbpath
) < 0) {
1648 BIO_printf(bio_err
, "unable to rename %s to %s\n",
1652 if (rename(odbpath
, dbfile
) < 0) {
1653 BIO_printf(bio_err
, "unable to rename %s to %s\n",
1666 free_index(CA_DB
*db
)
1669 TXT_DB_free(db
->db
);
1675 parse_yesno(const char *str
, int def
)
1681 case 'f': /* false */
1682 case 'F': /* FALSE */
1688 case 't': /* true */
1689 case 'T': /* TRUE */
1704 * subject is expected to be in the format /type0=value0/type1=value1/type2=...
1705 * where characters may be escaped by \
1708 parse_name(char *subject
, long chtype
, int multirdn
)
1710 X509_NAME
*name
= NULL
;
1711 size_t buflen
, max_ne
;
1712 char **ne_types
, **ne_values
;
1713 char *buf
, *bp
, *sp
;
1714 int i
, nid
, ne_num
= 0;
1718 * Buffer to copy the types and values into. Due to escaping the
1719 * copy can only become shorter.
1721 buflen
= strlen(subject
) + 1;
1722 buf
= malloc(buflen
);
1724 /* Maximum number of name elements. */
1725 max_ne
= buflen
/ 2 + 1;
1726 ne_types
= reallocarray(NULL
, max_ne
, sizeof(char *));
1727 ne_values
= reallocarray(NULL
, max_ne
, sizeof(char *));
1728 mval
= reallocarray(NULL
, max_ne
, sizeof(int));
1730 if (buf
== NULL
|| ne_types
== NULL
|| ne_values
== NULL
||
1732 BIO_printf(bio_err
, "malloc error\n");
1739 if (*subject
!= '/') {
1740 BIO_printf(bio_err
, "Subject does not start with '/'.\n");
1744 /* Skip leading '/'. */
1747 /* No multivalued RDN by default. */
1752 ne_types
[ne_num
] = bp
;
1754 /* is there anything to escape in the type...? */
1759 BIO_printf(bio_err
, "escape character "
1760 "at end of string\n");
1763 } else if (*sp
== '=') {
1771 BIO_printf(bio_err
, "end of string encountered while "
1772 "processing type of subject name element #%d\n",
1776 ne_values
[ne_num
] = bp
;
1782 BIO_printf(bio_err
, "escape character "
1783 "at end of string\n");
1786 } else if (*sp
== '/') {
1788 /* no multivalued RDN by default */
1789 mval
[ne_num
+ 1] = 0;
1791 } else if (*sp
== '+' && multirdn
) {
1792 /* a not escaped + signals a multivalued RDN */
1794 mval
[ne_num
+ 1] = -1;
1803 if ((name
= X509_NAME_new()) == NULL
)
1806 for (i
= 0; i
< ne_num
; i
++) {
1807 if ((nid
= OBJ_txt2nid(ne_types
[i
])) == NID_undef
) {
1809 "Subject Attribute %s has no known NID, skipped\n",
1813 if (!*ne_values
[i
]) {
1814 BIO_printf(bio_err
, "No value provided for Subject "
1815 "Attribute %s, skipped\n", ne_types
[i
]);
1818 if (!X509_NAME_add_entry_by_NID(name
, nid
, chtype
,
1819 (unsigned char *) ne_values
[i
], -1, -1, mval
[i
]))
1825 X509_NAME_free(name
);
1838 args_verify(char ***pargs
, int *pargc
, int *badarg
, BIO
*err
,
1839 X509_VERIFY_PARAM
**pm
)
1841 ASN1_OBJECT
*otmp
= NULL
;
1842 unsigned long flags
= 0;
1844 int purpose
= 0, depth
= -1;
1845 char **oldargs
= *pargs
;
1846 char *arg
= **pargs
, *argn
= (*pargs
)[1];
1848 const char *errstr
= NULL
;
1850 if (!strcmp(arg
, "-policy")) {
1854 otmp
= OBJ_txt2obj(argn
, 0);
1856 BIO_printf(err
, "Invalid Policy \"%s\"\n",
1862 } else if (strcmp(arg
, "-purpose") == 0) {
1863 X509_PURPOSE
*xptmp
;
1867 i
= X509_PURPOSE_get_by_sname(argn
);
1869 BIO_printf(err
, "unrecognized purpose\n");
1872 xptmp
= X509_PURPOSE_get0(i
);
1873 purpose
= X509_PURPOSE_get_id(xptmp
);
1877 } else if (strcmp(arg
, "-verify_depth") == 0) {
1881 depth
= strtonum(argn
, 1, INT_MAX
, &errstr
);
1883 BIO_printf(err
, "invalid depth %s: %s\n",
1889 } else if (strcmp(arg
, "-attime") == 0) {
1893 long long timestamp
;
1895 * interpret the -attime argument as seconds since
1898 if (sscanf(argn
, "%lli", ×tamp
) != 1) {
1900 "Error parsing timestamp %s\n",
1904 /* XXX 2038 truncation */
1905 at_time
= (time_t) timestamp
;
1908 } else if (!strcmp(arg
, "-ignore_critical"))
1909 flags
|= X509_V_FLAG_IGNORE_CRITICAL
;
1910 else if (!strcmp(arg
, "-issuer_checks"))
1911 flags
|= X509_V_FLAG_CB_ISSUER_CHECK
;
1912 else if (!strcmp(arg
, "-crl_check"))
1913 flags
|= X509_V_FLAG_CRL_CHECK
;
1914 else if (!strcmp(arg
, "-crl_check_all"))
1915 flags
|= X509_V_FLAG_CRL_CHECK
| X509_V_FLAG_CRL_CHECK_ALL
;
1916 else if (!strcmp(arg
, "-policy_check"))
1917 flags
|= X509_V_FLAG_POLICY_CHECK
;
1918 else if (!strcmp(arg
, "-explicit_policy"))
1919 flags
|= X509_V_FLAG_EXPLICIT_POLICY
;
1920 else if (!strcmp(arg
, "-inhibit_any"))
1921 flags
|= X509_V_FLAG_INHIBIT_ANY
;
1922 else if (!strcmp(arg
, "-inhibit_map"))
1923 flags
|= X509_V_FLAG_INHIBIT_MAP
;
1924 else if (!strcmp(arg
, "-x509_strict"))
1925 flags
|= X509_V_FLAG_X509_STRICT
;
1926 else if (!strcmp(arg
, "-extended_crl"))
1927 flags
|= X509_V_FLAG_EXTENDED_CRL_SUPPORT
;
1928 else if (!strcmp(arg
, "-use_deltas"))
1929 flags
|= X509_V_FLAG_USE_DELTAS
;
1930 else if (!strcmp(arg
, "-policy_print"))
1931 flags
|= X509_V_FLAG_NOTIFY_POLICY
;
1932 else if (!strcmp(arg
, "-check_ss_sig"))
1933 flags
|= X509_V_FLAG_CHECK_SS_SIGNATURE
;
1938 X509_VERIFY_PARAM_free(*pm
);
1942 if (!*pm
&& !(*pm
= X509_VERIFY_PARAM_new())) {
1947 X509_VERIFY_PARAM_add0_policy(*pm
, otmp
);
1951 X509_VERIFY_PARAM_set_flags(*pm
, flags
);
1954 X509_VERIFY_PARAM_set_purpose(*pm
, purpose
);
1957 X509_VERIFY_PARAM_set_depth(*pm
, depth
);
1960 X509_VERIFY_PARAM_set_time(*pm
, at_time
);
1966 *pargc
-= *pargs
- oldargs
;
1968 ASN1_OBJECT_free(otmp
);
1972 /* Read whole contents of a BIO into an allocated memory buffer and
1977 bio_to_mem(unsigned char **out
, int maxlen
, BIO
*in
)
1981 unsigned char tbuf
[1024];
1983 mem
= BIO_new(BIO_s_mem());
1987 if ((maxlen
!= -1) && maxlen
< 1024)
1991 len
= BIO_read(in
, tbuf
, len
);
1994 if (BIO_write(mem
, tbuf
, len
) != len
) {
2003 ret
= BIO_get_mem_data(mem
, (char **) out
);
2004 BIO_set_flags(mem
, BIO_FLAGS_MEM_RDONLY
);
2010 pkey_ctrl_string(EVP_PKEY_CTX
*ctx
, char *value
)
2013 char *stmp
, *vtmp
= NULL
;
2017 stmp
= strdup(value
);
2020 vtmp
= strchr(stmp
, ':');
2025 rv
= EVP_PKEY_CTX_ctrl_str(ctx
, stmp
, vtmp
);
2032 nodes_print(BIO
*out
, const char *name
, STACK_OF(X509_POLICY_NODE
) *nodes
)
2034 X509_POLICY_NODE
*node
;
2037 BIO_printf(out
, "%s Policies:", name
);
2039 BIO_puts(out
, "\n");
2040 for (i
= 0; i
< sk_X509_POLICY_NODE_num(nodes
); i
++) {
2041 node
= sk_X509_POLICY_NODE_value(nodes
, i
);
2042 X509_POLICY_NODE_print(out
, node
, 2);
2045 BIO_puts(out
, " <empty>\n");
2049 policies_print(BIO
*out
, X509_STORE_CTX
*ctx
)
2051 X509_POLICY_TREE
*tree
;
2052 int explicit_policy
;
2056 out
= BIO_new_fp(stderr
, BIO_NOCLOSE
);
2059 tree
= X509_STORE_CTX_get0_policy_tree(ctx
);
2060 explicit_policy
= X509_STORE_CTX_get_explicit_policy(ctx
);
2062 BIO_printf(out
, "Require explicit Policy: %s\n",
2063 explicit_policy
? "True" : "False");
2065 nodes_print(out
, "Authority", X509_policy_tree_get0_policies(tree
));
2066 nodes_print(out
, "User", X509_policy_tree_get0_user_policies(tree
));
2072 * next_protos_parse parses a comma separated list of strings into a string
2073 * in a format suitable for passing to SSL_CTX_set_next_protos_advertised.
2074 * outlen: (output) set to the length of the resulting buffer on success.
2075 * err: (maybe NULL) on failure, an error message line is written to this BIO.
2076 * in: a NUL termianted string like "abc,def,ghi"
2078 * returns: a malloced buffer or NULL on failure.
2081 next_protos_parse(unsigned short *outlen
, const char *in
)
2085 size_t i
, start
= 0;
2091 out
= malloc(strlen(in
) + 1);
2095 for (i
= 0; i
<= len
; ++i
) {
2096 if (i
== len
|| in
[i
] == ',') {
2097 if (i
- start
> 255) {
2101 out
[start
] = i
- start
;
2112 app_isdir(const char *name
)
2116 if (stat(name
, &st
) == 0)
2117 return S_ISDIR(st
.st_mode
);
2121 #define OPTION_WIDTH 18
2124 options_usage(struct option
*opts
)
2130 for (i
= 0; opts
[i
].name
!= NULL
; i
++) {
2131 if (opts
[i
].desc
== NULL
)
2134 snprintf(optstr
, sizeof(optstr
), "-%s %s", opts
[i
].name
,
2135 (opts
[i
].argname
!= NULL
) ? opts
[i
].argname
: "");
2136 fprintf(stderr
, " %-*s", OPTION_WIDTH
, optstr
);
2137 if (strlen(optstr
) > OPTION_WIDTH
)
2138 fprintf(stderr
, "\n %-*s", OPTION_WIDTH
, "");
2141 while ((q
= strchr(p
, '\n')) != NULL
) {
2142 fprintf(stderr
, " %.*s", (int)(q
- p
), p
);
2143 fprintf(stderr
, "\n %-*s", OPTION_WIDTH
, "");
2146 fprintf(stderr
, " %s\n", p
);
2151 options_parse(int argc
, char **argv
, struct option
*opts
, char **unnamed
,
2162 if (unnamed
!= NULL
)
2165 for (i
= 1; i
< argc
; i
++) {
2168 /* Single unnamed argument (without leading hyphen). */
2170 if (argsused
!= NULL
)
2172 if (unnamed
== NULL
)
2174 if (*unnamed
!= NULL
)
2180 /* End of named options (single hyphen). */
2184 if (argsused
!= NULL
)
2186 if (unnamed
!= NULL
&& i
== argc
- 1) {
2187 if (*unnamed
!= NULL
)
2195 /* See if there is a matching option... */
2196 for (j
= 0; opts
[j
].name
!= NULL
; j
++) {
2197 if (strcmp(p
, opts
[j
].name
) == 0)
2201 if (opt
->name
== NULL
&& opt
->type
== 0)
2204 if (opt
->type
== OPTION_ARG
||
2205 opt
->type
== OPTION_ARG_FORMAT
||
2206 opt
->type
== OPTION_ARG_FUNC
||
2207 opt
->type
== OPTION_ARG_INT
||
2208 opt
->type
== OPTION_ARG_LONG
||
2209 opt
->type
== OPTION_ARG_TIME
) {
2211 fprintf(stderr
, "missing %s argument for -%s\n",
2212 opt
->argname
, opt
->name
);
2217 switch (opt
->type
) {
2219 *opt
->opt
.arg
= argv
[i
];
2222 case OPTION_ARGV_FUNC
:
2223 if (opt
->opt
.argvfunc(argc
- i
, &argv
[i
], &used
) != 0)
2228 case OPTION_ARG_FORMAT
:
2229 fmt
= str2fmt(argv
[i
]);
2230 if (fmt
== FORMAT_UNDEF
) {
2231 fprintf(stderr
, "unknown %s '%s' for -%s\n",
2232 opt
->argname
, argv
[i
], opt
->name
);
2235 *opt
->opt
.value
= fmt
;
2238 case OPTION_ARG_FUNC
:
2239 if (opt
->opt
.argfunc(argv
[i
]) != 0)
2243 case OPTION_ARG_INT
:
2244 val
= strtonum(argv
[i
], 0, INT_MAX
, &errstr
);
2245 if (errstr
!= NULL
) {
2246 fprintf(stderr
, "%s %s argument for -%s\n",
2247 errstr
, opt
->argname
, opt
->name
);
2250 *opt
->opt
.value
= (int)val
;
2253 case OPTION_ARG_LONG
:
2254 val
= strtonum(argv
[i
], 0, LONG_MAX
, &errstr
);
2255 if (errstr
!= NULL
) {
2256 fprintf(stderr
, "%s %s argument for -%s\n",
2257 errstr
, opt
->argname
, opt
->name
);
2260 *opt
->opt
.lvalue
= (long)val
;
2263 case OPTION_ARG_TIME
:
2264 val
= strtonum(argv
[i
], 0, LLONG_MAX
, &errstr
);
2265 if (errstr
!= NULL
) {
2266 fprintf(stderr
, "%s %s argument for -%s\n",
2267 errstr
, opt
->argname
, opt
->name
);
2270 *opt
->opt
.tvalue
= val
;
2273 case OPTION_DISCARD
:
2277 if (opt
->opt
.func() != 0)
2285 case OPTION_FLAG_ORD
:
2286 *opt
->opt
.flag
= ++ord
;
2290 *opt
->opt
.value
= opt
->value
;
2293 case OPTION_VALUE_AND
:
2294 *opt
->opt
.value
&= opt
->value
;
2297 case OPTION_VALUE_OR
:
2298 *opt
->opt
.value
|= opt
->value
;
2302 fprintf(stderr
, "option %s - unknown type %i\n",
2303 opt
->name
, opt
->type
);
2309 if (argsused
!= NULL
)
2315 fprintf(stderr
, "too many arguments\n");
2319 fprintf(stderr
, "unknown option '%s'\n", arg
);