blob: 582b7134c393c41d0be2ec9d31e5c6b56e3ad5a2 [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
Abhijeet Rastogidf97f472023-05-13 20:04:45 -0700541/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of the
542 * client certificate's root CA.
543 */
544static int
545smp_fetch_ssl_r_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
546{
547 X509 *crt = NULL;
548 X509_NAME *name;
549 int ret = 0;
550 struct buffer *smp_trash;
551 struct connection *conn;
552 SSL *ssl;
553
554 conn = objt_conn(smp->sess->origin);
555 ssl = ssl_sock_get_ssl_object(conn);
556 if (!ssl)
557 return 0;
558
559 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
560 smp->flags |= SMP_F_MAY_CHANGE;
561 return 0;
562 }
563
564 crt = ssl_sock_get_verified_chain_root(ssl);
565 if (!crt)
566 goto out;
567
568 name = X509_get_subject_name(crt);
569 if (!name)
570 goto out;
571
572 smp_trash = get_trash_chunk();
573 if (args[0].type == ARGT_STR && args[0].data.str.data > 0) {
574 int pos = 1;
575
576 if (args[1].type == ARGT_SINT)
577 pos = args[1].data.sint;
578
579 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
580 goto out;
581 }
582 else if (args[2].type == ARGT_STR && args[2].data.str.data > 0) {
583 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
584 goto out;
585 }
586 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
587 goto out;
588
589 smp->flags = SMP_F_VOL_SESS;
590 smp->data.type = SMP_T_STR;
591 smp->data.u.str = *smp_trash;
592 ret = 1;
593out:
594 return ret;
595}
596
William Lallemand15e16942020-05-15 00:25:08 +0200597/* binary, returns a certificate in a binary chunk (der/raw).
598 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
599 * should be use.
600 */
601static int
602smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
603{
William Lallemandbfa3e812020-06-25 20:07:18 +0200604 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
605 int conn_server = (kw[4] == 's') ? 1 : 0;
606
William Lallemand15e16942020-05-15 00:25:08 +0200607 X509 *crt = NULL;
608 int ret = 0;
609 struct buffer *smp_trash;
610 struct connection *conn;
611 SSL *ssl;
612
William Lallemandbfa3e812020-06-25 20:07:18 +0200613 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200614 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +0200615 else
616 conn = objt_conn(smp->sess->origin);
617
William Lallemand15e16942020-05-15 00:25:08 +0200618 ssl = ssl_sock_get_ssl_object(conn);
619 if (!ssl)
620 return 0;
621
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200622 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +0200623 smp->flags |= SMP_F_MAY_CHANGE;
624 return 0;
625 }
626
627 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200628 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +0200629 else
630 crt = SSL_get_certificate(ssl);
631
632 if (!crt)
633 goto out;
634
635 smp_trash = get_trash_chunk();
636 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
637 goto out;
638
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200639 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +0200640 smp->data.u.str = *smp_trash;
641 smp->data.type = SMP_T_BIN;
642 ret = 1;
643out:
644 /* SSL_get_peer_certificate, it increase X509 * ref count */
645 if (cert_peer && crt)
646 X509_free(crt);
647 return ret;
648}
649
William Dauchya598b502020-08-06 18:11:38 +0200650/* binary, returns a chain certificate in a binary chunk (der/raw).
651 * The 5th keyword char is used to support only peer cert
652 */
653static int
654smp_fetch_ssl_x_chain_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
655{
656 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
657 int conn_server = (kw[4] == 's') ? 1 : 0;
658 struct buffer *smp_trash;
659 struct buffer *tmp_trash = NULL;
660 struct connection *conn;
661 STACK_OF(X509) *certs = NULL;
662 X509 *crt = NULL;
663 SSL *ssl;
664 int ret = 0;
665 int num_certs;
666 int i;
667
668 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200669 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Dauchya598b502020-08-06 18:11:38 +0200670 else
671 conn = objt_conn(smp->sess->origin);
672
673 if (!conn)
674 return 0;
675
676 ssl = ssl_sock_get_ssl_object(conn);
677 if (!ssl)
678 return 0;
679
680 if (conn->flags & CO_FL_WAIT_XPRT) {
681 smp->flags |= SMP_F_MAY_CHANGE;
682 return 0;
683 }
684
685 if (!cert_peer)
686 return 0;
687
688 certs = SSL_get_peer_cert_chain(ssl);
689 if (!certs)
690 return 0;
691
692 num_certs = sk_X509_num(certs);
693 if (!num_certs)
694 goto out;
695 smp_trash = get_trash_chunk();
696 tmp_trash = alloc_trash_chunk();
697 if (!tmp_trash)
698 goto out;
699 for (i = 0; i < num_certs; i++) {
700 crt = sk_X509_value(certs, i);
701 if (ssl_sock_crt2der(crt, tmp_trash) <= 0)
702 goto out;
703 chunk_cat(smp_trash, tmp_trash);
704 }
705
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200706 smp->flags = SMP_F_VOL_SESS;
William Dauchya598b502020-08-06 18:11:38 +0200707 smp->data.u.str = *smp_trash;
708 smp->data.type = SMP_T_BIN;
709 ret = 1;
710out:
711 if (tmp_trash)
712 free_trash_chunk(tmp_trash);
William Dauchya598b502020-08-06 18:11:38 +0200713 return ret;
714}
715
William Lallemand15e16942020-05-15 00:25:08 +0200716/* binary, returns serial of certificate in a binary chunk.
717 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
718 * should be use.
719 */
720static int
721smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
722{
William Lallemandbfa3e812020-06-25 20:07:18 +0200723 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
724 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +0200725 X509 *crt = NULL;
726 int ret = 0;
727 struct buffer *smp_trash;
728 struct connection *conn;
729 SSL *ssl;
730
William Lallemandbfa3e812020-06-25 20:07:18 +0200731 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200732 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +0200733 else
734 conn = objt_conn(smp->sess->origin);
William Lallemand15e16942020-05-15 00:25:08 +0200735 ssl = ssl_sock_get_ssl_object(conn);
736 if (!ssl)
737 return 0;
738
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200739 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +0200740 smp->flags |= SMP_F_MAY_CHANGE;
741 return 0;
742 }
743
744 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200745 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +0200746 else
747 crt = SSL_get_certificate(ssl);
748
749 if (!crt)
750 goto out;
751
752 smp_trash = get_trash_chunk();
753 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
754 goto out;
755
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200756 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +0200757 smp->data.u.str = *smp_trash;
758 smp->data.type = SMP_T_BIN;
759 ret = 1;
760out:
761 /* SSL_get_peer_certificate, it increase X509 * ref count */
762 if (cert_peer && crt)
763 X509_free(crt);
764 return ret;
765}
766
767/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
768 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
769 * should be use.
770 */
771static int
772smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
773{
William Lallemandbfa3e812020-06-25 20:07:18 +0200774 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
775 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +0200776 X509 *crt = NULL;
777 const EVP_MD *digest;
778 int ret = 0;
779 unsigned int len = 0;
780 struct buffer *smp_trash;
781 struct connection *conn;
782 SSL *ssl;
783
William Lallemandbfa3e812020-06-25 20:07:18 +0200784 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200785 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +0200786 else
787 conn = objt_conn(smp->sess->origin);
788
William Lallemand15e16942020-05-15 00:25:08 +0200789 ssl = ssl_sock_get_ssl_object(conn);
790 if (!ssl)
791 return 0;
792
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200793 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +0200794 smp->flags |= SMP_F_MAY_CHANGE;
795 return 0;
796 }
797
798 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200799 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +0200800 else
801 crt = SSL_get_certificate(ssl);
802 if (!crt)
803 goto out;
804
805 smp_trash = get_trash_chunk();
806 digest = EVP_sha1();
807 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
808 smp_trash->data = len;
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200809 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +0200810 smp->data.u.str = *smp_trash;
811 smp->data.type = SMP_T_BIN;
812 ret = 1;
813out:
814 /* SSL_get_peer_certificate, it increase X509 * ref count */
815 if (cert_peer && crt)
816 X509_free(crt);
817 return ret;
818}
819
820/* string, returns certificate's notafter date in ASN1_UTCTIME format.
821 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
822 * should be use.
823 */
824static int
825smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
826{
William Lallemandbfa3e812020-06-25 20:07:18 +0200827 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
828 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +0200829 X509 *crt = NULL;
830 int ret = 0;
831 struct buffer *smp_trash;
832 struct connection *conn;
833 SSL *ssl;
834
William Lallemandbfa3e812020-06-25 20:07:18 +0200835 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200836 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +0200837 else
838 conn = objt_conn(smp->sess->origin);
839
William Lallemand15e16942020-05-15 00:25:08 +0200840 ssl = ssl_sock_get_ssl_object(conn);
841 if (!ssl)
842 return 0;
843
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200844 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +0200845 smp->flags |= SMP_F_MAY_CHANGE;
846 return 0;
847 }
848
849 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200850 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +0200851 else
852 crt = SSL_get_certificate(ssl);
853 if (!crt)
854 goto out;
855
856 smp_trash = get_trash_chunk();
857 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
858 goto out;
859
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200860 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +0200861 smp->data.u.str = *smp_trash;
862 smp->data.type = SMP_T_STR;
863 ret = 1;
864out:
865 /* SSL_get_peer_certificate, it increase X509 * ref count */
866 if (cert_peer && crt)
867 X509_free(crt);
868 return ret;
869}
870
871/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
872 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
873 * should be use.
874 */
875static int
876smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
877{
William Lallemandbfa3e812020-06-25 20:07:18 +0200878 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
879 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +0200880 X509 *crt = NULL;
881 X509_NAME *name;
882 int ret = 0;
883 struct buffer *smp_trash;
884 struct connection *conn;
885 SSL *ssl;
886
William Lallemandbfa3e812020-06-25 20:07:18 +0200887 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200888 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +0200889 else
890 conn = objt_conn(smp->sess->origin);
891
William Lallemand15e16942020-05-15 00:25:08 +0200892 ssl = ssl_sock_get_ssl_object(conn);
893 if (!ssl)
894 return 0;
895
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200896 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +0200897 smp->flags |= SMP_F_MAY_CHANGE;
898 return 0;
899 }
900
901 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200902 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +0200903 else
904 crt = SSL_get_certificate(ssl);
905 if (!crt)
906 goto out;
907
908 name = X509_get_issuer_name(crt);
909 if (!name)
910 goto out;
911
912 smp_trash = get_trash_chunk();
Christopher Faulet3702f782021-01-29 11:30:37 +0100913 if (args[0].type == ARGT_STR && args[0].data.str.data > 0) {
William Lallemand15e16942020-05-15 00:25:08 +0200914 int pos = 1;
915
916 if (args[1].type == ARGT_SINT)
917 pos = args[1].data.sint;
918
919 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
920 goto out;
921 }
Christopher Faulet3702f782021-01-29 11:30:37 +0100922 else if (args[2].type == ARGT_STR && args[2].data.str.data > 0) {
William Lallemand15e16942020-05-15 00:25:08 +0200923 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
924 goto out;
925 }
926 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
927 goto out;
928
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200929 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +0200930 smp->data.type = SMP_T_STR;
931 smp->data.u.str = *smp_trash;
932 ret = 1;
933out:
934 /* SSL_get_peer_certificate, it increase X509 * ref count */
935 if (cert_peer && crt)
936 X509_free(crt);
937 return ret;
938}
939
940/* string, returns notbefore date in ASN1_UTCTIME format.
941 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
942 * should be use.
943 */
944static int
945smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
946{
William Lallemandbfa3e812020-06-25 20:07:18 +0200947 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
948 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +0200949 X509 *crt = NULL;
950 int ret = 0;
951 struct buffer *smp_trash;
952 struct connection *conn;
953 SSL *ssl;
954
William Lallemandbfa3e812020-06-25 20:07:18 +0200955 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200956 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +0200957 else
958 conn = objt_conn(smp->sess->origin);
959
William Lallemand15e16942020-05-15 00:25:08 +0200960 ssl = ssl_sock_get_ssl_object(conn);
961 if (!ssl)
962 return 0;
963
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200964 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +0200965 smp->flags |= SMP_F_MAY_CHANGE;
966 return 0;
967 }
968
969 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200970 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +0200971 else
972 crt = SSL_get_certificate(ssl);
973 if (!crt)
974 goto out;
975
976 smp_trash = get_trash_chunk();
977 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
978 goto out;
979
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +0200980 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +0200981 smp->data.u.str = *smp_trash;
982 smp->data.type = SMP_T_STR;
983 ret = 1;
984out:
985 /* SSL_get_peer_certificate, it increase X509 * ref count */
986 if (cert_peer && crt)
987 X509_free(crt);
988 return ret;
989}
990
991/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
992 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
993 * should be use.
994 */
995static int
996smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
997{
William Lallemandbfa3e812020-06-25 20:07:18 +0200998 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
999 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +02001000 X509 *crt = NULL;
1001 X509_NAME *name;
1002 int ret = 0;
1003 struct buffer *smp_trash;
1004 struct connection *conn;
1005 SSL *ssl;
1006
William Lallemandbfa3e812020-06-25 20:07:18 +02001007 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001008 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +02001009 else
1010 conn = objt_conn(smp->sess->origin);
1011
William Lallemand15e16942020-05-15 00:25:08 +02001012 ssl = ssl_sock_get_ssl_object(conn);
1013 if (!ssl)
1014 return 0;
1015
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001016 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +02001017 smp->flags |= SMP_F_MAY_CHANGE;
1018 return 0;
1019 }
1020
1021 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001022 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +02001023 else
1024 crt = SSL_get_certificate(ssl);
1025 if (!crt)
1026 goto out;
1027
1028 name = X509_get_subject_name(crt);
1029 if (!name)
1030 goto out;
1031
1032 smp_trash = get_trash_chunk();
Christopher Faulet3702f782021-01-29 11:30:37 +01001033 if (args[0].type == ARGT_STR && args[0].data.str.data > 0) {
William Lallemand15e16942020-05-15 00:25:08 +02001034 int pos = 1;
1035
1036 if (args[1].type == ARGT_SINT)
1037 pos = args[1].data.sint;
1038
1039 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
1040 goto out;
1041 }
Christopher Faulet3702f782021-01-29 11:30:37 +01001042 else if (args[2].type == ARGT_STR && args[2].data.str.data > 0) {
William Lallemand15e16942020-05-15 00:25:08 +02001043 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
1044 goto out;
1045 }
1046 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
1047 goto out;
1048
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001049 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001050 smp->data.type = SMP_T_STR;
1051 smp->data.u.str = *smp_trash;
1052 ret = 1;
1053out:
1054 /* SSL_get_peer_certificate, it increase X509 * ref count */
1055 if (cert_peer && crt)
1056 X509_free(crt);
1057 return ret;
1058}
1059
1060/* integer, returns true if current session use a client certificate */
1061static int
1062smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
1063{
1064 X509 *crt;
1065 struct connection *conn;
1066 SSL *ssl;
1067
1068 conn = objt_conn(smp->sess->origin);
1069 ssl = ssl_sock_get_ssl_object(conn);
1070 if (!ssl)
1071 return 0;
1072
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001073 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +02001074 smp->flags |= SMP_F_MAY_CHANGE;
1075 return 0;
1076 }
1077
1078 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001079 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +02001080 if (crt) {
1081 X509_free(crt);
1082 }
1083
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001084 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001085 smp->data.type = SMP_T_BOOL;
1086 smp->data.u.sint = (crt != NULL);
1087 return 1;
1088}
1089
1090/* integer, returns the certificate version
1091 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
1092 * should be use.
1093 */
1094static int
1095smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
1096{
William Lallemandbfa3e812020-06-25 20:07:18 +02001097 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
1098 int conn_server = (kw[4] == 's') ? 1 : 0;
1099
William Lallemand15e16942020-05-15 00:25:08 +02001100 X509 *crt;
1101 struct connection *conn;
1102 SSL *ssl;
1103
William Lallemandbfa3e812020-06-25 20:07:18 +02001104 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001105 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +02001106 else
1107 conn = objt_conn(smp->sess->origin);
William Lallemand15e16942020-05-15 00:25:08 +02001108 ssl = ssl_sock_get_ssl_object(conn);
1109 if (!ssl)
1110 return 0;
1111
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001112 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +02001113 smp->flags |= SMP_F_MAY_CHANGE;
1114 return 0;
1115 }
1116
1117 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001118 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +02001119 else
1120 crt = SSL_get_certificate(ssl);
1121 if (!crt)
1122 return 0;
1123
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001124 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001125 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
1126 /* SSL_get_peer_certificate increase X509 * ref count */
1127 if (cert_peer)
1128 X509_free(crt);
1129 smp->data.type = SMP_T_SINT;
1130
1131 return 1;
1132}
1133
1134/* string, returns the certificate's signature algorithm.
1135 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
1136 * should be use.
1137 */
1138static int
1139smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
1140{
William Lallemandbfa3e812020-06-25 20:07:18 +02001141 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
1142 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +02001143 X509 *crt;
1144 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
1145 int nid;
1146 struct connection *conn;
1147 SSL *ssl;
1148
William Lallemandbfa3e812020-06-25 20:07:18 +02001149 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001150 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +02001151 else
1152 conn = objt_conn(smp->sess->origin);
1153
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_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(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 /* SSL_get_peer_certificate increase X509 * ref count */
1185 if (cert_peer)
1186 X509_free(crt);
1187
1188 return 1;
1189}
1190
1191/* string, returns the certificate's key algorithm.
1192 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
1193 * should be use.
1194 */
1195static int
1196smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
1197{
William Lallemandbfa3e812020-06-25 20:07:18 +02001198 int cert_peer = (kw[4] == 'c' || kw[4] == 's') ? 1 : 0;
1199 int conn_server = (kw[4] == 's') ? 1 : 0;
William Lallemand15e16942020-05-15 00:25:08 +02001200 X509 *crt;
1201 ASN1_OBJECT *algorithm;
1202 int nid;
1203 struct connection *conn;
1204 SSL *ssl;
1205
William Lallemandbfa3e812020-06-25 20:07:18 +02001206 if (conn_server)
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001207 conn = smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemandbfa3e812020-06-25 20:07:18 +02001208 else
1209 conn = objt_conn(smp->sess->origin);
William Lallemand15e16942020-05-15 00:25:08 +02001210 ssl = ssl_sock_get_ssl_object(conn);
1211 if (!ssl)
1212 return 0;
1213
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001214 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +02001215 smp->flags |= SMP_F_MAY_CHANGE;
1216 return 0;
1217 }
1218
1219 if (cert_peer)
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001220 crt = ssl_sock_get_peer_certificate(ssl);
William Lallemand15e16942020-05-15 00:25:08 +02001221 else
1222 crt = SSL_get_certificate(ssl);
1223 if (!crt)
1224 return 0;
1225
1226 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
1227 nid = OBJ_obj2nid(algorithm);
1228
1229 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
1230 if (!smp->data.u.str.area) {
1231 /* SSL_get_peer_certificate increase X509 * ref count */
1232 if (cert_peer)
1233 X509_free(crt);
1234 return 0;
1235 }
1236
1237 smp->data.type = SMP_T_STR;
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001238 smp->flags |= SMP_F_VOL_SESS | SMP_F_CONST;
William Lallemand15e16942020-05-15 00:25:08 +02001239 smp->data.u.str.data = strlen(smp->data.u.str.area);
1240 if (cert_peer)
1241 X509_free(crt);
1242
1243 return 1;
1244}
1245
1246/* boolean, returns true if front conn. transport layer is SSL.
1247 * This function is also usable on backend conn if the fetch keyword 5th
1248 * char is 'b'.
1249 */
1250static int
1251smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
1252{
1253 struct connection *conn;
1254
1255 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001256 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001257 else
1258 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001259 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001260
1261 smp->data.type = SMP_T_BOOL;
Willy Tarreau939b0bf2022-04-11 11:29:11 +02001262 smp->data.u.sint = conn_is_ssl(conn);
William Lallemand15e16942020-05-15 00:25:08 +02001263 return 1;
1264}
1265
1266/* boolean, returns true if client present a SNI */
1267static int
1268smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
1269{
1270#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1271 struct connection *conn = objt_conn(smp->sess->origin);
1272 SSL *ssl = ssl_sock_get_ssl_object(conn);
1273
1274 smp->data.type = SMP_T_BOOL;
1275 smp->data.u.sint = ssl && SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) != NULL;
1276 return 1;
1277#else
1278 return 0;
1279#endif
1280}
1281
1282/* boolean, returns true if client session has been resumed.
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_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
1288{
1289 struct connection *conn;
1290 SSL *ssl;
1291
1292 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001293 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001294 else
1295 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001296 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001297
1298 ssl = ssl_sock_get_ssl_object(conn);
1299
1300 smp->data.type = SMP_T_BOOL;
1301 smp->data.u.sint = ssl && SSL_session_reused(ssl);
1302 return 1;
1303}
1304
1305/* string, returns the used cipher if front conn. transport layer is SSL.
1306 * This function is also usable on backend conn if the fetch keyword 5th
1307 * char is 'b'.
1308 */
1309static int
1310smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
1311{
1312 struct connection *conn;
1313 SSL *ssl;
1314
1315 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001316 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001317 else
1318 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001319 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001320
1321 smp->flags = 0;
1322 ssl = ssl_sock_get_ssl_object(conn);
1323 if (!ssl)
1324 return 0;
1325
1326 smp->data.u.str.area = (char *)SSL_get_cipher_name(ssl);
1327 if (!smp->data.u.str.area)
1328 return 0;
1329
1330 smp->data.type = SMP_T_STR;
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001331 smp->flags |= SMP_F_VOL_SESS | SMP_F_CONST;
William Lallemand15e16942020-05-15 00:25:08 +02001332 smp->data.u.str.data = strlen(smp->data.u.str.area);
1333
1334 return 1;
1335}
1336
1337/* integer, returns the algoritm's keysize if front conn. transport layer
1338 * is SSL.
1339 * This function is also usable on backend conn if the fetch keyword 5th
1340 * char is 'b'.
1341 */
1342static int
1343smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
1344{
1345 struct connection *conn;
1346 SSL *ssl;
1347 int sint;
1348
1349 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001350 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001351 else
1352 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001353 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001354
1355 smp->flags = 0;
1356 ssl = ssl_sock_get_ssl_object(conn);
1357 if (!ssl)
1358 return 0;
1359
1360 if (!SSL_get_cipher_bits(ssl, &sint))
1361 return 0;
1362
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001363 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001364 smp->data.u.sint = sint;
1365 smp->data.type = SMP_T_SINT;
1366
1367 return 1;
1368}
1369
1370/* integer, returns the used keysize if front conn. transport layer is SSL.
1371 * This function is also usable on backend conn if the fetch keyword 5th
1372 * char is 'b'.
1373 */
1374static int
1375smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
1376{
1377 struct connection *conn;
1378 SSL *ssl;
1379
1380 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001381 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001382 else
1383 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001384 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001385
1386 smp->flags = 0;
1387 ssl = ssl_sock_get_ssl_object(conn);
1388 if (!ssl)
1389 return 0;
1390
1391 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ssl, NULL);
1392 if (!smp->data.u.sint)
1393 return 0;
1394
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001395 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001396 smp->data.type = SMP_T_SINT;
1397
1398 return 1;
1399}
1400
1401#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
1402static int
1403smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
1404{
1405 struct connection *conn;
1406 SSL *ssl;
1407 unsigned int len = 0;
1408
1409 smp->flags = SMP_F_CONST;
1410 smp->data.type = SMP_T_STR;
1411
1412 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001413 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001414 else
1415 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001416 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001417
1418 ssl = ssl_sock_get_ssl_object(conn);
1419 if (!ssl)
1420 return 0;
1421
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001422 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001423 smp->data.u.str.area = NULL;
1424 SSL_get0_next_proto_negotiated(ssl,
1425 (const unsigned char **)&smp->data.u.str.area,
1426 &len);
1427
1428 if (!smp->data.u.str.area)
1429 return 0;
1430
1431 smp->data.u.str.data = len;
1432 return 1;
1433}
1434#endif
1435
1436#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1437static int
1438smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
1439{
1440 struct connection *conn;
1441 SSL *ssl;
1442 unsigned int len = 0;
1443
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001444 smp->flags = SMP_F_VOL_SESS | SMP_F_CONST;
William Lallemand15e16942020-05-15 00:25:08 +02001445 smp->data.type = SMP_T_STR;
1446
1447 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001448 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001449 else
1450 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001451 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001452
1453 ssl = ssl_sock_get_ssl_object(conn);
1454 if (!ssl)
1455 return 0;
1456
1457 smp->data.u.str.area = NULL;
1458 SSL_get0_alpn_selected(ssl,
1459 (const unsigned char **)&smp->data.u.str.area,
1460 &len);
1461
1462 if (!smp->data.u.str.area)
1463 return 0;
1464
1465 smp->data.u.str.data = len;
1466 return 1;
1467}
1468#endif
1469
1470/* string, returns the used protocol if front conn. transport layer is SSL.
1471 * This function is also usable on backend conn if the fetch keyword 5th
1472 * char is 'b'.
1473 */
1474static int
1475smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
1476{
1477 struct connection *conn;
1478 SSL *ssl;
1479
1480 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001481 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001482 else
1483 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001484 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001485
1486 smp->flags = 0;
1487 ssl = ssl_sock_get_ssl_object(conn);
1488 if (!ssl)
1489 return 0;
1490
1491 smp->data.u.str.area = (char *)SSL_get_version(ssl);
1492 if (!smp->data.u.str.area)
1493 return 0;
1494
1495 smp->data.type = SMP_T_STR;
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001496 smp->flags = SMP_F_VOL_SESS | SMP_F_CONST;
William Lallemand15e16942020-05-15 00:25:08 +02001497 smp->data.u.str.data = strlen(smp->data.u.str.area);
1498
1499 return 1;
1500}
1501
1502/* binary, returns the SSL stream id if front conn. transport layer is SSL.
1503 * This function is also usable on backend conn if the fetch keyword 5th
1504 * char is 'b'.
1505 */
1506#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
1507static int
1508smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
1509{
1510 struct connection *conn;
1511 SSL_SESSION *ssl_sess;
1512 SSL *ssl;
1513 unsigned int len = 0;
1514
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001515 smp->flags = SMP_F_VOL_SESS | SMP_F_CONST;
William Lallemand15e16942020-05-15 00:25:08 +02001516 smp->data.type = SMP_T_BIN;
1517
1518 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001519 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001520 else
1521 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001522 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001523
1524 ssl = ssl_sock_get_ssl_object(conn);
1525 if (!ssl)
1526 return 0;
1527
1528 ssl_sess = SSL_get_session(ssl);
1529 if (!ssl_sess)
1530 return 0;
1531
1532 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
1533 if (!smp->data.u.str.area || !len)
1534 return 0;
1535
1536 smp->data.u.str.data = len;
1537 return 1;
1538}
1539#endif
1540
1541
Ilya Shipitsindf627942021-03-25 00:41:41 +05001542#ifdef HAVE_SSL_EXTRACT_RANDOM
William Lallemand15e16942020-05-15 00:25:08 +02001543static int
1544smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
1545{
1546 struct connection *conn;
1547 struct buffer *data;
1548 SSL *ssl;
1549
1550 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001551 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001552 else
1553 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001554 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001555
1556 ssl = ssl_sock_get_ssl_object(conn);
1557 if (!ssl)
1558 return 0;
1559
1560 data = get_trash_chunk();
1561 if (kw[7] == 'c')
1562 data->data = SSL_get_client_random(ssl,
1563 (unsigned char *) data->area,
1564 data->size);
1565 else
1566 data->data = SSL_get_server_random(ssl,
1567 (unsigned char *) data->area,
1568 data->size);
1569 if (!data->data)
1570 return 0;
1571
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001572 smp->flags = SMP_F_VOL_TEST;
William Lallemand15e16942020-05-15 00:25:08 +02001573 smp->data.type = SMP_T_BIN;
1574 smp->data.u.str = *data;
1575
1576 return 1;
1577}
1578
1579static int
1580smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
1581{
1582 struct connection *conn;
1583 SSL_SESSION *ssl_sess;
1584 struct buffer *data;
1585 SSL *ssl;
1586
1587 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001588 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001589 else
1590 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001591 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02001592
1593 ssl = ssl_sock_get_ssl_object(conn);
1594 if (!ssl)
1595 return 0;
1596
1597 ssl_sess = SSL_get_session(ssl);
1598 if (!ssl_sess)
1599 return 0;
1600
1601 data = get_trash_chunk();
1602 data->data = SSL_SESSION_get_master_key(ssl_sess,
1603 (unsigned char *) data->area,
1604 data->size);
1605 if (!data->data)
1606 return 0;
1607
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001608 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001609 smp->data.type = SMP_T_BIN;
1610 smp->data.u.str = *data;
1611
1612 return 1;
1613}
1614#endif
1615
William Lallemand15e16942020-05-15 00:25:08 +02001616static int
1617smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
1618{
Willy Tarreau579259d2021-11-05 19:12:54 +01001619#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand15e16942020-05-15 00:25:08 +02001620 struct connection *conn;
1621 SSL *ssl;
1622
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001623 smp->flags = SMP_F_VOL_SESS | SMP_F_CONST;
William Lallemand15e16942020-05-15 00:25:08 +02001624 smp->data.type = SMP_T_STR;
1625
1626 conn = objt_conn(smp->sess->origin);
1627 ssl = ssl_sock_get_ssl_object(conn);
1628 if (!ssl)
1629 return 0;
1630
1631 smp->data.u.str.area = (char *)SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Remi Tricot-Le Bretona9967632022-01-07 17:12:01 +01001632 if (!smp->data.u.str.area) {
1633 /* We might have stored the SNI ourselves, look for it in the
1634 * context's ex_data.
1635 */
1636 smp->data.u.str.area = SSL_get_ex_data(ssl, ssl_client_sni_index);
1637
1638 if (!smp->data.u.str.area)
1639 return 0;
1640 }
William Lallemand15e16942020-05-15 00:25:08 +02001641
1642 smp->data.u.str.data = strlen(smp->data.u.str.area);
Remi Tricot-Le Bretona9967632022-01-07 17:12:01 +01001643
William Lallemand15e16942020-05-15 00:25:08 +02001644 return 1;
Willy Tarreau579259d2021-11-05 19:12:54 +01001645#else
1646 /* SNI not supported */
1647 return 0;
William Lallemand15e16942020-05-15 00:25:08 +02001648#endif
Willy Tarreau579259d2021-11-05 19:12:54 +01001649}
William Lallemand15e16942020-05-15 00:25:08 +02001650
Marcin Deranek959a48c2021-07-13 15:14:21 +02001651/* binary, returns tls client hello cipher list.
1652 * Arguments: filter_option (0,1)
1653 */
William Lallemand15e16942020-05-15 00:25:08 +02001654static int
1655smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
1656{
Marcin Deranek959a48c2021-07-13 15:14:21 +02001657 struct buffer *smp_trash;
William Lallemand15e16942020-05-15 00:25:08 +02001658 struct connection *conn;
1659 struct ssl_capture *capture;
1660 SSL *ssl;
1661
1662 conn = objt_conn(smp->sess->origin);
1663 ssl = ssl_sock_get_ssl_object(conn);
1664 if (!ssl)
1665 return 0;
1666
1667 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
1668 if (!capture)
1669 return 0;
1670
Marcin Deranek959a48c2021-07-13 15:14:21 +02001671 if (args[0].data.sint) {
1672 smp_trash = get_trash_chunk();
1673 exclude_tls_grease(capture->data + capture->ciphersuite_offset, capture->ciphersuite_len, smp_trash);
1674 smp->data.u.str.area = smp_trash->area;
1675 smp->data.u.str.data = smp_trash->data;
1676 smp->flags = SMP_F_VOL_SESS;
1677 }
1678 else {
1679 smp->data.u.str.area = capture->data + capture->ciphersuite_offset;
1680 smp->data.u.str.data = capture->ciphersuite_len;
1681 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
1682 }
1683
William Lallemand15e16942020-05-15 00:25:08 +02001684 smp->data.type = SMP_T_BIN;
William Lallemand15e16942020-05-15 00:25:08 +02001685 return 1;
1686}
1687
Marcin Deranek959a48c2021-07-13 15:14:21 +02001688/* binary, returns tls client hello cipher list as hexadecimal string.
1689 * Arguments: filter_option (0,1)
1690 */
William Lallemand15e16942020-05-15 00:25:08 +02001691static int
1692smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
1693{
1694 struct buffer *data;
1695
1696 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
1697 return 0;
1698
1699 data = get_trash_chunk();
1700 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001701 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001702 smp->data.type = SMP_T_BIN;
1703 smp->data.u.str = *data;
1704 return 1;
1705}
1706
Marcin Deranek959a48c2021-07-13 15:14:21 +02001707/* integer, returns xxh64 hash of tls client hello cipher list. */
William Lallemand15e16942020-05-15 00:25:08 +02001708static int
1709smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
1710{
1711 struct connection *conn;
1712 struct ssl_capture *capture;
1713 SSL *ssl;
1714
1715 conn = objt_conn(smp->sess->origin);
1716 ssl = ssl_sock_get_ssl_object(conn);
1717 if (!ssl)
1718 return 0;
1719
1720 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
1721 if (!capture)
1722 return 0;
1723
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001724 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02001725 smp->data.type = SMP_T_SINT;
1726 smp->data.u.sint = capture->xxh64;
1727 return 1;
1728}
1729
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001730static int
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02001731smp_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 +02001732{
1733 struct connection *conn;
1734 struct ssl_sock_ctx *ctx;
1735
Remi Tricot-Le Breton163cdeb2021-09-01 15:52:14 +02001736 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001737 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
Remi Tricot-Le Breton163cdeb2021-09-01 15:52:14 +02001738 else
1739 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001740 smp->strm ? sc_conn(smp->strm->scb) : NULL;
Remi Tricot-Le Breton163cdeb2021-09-01 15:52:14 +02001741
Willy Tarreauce7a5e02022-04-12 07:40:42 +02001742 if (!conn)
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001743 return 0;
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001744
1745 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
1746 smp->flags = SMP_F_MAY_CHANGE;
1747 return 0;
1748 }
1749
Willy Tarreauce7a5e02022-04-12 07:40:42 +02001750 ctx = conn_get_ssl_sock_ctx(conn);
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001751 if (!ctx)
1752 return 0;
1753
1754 smp->flags = SMP_F_VOL_SESS;
1755 smp->data.type = SMP_T_SINT;
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02001756 smp->data.u.sint = ctx->error_code;
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001757 return 1;
1758}
1759
1760static int
Marcin Deranek959a48c2021-07-13 15:14:21 +02001761smp_fetch_ssl_fc_protocol_hello_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
1762{
1763 struct connection *conn;
1764 struct ssl_capture *capture;
1765 SSL *ssl;
1766
1767 conn = objt_conn(smp->sess->origin);
1768 ssl = ssl_sock_get_ssl_object(conn);
1769 if (!ssl)
1770 return 0;
1771
1772 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
1773 if (!capture)
1774 return 0;
1775
1776 smp->flags = SMP_F_VOL_SESS;
1777 smp->data.type = SMP_T_SINT;
1778 smp->data.u.sint = capture->protocol_version;
1779 return 1;
1780}
1781
1782static int
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02001783smp_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 +02001784{
1785 struct connection *conn;
1786 struct ssl_sock_ctx *ctx;
1787 const char *err_code_str;
1788
Remi Tricot-Le Breton163cdeb2021-09-01 15:52:14 +02001789 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001790 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
Remi Tricot-Le Breton163cdeb2021-09-01 15:52:14 +02001791 else
1792 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001793 smp->strm ? sc_conn(smp->strm->scb) : NULL;
Remi Tricot-Le Breton163cdeb2021-09-01 15:52:14 +02001794
Willy Tarreauce7a5e02022-04-12 07:40:42 +02001795 if (!conn)
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001796 return 0;
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001797
1798 if (conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
1799 smp->flags = SMP_F_MAY_CHANGE;
1800 return 0;
1801 }
1802
Willy Tarreauce7a5e02022-04-12 07:40:42 +02001803 ctx = conn_get_ssl_sock_ctx(conn);
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02001804 if (!ctx || !ctx->error_code)
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001805 return 0;
1806
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02001807 err_code_str = ERR_error_string(ctx->error_code, NULL);
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001808
1809 smp->flags = SMP_F_VOL_SESS;
1810 smp->data.type = SMP_T_STR;
1811 smp->data.u.str.area = (char*)err_code_str;
1812 smp->data.u.str.data = strlen(err_code_str);
1813
Marcin Deranek959a48c2021-07-13 15:14:21 +02001814 return 1;
1815}
1816
1817/* binary, returns tls client hello extensions list.
1818 * Arguments: filter_option (0,1)
1819 */
1820static int
1821smp_fetch_ssl_fc_ext_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
1822{
1823 struct buffer *smp_trash;
1824 struct connection *conn;
1825 struct ssl_capture *capture;
1826 SSL *ssl;
1827
1828 conn = objt_conn(smp->sess->origin);
1829 ssl = ssl_sock_get_ssl_object(conn);
1830 if (!ssl)
1831 return 0;
1832
1833 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
1834 if (!capture)
1835 return 0;
1836
1837 if (args[0].data.sint) {
1838 smp_trash = get_trash_chunk();
1839 exclude_tls_grease(capture->data + capture->extensions_offset, capture->extensions_len, smp_trash);
1840 smp->data.u.str.area = smp_trash->area;
1841 smp->data.u.str.data = smp_trash->data;
1842 smp->flags = SMP_F_VOL_SESS;
1843 }
1844 else {
1845 smp->data.u.str.area = capture->data + capture->extensions_offset;
1846 smp->data.u.str.data = capture->extensions_len;
1847 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
1848 }
1849
1850 smp->data.type = SMP_T_BIN;
1851 return 1;
1852}
1853
1854/* binary, returns tls client hello supported elliptic curves.
1855 * Arguments: filter_option (0,1)
1856 */
1857static int
1858smp_fetch_ssl_fc_ecl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
1859{
1860 struct buffer *smp_trash;
1861 struct connection *conn;
1862 struct ssl_capture *capture;
1863 SSL *ssl;
1864
1865 conn = objt_conn(smp->sess->origin);
1866 ssl = ssl_sock_get_ssl_object(conn);
1867 if (!ssl)
1868 return 0;
1869
1870 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
1871 if (!capture)
1872 return 0;
1873
1874 if (args[0].data.sint) {
1875 smp_trash = get_trash_chunk();
1876 exclude_tls_grease(capture->data + capture->ec_offset, capture->ec_len, smp_trash);
1877 smp->data.u.str.area = smp_trash->area;
1878 smp->data.u.str.data = smp_trash->data;
1879 smp->flags = SMP_F_VOL_SESS;
1880 }
1881 else {
1882 smp->data.u.str.area = capture->data + capture->ec_offset;
1883 smp->data.u.str.data = capture->ec_len;
1884 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
1885 }
1886
1887 smp->data.type = SMP_T_BIN;
1888 return 1;
1889}
1890
1891/* binary, returns tls client hello supported elliptic curve point formats */
1892static int
1893smp_fetch_ssl_fc_ecf_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
1894{
1895 struct connection *conn;
1896 struct ssl_capture *capture;
1897 SSL *ssl;
1898
1899 conn = objt_conn(smp->sess->origin);
1900 ssl = ssl_sock_get_ssl_object(conn);
1901 if (!ssl)
1902 return 0;
1903
1904 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
1905 if (!capture)
1906 return 0;
1907
1908 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
1909 smp->data.type = SMP_T_BIN;
1910 smp->data.u.str.area = capture->data + capture->ec_formats_offset;
1911 smp->data.u.str.data = capture->ec_formats_len;
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02001912 return 1;
1913}
1914
William Lallemand7d42ef52020-07-06 11:41:30 +02001915/* Dump the SSL keylog, it only works with "tune.ssl.keylog 1" */
William Lallemand722180a2021-06-09 16:46:12 +02001916#ifdef HAVE_SSL_KEYLOG
William Lallemand7d42ef52020-07-06 11:41:30 +02001917static int smp_fetch_ssl_x_keylog(const struct arg *args, struct sample *smp, const char *kw, void *private)
1918{
1919 struct connection *conn;
1920 struct ssl_keylog *keylog;
1921 SSL *ssl;
1922 char *src = NULL;
1923 const char *sfx;
1924
William Lallemandb60a77b2022-11-18 15:00:15 +01001925 if (global_ssl.keylog <= 0)
1926 return 0;
1927
William Lallemand7d42ef52020-07-06 11:41:30 +02001928 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001929 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand7d42ef52020-07-06 11:41:30 +02001930
William Lallemandeec1d452020-07-07 10:48:13 +02001931 if (!conn)
1932 return 0;
1933
William Lallemand7d42ef52020-07-06 11:41:30 +02001934 if (conn->flags & CO_FL_WAIT_XPRT) {
1935 smp->flags |= SMP_F_MAY_CHANGE;
1936 return 0;
1937 }
1938
1939 ssl = ssl_sock_get_ssl_object(conn);
1940 if (!ssl)
1941 return 0;
1942
1943 keylog = SSL_get_ex_data(ssl, ssl_keylog_index);
1944 if (!keylog)
1945 return 0;
1946
1947 sfx = kw + strlen("ssl_xx_");
1948
1949 if (strcmp(sfx, "client_early_traffic_secret") == 0) {
1950 src = keylog->client_early_traffic_secret;
1951 } else if (strcmp(sfx, "client_handshake_traffic_secret") == 0) {
1952 src = keylog->client_handshake_traffic_secret;
1953 } else if (strcmp(sfx, "server_handshake_traffic_secret") == 0) {
1954 src = keylog->server_handshake_traffic_secret;
1955 } else if (strcmp(sfx, "client_traffic_secret_0") == 0) {
1956 src = keylog->client_traffic_secret_0;
1957 } else if (strcmp(sfx, "server_traffic_secret_0") == 0) {
1958 src = keylog->server_traffic_secret_0;
1959 } else if (strcmp(sfx, "exporter_secret") == 0) {
1960 src = keylog->exporter_secret;
1961 } else if (strcmp(sfx, "early_exporter_secret") == 0) {
1962 src = keylog->early_exporter_secret;
1963 }
1964
1965 if (!src || !*src)
1966 return 0;
1967
1968 smp->data.u.str.area = src;
1969 smp->data.type = SMP_T_STR;
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02001970 smp->flags |= SMP_F_VOL_TEST | SMP_F_CONST;
William Lallemand7d42ef52020-07-06 11:41:30 +02001971 smp->data.u.str.data = strlen(smp->data.u.str.area);
1972 return 1;
William Lallemand7d42ef52020-07-06 11:41:30 +02001973}
1974#endif
1975
William Lallemand15e16942020-05-15 00:25:08 +02001976static int
1977smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
1978{
Ilya Shipitsinc9dfee42020-10-31 02:10:02 +05001979#if defined(OPENSSL_IS_BORINGSSL) || defined(SSL_CTRL_GET_RAW_CIPHERLIST)
William Lallemand15e16942020-05-15 00:25:08 +02001980 struct buffer *data;
1981 int i;
1982
1983 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
1984 return 0;
1985
1986 data = get_trash_chunk();
1987 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
1988 const char *str;
1989 const SSL_CIPHER *cipher;
1990 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
1991 uint16_t id = (bin[0] << 8) | bin[1];
1992#if defined(OPENSSL_IS_BORINGSSL)
1993 cipher = SSL_get_cipher_by_value(id);
1994#else
1995 struct connection *conn = __objt_conn(smp->sess->origin);
1996 SSL *ssl = ssl_sock_get_ssl_object(conn);
1997 cipher = SSL_CIPHER_find(ssl, bin);
1998#endif
1999 str = SSL_CIPHER_get_name(cipher);
2000 if (!str || strcmp(str, "(NONE)") == 0)
2001 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
2002 else
2003 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
2004 }
2005 smp->data.type = SMP_T_STR;
2006 smp->data.u.str = *data;
2007 return 1;
2008#else
2009 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
2010#endif
2011}
2012
2013#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
2014static int
2015smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
2016{
2017 struct connection *conn;
2018 int finished_len;
2019 struct buffer *finished_trash;
2020 SSL *ssl;
2021
2022 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Willy Tarreaubde14ad2022-05-27 10:04:04 +02002023 conn = (kw[4] == 'b') ? sc_conn(__objt_check(smp->sess->origin)->sc) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02002024 else
2025 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
Willy Tarreaufd9417b2022-05-18 16:23:22 +02002026 smp->strm ? sc_conn(smp->strm->scb) : NULL;
William Lallemand15e16942020-05-15 00:25:08 +02002027
2028 smp->flags = 0;
2029 ssl = ssl_sock_get_ssl_object(conn);
2030 if (!ssl)
2031 return 0;
2032
2033 if (conn->flags & CO_FL_WAIT_XPRT) {
2034 smp->flags |= SMP_F_MAY_CHANGE;
2035 return 0;
2036 }
2037
2038 finished_trash = get_trash_chunk();
2039 if (!SSL_session_reused(ssl))
2040 finished_len = SSL_get_peer_finished(ssl,
2041 finished_trash->area,
2042 finished_trash->size);
2043 else
2044 finished_len = SSL_get_finished(ssl,
2045 finished_trash->area,
2046 finished_trash->size);
2047
2048 if (!finished_len)
2049 return 0;
2050
2051 finished_trash->data = finished_len;
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02002052 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02002053 smp->data.u.str = *finished_trash;
2054 smp->data.type = SMP_T_BIN;
2055
2056 return 1;
2057}
2058#endif
2059
2060/* integer, returns the first verify error in CA chain of client certificate chain. */
2061static int
2062smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
2063{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02002064 struct connection *conn = objt_conn(smp->sess->origin);
2065 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
William Lallemand15e16942020-05-15 00:25:08 +02002066
Willy Tarreau939b0bf2022-04-11 11:29:11 +02002067 if (conn && conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +02002068 smp->flags = SMP_F_MAY_CHANGE;
2069 return 0;
2070 }
2071
Remi Tricot-Le Breton89b65cf2021-07-29 09:45:50 +02002072 if (!ctx)
2073 return 0;
2074
William Lallemand15e16942020-05-15 00:25:08 +02002075 smp->data.type = SMP_T_SINT;
2076 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02002077 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02002078
2079 return 1;
2080}
2081
2082/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
2083static int
2084smp_fetch_ssl_c_ca_err_depth(const struct arg *args, struct sample *smp, const char *kw, void *private)
2085{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02002086 struct connection *conn = objt_conn(smp->sess->origin);
2087 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
William Lallemand15e16942020-05-15 00:25:08 +02002088
Willy Tarreau939b0bf2022-04-11 11:29:11 +02002089 if (conn && conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +02002090 smp->flags = SMP_F_MAY_CHANGE;
2091 return 0;
2092 }
William Lallemand15e16942020-05-15 00:25:08 +02002093
Remi Tricot-Le Breton89b65cf2021-07-29 09:45:50 +02002094 if (!ctx)
2095 return 0;
2096
William Lallemand15e16942020-05-15 00:25:08 +02002097 smp->data.type = SMP_T_SINT;
2098 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02002099 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02002100
2101 return 1;
2102}
2103
2104/* integer, returns the first verify error on client certificate */
2105static int
2106smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
2107{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02002108 struct connection *conn = objt_conn(smp->sess->origin);
2109 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
William Lallemand15e16942020-05-15 00:25:08 +02002110
Willy Tarreau939b0bf2022-04-11 11:29:11 +02002111 if (conn && conn->flags & CO_FL_WAIT_XPRT && !conn->err_code) {
William Lallemand15e16942020-05-15 00:25:08 +02002112 smp->flags = SMP_F_MAY_CHANGE;
2113 return 0;
2114 }
2115
Remi Tricot-Le Breton89b65cf2021-07-29 09:45:50 +02002116 if (!ctx)
2117 return 0;
2118
William Lallemand15e16942020-05-15 00:25:08 +02002119 smp->data.type = SMP_T_SINT;
2120 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02002121 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02002122
2123 return 1;
2124}
2125
2126/* integer, returns the verify result on client cert */
2127static int
2128smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
2129{
2130 struct connection *conn;
2131 SSL *ssl;
2132
2133 conn = objt_conn(smp->sess->origin);
2134 ssl = ssl_sock_get_ssl_object(conn);
2135 if (!ssl)
2136 return 0;
2137
2138 if (conn->flags & CO_FL_WAIT_XPRT) {
2139 smp->flags = SMP_F_MAY_CHANGE;
2140 return 0;
2141 }
2142
2143 smp->data.type = SMP_T_SINT;
2144 smp->data.u.sint = (long long int)SSL_get_verify_result(ssl);
Amaury Denoyelle2f0a7972020-10-15 16:41:08 +02002145 smp->flags = SMP_F_VOL_SESS;
William Lallemand15e16942020-05-15 00:25:08 +02002146
2147 return 1;
2148}
2149
2150/* Argument validation functions */
2151
2152/* This function is used to validate the arguments passed to any "x_dn" ssl
2153 * keywords. These keywords support specifying a third parameter that must be
2154 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
2155 */
2156int val_dnfmt(struct arg *arg, char **err_msg)
2157{
2158 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
2159 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
2160 return 0;
2161 }
2162 return 1;
2163}
2164
2165/* Note: must not be declared <const> as its list will be overwritten.
2166 * Please take care of keeping this list alphabetically sorted.
2167 */
2168static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2169 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
2170 { "ssl_bc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV },
2171#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2172 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
2173#endif
2174 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
2175#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
2176 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
2177#endif
2178 { "ssl_bc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
2179 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
2180 { "ssl_bc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
2181 { "ssl_bc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV },
2182#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
2183 { "ssl_bc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
2184#endif
Ilya Shipitsindf627942021-03-25 00:41:41 +05002185#ifdef HAVE_SSL_EXTRACT_RANDOM
William Lallemand15e16942020-05-15 00:25:08 +02002186 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
2187 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
2188 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
2189#endif
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02002190 { "ssl_bc_err", smp_fetch_ssl_fc_err, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV },
2191 { "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 +02002192 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2193 { "ssl_c_ca_err_depth", smp_fetch_ssl_c_ca_err_depth, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2194 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
William Dauchya598b502020-08-06 18:11:38 +02002195 { "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 +02002196 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2197 { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
2198 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2199 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2200 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Abhijeet Rastogidf97f472023-05-13 20:04:45 -07002201 { "ssl_c_r_dn", smp_fetch_ssl_r_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
William Lallemand15e16942020-05-15 00:25:08 +02002202 { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2203 { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
2204 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2205 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2206 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
2207 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2208 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2209 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2210 { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
2211 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2212 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2213 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2214 { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2215 { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
2216 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2217 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2218 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2219 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
2220 { "ssl_fc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2221 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2222 { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
2223 { "ssl_fc_has_early", smp_fetch_ssl_fc_has_early, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
2224 { "ssl_fc_has_sni", smp_fetch_ssl_fc_has_sni, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
2225 { "ssl_fc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
2226#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
2227 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2228#endif
2229#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2230 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2231#endif
2232 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2233#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
2234 { "ssl_fc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2235#endif
2236 { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2237#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
2238 { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2239#endif
Ilya Shipitsindf627942021-03-25 00:41:41 +05002240#ifdef HAVE_SSL_EXTRACT_RANDOM
William Lallemand15e16942020-05-15 00:25:08 +02002241 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2242 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2243 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2244#endif
William Lallemand7d42ef52020-07-06 11:41:30 +02002245
William Lallemand722180a2021-06-09 16:46:12 +02002246#ifdef HAVE_SSL_KEYLOG
William Lallemand7d42ef52020-07-06 11:41:30 +02002247 { "ssl_fc_client_early_traffic_secret", smp_fetch_ssl_x_keylog, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2248 { "ssl_fc_client_handshake_traffic_secret", smp_fetch_ssl_x_keylog, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2249 { "ssl_fc_server_handshake_traffic_secret", smp_fetch_ssl_x_keylog, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2250 { "ssl_fc_client_traffic_secret_0", smp_fetch_ssl_x_keylog, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2251 { "ssl_fc_server_traffic_secret_0", smp_fetch_ssl_x_keylog, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2252 { "ssl_fc_exporter_secret", smp_fetch_ssl_x_keylog, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2253 { "ssl_fc_early_exporter_secret", smp_fetch_ssl_x_keylog, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2254#endif
2255
William Lallemand15e16942020-05-15 00:25:08 +02002256 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Marcin Deranek959a48c2021-07-13 15:14:21 +02002257 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, ARG1(0,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
2258 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_USE_L5CLI },
2259 { "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 +02002260 { "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 +02002261 { "ssl_fc_err", smp_fetch_ssl_fc_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2262 { "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 +02002263 { "ssl_fc_protocol_hello_id",smp_fetch_ssl_fc_protocol_hello_id,0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
2264 { "ssl_fc_extlist_bin", smp_fetch_ssl_fc_ext_bin, ARG1(0,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
2265 { "ssl_fc_eclist_bin", smp_fetch_ssl_fc_ecl_bin, ARG1(0,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
2266 { "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 +02002267
2268/* SSL server certificate fetches */
2269 { "ssl_s_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
William Dauchya598b502020-08-06 18:11:38 +02002270 { "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 +02002271 { "ssl_s_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2272 { "ssl_s_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2273 { "ssl_s_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2274 { "ssl_s_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
2275 { "ssl_s_s_dn", smp_fetch_ssl_x_s_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
2276 { "ssl_s_i_dn", smp_fetch_ssl_x_i_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
2277 { "ssl_s_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2278 { "ssl_s_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
2279 { "ssl_s_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
William Lallemand15e16942020-05-15 00:25:08 +02002280 { NULL, NULL, 0, 0, 0 },
2281}};
2282
2283INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
2284
Willy Tarreau99ea1882021-10-06 15:37:17 +02002285/* Note: must not be declared <const> as its list will be overwritten */
2286static struct sample_conv_kw_list sample_conv_kws = {ILH, {
2287 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
2288#ifdef EVP_CIPH_GCM_MODE
2289 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
2290#endif
William Lallemand9fbc84e2022-11-03 18:56:37 +01002291 { "x509_v_err_str", sample_conv_x509_v_err, 0, NULL, SMP_T_SINT, SMP_T_STR },
Willy Tarreau99ea1882021-10-06 15:37:17 +02002292 { "digest", sample_conv_crypto_digest, ARG1(1,STR), check_crypto_digest, SMP_T_BIN, SMP_T_BIN },
2293 { "hmac", sample_conv_crypto_hmac, ARG2(2,STR,STR), check_crypto_hmac, SMP_T_BIN, SMP_T_BIN },
2294#if defined(HAVE_CRYPTO_memcmp)
2295 { "secure_memcmp", sample_conv_secure_memcmp, ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN, SMP_T_BOOL },
2296#endif
2297 { NULL, NULL, 0, 0, 0 },
2298}};
2299
2300INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);
2301
2302
William Lallemand15e16942020-05-15 00:25:08 +02002303/* Note: must not be declared <const> as its list will be overwritten.
2304 * Please take care of keeping this list alphabetically sorted.
2305 */
2306static struct acl_kw_list acl_kws = {ILH, {
2307 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
2308 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
2309 { /* END */ },
2310}};
2311
2312INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);