Make it possible to load keys from stdin, and restore that

functionality in the programs that had that before.
Part fo PR 164
This commit is contained in:
Richard Levitte 2002-08-01 16:28:40 +00:00
parent bd45950f4a
commit da9b972466
13 changed files with 50 additions and 34 deletions

View File

@ -798,7 +798,7 @@ end:
return(x); 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) const char *pass, ENGINE *e, const char *key_descrip)
{ {
BIO *key=NULL; BIO *key=NULL;
@ -808,7 +808,7 @@ EVP_PKEY *load_key(BIO *err, const char *file, int format,
cb_data.password = pass; cb_data.password = pass;
cb_data.prompt_info = file; cb_data.prompt_info = file;
if (file == NULL) if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
{ {
BIO_printf(err,"no keyfile specified\n"); BIO_printf(err,"no keyfile specified\n");
goto end; goto end;
@ -828,9 +828,16 @@ EVP_PKEY *load_key(BIO *err, const char *file, int format,
ERR_print_errors(err); ERR_print_errors(err);
goto end; goto end;
} }
if (file == NULL && maybe_stdin)
{
setvbuf(stdin, NULL, _IONBF, 0);
BIO_set_fp(key,stdin,BIO_NOCLOSE);
}
else
if (BIO_read_filename(key,file) <= 0) if (BIO_read_filename(key,file) <= 0)
{ {
BIO_printf(err, "Error opening %s %s\n", key_descrip, file); BIO_printf(err, "Error opening %s %s\n",
key_descrip, file);
ERR_print_errors(err); ERR_print_errors(err);
goto end; goto end;
} }
@ -867,7 +874,7 @@ EVP_PKEY *load_key(BIO *err, const char *file, int format,
return(pkey); 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) const char *pass, ENGINE *e, const char *key_descrip)
{ {
BIO *key=NULL; BIO *key=NULL;
@ -877,7 +884,7 @@ EVP_PKEY *load_pubkey(BIO *err, const char *file, int format,
cb_data.password = pass; cb_data.password = pass;
cb_data.prompt_info = file; cb_data.prompt_info = file;
if (file == NULL) if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
{ {
BIO_printf(err,"no keyfile specified\n"); BIO_printf(err,"no keyfile specified\n");
goto end; goto end;
@ -897,9 +904,16 @@ EVP_PKEY *load_pubkey(BIO *err, const char *file, int format,
ERR_print_errors(err); ERR_print_errors(err);
goto end; goto end;
} }
if (file == NULL && maybe_stdin)
{
setvbuf(stdin, NULL, _IONBF, 0);
BIO_set_fp(key,stdin,BIO_NOCLOSE);
}
else
if (BIO_read_filename(key,file) <= 0) if (BIO_read_filename(key,file) <= 0)
{ {
BIO_printf(err, "Error opening %s %s\n", key_descrip, file); BIO_printf(err, "Error opening %s %s\n",
key_descrip, file);
ERR_print_errors(err); ERR_print_errors(err);
goto end; goto end;
} }

View 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); int add_oid_section(BIO *err, CONF *conf);
X509 *load_cert(BIO *err, const char *file, int format, X509 *load_cert(BIO *err, const char *file, int format,
const char *pass, ENGINE *e, const char *cert_descrip); 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); 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); const char *pass, ENGINE *e, const char *key_descrip);
STACK_OF(X509) *load_certs(BIO *err, const char *file, int format, STACK_OF(X509) *load_certs(BIO *err, const char *file, int format,
const char *pass, ENGINE *e, const char *cert_descrip); const char *pass, ENGINE *e, const char *cert_descrip);

View File

@ -699,7 +699,7 @@ bad:
goto err; goto err;
} }
} }
pkey = load_key(bio_err, keyfile, keyform, key, e, pkey = load_key(bio_err, keyfile, keyform, 0, key, e,
"CA private key"); "CA private key");
if (key) memset(key,0,strlen(key)); if (key) memset(key,0,strlen(key));
if (pkey == NULL) if (pkey == NULL)

View File

@ -277,10 +277,10 @@ int MAIN(int argc, char **argv)
if(keyfile) if(keyfile)
{ {
if (want_pub) if (want_pub)
sigkey = load_pubkey(bio_err, keyfile, keyform, NULL, sigkey = load_pubkey(bio_err, keyfile, keyform, 0, NULL,
e, "key file"); e, "key file");
else else
sigkey = load_key(bio_err, keyfile, keyform, NULL, sigkey = load_key(bio_err, keyfile, keyform, 0, NULL,
e, "key file"); e, "key file");
if (!sigkey) if (!sigkey)
{ {

View File

@ -617,7 +617,7 @@ int MAIN(int argc, char **argv)
NULL, e, "responder other certificates"); NULL, e, "responder other certificates");
if (!rother) goto end; 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"); "responder private key");
if (!rkey) if (!rkey)
goto end; goto end;
@ -663,7 +663,7 @@ int MAIN(int argc, char **argv)
NULL, e, "signer certificates"); NULL, e, "signer certificates");
if (!sign_other) goto end; 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"); "signer private key");
if (!key) if (!key)
goto end; goto end;

View File

@ -427,7 +427,7 @@ int MAIN(int argc, char **argv)
CRYPTO_push_info("process -export_cert"); CRYPTO_push_info("process -export_cert");
CRYPTO_push_info("reading private key"); CRYPTO_push_info("reading private key");
#endif #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"); passin, e, "private key");
if (!key) { if (!key) {
goto export_end; goto export_end;

View File

@ -222,7 +222,8 @@ int MAIN(int argc, char **argv)
if (topk8) if (topk8)
{ {
BIO_free(in); /* Not needed in this section */ 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) { if (!pkey) {
return (1); return (1);
} }

View File

@ -683,7 +683,7 @@ bad:
if (keyfile != NULL) if (keyfile != NULL)
{ {
pkey = load_key(bio_err, keyfile, keyform, passin, e, pkey = load_key(bio_err, keyfile, keyform, 0, passin, e,
"Private Key"); "Private Key");
if (!pkey) if (!pkey)
{ {

View File

@ -238,12 +238,12 @@ bad:
if (pubin) if (pubin)
pkey = load_pubkey(bio_err, infile, pkey = load_pubkey(bio_err, infile,
(informat == FORMAT_NETSCAPE && sgckey ? (informat == FORMAT_NETSCAPE && sgckey ?
FORMAT_IISSGC : informat), FORMAT_IISSGC : informat), 1,
passin, e, "Public Key"); passin, e, "Public Key");
else else
pkey = load_key(bio_err, infile, pkey = load_key(bio_err, infile,
(informat == FORMAT_NETSCAPE && sgckey ? (informat == FORMAT_NETSCAPE && sgckey ?
FORMAT_IISSGC : informat), FORMAT_IISSGC : informat), 1,
passin, e, "Private Key"); passin, e, "Private Key");
if (pkey != NULL) if (pkey != NULL)

View File

@ -169,12 +169,12 @@ int MAIN(int argc, char **argv)
switch(key_type) { switch(key_type) {
case KEY_PRIVKEY: case KEY_PRIVKEY:
pkey = load_key(bio_err, keyfile, keyform, pkey = load_key(bio_err, keyfile, keyform, 0,
NULL, e, "Private Key"); NULL, e, "Private Key");
break; break;
case KEY_PUBKEY: case KEY_PUBKEY:
pkey = load_pubkey(bio_err, keyfile, keyform, pkey = load_pubkey(bio_err, keyfile, keyform, 0,
NULL, e, "Public Key"); NULL, e, "Public Key");
break; break;

View File

@ -428,7 +428,7 @@ int MAIN(int argc, char **argv)
} else keyfile = NULL; } else keyfile = NULL;
if(keyfile) { if(keyfile) {
key = load_key(bio_err, keyfile, keyform, passin, e, key = load_key(bio_err, keyfile, keyform, 0, passin, e,
"signing key file"); "signing key file");
if (!key) { if (!key) {
goto end; goto end;

View File

@ -186,7 +186,7 @@ bad:
if(keyfile) { if(keyfile) {
pkey = load_key(bio_err, pkey = load_key(bio_err,
strcmp(keyfile, "-") ? keyfile : NULL, strcmp(keyfile, "-") ? keyfile : NULL,
FORMAT_PEM, passin, e, "private key"); FORMAT_PEM, 1, passin, e, "private key");
if(!pkey) { if(!pkey) {
goto end; goto end;
} }

View File

@ -861,8 +861,8 @@ bad:
if (Upkey == NULL) if (Upkey == NULL)
{ {
Upkey=load_key(bio_err, Upkey=load_key(bio_err,
keyfile,keyformat, passin, e, keyfile, keyformat, 0,
"Private key"); passin, e, "Private key");
if (Upkey == NULL) goto end; if (Upkey == NULL) goto end;
} }
#ifndef OPENSSL_NO_DSA #ifndef OPENSSL_NO_DSA
@ -884,8 +884,9 @@ bad:
if (CAkeyfile != NULL) if (CAkeyfile != NULL)
{ {
CApkey=load_key(bio_err, CApkey=load_key(bio_err,
CAkeyfile,CAkeyformat, passin, CAkeyfile, CAkeyformat,
e, "CA Private Key"); 0, passin, e,
"CA Private Key");
if (CApkey == NULL) goto end; if (CApkey == NULL) goto end;
} }
#ifndef OPENSSL_NO_DSA #ifndef OPENSSL_NO_DSA
@ -916,8 +917,8 @@ bad:
else else
{ {
pk=load_key(bio_err, pk=load_key(bio_err,
keyfile,FORMAT_PEM, passin, e, keyfile, FORMAT_PEM, 0,
"request key"); passin, e, "request key");
if (pk == NULL) goto end; if (pk == NULL) goto end;
} }