Make it possible to load keys from stdin, and restore that
authorRichard Levitte <levitte@openssl.org>
Thu, 1 Aug 2002 16:28:40 +0000 (16:28 +0000)
committerRichard Levitte <levitte@openssl.org>
Thu, 1 Aug 2002 16:28:40 +0000 (16:28 +0000)
functionality in the programs that had that before.
Part fo PR 164

13 files changed:
apps/apps.c
apps/apps.h
apps/ca.c
apps/dgst.c
apps/ocsp.c
apps/pkcs12.c
apps/pkcs8.c
apps/req.c
apps/rsa.c
apps/rsautl.c
apps/smime.c
apps/spkac.c
apps/x509.c

index a302119d7f0250079a4bc8508b006aad01c6394b..6f64e6313f632964f9a49b440326442db79f97c7 100644 (file)
@@ -798,7 +798,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;
@@ -808,7 +808,7 @@ 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;
@@ -828,12 +828,19 @@ EVP_PKEY *load_key(BIO *err, const char *file, int format,
                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);
@@ -867,7 +874,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;
@@ -877,7 +884,7 @@ 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;
@@ -897,11 +904,18 @@ EVP_PKEY *load_pubkey(BIO *err, const char *file, int format,
                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)
                {
index a88902ac1333997af00af75d49cc230fb3a9dd4b..32a79605ee87ac3ea73442c8a718feb5822c6b7b 100644 (file)
@@ -233,9 +233,9 @@ int app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2);
 int add_oid_section(BIO *err, CONF *conf);
 X509 *load_cert(BIO *err, const char *file, int format,
        const char *pass, ENGINE *e, const char *cert_descrip);
-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);
-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);
 STACK_OF(X509) *load_certs(BIO *err, const char *file, int format,
        const char *pass, ENGINE *e, const char *cert_descrip);
index ad02e0072b98718f3d64ea3113707c021a0fe875..9633a39f785b1c4ab18ef369165efbb00690f932 100644 (file)
--- a/apps/ca.c
+++ b/apps/ca.c
@@ -699,7 +699,7 @@ bad:
                        goto err;
                        }
                }
-       pkey = load_key(bio_err, keyfile, keyform, key, e, 
+       pkey = load_key(bio_err, keyfile, keyform, 0, key, e, 
                "CA private key");
        if (key) memset(key,0,strlen(key));
        if (pkey == NULL)
index e21c3d83ac149c4d9a0cfc7cc13e0d8de672f2c7..32e40c1f5368406796b6578bb38b1f4087b3d1e0 100644 (file)
@@ -277,10 +277,10 @@ int MAIN(int argc, char **argv)
        if(keyfile)
                {
                if (want_pub)
-                       sigkey = load_pubkey(bio_err, keyfile, keyform, NULL,
+                       sigkey = load_pubkey(bio_err, keyfile, keyform, 0, NULL,
                                e, "key file");
                else
-                       sigkey = load_key(bio_err, keyfile, keyform, NULL,
+                       sigkey = load_key(bio_err, keyfile, keyform, 0, NULL,
                                e, "key file");
                if (!sigkey)
                        {
index 76a11ab0670f902837b76edd116f45819c28b676..59b97a634b3ce2c003b9f1cffb2eddca0dff8cdd 100644 (file)
@@ -617,7 +617,7 @@ int MAIN(int argc, char **argv)
                                NULL, e, "responder other certificates");
                        if (!rother) goto end;
                        }
-               rkey = load_key(bio_err, rkeyfile, FORMAT_PEM, NULL, NULL,
+               rkey = load_key(bio_err, rkeyfile, FORMAT_PEM, 0, NULL, NULL,
                        "responder private key");
                if (!rkey)
                        goto end;
@@ -663,7 +663,7 @@ int MAIN(int argc, char **argv)
                                NULL, e, "signer certificates");
                        if (!sign_other) goto end;
                        }
-               key = load_key(bio_err, keyfile, FORMAT_PEM, NULL, NULL,
+               key = load_key(bio_err, keyfile, FORMAT_PEM, 0, NULL, NULL,
                        "signer private key");
                if (!key)
                        goto end;
index e345cf14899a01d0b39df79a69e9c109c1b9b139..73550d1801540f73925e5c9a2bced9c357c732fa 100644 (file)
@@ -427,7 +427,7 @@ int MAIN(int argc, char **argv)
        CRYPTO_push_info("process -export_cert");
        CRYPTO_push_info("reading private key");
 #endif
-       key = load_key(bio_err, keyname ? keyname : infile, FORMAT_PEM,
+       key = load_key(bio_err, keyname ? keyname : infile, FORMAT_PEM, 1,
                passin, e, "private key");
        if (!key) {
                goto export_end;
index ba91caee6bb2a3eee9c50ecf4c61f69919402ebe..912e32006b6c4d75ec66c6eb88932d1482237c54 100644 (file)
@@ -222,7 +222,8 @@ int MAIN(int argc, char **argv)
        if (topk8)
                {
                BIO_free(in); /* Not needed in this section */
-               pkey = load_key(bio_err, infile, informat, passin, e, "key");
+               pkey = load_key(bio_err, infile, informat, 1,
+                       passin, e, "key");
                if (!pkey) {
                        return (1);
                }
index 75a36040617b6c35bfcc5c55c66feaf43395b120..cc87923159d316bfe9675a3fc0533253077321de 100644 (file)
@@ -683,7 +683,7 @@ bad:
 
        if (keyfile != NULL)
                {
-               pkey = load_key(bio_err, keyfile, keyform, passin, e,
+               pkey = load_key(bio_err, keyfile, keyform, 0, passin, e,
                        "Private Key");
                if (!pkey)
                        {
index 60a33815276443949c23f32cf9b0d9ffe6213a09..4e19bc16fb877d019abe2aa65e38be1ed2c90cbb 100644 (file)
@@ -238,12 +238,12 @@ bad:
                if (pubin)
                        pkey = load_pubkey(bio_err, infile,
                                (informat == FORMAT_NETSCAPE && sgckey ?
-                                       FORMAT_IISSGC : informat),
+                                       FORMAT_IISSGC : informat), 1,
                                passin, e, "Public Key");
                else
                        pkey = load_key(bio_err, infile,
                                (informat == FORMAT_NETSCAPE && sgckey ?
-                                       FORMAT_IISSGC : informat),
+                                       FORMAT_IISSGC : informat), 1,
                                passin, e, "Private Key");
 
                if (pkey != NULL)
index 9b02e6782e01bc657fad82d95f8be43381ab60a9..36957e5b8420334a355222d94e41d0995abda285 100644 (file)
@@ -169,12 +169,12 @@ int MAIN(int argc, char **argv)
        
        switch(key_type) {
                case KEY_PRIVKEY:
-               pkey = load_key(bio_err, keyfile, keyform,
+               pkey = load_key(bio_err, keyfile, keyform, 0,
                        NULL, e, "Private Key");
                break;
 
                case KEY_PUBKEY:
-               pkey = load_pubkey(bio_err, keyfile, keyform,
+               pkey = load_pubkey(bio_err, keyfile, keyform, 0,
                        NULL, e, "Public Key");
                break;
 
index 90fe026f567781d86445d240c0e3be6e63eebe4e..ef0e4774644542ffca00bd19158697b7bd69db62 100644 (file)
@@ -428,7 +428,7 @@ int MAIN(int argc, char **argv)
        } else keyfile = NULL;
 
        if(keyfile) {
-               key = load_key(bio_err, keyfile, keyform, passin, e,
+               key = load_key(bio_err, keyfile, keyform, 0, passin, e,
                               "signing key file");
                if (!key) {
                        goto end;
index 049a37963c2b6511f2251456cfca43f1e6223466..4ce53e36c9022820432338aa525719efafe22bd0 100644 (file)
@@ -186,7 +186,7 @@ bad:
        if(keyfile) {
                pkey = load_key(bio_err,
                                strcmp(keyfile, "-") ? keyfile : NULL,
-                               FORMAT_PEM, passin, e, "private key");
+                               FORMAT_PEM, 1, passin, e, "private key");
                if(!pkey) {
                        goto end;
                }
index dd98eb3b08b319b92b1b8d854fc18d6165eb5d5f..67476e34cfb2abceb1cf4695999fb92abf26e6db 100644 (file)
@@ -861,8 +861,8 @@ bad:
                                if (Upkey == NULL)
                                        {
                                        Upkey=load_key(bio_err,
-                                               keyfile,keyformat, passin, e,
-                                               "Private key");
+                                               keyfile, keyformat, 0,
+                                               passin, e, "Private key");
                                        if (Upkey == NULL) goto end;
                                        }
 #ifndef OPENSSL_NO_DSA
@@ -884,8 +884,9 @@ bad:
                                if (CAkeyfile != NULL)
                                        {
                                        CApkey=load_key(bio_err,
-                                               CAkeyfile,CAkeyformat, passin,
-                                               e, "CA Private Key");
+                                               CAkeyfile, CAkeyformat,
+                                               0, passin, e,
+                                               "CA Private Key");
                                        if (CApkey == NULL) goto end;
                                        }
 #ifndef OPENSSL_NO_DSA
@@ -916,8 +917,8 @@ bad:
                                else
                                        {
                                        pk=load_key(bio_err,
-                                               keyfile,FORMAT_PEM, passin, e,
-                                               "request key");
+                                               keyfile, FORMAT_PEM, 0,
+                                               passin, e, "request key");
                                        if (pk == NULL) goto end;
                                        }