+ if(out_bin == -1) {
+ if(keyfile) out_bin = 1;
+ else out_bin = 0;
+ }
+
+ if(randfile)
+ app_RAND_load_file(randfile, bio_err, 0);
+
+ if(outfile) {
+ if(out_bin)
+ out = BIO_new_file(outfile, "wb");
+ else out = BIO_new_file(outfile, "w");
+ } else {
+ out = BIO_new_fp(stdout, BIO_NOCLOSE);
+#ifdef OPENSSL_SYS_VMS
+ {
+ BIO *tmpbio = BIO_new(BIO_f_linebuffer());
+ out = BIO_push(tmpbio, out);
+ }
+#endif
+ }
+
+ if(!out) {
+ BIO_printf(bio_err, "Error opening output file %s\n",
+ outfile ? outfile : "(stdout)");
+ ERR_print_errors(bio_err);
+ goto end;
+ }
+
+ if(keyfile)
+ {
+ if (keyform == FORMAT_PEM)
+ {
+ BIO *keybio;
+ keybio = BIO_new_file(keyfile, "r");
+ if(!keybio)
+ {
+ BIO_printf(bio_err,
+ "Error opening key file %s\n",
+ keyfile);
+ ERR_print_errors(bio_err);
+ goto end;
+ }
+ if(want_pub)
+ sigkey = PEM_read_bio_PUBKEY(keybio,
+ NULL, NULL, NULL);
+ else
+ sigkey = PEM_read_bio_PrivateKey(keybio,
+ NULL, NULL, NULL);
+ BIO_free(keybio);
+ }
+ else if (keyform == FORMAT_ENGINE)
+ {
+ if (!e)
+ {
+ BIO_printf(bio_err,"no engine specified\n");
+ goto end;
+ }
+ if (want_pub)
+ sigkey = ENGINE_load_public_key(e, keyfile, NULL);
+ else
+ sigkey = ENGINE_load_private_key(e, keyfile, NULL);
+ }
+ else
+ {
+ BIO_printf(bio_err,
+ "bad input format specified for key file\n");
+ goto end;
+ }
+
+ if(!sigkey) {
+ BIO_printf(bio_err, "Error reading key file %s\n",
+ keyfile);
+ ERR_print_errors(bio_err);
+ goto end;
+ }
+ }
+
+ if(sigfile && sigkey) {
+ BIO *sigbio;
+ sigbio = BIO_new_file(sigfile, "rb");
+ siglen = EVP_PKEY_size(sigkey);
+ sigbuf = OPENSSL_malloc(siglen);
+ if(!sigbio) {
+ BIO_printf(bio_err, "Error opening signature file %s\n",
+ sigfile);
+ ERR_print_errors(bio_err);
+ goto end;
+ }
+ siglen = BIO_read(sigbio, sigbuf, siglen);
+ BIO_free(sigbio);
+ if(siglen <= 0) {
+ BIO_printf(bio_err, "Error reading signature file %s\n",
+ sigfile);
+ ERR_print_errors(bio_err);
+ goto end;
+ }
+ }
+
+
+