- }
- if (topk8)
- {
- BIO_free(in); /* Not needed in this section */
- pkey = load_key(bio_err, infile, informat, 1,
- passin, e, "key");
- if (!pkey) {
- return (1);
- }
- if (!(p8inf = EVP_PKEY2PKCS8_broken(pkey, p8_broken))) {
- BIO_printf(bio_err, "Error converting key\n", outfile);
- ERR_print_errors(bio_err);
- return (1);
- }
- if(nocrypt) {
- if(outformat == FORMAT_PEM)
- PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
- else if(outformat == FORMAT_ASN1)
- i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
- else {
- BIO_printf(bio_err, "Bad format specified for key\n");
- return (1);
- }
- } else {
- if(passout) p8pass = passout;
- else {
- p8pass = pass;
- EVP_read_pw_string(pass, 50, "Enter Encryption Password:", 1);
- }
- app_RAND_load_file(NULL, bio_err, 0);
- if (!(p8 = PKCS8_encrypt(pbe_nid, cipher,
- p8pass, strlen(p8pass),
- NULL, 0, iter, p8inf))) {
- BIO_printf(bio_err, "Error encrypting key\n",
- outfile);
- ERR_print_errors(bio_err);
- return (1);
- }
- app_RAND_write_file(NULL, bio_err);
- if(outformat == FORMAT_PEM)
- PEM_write_bio_PKCS8(out, p8);
- else if(outformat == FORMAT_ASN1)
- i2d_PKCS8_bio(out, p8);
- else {
- BIO_printf(bio_err, "Bad format specified for key\n");
- return (1);
- }
- X509_SIG_free(p8);
- }
- PKCS8_PRIV_KEY_INFO_free (p8inf);
- EVP_PKEY_free(pkey);
- BIO_free_all(out);
- if(passin) OPENSSL_free(passin);
- if(passout) OPENSSL_free(passout);
- return (0);
- }
+ }
+ if (topk8) {
+ pkey = load_key(bio_err, infile, informat, 1, passin, e, "key");
+ if (!pkey)
+ goto end;
+ if (!(p8inf = EVP_PKEY2PKCS8_broken(pkey, p8_broken))) {
+ BIO_printf(bio_err, "Error converting key\n");
+ ERR_print_errors(bio_err);
+ goto end;
+ }
+ if (nocrypt) {
+ if (outformat == FORMAT_PEM)
+ PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
+ else if (outformat == FORMAT_ASN1)
+ i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
+ else {
+ BIO_printf(bio_err, "Bad format specified for key\n");
+ goto end;
+ }
+ } else {
+ if (passout)
+ p8pass = passout;
+ else {
+ p8pass = pass;
+ if (EVP_read_pw_string
+ (pass, sizeof pass, "Enter Encryption Password:", 1))
+ goto end;
+ }
+ app_RAND_load_file(NULL, bio_err, 0);
+ if (!(p8 = PKCS8_encrypt(pbe_nid, cipher,
+ p8pass, strlen(p8pass),
+ NULL, 0, iter, p8inf))) {
+ BIO_printf(bio_err, "Error encrypting key\n");
+ ERR_print_errors(bio_err);
+ goto end;
+ }
+ app_RAND_write_file(NULL, bio_err);
+ if (outformat == FORMAT_PEM)
+ PEM_write_bio_PKCS8(out, p8);
+ else if (outformat == FORMAT_ASN1)
+ i2d_PKCS8_bio(out, p8);
+ else {
+ BIO_printf(bio_err, "Bad format specified for key\n");
+ goto end;
+ }
+ }
+
+ ret = 0;
+ goto end;
+ }
+
+ if (nocrypt) {
+ if (informat == FORMAT_PEM)
+ p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL);
+ else if (informat == FORMAT_ASN1)
+ p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
+ else {
+ BIO_printf(bio_err, "Bad format specified for key\n");
+ goto end;
+ }
+ } else {
+ if (informat == FORMAT_PEM)
+ p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
+ else if (informat == FORMAT_ASN1)
+ p8 = d2i_PKCS8_bio(in, NULL);
+ else {
+ BIO_printf(bio_err, "Bad format specified for key\n");
+ goto end;
+ }
+
+ if (!p8) {
+ BIO_printf(bio_err, "Error reading key\n");
+ ERR_print_errors(bio_err);
+ goto end;
+ }
+ if (passin)
+ p8pass = passin;
+ else {
+ p8pass = pass;
+ EVP_read_pw_string(pass, sizeof pass, "Enter Password:", 0);
+ }
+ p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
+ }
+
+ if (!p8inf) {
+ BIO_printf(bio_err, "Error decrypting key\n");
+ ERR_print_errors(bio_err);
+ goto end;
+ }