varnish-cache/vmod/vmod_blob.c
0
/*-
1
 * Copyright 2015-2017 UPLEX - Nils Goroll Systemoptimierung
2
 * All rights reserved.
3
 *
4 10704
 * Authors: Nils Goroll <nils.goroll@uplex.de>
5 27888
 *          Geoffrey Simmons <geoffrey.simmons@uplex.de>
6 23888
 *
7 15024
 * SPDX-License-Identifier: BSD-2-Clause
8 13904
 *
9 13872
 * Redistribution and use in source and binary forms, with or without
10 9360
 * modification, are permitted provided that the following conditions are met:
11 4000
 * 1. Redistributions of source code must retain the above copyright notice,
12
 *    this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright notice,
14
 *    this list of conditions and the following disclaimer in the documentation
15
 *    and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
18
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
 * DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
21
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 *
28
 */
29
30
#include "config.h"
31
#include <string.h>
32
33
#include "cache/cache.h"
34
35
#include "vcc_blob_if.h"
36
#include "vmod_blob.h"
37
38
#define VMOD_BLOB_TYPE 0xfade4faa
39
40
struct vmod_blob_blob {
41
        unsigned magic;
42
#define VMOD_BLOB_MAGIC 0xfade4fa9
43
        struct vrt_blob blob;
44
        void *freeptr;
45
        char *encoding[__MAX_ENCODING][2];
46
        pthread_mutex_t lock;
47
};
48
49
#define B64_FUNCS                                  \
50
                .decode_l       = base64_decode_l, \
51
                .decode         = base64_decode,   \
52
                .encode         = base64_encode
53
54
static const struct vmod_blob_fptr {
55
        len_f           *const decode_l;
56
        decode_f        *const decode;
57
        len_f           *const encode_l;
58
        encode_f        *const encode;
59
} func[__MAX_ENCODING] = {
60
        [IDENTITY] = {
61
                .decode_l       = id_decode_l,
62
                .decode         = id_decode,
63
                .encode_l       = id_encode_l,
64
                .encode         = id_encode
65
        },
66
        [BASE64] = {
67
                B64_FUNCS,
68
                .encode_l       = base64_encode_l
69
        },
70
        [BASE64URL] = {
71
                B64_FUNCS,
72
                .encode_l       = base64_encode_l
73
        },
74
        [BASE64URLNOPAD] = {
75
                B64_FUNCS,
76
                .encode_l       = base64nopad_encode_l
77
        },
78
        [BASE64CF] = {
79
                B64_FUNCS,
80
                .encode_l       = base64_encode_l
81
        },
82
        [HEX] = {
83
                .decode_l       = hex_decode_l,
84
                .decode         = hex_decode,
85
                .encode_l       = hex_encode_l,
86
                .encode         = hex_encode
87
        },
88
        [URL] = {
89
                .decode_l       = url_decode_l,
90
                .decode         = url_decode,
91
                .encode_l       = url_encode_l,
92
                .encode         = url_encode
93
        },
94
};
95
96
#undef B64_FUNCS
97
98
#define ERR(ctx, msg) \
99
        VRT_fail((ctx), "vmod blob error: " msg)
100
101
#define VERR(ctx, fmt, ...) \
102
        VRT_fail((ctx), "vmod blob error: " fmt, __VA_ARGS__)
103
104
#define ERRINVAL(ctx, enc) \
105
        VERR((ctx), "cannot decode, illegal encoding beginning with \"%s\"", \
106
             (enc))
107
108
#define VERRNOMEM(ctx, fmt, ...) \
109
        VERR((ctx), fmt ", out of space", __VA_ARGS__)
110
111
#define ERRNOMEM(ctx, msg) \
112
        ERR((ctx), msg ", out of space")
113
114
static char empty[1] = { '\0' };
115
116
static enum encoding
117 18560
parse_encoding(VCL_ENUM e)
118
{
119
#define VMODENUM(n)                             \
120
        do {                                    \
121
                if (e == VENUM(n)) return (n);  \
122
        } while (0);
123
#include "vmod_blob_tbl_encodings.h"
124 0
        WRONG("illegal encoding enum");
125
}
126
127
static enum case_e
128 10704
parse_case(VCL_ENUM e)
129
{
130
#define VMODENUM(n)                             \
131
        do {                                    \
132
                if (e == VENUM(n)) return (n);  \
133
        } while (0);
134
#include "vmod_blob_tbl_case.h"
135 0
        WRONG("illegal case enum");
136
}
137
138
139
static inline size_t
140 3712
decode_l(enum encoding dec, VCL_STRANDS s)
141
{
142 3712
        size_t len = 0;
143
144 3712
        AENC(dec);
145
146 8160
        for (int i = 0; i < s->n; i++)
147 8416
                if (s->p[i] != NULL && *s->p[i] != '\0')
148 3968
                        len += strlen(s->p[i]);
149
150 3712
        return (func[dec].decode_l(len));
151
}
152
153
static void
154 608
err_decode(VRT_CTX, const char *enc)
155
{
156 608
        switch (errno) {
157
        case EINVAL:
158 544
                ERRINVAL(ctx, enc);
159 544
                break;
160
        case ENOMEM:
161 64
                ERRNOMEM(ctx, "cannot decode");
162 64
                break;
163
        default:
164 0
                WRONG("invalid errno");
165 0
        }
166 608
}
167
168
static inline int
169 11120
encodes_hex(enum encoding enc)
170
{
171 11120
        return (enc == HEX || enc == URL);
172
}
173
174
/* Require case DEFAULT for all encodings besides HEX and URL. */
175
176
static inline int
177 10704
check_enc_case(VRT_CTX, VCL_ENUM encs, VCL_ENUM case_s, enum encoding enc,
178
    enum case_e kase)
179
{
180 10704
        if (!encodes_hex(enc) && kase != DEFAULT) {
181 256
                VERR(ctx, "case %s is illegal with encoding %s", case_s, encs);
182 256
                return (0);
183
        }
184 10448
        return (1);
185 10704
}
186
187
/* Objects */
188
189
VCL_VOID v_matchproto_(td_blob_blob__init)
190 800
vmod_blob__init(VRT_CTX, struct vmod_blob_blob **blobp, const char *vcl_name,
191
    VCL_ENUM decs, VCL_STRANDS strings)
192
{
193
        struct vmod_blob_blob *b;
194 800
        enum encoding dec = parse_encoding(decs);
195
        void *buf;
196
        ssize_t len;
197
198 800
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
199 800
        AN(blobp);
200 800
        AZ(*blobp);
201 800
        AN(vcl_name);
202 800
        AENC(dec);
203 800
        AN(strings);
204
205 800
        ALLOC_OBJ(b, VMOD_BLOB_MAGIC);
206 800
        AN(b);
207 800
        *blobp = b;
208 800
        AZ(pthread_mutex_init(&b->lock, NULL));
209
210 800
        b->blob.type = VMOD_BLOB_TYPE;
211
212 800
        len = decode_l(dec, strings);
213 800
        if (len == 0)
214 128
                return;
215
216 672
        assert(len > 0);
217
218 672
        buf = malloc(len);
219 672
        if (buf == NULL) {
220 0
                VERRNOMEM(ctx, "cannot create blob %s", vcl_name);
221 0
                return;
222
        }
223
224 672
        errno = 0;
225 672
        len = func[dec].decode(dec, buf, len, -1, strings);
226
227 672
        if (len == -1) {
228 128
                assert(errno == EINVAL);
229 128
                free(buf);
230 128
                VERR(ctx, "cannot create blob %s, illegal encoding beginning "
231
                    "with \"%s\"", vcl_name, strings->p[0]);
232 128
                return;
233
        }
234 544
        if (len == 0) {
235 0
                free(buf);
236 0
                memcpy(&b->blob, vrt_null_blob, sizeof b->blob);
237 0
                return;
238
        }
239 544
        b->blob.len = len;
240 544
        b->blob.blob = b->freeptr = buf;
241 800
}
242
243
VCL_BLOB v_matchproto_(td_blob_blob_get)
244 1712
vmod_blob_get(VRT_CTX, struct vmod_blob_blob *b)
245
{
246 1712
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
247 1712
        CHECK_OBJ_NOTNULL(b, VMOD_BLOB_MAGIC);
248 1712
        return (&b->blob);
249
}
250
251
VCL_STRING v_matchproto_(td_blob_blob_encode)
252 2832
vmod_blob_encode(VRT_CTX, struct vmod_blob_blob *b, VCL_ENUM encs,
253
    VCL_ENUM case_s)
254
{
255 2832
        enum encoding enc = parse_encoding(encs);
256 2832
        AENC(enc);
257 2832
        enum case_e kase = parse_case(case_s);
258
259 2832
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
260 2832
        CHECK_OBJ_NOTNULL(b, VMOD_BLOB_MAGIC);
261
262 2832
        if (!check_enc_case(ctx, encs, case_s, enc, kase))
263 128
                return (NULL);
264 2704
        if (b->blob.len == 0)
265 320
                return ("");
266 2384
        if (kase == DEFAULT)
267 1456
                kase = LOWER;
268
269 2384
        if (b->encoding[enc][kase] == NULL) {
270 976
                PTOK(pthread_mutex_lock(&b->lock));
271 976
                if (b->encoding[enc][kase] == NULL) {
272 976
                        ssize_t len = func[enc].encode_l(b->blob.len);
273
274 976
                        assert(len >= 0);
275 976
                        if (len == 0)
276 0
                                b->encoding[enc][kase] = empty;
277
                        else {
278 976
                                b->encoding[enc][kase] = malloc(len);
279 976
                                if (b->encoding[enc][kase] == NULL)
280 0
                                        ERRNOMEM(ctx, "cannot encode");
281
                                else {
282 976
                                        char *s = b->encoding[enc][kase];
283 976
                                        len =
284 1952
                                                func[enc].encode(
285 976
                                                        enc, kase, s, len,
286 976
                                                        b->blob.blob,
287 976
                                                        b->blob.len);
288 976
                                        assert(len >= 0);
289 976
                                        if (len == 0) {
290 0
                                                free(s);
291 0
                                                b->encoding[enc][kase] = empty;
292 0
                                        }
293
                                        else
294 976
                                                s[len] = '\0';
295
                                }
296
                        }
297 976
                }
298 976
                PTOK(pthread_mutex_unlock(&b->lock));
299 976
        }
300 2384
        return (b->encoding[enc][kase]);
301 2832
}
302
303
VCL_VOID v_matchproto_(td_blob_blob__fini)
304 400
vmod_blob__fini(struct vmod_blob_blob **blobp)
305
{
306
        struct vmod_blob_blob *b;
307
        char *s;
308
        int i, j;
309
310 400
        TAKE_OBJ_NOTNULL(b, blobp, VMOD_BLOB_MAGIC);
311
312 400
        if (b->freeptr != NULL) {
313 224
                free(b->freeptr);
314 224
                b->blob.blob = NULL;
315 224
        }
316
317 3600
        for (i = 0; i < __MAX_ENCODING; i++)
318 9600
                for (j = 0; j < 2; j++) {
319 6400
                        s = b->encoding[i][j];
320 6400
                        if (s != NULL && s != empty) {
321 320
                                free(s);
322 320
                                b->encoding[i][j] = NULL;
323 320
                        }
324 9600
                }
325
326 400
        PTOK(pthread_mutex_destroy(&b->lock));
327 400
        FREE_OBJ(b);
328 400
}
329
330
/* Functions */
331
332
VCL_BLOB v_matchproto_(td_blob_decode)
333 4144
vmod_decode(VRT_CTX, VCL_ENUM decs, VCL_INT length, VCL_STRANDS strings)
334
{
335 4144
        enum encoding dec = parse_encoding(decs);
336
        char *buf;
337
        ssize_t len;
338
        unsigned space;
339
340 4144
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
341 4144
        AENC(dec);
342 4144
        AN(strings);
343 4144
        CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
344
345 4144
        space = WS_ReserveAll(ctx->ws);
346 4144
        buf = WS_Reservation(ctx->ws);
347
348 4144
        if (length <= 0)
349 2848
                length = -1;
350 4144
        errno = 0;
351 4144
        len = func[dec].decode(dec, buf, space, length, strings);
352
353 4144
        if (len == -1) {
354 288
                err_decode(ctx, strings->p[0]);
355 288
                WS_Release(ctx->ws, 0);
356 288
                return (NULL);
357
        }
358 3856
        if (len == 0) {
359 672
                WS_Release(ctx->ws, 0);
360 672
                return (vrt_null_blob);
361
        }
362 3184
        WS_Release(ctx->ws, len);
363
364 3184
        assert(len > 0);
365
366 3184
        return (VRT_blob(ctx, "blob.decode", buf, len, VMOD_BLOB_TYPE));
367 4144
}
368
369
static VCL_STRING
370 7359
encode(VRT_CTX, enum encoding enc, enum case_e kase, VCL_BLOB b)
371
{
372
        ssize_t len;
373
        char *buf;
374
        unsigned space;
375
376 7359
        AENC(enc);
377
378 7359
        if (b == NULL)
379 432
                return (NULL);
380
381 6927
        CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
382 6927
        space = WS_ReserveAll(ctx->ws);
383 6927
        buf = WS_Reservation(ctx->ws);
384
385 6927
        len = func[enc].encode(enc, kase, buf, space, b->blob, b->len);
386
387 6927
        if (len == -1) {
388 80
                ERRNOMEM(ctx, "cannot encode");
389 80
                WS_Release(ctx->ws, 0);
390 80
                return (NULL);
391
        }
392 6847
        if (len == 0) {
393 576
                WS_Release(ctx->ws, 0);
394 576
                return ("");
395
        }
396 6271
        buf[len] = '\0';
397 6271
        WS_Release(ctx->ws, len + 1);
398 6271
        return (buf);
399 7359
}
400
401
VCL_STRING v_matchproto_(td_blob_encode)
402 4960
vmod_encode(VRT_CTX, VCL_ENUM encs, VCL_ENUM case_s, VCL_BLOB b)
403
{
404 4960
        enum encoding enc = parse_encoding(encs);
405 4960
        enum case_e kase = parse_case(case_s);
406
407 4960
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
408 4960
        if (!check_enc_case(ctx, encs, case_s, enc, kase))
409 128
                return (NULL);
410 4832
        return (encode(ctx, enc, kase, b));
411 4960
}
412
413
VCL_STRING v_matchproto_(td_blob_transcode)
414 2912
vmod_transcode(VRT_CTX, VCL_ENUM decs, VCL_ENUM encs, VCL_ENUM case_s,
415
               VCL_INT length, VCL_STRANDS strings)
416
{
417 2912
        enum encoding dec = parse_encoding(decs);
418 2912
        enum encoding enc = parse_encoding(encs);
419 2912
        enum case_e kase = parse_case(case_s);
420
        struct vrt_blob b;
421
        VCL_STRING r;
422
        size_t l;
423
        ssize_t len;
424
425 2912
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
426 2912
        CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
427 2912
        AN(strings);
428
429 2912
        AENC(dec);
430 2912
        AENC(enc);
431
432 2912
        if (!check_enc_case(ctx, encs, case_s, enc, kase))
433 0
                return (NULL);
434
435
        /*
436
         * Allocate space for the decoded blob on the stack
437
         * ignoring the limitation imposed by n
438
         */
439 2912
        l = decode_l(dec, strings);
440 2912
        if (l == 0)
441 0
                return ("");
442
443
        /* XXX: handle stack overflow? */
444 2912
        char buf[l];
445
446 2912
        if (length <= 0)
447 1552
                length = -1;
448 2912
        errno = 0;
449 2912
        len = func[dec].decode(dec, buf, l, length, strings);
450
451 2912
        if (len < 0) {
452 320
                err_decode(ctx, strings->p[0]);
453 320
                return (NULL);
454
        }
455
456 2592
        b.len = len;
457 2592
        b.blob = buf;
458
459
        /*
460
         * If the encoding and decoding are the same, and the decoding was
461
         * legal, just return the concatenated string.
462
         * For encodings with hex digits, we cannot assume the same result.
463
         * since the call may specify upper- or lower-case that differs
464
         * from the encoded string.
465
         */
466 2592
        if (length == -1 && enc == dec && !encodes_hex(enc))
467
                /*
468
                 * Returns NULL and invokes VCL failure on workspace
469
                 * overflow. If there is only one string already in the
470
                 * workspace, then it is re-used.
471
                 */
472 64
                return (VRT_STRANDS_string(ctx, strings));
473
474 2528
        r = encode(ctx, enc, kase, &b);
475 2528
        return (r);
476 2912
}
477
478
VCL_BOOL v_matchproto_(td_blob_same)
479 288
vmod_same(VRT_CTX, VCL_BLOB b1, VCL_BLOB b2)
480
{
481
482 288
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
483 288
        if (b1 == b2)
484 96
                return (1);
485 192
        if (b1 == NULL || b2 == NULL)
486 48
                return (0);
487 144
        return (b1->len == b2->len && b1->blob == b2->blob);
488 288
}
489
490
VCL_BOOL v_matchproto_(td_blob_equal)
491 192
vmod_equal(VRT_CTX, VCL_BLOB b1, VCL_BLOB b2)
492
{
493
494 192
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
495 192
        if (b1 == b2)
496 48
                return (1);
497 144
        if (b1 == NULL || b2 == NULL)
498 0
                return (0);
499 144
        if (b1->len != b2->len)
500 48
                return (0);
501 96
        if (b1->blob == b2->blob)
502 32
                return (1);
503 64
        if (b1->blob == NULL || b2->blob == NULL)
504 0
                return (0);
505 64
        return (memcmp(b1->blob, b2->blob, b1->len) == 0);
506 192
}
507
508
VCL_INT v_matchproto_(td_blob_length)
509 112
vmod_length(VRT_CTX, VCL_BLOB b)
510
{
511
512 112
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
513 112
        if (b == NULL)
514 0
                return (0);
515 112
        return (b->len);
516 112
}
517
518
VCL_BLOB v_matchproto_(td_blob_sub)
519 272
vmod_sub(VRT_CTX, VCL_BLOB b, VCL_BYTES n, VCL_BYTES off)
520
{
521
522 272
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
523 272
        assert(n >= 0);
524 272
        assert(off >= 0);
525
526 272
        if (b == NULL || b->len == 0 || b->blob == NULL) {
527 48
                ERR(ctx, "blob is empty in blob.sub()");
528 48
                return (NULL);
529
        }
530
531 224
        assert(b->len > 0);
532
533
        // XXX check for > SIZE_MAX ?
534 224
        if (off < 0 || n < 0) {
535 0
                ERR(ctx, "size or offset negative in blob.sub()");
536 0
                return (NULL);
537
        }
538
539 224
        if ((size_t)off > b->len || (size_t)n > b->len ||
540 192
            (size_t)off + (size_t)n > b->len) {
541 64
                VERR(ctx, "size %jd from offset %jd requires more bytes than "
542
                    "blob length %zd in blob.sub()",
543
                    (intmax_t)n, (intmax_t)off, b->len);
544 64
                return (NULL);
545
        }
546
547 320
        return (VRT_blob(ctx, "blob.sub",
548 160
            (const char *)b->blob + off, n, b->type));
549 272
}