Commit 4503d3aa authored by Geoff Simmons's avatar Geoff Simmons

Add VFP ece_encrypt.

This has brought about some debugging in the decrypt VFP as well.

configure has a flag --enable-set-salt. When enabled, the salt for
an encryption can be set from a bereq header (base64 encoded),
rather than generating random salt. This makes it possible to test
encryption results against known values.
parent 6762e253
......@@ -51,11 +51,23 @@ AC_SUBST([VARNISH_LIBRARY_PATH],
# Checks for C sources
# --enable-set-salt
AC_ARG_ENABLE(set-salt,
AS_HELP_STRING([--enable-set-salt],
[salt for encryption may be set for testing purposes (default is NO)]),
[],
[enable_set_salt=no])
AC_SUBST([ENABLE_SET_SALT], [$enable_set_salt])
if test "$enable_set_salt" = "yes"; then
AC_DEFINE([ENABLE_SET_SALT], [1], [Enable setting encryption salt])
fi
# --enable-stack-protector
AC_ARG_ENABLE(stack-protector,
AS_HELP_STRING([--enable-stack-protector],[enable stack protector (default is YES)]),
[],
[enable_stack_protector=yes])
AS_HELP_STRING([--enable-stack-protector],[enable stack protector (default is YES)]),
[],
[enable_stack_protector=yes])
if test "x$enable_stack_protector" != "xno"; then
AX_CHECK_COMPILE_FLAG([-fstack-protector],
......
......@@ -39,7 +39,8 @@ vmod_ece.3: vmod_ece.man.rst
AM_TESTS_ENVIRONMENT = \
PATH="$(VMOD_TEST_PATH)" \
LD_LIBRARY_PATH="$(VARNISH_LIBRARY_PATH)"
LD_LIBRARY_PATH="$(VARNISH_LIBRARY_PATH)" \
ENABLE_SET_SALT=@ENABLE_SET_SALT@
TEST_EXTENSIONS = .vtc
VTC_LOG_COMPILER = varnishtest -v
AM_VTC_LOG_FLAGS = -Dvmod_ece="$(VMOD_ECE)"
......
# looks like -*- vcl -*-
varnishtest "standard encryption VFP"
# Test data from the example in RFC 8188 ch 3.1
# This test is skipped unless configure was invoked with
# --enable-set-salt. This allows setting the salt from a bereq
# header, rather than generating random salt, so that tests with
# verifiable results are possible.
# If the bereq header (with a hard-wired name) is not present, then
# random salt is generated.
feature cmd {test $ENABLE_SET_SALT = "yes"}
server s1 {
rxreq
txresp -body {I am the walrus}
} -start
varnish v1 -arg "-p vsl_mask=+VfpAcct" -vcl+backend {
import ${vmod_ece};
# When set-salt is enabled, the base64-encoded salt can be
# read from bereq header XYZZY-ECE-Salt.
sub vcl_backend_response {
set bereq.http.X-ECE-Key-ID = "";
set bereq.http.XYZZY-ECE-Salt = "I1BsxtFttlv3u/Oo94xnmw==";
set beresp.filters = "ece_encrypt";
}
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
expect resp.http.Content-Encoding == "aes128gcm"
expect resp.bodylen == 53
} -run
# Unfortunately we don't have a way to check the body contents in vtc.
# The encrypted body has null bytes; varnishtest interprets these as
# string terminators, and fails the parse.
# The only way is to manually inspect the log file produced for this
# test. If the encryption is correct, then this line appears (for the
# body read by client c1):
# **** c1 chunked|#Pl\xc6\xd1m\xb6[\xf7\xbb\xf3\xa8\xf7\x8cg\x9b\x00\x00\x10\x00\x00\xf8\xd0\x15\xb9\xbd\xaa\x16\x00D\xb9\x02\x91j\x9a\x19\xbb\xe21\x90\x8b\xda\xdc\xc1\x01\xd4\xf0\xfe\x97/\x13\x868
logexpect l1 -v v1 -d 1 -g vxid -q "VfpAcct" {
expect 0 * Begin bereq
expect * = VfpAcct {^ece_encrypt \d+ 53$}
expect * = End
} -run
# looks like -*- vcl -*-
varnishtest "encryption->decryption round trips"
server s1 {
rxreq
txresp -body "I am the walrus"
rxreq
txresp -body "foo bar baz quux"
rxreq
txresp -body {Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.}
rxreq
txresp -body {Lorem ipsum dolor sit amet, leo ornare nulla, eu malesuada. Cras turpis pretium consectetuer mauris urna sodales, aliquet morbi malesuada potenti velit hendrerit, ut rhoncus lorem vitae neque sed, feugiat nec tellus gravida magna sit. Pretium ac massa arcu viverra, diam velit in aliqua adipiscing vehicula, aliquet iaculis a lobortis enim sit, ligula nisl ultricies, magna volutpat eros suspendisse massa. Ut mauris, purus mi arcu faucibus pretium elit. Pulvinar enim eu, et orci leo tortor parturient magna, commodo ut pellentesque, justo lorem vestibulum non lacinia odio. Consequatur diam volutpat.
Lacus donec libero venenatis venenatis, egestas pretium ut urna vehicula, ac eget ac, amet maecenas turpis viverra blandit. Ullamcorper massa sed in, nec velit nec aenean justo, nullam cras, ullamcorper eros pharetra magna urna metus, ultricies rutrum neque nunc eleifend. Pretium consequat leo in, amet varius integer, eros non et ut sit ac, magnis quis nibh dolor ornare sed. Turpis nec morbi, nunc a mauris faucibus molestie, dolor velit tincidunt, amet ac erat eros massa pede. Vitae venenatis volutpat eget vestibulum. In nam pharetra in vivamus pretium, nulla nunc sociosqu, aliquam nullam ligula, ut sed vestibulum quam morbi turpis mattis, wisi at per lorem aliquam ante sed. Mauris taciti ligula venenatis vehicula, morbi eget nec. Amet id sodales eos, diam suspendisse pulvinar, at vestibulum torquent nec eget risus est. Mauris sapien tortor eget nesciunt.
Ridiculus congue erat velit, nulla felis, viverra non tempor cum nulla, quis quisque enim at sociosqu vestibulum sit. Donec molestie. Cursus pretium donec phasellus lacinia velit nulla, quis sapien dapibus suscipit viverra magni commodo, viverra vel eros lectus tristique lorem vivamus. Tortor mi ante, aut elit, metus ante voluptatem nisl aenean eget amet, felis at et. Posuere lorem ridiculus, et leo viverra pellentesque neque, ullamcorper neque volutpat pretium amet laoreet. Sapien at, phasellus ullamco wisi, arcu fermentum fringilla nam turpis mi. Congue et donec rutrum, voluptatem et duis neque eget elit. Mus sit luctus est, nullam fringilla magna vel massa, vivamus vel adipiscing quis dolor quam. Risus laoreet consequat fusce suspendisse vitae, felis aliquet.
Enim per accumsan, augue id maecenas bibendum ullamcorper in fermentum, platea fermentum. Convallis pulvinar tortor eros sit cras nascetur, quam dis elit sed aliquam ac, nulla morbi suscipit egestas placerat pretium. Vehicula elementum duis, turpis wisi nascetur velit. Senectus aenean ipsum blandit, eleifend leo donec nunc, ut vel sagittis sed nam, ut aliquam quam aliquam at tellus. Vivamus donec libero feugiat amet gravida, felis id aliquam nullam consequat, aut eget suspendisse rutrum in. Orci tellus. A sapien scelerisque diam, ornare laoreet lobortis blandit in. Aliquam at suscipit, id risus. Leo lacus quam mollis erat morbi vel, ut adipiscing cillum dui. Elit bibendum tristique et non eros libero, egestas lectus turpis, quis gravida dolor volutpat, purus eu, consectetuer pharetra per vehicula quam tellus.}
rxreq
txresp -bodylen 65536
rxreq
txresp -nolen -hdr "Transfer-encoding: chunked"
chunkedlen 8
chunkedlen 8
chunkedlen 16
chunkedlen 32
chunkedlen 64
chunkedlen 128
chunkedlen 256
chunkedlen 512
chunkedlen 1024
chunkedlen 2048
chunkedlen 4096
chunkedlen 8192
chunkedlen 16384
chunkedlen 32768
chunkedlen 65536
chunkedlen 0
} -start
varnish v1 -arg "-p vsl_mask=+VfpAcct" -vcl+backend {
import ${vmod_ece};
sub vcl_backend_response {
set bereq.http.X-ECE-Key-ID = "a1";
set beresp.filters = "ece_encrypt ece_decrypt";
set beresp.uncacheable = true;
}
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
expect resp.http.Content-Encoding == <undef>
expect resp.bodylen == 15
expect resp.body == "I am the walrus"
txreq
rxresp
expect resp.status == 200
expect resp.http.Content-Encoding == <undef>
expect resp.bodylen == 16
expect resp.body == "foo bar baz quux"
txreq
rxresp
expect resp.status == 200
expect resp.http.Content-Encoding == <undef>
expect resp.bodylen == 269
expect resp.body == {Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.}
txreq
rxresp
expect resp.status == 200
expect resp.http.Content-Encoding == <undef>
expect resp.bodylen == 3067
expect resp.body == {Lorem ipsum dolor sit amet, leo ornare nulla, eu malesuada. Cras turpis pretium consectetuer mauris urna sodales, aliquet morbi malesuada potenti velit hendrerit, ut rhoncus lorem vitae neque sed, feugiat nec tellus gravida magna sit. Pretium ac massa arcu viverra, diam velit in aliqua adipiscing vehicula, aliquet iaculis a lobortis enim sit, ligula nisl ultricies, magna volutpat eros suspendisse massa. Ut mauris, purus mi arcu faucibus pretium elit. Pulvinar enim eu, et orci leo tortor parturient magna, commodo ut pellentesque, justo lorem vestibulum non lacinia odio. Consequatur diam volutpat.
Lacus donec libero venenatis venenatis, egestas pretium ut urna vehicula, ac eget ac, amet maecenas turpis viverra blandit. Ullamcorper massa sed in, nec velit nec aenean justo, nullam cras, ullamcorper eros pharetra magna urna metus, ultricies rutrum neque nunc eleifend. Pretium consequat leo in, amet varius integer, eros non et ut sit ac, magnis quis nibh dolor ornare sed. Turpis nec morbi, nunc a mauris faucibus molestie, dolor velit tincidunt, amet ac erat eros massa pede. Vitae venenatis volutpat eget vestibulum. In nam pharetra in vivamus pretium, nulla nunc sociosqu, aliquam nullam ligula, ut sed vestibulum quam morbi turpis mattis, wisi at per lorem aliquam ante sed. Mauris taciti ligula venenatis vehicula, morbi eget nec. Amet id sodales eos, diam suspendisse pulvinar, at vestibulum torquent nec eget risus est. Mauris sapien tortor eget nesciunt.
Ridiculus congue erat velit, nulla felis, viverra non tempor cum nulla, quis quisque enim at sociosqu vestibulum sit. Donec molestie. Cursus pretium donec phasellus lacinia velit nulla, quis sapien dapibus suscipit viverra magni commodo, viverra vel eros lectus tristique lorem vivamus. Tortor mi ante, aut elit, metus ante voluptatem nisl aenean eget amet, felis at et. Posuere lorem ridiculus, et leo viverra pellentesque neque, ullamcorper neque volutpat pretium amet laoreet. Sapien at, phasellus ullamco wisi, arcu fermentum fringilla nam turpis mi. Congue et donec rutrum, voluptatem et duis neque eget elit. Mus sit luctus est, nullam fringilla magna vel massa, vivamus vel adipiscing quis dolor quam. Risus laoreet consequat fusce suspendisse vitae, felis aliquet.
Enim per accumsan, augue id maecenas bibendum ullamcorper in fermentum, platea fermentum. Convallis pulvinar tortor eros sit cras nascetur, quam dis elit sed aliquam ac, nulla morbi suscipit egestas placerat pretium. Vehicula elementum duis, turpis wisi nascetur velit. Senectus aenean ipsum blandit, eleifend leo donec nunc, ut vel sagittis sed nam, ut aliquam quam aliquam at tellus. Vivamus donec libero feugiat amet gravida, felis id aliquam nullam consequat, aut eget suspendisse rutrum in. Orci tellus. A sapien scelerisque diam, ornare laoreet lobortis blandit in. Aliquam at suscipit, id risus. Leo lacus quam mollis erat morbi vel, ut adipiscing cillum dui. Elit bibendum tristique et non eros libero, egestas lectus turpis, quis gravida dolor volutpat, purus eu, consectetuer pharetra per vehicula quam tellus.}
txreq
rxresp
expect resp.status == 200
expect resp.http.Content-Encoding == <undef>
expect resp.bodylen == 65536
txreq
rxresp
expect resp.status == 200
expect resp.http.Content-Encoding == <undef>
expect resp.bodylen == 131072
} -run
logexpect l1 -v v1 -d 1 -g vxid -q "VfpAcct" {
expect 0 * Begin bereq
expect * = VfpAcct {^ece_decrypt \d+ 15$}
expect 0 = VfpAcct {^ece_encrypt \d+ \d+$}
expect * = End
expect 0 * Begin bereq
expect * = VfpAcct {^ece_decrypt \d+ 16$}
expect 0 = VfpAcct {^ece_encrypt \d+ \d+$}
expect * = End
expect 0 * Begin bereq
expect * = VfpAcct {^ece_decrypt \d+ 269$}
expect * = VfpAcct {^ece_encrypt \d+ \d+$}
expect * = End
expect 0 * Begin bereq
expect * = VfpAcct {^ece_decrypt \d+ 3067$}
expect * = VfpAcct {^ece_encrypt \d+ \d+$}
expect * = End
expect 0 * Begin bereq
expect * = VfpAcct {^ece_decrypt \d+ 65536$}
expect * = VfpAcct {^ece_encrypt \d+ \d+$}
expect * = End
expect 0 * Begin bereq
expect * = VfpAcct {^ece_decrypt \d+ 131072$}
expect * = VfpAcct {^ece_encrypt \d+ \d+$}
expect * = End
} -run
This diff is collapsed.
......@@ -28,4 +28,4 @@
#include "cache/cache_filter.h"
extern const struct vfp vfp_decrypt;
extern const struct vfp vfp_decrypt, vfp_encrypt;
/*-
* Copyright (c) 2019 UPLEX Nils Goroll Systemoptimierung
* All rights reserved
*
* Author: Geoffrey Simmons <geoffrey.simmons@uplex.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* Included from vfp.c only if set-salt was enabled at configure time. */
/*
* libcrypto base64 encodings use the alphabet [a-ZA-Z0-9/+] and = padding
* (just like BASE64 for VMOD blob).
*
* EVP_DecodeBlock() adds padding bytes, so that
* len(decoded) == len(encoded)*3/4
* hence the use of SALT_LEN + 2.
*/
#define SET_SALT_HDR "\017XYZZY-ECE-Salt:"
#define SALT_B64_LEN 24
#define SALT_DECODED_LEN (SALT_LEN + 2)
static enum vfp_status
set_salt(struct vfp_ctx *ctx, uint8_t *salt)
{
const char *salt_b64;
uint8_t decoded[SALT_DECODED_LEN];
int len;
CHECK_OBJ_NOTNULL(ctx, VFP_CTX_MAGIC);
AN(salt);
if (http_GetHdr(ctx->req, SET_SALT_HDR, &salt_b64) == 0)
return (VFP_NULL);
len = EVP_DecodeBlock(decoded, (const unsigned char *)salt_b64,
SALT_B64_LEN);
if (len != SALT_DECODED_LEN)
return (VFP_Error(ctx, "set-salt base64 decoding error"));
memcpy(salt, decoded, SALT_LEN);
return (VFP_OK);
}
......@@ -47,9 +47,11 @@ VPFX(event)(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e e)
switch(e) {
case VCL_EVENT_LOAD:
VRT_AddVFP(ctx, &vfp_encrypt);
VRT_AddVFP(ctx, &vfp_decrypt);
return (0);
case VCL_EVENT_DISCARD:
VRT_RemoveVFP(ctx, &vfp_encrypt);
VRT_RemoveVFP(ctx, &vfp_decrypt);
return (0);
case VCL_EVENT_WARM:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment