From 3cbb7937fa9ab7cbdab57ff37575d223b6153ec2 Mon Sep 17 00:00:00 2001 From: "Dr. Stephen Henson" Date: Sat, 5 Jun 1999 01:45:20 +0000 Subject: [PATCH] Add d2i,i2d bio and fp functions for PKCS#8 and add -inform and -outform arguments to pkcs8 application. --- apps/pkcs8.c | 30 +++++++++++++++++++++---- crypto/x509/x509.h | 10 +++++++++ crypto/x509/x_all.c | 54 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/apps/pkcs8.c b/apps/pkcs8.c index 07359d6515..442eced7ef 100644 --- a/apps/pkcs8.c +++ b/apps/pkcs8.c @@ -72,6 +72,7 @@ int MAIN(int argc, char **argv) int topk8 = 0; int pbe_nid = -1; int iter = PKCS12_DEFAULT_ITER; + int informat, outformat; int p8_broken = PKCS8_OK; X509_SIG *p8; PKCS8_PRIV_KEY_INFO *p8inf; @@ -79,11 +80,23 @@ int MAIN(int argc, char **argv) char pass[50]; int badarg = 0; if (bio_err == NULL) bio_err = BIO_new_fp (stderr, BIO_NOCLOSE); + informat=FORMAT_PEM; + outformat=FORMAT_PEM; ERR_load_crypto_strings(); SSLeay_add_all_algorithms(); args = argv + 1; while (!badarg && *args && *args[0] == '-') { - if (!strcmp (*args, "-topk8")) topk8 = 1; + if (!strcmp(*args,"-inform")) { + if (args[1]) { + args++; + informat=str2fmt(*args); + } else badarg = 1; + } else if (!strcmp(*args,"-outform")) { + if (args[1]) { + args++; + outformat=str2fmt(*args); + } else badarg = 1; + } else if (!strcmp (*args, "-topk8")) topk8 = 1; else if (!strcmp (*args, "-noiter")) iter = 1; else if (!strcmp (*args, "-nooct")) p8_broken = PKCS8_NO_OCTET; else if (!strcmp (*args, "-in")) { @@ -114,7 +127,7 @@ int MAIN(int argc, char **argv) if (pbe_nid == -1) pbe_nid = NID_pbeWithMD5AndDES_CBC; if (infile) { - if (!(in = BIO_new_file (infile, "r"))) { + if (!(in = BIO_new_file (infile, "rb"))) { BIO_printf (bio_err, "Can't open input file %s\n", infile); return (1); @@ -122,7 +135,7 @@ int MAIN(int argc, char **argv) } else in = BIO_new_fp (stdin, BIO_NOCLOSE); if (outfile) { - if (!(out = BIO_new_file (outfile, "w"))) { + if (!(out = BIO_new_file (outfile, "wb"))) { BIO_printf (bio_err, "Can't open output file %s\n", outfile); return (1); @@ -154,7 +167,16 @@ int MAIN(int argc, char **argv) return (0); } - if (!(p8 = PEM_read_bio_PKCS8 (in, NULL, NULL))) { + if(informat == FORMAT_PEM) + p8 = PEM_read_bio_PKCS8(in, NULL, NULL); + else if(informat == FORMAT_ASN1) + p8 = d2i_PKCS8_bio(in, NULL); + else { + BIO_printf(bio_err, "Bad input format specified for key\n"); + return (1); + } + + if (!p8) { BIO_printf (bio_err, "Error reading key\n", outfile); ERR_print_errors(bio_err); return (1); diff --git a/crypto/x509/x509.h b/crypto/x509/x509.h index d15712c561..0ed0494d21 100644 --- a/crypto/x509/x509.h +++ b/crypto/x509/x509.h @@ -581,6 +581,11 @@ int i2d_RSAPublicKey_fp(FILE *fp,RSA *rsa); #ifndef NO_DSA DSA *d2i_DSAPrivateKey_fp(FILE *fp, DSA **dsa); int i2d_DSAPrivateKey_fp(FILE *fp, DSA *dsa); +X509_SIG *d2i_PKCS8_fp(FILE *fp,X509_SIG **p8); +int i2d_PKCS8_fp(FILE *fp,X509_SIG *p8); +PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, + PKCS8_PRIV_KEY_INFO **p8inf); +int i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp,PKCS8_PRIV_KEY_INFO *p8inf); #endif #endif @@ -600,6 +605,11 @@ int i2d_RSAPublicKey_bio(BIO *bp,RSA *rsa); #ifndef NO_DSA DSA *d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa); int i2d_DSAPrivateKey_bio(BIO *bp, DSA *dsa); +X509_SIG *d2i_PKCS8_bio(BIO *bp,X509_SIG **p8); +int i2d_PKCS8_bio(BIO *bp,X509_SIG *p8); +PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, + PKCS8_PRIV_KEY_INFO **p8inf); +int i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp,PKCS8_PRIV_KEY_INFO *p8inf); #endif #endif diff --git a/crypto/x509/x_all.c b/crypto/x509/x_all.c index 48f048a065..f2af895df0 100644 --- a/crypto/x509/x_all.c +++ b/crypto/x509/x_all.c @@ -381,3 +381,57 @@ int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data, EVP_MD *type, (char *)data,md,len)); } + +#ifndef NO_FP_API +X509_SIG *d2i_PKCS8_fp(FILE *fp, X509_SIG **p8) + { + return((X509_SIG *)ASN1_d2i_fp((char *(*)())X509_SIG_new, + (char *(*)())d2i_X509_SIG, (fp),(unsigned char **)(p8))); + } + +int i2d_PKCS8_fp(FILE *fp, X509_SIG *p8) + { + return(ASN1_i2d_fp(i2d_X509_SIG,fp,(unsigned char *)p8)); + } +#endif + +X509_SIG *d2i_PKCS8_bio(BIO *bp, X509_SIG **p8) + { + return((X509_SIG *)ASN1_d2i_bio((char *(*)())X509_SIG_new, + (char *(*)())d2i_X509_SIG, (bp),(unsigned char **)(p8))); + } + +int i2d_PKCS8_bio(BIO *bp, X509_SIG *p8) + { + return(ASN1_i2d_bio(i2d_X509_SIG,bp,(unsigned char *)p8)); + } + +#ifndef NO_FP_API +PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, + PKCS8_PRIV_KEY_INFO **p8inf) + { + return((PKCS8_PRIV_KEY_INFO *)ASN1_d2i_fp( + (char *(*)())PKCS8_PRIV_KEY_INFO_new, + (char *(*)())d2i_PKCS8_PRIV_KEY_INFO, (fp), + (unsigned char **)(p8inf))); + } + +int i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, PKCS8_PRIV_KEY_INFO *p8inf) + { + return(ASN1_i2d_fp(i2d_PKCS8_PRIV_KEY_INFO,fp,(unsigned char *)p8inf)); + } +#endif + +PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, + PKCS8_PRIV_KEY_INFO **p8inf) + { + return((PKCS8_PRIV_KEY_INFO *)ASN1_d2i_bio( + (char *(*)())PKCS8_PRIV_KEY_INFO_new, + (char *(*)())d2i_PKCS8_PRIV_KEY_INFO, (bp), + (unsigned char **)(p8inf))); + } + +int i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, PKCS8_PRIV_KEY_INFO *p8inf) + { + return(ASN1_i2d_bio(i2d_PKCS8_PRIV_KEY_INFO,bp,(unsigned char *)p8inf)); + } -- 2.25.1