varnish-cache/vmod/vmod_proxy.c
0
/*-
1
 * Copyright (c) 2018 GANDI SAS
2
 * All rights reserved.
3
 *
4
 * Author: Emmanuel Hocdet <manu@gandi.net>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <ctype.h>
33
#include <stdlib.h>
34
#include <string.h>
35
36
#include "cache/cache.h"
37
38
#include "vend.h"
39
40
#include "proxy/cache_proxy.h"
41
42
#include "vcc_proxy_if.h"
43
44
45
struct pp2_tlv_ssl {
46
        uint8_t  client;
47
        uint32_t verify;
48
}__attribute__((packed));
49
50
#define PP2_CLIENT_SSL           0x01
51
#define PP2_CLIENT_CERT_CONN     0x02
52
#define PP2_CLIENT_CERT_SESS     0x04
53
54
static VCL_BOOL
55 75
tlv_ssl_flag(VRT_CTX, int flag)
56
{
57
        const struct pp2_tlv_ssl *dst;
58
        int len;
59 75
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
60
61 75
        if (VPX_tlv(ctx->req, PP2_TYPE_SSL, (const void **)&dst, &len))
62 0
                return (0);
63
64 75
        return ((dst->client & flag) == flag);
65 75
}
66
67
VCL_BOOL v_matchproto_(td_proxy_is_ssl)
68 25
vmod_is_ssl(VRT_CTX)
69
{
70 25
        return (tlv_ssl_flag(ctx, PP2_CLIENT_SSL));
71
}
72
73
VCL_BOOL v_matchproto_(td_proxy_client_has_cert_sess)
74 25
vmod_client_has_cert_sess(VRT_CTX)
75
{
76 25
        return (tlv_ssl_flag(ctx, PP2_CLIENT_CERT_SESS));
77
}
78
79
VCL_BOOL v_matchproto_(td_proxy_client_has_cert_conn)
80 25
vmod_client_has_cert_conn(VRT_CTX)
81
{
82 25
        return (tlv_ssl_flag(ctx, PP2_CLIENT_CERT_CONN));
83
}
84
85
/* return come from SSL_get_verify_result */
86
VCL_INT v_matchproto_(td_proxy_ssl_verify_result)
87 25
vmod_ssl_verify_result(VRT_CTX)
88
{
89
        const struct pp2_tlv_ssl *dst;
90
        int len;
91 25
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
92
93 25
        if (VPX_tlv(ctx->req, PP2_TYPE_SSL, (const void **)&dst, &len))
94 0
                return (0); /* X509_V_OK */
95
96 25
        return (vbe32dec(&dst->verify));
97 25
}
98
99
static VCL_STRING
100 375
tlv_string(VRT_CTX, int tlv)
101
{
102
        const char *ptr;
103
        char *d;
104
        int len;
105
106 375
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
107
108 375
        if (VPX_tlv(ctx->req, tlv, (const void **)&ptr, &len))
109 50
                return (NULL);
110 325
        d = WS_Alloc(ctx->ws, len+1);
111 325
        if (d == NULL) {
112 0
                VRT_fail(ctx, "proxy.TLV: out of workspace");
113 0
                return (NULL);
114
        }
115 325
        AN(ptr);
116 325
        memcpy(d, ptr, len);
117 325
        d[len] = '\0';
118 325
        return (d);
119 375
}
120
121
VCL_STRING v_matchproto_(td_proxy_alpn)
122 25
vmod_alpn(VRT_CTX)
123
{
124 25
        return (tlv_string(ctx, PP2_TYPE_ALPN));
125
}
126
127
VCL_STRING v_matchproto_(td_proxy_authority)
128 225
vmod_authority(VRT_CTX)
129
{
130 225
        return (tlv_string(ctx, PP2_TYPE_AUTHORITY));
131
}
132
133
VCL_STRING v_matchproto_(td_proxy_ssl_version)
134 25
vmod_ssl_version(VRT_CTX)
135
{
136 25
        return (tlv_string(ctx, PP2_SUBTYPE_SSL_VERSION));
137
}
138
139
VCL_STRING v_matchproto_(td_proxy_ssl_cipher)
140 25
vmod_ssl_cipher(VRT_CTX)
141
{
142 25
        return (tlv_string(ctx, PP2_SUBTYPE_SSL_CIPHER));
143
}
144
145
VCL_STRING v_matchproto_(td_proxy_cert_sign)
146 25
vmod_cert_sign(VRT_CTX)
147
{
148 25
        return (tlv_string(ctx, PP2_SUBTYPE_SSL_SIG_ALG));
149
}
150
151
VCL_STRING v_matchproto_(td_proxy_cert_key)
152 25
vmod_cert_key(VRT_CTX)
153
{
154 25
        return (tlv_string(ctx, PP2_SUBTYPE_SSL_KEY_ALG));
155
}
156
157
VCL_STRING v_matchproto_(td_proxy_client_cert_cn)
158 25
vmod_client_cert_cn(VRT_CTX)
159
{
160 25
        return (tlv_string(ctx, PP2_SUBTYPE_SSL_CN));
161
}