blob: 5c6ad1ca2075ec090844fd64ecc210fb9199ece5 [file] [log] [blame]
William Lallemand15e16942020-05-15 00:25:08 +02001/*
2 * This file contains the sample fetches related to the SSL
3 *
4 * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 * Copyright (C) 2020 HAProxy Technologies, William Lallemand <wlallemand@haproxy.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#define _GNU_SOURCE
14#include <ctype.h>
15#include <dirent.h>
16#include <errno.h>
William Lallemand15e16942020-05-15 00:25:08 +020017#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21
Willy Tarreaudcc048a2020-06-04 19:11:43 +020022#include <haproxy/acl.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020024#include <haproxy/arg.h>
Willy Tarreau99ea1882021-10-06 15:37:17 +020025#include <haproxy/base64.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020026#include <haproxy/buf-t.h>
Willy Tarreau939b0bf2022-04-11 11:29:11 +020027#include <haproxy/connection.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020028#include <haproxy/obj_type.h>
Willy Tarreau6019fab2020-05-27 16:26:00 +020029#include <haproxy/openssl-compat.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020030#include <haproxy/sample.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020031#include <haproxy/ssl_sock.h>
Willy Tarreaub2bd8652020-06-04 14:21:22 +020032#include <haproxy/ssl_utils.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020033#include <haproxy/stconn.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020034#include <haproxy/tools.h>
Willy Tarreau99ea1882021-10-06 15:37:17 +020035#include <haproxy/vars.h>
William Lallemand15e16942020-05-15 00:25:08 +020036
William Lallemand15e16942020-05-15 00:25:08 +020037
38/***** Below are some sample fetching functions for ACL/patterns *****/
39
Willy Tarreau99ea1882021-10-06 15:37:17 +020040#if defined(HAVE_CRYPTO_memcmp)
41/* Compares bytestring with a variable containing a bytestring. Return value
42 * is `true` if both bytestrings are bytewise identical and `false` otherwise.
43 *
44 * Comparison will be performed in constant time if both bytestrings are of
45 * the same length. If the lengths differ execution time will not be constant.
46 */
47static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private)
48{
49 struct sample tmp;
50 int result;
51
52 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
53 if (arg_p[0].type != ARGT_VAR)
54 return 0;
55
56 if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_BIN))
57 return 0;
58
59 if (smp->data.u.str.data != tmp.data.u.str.data) {
60 smp->data.u.sint = 0;
61 smp->data.type = SMP_T_BOOL;
62 return 1;
63 }
64
65 /* The following comparison is performed in constant time. */
66 result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data);
67
68 smp->data.u.sint = result == 0;
69 smp->data.type = SMP_T_BOOL;
70 return 1;
71}
72
73/* This function checks the "secure_memcmp" converter's arguments and extracts the
74 * variable name and its scope.
75 */
76static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv,
77 const char *file, int line, char **err)
78{
79 if (!args[0].data.str.data) {
80 memprintf(err, "missing variable name");
81 return 0;
82 }
83
84 /* Try to decode a variable. */
85 if (vars_check_arg(&args[0], NULL))
86 return 1;
87
88 memprintf(err, "failed to register variable name '%s'",
89 args[0].data.str.area);
90 return 0;
91}
92#endif // HAVE_secure_memcmp()
93
94static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
95 const char *file, int line, char **err)
96{
97 if (args[0].type == ARGT_STOP)
98 return 1;
99 if (args[0].type != ARGT_SINT) {
100 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
101 return 0;
102 }
103
104 switch (args[0].data.sint) {
105 case 224:
106 case 256:
107 case 384:
108 case 512:
109 /* this is okay */
110 return 1;
111 default:
112 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
113 return 0;
114 }
115}
116
117static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
118{
119 struct buffer *trash = get_trash_chunk();
120 int bits = 256;
Remi Tricot-Le Breton2559bc82022-02-08 17:45:53 +0100121 EVP_MD_CTX *mdctx;
122 const EVP_MD *evp = NULL;
123 unsigned int digest_length = 0;
Willy Tarreau99ea1882021-10-06 15:37:17 +0200124 if (arg_p->data.sint)
125 bits = arg_p->data.sint;
126
127 switch (bits) {
Remi Tricot-Le Breton2559bc82022-02-08 17:45:53 +0100128 case 224:
129 evp = EVP_sha224();
Willy Tarreau99ea1882021-10-06 15:37:17 +0200130 break;
Remi Tricot-Le Breton2559bc82022-02-08 17:45:53 +0100131 case 256:
132 evp = EVP_sha256();
Willy Tarreau99ea1882021-10-06 15:37:17 +0200133 break;
Remi Tricot-Le Breton2559bc82022-02-08 17:45:53 +0100134 case 384:
135 evp = EVP_sha384();
Willy Tarreau99ea1882021-10-06 15:37:17 +0200136 break;
Remi Tricot-Le Breton2559bc82022-02-08 17:45:53 +0100137 case 512:
138 evp = EVP_sha512();
Willy Tarreau99ea1882021-10-06 15:37:17 +0200139 break;
Willy Tarreau99ea1882021-10-06 15:37:17 +0200140 default:
141 return 0;
142 }
143
Remi Tricot-Le Breton2559bc82022-02-08 17:45:53 +0100144 mdctx = EVP_MD_CTX_new();
145 if (!mdctx)
146 return 0;
147 EVP_DigestInit_ex(mdctx, evp, NULL);
148 EVP_DigestUpdate(mdctx, smp->data.u.str.area, smp->data.u.str.data);
149 EVP_DigestFinal_ex(mdctx, (unsigned char*)trash->area, &digest_length);
150 trash->data = digest_length;
151
152 EVP_MD_CTX_free(mdctx);
153
Willy Tarreau99ea1882021-10-06 15:37:17 +0200154 smp->data.u.str = *trash;
155 smp->data.type = SMP_T_BIN;
156 smp->flags &= ~SMP_F_CONST;
157 return 1;
158}
159
160/* This function checks an <arg> and fills it with a variable type if the
161 * <arg> string contains a valid variable name. If failed, the function
162 * tries to perform a base64 decode operation on the same string, and
163 * fills the <arg> with the decoded content.
164 *
165 * Validation is skipped if the <arg> string is empty.
166 *
167 * This function returns 0 if the variable lookup fails and the specified
168 * <arg> string is not a valid base64 encoded string, as well if
169 * unexpected argument type is specified or memory allocation error
170 * occurs. Otherwise it returns 1.
171 */
172static inline int sample_check_arg_base64(struct arg *arg, char **err)
173{
174 char *dec = NULL;
175 int dec_size;
176
177 if (arg->type != ARGT_STR) {
178 memprintf(err, "unexpected argument type");
179 return 0;
180 }
181
182 if (arg->data.str.data == 0) /* empty */
183 return 1;
184
185 if (vars_check_arg(arg, NULL))
186 return 1;
187
188 if (arg->data.str.data % 4) {
189 memprintf(err, "argument needs to be base64 encoded, and "
190 "can either be a string or a variable");
191 return 0;
192 }
193
194 dec_size = (arg->data.str.data / 4 * 3)
195 - (arg->data.str.area[arg->data.str.data-1] == '=' ? 1 : 0)
196 - (arg->data.str.area[arg->data.str.data-2] == '=' ? 1 : 0);
197
198 if ((dec = malloc(dec_size)) == NULL) {
199 memprintf(err, "memory allocation error");
200 return 0;
201 }
202
203 dec_size = base64dec(arg->data.str.area, arg->data.str.data, dec, dec_size);
204 if (dec_size < 0) {
205 memprintf(err, "argument needs to be base64 encoded, and "
206 "can either be a string or a variable");
207 free(dec);
208 return 0;
209 }
210
211 /* base64 decoded */
212 chunk_destroy(&arg->data.str);
213 arg->data.str.area = dec;
214 arg->data.str.data = dec_size;
215 return 1;
216}
217
218#ifdef EVP_CIPH_GCM_MODE
219static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
220 const char *file, int line, char **err)
221{
222 switch(args[0].data.sint) {
223 case 128:
224 case 192:
225 case 256:
226 break;
227 default:
228 memprintf(err, "key size must be 128, 192 or 256 (bits).");
229 return 0;
230 }
231
232 /* Try to decode variables. */
233 if (!sample_check_arg_base64(&args[1], err)) {
234 memprintf(err, "failed to parse nonce : %s", *err);
235 return 0;
236 }
237 if (!sample_check_arg_base64(&args[2], err)) {
238 memprintf(err, "failed to parse key : %s", *err);
239 return 0;
240 }
241 if (!sample_check_arg_base64(&args[3], err)) {
242 memprintf(err, "failed to parse aead_tag : %s", *err);
243 return 0;
244 }
245
246 return 1;
247}
248
249/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
250static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
251{
252 struct sample nonce, key, aead_tag;
253 struct buffer *smp_trash = NULL, *smp_trash_alloc = NULL;
254 EVP_CIPHER_CTX *ctx;
255 int dec_size, ret;
256
257 smp_trash_alloc = alloc_trash_chunk();
258 if (!smp_trash_alloc)
259 return 0;
260
261 /* smp copy */
262 smp_trash_alloc->data = smp->data.u.str.data;
263 if (unlikely(smp_trash_alloc->data > smp_trash_alloc->size))
264 smp_trash_alloc->data = smp_trash_alloc->size;
265 memcpy(smp_trash_alloc->area, smp->data.u.str.area, smp_trash_alloc->data);
266
267 ctx = EVP_CIPHER_CTX_new();
268
269 if (!ctx)
270 goto err;
271
272 smp_trash = alloc_trash_chunk();
273 if (!smp_trash)
274 goto err;
275
276 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
277 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
278 goto err;
279
280 if (arg_p[1].type == ARGT_VAR) {
281 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
282 if (dec_size < 0)
283 goto err;
284 smp_trash->data = dec_size;
285 nonce.data.u.str = *smp_trash;
286 }
287
288 /* Set cipher type and mode */
289 switch(arg_p[0].data.sint) {
290 case 128:
291 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
292 break;
293 case 192:
294 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
295 break;
296 case 256:
297 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
298 break;
299 }
300
301 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce.data.u.str.data, NULL);
302
303 /* Initialise IV */
304 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) nonce.data.u.str.area))
305 goto err;
306
307 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
308 if (!sample_conv_var2smp_str(&arg_p[2], &key))
309 goto err;
310
311 if (arg_p[2].type == ARGT_VAR) {
312 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
313 if (dec_size < 0)
314 goto err;
315 smp_trash->data = dec_size;
316 key.data.u.str = *smp_trash;
317 }
318
319 /* Initialise key */
320 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) key.data.u.str.area, NULL))
321 goto err;
322
323 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
324 (unsigned char *) smp_trash_alloc->area, (int) smp_trash_alloc->data))
325 goto err;
326
327 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
328 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
329 goto err;
330
331 if (arg_p[3].type == ARGT_VAR) {
332 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
333 if (dec_size < 0)
334 goto err;
335 smp_trash_alloc->data = dec_size;
336 aead_tag.data.u.str = *smp_trash_alloc;
337 }
338
339 dec_size = smp_trash->data;
340
341 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, aead_tag.data.u.str.data, (void *) aead_tag.data.u.str.area);
342 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
343
344 if (ret <= 0)
345 goto err;
346
347 smp->data.u.str.data = dec_size + smp_trash->data;
348 smp->data.u.str.area = smp_trash->area;
349 smp->data.type = SMP_T_BIN;
350 smp_dup(smp);
351 free_trash_chunk(smp_trash_alloc);
352 free_trash_chunk(smp_trash);
353 return 1;
354
355err:
356 free_trash_chunk(smp_trash_alloc);
357 free_trash_chunk(smp_trash);
358 return 0;
359}
360#endif
361
362static int check_crypto_digest(struct arg *args, struct sample_conv *conv,
363 const char *file, int line, char **err)
364{
365 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
366
367 if (evp)
368 return 1;
369
370 memprintf(err, "algorithm must be a valid OpenSSL message digest name.");
371 return 0;
372}
373
374static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private)
375{
376 struct buffer *trash = get_trash_chunk();
377 unsigned char *md = (unsigned char*) trash->area;
378 unsigned int md_len = trash->size;
379 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
380 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
381
382 if (!ctx)
383 return 0;
384
385 if (!EVP_DigestInit_ex(ctx, evp, NULL) ||
386 !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) ||
387 !EVP_DigestFinal_ex(ctx, md, &md_len)) {
388 EVP_MD_CTX_free(ctx);
389 return 0;
390 }
391
392 EVP_MD_CTX_free(ctx);
393
394 trash->data = md_len;
395 smp->data.u.str = *trash;
396 smp->data.type = SMP_T_BIN;
397 smp->flags &= ~SMP_F_CONST;
398 return 1;
399}
400
William Lallemand9fbc84e2022-11-03 18:56:37 +0100401/* Take a numerical X509_V_ERR and return its constant name */
402static int sample_conv_x509_v_err(const struct arg *arg_p, struct sample *smp, void *private)
403{
404 const char *res = x509_v_err_int_to_str(smp->data.u.sint);
405
406 /* if the value was found return its string */
407 if (res) {
408 smp->data.u.str.area = (char *)res;
409 smp->data.u.str.data = strlen(res);
410 smp->data.type = SMP_T_STR;
411 smp->flags |= SMP_F_CONST;
412
413 return 1;
William Lallemand117c7fd2023-05-03 15:13:10 +0200414 } else {
415 struct buffer *smp_trash = get_trash_chunk();
416
417 /* if the conversion failed, output the numbers as string */
418 chunk_printf(smp_trash, "%llu", smp->data.u.sint);
419
420 smp->data.u.str = *smp_trash;
421 smp->data.type = SMP_T_STR;
422 smp->flags &= ~SMP_F_CONST;
423
424 return 1;
William Lallemand9fbc84e2022-11-03 18:56:37 +0100425 }
426
427 return 0;
428}
429
Willy Tarreau99ea1882021-10-06 15:37:17 +0200430static int check_crypto_hmac(struct arg *args, struct sample_conv *conv,
431 const char *file, int line, char **err)
432{
433 if (!check_crypto_digest(args, conv, file, line, err))
434 return 0;
435
436 if (!sample_check_arg_base64(&args[1], err)) {
437 memprintf(err, "failed to parse key : %s", *err);
438 return 0;
439 }
440
441 return 1;
442}
443
444static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private)
445{
446 struct sample key;
447 struct buffer *trash = NULL, *key_trash = NULL;
448 unsigned char *md;
449 unsigned int md_len;
450 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
451 int dec_size;
452
453 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
454 if (!sample_conv_var2smp_str(&args[1], &key))
455 return 0;
456
457 if (args[1].type == ARGT_VAR) {
458 key_trash = alloc_trash_chunk();
459 if (!key_trash)
460 goto err;
461
462 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size);
463 if (dec_size < 0)
464 goto err;
465 key_trash->data = dec_size;
466 key.data.u.str = *key_trash;
467 }
468
469 trash = alloc_trash_chunk();
470 if (!trash)
471 goto err;
472
473 md = (unsigned char*) trash->area;
474 md_len = trash->size;
475 if (!HMAC(evp, key.data.u.str.area, key.data.u.str.data, (const unsigned char*) smp->data.u.str.area,
476 smp->data.u.str.data, md, &md_len))
477 goto err;
478
479 free_trash_chunk(key_trash);
480
481 trash->data = md_len;
482 smp->data.u.str = *trash;
483 smp->data.type = SMP_T_BIN;
484 smp_dup(smp);
485 free_trash_chunk(trash);
486 return 1;
487
488err:
489 free_trash_chunk(key_trash);
490 free_trash_chunk(trash);
491 return 0;
492}
493
William Lallemand15e16942020-05-15 00:25:08 +0200494static int
495smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
496{
497 SSL *ssl;
498 struct connection *conn;
499
500 conn = objt_conn(smp->sess->origin);
501 ssl = ssl_sock_get_ssl_object(conn);
502 if (!ssl)
503 return 0;
504
505 smp->flags = 0;
506 smp->data.type = SMP_T_BOOL;
507#ifdef OPENSSL_IS_BORINGSSL
508 {
509 smp->data.u.sint = (SSL_in_early_data(ssl) &&
510 SSL_early_data_accepted(ssl));
511 }
512#else
513 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
514 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
515#endif
516 return 1;
517}
518
519/* boolean, returns true if client cert was present */
520static int
521smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
522{
Willy Tarreau939b0bf2022-04-11 11:29:11 +0200523 struct connection *conn = objt_conn(smp->sess->origin);
524 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
William Lallemand15e16942020-05-15 00:25:08 +0200525
Willy Tarreau939b0bf2022-04-11 11:29:11 +0200526 if (!ctx)
William Lallemand15e16942020-05-15 00:25:08 +0200527 return 0;
528
William Lallemand15e16942020-05-15 00:25:08 +0200529 if (conn->flags & CO_FL_WAIT_XPRT) {
530 smp->flags |= SMP_F_MAY_CHANGE;
531 return 0;
532 }
533
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200534 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +0200535 smp->data.type = SMP_T_BOOL;
536 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
537
538 return 1;
539}
540
541/* binary, returns a certificate in a binary chunk (der/raw).
542 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
543 * should be use.
544 */
545static int
546smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
547{
William Lallemandbfa3e812020-06-25 20:07:18 +0200548 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
549 int conn_server = (kw[4] == 's') ? 1 : 0;
550
William Lallemand15e16942020-05-15 00:25:08 +0200551 X509 *crt = NULL;
552 int ret = 0;
553 struct buffer *smp_trash;
554 struct connection *conn;
555 SSL *ssl;
556
William Lallemandbfa3e812020-06-25 20:07:18 +0200557 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200558 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +0200559 else
560 conn = objt_conn(smp->sess->origin);
561
William Lallemand15e16942020-05-15 00:25:08 +0200562 ssl = ssl_sock_get_ssl_object(conn);
563 if (!ssl)
564 return 0;
565
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200566 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +0200567 smp->flags |= SMP_F_MAY_CHANGE;
568 return 0;
569 }
570
571 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200572 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +0200573 else
574 crt = SSL_get_certificate(ssl);
575
576 if (!crt)
577 goto out;
578
579 smp_trash = get_trash_chunk();
580 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
581 goto out;
582
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200583 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +0200584 smp->data.u.str = *smp_trash;
585 smp->data.type = SMP_T_BIN;
586 ret = 1;
587out:
588 /* SSL_get_peer_certificate, it increase X509 * ref count */
589 if (cert_peer && crt)
590 X509_free(crt);
591 return ret;
592}
593
William Dauchya598b502020-08-06 18:11:38 +0200594/* binary, returns a chain certificate in a binary chunk (der/raw).
595 * The 5th keyword char is used to support only peer cert
596 */
597static int
598smp_fetch_ssl_x_chain_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
599{
600 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
601 int conn_server = (kw[4] == 's') ? 1 : 0;
602 struct buffer *smp_trash;
603 struct buffer *tmp_trash = NULL;
604 struct connection *conn;
605 STACK_OF(X509) *certs = NULL;
606 X509 *crt = NULL;
607 SSL *ssl;
608 int ret = 0;
609 int num_certs;
610 int i;
611
612 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200613 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Dauchya598b502020-08-06 18:11:38 +0200614 else
615 conn = objt_conn(smp->sess->origin);
616
617 if (!conn)
618 return 0;
619
620 ssl = ssl_sock_get_ssl_object(conn);
621 if (!ssl)
622 return 0;
623
624 if (conn->flags & CO_FL_WAIT_XPRT) {
625 smp->flags |= SMP_F_MAY_CHANGE;
626 return 0;
627 }
628
629 if (!cert_peer)
630 return 0;
631
632 certs = SSL_get_peer_cert_chain(ssl);
633 if (!certs)
634 return 0;
635
636 num_certs = sk_X509_num(certs);
637 if (!num_certs)
638 goto out;
639 smp_trash = get_trash_chunk();
640 tmp_trash = alloc_trash_chunk();
641 if (!tmp_trash)
642 goto out;
643 for (i = 0; i < num_certs; i++) {
644 crt = sk_X509_value(certs, i);
645 if (ssl_sock_crt2der(crt, tmp_trash) <= 0)
646 goto out;
647 chunk_cat(smp_trash, tmp_trash);
648 }
649
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200650 smp->flags = SMP_F_VOL_SESS;
William Dauchya598b502020-08-06 18:11:38 +0200651 smp->data.u.str = *smp_trash;
652 smp->data.type = SMP_T_BIN;
653 ret = 1;
654out:
655 if (tmp_trash)
656 free_trash_chunk(tmp_trash);
William Dauchya598b502020-08-06 18:11:38 +0200657 return ret;
658}
659
William Lallemand15e16942020-05-15 00:25:08 +0200660/* binary, returns serial of certificate in a binary chunk.
661 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
662 * should be use.
663 */
664static int
665smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
666{
William Lallemandbfa3e812020-06-25 20:07:18 +0200667 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
668 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +0200669 X509 *crt = NULL;
670 int ret = 0;
671 struct buffer *smp_trash;
672 struct connection *conn;
673 SSL *ssl;
674
William Lallemandbfa3e812020-06-25 20:07:18 +0200675 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200676 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +0200677 else
678 conn = objt_conn(smp->sess->origin);
William Lallemand15e16942020-05-15 00:25:08 +0200679 ssl = ssl_sock_get_ssl_object(conn);
680 if (!ssl)
681 return 0;
682
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200683 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +0200684 smp->flags |= SMP_F_MAY_CHANGE;
685 return 0;
686 }
687
688 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200689 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +0200690 else
691 crt = SSL_get_certificate(ssl);
692
693 if (!crt)
694 goto out;
695
696 smp_trash = get_trash_chunk();
697 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
698 goto out;
699
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200700 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +0200701 smp->data.u.str = *smp_trash;
702 smp->data.type = SMP_T_BIN;
703 ret = 1;
704out:
705 /* SSL_get_peer_certificate, it increase X509 * ref count */
706 if (cert_peer && crt)
707 X509_free(crt);
708 return ret;
709}
710
711/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
712 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
713 * should be use.
714 */
715static int
716smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
717{
William Lallemandbfa3e812020-06-25 20:07:18 +0200718 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
719 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +0200720 X509 *crt = NULL;
721 const EVP_MD *digest;
722 int ret = 0;
723 unsigned int len = 0;
724 struct buffer *smp_trash;
725 struct connection *conn;
726 SSL *ssl;
727
William Lallemandbfa3e812020-06-25 20:07:18 +0200728 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200729 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +0200730 else
731 conn = objt_conn(smp->sess->origin);
732
William Lallemand15e16942020-05-15 00:25:08 +0200733 ssl = ssl_sock_get_ssl_object(conn);
734 if (!ssl)
735 return 0;
736
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200737 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +0200738 smp->flags |= SMP_F_MAY_CHANGE;
739 return 0;
740 }
741
742 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200743 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +0200744 else
745 crt = SSL_get_certificate(ssl);
746 if (!crt)
747 goto out;
748
749 smp_trash = get_trash_chunk();
750 digest = EVP_sha1();
751 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
752 smp_trash->data = len;
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200753 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +0200754 smp->data.u.str = *smp_trash;
755 smp->data.type = SMP_T_BIN;
756 ret = 1;
757out:
758 /* SSL_get_peer_certificate, it increase X509 * ref count */
759 if (cert_peer && crt)
760 X509_free(crt);
761 return ret;
762}
763
764/* string, returns certificate's notafter date in ASN1_UTCTIME format.
765 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
766 * should be use.
767 */
768static int
769smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
770{
William Lallemandbfa3e812020-06-25 20:07:18 +0200771 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
772 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +0200773 X509 *crt = NULL;
774 int ret = 0;
775 struct buffer *smp_trash;
776 struct connection *conn;
777 SSL *ssl;
778
William Lallemandbfa3e812020-06-25 20:07:18 +0200779 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200780 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +0200781 else
782 conn = objt_conn(smp->sess->origin);
783
William Lallemand15e16942020-05-15 00:25:08 +0200784 ssl = ssl_sock_get_ssl_object(conn);
785 if (!ssl)
786 return 0;
787
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200788 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +0200789 smp->flags |= SMP_F_MAY_CHANGE;
790 return 0;
791 }
792
793 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200794 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +0200795 else
796 crt = SSL_get_certificate(ssl);
797 if (!crt)
798 goto out;
799
800 smp_trash = get_trash_chunk();
801 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
802 goto out;
803
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200804 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +0200805 smp->data.u.str = *smp_trash;
806 smp->data.type = SMP_T_STR;
807 ret = 1;
808out:
809 /* SSL_get_peer_certificate, it increase X509 * ref count */
810 if (cert_peer && crt)
811 X509_free(crt);
812 return ret;
813}
814
815/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
816 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
817 * should be use.
818 */
819static int
820smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
821{
William Lallemandbfa3e812020-06-25 20:07:18 +0200822 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
823 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +0200824 X509 *crt = NULL;
825 X509_NAME *name;
826 int ret = 0;
827 struct buffer *smp_trash;
828 struct connection *conn;
829 SSL *ssl;
830
William Lallemandbfa3e812020-06-25 20:07:18 +0200831 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200832 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +0200833 else
834 conn = objt_conn(smp->sess->origin);
835
William Lallemand15e16942020-05-15 00:25:08 +0200836 ssl = ssl_sock_get_ssl_object(conn);
837 if (!ssl)
838 return 0;
839
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200840 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +0200841 smp->flags |= SMP_F_MAY_CHANGE;
842 return 0;
843 }
844
845 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200846 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +0200847 else
848 crt = SSL_get_certificate(ssl);
849 if (!crt)
850 goto out;
851
852 name = X509_get_issuer_name(crt);
853 if (!name)
854 goto out;
855
856 smp_trash = get_trash_chunk();
Christopher Faulet3702f782021-01-29 11:30:37 +0100857 if (args[0].type == ARGT_STR && args[0].data.str.data > 0) {
William Lallemand15e16942020-05-15 00:25:08 +0200858 int pos = 1;
859
860 if (args[1].type == ARGT_SINT)
861 pos = args[1].data.sint;
862
863 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
864 goto out;
865 }
Christopher Faulet3702f782021-01-29 11:30:37 +0100866 else if (args[2].type == ARGT_STR && args[2].data.str.data > 0) {
William Lallemand15e16942020-05-15 00:25:08 +0200867 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
868 goto out;
869 }
870 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
871 goto out;
872
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200873 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +0200874 smp->data.type = SMP_T_STR;
875 smp->data.u.str = *smp_trash;
876 ret = 1;
877out:
878 /* SSL_get_peer_certificate, it increase X509 * ref count */
879 if (cert_peer && crt)
880 X509_free(crt);
881 return ret;
882}
883
884/* string, returns notbefore date in ASN1_UTCTIME format.
885 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
886 * should be use.
887 */
888static int
889smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
890{
William Lallemandbfa3e812020-06-25 20:07:18 +0200891 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
892 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +0200893 X509 *crt = NULL;
894 int ret = 0;
895 struct buffer *smp_trash;
896 struct connection *conn;
897 SSL *ssl;
898
William Lallemandbfa3e812020-06-25 20:07:18 +0200899 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200900 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +0200901 else
902 conn = objt_conn(smp->sess->origin);
903
William Lallemand15e16942020-05-15 00:25:08 +0200904 ssl = ssl_sock_get_ssl_object(conn);
905 if (!ssl)
906 return 0;
907
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200908 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +0200909 smp->flags |= SMP_F_MAY_CHANGE;
910 return 0;
911 }
912
913 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200914 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +0200915 else
916 crt = SSL_get_certificate(ssl);
917 if (!crt)
918 goto out;
919
920 smp_trash = get_trash_chunk();
921 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
922 goto out;
923
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200924 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +0200925 smp->data.u.str = *smp_trash;
926 smp->data.type = SMP_T_STR;
927 ret = 1;
928out:
929 /* SSL_get_peer_certificate, it increase X509 * ref count */
930 if (cert_peer && crt)
931 X509_free(crt);
932 return ret;
933}
934
935/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
936 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
937 * should be use.
938 */
939static int
940smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
941{
William Lallemandbfa3e812020-06-25 20:07:18 +0200942 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
943 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +0200944 X509 *crt = NULL;
945 X509_NAME *name;
946 int ret = 0;
947 struct buffer *smp_trash;
948 struct connection *conn;
949 SSL *ssl;
950
William Lallemandbfa3e812020-06-25 20:07:18 +0200951 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200952 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +0200953 else
954 conn = objt_conn(smp->sess->origin);
955
William Lallemand15e16942020-05-15 00:25:08 +0200956 ssl = ssl_sock_get_ssl_object(conn);
957 if (!ssl)
958 return 0;
959
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200960 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +0200961 smp->flags |= SMP_F_MAY_CHANGE;
962 return 0;
963 }
964
965 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200966 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +0200967 else
968 crt = SSL_get_certificate(ssl);
969 if (!crt)
970 goto out;
971
972 name = X509_get_subject_name(crt);
973 if (!name)
974 goto out;
975
976 smp_trash = get_trash_chunk();
Christopher Faulet3702f782021-01-29 11:30:37 +0100977 if (args[0].type == ARGT_STR && args[0].data.str.data > 0) {
William Lallemand15e16942020-05-15 00:25:08 +0200978 int pos = 1;
979
980 if (args[1].type == ARGT_SINT)
981 pos = args[1].data.sint;
982
983 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
984 goto out;
985 }
Christopher Faulet3702f782021-01-29 11:30:37 +0100986 else if (args[2].type == ARGT_STR && args[2].data.str.data > 0) {
William Lallemand15e16942020-05-15 00:25:08 +0200987 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
988 goto out;
989 }
990 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
991 goto out;
992
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200993 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +0200994 smp->data.type = SMP_T_STR;
995 smp->data.u.str = *smp_trash;
996 ret = 1;
997out:
998 /* SSL_get_peer_certificate, it increase X509 * ref count */
999 if (cert_peer && crt)
1000 X509_free(crt);
1001 return ret;
1002}
1003
1004/* integer, returns true if current session use a client certificate */
1005static int
1006smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
1007{
1008 X509 *crt;
1009 struct connection *conn;
1010 SSL *ssl;
1011
1012 conn = objt_conn(smp->sess->origin);
1013 ssl = ssl_sock_get_ssl_object(conn);
1014 if (!ssl)
1015 return 0;
1016
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001017 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +02001018 smp->flags |= SMP_F_MAY_CHANGE;
1019 return 0;
1020 }
1021
1022 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001023 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +02001024 if (crt) {
1025 X509_free(crt);
1026 }
1027
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001028 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001029 smp->data.type = SMP_T_BOOL;
1030 smp->data.u.sint = (crt != NULL);
1031 return 1;
1032}
1033
1034/* integer, returns the certificate version
1035 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
1036 * should be use.
1037 */
1038static int
1039smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
1040{
William Lallemandbfa3e812020-06-25 20:07:18 +02001041 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
1042 int conn_server = (kw[4] == 's') ? 1 : 0;
1043
William Lallemand15e16942020-05-15 00:25:08 +02001044 X509 *crt;
1045 struct connection *conn;
1046 SSL *ssl;
1047
William Lallemandbfa3e812020-06-25 20:07:18 +02001048 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001049 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +02001050 else
1051 conn = objt_conn(smp->sess->origin);
William Lallemand15e16942020-05-15 00:25:08 +02001052 ssl = ssl_sock_get_ssl_object(conn);
1053 if (!ssl)
1054 return 0;
1055
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001056 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +02001057 smp->flags |= SMP_F_MAY_CHANGE;
1058 return 0;
1059 }
1060
1061 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001062 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +02001063 else
1064 crt = SSL_get_certificate(ssl);
1065 if (!crt)
1066 return 0;
1067
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001068 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001069 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
1070 /* SSL_get_peer_certificate increase X509 * ref count */
1071 if (cert_peer)
1072 X509_free(crt);
1073 smp->data.type = SMP_T_SINT;
1074
1075 return 1;
1076}
1077
1078/* string, returns the certificate's signature algorithm.
1079 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
1080 * should be use.
1081 */
1082static int
1083smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
1084{
William Lallemandbfa3e812020-06-25 20:07:18 +02001085 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
1086 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +02001087 X509 *crt;
1088 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
1089 int nid;
1090 struct connection *conn;
1091 SSL *ssl;
1092
William Lallemandbfa3e812020-06-25 20:07:18 +02001093 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001094 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +02001095 else
1096 conn = objt_conn(smp->sess->origin);
1097
William Lallemand15e16942020-05-15 00:25:08 +02001098 ssl = ssl_sock_get_ssl_object(conn);
1099 if (!ssl)
1100 return 0;
1101
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001102 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +02001103 smp->flags |= SMP_F_MAY_CHANGE;
1104 return 0;
1105 }
1106
1107 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001108 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +02001109 else
1110 crt = SSL_get_certificate(ssl);
1111 if (!crt)
1112 return 0;
1113
1114 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
1115 nid = OBJ_obj2nid(algorithm);
1116
1117 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
1118 if (!smp->data.u.str.area) {
1119 /* SSL_get_peer_certificate increase X509 * ref count */
1120 if (cert_peer)
1121 X509_free(crt);
1122 return 0;
1123 }
1124
1125 smp->data.type = SMP_T_STR;
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001126 smp->flags |= SMP_F_VOL_SESS | SMP_F_CONST;
William Lallemand15e16942020-05-15 00:25:08 +02001127 smp->data.u.str.data = strlen(smp->data.u.str.area);
1128 /* SSL_get_peer_certificate increase X509 * ref count */
1129 if (cert_peer)
1130 X509_free(crt);
1131
1132 return 1;
1133}
1134
1135/* string, returns the certificate's key algorithm.
1136 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
1137 * should be use.
1138 */
1139static int
1140smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
1141{
William Lallemandbfa3e812020-06-25 20:07:18 +02001142 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
1143 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +02001144 X509 *crt;
1145 ASN1_OBJECT *algorithm;
1146 int nid;
1147 struct connection *conn;
1148 SSL *ssl;
1149
William Lallemandbfa3e812020-06-25 20:07:18 +02001150 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001151 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +02001152 else
1153 conn = objt_conn(smp->sess->origin);
William Lallemand15e16942020-05-15 00:25:08 +02001154 ssl = ssl_sock_get_ssl_object(conn);
1155 if (!ssl)
1156 return 0;
1157
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001158 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +02001159 smp->flags |= SMP_F_MAY_CHANGE;
1160 return 0;
1161 }
1162
1163 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001164 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +02001165 else
1166 crt = SSL_get_certificate(ssl);
1167 if (!crt)
1168 return 0;
1169
1170 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
1171 nid = OBJ_obj2nid(algorithm);
1172
1173 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
1174 if (!smp->data.u.str.area) {
1175 /* SSL_get_peer_certificate increase X509 * ref count */
1176 if (cert_peer)
1177 X509_free(crt);
1178 return 0;
1179 }
1180
1181 smp->data.type = SMP_T_STR;
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001182 smp->flags |= SMP_F_VOL_SESS | SMP_F_CONST;
William Lallemand15e16942020-05-15 00:25:08 +02001183 smp->data.u.str.data = strlen(smp->data.u.str.area);
1184 if (cert_peer)
1185 X509_free(crt);
1186
1187 return 1;
1188}
1189
1190/* boolean, returns true if front conn. transport layer is SSL.
1191 * This function is also usable on backend conn if the fetch keyword 5th
1192 * char is 'b'.
1193 */
1194static int
1195smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
1196{
1197 struct connection *conn;
1198
1199 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001200 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001201 else
1202 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001203 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001204
1205 smp->data.type = SMP_T_BOOL;
Willy Tarreau939b0bf2022-04-11 11:29:11 +02001206 smp->data.u.sint = conn_is_ssl(conn);
William Lallemand15e16942020-05-15 00:25:08 +02001207 return 1;
1208}
1209
1210/* boolean, returns true if client present a SNI */
1211static int
1212smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
1213{
1214#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1215 struct connection *conn = objt_conn(smp->sess->origin);
1216 SSL *ssl = ssl_sock_get_ssl_object(conn);
1217
1218 smp->data.type = SMP_T_BOOL;
1219 smp->data.u.sint = ssl && SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) != NULL;
1220 return 1;
1221#else
1222 return 0;
1223#endif
1224}
1225
1226/* boolean, returns true if client session has been resumed.
1227 * This function is also usable on backend conn if the fetch keyword 5th
1228 * char is 'b'.
1229 */
1230static int
1231smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
1232{
1233 struct connection *conn;
1234 SSL *ssl;
1235
1236 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001237 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001238 else
1239 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001240 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001241
1242 ssl = ssl_sock_get_ssl_object(conn);
1243
1244 smp->data.type = SMP_T_BOOL;
1245 smp->data.u.sint = ssl && SSL_session_reused(ssl);
1246 return 1;
1247}
1248
1249/* string, returns the used cipher if front conn. transport layer is SSL.
1250 * This function is also usable on backend conn if the fetch keyword 5th
1251 * char is 'b'.
1252 */
1253static int
1254smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
1255{
1256 struct connection *conn;
1257 SSL *ssl;
1258
1259 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001260 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001261 else
1262 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001263 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001264
1265 smp->flags = 0;
1266 ssl = ssl_sock_get_ssl_object(conn);
1267 if (!ssl)
1268 return 0;
1269
1270 smp->data.u.str.area = (char *)SSL_get_cipher_name(ssl);
1271 if (!smp->data.u.str.area)
1272 return 0;
1273
1274 smp->data.type = SMP_T_STR;
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001275 smp->flags |= SMP_F_VOL_SESS | SMP_F_CONST;
William Lallemand15e16942020-05-15 00:25:08 +02001276 smp->data.u.str.data = strlen(smp->data.u.str.area);
1277
1278 return 1;
1279}
1280
1281/* integer, returns the algoritm's keysize if front conn. transport layer
1282 * is SSL.
1283 * This function is also usable on backend conn if the fetch keyword 5th
1284 * char is 'b'.
1285 */
1286static int
1287smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
1288{
1289 struct connection *conn;
1290 SSL *ssl;
1291 int sint;
1292
1293 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001294 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001295 else
1296 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001297 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001298
1299 smp->flags = 0;
1300 ssl = ssl_sock_get_ssl_object(conn);
1301 if (!ssl)
1302 return 0;
1303
1304 if (!SSL_get_cipher_bits(ssl, &sint))
1305 return 0;
1306
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001307 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001308 smp->data.u.sint = sint;
1309 smp->data.type = SMP_T_SINT;
1310
1311 return 1;
1312}
1313
1314/* integer, returns the used keysize if front conn. transport layer is SSL.
1315 * This function is also usable on backend conn if the fetch keyword 5th
1316 * char is 'b'.
1317 */
1318static int
1319smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
1320{
1321 struct connection *conn;
1322 SSL *ssl;
1323
1324 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001325 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001326 else
1327 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001328 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001329
1330 smp->flags = 0;
1331 ssl = ssl_sock_get_ssl_object(conn);
1332 if (!ssl)
1333 return 0;
1334
1335 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ssl, NULL);
1336 if (!smp->data.u.sint)
1337 return 0;
1338
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001339 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001340 smp->data.type = SMP_T_SINT;
1341
1342 return 1;
1343}
1344
1345#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
1346static int
1347smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
1348{
1349 struct connection *conn;
1350 SSL *ssl;
1351 unsigned int len = 0;
1352
1353 smp->flags = SMP_F_CONST;
1354 smp->data.type = SMP_T_STR;
1355
1356 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001357 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001358 else
1359 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001360 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001361
1362 ssl = ssl_sock_get_ssl_object(conn);
1363 if (!ssl)
1364 return 0;
1365
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001366 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001367 smp->data.u.str.area = NULL;
1368 SSL_get0_next_proto_negotiated(ssl,
1369 (const unsigned char **)&smp->data.u.str.area,
1370 &len);
1371
1372 if (!smp->data.u.str.area)
1373 return 0;
1374
1375 smp->data.u.str.data = len;
1376 return 1;
1377}
1378#endif
1379
1380#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1381static int
1382smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
1383{
1384 struct connection *conn;
1385 SSL *ssl;
1386 unsigned int len = 0;
1387
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001388 smp->flags = SMP_F_VOL_SESS | SMP_F_CONST;
William Lallemand15e16942020-05-15 00:25:08 +02001389 smp->data.type = SMP_T_STR;
1390
1391 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001392 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001393 else
1394 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001395 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001396
1397 ssl = ssl_sock_get_ssl_object(conn);
1398 if (!ssl)
1399 return 0;
1400
1401 smp->data.u.str.area = NULL;
1402 SSL_get0_alpn_selected(ssl,
1403 (const unsigned char **)&smp->data.u.str.area,
1404 &len);
1405
1406 if (!smp->data.u.str.area)
1407 return 0;
1408
1409 smp->data.u.str.data = len;
1410 return 1;
1411}
1412#endif
1413
1414/* string, returns the used protocol if front conn. transport layer is SSL.
1415 * This function is also usable on backend conn if the fetch keyword 5th
1416 * char is 'b'.
1417 */
1418static int
1419smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
1420{
1421 struct connection *conn;
1422 SSL *ssl;
1423
1424 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001425 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001426 else
1427 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001428 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001429
1430 smp->flags = 0;
1431 ssl = ssl_sock_get_ssl_object(conn);
1432 if (!ssl)
1433 return 0;
1434
1435 smp->data.u.str.area = (char *)SSL_get_version(ssl);
1436 if (!smp->data.u.str.area)
1437 return 0;
1438
1439 smp->data.type = SMP_T_STR;
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001440 smp->flags = SMP_F_VOL_SESS | SMP_F_CONST;
William Lallemand15e16942020-05-15 00:25:08 +02001441 smp->data.u.str.data = strlen(smp->data.u.str.area);
1442
1443 return 1;
1444}
1445
1446/* binary, returns the SSL stream id if front conn. transport layer is SSL.
1447 * This function is also usable on backend conn if the fetch keyword 5th
1448 * char is 'b'.
1449 */
1450#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
1451static int
1452smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
1453{
1454 struct connection *conn;
1455 SSL_SESSION *ssl_sess;
1456 SSL *ssl;
1457 unsigned int len = 0;
1458
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001459 smp->flags = SMP_F_VOL_SESS | SMP_F_CONST;
William Lallemand15e16942020-05-15 00:25:08 +02001460 smp->data.type = SMP_T_BIN;
1461
1462 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001463 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001464 else
1465 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001466 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001467
1468 ssl = ssl_sock_get_ssl_object(conn);
1469 if (!ssl)
1470 return 0;
1471
1472 ssl_sess = SSL_get_session(ssl);
1473 if (!ssl_sess)
1474 return 0;
1475
1476 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
1477 if (!smp->data.u.str.area || !len)
1478 return 0;
1479
1480 smp->data.u.str.data = len;
1481 return 1;
1482}
1483#endif
1484
1485
Ilya Shipitsindf627942021-03-25 00:41:41 +05001486#ifdef HAVE_SSL_EXTRACT_RANDOM
William Lallemand15e16942020-05-15 00:25:08 +02001487static int
1488smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
1489{
1490 struct connection *conn;
1491 struct buffer *data;
1492 SSL *ssl;
1493
1494 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001495 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001496 else
1497 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001498 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001499
1500 ssl = ssl_sock_get_ssl_object(conn);
1501 if (!ssl)
1502 return 0;
1503
1504 data = get_trash_chunk();
1505 if (kw[7] == 'c')
1506 data->data = SSL_get_client_random(ssl,
1507 (unsigned char *) data->area,
1508 data->size);
1509 else
1510 data->data = SSL_get_server_random(ssl,
1511 (unsigned char *) data->area,
1512 data->size);
1513 if (!data->data)
1514 return 0;
1515
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001516 smp->flags = SMP_F_VOL_TEST;
William Lallemand15e16942020-05-15 00:25:08 +02001517 smp->data.type = SMP_T_BIN;
1518 smp->data.u.str = *data;
1519
1520 return 1;
1521}
1522
1523static int
1524smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
1525{
1526 struct connection *conn;
1527 SSL_SESSION *ssl_sess;
1528 struct buffer *data;
1529 SSL *ssl;
1530
1531 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001532 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001533 else
1534 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001535 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001536
1537 ssl = ssl_sock_get_ssl_object(conn);
1538 if (!ssl)
1539 return 0;
1540
1541 ssl_sess = SSL_get_session(ssl);
1542 if (!ssl_sess)
1543 return 0;
1544
1545 data = get_trash_chunk();
1546 data->data = SSL_SESSION_get_master_key(ssl_sess,
1547 (unsigned char *) data->area,
1548 data->size);
1549 if (!data->data)
1550 return 0;
1551
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001552 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001553 smp->data.type = SMP_T_BIN;
1554 smp->data.u.str = *data;
1555
1556 return 1;
1557}
1558#endif
1559
William Lallemand15e16942020-05-15 00:25:08 +02001560static int
1561smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
1562{
Willy Tarreau579259d2021-11-05 19:12:54 +01001563#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand15e16942020-05-15 00:25:08 +02001564 struct connection *conn;
1565 SSL *ssl;
1566
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001567 smp->flags = SMP_F_VOL_SESS | SMP_F_CONST;
William Lallemand15e16942020-05-15 00:25:08 +02001568 smp->data.type = SMP_T_STR;
1569
1570 conn = objt_conn(smp->sess->origin);
1571 ssl = ssl_sock_get_ssl_object(conn);
1572 if (!ssl)
1573 return 0;
1574
1575 smp->data.u.str.area = (char *)SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Remi Tricot-Le Bretona9967632022-01-07 17:12:01 +01001576 if (!smp->data.u.str.area) {
1577 /* We might have stored the SNI ourselves, look for it in the
1578 * context's ex_data.
1579 */
1580 smp->data.u.str.area = SSL_get_ex_data(ssl, ssl_client_sni_index);
1581
1582 if (!smp->data.u.str.area)
1583 return 0;
1584 }
William Lallemand15e16942020-05-15 00:25:08 +02001585
1586 smp->data.u.str.data = strlen(smp->data.u.str.area);
Remi Tricot-Le Bretona9967632022-01-07 17:12:01 +01001587
William Lallemand15e16942020-05-15 00:25:08 +02001588 return 1;
Willy Tarreau579259d2021-11-05 19:12:54 +01001589#else
1590 /* SNI not supported */
1591 return 0;
William Lallemand15e16942020-05-15 00:25:08 +02001592#endif
Willy Tarreau579259d2021-11-05 19:12:54 +01001593}
William Lallemand15e16942020-05-15 00:25:08 +02001594
Marcin Deranek959a48c2021-07-13 15:14:21 +02001595/* binary, returns tls client hello cipher list.
1596 * Arguments: filter_option (0,1)
1597 */
William Lallemand15e16942020-05-15 00:25:08 +02001598static int
1599smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
1600{
Marcin Deranek959a48c2021-07-13 15:14:21 +02001601 struct buffer *smp_trash;
William Lallemand15e16942020-05-15 00:25:08 +02001602 struct connection *conn;
1603 struct ssl_capture *capture;
1604 SSL *ssl;
1605
1606 conn = objt_conn(smp->sess->origin);
1607 ssl = ssl_sock_get_ssl_object(conn);
1608 if (!ssl)
1609 return 0;
1610
1611 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
1612 if (!capture)
1613 return 0;
1614
Marcin Deranek959a48c2021-07-13 15:14:21 +02001615 if (args[0].data.sint) {
1616 smp_trash = get_trash_chunk();
1617 exclude_tls_grease(capture->data + capture->ciphersuite_offset, capture->ciphersuite_len, smp_trash);
1618 smp->data.u.str.area = smp_trash->area;
1619 smp->data.u.str.data = smp_trash->data;
1620 smp->flags = SMP_F_VOL_SESS;
1621 }
1622 else {
1623 smp->data.u.str.area = capture->data + capture->ciphersuite_offset;
1624 smp->data.u.str.data = capture->ciphersuite_len;
1625 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
1626 }
1627
William Lallemand15e16942020-05-15 00:25:08 +02001628 smp->data.type = SMP_T_BIN;
William Lallemand15e16942020-05-15 00:25:08 +02001629 return 1;
1630}
1631
Marcin Deranek959a48c2021-07-13 15:14:21 +02001632/* binary, returns tls client hello cipher list as hexadecimal string.
1633 * Arguments: filter_option (0,1)
1634 */
William Lallemand15e16942020-05-15 00:25:08 +02001635static int
1636smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
1637{
1638 struct buffer *data;
1639
1640 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
1641 return 0;
1642
1643 data = get_trash_chunk();
1644 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001645 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001646 smp->data.type = SMP_T_BIN;
1647 smp->data.u.str = *data;
1648 return 1;
1649}
1650
Marcin Deranek959a48c2021-07-13 15:14:21 +02001651/* integer, returns xxh64 hash of tls client hello cipher list. */
William Lallemand15e16942020-05-15 00:25:08 +02001652static int
1653smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
1654{
1655 struct connection *conn;
1656 struct ssl_capture *capture;
1657 SSL *ssl;
1658
1659 conn = objt_conn(smp->sess->origin);
1660 ssl = ssl_sock_get_ssl_object(conn);
1661 if (!ssl)
1662 return 0;
1663
1664 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
1665 if (!capture)
1666 return 0;
1667
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001668 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001669 smp->data.type = SMP_T_SINT;
1670 smp->data.u.sint = capture->xxh64;
1671 return 1;
1672}
1673
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001674static int
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02001675smp_fetch_ssl_fc_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001676{
1677 struct connection *conn;
1678 struct ssl_sock_ctx *ctx;
1679
Remi Tricot-Le Breton163cdeb2021-09-01 15:52:14 +02001680 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001681 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
Remi Tricot-Le Breton163cdeb2021-09-01 15:52:14 +02001682 else
1683 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001684 smp->strm ? sc_conn(smp->strm->scb) : NULL;
Remi Tricot-Le Breton163cdeb2021-09-01 15:52:14 +02001685
Willy Tarreauce7a5e02022-04-12 07:40:42 +02001686 if (!conn)
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001687 return 0;
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001688
1689 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
1690 smp->flags = SMP_F_MAY_CHANGE;
1691 return 0;
1692 }
1693
Willy Tarreauce7a5e02022-04-12 07:40:42 +02001694 ctx = conn_get_ssl_sock_ctx(conn);
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001695 if (!ctx)
1696 return 0;
1697
1698 smp->flags = SMP_F_VOL_SESS;
1699 smp->data.type = SMP_T_SINT;
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02001700 smp->data.u.sint = ctx->error_code;
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001701 return 1;
1702}
1703
1704static int
Marcin Deranek959a48c2021-07-13 15:14:21 +02001705smp_fetch_ssl_fc_protocol_hello_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
1706{
1707 struct connection *conn;
1708 struct ssl_capture *capture;
1709 SSL *ssl;
1710
1711 conn = objt_conn(smp->sess->origin);
1712 ssl = ssl_sock_get_ssl_object(conn);
1713 if (!ssl)
1714 return 0;
1715
1716 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
1717 if (!capture)
1718 return 0;
1719
1720 smp->flags = SMP_F_VOL_SESS;
1721 smp->data.type = SMP_T_SINT;
1722 smp->data.u.sint = capture->protocol_version;
1723 return 1;
1724}
1725
1726static int
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02001727smp_fetch_ssl_fc_err_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001728{
1729 struct connection *conn;
1730 struct ssl_sock_ctx *ctx;
1731 const char *err_code_str;
1732
Remi Tricot-Le Breton163cdeb2021-09-01 15:52:14 +02001733 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001734 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
Remi Tricot-Le Breton163cdeb2021-09-01 15:52:14 +02001735 else
1736 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001737 smp->strm ? sc_conn(smp->strm->scb) : NULL;
Remi Tricot-Le Breton163cdeb2021-09-01 15:52:14 +02001738
Willy Tarreauce7a5e02022-04-12 07:40:42 +02001739 if (!conn)
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001740 return 0;
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001741
1742 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
1743 smp->flags = SMP_F_MAY_CHANGE;
1744 return 0;
1745 }
1746
Willy Tarreauce7a5e02022-04-12 07:40:42 +02001747 ctx = conn_get_ssl_sock_ctx(conn);
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02001748 if (!ctx || !ctx->error_code)
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001749 return 0;
1750
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02001751 err_code_str = ERR_error_string(ctx->error_code, NULL);
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001752
1753 smp->flags = SMP_F_VOL_SESS;
1754 smp->data.type = SMP_T_STR;
1755 smp->data.u.str.area = (char*)err_code_str;
1756 smp->data.u.str.data = strlen(err_code_str);
1757
Marcin Deranek959a48c2021-07-13 15:14:21 +02001758 return 1;
1759}
1760
1761/* binary, returns tls client hello extensions list.
1762 * Arguments: filter_option (0,1)
1763 */
1764static int
1765smp_fetch_ssl_fc_ext_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
1766{
1767 struct buffer *smp_trash;
1768 struct connection *conn;
1769 struct ssl_capture *capture;
1770 SSL *ssl;
1771
1772 conn = objt_conn(smp->sess->origin);
1773 ssl = ssl_sock_get_ssl_object(conn);
1774 if (!ssl)
1775 return 0;
1776
1777 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
1778 if (!capture)
1779 return 0;
1780
1781 if (args[0].data.sint) {
1782 smp_trash = get_trash_chunk();
1783 exclude_tls_grease(capture->data + capture->extensions_offset, capture->extensions_len, smp_trash);
1784 smp->data.u.str.area = smp_trash->area;
1785 smp->data.u.str.data = smp_trash->data;
1786 smp->flags = SMP_F_VOL_SESS;
1787 }
1788 else {
1789 smp->data.u.str.area = capture->data + capture->extensions_offset;
1790 smp->data.u.str.data = capture->extensions_len;
1791 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
1792 }
1793
1794 smp->data.type = SMP_T_BIN;
1795 return 1;
1796}
1797
1798/* binary, returns tls client hello supported elliptic curves.
1799 * Arguments: filter_option (0,1)
1800 */
1801static int
1802smp_fetch_ssl_fc_ecl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
1803{
1804 struct buffer *smp_trash;
1805 struct connection *conn;
1806 struct ssl_capture *capture;
1807 SSL *ssl;
1808
1809 conn = objt_conn(smp->sess->origin);
1810 ssl = ssl_sock_get_ssl_object(conn);
1811 if (!ssl)
1812 return 0;
1813
1814 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
1815 if (!capture)
1816 return 0;
1817
1818 if (args[0].data.sint) {
1819 smp_trash = get_trash_chunk();
1820 exclude_tls_grease(capture->data + capture->ec_offset, capture->ec_len, smp_trash);
1821 smp->data.u.str.area = smp_trash->area;
1822 smp->data.u.str.data = smp_trash->data;
1823 smp->flags = SMP_F_VOL_SESS;
1824 }
1825 else {
1826 smp->data.u.str.area = capture->data + capture->ec_offset;
1827 smp->data.u.str.data = capture->ec_len;
1828 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
1829 }
1830
1831 smp->data.type = SMP_T_BIN;
1832 return 1;
1833}
1834
1835/* binary, returns tls client hello supported elliptic curve point formats */
1836static int
1837smp_fetch_ssl_fc_ecf_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
1838{
1839 struct connection *conn;
1840 struct ssl_capture *capture;
1841 SSL *ssl;
1842
1843 conn = objt_conn(smp->sess->origin);
1844 ssl = ssl_sock_get_ssl_object(conn);
1845 if (!ssl)
1846 return 0;
1847
1848 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
1849 if (!capture)
1850 return 0;
1851
1852 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
1853 smp->data.type = SMP_T_BIN;
1854 smp->data.u.str.area = capture->data + capture->ec_formats_offset;
1855 smp->data.u.str.data = capture->ec_formats_len;
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001856 return 1;
1857}
1858
William Lallemand7d42ef52020-07-06 11:41:30 +02001859/* Dump the SSL keylog, it only works with "tune.ssl.keylog 1" */
William Lallemand722180a2021-06-09 16:46:12 +02001860#ifdef HAVE_SSL_KEYLOG
William Lallemand7d42ef52020-07-06 11:41:30 +02001861static int smp_fetch_ssl_x_keylog(const struct arg *args, struct sample *smp, const char *kw, void *private)
1862{
1863 struct connection *conn;
1864 struct ssl_keylog *keylog;
1865 SSL *ssl;
1866 char *src = NULL;
1867 const char *sfx;
1868
William Lallemandb60a77b2022-11-18 15:00:15 +01001869 if (global_ssl.keylog <= 0)
1870 return 0;
1871
William Lallemand7d42ef52020-07-06 11:41:30 +02001872 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001873 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand7d42ef52020-07-06 11:41:30 +02001874
William Lallemandeec1d452020-07-07 10:48:13 +02001875 if (!conn)
1876 return 0;
1877
William Lallemand7d42ef52020-07-06 11:41:30 +02001878 if (conn->flags & CO_FL_WAIT_XPRT) {
1879 smp->flags |= SMP_F_MAY_CHANGE;
1880 return 0;
1881 }
1882
1883 ssl = ssl_sock_get_ssl_object(conn);
1884 if (!ssl)
1885 return 0;
1886
1887 keylog = SSL_get_ex_data(ssl, ssl_keylog_index);
1888 if (!keylog)
1889 return 0;
1890
1891 sfx = kw + strlen("ssl_xx_");
1892
1893 if (strcmp(sfx, "client_early_traffic_secret") == 0) {
1894 src = keylog->client_early_traffic_secret;
1895 } else if (strcmp(sfx, "client_handshake_traffic_secret") == 0) {
1896 src = keylog->client_handshake_traffic_secret;
1897 } else if (strcmp(sfx, "server_handshake_traffic_secret") == 0) {
1898 src = keylog->server_handshake_traffic_secret;
1899 } else if (strcmp(sfx, "client_traffic_secret_0") == 0) {
1900 src = keylog->client_traffic_secret_0;
1901 } else if (strcmp(sfx, "server_traffic_secret_0") == 0) {
1902 src = keylog->server_traffic_secret_0;
1903 } else if (strcmp(sfx, "exporter_secret") == 0) {
1904 src = keylog->exporter_secret;
1905 } else if (strcmp(sfx, "early_exporter_secret") == 0) {
1906 src = keylog->early_exporter_secret;
1907 }
1908
1909 if (!src || !*src)
1910 return 0;
1911
1912 smp->data.u.str.area = src;
1913 smp->data.type = SMP_T_STR;
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001914 smp->flags |= SMP_F_VOL_TEST | SMP_F_CONST;
William Lallemand7d42ef52020-07-06 11:41:30 +02001915 smp->data.u.str.data = strlen(smp->data.u.str.area);
1916 return 1;
William Lallemand7d42ef52020-07-06 11:41:30 +02001917}
1918#endif
1919
William Lallemand15e16942020-05-15 00:25:08 +02001920static int
1921smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
1922{
Ilya Shipitsinc9dfee42020-10-31 02:10:02 +05001923#if defined(OPENSSL_IS_BORINGSSL) || defined(SSL_CTRL_GET_RAW_CIPHERLIST)
William Lallemand15e16942020-05-15 00:25:08 +02001924 struct buffer *data;
1925 int i;
1926
1927 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
1928 return 0;
1929
1930 data = get_trash_chunk();
1931 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
1932 const char *str;
1933 const SSL_CIPHER *cipher;
1934 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
1935 uint16_t id = (bin[0] << 8) | bin[1];
1936#if defined(OPENSSL_IS_BORINGSSL)
1937 cipher = SSL_get_cipher_by_value(id);
1938#else
1939 struct connection *conn = __objt_conn(smp->sess->origin);
1940 SSL *ssl = ssl_sock_get_ssl_object(conn);
1941 cipher = SSL_CIPHER_find(ssl, bin);
1942#endif
1943 str = SSL_CIPHER_get_name(cipher);
1944 if (!str || strcmp(str, "(NONE)") == 0)
1945 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
1946 else
1947 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
1948 }
1949 smp->data.type = SMP_T_STR;
1950 smp->data.u.str = *data;
1951 return 1;
1952#else
1953 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
1954#endif
1955}
1956
1957#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
1958static int
1959smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
1960{
1961 struct connection *conn;
1962 int finished_len;
1963 struct buffer *finished_trash;
1964 SSL *ssl;
1965
1966 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001967 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001968 else
1969 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001970 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001971
1972 smp->flags = 0;
1973 ssl = ssl_sock_get_ssl_object(conn);
1974 if (!ssl)
1975 return 0;
1976
1977 if (conn->flags & CO_FL_WAIT_XPRT) {
1978 smp->flags |= SMP_F_MAY_CHANGE;
1979 return 0;
1980 }
1981
1982 finished_trash = get_trash_chunk();
1983 if (!SSL_session_reused(ssl))
1984 finished_len = SSL_get_peer_finished(ssl,
1985 finished_trash->area,
1986 finished_trash->size);
1987 else
1988 finished_len = SSL_get_finished(ssl,
1989 finished_trash->area,
1990 finished_trash->size);
1991
1992 if (!finished_len)
1993 return 0;
1994
1995 finished_trash->data = finished_len;
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001996 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001997 smp->data.u.str = *finished_trash;
1998 smp->data.type = SMP_T_BIN;
1999
2000 return 1;
2001}
2002#endif
2003
2004/* integer, returns the first verify error in CA chain of client certificate chain. */
2005static int
2006smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
2007{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02002008 struct connection *conn = objt_conn(smp->sess->origin);
2009 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
William Lallemand15e16942020-05-15 00:25:08 +02002010
Willy Tarreau939b0bf2022-04-11 11:29:11 +02002011 if (conn && conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +02002012 smp->flags = SMP_F_MAY_CHANGE;
2013 return 0;
2014 }
2015
Remi Tricot-Le Breton89b65cf2021-07-29 09:45:50 +02002016 if (!ctx)
2017 return 0;
2018
William Lallemand15e16942020-05-15 00:25:08 +02002019 smp->data.type = SMP_T_SINT;
2020 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02002021 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02002022
2023 return 1;
2024}
2025
2026/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
2027static int
2028smp_fetch_ssl_c_ca_err_depth(const struct arg *args, struct sample *smp, const char *kw, void *private)
2029{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02002030 struct connection *conn = objt_conn(smp->sess->origin);
2031 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
William Lallemand15e16942020-05-15 00:25:08 +02002032
Willy Tarreau939b0bf2022-04-11 11:29:11 +02002033 if (conn && conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +02002034 smp->flags = SMP_F_MAY_CHANGE;
2035 return 0;
2036 }
William Lallemand15e16942020-05-15 00:25:08 +02002037
Remi Tricot-Le Breton89b65cf2021-07-29 09:45:50 +02002038 if (!ctx)
2039 return 0;
2040
William Lallemand15e16942020-05-15 00:25:08 +02002041 smp->data.type = SMP_T_SINT;
2042 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02002043 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02002044
2045 return 1;
2046}
2047
2048/* integer, returns the first verify error on client certificate */
2049static int
2050smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
2051{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02002052 struct connection *conn = objt_conn(smp->sess->origin);
2053 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
William Lallemand15e16942020-05-15 00:25:08 +02002054
Willy Tarreau939b0bf2022-04-11 11:29:11 +02002055 if (conn && conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +02002056 smp->flags = SMP_F_MAY_CHANGE;
2057 return 0;
2058 }
2059
Remi Tricot-Le Breton89b65cf2021-07-29 09:45:50 +02002060 if (!ctx)
2061 return 0;
2062
William Lallemand15e16942020-05-15 00:25:08 +02002063 smp->data.type = SMP_T_SINT;
2064 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02002065 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02002066
2067 return 1;
2068}
2069
2070/* integer, returns the verify result on client cert */
2071static int
2072smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
2073{
2074 struct connection *conn;
2075 SSL *ssl;
2076
2077 conn = objt_conn(smp->sess->origin);
2078 ssl = ssl_sock_get_ssl_object(conn);
2079 if (!ssl)
2080 return 0;
2081
2082 if (conn->flags & CO_FL_WAIT_XPRT) {
2083 smp->flags = SMP_F_MAY_CHANGE;
2084 return 0;
2085 }
2086
2087 smp->data.type = SMP_T_SINT;
2088 smp->data.u.sint = (long long int)SSL_get_verify_result(ssl);
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02002089 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02002090
2091 return 1;
2092}
2093
2094/* Argument validation functions */
2095
2096/* This function is used to validate the arguments passed to any "x_dn" ssl
2097 * keywords. These keywords support specifying a third parameter that must be
2098 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
2099 */
2100int val_dnfmt(struct arg *arg, char **err_msg)
2101{
2102 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
2103 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
2104 return 0;
2105 }
2106 return 1;
2107}
2108
2109/* Note: must not be declared <const> as its list will be overwritten.
2110 * Please take care of keeping this list alphabetically sorted.
2111 */
2112static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2113 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
2114 { "ssl_bc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV },
2115#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2116 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
2117#endif
2118 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
2119#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
2120 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
2121#endif
2122 { "ssl_bc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
2123 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
2124 { "ssl_bc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
2125 { "ssl_bc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV },
2126#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
2127 { "ssl_bc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
2128#endif
Ilya Shipitsindf627942021-03-25 00:41:41 +05002129#ifdef HAVE_SSL_EXTRACT_RANDOM
William Lallemand15e16942020-05-15 00:25:08 +02002130 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
2131 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
2132 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
2133#endif
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02002134 { "ssl_bc_err", smp_fetch_ssl_fc_err, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV },
2135 { "ssl_bc_err_str", smp_fetch_ssl_fc_err_str, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
William Lallemand15e16942020-05-15 00:25:08 +02002136 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2137 { "ssl_c_ca_err_depth", smp_fetch_ssl_c_ca_err_depth, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2138 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
William Dauchya598b502020-08-06 18:11:38 +02002139 { "ssl_c_chain_der", smp_fetch_ssl_x_chain_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
William Lallemand15e16942020-05-15 00:25:08 +02002140 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2141 { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
2142 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2143 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2144 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2145 { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2146 { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
2147 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2148 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2149 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
2150 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2151 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2152 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2153 { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
2154 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2155 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2156 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2157 { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2158 { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
2159 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2160 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2161 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2162 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
2163 { "ssl_fc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2164 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2165 { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
2166 { "ssl_fc_has_early", smp_fetch_ssl_fc_has_early, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
2167 { "ssl_fc_has_sni", smp_fetch_ssl_fc_has_sni, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
2168 { "ssl_fc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
2169#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
2170 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2171#endif
2172#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2173 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2174#endif
2175 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2176#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
2177 { "ssl_fc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2178#endif
2179 { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2180#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
2181 { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2182#endif
Ilya Shipitsindf627942021-03-25 00:41:41 +05002183#ifdef HAVE_SSL_EXTRACT_RANDOM
William Lallemand15e16942020-05-15 00:25:08 +02002184 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2185 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2186 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2187#endif
William Lallemand7d42ef52020-07-06 11:41:30 +02002188
William Lallemand722180a2021-06-09 16:46:12 +02002189#ifdef HAVE_SSL_KEYLOG
William Lallemand7d42ef52020-07-06 11:41:30 +02002190 { "ssl_fc_client_early_traffic_secret", smp_fetch_ssl_x_keylog, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2191 { "ssl_fc_client_handshake_traffic_secret", smp_fetch_ssl_x_keylog, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2192 { "ssl_fc_server_handshake_traffic_secret", smp_fetch_ssl_x_keylog, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2193 { "ssl_fc_client_traffic_secret_0", smp_fetch_ssl_x_keylog, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2194 { "ssl_fc_server_traffic_secret_0", smp_fetch_ssl_x_keylog, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2195 { "ssl_fc_exporter_secret", smp_fetch_ssl_x_keylog, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2196 { "ssl_fc_early_exporter_secret", smp_fetch_ssl_x_keylog, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2197#endif
2198
William Lallemand15e16942020-05-15 00:25:08 +02002199 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Marcin Deranek959a48c2021-07-13 15:14:21 +02002200 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, ARG1(0,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
2201 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_USE_L5CLI },
2202 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, ARG1(0,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
William Lallemand15e16942020-05-15 00:25:08 +02002203 { "ssl_fc_cipherlist_xxh", smp_fetch_ssl_fc_cl_xxh64, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02002204 { "ssl_fc_err", smp_fetch_ssl_fc_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2205 { "ssl_fc_err_str", smp_fetch_ssl_fc_err_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Marcin Deranek959a48c2021-07-13 15:14:21 +02002206 { "ssl_fc_protocol_hello_id",smp_fetch_ssl_fc_protocol_hello_id,0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2207 { "ssl_fc_extlist_bin", smp_fetch_ssl_fc_ext_bin, ARG1(0,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
2208 { "ssl_fc_eclist_bin", smp_fetch_ssl_fc_ecl_bin, ARG1(0,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
2209 { "ssl_fc_ecformats_bin", smp_fetch_ssl_fc_ecf_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
William Lallemandbfa3e812020-06-25 20:07:18 +02002210
2211/* SSL server certificate fetches */
2212 { "ssl_s_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
William Dauchya598b502020-08-06 18:11:38 +02002213 { "ssl_s_chain_der", smp_fetch_ssl_x_chain_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
William Lallemandbfa3e812020-06-25 20:07:18 +02002214 { "ssl_s_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2215 { "ssl_s_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2216 { "ssl_s_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2217 { "ssl_s_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2218 { "ssl_s_s_dn", smp_fetch_ssl_x_s_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
2219 { "ssl_s_i_dn", smp_fetch_ssl_x_i_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
2220 { "ssl_s_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2221 { "ssl_s_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2222 { "ssl_s_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
William Lallemand15e16942020-05-15 00:25:08 +02002223 { NULL, NULL, 0, 0, 0 },
2224}};
2225
2226INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
2227
Willy Tarreau99ea1882021-10-06 15:37:17 +02002228/* Note: must not be declared <const> as its list will be overwritten */
2229static struct sample_conv_kw_list sample_conv_kws = {ILH, {
2230 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
2231#ifdef EVP_CIPH_GCM_MODE
2232 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
2233#endif
William Lallemand9fbc84e2022-11-03 18:56:37 +01002234 { "x509_v_err_str", sample_conv_x509_v_err, 0, NULL, SMP_T_SINT, SMP_T_STR },
Willy Tarreau99ea1882021-10-06 15:37:17 +02002235 { "digest", sample_conv_crypto_digest, ARG1(1,STR), check_crypto_digest, SMP_T_BIN, SMP_T_BIN },
2236 { "hmac", sample_conv_crypto_hmac, ARG2(2,STR,STR), check_crypto_hmac, SMP_T_BIN, SMP_T_BIN },
2237#if defined(HAVE_CRYPTO_memcmp)
2238 { "secure_memcmp", sample_conv_secure_memcmp, ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN, SMP_T_BOOL },
2239#endif
2240 { NULL, NULL, 0, 0, 0 },
2241}};
2242
2243INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);
2244
2245
William Lallemand15e16942020-05-15 00:25:08 +02002246/* Note: must not be declared <const> as its list will be overwritten.
2247 * Please take care of keeping this list alphabetically sorted.
2248 */
2249static struct acl_kw_list acl_kws = {ILH, {
2250 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
2251 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
2252 { /* END */ },
2253}};
2254
2255INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);