25RCSID(
"$Id: 4fa71237698596e98c0b2849f86be943ac2defea $")
29#define LOG_PREFIX "tls"
31#include <freeradius-devel/tls/openssl_user_macros.h>
32#include <freeradius-devel/util/pair.h>
33#include <freeradius-devel/server/request.h>
34#include <freeradius-devel/server/pair.h>
35#include <freeradius-devel/der/der.h>
43#include <openssl/x509v3.h>
44#include <openssl/ssl.h>
53 GENERAL_NAMES *
names = NULL;
57 if (!(
names = X509V3_EXT_d2i(ext)))
return false;
59 for (i = 0; i < sk_GENERAL_NAME_num(
names); i++) {
60 GENERAL_NAME *
name = sk_GENERAL_NAME_value(
names, i);
68 (
char const *)ASN1_STRING_get0_data(
name->d.rfc822Name),
69 ASN1_STRING_length(
name->d.rfc822Name),
true) == 0);
77 (
char const *)ASN1_STRING_get0_data(
name->d.dNSName),
78 ASN1_STRING_length(
name->d.dNSName),
true) == 0);
84 if (NID_ms_upn != OBJ_obj2nid(
name->d.otherName->type_id))
break;
87 if (
name->d.otherName->value->type == V_ASN1_UTF8STRING) {
91 (
char const *)ASN1_STRING_get0_data(
name->d.otherName->value->value.utf8string),
92 ASN1_STRING_length(
name->d.otherName->value->value.utf8string),
96 RWARN(
"Invalid UPN in Subject Alt Name (should be UTF-8)");
114 ASN1_STRING *s = X509_EXTENSION_get_data(ext);
115 char unsigned const *
data = ASN1_STRING_get0_data(s);
116 STACK_OF(DIST_POINT) *dps;
118 STACK_OF(GENERAL_NAME) *
names;
123 if (!(dps = d2i_CRL_DIST_POINTS(NULL, &
data, ASN1_STRING_length(s))))
return false;
125 for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
126 dp = sk_DIST_POINT_value(dps, i);
131 if (!dp->distpoint) {
132 CRL_DIST_POINTS_free(dps);
136 names = dp->distpoint->name.fullname;
142 if (dp->reasons)
continue;
144 for (j = 0; j < sk_GENERAL_NAME_num(
names); j++) {
147 if (
name->type != GEN_URI)
continue;
151 (
char const *)ASN1_STRING_get0_data(
name->d.uniformResourceIdentifier),
156 CRL_DIST_POINTS_free(dps);
174#
if OPENSSL_VERSION_NUMBER >= 0x30400000L
184 ASN1_TIME
const *asn_time;
187 STACK_OF(X509_EXTENSION)
const *ext_list = NULL;
191 bool san_found =
false, crl_found =
false;
197#if OPENSSL_VERSION_NUMBER >= 0x30400000L
205 der_len = i2d_X509(cert, NULL);
207 fr_tls_log(request,
"Failed retrieving certificate");
221 fr_tls_log(request,
"Failed decoding certificate");
237 if (
unlikely(X509_NAME_print_ex(fr_tls_bio_dbuff_thread_local(
vp, 256, 0),
238 X509_get_subject_name(cert), 0, XN_FLAG_ONELINE) < 0)) {
239 fr_tls_bio_dbuff_thread_local_clear();
240 fr_tls_log(request,
"Failed retrieving certificate subject");
252 slen = X509_NAME_get_text_by_NID(X509_get_subject_name(cert),
253 NID_commonName, NULL, 0);
260 slen = X509_NAME_get_text_by_NID(X509_get_subject_name(cert), NID_commonName, cn, (
size_t)slen + 1);
262 fr_tls_log(request,
"Failed retrieving certificate common name");
271 ASN1_BIT_STRING
const *sig;
272 X509_ALGOR
const *alg;
274 X509_get0_signature(&sig, &alg, cert);
278 (
uint8_t const *)ASN1_STRING_get0_data(sig),
279 ASN1_STRING_length(sig),
true) == 0);
281 OBJ_obj2txt(
buff,
sizeof(
buff), alg->algorithm, 0);
290 if (
unlikely(X509_NAME_print_ex(fr_tls_bio_dbuff_thread_local(
vp, 256, 0),
291 X509_get_issuer_name(cert), 0, XN_FLAG_ONELINE) < 0)) {
292 fr_tls_bio_dbuff_thread_local_clear();
293 fr_tls_log(request,
"Failed retrieving certificate issuer");
302 ASN1_INTEGER
const *serial = NULL;
306 serial = X509_get0_serialNumber(cert);
308 fr_tls_log(request,
"Failed retrieving certificate serial");
312 len = i2d_ASN1_INTEGER(serial, NULL);
315 i2d_ASN1_INTEGER(serial, &der);
321 asn_time = X509_get0_notBefore(cert);
324 RPWDEBUG(
"Failed parsing certificate not-before");
334 asn_time = X509_get0_notAfter(cert);
337 RPWDEBUG(
"Failed parsing certificate not-after");
347 loc = X509_get_ext_by_NID(cert, NID_subject_alt_name, 0);
349 X509_EXTENSION *ext = X509_get_ext(cert, loc);
350 if (ext) san_found = tls_session_pairs_from_san(
pair_list, ctx, request, ext);
353 loc = X509_get_ext_by_NID(cert, NID_crl_distribution_points, 0);
355 X509_EXTENSION *ext = X509_get_ext(cert, loc);
356 if (ext) crl_found = tls_session_pairs_from_crl(
pair_list, ctx, request, ext);
362 ext_list = X509_get0_extensions(cert);
364 RWDEBUG(
"Failed retrieving extensions");
372 if (sk_X509_EXTENSION_num(ext_list) > 0) {
375 fr_tls_bio_dbuff_t *bd;
378 bio = fr_tls_bio_dbuff_alloc(&bd, NULL, NULL, 257, 4097,
true);
379 in = fr_tls_bio_dbuff_in(bd);
380 out = fr_tls_bio_dbuff_out(bd);
382 for (i = 0; i < sk_X509_EXTENSION_num(ext_list); i++) {
388 ext = sk_X509_EXTENSION_value(ext_list, i);
390 obj = X509_EXTENSION_get_object(ext);
400 if (OBJ_obj2nid(obj) == NID_subject_alt_name) {
401 if (!san_found) san_found = tls_session_pairs_from_san(
pair_list, ctx, request, ext);
405 if (OBJ_obj2nid(obj) == NID_crl_distribution_points) {
406 if (!crl_found) crl_found = tls_session_pairs_from_crl(
pair_list, ctx, request, ext);
410 if (i2a_ASN1_OBJECT(bio, obj) <= 0) {
411 RPWDEBUG(
"Skipping X509 Extension (%i) conversion to attribute. "
412 "Conversion from ASN1 failed...", i);
414 fr_tls_bio_dbuff_reset(bd);
432 RWDEBUG(
"Attribute name too long");
441 RWDEBUG3(
"Skipping attribute \"%pV\": "
442 "Add a dictionary definition if you want to access it",
449 fr_tls_bio_dbuff_reset(bd);
451 if (X509V3_EXT_print(bio, ext, X509V3_EXT_PARSE_UNKNOWN, 0) != 1) {
452 REDEBUG(
"Failed extracting data for \"%s\"", da->name);
465 fr_tls_bio_dbuff_reset(bd);
477 size_t hlen,
size_t dlen,
size_t total_len,
fr_dict_attr_t const *da)
479 size_t i,extension_len;
485 if (total_len < hlen) {
486 REDEBUG(
"Missing length in %s extension", da->name);
492 extension_len = extension[0];
495 extension_len = (extension[0] << 8) + extension[1];
498 if (extension_len * dlen + hlen > total_len) {
499 REDEBUG(
"Invalid length in %s extension", da->name);
503 data = extension + hlen;
505 for (i = 0; i < extension_len; i += dlen) {
529int fr_tls_session_client_hello_cb(SSL *ssl,
UNUSED int *al,
UNUSED void *arg)
531 request_t *request = SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_REQUEST);
533 uint8_t const *ciphers, *extension;
534 int i, *extensions = NULL;
536 STACK_OF(SSL_CIPHER) *sk;
537 SSL_CIPHER
const *cipher;
538 STACK_OF(SSL_CIPHER) *scsvs;
544 if (container)
return SSL_CLIENT_HELLO_SUCCESS;
548 data_size = SSL_client_hello_get0_ciphers(ssl, &ciphers);
549 if (SSL_bytes_to_cipher_list(ssl, ciphers, data_size, SSL_client_hello_isv2(ssl), &sk, &scsvs) == 0) {
550 RPEDEBUG(
"Failed to decode cipher list");
552 if (extensions) OPENSSL_free(extensions);
554 return SSL_CLIENT_HELLO_ERROR;
557 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
559 cipher = sk_SSL_CIPHER_value(sk, i);
563 sk_SSL_CIPHER_free(sk);
564 sk_SSL_CIPHER_free(scsvs);
571 if (SSL_client_hello_get1_extensions_present(ssl, &extensions, &data_size) == 0) {
572 RPEDEBUG(
"Failed to fetch client hello extensions");
576 for (j = 0; j < data_size; j++) {
579 if (SSL_client_hello_get0_ext(ssl, extensions[j], &extension, &total_len) == 0) {
580 RPDEBUG(
"Failed getting client hello extension %d", extensions[j]);
584 switch (extensions[j]) {
585 case TLSEXT_TYPE_supported_groups:
586 if (fr_tls_extension_decode(request, container, extension, 2, 2, total_len,
590 case TLSEXT_TYPE_ec_point_formats:
591 if (fr_tls_extension_decode(request, container, extension, 1, 1, total_len,
595 case TLSEXT_TYPE_signature_algorithms:
596 if (fr_tls_extension_decode(request, container, extension, 2, 2, total_len,
600 case TLSEXT_TYPE_supported_versions:
601 if (fr_tls_extension_decode(request, container, extension, 1, 2, total_len,
605 case TLSEXT_TYPE_psk_kex_modes:
606 if (fr_tls_extension_decode(request, container, extension, 1, 1, total_len,
612 if (extensions) OPENSSL_free(extensions);
619 vp->vp_uint16 = SSL_client_hello_get0_legacy_version(ssl);
623 return SSL_CLIENT_HELLO_SUCCESS;
#define USES_APPLE_DEPRECATED_API
#define DIAG_UNKNOWN_PRAGMAS
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
#define fr_dbuff_set(_dst, _src)
Set the 'current' position in a dbuff or marker using another dbuff or marker, a char pointer,...
#define fr_dbuff_end(_dbuff_or_marker)
Return the current 'end' position of a dbuff or marker.
#define fr_dbuff_remaining(_dbuff_or_marker)
Return the number of bytes remaining between the dbuff or marker and the end of the buffer.
#define fr_dbuff_in_bytes(_dbuff_or_marker,...)
Copy a byte sequence into a dbuff or marker.
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
TALLOC_CTX * tmp_ctx
ctx under which temporary data will be allocated
bool const fr_dict_attr_allowed_chars[SBUFF_CHAR_CLASS]
Characters allowed in a single dictionary attribute name.
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *attr))
Locate a fr_dict_attr_t by its name.
HIDDEN fr_dict_attr_t const * attr_tls_certificate
Attribute definitions for lib curl.
#define RPEDEBUG(fmt,...)
#define RPWDEBUG3(fmt,...)
#define RPWDEBUG(fmt,...)
#define RWDEBUG3(fmt,...)
HIDDEN fr_dict_attr_t const * attr_tls_certificate_subject
HIDDEN fr_dict_attr_t const * attr_tls_client_hello
HIDDEN fr_dict_attr_t const * attr_tls_client_hello_psk_key_mode
HIDDEN fr_dict_attr_t const * attr_tls_certificate_serial
HIDDEN fr_dict_attr_t const * attr_tls_certificate_subject_alt_name_dns
HIDDEN fr_dict_attr_t const * attr_tls_client_hello_supported_group
HIDDEN fr_dict_attr_t const * attr_tls_certificate_not_after
HIDDEN fr_dict_attr_t const * attr_tls_certificate_not_before
HIDDEN fr_dict_attr_t const * attr_tls_certificate_signature_algorithm
HIDDEN fr_dict_attr_t const * attr_tls_certificate_subject_alt_name_upn
HIDDEN fr_dict_attr_t const * attr_tls_client_hello_sig_algo
HIDDEN fr_dict_attr_t const * attr_tls_certificate_common_name
HIDDEN fr_dict_attr_t const * attr_tls_certificate_issuer
HIDDEN fr_dict_attr_t const * attr_tls_certificate_x509v3_crl_distribution_points
HIDDEN fr_dict_attr_t const * attr_tls_client_hello_ec_point_format
HIDDEN fr_dict_attr_t const * attr_der_certificate
HIDDEN fr_dict_attr_t const * attr_tls_certificate_signature
HIDDEN fr_dict_attr_t const * attr_tls_client_hello_tls_version
HIDDEN fr_dict_attr_t const * attr_tls_client_hello_cipher
HIDDEN fr_dict_attr_t const * attr_tls_certificate_subject_alt_name_email
@ FR_TYPE_UINT16
16 Bit unsigned integer.
@ FR_TYPE_UINT8
8 Bit unsigned integer.
int fr_pair_append_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t (and append)
int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
Copy data into an "octets" data type.
int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
Copy data into an "string" data type.
int fr_pair_value_bstrdup_buffer_shallow(fr_pair_t *vp, char const *src, bool tainted)
Assign a string to a "string" type value pair.
int fr_pair_value_from_str(fr_pair_t *vp, char const *value, size_t inlen, fr_sbuff_unescape_rules_t const *uerules, UNUSED bool tainted)
Convert string value to native attribute value.
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
int fr_pair_value_bstrndup(fr_pair_t *vp, char const *src, size_t len, bool tainted)
Copy data into a "string" type value pair.
int fr_pair_value_bstr_alloc(fr_pair_t *vp, char **out, size_t size, bool tainted)
Pre-allocate a memory buffer for a "string" type value pair.
int fr_pair_value_mem_alloc(fr_pair_t *vp, uint8_t **out, size_t size, bool tainted)
Pre-allocate a memory buffer for a "octets" type value pair.
ssize_t fr_der_decode_pair_dbuff(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
static char buff[sizeof("18446744073709551615")+3]
Stores an attribute, a value and various bits of other data.
static const char * names[8]
static fr_unix_time_t fr_unix_time_from_time(time_t time)
Convert a time_t into out internal fr_unix_time_t.
void fr_pair_list_free(fr_pair_list_t *list)
Free memory used by a valuepair list.
void fr_pair_list_prepend(fr_pair_list_t *dst, fr_pair_list_t *src)
Move a list of fr_pair_t from a temporary list to the head of a destination list.
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
int fr_tls_utils_asn1time_to_epoch(time_t *out, ASN1_TIME const *asn1)
Convert OpenSSL's ASN1_TIME to an epoch time.
int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated string to a fr_value_box_t.
#define fr_box_strvalue_buffer(_val)
#define fr_box_strvalue_len(_val, _len)
static size_t char ** out