sl@0: /* apps/apps.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: #include sl@0: #endif sl@0: #ifndef OPENSSL_NO_RSA sl@0: #include sl@0: #endif sl@0: #include sl@0: sl@0: #define NON_MAIN sl@0: #include "apps.h" sl@0: #undef NON_MAIN sl@0: sl@0: typedef struct { sl@0: const char *name; sl@0: unsigned long flag; sl@0: unsigned long mask; sl@0: } NAME_EX_TBL; sl@0: sl@0: static UI_METHOD *ui_method = NULL; sl@0: sl@0: static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl); sl@0: static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl); sl@0: sl@0: #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA) sl@0: /* Looks like this stuff is worth moving into separate function */ sl@0: static EVP_PKEY * sl@0: load_netscape_key(BIO *err, BIO *key, const char *file, sl@0: const char *key_descrip, int format); sl@0: #endif sl@0: sl@0: int app_init(long mesgwin); sl@0: #ifdef undef /* never finished - probably never will be :-) */ sl@0: int args_from_file(char *file, int *argc, char **argv[]) sl@0: { sl@0: FILE *fp; sl@0: int num,i; sl@0: unsigned int len; sl@0: static char *buf=NULL; sl@0: static char **arg=NULL; sl@0: char *p; sl@0: struct stat stbuf; sl@0: sl@0: if (stat(file,&stbuf) < 0) return(0); sl@0: sl@0: fp=fopen(file,"r"); sl@0: if (fp == NULL) sl@0: return(0); sl@0: sl@0: *argc=0; sl@0: *argv=NULL; sl@0: sl@0: len=(unsigned int)stbuf.st_size; sl@0: if (buf != NULL) OPENSSL_free(buf); sl@0: buf=(char *)OPENSSL_malloc(len+1); sl@0: if (buf == NULL) return(0); sl@0: sl@0: len=fread(buf,1,len,fp); sl@0: if (len <= 1) return(0); sl@0: buf[len]='\0'; sl@0: sl@0: i=0; sl@0: for (p=buf; *p; p++) sl@0: if (*p == '\n') i++; sl@0: if (arg != NULL) OPENSSL_free(arg); sl@0: arg=(char **)OPENSSL_malloc(sizeof(char *)*(i*2)); sl@0: sl@0: *argv=arg; sl@0: num=0; sl@0: p=buf; sl@0: for (;;) sl@0: { sl@0: if (!*p) break; sl@0: if (*p == '#') /* comment line */ sl@0: { sl@0: while (*p && (*p != '\n')) p++; sl@0: continue; sl@0: } sl@0: /* else we have a line */ sl@0: *(arg++)=p; sl@0: num++; sl@0: while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n'))) sl@0: p++; sl@0: if (!*p) break; sl@0: if (*p == '\n') sl@0: { sl@0: *(p++)='\0'; sl@0: continue; sl@0: } sl@0: /* else it is a tab or space */ sl@0: p++; sl@0: while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n'))) sl@0: p++; sl@0: if (!*p) break; sl@0: if (*p == '\n') sl@0: { sl@0: p++; sl@0: continue; sl@0: } sl@0: *(arg++)=p++; sl@0: num++; sl@0: while (*p && (*p != '\n')) p++; sl@0: if (!*p) break; sl@0: /* else *p == '\n' */ sl@0: *(p++)='\0'; sl@0: } sl@0: *argc=num; sl@0: return(1); sl@0: } sl@0: #endif sl@0: sl@0: int str2fmt(char *s) sl@0: { sl@0: if ((*s == 'D') || (*s == 'd')) sl@0: return(FORMAT_ASN1); sl@0: else if ((*s == 'T') || (*s == 't')) sl@0: return(FORMAT_TEXT); sl@0: else if ((*s == 'P') || (*s == 'p')) sl@0: return(FORMAT_PEM); sl@0: else if ((*s == 'N') || (*s == 'n')) sl@0: return(FORMAT_NETSCAPE); sl@0: else if ((*s == 'S') || (*s == 's')) sl@0: return(FORMAT_SMIME); sl@0: else if ((*s == '1') sl@0: || (strcmp(s,"PKCS12") == 0) || (strcmp(s,"pkcs12") == 0) sl@0: || (strcmp(s,"P12") == 0) || (strcmp(s,"p12") == 0)) sl@0: return(FORMAT_PKCS12); sl@0: else if ((*s == 'E') || (*s == 'e')) sl@0: return(FORMAT_ENGINE); sl@0: else sl@0: return(FORMAT_UNDEF); sl@0: } sl@0: sl@0: #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) || defined(OPENSSL_SYS_NETWARE) sl@0: svoid program_name(char *in, char *out, int size) sl@0: { sl@0: int i,n; sl@0: char *p=NULL; sl@0: sl@0: n=strlen(in); sl@0: /* find the last '/', '\' or ':' */ sl@0: for (i=n-1; i>0; i--) sl@0: { sl@0: if ((in[i] == '/') || (in[i] == '\\') || (in[i] == ':')) sl@0: { sl@0: p= &(in[i+1]); sl@0: break; sl@0: } sl@0: } sl@0: if (p == NULL) sl@0: p=in; sl@0: n=strlen(p); sl@0: sl@0: #if defined(OPENSSL_SYS_NETWARE) sl@0: /* strip off trailing .nlm if present. */ sl@0: if ((n > 4) && (p[n-4] == '.') && sl@0: ((p[n-3] == 'n') || (p[n-3] == 'N')) && sl@0: ((p[n-2] == 'l') || (p[n-2] == 'L')) && sl@0: ((p[n-1] == 'm') || (p[n-1] == 'M'))) sl@0: n-=4; sl@0: #else sl@0: /* strip off trailing .exe if present. */ sl@0: if ((n > 4) && (p[n-4] == '.') && sl@0: ((p[n-3] == 'e') || (p[n-3] == 'E')) && sl@0: ((p[n-2] == 'x') || (p[n-2] == 'X')) && sl@0: ((p[n-1] == 'e') || (p[n-1] == 'E'))) sl@0: n-=4; sl@0: #endif sl@0: sl@0: if (n > size-1) sl@0: n=size-1; sl@0: sl@0: for (i=0; i= 'A') && (p[i] <= 'Z')) sl@0: out[i]=p[i]-'A'+'a'; sl@0: else sl@0: out[i]=p[i]; sl@0: } sl@0: out[n]='\0'; sl@0: } sl@0: #else sl@0: #ifdef OPENSSL_SYS_VMS sl@0: svoid program_name(char *in, char *out, int size) sl@0: { sl@0: char *p=in, *q; sl@0: char *chars=":]>"; sl@0: sl@0: while(*chars != '\0') sl@0: { sl@0: q=strrchr(p,*chars); sl@0: if (q > p) sl@0: p = q + 1; sl@0: chars++; sl@0: } sl@0: sl@0: q=strrchr(p,'.'); sl@0: if (q == NULL) sl@0: q = p + strlen(p); sl@0: strncpy(out,p,size-1); sl@0: if (q-p >= size) sl@0: { sl@0: out[size-1]='\0'; sl@0: } sl@0: else sl@0: { sl@0: out[q-p]='\0'; sl@0: } sl@0: } sl@0: #else sl@0: void program_name(char *in, char *out, int size) sl@0: { sl@0: char *p; sl@0: sl@0: p=strrchr(in,'/'); sl@0: if (p != NULL) sl@0: p++; sl@0: else sl@0: p=in; sl@0: BUF_strlcpy(out,p,size); sl@0: } sl@0: #endif sl@0: #endif sl@0: sl@0: int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[]) sl@0: { sl@0: int num,len,i; sl@0: char *p; sl@0: sl@0: *argc=0; sl@0: *argv=NULL; sl@0: sl@0: len=strlen(buf); sl@0: i=0; sl@0: if (arg->count == 0) sl@0: { sl@0: arg->count=20; sl@0: arg->data=(char **)OPENSSL_malloc(sizeof(char *)*arg->count); sl@0: } sl@0: for (i=0; icount; i++) sl@0: arg->data[i]=NULL; sl@0: sl@0: num=0; sl@0: p=buf; sl@0: for (;;) sl@0: { sl@0: /* first scan over white space */ sl@0: if (!*p) break; sl@0: while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n'))) sl@0: p++; sl@0: if (!*p) break; sl@0: sl@0: /* The start of something good :-) */ sl@0: if (num >= arg->count) sl@0: { sl@0: char **tmp_p; sl@0: int tlen = arg->count + 20; sl@0: tmp_p = (char **)OPENSSL_realloc(arg->data, sl@0: sizeof(char *)*tlen); sl@0: if (tmp_p == NULL) sl@0: return 0; sl@0: arg->data = tmp_p; sl@0: arg->count = tlen; sl@0: /* initialize newly allocated data */ sl@0: for (i = num; i < arg->count; i++) sl@0: arg->data[i] = NULL; sl@0: } sl@0: arg->data[num++]=p; sl@0: sl@0: /* now look for the end of this */ sl@0: if ((*p == '\'') || (*p == '\"')) /* scan for closing quote */ sl@0: { sl@0: i= *(p++); sl@0: arg->data[num-1]++; /* jump over quote */ sl@0: while (*p && (*p != i)) sl@0: p++; sl@0: *p='\0'; sl@0: } sl@0: else sl@0: { sl@0: while (*p && ((*p != ' ') && sl@0: (*p != '\t') && (*p != '\n'))) sl@0: p++; sl@0: sl@0: if (*p == '\0') sl@0: p--; sl@0: else sl@0: *p='\0'; sl@0: } sl@0: p++; sl@0: } sl@0: *argc=num; sl@0: *argv=arg->data; sl@0: return(1); sl@0: } sl@0: sl@0: #ifndef APP_INIT sl@0: int app_init(long mesgwin) sl@0: { sl@0: return(1); sl@0: } sl@0: #endif sl@0: sl@0: sl@0: int dump_cert_text (BIO *out, X509 *x) sl@0: { sl@0: char *p; sl@0: sl@0: p=X509_NAME_oneline(X509_get_subject_name(x),NULL,0); sl@0: BIO_puts(out,"subject="); sl@0: BIO_puts(out,p); sl@0: OPENSSL_free(p); sl@0: sl@0: p=X509_NAME_oneline(X509_get_issuer_name(x),NULL,0); sl@0: BIO_puts(out,"\nissuer="); sl@0: BIO_puts(out,p); sl@0: BIO_puts(out,"\n"); sl@0: OPENSSL_free(p); sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: static int ui_open(UI *ui) sl@0: { sl@0: return UI_method_get_opener(UI_OpenSSL())(ui); sl@0: } sl@0: static int ui_read(UI *ui, UI_STRING *uis) sl@0: { sl@0: if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD sl@0: && UI_get0_user_data(ui)) sl@0: { sl@0: switch(UI_get_string_type(uis)) sl@0: { sl@0: case UIT_PROMPT: sl@0: case UIT_VERIFY: sl@0: { sl@0: const char *password = sl@0: ((PW_CB_DATA *)UI_get0_user_data(ui))->password; sl@0: if (password && password[0] != '\0') sl@0: { sl@0: UI_set_result(ui, uis, password); sl@0: return 1; sl@0: } sl@0: } sl@0: default: sl@0: break; sl@0: } sl@0: } sl@0: return UI_method_get_reader(UI_OpenSSL())(ui, uis); sl@0: } sl@0: static int ui_write(UI *ui, UI_STRING *uis) sl@0: { sl@0: if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD sl@0: && UI_get0_user_data(ui)) sl@0: { sl@0: switch(UI_get_string_type(uis)) sl@0: { sl@0: case UIT_PROMPT: sl@0: case UIT_VERIFY: sl@0: { sl@0: const char *password = sl@0: ((PW_CB_DATA *)UI_get0_user_data(ui))->password; sl@0: if (password && password[0] != '\0') sl@0: return 1; sl@0: } sl@0: default: sl@0: break; sl@0: } sl@0: } sl@0: return UI_method_get_writer(UI_OpenSSL())(ui, uis); sl@0: } sl@0: static int ui_close(UI *ui) sl@0: { sl@0: return UI_method_get_closer(UI_OpenSSL())(ui); sl@0: } sl@0: int setup_ui_method(void) sl@0: { sl@0: ui_method = UI_create_method("OpenSSL application user interface"); sl@0: UI_method_set_opener(ui_method, ui_open); sl@0: UI_method_set_reader(ui_method, ui_read); sl@0: UI_method_set_writer(ui_method, ui_write); sl@0: UI_method_set_closer(ui_method, ui_close); sl@0: return 0; sl@0: } sl@0: void destroy_ui_method(void) sl@0: { sl@0: if(ui_method) sl@0: { sl@0: UI_destroy_method(ui_method); sl@0: ui_method = NULL; sl@0: } sl@0: } sl@0: int password_callback(char *buf, int bufsiz, int verify, sl@0: PW_CB_DATA *cb_tmp) sl@0: { sl@0: UI *ui = NULL; sl@0: int res = 0; sl@0: const char *prompt_info = NULL; sl@0: const char *password = NULL; sl@0: PW_CB_DATA *cb_data = (PW_CB_DATA *)cb_tmp; sl@0: sl@0: if (cb_data) sl@0: { sl@0: if (cb_data->password) sl@0: password = cb_data->password; sl@0: if (cb_data->prompt_info) sl@0: prompt_info = cb_data->prompt_info; sl@0: } sl@0: sl@0: if (password) sl@0: { sl@0: res = strlen(password); sl@0: if (res > bufsiz) sl@0: res = bufsiz; sl@0: memcpy(buf, password, res); sl@0: return res; sl@0: } sl@0: sl@0: ui = UI_new_method(ui_method); sl@0: if (ui) sl@0: { sl@0: int ok = 0; sl@0: char *buff = NULL; sl@0: int ui_flags = 0; sl@0: char *prompt = NULL; sl@0: sl@0: prompt = UI_construct_prompt(ui, "pass phrase", sl@0: prompt_info); sl@0: sl@0: ui_flags |= UI_INPUT_FLAG_DEFAULT_PWD; sl@0: UI_ctrl(ui, UI_CTRL_PRINT_ERRORS, 1, 0, 0); sl@0: sl@0: if (ok >= 0) sl@0: ok = UI_add_input_string(ui,prompt,ui_flags,buf, sl@0: PW_MIN_LENGTH,BUFSIZ-1); sl@0: if (ok >= 0 && verify) sl@0: { sl@0: buff = (char *)OPENSSL_malloc(bufsiz); sl@0: ok = UI_add_verify_string(ui,prompt,ui_flags,buff, sl@0: PW_MIN_LENGTH,BUFSIZ-1, buf); sl@0: } sl@0: if (ok >= 0) sl@0: do sl@0: { sl@0: ok = UI_process(ui); sl@0: } sl@0: while (ok < 0 && UI_ctrl(ui, UI_CTRL_IS_REDOABLE, 0, 0, 0)); sl@0: sl@0: if (buff) sl@0: { sl@0: OPENSSL_cleanse(buff,(unsigned int)bufsiz); sl@0: OPENSSL_free(buff); sl@0: } sl@0: sl@0: if (ok >= 0) sl@0: res = strlen(buf); sl@0: if (ok == -1) sl@0: { sl@0: BIO_printf(bio_err, "User interface error\n"); sl@0: ERR_print_errors(bio_err); sl@0: OPENSSL_cleanse(buf,(unsigned int)bufsiz); sl@0: res = 0; sl@0: } sl@0: if (ok == -2) sl@0: { sl@0: BIO_printf(bio_err,"aborted!\n"); sl@0: OPENSSL_cleanse(buf,(unsigned int)bufsiz); sl@0: res = 0; sl@0: } sl@0: UI_free(ui); sl@0: OPENSSL_free(prompt); sl@0: } sl@0: return res; sl@0: } sl@0: sl@0: static char *app_get_pass(BIO *err, char *arg, int keepbio); sl@0: sl@0: int app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2) sl@0: { sl@0: int same; sl@0: if(!arg2 || !arg1 || strcmp(arg1, arg2)) same = 0; sl@0: else same = 1; sl@0: if(arg1) { sl@0: *pass1 = app_get_pass(err, arg1, same); sl@0: if(!*pass1) return 0; sl@0: } else if(pass1) *pass1 = NULL; sl@0: if(arg2) { sl@0: *pass2 = app_get_pass(err, arg2, same ? 2 : 0); sl@0: if(!*pass2) return 0; sl@0: } else if(pass2) *pass2 = NULL; sl@0: return 1; sl@0: } sl@0: sl@0: static char *app_get_pass(BIO *err, char *arg, int keepbio) sl@0: { sl@0: char *tmp, tpass[APP_PASS_LEN]; sl@0: static BIO *pwdbio = NULL; sl@0: int i; sl@0: if(!strncmp(arg, "pass:", 5)) return BUF_strdup(arg + 5); sl@0: if(!strncmp(arg, "env:", 4)) { sl@0: tmp = getenv(arg + 4); sl@0: if(!tmp) { sl@0: BIO_printf(err, "Can't read environment variable %s\n", arg + 4); sl@0: return NULL; sl@0: } sl@0: return BUF_strdup(tmp); sl@0: } sl@0: if(!keepbio || !pwdbio) { sl@0: if(!strncmp(arg, "file:", 5)) { sl@0: pwdbio = BIO_new_file(arg + 5, "r"); sl@0: if(!pwdbio) { sl@0: BIO_printf(err, "Can't open file %s\n", arg + 5); sl@0: return NULL; sl@0: } sl@0: } else if(!strncmp(arg, "fd:", 3)) { sl@0: BIO *btmp; sl@0: i = atoi(arg + 3); sl@0: if(i >= 0) pwdbio = BIO_new_fd(i, BIO_NOCLOSE); sl@0: if((i < 0) || !pwdbio) { sl@0: BIO_printf(err, "Can't access file descriptor %s\n", arg + 3); sl@0: return NULL; sl@0: } sl@0: /* Can't do BIO_gets on an fd BIO so add a buffering BIO */ sl@0: btmp = BIO_new(BIO_f_buffer()); sl@0: pwdbio = BIO_push(btmp, pwdbio); sl@0: } else sl@0: if(!strcmp(arg, "stdin")) { sl@0: pwdbio = BIO_new_fp(stdin, BIO_NOCLOSE); sl@0: if(!pwdbio) { sl@0: BIO_printf(err, "Can't open BIO for stdin\n"); sl@0: return NULL; sl@0: } sl@0: sl@0: } else { sl@0: BIO_printf(err, "Invalid password argument \"%s\"\n", arg); sl@0: return NULL; sl@0: } sl@0: } sl@0: i = BIO_gets(pwdbio, tpass, APP_PASS_LEN); sl@0: if(keepbio != 1) { sl@0: BIO_free_all(pwdbio); sl@0: pwdbio = NULL; sl@0: } sl@0: if(i <= 0) { sl@0: BIO_printf(err, "Error reading password from BIO\n"); sl@0: return NULL; sl@0: } sl@0: tmp = strchr(tpass, '\n'); sl@0: if(tmp) *tmp = 0; sl@0: return BUF_strdup(tpass); sl@0: } sl@0: sl@0: int add_oid_section(BIO *err, CONF *conf) sl@0: { sl@0: char *p; sl@0: STACK_OF(CONF_VALUE) *sktmp; sl@0: CONF_VALUE *cnf; sl@0: int i; sl@0: if(!(p=NCONF_get_string(conf,NULL,"oid_section"))) sl@0: { sl@0: ERR_clear_error(); sl@0: return 1; sl@0: } sl@0: if(!(sktmp = NCONF_get_section(conf, p))) { sl@0: BIO_printf(err, "problem loading oid section %s\n", p); sl@0: return 0; sl@0: } sl@0: for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++) { sl@0: cnf = sk_CONF_VALUE_value(sktmp, i); sl@0: if(OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) { sl@0: BIO_printf(err, "problem creating object %s=%s\n", sl@0: cnf->name, cnf->value); sl@0: return 0; sl@0: } sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: static int load_pkcs12(BIO *err, BIO *in, const char *desc, sl@0: pem_password_cb *pem_cb, void *cb_data, sl@0: EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca) sl@0: { sl@0: const char *pass; sl@0: char tpass[PEM_BUFSIZE]; sl@0: int len, ret = 0; sl@0: PKCS12 *p12; sl@0: p12 = d2i_PKCS12_bio(in, NULL); sl@0: if (p12 == NULL) sl@0: { sl@0: BIO_printf(err, "Error loading PKCS12 file for %s\n", desc); sl@0: goto die; sl@0: } sl@0: /* See if an empty password will do */ sl@0: if (PKCS12_verify_mac(p12, "", 0) || PKCS12_verify_mac(p12, NULL, 0)) sl@0: pass = ""; sl@0: else sl@0: { sl@0: if (!pem_cb) sl@0: pem_cb = (pem_password_cb *)password_callback; sl@0: len = pem_cb(tpass, PEM_BUFSIZE, 0, cb_data); sl@0: if (len < 0) sl@0: { sl@0: BIO_printf(err, "Passpharse callback error for %s\n", sl@0: desc); sl@0: goto die; sl@0: } sl@0: if (len < PEM_BUFSIZE) sl@0: tpass[len] = 0; sl@0: if (!PKCS12_verify_mac(p12, tpass, len)) sl@0: { sl@0: BIO_printf(err, sl@0: "Mac verify error (wrong password?) in PKCS12 file for %s\n", desc); sl@0: goto die; sl@0: } sl@0: pass = tpass; sl@0: } sl@0: ret = PKCS12_parse(p12, pass, pkey, cert, ca); sl@0: die: sl@0: if (p12) sl@0: PKCS12_free(p12); sl@0: return ret; sl@0: } sl@0: sl@0: X509 *load_cert(BIO *err, const char *file, int format, sl@0: const char *pass, ENGINE *e, const char *cert_descrip) sl@0: { sl@0: ASN1_HEADER *ah=NULL; sl@0: BUF_MEM *buf=NULL; sl@0: X509 *x=NULL; sl@0: BIO *cert; sl@0: sl@0: if ((cert=BIO_new(BIO_s_file())) == NULL) sl@0: { sl@0: ERR_print_errors(err); sl@0: goto end; sl@0: } sl@0: sl@0: if (file == NULL) sl@0: { sl@0: setvbuf(stdin, NULL, _IONBF, 0); sl@0: BIO_set_fp(cert,stdin,BIO_NOCLOSE); sl@0: } sl@0: else sl@0: { sl@0: if (BIO_read_filename(cert,file) <= 0) sl@0: { sl@0: BIO_printf(err, "Error opening %s %s\n", sl@0: cert_descrip, file); sl@0: ERR_print_errors(err); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: if (format == FORMAT_ASN1) sl@0: x=d2i_X509_bio(cert,NULL); sl@0: else if (format == FORMAT_NETSCAPE) sl@0: { sl@0: const unsigned char *p,*op; sl@0: int size=0,i; sl@0: sl@0: /* We sort of have to do it this way because it is sort of nice sl@0: * to read the header first and check it, then sl@0: * try to read the certificate */ sl@0: buf=BUF_MEM_new(); sl@0: for (;;) sl@0: { sl@0: if ((buf == NULL) || (!BUF_MEM_grow(buf,size+1024*10))) sl@0: goto end; sl@0: i=BIO_read(cert,&(buf->data[size]),1024*10); sl@0: size+=i; sl@0: if (i == 0) break; sl@0: if (i < 0) sl@0: { sl@0: perror("reading certificate"); sl@0: goto end; sl@0: } sl@0: } sl@0: p=(unsigned char *)buf->data; sl@0: op=p; sl@0: sl@0: /* First load the header */ sl@0: if ((ah=d2i_ASN1_HEADER(NULL,&p,(long)size)) == NULL) sl@0: goto end; sl@0: if ((ah->header == NULL) || (ah->header->data == NULL) || sl@0: (strncmp(NETSCAPE_CERT_HDR,(char *)ah->header->data, sl@0: ah->header->length) != 0)) sl@0: { sl@0: BIO_printf(err,"Error reading header on certificate\n"); sl@0: goto end; sl@0: } sl@0: /* header is ok, so now read the object */ sl@0: p=op; sl@0: ah->meth=X509_asn1_meth(); sl@0: if ((ah=d2i_ASN1_HEADER(&ah,&p,(long)size)) == NULL) sl@0: goto end; sl@0: x=(X509 *)ah->data; sl@0: ah->data=NULL; sl@0: } sl@0: else if (format == FORMAT_PEM) sl@0: x=PEM_read_bio_X509_AUX(cert,NULL, sl@0: (pem_password_cb *)password_callback, NULL); sl@0: else if (format == FORMAT_PKCS12) sl@0: { sl@0: if (!load_pkcs12(err, cert,cert_descrip, NULL, NULL, sl@0: NULL, &x, NULL)) sl@0: goto end; sl@0: } sl@0: else { sl@0: BIO_printf(err,"bad input format specified for %s\n", sl@0: cert_descrip); sl@0: goto end; sl@0: } sl@0: end: sl@0: if (x == NULL) sl@0: { sl@0: BIO_printf(err,"unable to load certificate\n"); sl@0: ERR_print_errors(err); sl@0: } sl@0: if (ah != NULL) ASN1_HEADER_free(ah); sl@0: if (cert != NULL) BIO_free(cert); sl@0: if (buf != NULL) BUF_MEM_free(buf); sl@0: return(x); sl@0: } sl@0: sl@0: EVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin, sl@0: const char *pass, ENGINE *e, const char *key_descrip) sl@0: { sl@0: BIO *key=NULL; sl@0: EVP_PKEY *pkey=NULL; sl@0: PW_CB_DATA cb_data; sl@0: sl@0: cb_data.password = pass; sl@0: cb_data.prompt_info = file; sl@0: sl@0: if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE)) sl@0: { sl@0: BIO_printf(err,"no keyfile specified\n"); sl@0: goto end; sl@0: } sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: if (format == FORMAT_ENGINE) sl@0: { sl@0: if (!e) sl@0: BIO_printf(bio_err,"no engine specified\n"); sl@0: else sl@0: pkey = ENGINE_load_private_key(e, file, sl@0: ui_method, &cb_data); sl@0: goto end; sl@0: } sl@0: #endif sl@0: key=BIO_new(BIO_s_file()); sl@0: if (key == NULL) sl@0: { sl@0: ERR_print_errors(err); sl@0: goto end; sl@0: } sl@0: if (file == NULL && maybe_stdin) sl@0: { sl@0: setvbuf(stdin, NULL, _IONBF, 0); sl@0: BIO_set_fp(key,stdin,BIO_NOCLOSE); sl@0: } sl@0: else sl@0: if (BIO_read_filename(key,file) <= 0) sl@0: { sl@0: BIO_printf(err, "Error opening %s %s\n", sl@0: key_descrip, file); sl@0: ERR_print_errors(err); sl@0: goto end; sl@0: } sl@0: if (format == FORMAT_ASN1) sl@0: { sl@0: pkey=d2i_PrivateKey_bio(key, NULL); sl@0: } sl@0: else if (format == FORMAT_PEM) sl@0: { sl@0: pkey=PEM_read_bio_PrivateKey(key,NULL, sl@0: (pem_password_cb *)password_callback, &cb_data); sl@0: } sl@0: #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA) sl@0: else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC) sl@0: pkey = load_netscape_key(err, key, file, key_descrip, format); sl@0: #endif sl@0: else if (format == FORMAT_PKCS12) sl@0: { sl@0: if (!load_pkcs12(err, key, key_descrip, sl@0: (pem_password_cb *)password_callback, &cb_data, sl@0: &pkey, NULL, NULL)) sl@0: goto end; sl@0: } sl@0: else sl@0: { sl@0: BIO_printf(err,"bad input format specified for key file\n"); sl@0: goto end; sl@0: } sl@0: end: sl@0: if (key != NULL) BIO_free(key); sl@0: if (pkey == NULL) sl@0: BIO_printf(err,"unable to load %s\n", key_descrip); sl@0: return(pkey); sl@0: } sl@0: sl@0: EVP_PKEY *load_pubkey(BIO *err, const char *file, int format, int maybe_stdin, sl@0: const char *pass, ENGINE *e, const char *key_descrip) sl@0: { sl@0: BIO *key=NULL; sl@0: EVP_PKEY *pkey=NULL; sl@0: PW_CB_DATA cb_data; sl@0: sl@0: cb_data.password = pass; sl@0: cb_data.prompt_info = file; sl@0: sl@0: if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE)) sl@0: { sl@0: BIO_printf(err,"no keyfile specified\n"); sl@0: goto end; sl@0: } sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: if (format == FORMAT_ENGINE) sl@0: { sl@0: if (!e) sl@0: BIO_printf(bio_err,"no engine specified\n"); sl@0: else sl@0: pkey = ENGINE_load_public_key(e, file, sl@0: ui_method, &cb_data); sl@0: goto end; sl@0: } sl@0: #endif sl@0: key=BIO_new(BIO_s_file()); sl@0: if (key == NULL) sl@0: { sl@0: ERR_print_errors(err); sl@0: goto end; sl@0: } sl@0: if (file == NULL && maybe_stdin) sl@0: { sl@0: setvbuf(stdin, NULL, _IONBF, 0); sl@0: BIO_set_fp(key,stdin,BIO_NOCLOSE); sl@0: } sl@0: else sl@0: if (BIO_read_filename(key,file) <= 0) sl@0: { sl@0: BIO_printf(err, "Error opening %s %s\n", sl@0: key_descrip, file); sl@0: ERR_print_errors(err); sl@0: goto end; sl@0: } sl@0: if (format == FORMAT_ASN1) sl@0: { sl@0: pkey=d2i_PUBKEY_bio(key, NULL); sl@0: } sl@0: else if (format == FORMAT_PEM) sl@0: { sl@0: pkey=PEM_read_bio_PUBKEY(key,NULL, sl@0: (pem_password_cb *)password_callback, &cb_data); sl@0: } sl@0: #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA) sl@0: else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC) sl@0: pkey = load_netscape_key(err, key, file, key_descrip, format); sl@0: #endif sl@0: else sl@0: { sl@0: BIO_printf(err,"bad input format specified for key file\n"); sl@0: goto end; sl@0: } sl@0: end: sl@0: if (key != NULL) BIO_free(key); sl@0: if (pkey == NULL) sl@0: BIO_printf(err,"unable to load %s\n", key_descrip); sl@0: return(pkey); sl@0: } sl@0: sl@0: #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA) sl@0: static EVP_PKEY * sl@0: load_netscape_key(BIO *err, BIO *key, const char *file, sl@0: const char *key_descrip, int format) sl@0: { sl@0: EVP_PKEY *pkey; sl@0: BUF_MEM *buf; sl@0: RSA *rsa; sl@0: const unsigned char *p; sl@0: int size, i; sl@0: sl@0: buf=BUF_MEM_new(); sl@0: pkey = EVP_PKEY_new(); sl@0: size = 0; sl@0: if (buf == NULL || pkey == NULL) sl@0: goto error; sl@0: for (;;) sl@0: { sl@0: if (!BUF_MEM_grow_clean(buf,size+1024*10)) sl@0: goto error; sl@0: i = BIO_read(key, &(buf->data[size]), 1024*10); sl@0: size += i; sl@0: if (i == 0) sl@0: break; sl@0: if (i < 0) sl@0: { sl@0: BIO_printf(err, "Error reading %s %s", sl@0: key_descrip, file); sl@0: goto error; sl@0: } sl@0: } sl@0: p=(unsigned char *)buf->data; sl@0: rsa = d2i_RSA_NET(NULL,&p,(long)size,NULL, sl@0: (format == FORMAT_IISSGC ? 1 : 0)); sl@0: if (rsa == NULL) sl@0: goto error; sl@0: BUF_MEM_free(buf); sl@0: EVP_PKEY_set1_RSA(pkey, rsa); sl@0: return pkey; sl@0: error: sl@0: BUF_MEM_free(buf); sl@0: EVP_PKEY_free(pkey); sl@0: return NULL; sl@0: } sl@0: #endif /* ndef OPENSSL_NO_RC4 */ sl@0: sl@0: STACK_OF(X509) *load_certs(BIO *err, const char *file, int format, sl@0: const char *pass, ENGINE *e, const char *cert_descrip) sl@0: { sl@0: BIO *certs; sl@0: int i; sl@0: STACK_OF(X509) *othercerts = NULL; sl@0: STACK_OF(X509_INFO) *allcerts = NULL; sl@0: X509_INFO *xi; sl@0: PW_CB_DATA cb_data; sl@0: sl@0: cb_data.password = pass; sl@0: cb_data.prompt_info = file; sl@0: sl@0: if((certs = BIO_new(BIO_s_file())) == NULL) sl@0: { sl@0: ERR_print_errors(err); sl@0: goto end; sl@0: } sl@0: sl@0: if (file == NULL) sl@0: BIO_set_fp(certs,stdin,BIO_NOCLOSE); sl@0: else sl@0: { sl@0: if (BIO_read_filename(certs,file) <= 0) sl@0: { sl@0: BIO_printf(err, "Error opening %s %s\n", sl@0: cert_descrip, file); sl@0: ERR_print_errors(err); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: if (format == FORMAT_PEM) sl@0: { sl@0: othercerts = sk_X509_new_null(); sl@0: if(!othercerts) sl@0: { sl@0: sk_X509_free(othercerts); sl@0: othercerts = NULL; sl@0: goto end; sl@0: } sl@0: allcerts = PEM_X509_INFO_read_bio(certs, NULL, sl@0: (pem_password_cb *)password_callback, &cb_data); sl@0: for(i = 0; i < sk_X509_INFO_num(allcerts); i++) sl@0: { sl@0: xi = sk_X509_INFO_value (allcerts, i); sl@0: if (xi->x509) sl@0: { sl@0: sk_X509_push(othercerts, xi->x509); sl@0: xi->x509 = NULL; sl@0: } sl@0: } sl@0: goto end; sl@0: } sl@0: else { sl@0: BIO_printf(err,"bad input format specified for %s\n", sl@0: cert_descrip); sl@0: goto end; sl@0: } sl@0: end: sl@0: if (othercerts == NULL) sl@0: { sl@0: BIO_printf(err,"unable to load certificates\n"); sl@0: ERR_print_errors(err); sl@0: } sl@0: if (allcerts) sk_X509_INFO_pop_free(allcerts, X509_INFO_free); sl@0: if (certs != NULL) BIO_free(certs); sl@0: return(othercerts); sl@0: } sl@0: sl@0: sl@0: #define X509V3_EXT_UNKNOWN_MASK (0xfL << 16) sl@0: /* Return error for unknown extensions */ sl@0: #define X509V3_EXT_DEFAULT 0 sl@0: /* Print error for unknown extensions */ sl@0: #define X509V3_EXT_ERROR_UNKNOWN (1L << 16) sl@0: /* ASN1 parse unknown extensions */ sl@0: #define X509V3_EXT_PARSE_UNKNOWN (2L << 16) sl@0: /* BIO_dump unknown extensions */ sl@0: #define X509V3_EXT_DUMP_UNKNOWN (3L << 16) sl@0: sl@0: #define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \ sl@0: X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION) sl@0: sl@0: int set_cert_ex(unsigned long *flags, const char *arg) sl@0: { sl@0: static const NAME_EX_TBL cert_tbl[] = { sl@0: { "compatible", X509_FLAG_COMPAT, 0xffffffffl}, sl@0: { "ca_default", X509_FLAG_CA, 0xffffffffl}, sl@0: { "no_header", X509_FLAG_NO_HEADER, 0}, sl@0: { "no_version", X509_FLAG_NO_VERSION, 0}, sl@0: { "no_serial", X509_FLAG_NO_SERIAL, 0}, sl@0: { "no_signame", X509_FLAG_NO_SIGNAME, 0}, sl@0: { "no_validity", X509_FLAG_NO_VALIDITY, 0}, sl@0: { "no_subject", X509_FLAG_NO_SUBJECT, 0}, sl@0: { "no_issuer", X509_FLAG_NO_ISSUER, 0}, sl@0: { "no_pubkey", X509_FLAG_NO_PUBKEY, 0}, sl@0: { "no_extensions", X509_FLAG_NO_EXTENSIONS, 0}, sl@0: { "no_sigdump", X509_FLAG_NO_SIGDUMP, 0}, sl@0: { "no_aux", X509_FLAG_NO_AUX, 0}, sl@0: { "no_attributes", X509_FLAG_NO_ATTRIBUTES, 0}, sl@0: { "ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK}, sl@0: { "ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, sl@0: { "ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, sl@0: { "ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, sl@0: { NULL, 0, 0} sl@0: }; sl@0: return set_multi_opts(flags, arg, cert_tbl); sl@0: } sl@0: sl@0: int set_name_ex(unsigned long *flags, const char *arg) sl@0: { sl@0: static const NAME_EX_TBL ex_tbl[] = { sl@0: { "esc_2253", ASN1_STRFLGS_ESC_2253, 0}, sl@0: { "esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0}, sl@0: { "esc_msb", ASN1_STRFLGS_ESC_MSB, 0}, sl@0: { "use_quote", ASN1_STRFLGS_ESC_QUOTE, 0}, sl@0: { "utf8", ASN1_STRFLGS_UTF8_CONVERT, 0}, sl@0: { "ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0}, sl@0: { "show_type", ASN1_STRFLGS_SHOW_TYPE, 0}, sl@0: { "dump_all", ASN1_STRFLGS_DUMP_ALL, 0}, sl@0: { "dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0}, sl@0: { "dump_der", ASN1_STRFLGS_DUMP_DER, 0}, sl@0: { "compat", XN_FLAG_COMPAT, 0xffffffffL}, sl@0: { "sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK}, sl@0: { "sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK}, sl@0: { "sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK}, sl@0: { "sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK}, sl@0: { "dn_rev", XN_FLAG_DN_REV, 0}, sl@0: { "nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK}, sl@0: { "sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK}, sl@0: { "lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK}, sl@0: { "align", XN_FLAG_FN_ALIGN, 0}, sl@0: { "oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK}, sl@0: { "space_eq", XN_FLAG_SPC_EQ, 0}, sl@0: { "dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0}, sl@0: { "RFC2253", XN_FLAG_RFC2253, 0xffffffffL}, sl@0: { "oneline", XN_FLAG_ONELINE, 0xffffffffL}, sl@0: { "multiline", XN_FLAG_MULTILINE, 0xffffffffL}, sl@0: { "ca_default", XN_FLAG_MULTILINE, 0xffffffffL}, sl@0: { NULL, 0, 0} sl@0: }; sl@0: return set_multi_opts(flags, arg, ex_tbl); sl@0: } sl@0: sl@0: int set_ext_copy(int *copy_type, const char *arg) sl@0: { sl@0: if (!strcasecmp(arg, "none")) sl@0: *copy_type = EXT_COPY_NONE; sl@0: else if (!strcasecmp(arg, "copy")) sl@0: *copy_type = EXT_COPY_ADD; sl@0: else if (!strcasecmp(arg, "copyall")) sl@0: *copy_type = EXT_COPY_ALL; sl@0: else sl@0: return 0; sl@0: return 1; sl@0: } sl@0: sl@0: int copy_extensions(X509 *x, X509_REQ *req, int copy_type) sl@0: { sl@0: STACK_OF(X509_EXTENSION) *exts = NULL; sl@0: X509_EXTENSION *ext, *tmpext; sl@0: ASN1_OBJECT *obj; sl@0: int i, idx, ret = 0; sl@0: if (!x || !req || (copy_type == EXT_COPY_NONE)) sl@0: return 1; sl@0: exts = X509_REQ_get_extensions(req); sl@0: sl@0: for(i = 0; i < sk_X509_EXTENSION_num(exts); i++) { sl@0: ext = sk_X509_EXTENSION_value(exts, i); sl@0: obj = X509_EXTENSION_get_object(ext); sl@0: idx = X509_get_ext_by_OBJ(x, obj, -1); sl@0: /* Does extension exist? */ sl@0: if (idx != -1) { sl@0: /* If normal copy don't override existing extension */ sl@0: if (copy_type == EXT_COPY_ADD) sl@0: continue; sl@0: /* Delete all extensions of same type */ sl@0: do { sl@0: tmpext = X509_get_ext(x, idx); sl@0: X509_delete_ext(x, idx); sl@0: X509_EXTENSION_free(tmpext); sl@0: idx = X509_get_ext_by_OBJ(x, obj, -1); sl@0: } while (idx != -1); sl@0: } sl@0: if (!X509_add_ext(x, ext, -1)) sl@0: goto end; sl@0: } sl@0: sl@0: ret = 1; sl@0: sl@0: end: sl@0: sl@0: sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl) sl@0: { sl@0: STACK_OF(CONF_VALUE) *vals; sl@0: CONF_VALUE *val; sl@0: int i, ret = 1; sl@0: if(!arg) return 0; sl@0: vals = X509V3_parse_list(arg); sl@0: for (i = 0; i < sk_CONF_VALUE_num(vals); i++) { sl@0: val = sk_CONF_VALUE_value(vals, i); sl@0: if (!set_table_opts(flags, val->name, in_tbl)) sl@0: ret = 0; sl@0: } sl@0: sk_CONF_VALUE_pop_free(vals, X509V3_conf_free); sl@0: return ret; sl@0: } sl@0: sl@0: static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl) sl@0: { sl@0: char c; sl@0: const NAME_EX_TBL *ptbl; sl@0: c = arg[0]; sl@0: sl@0: if(c == '-') { sl@0: c = 0; sl@0: arg++; sl@0: } else if (c == '+') { sl@0: c = 1; sl@0: arg++; sl@0: } else c = 1; sl@0: sl@0: for(ptbl = in_tbl; ptbl->name; ptbl++) { sl@0: if(!strcasecmp(arg, ptbl->name)) { sl@0: *flags &= ~ptbl->mask; sl@0: if(c) *flags |= ptbl->flag; sl@0: else *flags &= ~ptbl->flag; sl@0: return 1; sl@0: } sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: void print_name(BIO *out, const char *title, X509_NAME *nm, unsigned long lflags) sl@0: { sl@0: char *buf; sl@0: char mline = 0; sl@0: int indent = 0; sl@0: sl@0: if(title) BIO_puts(out, title); sl@0: if((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) { sl@0: mline = 1; sl@0: indent = 4; sl@0: } sl@0: if(lflags == XN_FLAG_COMPAT) { sl@0: buf = X509_NAME_oneline(nm, 0, 0); sl@0: BIO_puts(out, buf); sl@0: BIO_puts(out, "\n"); sl@0: OPENSSL_free(buf); sl@0: } else { sl@0: if(mline) BIO_puts(out, "\n"); sl@0: X509_NAME_print_ex(out, nm, indent, lflags); sl@0: BIO_puts(out, "\n"); sl@0: } sl@0: } sl@0: sl@0: X509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath) sl@0: { sl@0: X509_STORE *store; sl@0: X509_LOOKUP *lookup; sl@0: if(!(store = X509_STORE_new())) goto end; sl@0: lookup=X509_STORE_add_lookup(store,X509_LOOKUP_file()); sl@0: if (lookup == NULL) goto end; sl@0: if (CAfile) { sl@0: if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) { sl@0: BIO_printf(bp, "Error loading file %s\n", CAfile); sl@0: goto end; sl@0: } sl@0: } else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT); sl@0: sl@0: lookup=X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir()); sl@0: if (lookup == NULL) goto end; sl@0: if (CApath) { sl@0: if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) { sl@0: BIO_printf(bp, "Error loading directory %s\n", CApath); sl@0: goto end; sl@0: } sl@0: } else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT); sl@0: sl@0: ERR_clear_error(); sl@0: return store; sl@0: end: sl@0: X509_STORE_free(store); sl@0: return NULL; sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: /* Try to load an engine in a shareable library */ sl@0: static ENGINE *try_load_engine(BIO *err, const char *engine, int debug) sl@0: { sl@0: ENGINE *e = ENGINE_by_id("dynamic"); sl@0: if (e) sl@0: { sl@0: if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0) sl@0: || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0)) sl@0: { sl@0: ENGINE_free(e); sl@0: e = NULL; sl@0: } sl@0: } sl@0: return e; sl@0: } sl@0: sl@0: ENGINE *setup_engine(BIO *err, const char *engine, int debug) sl@0: { sl@0: ENGINE *e = NULL; sl@0: sl@0: if (engine) sl@0: { sl@0: if(strcmp(engine, "auto") == 0) sl@0: { sl@0: BIO_printf(err,"enabling auto ENGINE support\n"); sl@0: ENGINE_register_all_complete(); sl@0: return NULL; sl@0: } sl@0: if((e = ENGINE_by_id(engine)) == NULL sl@0: && (e = try_load_engine(err, engine, debug)) == NULL) sl@0: { sl@0: BIO_printf(err,"invalid engine \"%s\"\n", engine); sl@0: ERR_print_errors(err); sl@0: return NULL; sl@0: } sl@0: if (debug) sl@0: { sl@0: ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM, sl@0: 0, err, 0); sl@0: } sl@0: ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, ui_method, 0, 1); sl@0: if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) sl@0: { sl@0: BIO_printf(err,"can't use that engine\n"); sl@0: ERR_print_errors(err); sl@0: ENGINE_free(e); sl@0: return NULL; sl@0: } sl@0: sl@0: BIO_printf(err,"engine \"%s\" set.\n", ENGINE_get_id(e)); sl@0: sl@0: /* Free our "structural" reference. */ sl@0: ENGINE_free(e); sl@0: } sl@0: return e; sl@0: } sl@0: #endif sl@0: sl@0: int load_config(BIO *err, CONF *cnf) sl@0: { sl@0: if (!cnf) sl@0: cnf = config; sl@0: if (!cnf) sl@0: return 1; sl@0: sl@0: OPENSSL_load_builtin_modules(); sl@0: sl@0: if (CONF_modules_load(cnf, NULL, 0) <= 0) sl@0: { sl@0: BIO_printf(err, "Error configuring OpenSSL\n"); sl@0: ERR_print_errors(err); sl@0: return 0; sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: char *make_config_name() sl@0: { sl@0: const char *t=X509_get_default_cert_area(); sl@0: size_t len; sl@0: char *p; sl@0: sl@0: len=strlen(t)+strlen(OPENSSL_CONF)+2; sl@0: p=OPENSSL_malloc(len); sl@0: BUF_strlcpy(p,t,len); sl@0: #ifndef OPENSSL_SYS_VMS sl@0: BUF_strlcat(p,"/",len); sl@0: #endif sl@0: BUF_strlcat(p,OPENSSL_CONF,len); sl@0: sl@0: return p; sl@0: } sl@0: sl@0: static unsigned long index_serial_hash(const char **a) sl@0: { sl@0: const char *n; sl@0: sl@0: n=a[DB_serial]; sl@0: while (*n == '0') n++; sl@0: return(lh_strhash(n)); sl@0: } sl@0: sl@0: static int index_serial_cmp(const char **a, const char **b) sl@0: { sl@0: const char *aa,*bb; sl@0: sl@0: for (aa=a[DB_serial]; *aa == '0'; aa++); sl@0: for (bb=b[DB_serial]; *bb == '0'; bb++); sl@0: return(strcmp(aa,bb)); sl@0: } sl@0: sl@0: static int index_name_qual(char **a) sl@0: { return(a[0][0] == 'V'); } sl@0: sl@0: static unsigned long index_name_hash(const char **a) sl@0: { return(lh_strhash(a[DB_name])); } sl@0: sl@0: int index_name_cmp(const char **a, const char **b) sl@0: { return(strcmp(a[DB_name], sl@0: b[DB_name])); } sl@0: sl@0: static IMPLEMENT_LHASH_HASH_FN(index_serial_hash,const char **) sl@0: static IMPLEMENT_LHASH_COMP_FN(index_serial_cmp,const char **) sl@0: static IMPLEMENT_LHASH_HASH_FN(index_name_hash,const char **) sl@0: static IMPLEMENT_LHASH_COMP_FN(index_name_cmp,const char **) sl@0: sl@0: #undef BSIZE sl@0: #define BSIZE 256 sl@0: sl@0: BIGNUM *load_serial(char *serialfile, int create, ASN1_INTEGER **retai) sl@0: { sl@0: BIO *in=NULL; sl@0: BIGNUM *ret=NULL; sl@0: MS_STATIC char buf[1024]; sl@0: ASN1_INTEGER *ai=NULL; sl@0: sl@0: ai=ASN1_INTEGER_new(); sl@0: if (ai == NULL) goto err; sl@0: sl@0: if ((in=BIO_new(BIO_s_file())) == NULL) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto err; sl@0: } sl@0: sl@0: if (BIO_read_filename(in,serialfile) <= 0) sl@0: { sl@0: if (!create) sl@0: { sl@0: perror(serialfile); sl@0: goto err; sl@0: } sl@0: else sl@0: { sl@0: ret=BN_new(); sl@0: if (ret == NULL || !rand_serial(ret, ai)) sl@0: BIO_printf(bio_err, "Out of memory\n"); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: if (!a2i_ASN1_INTEGER(in,ai,buf,1024)) sl@0: { sl@0: BIO_printf(bio_err,"unable to load number from %s\n", sl@0: serialfile); sl@0: goto err; sl@0: } sl@0: ret=ASN1_INTEGER_to_BN(ai,NULL); sl@0: if (ret == NULL) sl@0: { sl@0: BIO_printf(bio_err,"error converting number from bin to BIGNUM\n"); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: if (ret && retai) sl@0: { sl@0: *retai = ai; sl@0: ai = NULL; sl@0: } sl@0: err: sl@0: if (in != NULL) BIO_free(in); sl@0: if (ai != NULL) ASN1_INTEGER_free(ai); sl@0: return(ret); sl@0: } sl@0: sl@0: int save_serial(char *serialfile, char *suffix, BIGNUM *serial, ASN1_INTEGER **retai) sl@0: { sl@0: char buf[1][BSIZE]; sl@0: BIO *out = NULL; sl@0: int ret=0; sl@0: ASN1_INTEGER *ai=NULL; sl@0: int j; sl@0: sl@0: if (suffix == NULL) sl@0: j = strlen(serialfile); sl@0: else sl@0: j = strlen(serialfile) + strlen(suffix) + 1; sl@0: if (j >= BSIZE) sl@0: { sl@0: BIO_printf(bio_err,"file name too long\n"); sl@0: goto err; sl@0: } sl@0: sl@0: if (suffix == NULL) sl@0: BUF_strlcpy(buf[0], serialfile, BSIZE); sl@0: else sl@0: { sl@0: #ifndef OPENSSL_SYS_VMS sl@0: j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", serialfile, suffix); sl@0: #else sl@0: j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", serialfile, suffix); sl@0: #endif sl@0: } sl@0: #ifdef RL_DEBUG sl@0: BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]); sl@0: #endif sl@0: out=BIO_new(BIO_s_file()); sl@0: if (out == NULL) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto err; sl@0: } sl@0: if (BIO_write_filename(out,buf[0]) <= 0) sl@0: { sl@0: perror(serialfile); sl@0: goto err; sl@0: } sl@0: sl@0: if ((ai=BN_to_ASN1_INTEGER(serial,NULL)) == NULL) sl@0: { sl@0: BIO_printf(bio_err,"error converting serial to ASN.1 format\n"); sl@0: goto err; sl@0: } sl@0: i2a_ASN1_INTEGER(out,ai); sl@0: BIO_puts(out,"\n"); sl@0: ret=1; sl@0: if (retai) sl@0: { sl@0: *retai = ai; sl@0: ai = NULL; sl@0: } sl@0: err: sl@0: if (out != NULL) BIO_free_all(out); sl@0: if (ai != NULL) ASN1_INTEGER_free(ai); sl@0: return(ret); sl@0: } sl@0: sl@0: int rotate_serial(char *serialfile, char *new_suffix, char *old_suffix) sl@0: { sl@0: char buf[5][BSIZE]; sl@0: int i,j; sl@0: struct stat sb; sl@0: sl@0: i = strlen(serialfile) + strlen(old_suffix); sl@0: j = strlen(serialfile) + strlen(new_suffix); sl@0: if (i > j) j = i; sl@0: if (j + 1 >= BSIZE) sl@0: { sl@0: BIO_printf(bio_err,"file name too long\n"); sl@0: goto err; sl@0: } sl@0: sl@0: #ifndef OPENSSL_SYS_VMS sl@0: j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", sl@0: serialfile, new_suffix); sl@0: #else sl@0: j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", sl@0: serialfile, new_suffix); sl@0: #endif sl@0: #ifndef OPENSSL_SYS_VMS sl@0: j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s", sl@0: serialfile, old_suffix); sl@0: #else sl@0: j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s", sl@0: serialfile, old_suffix); sl@0: #endif sl@0: if (stat(serialfile,&sb) < 0) sl@0: { sl@0: if (errno != ENOENT sl@0: #ifdef ENOTDIR sl@0: && errno != ENOTDIR sl@0: #endif sl@0: ) sl@0: goto err; sl@0: } sl@0: else sl@0: { sl@0: #ifdef RL_DEBUG sl@0: BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", sl@0: serialfile, buf[1]); sl@0: #endif sl@0: if (rename(serialfile,buf[1]) < 0) sl@0: { sl@0: BIO_printf(bio_err, sl@0: "unable to rename %s to %s\n", sl@0: serialfile, buf[1]); sl@0: perror("reason"); sl@0: goto err; sl@0: } sl@0: } sl@0: #ifdef RL_DEBUG sl@0: BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", sl@0: buf[0],serialfile); sl@0: #endif sl@0: if (rename(buf[0],serialfile) < 0) sl@0: { sl@0: BIO_printf(bio_err, sl@0: "unable to rename %s to %s\n", sl@0: buf[0],serialfile); sl@0: perror("reason"); sl@0: rename(buf[1],serialfile); sl@0: goto err; sl@0: } sl@0: return 1; sl@0: err: sl@0: return 0; sl@0: } sl@0: sl@0: int rand_serial(BIGNUM *b, ASN1_INTEGER *ai) sl@0: { sl@0: BIGNUM *btmp; sl@0: int ret = 0; sl@0: if (b) sl@0: btmp = b; sl@0: else sl@0: btmp = BN_new(); sl@0: sl@0: if (!btmp) sl@0: return 0; sl@0: sl@0: if (!BN_pseudo_rand(btmp, SERIAL_RAND_BITS, 0, 0)) sl@0: goto error; sl@0: if (ai && !BN_to_ASN1_INTEGER(btmp, ai)) sl@0: goto error; sl@0: sl@0: ret = 1; sl@0: sl@0: error: sl@0: sl@0: if (!b) sl@0: BN_free(btmp); sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: CA_DB *load_index(char *dbfile, DB_ATTR *db_attr) sl@0: { sl@0: CA_DB *retdb = NULL; sl@0: TXT_DB *tmpdb = NULL; sl@0: BIO *in = BIO_new(BIO_s_file()); sl@0: CONF *dbattr_conf = NULL; sl@0: char buf[1][BSIZE]; sl@0: long errorline= -1; sl@0: sl@0: if (in == NULL) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto err; sl@0: } sl@0: if (BIO_read_filename(in,dbfile) <= 0) sl@0: { sl@0: perror(dbfile); sl@0: BIO_printf(bio_err,"unable to open '%s'\n",dbfile); sl@0: goto err; sl@0: } sl@0: if ((tmpdb = TXT_DB_read(in,DB_NUMBER)) == NULL) sl@0: { sl@0: if (tmpdb != NULL) TXT_DB_free(tmpdb); sl@0: goto err; sl@0: } sl@0: sl@0: #ifndef OPENSSL_SYS_VMS sl@0: BIO_snprintf(buf[0], sizeof buf[0], "%s.attr", dbfile); sl@0: #else sl@0: BIO_snprintf(buf[0], sizeof buf[0], "%s-attr", dbfile); sl@0: #endif sl@0: dbattr_conf = NCONF_new(NULL); sl@0: if (NCONF_load(dbattr_conf,buf[0],&errorline) <= 0) sl@0: { sl@0: if (errorline > 0) sl@0: { sl@0: BIO_printf(bio_err, sl@0: "error on line %ld of db attribute file '%s'\n" sl@0: ,errorline,buf[0]); sl@0: goto err; sl@0: } sl@0: else sl@0: { sl@0: NCONF_free(dbattr_conf); sl@0: dbattr_conf = NULL; sl@0: } sl@0: } sl@0: sl@0: if ((retdb = OPENSSL_malloc(sizeof(CA_DB))) == NULL) sl@0: { sl@0: fprintf(stderr, "Out of memory\n"); sl@0: goto err; sl@0: } sl@0: sl@0: retdb->db = tmpdb; sl@0: tmpdb = NULL; sl@0: if (db_attr) sl@0: retdb->attributes = *db_attr; sl@0: else sl@0: { sl@0: retdb->attributes.unique_subject = 1; sl@0: } sl@0: sl@0: if (dbattr_conf) sl@0: { sl@0: char *p = NCONF_get_string(dbattr_conf,NULL,"unique_subject"); sl@0: if (p) sl@0: { sl@0: #ifdef RL_DEBUG sl@0: BIO_printf(bio_err, "DEBUG[load_index]: unique_subject = \"%s\"\n", p); sl@0: #endif sl@0: retdb->attributes.unique_subject = parse_yesno(p,1); sl@0: } sl@0: } sl@0: sl@0: err: sl@0: if (dbattr_conf) NCONF_free(dbattr_conf); sl@0: if (tmpdb) TXT_DB_free(tmpdb); sl@0: if (in) BIO_free_all(in); sl@0: return retdb; sl@0: } sl@0: sl@0: int index_index(CA_DB *db) sl@0: { sl@0: if (!TXT_DB_create_index(db->db, DB_serial, NULL, sl@0: LHASH_HASH_FN(index_serial_hash), sl@0: LHASH_COMP_FN(index_serial_cmp))) sl@0: { sl@0: BIO_printf(bio_err, sl@0: "error creating serial number index:(%ld,%ld,%ld)\n", sl@0: db->db->error,db->db->arg1,db->db->arg2); sl@0: return 0; sl@0: } sl@0: sl@0: if (db->attributes.unique_subject sl@0: && !TXT_DB_create_index(db->db, DB_name, index_name_qual, sl@0: LHASH_HASH_FN(index_name_hash), sl@0: LHASH_COMP_FN(index_name_cmp))) sl@0: { sl@0: BIO_printf(bio_err,"error creating name index:(%ld,%ld,%ld)\n", sl@0: db->db->error,db->db->arg1,db->db->arg2); sl@0: return 0; sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: int save_index(const char *dbfile, const char *suffix, CA_DB *db) sl@0: { sl@0: char buf[3][BSIZE]; sl@0: BIO *out = BIO_new(BIO_s_file()); sl@0: int j; sl@0: sl@0: if (out == NULL) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto err; sl@0: } sl@0: sl@0: j = strlen(dbfile) + strlen(suffix); sl@0: if (j + 6 >= BSIZE) sl@0: { sl@0: BIO_printf(bio_err,"file name too long\n"); sl@0: goto err; sl@0: } sl@0: sl@0: #ifndef OPENSSL_SYS_VMS sl@0: j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr", dbfile); sl@0: #else sl@0: j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr", dbfile); sl@0: #endif sl@0: #ifndef OPENSSL_SYS_VMS sl@0: j = BIO_snprintf(buf[1], sizeof buf[1], "%s.attr.%s", dbfile, suffix); sl@0: #else sl@0: j = BIO_snprintf(buf[1], sizeof buf[1], "%s-attr-%s", dbfile, suffix); sl@0: #endif sl@0: #ifndef OPENSSL_SYS_VMS sl@0: j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", dbfile, suffix); sl@0: #else sl@0: j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", dbfile, suffix); sl@0: #endif sl@0: #ifdef RL_DEBUG sl@0: BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]); sl@0: #endif sl@0: if (BIO_write_filename(out,buf[0]) <= 0) sl@0: { sl@0: perror(dbfile); sl@0: BIO_printf(bio_err,"unable to open '%s'\n", dbfile); sl@0: goto err; sl@0: } sl@0: j=TXT_DB_write(out,db->db); sl@0: if (j <= 0) goto err; sl@0: sl@0: BIO_free(out); sl@0: sl@0: out = BIO_new(BIO_s_file()); sl@0: #ifdef RL_DEBUG sl@0: BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[1]); sl@0: #endif sl@0: if (BIO_write_filename(out,buf[1]) <= 0) sl@0: { sl@0: perror(buf[2]); sl@0: BIO_printf(bio_err,"unable to open '%s'\n", buf[2]); sl@0: goto err; sl@0: } sl@0: BIO_printf(out,"unique_subject = %s\n", sl@0: db->attributes.unique_subject ? "yes" : "no"); sl@0: BIO_free(out); sl@0: sl@0: return 1; sl@0: err: sl@0: return 0; sl@0: } sl@0: sl@0: int rotate_index(const char *dbfile, const char *new_suffix, const char *old_suffix) sl@0: { sl@0: char buf[5][BSIZE]; sl@0: int i,j; sl@0: struct stat sb; sl@0: sl@0: i = strlen(dbfile) + strlen(old_suffix); sl@0: j = strlen(dbfile) + strlen(new_suffix); sl@0: if (i > j) j = i; sl@0: if (j + 6 >= BSIZE) sl@0: { sl@0: BIO_printf(bio_err,"file name too long\n"); sl@0: goto err; sl@0: } sl@0: sl@0: #ifndef OPENSSL_SYS_VMS sl@0: j = BIO_snprintf(buf[4], sizeof buf[4], "%s.attr", dbfile); sl@0: #else sl@0: j = BIO_snprintf(buf[4], sizeof buf[4], "%s-attr", dbfile); sl@0: #endif sl@0: #ifndef OPENSSL_SYS_VMS sl@0: j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr.%s", sl@0: dbfile, new_suffix); sl@0: #else sl@0: j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr-%s", sl@0: dbfile, new_suffix); sl@0: #endif sl@0: #ifndef OPENSSL_SYS_VMS sl@0: j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", sl@0: dbfile, new_suffix); sl@0: #else sl@0: j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", sl@0: dbfile, new_suffix); sl@0: #endif sl@0: #ifndef OPENSSL_SYS_VMS sl@0: j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s", sl@0: dbfile, old_suffix); sl@0: #else sl@0: j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s", sl@0: dbfile, old_suffix); sl@0: #endif sl@0: #ifndef OPENSSL_SYS_VMS sl@0: j = BIO_snprintf(buf[3], sizeof buf[3], "%s.attr.%s", sl@0: dbfile, old_suffix); sl@0: #else sl@0: j = BIO_snprintf(buf[3], sizeof buf[3], "%s-attr-%s", sl@0: dbfile, old_suffix); sl@0: #endif sl@0: if (stat(dbfile,&sb) < 0) sl@0: { sl@0: if (errno != ENOENT sl@0: #ifdef ENOTDIR sl@0: && errno != ENOTDIR sl@0: #endif sl@0: ) sl@0: goto err; sl@0: } sl@0: else sl@0: { sl@0: #ifdef RL_DEBUG sl@0: BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", sl@0: dbfile, buf[1]); sl@0: #endif sl@0: if (rename(dbfile,buf[1]) < 0) sl@0: { sl@0: BIO_printf(bio_err, sl@0: "unable to rename %s to %s\n", sl@0: dbfile, buf[1]); sl@0: perror("reason"); sl@0: goto err; sl@0: } sl@0: } sl@0: #ifdef RL_DEBUG sl@0: BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", sl@0: buf[0],dbfile); sl@0: #endif sl@0: if (rename(buf[0],dbfile) < 0) sl@0: { sl@0: BIO_printf(bio_err, sl@0: "unable to rename %s to %s\n", sl@0: buf[0],dbfile); sl@0: perror("reason"); sl@0: rename(buf[1],dbfile); sl@0: goto err; sl@0: } sl@0: if (stat(buf[4],&sb) < 0) sl@0: { sl@0: if (errno != ENOENT sl@0: #ifdef ENOTDIR sl@0: && errno != ENOTDIR sl@0: #endif sl@0: ) sl@0: goto err; sl@0: } sl@0: else sl@0: { sl@0: #ifdef RL_DEBUG sl@0: BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", sl@0: buf[4],buf[3]); sl@0: #endif sl@0: if (rename(buf[4],buf[3]) < 0) sl@0: { sl@0: BIO_printf(bio_err, sl@0: "unable to rename %s to %s\n", sl@0: buf[4], buf[3]); sl@0: perror("reason"); sl@0: rename(dbfile,buf[0]); sl@0: rename(buf[1],dbfile); sl@0: goto err; sl@0: } sl@0: } sl@0: #ifdef RL_DEBUG sl@0: BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", sl@0: buf[2],buf[4]); sl@0: #endif sl@0: if (rename(buf[2],buf[4]) < 0) sl@0: { sl@0: BIO_printf(bio_err, sl@0: "unable to rename %s to %s\n", sl@0: buf[2],buf[4]); sl@0: perror("reason"); sl@0: rename(buf[3],buf[4]); sl@0: rename(dbfile,buf[0]); sl@0: rename(buf[1],dbfile); sl@0: goto err; sl@0: } sl@0: return 1; sl@0: err: sl@0: return 0; sl@0: } sl@0: sl@0: void free_index(CA_DB *db) sl@0: { sl@0: if (db) sl@0: { sl@0: if (db->db) TXT_DB_free(db->db); sl@0: OPENSSL_free(db); sl@0: } sl@0: } sl@0: sl@0: int parse_yesno(const char *str, int def) sl@0: { sl@0: int ret = def; sl@0: if (str) sl@0: { sl@0: switch (*str) sl@0: { sl@0: case 'f': /* false */ sl@0: case 'F': /* FALSE */ sl@0: case 'n': /* no */ sl@0: case 'N': /* NO */ sl@0: case '0': /* 0 */ sl@0: ret = 0; sl@0: break; sl@0: case 't': /* true */ sl@0: case 'T': /* TRUE */ sl@0: case 'y': /* yes */ sl@0: case 'Y': /* YES */ sl@0: case '1': /* 1 */ sl@0: ret = 0; sl@0: break; sl@0: default: sl@0: ret = def; sl@0: break; sl@0: } sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: /* sl@0: * subject is expected to be in the format /type0=value0/type1=value1/type2=... sl@0: * where characters may be escaped by \ sl@0: */ sl@0: X509_NAME *parse_name(char *subject, long chtype, int multirdn) sl@0: { sl@0: size_t buflen = strlen(subject)+1; /* to copy the types and values into. due to escaping, the copy can only become shorter */ sl@0: char *buf = OPENSSL_malloc(buflen); sl@0: size_t max_ne = buflen / 2 + 1; /* maximum number of name elements */ sl@0: char **ne_types = OPENSSL_malloc(max_ne * sizeof (char *)); sl@0: char **ne_values = OPENSSL_malloc(max_ne * sizeof (char *)); sl@0: int *mval = OPENSSL_malloc (max_ne * sizeof (int)); sl@0: sl@0: char *sp = subject, *bp = buf; sl@0: int i, ne_num = 0; sl@0: sl@0: X509_NAME *n = NULL; sl@0: int nid; sl@0: sl@0: if (!buf || !ne_types || !ne_values) sl@0: { sl@0: BIO_printf(bio_err, "malloc error\n"); sl@0: goto error; sl@0: } sl@0: sl@0: if (*subject != '/') sl@0: { sl@0: BIO_printf(bio_err, "Subject does not start with '/'.\n"); sl@0: goto error; sl@0: } sl@0: sp++; /* skip leading / */ sl@0: sl@0: /* no multivalued RDN by default */ sl@0: mval[ne_num] = 0; sl@0: sl@0: while (*sp) sl@0: { sl@0: /* collect type */ sl@0: ne_types[ne_num] = bp; sl@0: while (*sp) sl@0: { sl@0: if (*sp == '\\') /* is there anything to escape in the type...? */ sl@0: { sl@0: if (*++sp) sl@0: *bp++ = *sp++; sl@0: else sl@0: { sl@0: BIO_printf(bio_err, "escape character at end of string\n"); sl@0: goto error; sl@0: } sl@0: } sl@0: else if (*sp == '=') sl@0: { sl@0: sp++; sl@0: *bp++ = '\0'; sl@0: break; sl@0: } sl@0: else sl@0: *bp++ = *sp++; sl@0: } sl@0: if (!*sp) sl@0: { sl@0: BIO_printf(bio_err, "end of string encountered while processing type of subject name element #%d\n", ne_num); sl@0: goto error; sl@0: } sl@0: ne_values[ne_num] = bp; sl@0: while (*sp) sl@0: { sl@0: if (*sp == '\\') sl@0: { sl@0: if (*++sp) sl@0: *bp++ = *sp++; sl@0: else sl@0: { sl@0: BIO_printf(bio_err, "escape character at end of string\n"); sl@0: goto error; sl@0: } sl@0: } sl@0: else if (*sp == '/') sl@0: { sl@0: sp++; sl@0: /* no multivalued RDN by default */ sl@0: mval[ne_num+1] = 0; sl@0: break; sl@0: } sl@0: else if (*sp == '+' && multirdn) sl@0: { sl@0: /* a not escaped + signals a mutlivalued RDN */ sl@0: sp++; sl@0: mval[ne_num+1] = -1; sl@0: break; sl@0: } sl@0: else sl@0: *bp++ = *sp++; sl@0: } sl@0: *bp++ = '\0'; sl@0: ne_num++; sl@0: } sl@0: sl@0: if (!(n = X509_NAME_new())) sl@0: goto error; sl@0: sl@0: for (i = 0; i < ne_num; i++) sl@0: { sl@0: if ((nid=OBJ_txt2nid(ne_types[i])) == NID_undef) sl@0: { sl@0: BIO_printf(bio_err, "Subject Attribute %s has no known NID, skipped\n", ne_types[i]); sl@0: continue; sl@0: } sl@0: sl@0: if (!*ne_values[i]) sl@0: { sl@0: BIO_printf(bio_err, "No value provided for Subject Attribute %s, skipped\n", ne_types[i]); sl@0: continue; sl@0: } sl@0: sl@0: if (!X509_NAME_add_entry_by_NID(n, nid, chtype, (unsigned char*)ne_values[i], -1,-1,mval[i])) sl@0: goto error; sl@0: } sl@0: sl@0: OPENSSL_free(ne_values); sl@0: OPENSSL_free(ne_types); sl@0: OPENSSL_free(buf); sl@0: return n; sl@0: sl@0: error: sl@0: X509_NAME_free(n); sl@0: if (ne_values) sl@0: OPENSSL_free(ne_values); sl@0: if (ne_types) sl@0: OPENSSL_free(ne_types); sl@0: if (buf) sl@0: OPENSSL_free(buf); sl@0: return NULL; sl@0: } sl@0: sl@0: /* This code MUST COME AFTER anything that uses rename() */ sl@0: #ifdef OPENSSL_SYS_WIN32 sl@0: int WIN32_rename(const char *from, const char *to) sl@0: { sl@0: #ifndef OPENSSL_SYS_WINCE sl@0: /* Windows rename gives an error if 'to' exists, so delete it sl@0: * first and ignore file not found errror sl@0: */ sl@0: if((remove(to) != 0) && (errno != ENOENT)) sl@0: return -1; sl@0: #undef rename sl@0: return rename(from, to); sl@0: #else sl@0: /* convert strings to UNICODE */ sl@0: { sl@0: BOOL result = FALSE; sl@0: WCHAR* wfrom; sl@0: WCHAR* wto; sl@0: int i; sl@0: wfrom = malloc((strlen(from)+1)*2); sl@0: wto = malloc((strlen(to)+1)*2); sl@0: if (wfrom != NULL && wto != NULL) sl@0: { sl@0: for (i=0; i<(int)strlen(from)+1; i++) sl@0: wfrom[i] = (short)from[i]; sl@0: for (i=0; i<(int)strlen(to)+1; i++) sl@0: wto[i] = (short)to[i]; sl@0: result = MoveFile(wfrom, wto); sl@0: } sl@0: if (wfrom != NULL) sl@0: free(wfrom); sl@0: if (wto != NULL) sl@0: free(wto); sl@0: return result; sl@0: } sl@0: #endif sl@0: } sl@0: #endif sl@0: sl@0: int args_verify(char ***pargs, int *pargc, sl@0: int *badarg, BIO *err, X509_VERIFY_PARAM **pm) sl@0: { sl@0: ASN1_OBJECT *otmp = NULL; sl@0: unsigned long flags = 0; sl@0: int i; sl@0: int purpose = 0; sl@0: char **oldargs = *pargs; sl@0: char *arg = **pargs, *argn = (*pargs)[1]; sl@0: if (!strcmp(arg, "-policy")) sl@0: { sl@0: if (!argn) sl@0: *badarg = 1; sl@0: else sl@0: { sl@0: otmp = OBJ_txt2obj(argn, 0); sl@0: if (!otmp) sl@0: { sl@0: BIO_printf(err, "Invalid Policy \"%s\"\n", sl@0: argn); sl@0: *badarg = 1; sl@0: } sl@0: } sl@0: (*pargs)++; sl@0: } sl@0: else if (strcmp(arg,"-purpose") == 0) sl@0: { sl@0: X509_PURPOSE *xptmp; sl@0: if (!argn) sl@0: *badarg = 1; sl@0: else sl@0: { sl@0: i = X509_PURPOSE_get_by_sname(argn); sl@0: if(i < 0) sl@0: { sl@0: BIO_printf(err, "unrecognized purpose\n"); sl@0: *badarg = 1; sl@0: } sl@0: else sl@0: { sl@0: xptmp = X509_PURPOSE_get0(i); sl@0: purpose = X509_PURPOSE_get_id(xptmp); sl@0: } sl@0: } sl@0: (*pargs)++; sl@0: } sl@0: else if (!strcmp(arg, "-ignore_critical")) sl@0: flags |= X509_V_FLAG_IGNORE_CRITICAL; sl@0: else if (!strcmp(arg, "-issuer_checks")) sl@0: flags |= X509_V_FLAG_CB_ISSUER_CHECK; sl@0: else if (!strcmp(arg, "-crl_check")) sl@0: flags |= X509_V_FLAG_CRL_CHECK; sl@0: else if (!strcmp(arg, "-crl_check_all")) sl@0: flags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL; sl@0: else if (!strcmp(arg, "-policy_check")) sl@0: flags |= X509_V_FLAG_POLICY_CHECK; sl@0: else if (!strcmp(arg, "-explicit_policy")) sl@0: flags |= X509_V_FLAG_EXPLICIT_POLICY; sl@0: else if (!strcmp(arg, "-x509_strict")) sl@0: flags |= X509_V_FLAG_X509_STRICT; sl@0: else if (!strcmp(arg, "-policy_print")) sl@0: flags |= X509_V_FLAG_NOTIFY_POLICY; sl@0: else sl@0: return 0; sl@0: sl@0: if (*badarg) sl@0: { sl@0: if (*pm) sl@0: X509_VERIFY_PARAM_free(*pm); sl@0: *pm = NULL; sl@0: goto end; sl@0: } sl@0: sl@0: if (!*pm && !(*pm = X509_VERIFY_PARAM_new())) sl@0: { sl@0: *badarg = 1; sl@0: goto end; sl@0: } sl@0: sl@0: if (otmp) sl@0: X509_VERIFY_PARAM_add0_policy(*pm, otmp); sl@0: if (flags) sl@0: X509_VERIFY_PARAM_set_flags(*pm, flags); sl@0: sl@0: if (purpose) sl@0: X509_VERIFY_PARAM_set_purpose(*pm, purpose); sl@0: sl@0: end: sl@0: sl@0: (*pargs)++; sl@0: sl@0: if (pargc) sl@0: *pargc -= *pargs - oldargs; sl@0: sl@0: return 1; sl@0: sl@0: } sl@0: sl@0: static void nodes_print(BIO *out, const char *name, sl@0: STACK_OF(X509_POLICY_NODE) *nodes) sl@0: { sl@0: X509_POLICY_NODE *node; sl@0: int i; sl@0: BIO_printf(out, "%s Policies:", name); sl@0: if (nodes) sl@0: { sl@0: BIO_puts(out, "\n"); sl@0: for (i = 0; i < sk_X509_POLICY_NODE_num(nodes); i++) sl@0: { sl@0: node = sk_X509_POLICY_NODE_value(nodes, i); sl@0: X509_POLICY_NODE_print(out, node, 2); sl@0: } sl@0: } sl@0: else sl@0: BIO_puts(out, " \n"); sl@0: } sl@0: sl@0: void policies_print(BIO *out, X509_STORE_CTX *ctx) sl@0: { sl@0: X509_POLICY_TREE *tree; sl@0: int explicit_policy; sl@0: int free_out = 0; sl@0: if (out == NULL) sl@0: { sl@0: out = BIO_new_fp(stderr, BIO_NOCLOSE); sl@0: free_out = 1; sl@0: } sl@0: tree = X509_STORE_CTX_get0_policy_tree(ctx); sl@0: explicit_policy = X509_STORE_CTX_get_explicit_policy(ctx); sl@0: sl@0: BIO_printf(out, "Require explicit Policy: %s\n", sl@0: explicit_policy ? "True" : "False"); sl@0: sl@0: nodes_print(out, "Authority", X509_policy_tree_get0_policies(tree)); sl@0: nodes_print(out, "User", X509_policy_tree_get0_user_policies(tree)); sl@0: if (free_out) sl@0: BIO_free(out); sl@0: }