Reduce chances of issuer and serial number duplication by use of random
[openssl.git] / apps / apps.c
index dd19a4cc3a1ba67941884b23cde1f3a86a4b54f0..6925ab4cdd84c6ce80b459b151c47f7ec95ab06b 100644 (file)
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
+/* ====================================================================
+ * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer. 
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ *    software must display the following acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
+ *
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission. For written permission, please contact
+ *    openssl-core@openssl.org.
+ *
+ * 5. Products derived from this software may not be called "OpenSSL"
+ *    nor may "OpenSSL" appear in their names without prior written
+ *    permission of the OpenSSL Project.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com).  This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com).
+ *
+ */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#define NON_MAIN
-#include "apps.h"
-#undef NON_MAIN
+#include <ctype.h>
 #include <openssl/err.h>
 #include <openssl/x509.h>
 #include <openssl/x509v3.h>
 #include <openssl/pem.h>
 #include <openssl/pkcs12.h>
+#include <openssl/ui.h>
 #include <openssl/safestack.h>
-
-#ifdef OPENSSL_SYS_WINDOWS
-#define strcasecmp _stricmp
-#else
-#include <strings.h>
+#ifndef OPENSSL_NO_ENGINE
+#include <openssl/engine.h>
 #endif
+#include <openssl/rsa.h>
 
-#ifdef OPENSSL_SYS_WINDOWS
-#  include "bss_file.c"
-#endif
+#define NON_MAIN
+#include "apps.h"
+#undef NON_MAIN
 
 typedef struct {
        char *name;
@@ -87,9 +137,18 @@ typedef struct {
        unsigned long mask;
 } NAME_EX_TBL;
 
+static UI_METHOD *ui_method = NULL;
+
 static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
 static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
 
+#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
+/* Looks like this stuff is worth moving into separate function */
+static EVP_PKEY *
+load_netscape_key(BIO *err, BIO *key, const char *file,
+               const char *key_descrip, int format);
+#endif
+
 int app_init(long mesgwin);
 #ifdef undef /* never finished - probably never will be :-) */
 int args_from_file(char *file, int *argc, char **argv[])
@@ -192,7 +251,7 @@ int str2fmt(char *s)
                return(FORMAT_UNDEF);
        }
 
-#if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
+#if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) || defined(OPENSSL_SYS_NETWARE)
 void program_name(char *in, char *out, int size)
        {
        int i,n;
@@ -211,12 +270,23 @@ void program_name(char *in, char *out, int size)
        if (p == NULL)
                p=in;
        n=strlen(p);
+
+#if defined(OPENSSL_SYS_NETWARE)
+   /* strip off trailing .nlm if present. */
+   if ((n > 4) && (p[n-4] == '.') &&
+      ((p[n-3] == 'n') || (p[n-3] == 'N')) &&
+      ((p[n-2] == 'l') || (p[n-2] == 'L')) &&
+      ((p[n-1] == 'm') || (p[n-1] == 'M')))
+      n-=4;
+#else
        /* strip off trailing .exe if present. */
        if ((n > 4) && (p[n-4] == '.') &&
                ((p[n-3] == 'e') || (p[n-3] == 'E')) &&
                ((p[n-2] == 'x') || (p[n-2] == 'X')) &&
                ((p[n-1] == 'e') || (p[n-1] == 'E')))
                n-=4;
+#endif
+
        if (n > size-1)
                n=size-1;
 
@@ -246,9 +316,16 @@ void program_name(char *in, char *out, int size)
 
        q=strrchr(p,'.');
        if (q == NULL)
-               q = in+size;
-       strncpy(out,p,q-p);
-       out[q-p]='\0';
+               q = p + strlen(p);
+       strncpy(out,p,size-1);
+       if (q-p >= size)
+               {
+               out[size-1]='\0';
+               }
+       else
+               {
+               out[q-p]='\0';
+               }
        }
 #else
 void program_name(char *in, char *out, int size)
@@ -260,28 +337,11 @@ void program_name(char *in, char *out, int size)
                p++;
        else
                p=in;
-       strncpy(out,p,size-1);
-       out[size-1]='\0';
+       BUF_strlcpy(out,p,size);
        }
 #endif
 #endif
 
-#ifdef OPENSSL_SYS_WIN32
-int WIN32_rename(char *from, char *to)
-       {
-#ifdef OPENSSL_SYS_WINNT
-       int ret;
-/* Note: MoveFileEx() doesn't work under Win95, Win98 */
-
-       ret=MoveFileEx(from,to,MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED);
-       return(ret?0:-1);
-#else
-       unlink(to);
-       return MoveFile(from, to);
-#endif
-       }
-#endif
-
 int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[])
        {
        int num,len,i;
@@ -357,25 +417,100 @@ int app_init(long mesgwin)
 
 int dump_cert_text (BIO *out, X509 *x)
 {
-       char buf[256];
-       X509_NAME_oneline(X509_get_subject_name(x),buf,256);
+       char *p;
+
+       p=X509_NAME_oneline(X509_get_subject_name(x),NULL,0);
        BIO_puts(out,"subject=");
-       BIO_puts(out,buf);
+       BIO_puts(out,p);
+       OPENSSL_free(p);
 
-       X509_NAME_oneline(X509_get_issuer_name(x),buf,256);
-       BIO_puts(out,"\nissuer= ");
-       BIO_puts(out,buf);
+       p=X509_NAME_oneline(X509_get_issuer_name(x),NULL,0);
+       BIO_puts(out,"\nissuer=");
+       BIO_puts(out,p);
        BIO_puts(out,"\n");
-        return 0;
+       OPENSSL_free(p);
+
+       return 0;
 }
 
+static int ui_open(UI *ui)
+       {
+       return UI_method_get_opener(UI_OpenSSL())(ui);
+       }
+static int ui_read(UI *ui, UI_STRING *uis)
+       {
+       if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
+               && UI_get0_user_data(ui))
+               {
+               switch(UI_get_string_type(uis))
+                       {
+               case UIT_PROMPT:
+               case UIT_VERIFY:
+                       {
+                       const char *password =
+                               ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
+                       if (password && password[0] != '\0')
+                               {
+                               UI_set_result(ui, uis, password);
+                               return 1;
+                               }
+                       }
+               default:
+                       break;
+                       }
+               }
+       return UI_method_get_reader(UI_OpenSSL())(ui, uis);
+       }
+static int ui_write(UI *ui, UI_STRING *uis)
+       {
+       if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
+               && UI_get0_user_data(ui))
+               {
+               switch(UI_get_string_type(uis))
+                       {
+               case UIT_PROMPT:
+               case UIT_VERIFY:
+                       {
+                       const char *password =
+                               ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
+                       if (password && password[0] != '\0')
+                               return 1;
+                       }
+               default:
+                       break;
+                       }
+               }
+       return UI_method_get_writer(UI_OpenSSL())(ui, uis);
+       }
+static int ui_close(UI *ui)
+       {
+       return UI_method_get_closer(UI_OpenSSL())(ui);
+       }
+int setup_ui_method(void)
+       {
+       ui_method = UI_create_method("OpenSSL application user interface");
+       UI_method_set_opener(ui_method, ui_open);
+       UI_method_set_reader(ui_method, ui_read);
+       UI_method_set_writer(ui_method, ui_write);
+       UI_method_set_closer(ui_method, ui_close);
+       return 0;
+       }
+void destroy_ui_method(void)
+       {
+       if(ui_method)
+               {
+               UI_destroy_method(ui_method);
+               ui_method = NULL;
+               }
+       }
 int password_callback(char *buf, int bufsiz, int verify,
-       PW_CB_DATA *cb_data)
+       PW_CB_DATA *cb_tmp)
        {
-       int i,j;
-       char prompt[80];
+       UI *ui = NULL;
+       int res = 0;
        const char *prompt_info = NULL;
        const char *password = NULL;
+       PW_CB_DATA *cb_data = (PW_CB_DATA *)cb_tmp;
 
        if (cb_data)
                {
@@ -385,39 +520,70 @@ int password_callback(char *buf, int bufsiz, int verify,
                        prompt_info = cb_data->prompt_info;
                }
 
-       if(password) {
-               i=strlen(password);
-               i=(i > bufsiz)?bufsiz:i;
-               memcpy(buf,password,i);
-               return(i);
-       }
-
-       if (EVP_get_pw_prompt())
-               BIO_snprintf(prompt, sizeof(prompt)-1, EVP_get_pw_prompt(),
-                       prompt_info ? prompt_info : "");
-       else
-               BIO_snprintf(prompt, sizeof(prompt)-1,
-                       "Enter pass phrase for %s:",
-                       prompt_info ? prompt_info : "");
+       if (password)
+               {
+               res = strlen(password);
+               if (res > bufsiz)
+                       res = bufsiz;
+               memcpy(buf, password, res);
+               return res;
+               }
 
-       for (;;)
+       ui = UI_new_method(ui_method);
+       if (ui)
                {
-               i=EVP_read_pw_string(buf,bufsiz,prompt,verify);
-               if (i != 0)
+               int ok = 0;
+               char *buff = NULL;
+               int ui_flags = 0;
+               char *prompt = NULL;
+
+               prompt = UI_construct_prompt(ui, "pass phrase",
+                       cb_data->prompt_info);
+
+               ui_flags |= UI_INPUT_FLAG_DEFAULT_PWD;
+               UI_ctrl(ui, UI_CTRL_PRINT_ERRORS, 1, 0, 0);
+
+               if (ok >= 0)
+                       ok = UI_add_input_string(ui,prompt,ui_flags,buf,
+                               PW_MIN_LENGTH,BUFSIZ-1);
+               if (ok >= 0 && verify)
                        {
-                       BIO_printf(bio_err,"aborted!\n");
-                       memset(buf,0,(unsigned int)bufsiz);
-                       return(-1);
+                       buff = (char *)OPENSSL_malloc(bufsiz);
+                       ok = UI_add_verify_string(ui,prompt,ui_flags,buff,
+                               PW_MIN_LENGTH,BUFSIZ-1, buf);
                        }
-               j=strlen(buf);
-               if (j < PW_MIN_LENGTH)
+               if (ok >= 0)
+                       do
+                               {
+                               ok = UI_process(ui);
+                               }
+                       while (ok < 0 && UI_ctrl(ui, UI_CTRL_IS_REDOABLE, 0, 0, 0));
+
+               if (buff)
                        {
-                       BIO_printf(bio_err,"phrase is too short, needs to be at least %d chars\n",PW_MIN_LENGTH);
+                       OPENSSL_cleanse(buff,(unsigned int)bufsiz);
+                       OPENSSL_free(buff);
                        }
-               else
-                       break;
+
+               if (ok >= 0)
+                       res = strlen(buf);
+               if (ok == -1)
+                       {
+                       BIO_printf(bio_err, "User interface error\n");
+                       ERR_print_errors(bio_err);
+                       OPENSSL_cleanse(buf,(unsigned int)bufsiz);
+                       res = 0;
+                       }
+               if (ok == -2)
+                       {
+                       BIO_printf(bio_err,"aborted!\n");
+                       OPENSSL_cleanse(buf,(unsigned int)bufsiz);
+                       res = 0;
+                       }
+               UI_free(ui);
+               OPENSSL_free(prompt);
                }
-       return(j);
+       return res;
        }
 
 static char *app_get_pass(BIO *err, char *arg, int keepbio);
@@ -495,18 +661,18 @@ static char *app_get_pass(BIO *err, char *arg, int keepbio)
        return BUF_strdup(tpass);
 }
 
-int add_oid_section(BIO *err, LHASH *conf)
+int add_oid_section(BIO *err, CONF *conf)
 {      
        char *p;
        STACK_OF(CONF_VALUE) *sktmp;
        CONF_VALUE *cnf;
        int i;
-       if(!(p=CONF_get_string(conf,NULL,"oid_section")))
+       if(!(p=NCONF_get_string(conf,NULL,"oid_section")))
                {
                ERR_clear_error();
                return 1;
                }
-       if(!(sktmp = CONF_get_section(conf, p))) {
+       if(!(sktmp = NCONF_get_section(conf, p))) {
                BIO_printf(err, "problem loading oid section %s\n", p);
                return 0;
        }
@@ -555,7 +721,7 @@ X509 *load_cert(BIO *err, const char *file, int format,
                x=d2i_X509_bio(cert,NULL);
        else if (format == FORMAT_NETSCAPE)
                {
-               unsigned char *p,*op;
+               const unsigned char *p,*op;
                int size=0,i;
 
                /* We sort of have to do it this way because it is sort of nice
@@ -624,7 +790,7 @@ end:
        return(x);
        }
 
-EVP_PKEY *load_key(BIO *err, const char *file, int format,
+EVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin,
        const char *pass, ENGINE *e, const char *key_descrip)
        {
        BIO *key=NULL;
@@ -634,32 +800,41 @@ EVP_PKEY *load_key(BIO *err, const char *file, int format,
        cb_data.password = pass;
        cb_data.prompt_info = file;
 
-       if (file == NULL)
+       if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
                {
                BIO_printf(err,"no keyfile specified\n");
                goto end;
                }
+#ifndef OPENSSL_NO_ENGINE
        if (format == FORMAT_ENGINE)
                {
                if (!e)
                        BIO_printf(bio_err,"no engine specified\n");
                else
                        pkey = ENGINE_load_private_key(e, file,
-                               (pem_password_cb *)password_callback, &cb_data);
+                               ui_method, &cb_data);
                goto end;
                }
+#endif
        key=BIO_new(BIO_s_file());
        if (key == NULL)
                {
                ERR_print_errors(err);
                goto end;
                }
-       if (BIO_read_filename(key,file) <= 0)
+       if (file == NULL && maybe_stdin)
                {
-               BIO_printf(err, "Error opening %s %s\n", key_descrip, file);
-               ERR_print_errors(err);
-               goto end;
+               setvbuf(stdin, NULL, _IONBF, 0);
+               BIO_set_fp(key,stdin,BIO_NOCLOSE);
                }
+       else
+               if (BIO_read_filename(key,file) <= 0)
+                       {
+                       BIO_printf(err, "Error opening %s %s\n",
+                               key_descrip, file);
+                       ERR_print_errors(err);
+                       goto end;
+                       }
        if (format == FORMAT_ASN1)
                {
                pkey=d2i_PrivateKey_bio(key, NULL);
@@ -669,6 +844,10 @@ EVP_PKEY *load_key(BIO *err, const char *file, int format,
                pkey=PEM_read_bio_PrivateKey(key,NULL,
                        (pem_password_cb *)password_callback, &cb_data);
                }
+#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
+       else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
+               pkey = load_netscape_key(err, key, file, key_descrip, format);
+#endif
        else if (format == FORMAT_PKCS12)
                {
                PKCS12 *p12 = d2i_PKCS12_bio(key, NULL);
@@ -689,7 +868,7 @@ EVP_PKEY *load_key(BIO *err, const char *file, int format,
        return(pkey);
        }
 
-EVP_PKEY *load_pubkey(BIO *err, const char *file, int format,
+EVP_PKEY *load_pubkey(BIO *err, const char *file, int format, int maybe_stdin,
        const char *pass, ENGINE *e, const char *key_descrip)
        {
        BIO *key=NULL;
@@ -699,31 +878,40 @@ EVP_PKEY *load_pubkey(BIO *err, const char *file, int format,
        cb_data.password = pass;
        cb_data.prompt_info = file;
 
-       if (file == NULL)
+       if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
                {
                BIO_printf(err,"no keyfile specified\n");
                goto end;
                }
+#ifndef OPENSSL_NO_ENGINE
        if (format == FORMAT_ENGINE)
                {
                if (!e)
                        BIO_printf(bio_err,"no engine specified\n");
                else
                        pkey = ENGINE_load_public_key(e, file,
-                               (pem_password_cb *)password_callback, &cb_data);
+                               ui_method, &cb_data);
                goto end;
                }
+#endif
        key=BIO_new(BIO_s_file());
        if (key == NULL)
                {
                ERR_print_errors(err);
                goto end;
                }
-       if (BIO_read_filename(key,file) <= 0)
+       if (file == NULL && maybe_stdin)
                {
-               BIO_printf(err, "Error opening %s %s\n", key_descrip, file);
-               ERR_print_errors(err);
-               goto end;
+               setvbuf(stdin, NULL, _IONBF, 0);
+               BIO_set_fp(key,stdin,BIO_NOCLOSE);
+               }
+       else
+               if (BIO_read_filename(key,file) <= 0)
+                       {
+                       BIO_printf(err, "Error opening %s %s\n",
+                               key_descrip, file);
+                       ERR_print_errors(err);
+                       goto end;
                }
        if (format == FORMAT_ASN1)
                {
@@ -734,6 +922,10 @@ EVP_PKEY *load_pubkey(BIO *err, const char *file, int format,
                pkey=PEM_read_bio_PUBKEY(key,NULL,
                        (pem_password_cb *)password_callback, &cb_data);
                }
+#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
+       else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
+               pkey = load_netscape_key(err, key, file, key_descrip, format);
+#endif
        else
                {
                BIO_printf(err,"bad input format specified for key file\n");
@@ -746,6 +938,52 @@ EVP_PKEY *load_pubkey(BIO *err, const char *file, int format,
        return(pkey);
        }
 
+#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
+static EVP_PKEY *
+load_netscape_key(BIO *err, BIO *key, const char *file,
+               const char *key_descrip, int format)
+       {
+       EVP_PKEY *pkey;
+       BUF_MEM *buf;
+       RSA     *rsa;
+       const unsigned char *p;
+       int size, i;
+
+       buf=BUF_MEM_new();
+       pkey = EVP_PKEY_new();
+       size = 0;
+       if (buf == NULL || pkey == NULL)
+               goto error;
+       for (;;)
+               {
+               if (!BUF_MEM_grow_clean(buf,size+1024*10))
+                       goto error;
+               i = BIO_read(key, &(buf->data[size]), 1024*10);
+               size += i;
+               if (i == 0)
+                       break;
+               if (i < 0)
+                       {
+                               BIO_printf(err, "Error reading %s %s",
+                                       key_descrip, file);
+                               goto error;
+                       }
+               }
+       p=(unsigned char *)buf->data;
+       rsa = d2i_RSA_NET(NULL,&p,(long)size,NULL,
+               (format == FORMAT_IISSGC ? 1 : 0));
+       if (rsa == NULL)
+               goto error;
+       BUF_MEM_free(buf);
+       EVP_PKEY_set1_RSA(pkey, rsa);
+       return pkey;
+error:
+       BUF_MEM_free(buf);
+       EVP_PKEY_free(pkey);
+       return NULL;
+       }
+#endif /* ndef OPENSSL_NO_RC4 */
+
 STACK_OF(X509) *load_certs(BIO *err, const char *file, int format,
        const char *pass, ENGINE *e, const char *cert_descrip)
        {
@@ -846,6 +1084,7 @@ int set_cert_ex(unsigned long *flags, const char *arg)
                { "no_extensions", X509_FLAG_NO_EXTENSIONS, 0},
                { "no_sigdump", X509_FLAG_NO_SIGDUMP, 0},
                { "no_aux", X509_FLAG_NO_AUX, 0},
+               { "no_attributes", X509_FLAG_NO_ATTRIBUTES, 0},
                { "ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK},
                { "ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
                { "ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
@@ -989,18 +1228,20 @@ static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_T
 
 void print_name(BIO *out, char *title, X509_NAME *nm, unsigned long lflags)
 {
-       char buf[256];
+       char *buf;
        char mline = 0;
        int indent = 0;
+
        if(title) BIO_puts(out, title);
        if((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
                mline = 1;
                indent = 4;
        }
        if(lflags == XN_FLAG_COMPAT) {
-               X509_NAME_oneline(nm,buf,256);
-               BIO_puts(out,buf);
+               buf = X509_NAME_oneline(nm, 0, 0);
+               BIO_puts(out, buf);
                BIO_puts(out, "\n");
+               OPENSSL_free(buf);
        } else {
                if(mline) BIO_puts(out, "\n");
                X509_NAME_print_ex(out, nm, indent, lflags);
@@ -1038,15 +1279,40 @@ X509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath)
        return NULL;
 }
 
+#ifndef OPENSSL_NO_ENGINE
+/* Try to load an engine in a shareable library */
+static ENGINE *try_load_engine(BIO *err, const char *engine, int debug)
+       {
+       ENGINE *e = ENGINE_by_id("dynamic");
+       if (e)
+               {
+               if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0)
+                       || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
+                       {
+                       ENGINE_free(e);
+                       e = NULL;
+                       }
+               }
+       return e;
+       }
+
 ENGINE *setup_engine(BIO *err, const char *engine, int debug)
         {
         ENGINE *e = NULL;
 
         if (engine)
                 {
-               if((e = ENGINE_by_id(engine)) == NULL)
+               if(strcmp(engine, "auto") == 0)
+                       {
+                       BIO_printf(err,"enabling auto ENGINE support\n");
+                       ENGINE_register_all_complete();
+                       return NULL;
+                       }
+               if((e = ENGINE_by_id(engine)) == NULL
+                       && (e = try_load_engine(err, engine, debug)) == NULL)
                        {
                        BIO_printf(err,"invalid engine \"%s\"\n", engine);
+                       ERR_print_errors(err);
                        return NULL;
                        }
                if (debug)
@@ -1054,15 +1320,822 @@ ENGINE *setup_engine(BIO *err, const char *engine, int debug)
                        ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM,
                                0, err, 0);
                        }
-                ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, UI_OpenSSL(), 0, 1);
+                ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, ui_method, 0, 1);
                if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
                        {
                        BIO_printf(err,"can't use that engine\n");
+                       ERR_print_errors(err);
+                       ENGINE_free(e);
                        return NULL;
                        }
-               BIO_printf(err,"engine \"%s\" set.\n", engine);
+
+               BIO_printf(err,"engine \"%s\" set.\n", ENGINE_get_id(e));
+
                /* Free our "structural" reference. */
                ENGINE_free(e);
                }
         return e;
         }
+#endif
+
+int load_config(BIO *err, CONF *cnf)
+       {
+       if (!cnf)
+               cnf = config;
+       if (!cnf)
+               return 1;
+
+       OPENSSL_load_builtin_modules();
+
+       if (CONF_modules_load(cnf, NULL, 0) <= 0)
+               {
+               BIO_printf(err, "Error configuring OpenSSL\n");
+               ERR_print_errors(err);
+               return 0;
+               }
+       return 1;
+       }
+
+char *make_config_name()
+       {
+       const char *t=X509_get_default_cert_area();
+       size_t len;
+       char *p;
+
+       len=strlen(t)+strlen(OPENSSL_CONF)+2;
+       p=OPENSSL_malloc(len);
+       BUF_strlcpy(p,t,len);
+#ifndef OPENSSL_SYS_VMS
+       BUF_strlcat(p,"/",len);
+#endif
+       BUF_strlcat(p,OPENSSL_CONF,len);
+
+       return p;
+       }
+
+static unsigned long index_serial_hash(const char **a)
+       {
+       const char *n;
+
+       n=a[DB_serial];
+       while (*n == '0') n++;
+       return(lh_strhash(n));
+       }
+
+static int index_serial_cmp(const char **a, const char **b)
+       {
+       const char *aa,*bb;
+
+       for (aa=a[DB_serial]; *aa == '0'; aa++);
+       for (bb=b[DB_serial]; *bb == '0'; bb++);
+       return(strcmp(aa,bb));
+       }
+
+static int index_name_qual(char **a)
+       { return(a[0][0] == 'V'); }
+
+static unsigned long index_name_hash(const char **a)
+       { return(lh_strhash(a[DB_name])); }
+
+int index_name_cmp(const char **a, const char **b)
+       { return(strcmp(a[DB_name],
+            b[DB_name])); }
+
+static IMPLEMENT_LHASH_HASH_FN(index_serial_hash,const char **)
+static IMPLEMENT_LHASH_COMP_FN(index_serial_cmp,const char **)
+static IMPLEMENT_LHASH_HASH_FN(index_name_hash,const char **)
+static IMPLEMENT_LHASH_COMP_FN(index_name_cmp,const char **)
+
+#undef BSIZE
+#define BSIZE 256
+
+BIGNUM *load_serial(char *serialfile, int create, ASN1_INTEGER **retai)
+       {
+       BIO *in=NULL;
+       BIGNUM *ret=NULL;
+       MS_STATIC char buf[1024];
+       ASN1_INTEGER *ai=NULL;
+
+       ai=ASN1_INTEGER_new();
+       if (ai == NULL) goto err;
+
+       if ((in=BIO_new(BIO_s_file())) == NULL)
+               {
+               ERR_print_errors(bio_err);
+               goto err;
+               }
+
+       if (BIO_read_filename(in,serialfile) <= 0)
+               {
+               if (!create)
+                       {
+                       perror(serialfile);
+                       goto err;
+                       }
+               else
+                       {
+                       ret=BN_new();
+                       if (ret == NULL || !rand_serial(ret, ai))
+                               BIO_printf(bio_err, "Out of memory\n");
+                       }
+               }
+       else
+               {
+               if (!a2i_ASN1_INTEGER(in,ai,buf,1024))
+                       {
+                       BIO_printf(bio_err,"unable to load number from %s\n",
+                               serialfile);
+                       goto err;
+                       }
+               ret=ASN1_INTEGER_to_BN(ai,NULL);
+               if (ret == NULL)
+                       {
+                       BIO_printf(bio_err,"error converting number from bin to BIGNUM\n");
+                       goto err;
+                       }
+               }
+
+       if (ret && retai)
+               {
+               *retai = ai;
+               ai = NULL;
+               }
+ err:
+       if (in != NULL) BIO_free(in);
+       if (ai != NULL) ASN1_INTEGER_free(ai);
+       return(ret);
+       }
+
+int save_serial(char *serialfile, char *suffix, BIGNUM *serial, ASN1_INTEGER **retai)
+       {
+       char buf[1][BSIZE];
+       BIO *out = NULL;
+       int ret=0;
+       ASN1_INTEGER *ai=NULL;
+       int j;
+
+       if (suffix == NULL)
+               j = strlen(serialfile);
+       else
+               j = strlen(serialfile) + strlen(suffix) + 1;
+       if (j >= BSIZE)
+               {
+               BIO_printf(bio_err,"file name too long\n");
+               goto err;
+               }
+
+       if (suffix == NULL)
+               BUF_strlcpy(buf[0], serialfile, BSIZE);
+       else
+               {
+#ifndef OPENSSL_SYS_VMS
+               j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", serialfile, suffix);
+#else
+               j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", serialfile, suffix);
+#endif
+               }
+#ifdef RL_DEBUG
+       BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]);
+#endif
+       out=BIO_new(BIO_s_file());
+       if (out == NULL)
+               {
+               ERR_print_errors(bio_err);
+               goto err;
+               }
+       if (BIO_write_filename(out,buf[0]) <= 0)
+               {
+               perror(serialfile);
+               goto err;
+               }
+
+       if ((ai=BN_to_ASN1_INTEGER(serial,NULL)) == NULL)
+               {
+               BIO_printf(bio_err,"error converting serial to ASN.1 format\n");
+               goto err;
+               }
+       i2a_ASN1_INTEGER(out,ai);
+       BIO_puts(out,"\n");
+       ret=1;
+       if (retai)
+               {
+               *retai = ai;
+               ai = NULL;
+               }
+err:
+       if (out != NULL) BIO_free_all(out);
+       if (ai != NULL) ASN1_INTEGER_free(ai);
+       return(ret);
+       }
+
+int rotate_serial(char *serialfile, char *new_suffix, char *old_suffix)
+       {
+       char buf[5][BSIZE];
+       int i,j;
+       struct stat sb;
+
+       i = strlen(serialfile) + strlen(old_suffix);
+       j = strlen(serialfile) + strlen(new_suffix);
+       if (i > j) j = i;
+       if (j + 1 >= BSIZE)
+               {
+               BIO_printf(bio_err,"file name too long\n");
+               goto err;
+               }
+
+#ifndef OPENSSL_SYS_VMS
+       j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s",
+               serialfile, new_suffix);
+#else
+       j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s",
+               serialfile, new_suffix);
+#endif
+#ifndef OPENSSL_SYS_VMS
+       j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s",
+               serialfile, old_suffix);
+#else
+       j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s",
+               serialfile, old_suffix);
+#endif
+       if (stat(serialfile,&sb) < 0)
+               {
+               if (errno != ENOENT 
+#ifdef ENOTDIR
+                       && errno != ENOTDIR)
+#endif
+                       goto err;
+               }
+       else
+               {
+#ifdef RL_DEBUG
+               BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
+                       serialfile, buf[1]);
+#endif
+               if (rename(serialfile,buf[1]) < 0)
+                       {
+                       BIO_printf(bio_err,
+                               "unable to rename %s to %s\n",
+                               serialfile, buf[1]);
+                       perror("reason");
+                       goto err;
+                       }
+               }
+#ifdef RL_DEBUG
+       BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
+               buf[0],serialfile);
+#endif
+       if (rename(buf[0],serialfile) < 0)
+               {
+               BIO_printf(bio_err,
+                       "unable to rename %s to %s\n",
+                       buf[0],serialfile);
+               perror("reason");
+               rename(buf[1],serialfile);
+               goto err;
+               }
+       return 1;
+ err:
+       return 0;
+       }
+
+int rand_serial(BIGNUM *b, ASN1_INTEGER *ai)
+       {
+       BIGNUM *btmp;
+       int ret = 0;
+       if (b)
+               btmp = b;
+       else
+               btmp = BN_new();
+
+       if (!btmp)
+               return 0;
+
+       if (!BN_pseudo_rand(btmp, SERIAL_RAND_BITS, 0, 0))
+               goto error;
+       if (ai && !BN_to_ASN1_INTEGER(btmp, ai))
+               goto error;
+
+       ret = 1;
+       
+       error:
+
+       if (!b)
+               BN_free(btmp);
+       
+       return ret;
+       }
+
+CA_DB *load_index(char *dbfile, DB_ATTR *db_attr)
+       {
+       CA_DB *retdb = NULL;
+       TXT_DB *tmpdb = NULL;
+       BIO *in = BIO_new(BIO_s_file());
+       CONF *dbattr_conf = NULL;
+       char buf[1][BSIZE];
+       long errorline= -1;
+
+       if (in == NULL)
+               {
+               ERR_print_errors(bio_err);
+               goto err;
+               }
+       if (BIO_read_filename(in,dbfile) <= 0)
+               {
+               perror(dbfile);
+               BIO_printf(bio_err,"unable to open '%s'\n",dbfile);
+               goto err;
+               }
+       if ((tmpdb = TXT_DB_read(in,DB_NUMBER)) == NULL)
+               {
+               if (tmpdb != NULL) TXT_DB_free(tmpdb);
+               goto err;
+               }
+
+#ifndef OPENSSL_SYS_VMS
+       BIO_snprintf(buf[0], sizeof buf[0], "%s.attr", dbfile);
+#else
+       BIO_snprintf(buf[0], sizeof buf[0], "%s-attr", dbfile);
+#endif
+       dbattr_conf = NCONF_new(NULL);
+       if (NCONF_load(dbattr_conf,buf[0],&errorline) <= 0)
+               {
+               if (errorline > 0)
+                       {
+                       BIO_printf(bio_err,
+                               "error on line %ld of db attribute file '%s'\n"
+                               ,errorline,buf[0]);
+                       goto err;
+                       }
+               else
+                       {
+                       NCONF_free(dbattr_conf);
+                       dbattr_conf = NULL;
+                       }
+               }
+
+       if ((retdb = OPENSSL_malloc(sizeof(CA_DB))) == NULL)
+               {
+               fprintf(stderr, "Out of memory\n");
+               goto err;
+               }
+
+       retdb->db = tmpdb;
+       tmpdb = NULL;
+       if (db_attr)
+               retdb->attributes = *db_attr;
+       else
+               {
+               retdb->attributes.unique_subject = 1;
+               }
+
+       if (dbattr_conf)
+               {
+               char *p = NCONF_get_string(dbattr_conf,NULL,"unique_subject");
+               if (p)
+                       {
+#ifdef RL_DEBUG
+                       BIO_printf(bio_err, "DEBUG[load_index]: unique_subject = \"%s\"\n", p);
+#endif
+                       retdb->attributes.unique_subject = parse_yesno(p,1);
+                       }
+               }
+
+ err:
+       if (dbattr_conf) NCONF_free(dbattr_conf);
+       if (tmpdb) TXT_DB_free(tmpdb);
+       if (in) BIO_free_all(in);
+       return retdb;
+       }
+
+int index_index(CA_DB *db)
+       {
+       if (!TXT_DB_create_index(db->db, DB_serial, NULL,
+                               LHASH_HASH_FN(index_serial_hash),
+                               LHASH_COMP_FN(index_serial_cmp)))
+               {
+               BIO_printf(bio_err,
+                 "error creating serial number index:(%ld,%ld,%ld)\n",
+                                       db->db->error,db->db->arg1,db->db->arg2);
+                       return 0;
+               }
+
+       if (db->attributes.unique_subject
+               && !TXT_DB_create_index(db->db, DB_name, index_name_qual,
+                       LHASH_HASH_FN(index_name_hash),
+                       LHASH_COMP_FN(index_name_cmp)))
+               {
+               BIO_printf(bio_err,"error creating name index:(%ld,%ld,%ld)\n",
+                       db->db->error,db->db->arg1,db->db->arg2);
+               return 0;
+               }
+       return 1;
+       }
+
+int save_index(char *dbfile, char *suffix, CA_DB *db)
+       {
+       char buf[3][BSIZE];
+       BIO *out = BIO_new(BIO_s_file());
+       int j;
+
+       if (out == NULL)
+               {
+               ERR_print_errors(bio_err);
+               goto err;
+               }
+
+       j = strlen(dbfile) + strlen(suffix);
+       if (j + 6 >= BSIZE)
+               {
+               BIO_printf(bio_err,"file name too long\n");
+               goto err;
+               }
+
+#ifndef OPENSSL_SYS_VMS
+       j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr", dbfile);
+#else
+       j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr", dbfile);
+#endif
+#ifndef OPENSSL_SYS_VMS
+       j = BIO_snprintf(buf[1], sizeof buf[1], "%s.attr.%s", dbfile, suffix);
+#else
+       j = BIO_snprintf(buf[1], sizeof buf[1], "%s-attr-%s", dbfile, suffix);
+#endif
+#ifndef OPENSSL_SYS_VMS
+       j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", dbfile, suffix);
+#else
+       j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", dbfile, suffix);
+#endif
+#ifdef RL_DEBUG
+       BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]);
+#endif
+       if (BIO_write_filename(out,buf[0]) <= 0)
+               {
+               perror(dbfile);
+               BIO_printf(bio_err,"unable to open '%s'\n", dbfile);
+               goto err;
+               }
+       j=TXT_DB_write(out,db->db);
+       if (j <= 0) goto err;
+                       
+       BIO_free(out);
+
+       out = BIO_new(BIO_s_file());
+#ifdef RL_DEBUG
+       BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[1]);
+#endif
+       if (BIO_write_filename(out,buf[1]) <= 0)
+               {
+               perror(buf[2]);
+               BIO_printf(bio_err,"unable to open '%s'\n", buf[2]);
+               goto err;
+               }
+       BIO_printf(out,"unique_subject = %s\n",
+               db->attributes.unique_subject ? "yes" : "no");
+       BIO_free(out);
+
+       return 1;
+ err:
+       return 0;
+       }
+
+int rotate_index(char *dbfile, char *new_suffix, char *old_suffix)
+       {
+       char buf[5][BSIZE];
+       int i,j;
+       struct stat sb;
+
+       i = strlen(dbfile) + strlen(old_suffix);
+       j = strlen(dbfile) + strlen(new_suffix);
+       if (i > j) j = i;
+       if (j + 6 >= BSIZE)
+               {
+               BIO_printf(bio_err,"file name too long\n");
+               goto err;
+               }
+
+#ifndef OPENSSL_SYS_VMS
+       j = BIO_snprintf(buf[4], sizeof buf[4], "%s.attr", dbfile);
+#else
+       j = BIO_snprintf(buf[4], sizeof buf[4], "%s-attr", dbfile);
+#endif
+#ifndef OPENSSL_SYS_VMS
+       j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr.%s",
+               dbfile, new_suffix);
+#else
+       j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr-%s",
+               dbfile, new_suffix);
+#endif
+#ifndef OPENSSL_SYS_VMS
+       j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s",
+               dbfile, new_suffix);
+#else
+       j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s",
+               dbfile, new_suffix);
+#endif
+#ifndef OPENSSL_SYS_VMS
+       j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s",
+               dbfile, old_suffix);
+#else
+       j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s",
+               dbfile, old_suffix);
+#endif
+#ifndef OPENSSL_SYS_VMS
+       j = BIO_snprintf(buf[3], sizeof buf[3], "%s.attr.%s",
+               dbfile, old_suffix);
+#else
+       j = BIO_snprintf(buf[3], sizeof buf[3], "%s-attr-%s",
+               dbfile, old_suffix);
+#endif
+       if (stat(dbfile,&sb) < 0)
+               {
+               if (errno != ENOENT 
+#ifdef ENOTDIR
+                       && errno != ENOTDIR)
+#endif
+                       goto err;
+               }
+       else
+               {
+#ifdef RL_DEBUG
+               BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
+                       dbfile, buf[1]);
+#endif
+               if (rename(dbfile,buf[1]) < 0)
+                       {
+                       BIO_printf(bio_err,
+                               "unable to rename %s to %s\n",
+                               dbfile, buf[1]);
+                       perror("reason");
+                       goto err;
+                       }
+               }
+#ifdef RL_DEBUG
+       BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
+               buf[0],dbfile);
+#endif
+       if (rename(buf[0],dbfile) < 0)
+               {
+               BIO_printf(bio_err,
+                       "unable to rename %s to %s\n",
+                       buf[0],dbfile);
+               perror("reason");
+               rename(buf[1],dbfile);
+               goto err;
+               }
+       if (stat(buf[4],&sb) < 0)
+               {
+               if (errno != ENOENT 
+#ifdef ENOTDIR
+                       && errno != ENOTDIR)
+#endif
+                       goto err;
+               }
+       else
+               {
+#ifdef RL_DEBUG
+               BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
+                       buf[4],buf[3]);
+#endif
+               if (rename(buf[4],buf[3]) < 0)
+                       {
+                       BIO_printf(bio_err,
+                               "unable to rename %s to %s\n",
+                               buf[4], buf[3]);
+                       perror("reason");
+                       rename(dbfile,buf[0]);
+                       rename(buf[1],dbfile);
+                       goto err;
+                       }
+               }
+#ifdef RL_DEBUG
+       BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
+               buf[2],buf[4]);
+#endif
+       if (rename(buf[2],buf[4]) < 0)
+               {
+               BIO_printf(bio_err,
+                       "unable to rename %s to %s\n",
+                       buf[2],buf[4]);
+               perror("reason");
+               rename(buf[3],buf[4]);
+               rename(dbfile,buf[0]);
+               rename(buf[1],dbfile);
+               goto err;
+               }
+       return 1;
+ err:
+       return 0;
+       }
+
+void free_index(CA_DB *db)
+       {
+       if (db)
+               {
+               if (db->db) TXT_DB_free(db->db);
+               OPENSSL_free(db);
+               }
+       }
+
+int parse_yesno(char *str, int def)
+       {
+       int ret = def;
+       if (str)
+               {
+               switch (*str)
+                       {
+               case 'f': /* false */
+               case 'F': /* FALSE */
+               case 'n': /* no */
+               case 'N': /* NO */
+               case '0': /* 0 */
+                       ret = 0;
+                       break;
+               case 't': /* true */
+               case 'T': /* TRUE */
+               case 'y': /* yes */
+               case 'Y': /* YES */
+               case '1': /* 1 */
+                       ret = 0;
+                       break;
+               default:
+                       ret = def;
+                       break;
+                       }
+               }
+       return ret;
+       }
+
+/*
+ * subject is expected to be in the format /type0=value0/type1=value1/type2=...
+ * where characters may be escaped by \
+ */
+X509_NAME *parse_name(char *subject, long chtype, int multirdn)
+       {
+       size_t buflen = strlen(subject)+1; /* to copy the types and values into. due to escaping, the copy can only become shorter */
+       char *buf = OPENSSL_malloc(buflen);
+       size_t max_ne = buflen / 2 + 1; /* maximum number of name elements */
+       char **ne_types = OPENSSL_malloc(max_ne * sizeof (char *));
+       char **ne_values = OPENSSL_malloc(max_ne * sizeof (char *));
+       int *mval = OPENSSL_malloc (max_ne * sizeof (int));
+
+       char *sp = subject, *bp = buf;
+       int i, ne_num = 0;
+
+       X509_NAME *n = NULL;
+       int nid;
+
+       if (!buf || !ne_types || !ne_values)
+               {
+               BIO_printf(bio_err, "malloc error\n");
+               goto error;
+               }       
+
+       if (*subject != '/')
+               {
+               BIO_printf(bio_err, "Subject does not start with '/'.\n");
+               goto error;
+               }
+       sp++; /* skip leading / */
+
+       /* no multivalued RDN by default */
+       mval[ne_num] = 0;
+
+       while (*sp)
+               {
+               /* collect type */
+               ne_types[ne_num] = bp;
+               while (*sp)
+                       {
+                       if (*sp == '\\') /* is there anything to escape in the type...? */
+                               {
+                               if (*++sp)
+                                       *bp++ = *sp++;
+                               else    
+                                       {
+                                       BIO_printf(bio_err, "escape character at end of string\n");
+                                       goto error;
+                                       }
+                               }       
+                       else if (*sp == '=')
+                               {
+                               sp++;
+                               *bp++ = '\0';
+                               break;
+                               }
+                       else
+                               *bp++ = *sp++;
+                       }
+               if (!*sp)
+                       {
+                       BIO_printf(bio_err, "end of string encountered while processing type of subject name element #%d\n", ne_num);
+                       goto error;
+                       }
+               ne_values[ne_num] = bp;
+               while (*sp)
+                       {
+                       if (*sp == '\\')
+                               {
+                               if (*++sp)
+                                       *bp++ = *sp++;
+                               else
+                                       {
+                                       BIO_printf(bio_err, "escape character at end of string\n");
+                                       goto error;
+                                       }
+                               }
+                       else if (*sp == '/')
+                               {
+                               sp++;
+                               /* no multivalued RDN by default */
+                               mval[ne_num+1] = 0;
+                               break;
+                               }
+                       else if (*sp == '+' && multirdn)
+                               {
+                               /* a not escaped + signals a mutlivalued RDN */
+                               sp++;
+                               mval[ne_num+1] = -1;
+                               break;
+                               }
+                       else
+                               *bp++ = *sp++;
+                       }
+               *bp++ = '\0';
+               ne_num++;
+               }       
+
+       if (!(n = X509_NAME_new()))
+               goto error;
+
+       for (i = 0; i < ne_num; i++)
+               {
+               if ((nid=OBJ_txt2nid(ne_types[i])) == NID_undef)
+                       {
+                       BIO_printf(bio_err, "Subject Attribute %s has no known NID, skipped\n", ne_types[i]);
+                       continue;
+                       }
+
+               if (!*ne_values[i])
+                       {
+                       BIO_printf(bio_err, "No value provided for Subject Attribute %s, skipped\n", ne_types[i]);
+                       continue;
+                       }
+
+               if (!X509_NAME_add_entry_by_NID(n, nid, chtype, (unsigned char*)ne_values[i], -1,-1,mval[i]))
+                       goto error;
+               }
+
+       OPENSSL_free(ne_values);
+       OPENSSL_free(ne_types);
+       OPENSSL_free(buf);
+       return n;
+
+error:
+       X509_NAME_free(n);
+       if (ne_values)
+               OPENSSL_free(ne_values);
+       if (ne_types)
+               OPENSSL_free(ne_types);
+       if (buf)
+               OPENSSL_free(buf);
+       return NULL;
+}
+
+/* This code MUST COME AFTER anything that uses rename() */
+#ifdef OPENSSL_SYS_WIN32
+int WIN32_rename(char *from, char *to)
+       {
+#ifndef OPENSSL_SYS_WINCE
+       /* Windows rename gives an error if 'to' exists, so delete it
+        * first and ignore file not found errror
+        */
+       if((remove(to) != 0) && (errno != ENOENT))
+               return -1;
+#undef rename
+       return rename(from, to);
+#else
+       /* convert strings to UNICODE */
+       {
+       BOOL result = FALSE;
+       WCHAR* wfrom;
+       WCHAR* wto;
+       int i;
+       wfrom = malloc((strlen(from)+1)*2);
+       wto = malloc((strlen(to)+1)*2);
+       if (wfrom != NULL && wto != NULL)
+               {
+               for (i=0; i<(int)strlen(from)+1; i++)
+                       wfrom[i] = (short)from[i];
+               for (i=0; i<(int)strlen(to)+1; i++)
+                       wto[i] = (short)to[i];
+               result = MoveFile(wfrom, wto);
+               }
+       if (wfrom != NULL)
+               free(wfrom);
+       if (wto != NULL)
+               free(wto);
+       return result;
+       }
+#endif
+       }
+#endif