Code Coverage: vinyl-cache/vmod/vmod_proxy.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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 1
#define PP2_CLIENT_SSL           0x01
51 1
#define PP2_CLIENT_CERT_CONN     0x02
52 1
#define PP2_CLIENT_CERT_SESS     0x04
53
54
static VCL_BOOL
55
tlv_ssl_flag(VRT_CTX, int flag)
56 3
{
57 3
        const struct pp2_tlv_ssl *dst;
58 3
        int len;
59 3
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
60
61 3
        if (VPX_tlv(ctx->req, PP2_TYPE_SSL, (const void **)&dst, &len))
62 0
                return (0);
63
64 3
        return ((dst->client & flag) == flag);
65 3
}
66
67
VCL_BOOL v_matchproto_(td_proxy_is_ssl)
68
vmod_is_ssl(VRT_CTX)
69 1
{
70 1
        return (tlv_ssl_flag(ctx, PP2_CLIENT_SSL));
71 1
}
72
73
VCL_BOOL v_matchproto_(td_proxy_client_has_cert_sess)
74
vmod_client_has_cert_sess(VRT_CTX)
75 1
{
76 1
        return (tlv_ssl_flag(ctx, PP2_CLIENT_CERT_SESS));
77 1
}
78
79
VCL_BOOL v_matchproto_(td_proxy_client_has_cert_conn)
80
vmod_client_has_cert_conn(VRT_CTX)
81 1
{
82 1
        return (tlv_ssl_flag(ctx, PP2_CLIENT_CERT_CONN));
83 1
}
84
85
/* return come from SSL_get_verify_result */
86
VCL_INT v_matchproto_(td_proxy_ssl_verify_result)
87
vmod_ssl_verify_result(VRT_CTX)
88 1
{
89 1
        const struct pp2_tlv_ssl *dst;
90 1
        int len;
91 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
92
93 1
        if (VPX_tlv(ctx->req, PP2_TYPE_SSL, (const void **)&dst, &len))
94 0
                return (0); /* X509_V_OK */
95
96 1
        return (vbe32dec(&dst->verify));
97 1
}
98
99
static VCL_STRING
100
tlv_string(VRT_CTX, int tlv)
101 22
{
102 22
        const char *ptr;
103 22
        char *d;
104 22
        int len;
105
106 22
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
107
108 22
        if (VPX_tlv(ctx->req, tlv, (const void **)&ptr, &len))
109 7
                return (NULL);
110 15
        d = WS_Alloc(ctx->ws, len+1);
111 15
        if (d == NULL) {
112 0
                VRT_fail(ctx, "proxy.TLV: out of workspace");
113 0
                return (NULL);
114 0
        }
115 15
        AN(ptr);
116 15
        vmemcpy(d, ptr, len);
117 15
        d[len] = '\0';
118 15
        return (d);
119 15
}
120
121
VCL_STRING v_matchproto_(td_proxy_alpn)
122
vmod_alpn(VRT_CTX)
123 8
{
124 8
        return (tlv_string(ctx, PP2_TYPE_ALPN));
125 8
}
126
127
VCL_STRING v_matchproto_(td_proxy_authority)
128
vmod_authority(VRT_CTX)
129 9
{
130 9
        return (tlv_string(ctx, PP2_TYPE_AUTHORITY));
131 9
}
132
133
VCL_STRING v_matchproto_(td_proxy_ssl_version)
134
vmod_ssl_version(VRT_CTX)
135 1
{
136 1
        return (tlv_string(ctx, PP2_SUBTYPE_SSL_VERSION));
137 1
}
138
139
VCL_STRING v_matchproto_(td_proxy_ssl_cipher)
140
vmod_ssl_cipher(VRT_CTX)
141 1
{
142 1
        return (tlv_string(ctx, PP2_SUBTYPE_SSL_CIPHER));
143 1
}
144
145
VCL_STRING v_matchproto_(td_proxy_cert_sign)
146
vmod_cert_sign(VRT_CTX)
147 1
{
148 1
        return (tlv_string(ctx, PP2_SUBTYPE_SSL_SIG_ALG));
149 1
}
150
151
VCL_STRING v_matchproto_(td_proxy_cert_key)
152
vmod_cert_key(VRT_CTX)
153 1
{
154 1
        return (tlv_string(ctx, PP2_SUBTYPE_SSL_KEY_ALG));
155 1
}
156
157
VCL_STRING v_matchproto_(td_proxy_client_cert_cn)
158
vmod_client_cert_cn(VRT_CTX)
159 1
{
160 1
        return (tlv_string(ctx, PP2_SUBTYPE_SSL_CN));
161 1
}