summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorEasySec <easy.sec@free.fr>2022-02-12 02:07:34 +0100
committerPauli <pauli@openssl.org>2022-02-18 15:04:28 +1100
commit7850cc8307b9105f37dde864d5c8c881c522b28a (patch)
treea8bf35ec78300468ca132d62b6931eca78e729ff /apps
parentb089d546242bbc073aefb6f6471586e484118863 (diff)
enc : add support for wrap mode
Reviewed-by: Tomas Mraz <tomas@openssl.org> Reviewed-by: Paul Dale <pauli@openssl.org> (Merged from https://github.com/openssl/openssl/pull/17691)
Diffstat (limited to 'apps')
-rw-r--r--apps/enc.c25
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");