Make better use of load_cert, load_certs and load_key.
[oweals/openssl.git] / apps / x509.c
index 658afcef5ed1a7e4cf4e3a915f6f52235cb30beb..8a392d813a3515c51b650adde75ad51d65b7643c 100644 (file)
@@ -448,23 +448,7 @@ bad:
                goto end;
                }
 
-       if (engine != NULL)
-               {
-               if((e = ENGINE_by_id(engine)) == NULL)
-                       {
-                       BIO_printf(bio_err,"invalid engine \"%s\"\n",
-                               engine);
-                       goto end;
-                       }
-               if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
-                       {
-                       BIO_printf(bio_err,"can't use that engine\n");
-                       goto end;
-                       }
-               BIO_printf(bio_err,"engine \"%s\" set.\n", engine);
-               /* Free our "structural" reference. */
-               ENGINE_free(e);
-               }
+        e = setup_engine(bio_err, engine, 0);
 
        if (need_rand)
                app_RAND_load_file(NULL, bio_err, 0);
@@ -619,12 +603,12 @@ bad:
                EVP_PKEY_free(pkey);
                }
        else
-               x=load_cert(bio_err,infile,informat);
+               x=load_cert(bio_err,infile,informat,NULL,e,"Certificate");
 
        if (x == NULL) goto end;
        if (CA_flag)
                {
-               xca=load_cert(bio_err,CAfile,CAformat);
+               xca=load_cert(bio_err,CAfile,CAformat,NULL,e,"CA Certificate");
                if (xca == NULL) goto end;
                }
 
@@ -874,7 +858,8 @@ bad:
                                if (Upkey == NULL)
                                        {
                                        Upkey=load_key(bio_err,
-                                               keyfile,keyformat, passin, e);
+                                               keyfile,keyformat, passin, e,
+                                               "Private key");
                                        if (Upkey == NULL) goto end;
                                        }
 #ifndef OPENSSL_NO_DSA
@@ -893,7 +878,7 @@ bad:
                                        {
                                        CApkey=load_key(bio_err,
                                                CAkeyfile,CAkeyformat, passin,
-                                               e);
+                                               e, "CA Private Key");
                                        if (CApkey == NULL) goto end;
                                        }
 #ifndef OPENSSL_NO_DSA
@@ -920,7 +905,8 @@ bad:
                                else
                                        {
                                        pk=load_key(bio_err,
-                                               keyfile,FORMAT_PEM, passin, e);
+                                               keyfile,FORMAT_PEM, passin, e,
+                                               "request key");
                                        if (pk == NULL) goto end;
                                        }
 
@@ -1023,6 +1009,7 @@ end:
        sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
        sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
        if (passin) OPENSSL_free(passin);
+       apps_shutdown();
        EXIT(ret);
        }
 
@@ -1030,9 +1017,10 @@ static ASN1_INTEGER *load_serial(char *CAfile, char *serialfile, int create)
        {
        char *buf = NULL, *p;
        MS_STATIC char buf2[1024];
-       ASN1_INTEGER *bs = NULL, bs2;
+       ASN1_INTEGER *bs = NULL, *bs2 = NULL;
        BIO *io = NULL;
        BIGNUM *serial = NULL;
+
        buf=OPENSSL_malloc( ((serialfile == NULL)
                        ?(strlen(CAfile)+strlen(POSTFIX)+1)
                        :(strlen(serialfile)))+1);
@@ -1099,22 +1087,26 @@ static ASN1_INTEGER *load_serial(char *CAfile, char *serialfile, int create)
 
        if (!BN_add_word(serial,1))
                { BIO_printf(bio_err,"add_word failure\n"); goto end; }
-       bs2.data=(unsigned char *)buf2;
-       bs2.length=BN_bn2bin(serial,bs2.data);
-
+       if (!(bs2 = BN_to_ASN1_INTEGER(serial, NULL)))
+               { BIO_printf(bio_err,"error converting bn 2 asn1_integer\n"); goto end; }
        if (BIO_write_filename(io,buf) <= 0)
                {
                BIO_printf(bio_err,"error attempting to write serial number file\n");
                perror(buf);
                goto end;
                }
-       i2a_ASN1_INTEGER(io,&bs2);
+       i2a_ASN1_INTEGER(io,bs2);
        BIO_puts(io,"\n");
+
        BIO_free(io);
+       if (buf) OPENSSL_free(buf);
+       ASN1_INTEGER_free(bs2);
+       BN_free(serial);
        io=NULL;
        return bs;
 
        end:
+       if (buf) OPENSSL_free(buf);
        BIO_free(io);
        ASN1_INTEGER_free(bs);
        BN_free(serial);
@@ -1277,6 +1269,3 @@ static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
                }
        return 1;
 }
-
-
-