X-Git-Url: https://git.openssl.org/?a=blobdiff_plain;f=apps%2Fx509.c;h=12888c0486f30c74afbc9bf90c3c97bcc31181e9;hb=d0ff2a237bfba01dda49a32a76e7551cf58a06cc;hp=1ae673d4698721c456e99aa3de52c654df75416b;hpb=acba75c59dc6c42b39caab6168d1284ae0f09e8f;p=openssl.git diff --git a/apps/x509.c b/apps/x509.c index 1ae673d469..12888c0486 100644 --- a/apps/x509.c +++ b/apps/x509.c @@ -60,7 +60,7 @@ #include #include #include -#ifdef NO_STDIO +#ifdef OPENSSL_NO_STDIO #define APPS_WIN16 #endif #include "apps.h" @@ -73,7 +73,6 @@ #include #include #include -#include #undef PROG #define PROG x509_main @@ -139,10 +138,10 @@ NULL static int MS_CALLBACK callb(int ok, X509_STORE_CTX *ctx); static int sign (X509 *x, EVP_PKEY *pkey,int days,int clrext, const EVP_MD *digest, - LHASH *conf, char *section); + CONF *conf, char *section); static int x509_certify (X509_STORE *ctx,char *CAfile,const EVP_MD *digest, X509 *x,X509 *xca,EVP_PKEY *pkey,char *serial, - int create,int days, int clrext, LHASH *conf, char *section, + int create,int days, int clrext, CONF *conf, char *section, ASN1_INTEGER *sno); static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt); static int reqfile=0; @@ -179,7 +178,7 @@ int MAIN(int argc, char **argv) int fingerprint=0; char buf[256]; const EVP_MD *md_alg,*digest=EVP_md5(); - LHASH *extconf = NULL; + CONF *extconf = NULL; char *extsect = NULL, *extfile = NULL, *passin = NULL, *passargin = NULL; int need_rand = 0; int checkend=0,checkoffset=0; @@ -192,8 +191,11 @@ int MAIN(int argc, char **argv) if (bio_err == NULL) bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); + + if (!load_config(bio_err, NULL)) + goto end; STDout=BIO_new_fp(stdout,BIO_NOCLOSE); -#ifdef VMS +#ifdef OPENSSL_SYS_VMS { BIO *tmpbio = BIO_new(BIO_f_linebuffer()); STDout = BIO_push(tmpbio, STDout); @@ -448,23 +450,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); @@ -495,7 +481,8 @@ bad: { long errorline; X509V3_CTX ctx2; - if (!(extconf=CONF_load(NULL,extfile,&errorline))) + extconf = NCONF_new(NULL); + if (!NCONF_load(extconf, extfile,&errorline)) { if (errorline <= 0) BIO_printf(bio_err, @@ -509,7 +496,7 @@ bad: } if (!extsect) { - extsect = CONF_get_string(extconf, "default", "extensions"); + extsect = NCONF_get_string(extconf, "default", "extensions"); if (!extsect) { ERR_clear_error(); @@ -517,8 +504,8 @@ bad: } } X509V3_set_ctx_test(&ctx2); - X509V3_set_conf_lhash(&ctx2, extconf); - if (!X509V3_EXT_add_conf(extconf, &ctx2, extsect, NULL)) + X509V3_set_nconf(&ctx2, extconf); + if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) { BIO_printf(bio_err, "Error Loading extension section %s\n", @@ -619,12 +606,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; } @@ -642,7 +629,7 @@ bad: if (outfile == NULL) { BIO_set_fp(out,stdout,BIO_NOCLOSE); -#ifdef VMS +#ifdef OPENSSL_SYS_VMS { BIO *tmpbio = BIO_new(BIO_f_linebuffer()); out = BIO_push(tmpbio, out); @@ -746,12 +733,12 @@ bad: goto end; } BIO_printf(STDout,"Modulus="); -#ifndef NO_RSA +#ifndef OPENSSL_NO_RSA if (pkey->type == EVP_PKEY_RSA) BN_print(STDout,pkey->pkey.rsa->n); else #endif -#ifndef NO_DSA +#ifndef OPENSSL_NO_DSA if (pkey->type == EVP_PKEY_DSA) BN_print(STDout,pkey->pkey.dsa->pub_key); else @@ -874,13 +861,18 @@ 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 NO_DSA +#ifndef OPENSSL_NO_DSA if (Upkey->type == EVP_PKEY_DSA) digest=EVP_dss1(); #endif +#ifndef OPENSSL_NO_ECDSA + if (Upkey->type == EVP_PKEY_ECDSA) + digest=EVP_ecdsa(); +#endif assert(need_rand); if (!sign(x,Upkey,days,clrext,digest, @@ -893,13 +885,17 @@ bad: { CApkey=load_key(bio_err, CAkeyfile,CAkeyformat, passin, - e); + e, "CA Private Key"); if (CApkey == NULL) goto end; } -#ifndef NO_DSA +#ifndef OPENSSL_NO_DSA if (CApkey->type == EVP_PKEY_DSA) digest=EVP_dss1(); #endif +#ifndef OPENSSL_NO_ECDSA + if (CApkey->type == EVP_PKEY_ECDSA) + digest = EVP_ecdsa(); +#endif assert(need_rand); if (!x509_certify(ctx,CAfile,digest,x,xca, @@ -920,7 +916,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; } @@ -928,6 +925,8 @@ bad: if (pk->type == EVP_PKEY_DSA) digest=EVP_dss1(); + else if (pk->type == EVP_PKEY_ECDSA) + digest=EVP_ecdsa(); rq=X509_to_X509_REQ(x,pk,digest); EVP_PKEY_free(pk); @@ -1009,7 +1008,7 @@ end: if (need_rand) app_RAND_write_file(NULL, bio_err); OBJ_cleanup(); - CONF_free(extconf); + NCONF_free(extconf); BIO_free_all(out); BIO_free_all(STDout); X509_STORE_free(ctx); @@ -1023,6 +1022,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 +1030,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; + BIGNUM *serial = NULL; + buf=OPENSSL_malloc( ((serialfile == NULL) ?(strlen(CAfile)+strlen(POSTFIX)+1) :(strlen(serialfile)))+1); @@ -1099,22 +1100,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); @@ -1124,7 +1129,7 @@ static ASN1_INTEGER *load_serial(char *CAfile, char *serialfile, int create) static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest, X509 *x, X509 *xca, EVP_PKEY *pkey, char *serialfile, int create, - int days, int clrext, LHASH *conf, char *section, ASN1_INTEGER *sno) + int days, int clrext, CONF *conf, char *section, ASN1_INTEGER *sno) { int ret=0; ASN1_INTEGER *bs=NULL; @@ -1135,7 +1140,11 @@ static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest, EVP_PKEY_copy_parameters(upkey,pkey); EVP_PKEY_free(upkey); - X509_STORE_CTX_init(&xsc,ctx,x,NULL); + if(!X509_STORE_CTX_init(&xsc,ctx,x,NULL)) + { + BIO_printf(bio_err,"Error initialising X509 store\n"); + goto end; + } if (sno) bs = sno; else if (!(bs = load_serial(CAfile, serialfile, create))) goto end; @@ -1174,8 +1183,8 @@ static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest, X509V3_CTX ctx2; X509_set_version(x,2); /* version 3 certificate */ X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0); - X509V3_set_conf_lhash(&ctx2, conf); - if (!X509V3_EXT_add_conf(conf, &ctx2, section, x)) goto end; + X509V3_set_nconf(&ctx2, conf); + if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x)) goto end; } if (!X509_sign(x,pkey,digest)) goto end; @@ -1221,7 +1230,7 @@ static int MS_CALLBACK callb(int ok, X509_STORE_CTX *ctx) /* self sign */ static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, const EVP_MD *digest, - LHASH *conf, char *section) + CONF *conf, char *section) { EVP_PKEY *pktmp; @@ -1251,8 +1260,8 @@ static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, const EVP_MD *dig X509V3_CTX ctx; X509_set_version(x,2); /* version 3 certificate */ X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0); - X509V3_set_conf_lhash(&ctx, conf); - if (!X509V3_EXT_add_conf(conf, &ctx, section, x)) goto err; + X509V3_set_nconf(&ctx, conf); + if (!X509V3_EXT_add_nconf(conf, &ctx, section, x)) goto err; } if (!X509_sign(x,pkey,digest)) goto err; return 1; @@ -1277,6 +1286,3 @@ static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt) } return 1; } - - -