diff options
Diffstat (limited to 'apps')
-rw-r--r-- | apps/enc.c | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/apps/enc.c b/apps/enc.c index b14129d9b0..d50baa6d2f 100644 --- a/apps/enc.c +++ b/apps/enc.c @@ -127,6 +127,8 @@ int enc_main(int argc, char **argv) int pbkdf2 = 0; int iter = 0; long n; + int streamable = 1; + int wrap = 0; struct doall_enc_ciphers dec; #ifdef ZLIB int do_zlib = 0; @@ -298,6 +300,10 @@ int enc_main(int argc, char **argv) /* Get the cipher name, either from progname (if set) or flag. */ if (!opt_cipher(ciphername, &cipher)) goto opthelp; + if (cipher && (EVP_CIPHER_mode(cipher) == EVP_CIPH_WRAP_MODE)) { + wrap = 1; + streamable = 0; + } if (digestname != NULL) { if (!opt_md(digestname, &dgst)) goto opthelp; @@ -328,6 +334,10 @@ int enc_main(int argc, char **argv) buff = app_malloc(EVP_ENCODE_LENGTH(bsize), "evp buffer"); if (infile == NULL) { + if (!streamable) { + BIO_printf(bio_err, "Unstreamable cipher mode\n"); + goto end; + } in = dup_bio_in(informat); } else { in = bio_open_default(infile, 'r', informat); @@ -524,7 +534,8 @@ int enc_main(int argc, char **argv) } } if ((hiv == NULL) && (str == NULL) - && EVP_CIPHER_get_iv_length(cipher) != 0) { + && EVP_CIPHER_get_iv_length(cipher) != 0 + && wrap == 0) { /* * No IV was explicitly set and no IV was generated. * Hence the IV is undefined, making correct decryption impossible. @@ -551,6 +562,9 @@ int enc_main(int argc, char **argv) BIO_get_cipher_ctx(benc, &ctx); + if (wrap == 1) + EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); + if (!EVP_CipherInit_ex(ctx, cipher, e, NULL, NULL, enc)) { BIO_printf(bio_err, "Error setting cipher %s\n", EVP_CIPHER_get0_name(cipher)); @@ -561,7 +575,8 @@ int enc_main(int argc, char **argv) if (nopad) EVP_CIPHER_CTX_set_padding(ctx, 0); - if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc)) { + if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, + (hiv == NULL && wrap == 1 ? NULL : iv), enc)) { BIO_printf(bio_err, "Error setting cipher %s\n", EVP_CIPHER_get0_name(cipher)); ERR_print_errors(bio_err); @@ -607,10 +622,16 @@ int enc_main(int argc, char **argv) inl = BIO_read(rbio, (char *)buff, bsize); if (inl <= 0) break; + if (!streamable && !BIO_eof(rbio)) { /* do not output data */ + BIO_printf(bio_err, "Unstreamable cipher mode\n"); + goto end; + } if (BIO_write(wbio, (char *)buff, inl) != inl) { BIO_printf(bio_err, "error writing output file\n"); goto end; } + if (!streamable) + break; } if (!BIO_flush(wbio)) { BIO_printf(bio_err, "bad decrypt\n"); |