Coverage Report

Created: 2025-10-28 07:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ndpi/src/lib/protocols/tls.c
Line
Count
Source
1
/*
2
 * tls.c - TLS/TLS/DTLS dissector
3
 *
4
 * Copyright (C) 2016-24 - ntop.org
5
 *
6
 * nDPI is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * nDPI is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with nDPI.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 */
20
21
#include "ndpi_protocol_ids.h"
22
23
#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_TLS
24
25
#include "ndpi_api.h"
26
#include "ndpi_md5.h"
27
#include "ndpi_sha1.h"
28
#include "ndpi_sha256.h"
29
#include "ndpi_encryption.h"
30
#include "ndpi_private.h"
31
32
//#define JA4R_DECIMAL 1
33
34
static void ndpi_search_tls_wrapper(struct ndpi_detection_module_struct *ndpi_struct,
35
            struct ndpi_flow_struct *flow);
36
37
// #define DEBUG_TLS_MEMORY       1
38
// #define DEBUG_TLS              1
39
// #define DEBUG_TLS_BLOCKS       1
40
// #define DEBUG_CERTIFICATE_HASH
41
42
// #define DEBUG_HEURISTIC
43
44
// #define DEBUG_JA 1
45
46
/* #define DEBUG_FINGERPRINT      1 */
47
/* #define DEBUG_ENCRYPTED_SNI    1 */
48
49
/* **************************************** */
50
51
/*
52
  JA3
53
  https://engineering.salesforce.com/tls-fingerprinting-with-ja3-and-ja3s-247362855967
54
55
  JA4
56
  https://github.com/FoxIO-LLC/ja4/blob/main/technical_details/JA4.md
57
*/
58
59
5.94M
#define JA_STR_LEN        1024
60
5.14M
#define MAX_NUM_JA         128
61
#define MAX_JA_STRLEN      256
62
63
union ja_info {
64
  struct {
65
    u_int16_t tls_handshake_version;
66
    u_int16_t num_ciphers, cipher[MAX_NUM_JA];
67
    u_int16_t num_tls_extensions, tls_extension[MAX_NUM_JA];
68
    u_int16_t num_elliptic_curve, elliptic_curve[MAX_NUM_JA];
69
    u_int16_t num_elliptic_curve_point_format, elliptic_curve_point_format[MAX_NUM_JA];
70
    u_int16_t num_signature_algorithms, signature_algorithms[MAX_NUM_JA];
71
    u_int16_t num_supported_versions, supported_versions[MAX_NUM_JA];
72
    char signature_algorithms_str[MAX_JA_STRLEN], alpn[MAX_JA_STRLEN];
73
    char alpn_original_last;  /* Store original last character before null terminator */
74
  } client;
75
76
  struct {
77
    u_int16_t tls_handshake_version;
78
    u_int16_t num_ciphers, cipher[MAX_NUM_JA];
79
    u_int16_t num_tls_extensions, tls_extension[MAX_NUM_JA];
80
    u_int16_t tls_supported_version;
81
    u_int16_t num_elliptic_curve_point_format, elliptic_curve_point_format[MAX_NUM_JA];
82
    char alpn[MAX_JA_STRLEN];
83
  } server;
84
};
85
86
/*
87
  NOTE
88
89
  How to view the certificate fingerprint
90
  1. Using wireshark save the certificate on certificate.bin file as explained
91
  in https://security.stackexchange.com/questions/123851/how-can-i-extract-the-certificate-from-this-pcap-file
92
93
  2. openssl x509 -inform der -in certificate.bin -text > certificate.der
94
  3. openssl x509 -noout -fingerprint -sha1 -inform pem -in certificate.der
95
  SHA1 Fingerprint=15:9A:76....
96
97
  $ shasum -a 1 www.grc.com.bin
98
  159a76.....
99
*/
100
101
#define NDPI_MAX_TLS_REQUEST_SIZE 10000
102
29.3k
#define TLS_THRESHOLD             34387200 /* Threshold for certificate validity                                */
103
72.7k
#define TLS_LIMIT_DATE            1598918400 /* From 01/09/2020 TLS certificates lifespan is limited to 13 months */
104
105
106
static void ndpi_int_tls_add_connection(struct ndpi_detection_module_struct *ndpi_struct,
107
          struct ndpi_flow_struct *flow);
108
109
/* **************************************** */
110
111
1.83k
static bool str_contains_digit(char *str) {
112
1.83k
  u_int i = 0;
113
114
11.3k
  for(i=0; (str[i] != '.') && (str[i] != '\0'); i++) {
115
11.0k
    if(isdigit(str[i]))
116
1.52k
      return(true);
117
11.0k
  }
118
119
313
  return(false);
120
1.83k
}
121
122
/* **************************************** */
123
124
/* TODO: rename */
125
static int keep_extra_dissection_tcp(struct ndpi_detection_module_struct *ndpi_struct,
126
26.2k
                                     struct ndpi_flow_struct *flow) {
127
26.2k
  if(ndpi_struct->cfg.tls_blocks_analysis_enabled)
128
0
    return(1); /* Process as much TLS blocks as the max packet number */
129
  
130
  /* Common path: found handshake on both directions */
131
26.2k
  if(
132
26.2k
     (flow->tls_quic.certificate_processed == 1 && flow->protos.tls_quic.client_hello_processed)
133
134
     /* Application Data on both directions: handshake already ended (did we miss it?) */
135
22.7k
     || (flow->l4.tcp.tls.app_data_seen[0] == 1 && flow->l4.tcp.tls.app_data_seen[1] == 1)
136
137
     /* Handshake on one direction and Application Data on the other */
138
22.0k
     || ((flow->protos.tls_quic.client_hello_processed && flow->l4.tcp.tls.app_data_seen[!flow->protos.tls_quic.ch_direction] == 1) ||
139
21.1k
   (flow->protos.tls_quic.server_hello_processed && flow->l4.tcp.tls.app_data_seen[flow->protos.tls_quic.ch_direction] == 1))
140
26.2k
     ) {
141
5.46k
    return 0;
142
5.46k
  }
143
144
  /* Are we interested only in the (sub)-classification? */
145
146
20.8k
  if(/* Subclassification */
147
20.8k
     flow->detected_protocol_stack[1] != NDPI_PROTOCOL_UNKNOWN &&
148
     /* No metadata from SH or certificate */
149
7.29k
     !ndpi_struct->cfg.tls_alpn_negotiated_enabled &&
150
0
     !ndpi_struct->cfg.tls_cipher_enabled &&
151
0
     !ndpi_struct->cfg.tls_sha1_fingerprint_enabled &&
152
0
     !ndpi_struct->cfg.tls_cert_server_names_enabled &&
153
0
     !ndpi_struct->cfg.tls_cert_validity_enabled &&
154
0
     !ndpi_struct->cfg.tls_cert_issuer_enabled &&
155
0
     !ndpi_struct->cfg.tls_cert_subject_enabled &&
156
0
     !ndpi_struct->cfg.tls_broswer_enabled &&
157
0
     !ndpi_struct->cfg.tls_ja3s_fingerprint_enabled &&
158
     /* No flow risks from SH or certificate: we should have disabled all
159
        metadata needed for flow risks, so we should not need to explicitly
160
        check them */
161
     /* Ookla aggressiveness has no impact here because it is evaluated only
162
        without sub-classification */
163
     /* TLS heuristics */
164
0
     (ndpi_struct->cfg.tls_heuristics == 0 || is_flow_addr_informative(flow))) {
165
0
    return 0;
166
0
  }
167
168
20.8k
  return 1;
169
20.8k
}
170
171
/* **************************************** */
172
173
/* Heuristic to detect proxied/obfuscated TLS flows, based on
174
   https://www.usenix.org/conference/usenixsecurity24/presentation/xue-fingerprinting.
175
   Main differences between the paper and our implementation:
176
    * only Mahalanobis Distance, no Chi-squared Test
177
    * instead of 3-grams, we use 4-grams, always starting from the Client -> Server direction
178
    * consecutive packets in the same direction always belong to the same burst/flight
179
180
   Core idea:
181
    * the packets/bytes distribution of a TLS handshake is quite unique
182
    * this fingerprint is still detectable if the handshake is
183
      encrypted/proxied/obfuscated
184
*/
185
186
struct tls_obfuscated_heuristic_set {
187
  u_int8_t stage;
188
  u_int32_t bytes[4];
189
  u_int32_t pkts[4];
190
};
191
192
struct tls_obfuscated_heuristic_state {
193
  u_int8_t num_pkts;
194
195
  /* Burst/flight: consecutive packets in the same direction.
196
   * Set: packet/bytes distribution of 4 consecutive bursts (always starting from C->S),
197
          i.e. a 4-grams (using the paper terminology)
198
   * We have up tp 2 sets contemporarily active.
199
   * At the first pkt of a new C->S flight, we close the oldest set and open a new one */
200
  struct tls_obfuscated_heuristic_set sets[2];
201
};
202
203
static int check_set(struct ndpi_detection_module_struct* ndpi_struct,
204
                     struct tls_obfuscated_heuristic_set *set)
205
26.7k
{
206
#ifdef NDPI_ENABLE_DEBUG_MESSAGES
207
  struct ndpi_packet_struct* packet = &ndpi_struct->packet;
208
#endif
209
210
  /* Model: TLS 1.2; Firefox; No session resumption/0rtt */
211
26.7k
  const float i_s_tls_12[4 * 4] = { 0.000292421113167604, 4.43677617831228E-07, -5.69966093492813E-05, -2.18124698406311E-06,
212
26.7k
                                    4.43677617831228E-07, 5.98954952745268E-07, -3.59798436724817E-07, 5.71638172955893E-07,
213
26.7k
                                    -5.69966093492813E-05, -3.59798436724817E-07, 0.00076893788148309, 2.22278496185964E-05,
214
26.7k
                                    -2.18124698406311E-06, 5.71638172955893E-07, 2.22278496185964E-05, 5.72770077086287E-05 };
215
26.7k
  const float average_tls_12[4] = { 212.883690341977, 4514.71195039459, 107.770762871101, 307.580232995115 };
216
26.7k
  const float distance_tls_12 = 3.5;
217
218
  /* Model: TLS 1.3; Firefox; No session resumption/0rtt; no PQ; ECH(-grease) enabled */
219
26.7k
  const float i_s_tls_13[4 * 4] = { 3.08030337925007E-05, 1.16179172096944E-07, 1.05356744968627E-07, 3.8862884355278E-08,
220
26.7k
                                    1.16179172096944E-07, 6.93179117519316E-07, 2.77413220880937E-08, -3.63723200682445E-09,
221
26.7k
                                    1.05356744968627E-07, 2.77413220880937E-08, 1.0260950589675E-06, -1.08769813590053E-08,
222
26.7k
                                    3.88628843552779E-08, -3.63723200682445E-09, -1.08769813590053E-08, 8.63307792288604E-08 };
223
26.7k
  const float average_tls_13[4] = { 640.657378447541, 4649.30338356554, 448.408302530566, 1094.2013079329};
224
26.7k
  const float distance_tls_13 = 3.0;
225
226
  /* Model: TLS 1.2/1.3; Chrome; No session resumption/0rtt; PQ; ECH(-grease) enabled */
227
26.7k
  const float i_s_chrome[4 * 4] = { 6.72374390966642E-06, -2.32109583941723E-08, 6.67140014394388E-08, 1.2526322628285E-08,
228
26.7k
                                    -2.32109583941723E-08, 5.64668947932086E-07, 4.58963631972597E-08, 6.41254684791958E-09,
229
26.7k
                                    6.67140014394388E-08, 4.58963631972597E-08, 6.04057768431344E-07, -9.1507432597718E-10,
230
26.7k
                                    1.2526322628285E-08, 6.41254684791958E-09, -9.1507432597718E-10, 1.01184796635481E-07 };
231
26.7k
  const float average_chrome[4] = { 1850.43045387994, 4903.07735480722, 785.25280624695, 1051.22303562714 };
232
26.7k
  const float distance_chrome = 3.0;
233
234
  /* TODO: * ECH/PQ are still under development/deployment -> re-evaluate these models
235
           * Session resumptions/0rtt
236
     * Non-web traffic */
237
238
  /* This is the only logic about pkts distributions.
239
     ClientHello shoudn't be splitted in too many fragments: usually 1; 2 with PQ */
240
26.7k
  if(set->pkts[0] > 3) {
241
3.61k
    NDPI_LOG_DBG2(ndpi_struct, "TLS-Obf-Heur: too many pkts in the first burst %d\n", set->pkts[0]);
242
3.61k
    return 0;
243
3.61k
  }
244
245
23.1k
  if(ndpi_mahalanobis_distance(set->bytes, 4, average_chrome, i_s_chrome) < distance_chrome ||
246
     /* To avoid false positives: we didn't find TLS 1.3 CH smaller than 517 */
247
21.9k
     (set->bytes[0] >= 517 && ndpi_mahalanobis_distance(set->bytes, 4, average_tls_13, i_s_tls_13) < distance_tls_13) ||
248
21.4k
     ndpi_mahalanobis_distance(set->bytes, 4, average_tls_12, i_s_tls_12) < distance_tls_12) {
249
250
2.05k
    NDPI_LOG_DBG(ndpi_struct, "TLS-Obf-Heur: md %f %f %f [%d/%d/%d/%d %d/%d/%d/%d] TCP? %d\n",
251
2.05k
                 ndpi_mahalanobis_distance(set->bytes, 4, average_tls_12, i_s_tls_12),
252
2.05k
                 ndpi_mahalanobis_distance(set->bytes, 4, average_tls_13, i_s_tls_13),
253
2.05k
                 ndpi_mahalanobis_distance(set->bytes, 4, average_chrome, i_s_chrome),
254
2.05k
                 set->pkts[0], set->pkts[1], set->pkts[2], set->pkts[3],
255
2.05k
                 set->bytes[0], set->bytes[1], set->bytes[2], set->bytes[3],
256
2.05k
                 !!packet->tcp);
257
2.05k
    return 1;
258
2.05k
  }
259
260
21.0k
  return 0;
261
23.1k
}
262
263
/* **************************************** */
264
265
static int tls_obfuscated_heur_search(struct ndpi_detection_module_struct* ndpi_struct,
266
1.24M
                                      struct ndpi_flow_struct* flow) {
267
1.24M
  struct ndpi_packet_struct* packet = &ndpi_struct->packet;
268
1.24M
  struct tls_obfuscated_heuristic_state *state = flow->tls_quic.obfuscated_heur_state;
269
1.24M
  struct tls_obfuscated_heuristic_set *set;
270
1.24M
  int i, j;
271
1.24M
  int is_tls_in_tls_heur = 0;
272
1.24M
  int byte_overhead;
273
274
  /* Stages:
275
     0: Unused/Start
276
     1: C->S : burst 1
277
     2: S->C : burst 2
278
     3: C->S : burst 3
279
     4: S->C : burst 4
280
     5: C->S : End
281
  */
282
283
1.24M
  if(!state)
284
1
    return 1; /* Exclude */
285
286
1.24M
  if(packet->payload_packet_len == 0)
287
265
    return 0; /* Continue */
288
289
1.24M
  NDPI_LOG_DBG2(ndpi_struct, "TLS-Obf-Heur: num_pkts %d\n", state->num_pkts);
290
291
1.24M
  if(flow->extra_packets_func &&
292
2.39k
     (flow->detected_protocol_stack[0] == NDPI_PROTOCOL_TLS ||
293
2.39k
      flow->detected_protocol_stack[1] == NDPI_PROTOCOL_TLS)) /* TLS-in-TLS heuristic */
294
4
    is_tls_in_tls_heur = 1;
295
296
  /* We try to keep into account the overhead (header/padding/mac/iv/nonce) of the
297
     external layers (i.e. above the TLS hanshake we are trying to detect) */
298
1.24M
  if(is_tls_in_tls_heur == 1) {
299
    /* According to https://datatracker.ietf.org/doc/html/draft-mattsson-uta-tls-overhead-01
300
       the average packet overhead for TLS is 29 bytes.
301
       TODO: this draft is OLD and about TLS 1.2
302
       Looking at real traffic, we found that we can have TLS packets 24 bytes long
303
    */
304
4
    byte_overhead = 24;
305
1.24M
  } else {
306
    /* The paper says that the overhead is usually quite small
307
       ["typically ranging between 20 to 60 bytes"], without any citations.
308
       From the tests, it seams that we can ignore it */
309
1.24M
    byte_overhead = 0;
310
1.24M
  }
311
312
1.24M
  if(packet->payload_packet_len < byte_overhead) {
313
0
    NDPI_LOG_DBG2(ndpi_struct, "TLS-Obf-Heur: packet too small. Stop.\n");
314
0
    return 1; /* Exclude */
315
0
  }
316
317
1.24M
  if(is_tls_in_tls_heur == 1) {
318
    /* We usually stop processing TLS handshake (and switch to this extra dissection
319
       data path) after the FIRST Change-Cipher message. However, for this
320
       heuristic, we need to ignore all packets before a Change-Cipher is sent in the
321
       same direction */
322
4
    if(current_pkt_from_client_to_server(ndpi_struct, flow) &&
323
2
       flow->tls_quic.change_cipher_from_client == 0) {
324
2
      if(packet->payload[0] == 0x14) {
325
1
        NDPI_LOG_DBG2(ndpi_struct, "TLS-Obf-Heur: Change-Cipher from client\n");
326
1
        flow->tls_quic.change_cipher_from_client = 1;
327
1
      }
328
2
      NDPI_LOG_DBG2(ndpi_struct, "TLS-Obf-Heur: skip\n");
329
2
      return 0; /* Continue */
330
2
    }
331
2
    if(current_pkt_from_server_to_client(ndpi_struct, flow) &&
332
2
       flow->tls_quic.change_cipher_from_server == 0) {
333
2
      if(packet->payload[0] == 0x14) {
334
1
        flow->tls_quic.change_cipher_from_server = 1;
335
1
        NDPI_LOG_DBG2(ndpi_struct, "TLS-Obf-Heur: Change-Cipher from server\n");
336
1
      }
337
2
      NDPI_LOG_DBG2(ndpi_struct, "TLS-Obf-Heur: skip\n");
338
2
      return 0; /* Continue */
339
2
    }
340
2
  }
341
342
1.24M
  if(state->num_pkts++ > ndpi_struct->cfg.tls_heuristics_max_packets) {
343
1.28k
    NDPI_LOG_DBG2(ndpi_struct, "TLS-Obf-Heur: too many pkts. Stop\n");
344
1.28k
    return 1; /* Exclude */
345
1.28k
  }
346
347
  /* Update active sets */
348
3.63M
  for(i = 0; i < 2; i ++) {
349
2.43M
    set = &state->sets[i];
350
2.43M
    switch(set->stage) {
351
1.67M
    case 0:
352
      /* This happen only at the beginning of the heuristic: after the first pkt
353
         of the third (absolute) burst, we always have both sets used */
354
1.67M
      if(i == 0 || state->sets[0].stage == 3) {
355
661k
        if(current_pkt_from_client_to_server(ndpi_struct, flow)) {
356
617k
          NDPI_LOG_DBG2(ndpi_struct, "TLS-Obf-Heur: open set %d\n", i);
357
617k
          set->stage = 1;
358
617k
          break;
359
617k
  } else {
360
          /* First packet should be always from client.
361
       This shortcut makes detection harder if, before the obfuscated TLS hanshake
362
       there is random traffic */
363
44.2k
          return 1; /* Exclude */
364
44.2k
  }
365
661k
      }
366
1.01M
      continue;
367
1.01M
    case 1:
368
474k
      if(current_pkt_from_server_to_client(ndpi_struct, flow))
369
121k
        set->stage = 2;
370
474k
      break;
371
138k
    case 2:
372
138k
      if(current_pkt_from_client_to_server(ndpi_struct, flow))
373
59.8k
        set->stage = 3;
374
138k
      break;
375
88.5k
    case 3:
376
88.5k
      if(current_pkt_from_server_to_client(ndpi_struct, flow))
377
36.9k
        set->stage = 4;
378
88.5k
      break;
379
59.0k
    case 4:
380
59.0k
      if(current_pkt_from_client_to_server(ndpi_struct, flow))
381
26.7k
        set->stage = 5;
382
59.0k
      break;
383
    /* We cant have 5 here */
384
2.43M
    }
385
386
1.37M
    if(set->stage != 5) {
387
1.35M
      set->bytes[set->stage - 1] += (packet->payload_packet_len - byte_overhead);
388
1.35M
      set->pkts[set->stage - 1] += 1;
389
1.35M
    }
390
391
1.37M
    NDPI_LOG_DBG2(ndpi_struct, "TLS-Obf-Heur: set %d stage %d bytes %d/%d/%d/%d pkts %d/%d/%d/%d\n",
392
1.37M
                  i, set->stage,
393
1.37M
                  set->bytes[0], set->bytes[1], set->bytes[2], set->bytes[3],
394
1.37M
                  set->pkts[0], set->pkts[1], set->pkts[2], set->pkts[3]);
395
396
    /* Check completed set */
397
1.37M
    if(set->stage == 5) {
398
26.7k
      NDPI_LOG_DBG2(ndpi_struct, "TLS-Obf-Heur: set %d completed\n", i);
399
26.7k
      if(check_set(ndpi_struct, set)) {
400
        /* Heuristic match */
401
402
2.05k
        return 2; /* Found */
403
24.6k
      } else {
404
        /* Close this set and open a new one... */
405
24.6k
        set->stage = 1;
406
24.6k
        set->bytes[0] = packet->payload_packet_len - byte_overhead;
407
24.6k
        set->pkts[0] = 1;
408
98.6k
  for(j = 1; j < 4; j++) {
409
74.0k
          set->bytes[j] = 0;
410
74.0k
          set->pkts[j] = 0;
411
74.0k
        }
412
24.6k
        NDPI_LOG_DBG2(ndpi_struct, "TLS-Obf-Heur: set %d closed and reused\n", i);
413
24.6k
      }
414
26.7k
    }
415
1.37M
  }
416
417
1.19M
  return 0; /* Continue */
418
1.24M
}
419
420
/* **************************************** */
421
422
static int tls_obfuscated_heur_search_again(struct ndpi_detection_module_struct* ndpi_struct,
423
1.24M
              struct ndpi_flow_struct* flow) {
424
1.24M
  int rc;
425
426
1.24M
  NDPI_LOG_DBG2(ndpi_struct, "TLS-Obf-Heur: extra dissection\n");
427
428
1.24M
  rc = tls_obfuscated_heur_search(ndpi_struct, flow);
429
1.24M
  if(rc == 0)
430
1.19M
    return 1; /* Keep working */
431
47.5k
  if(rc == 2) {
432
2.05k
    NDPI_LOG_DBG(ndpi_struct, "TLS-Obf-Heur: found!\n");
433
434
    /* Right now, if an heuritic matches, we set the classification/risk.
435
       TODO: avoid false positives!
436
       Some ideas:
437
        * try to identify the servers: we wait for multiple sessions to the same server,
438
          before to start marking the flows to that address
439
        * consider the number of burst after TLS handshake (see Fig 8 paper) */
440
441
2.05k
    if(flow->detected_protocol_stack[0] == NDPI_PROTOCOL_UNKNOWN) {
442
2.04k
      ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_TLS, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI_AGGRESSIVE);
443
2.04k
      ndpi_set_risk(ndpi_struct, flow, NDPI_OBFUSCATED_TRAFFIC, "Obfuscated TLS traffic");
444
2.04k
    } else {
445
10
      flow->confidence = NDPI_CONFIDENCE_DPI_AGGRESSIVE; /* Update the value */
446
10
      if(flow->detected_protocol_stack[0] == NDPI_PROTOCOL_TLS ||
447
10
         flow->detected_protocol_stack[1] == NDPI_PROTOCOL_TLS)
448
0
        ndpi_set_risk(ndpi_struct, flow, NDPI_OBFUSCATED_TRAFFIC, "Obfuscated TLS-in-TLS traffic");
449
10
      else
450
10
        ndpi_set_risk(ndpi_struct, flow, NDPI_OBFUSCATED_TRAFFIC, "Obfuscated TLS-in-HTTP-WebSocket traffic");
451
10
    }
452
453
2.05k
    ndpi_master_app_protocol proto;
454
2.05k
    proto.master_protocol = ndpi_get_master_proto(ndpi_struct, flow);
455
2.05k
    proto.app_protocol = NDPI_PROTOCOL_UNKNOWN;
456
2.05k
    flow->category = get_proto_category(ndpi_struct, proto);
457
2.05k
    flow->breed = get_proto_breed(ndpi_struct, proto);
458
2.05k
  }
459
47.5k
  NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); /* Not necessary in extra-dissection data path,
460
                                                but we need it with the plain heuristic */
461
47.5k
  return 0; /* Stop */
462
1.24M
}
463
464
/* **************************************** */
465
466
void switch_extra_dissection_to_tls_obfuscated_heur(struct ndpi_detection_module_struct* ndpi_struct,
467
                                                    struct ndpi_flow_struct* flow)
468
248
{
469
248
  NDPI_LOG_DBG(ndpi_struct, "Switching to TLS Obfuscated heuristic\n");
470
471
248
  if(flow->tls_quic.obfuscated_heur_state == NULL)
472
219
    flow->tls_quic.obfuscated_heur_state = ndpi_calloc(1, sizeof(struct tls_obfuscated_heuristic_state));
473
29
  else /* If state has been already allocated (because of NDPI_HEURISTICS_TLS_OBFUSCATED_PLAIN) reset it */
474
29
    memset(flow->tls_quic.obfuscated_heur_state, '\0', sizeof(struct tls_obfuscated_heuristic_state));
475
476
  /* "* 2" to take into account ACKs. The "real" check is performend against
477
     "tls_heuristics_max_packets" in tls_obfuscated_heur_search, as expected */
478
248
  flow->max_extra_packets_to_check = ndpi_struct->cfg.tls_heuristics_max_packets * 2;
479
248
  flow->extra_packets_func = tls_obfuscated_heur_search_again;
480
248
}
481
482
/* **************************************** */
483
484
static int ndpi_search_tls_memory(const u_int8_t *payload,
485
          u_int16_t payload_len,
486
          u_int32_t seq,
487
2.66M
          message_t *message) {
488
2.66M
  u_int avail_bytes;
489
490
#ifdef DEBUG_TLS_MEMORY
491
  printf("[TLS Mem] Handling TLS flow [payload_len: %u][buffer_len: %u]\n",
492
   payload_len,
493
   message->buffer_len);
494
#endif
495
496
2.66M
  if(message->buffer == NULL) {
497
    /* Allocate buffer */
498
2.45M
    message->buffer_len = 2048, message->buffer_used = 0;
499
2.45M
    message->buffer = (u_int8_t*)ndpi_malloc(message->buffer_len);
500
501
2.45M
    if(message->buffer == NULL)
502
12.4k
      return -1;
503
504
#ifdef DEBUG_TLS_MEMORY
505
    printf("[TLS Mem] Allocating %u buffer\n", message->buffer_len);
506
#endif
507
2.45M
  }
508
509
2.65M
  avail_bytes = message->buffer_len - message->buffer_used;
510
511
2.65M
  if(avail_bytes < payload_len) {
512
79.7k
    u_int new_len = message->buffer_len + payload_len - avail_bytes + 1;
513
79.7k
    void *newbuf  = ndpi_realloc(message->buffer,
514
79.7k
         message->buffer_len, new_len);
515
79.7k
    if(!newbuf) return -1;
516
517
#ifdef DEBUG_TLS_MEMORY
518
    printf("[TLS Mem] Enlarging %u -> %u buffer\n", message->buffer_len, new_len);
519
#endif
520
521
78.7k
    message->buffer = (u_int8_t*)newbuf;
522
78.7k
    message->buffer_len = new_len;
523
78.7k
    avail_bytes = message->buffer_len - message->buffer_used;
524
78.7k
  }
525
526
2.65M
  if(payload_len > 0 && avail_bytes >= payload_len) {
527
2.65M
    u_int8_t ok = 0;
528
529
2.65M
    if(message->next_seq != 0) {
530
209k
      if(seq == message->next_seq)
531
134k
  ok = 1;
532
209k
    } else
533
2.44M
      ok = 1;
534
535
2.65M
    if(ok) {
536
2.57M
      memcpy(&message->buffer[message->buffer_used],
537
2.57M
       payload, payload_len);
538
539
2.57M
      message->buffer_used += payload_len;
540
#ifdef DEBUG_TLS_MEMORY
541
      printf("[TLS Mem] Copied data to buffer [%u/%u bytes][tcp_seq: %u][next: %u]\n",
542
       message->buffer_used, message->buffer_len,
543
       seq,
544
       seq + payload_len);
545
#endif
546
547
2.57M
      message->next_seq = seq + payload_len;
548
2.57M
    } else {
549
#ifdef DEBUG_TLS_MEMORY
550
      printf("[TLS Mem] Skipping packet [%u bytes][tcp_seq: %u][expected next: %u]\n",
551
       message->buffer_len,
552
       seq,
553
       message->next_seq);
554
#endif
555
75.6k
    }
556
2.65M
  }
557
2.65M
  return 0;
558
2.65M
}
559
560
/* **************************************** */
561
562
186k
static void cleanupServerName(char *buffer, u_int buffer_len) {
563
186k
  u_int i;
564
565
  /* Now all lowecase */
566
3.15M
  for(i=0; i<buffer_len; i++)
567
2.96M
    buffer[i] = tolower(buffer[i]);
568
186k
}
569
570
/* **************************************** */
571
572
/*
573
  Return code
574
  -1: error (buffer too short)
575
  0: OK but buffer is not human readeable (so something went wrong)
576
  1: OK
577
*/
578
static int extractRDNSequence(struct ndpi_packet_struct *packet,
579
            u_int offset, char *buffer, u_int buffer_len,
580
            char *rdnSeqBuf, u_int *rdnSeqBuf_offset,
581
            u_int rdnSeqBuf_len,
582
214k
            const char *label) {
583
214k
  u_int8_t str_len, is_printable = 1;
584
214k
  char *str;
585
214k
  u_int len;
586
587
214k
  if(*rdnSeqBuf_offset >= rdnSeqBuf_len) {
588
#ifdef DEBUG_TLS
589
    printf("[TLS] %s() [buffer capacity reached][%u]\n",
590
           __FUNCTION__, rdnSeqBuf_len);
591
#endif
592
14
    return -1;
593
14
  }
594
214k
  if((offset+4) >= packet->payload_packet_len)
595
117
    return(-1);
596
597
214k
  str_len = packet->payload[offset+4];
598
599
  // packet is truncated... further inspection is not needed
600
214k
  if((offset+4+str_len) >= packet->payload_packet_len)
601
1.52k
    return(-1);
602
603
212k
  str = (char*)&packet->payload[offset+5];
604
605
212k
  len = (u_int)ndpi_min(str_len, buffer_len-1);
606
212k
  strncpy(buffer, str, len);
607
212k
  buffer[len] = '\0';
608
609
  // check string is printable
610
212k
  is_printable = ndpi_normalize_printable_string(buffer, len);
611
612
212k
  if(is_printable) {
613
161k
    int rc = ndpi_snprintf(&rdnSeqBuf[*rdnSeqBuf_offset],
614
161k
         rdnSeqBuf_len-(*rdnSeqBuf_offset),
615
161k
         "%s%s=%s", (*rdnSeqBuf_offset > 0) ? ", " : "",
616
161k
         label, buffer);
617
618
161k
    if(rc > 0 && ((u_int)rc > rdnSeqBuf_len-(*rdnSeqBuf_offset)))
619
48
      return -1; /* Truncated; not enough buffer */
620
160k
    if(rc > 0)
621
160k
      (*rdnSeqBuf_offset) += rc;
622
160k
  }
623
624
212k
  return(is_printable);
625
212k
}
626
627
/* **************************************** */
628
629
static u_int64_t make_tls_cert_key(struct ndpi_packet_struct *packet, int is_from_client)
630
114k
{
631
114k
  u_int64_t key;
632
633
  /* Server ip/port */
634
114k
  if(packet->iphv6 == NULL) {
635
106k
    if(packet->tcp) {
636
75.5k
      if(is_from_client)
637
36.1k
        key = ((u_int64_t)packet->iph->daddr << 32) | packet->tcp->dest;
638
39.3k
      else
639
39.3k
        key = ((u_int64_t)packet->iph->saddr << 32) | packet->tcp->source;
640
75.5k
    } else {
641
31.2k
      if(is_from_client)
642
22.9k
        key = ((u_int64_t)packet->iph->daddr << 32) | packet->udp->dest;
643
8.34k
      else
644
8.34k
        key = ((u_int64_t)packet->iph->saddr << 32) | packet->udp->source;
645
31.2k
    }
646
106k
  } else {
647
7.93k
    if(packet->tcp) {
648
7.45k
      if(is_from_client)
649
3.13k
        key = (ndpi_quick_hash64((const char *)&packet->iphv6->ip6_dst, 16) << 16) | packet->tcp->dest;
650
4.32k
      else
651
4.32k
        key = (ndpi_quick_hash64((const char *)&packet->iphv6->ip6_src, 16) << 16) | packet->tcp->source;
652
7.45k
    } else {
653
479
      if(is_from_client)
654
357
        key = (ndpi_quick_hash64((const char *)&packet->iphv6->ip6_dst, 16) << 16) | packet->udp->dest;
655
122
      else
656
122
        key = (ndpi_quick_hash64((const char *)&packet->iphv6->ip6_src, 16) << 16) | packet->udp->source;
657
479
    }
658
7.93k
  }
659
660
114k
  return key;
661
114k
}
662
663
/* **************************************** */
664
665
static void checkTLSSubprotocol(struct ndpi_detection_module_struct *ndpi_struct,
666
        struct ndpi_flow_struct *flow,
667
168k
        int is_from_client) {
668
168k
  struct ndpi_packet_struct *packet = &ndpi_struct->packet;
669
670
168k
  if(ndpi_struct->cfg.tls_subclassification_enabled &&
671
167k
     flow->detected_protocol_stack[1] == NDPI_PROTOCOL_UNKNOWN) {
672
    /* Subprotocol not yet set */
673
674
114k
    if(ndpi_struct->tls_cert_cache) {
675
114k
      u_int16_t cached_proto;
676
114k
      u_int64_t key;
677
678
114k
      key = make_tls_cert_key(packet, is_from_client);
679
680
114k
      if(ndpi_lru_find_cache(ndpi_struct->tls_cert_cache, key,
681
114k
           &cached_proto, 0 /* Don't remove it as it can be used for other connections */,
682
114k
           ndpi_get_current_time(flow))) {
683
1.81k
        ndpi_master_app_protocol proto;
684
685
1.81k
  ndpi_set_detected_protocol(ndpi_struct, flow, cached_proto, ndpi_get_master_proto(ndpi_struct, flow), NDPI_CONFIDENCE_DPI_CACHE);
686
1.81k
  proto.master_protocol = ndpi_get_master_proto(ndpi_struct, flow);
687
1.81k
  proto.app_protocol = cached_proto;
688
1.81k
  flow->category = get_proto_category(ndpi_struct, proto);
689
1.81k
  flow->breed = get_proto_breed(ndpi_struct, proto);
690
1.81k
  ndpi_check_subprotocol_risk(ndpi_struct, flow, cached_proto);
691
1.81k
  ndpi_unset_risk(ndpi_struct, flow, NDPI_NUMERIC_IP_HOST);
692
1.81k
      }
693
114k
    }
694
114k
  }
695
168k
}
696
697
/* **************************************** */
698
699
/* See https://blog.catchpoint.com/2017/05/12/dissecting-tls-using-wireshark/ */
700
void processCertificateElements(struct ndpi_detection_module_struct *ndpi_struct,
701
        struct ndpi_flow_struct *flow,
702
22.2k
        u_int16_t p_offset, u_int16_t certificate_len) {
703
22.2k
  struct ndpi_packet_struct *packet = &ndpi_struct->packet;
704
22.2k
  u_int16_t num_found = 0;
705
22.2k
  int32_t i;
706
22.2k
  char buffer[64] = { '\0' }, rdnSeqBuf[2048];
707
22.2k
  u_int rdn_len = 0;
708
709
22.2k
  rdnSeqBuf[0] = '\0';
710
711
#ifdef DEBUG_TLS
712
  printf("[TLS] %s() [offset: %u][certificate_len: %u]\n", __FUNCTION__, p_offset, certificate_len);
713
#endif
714
715
  /* Check after handshake protocol header (5 bytes) and message header (4 bytes) */
716
19.7M
  for(i = p_offset; i < certificate_len - 2; i++) {
717
    /*
718
      See https://www.ibm.com/support/knowledgecenter/SSFKSJ_7.5.0/com.ibm.mq.sec.doc/q009860_.htm
719
      for X.509 certificate labels
720
    */
721
19.7M
    if((packet->payload[i] == 0x55) && (packet->payload[i+1] == 0x04) && (packet->payload[i+2] == 0x03)) {
722
      /* Common Name */
723
35.9k
      int rc = extractRDNSequence(packet, i, buffer, sizeof(buffer), rdnSeqBuf, &rdn_len, sizeof(rdnSeqBuf), "CN");
724
35.9k
      if(rc == -1) break;
725
726
#ifdef DEBUG_TLS
727
      printf("[TLS] %s() [%s][%s: %s]\n", __FUNCTION__, (num_found == 0) ? "Subject" : "Issuer", "Common Name", buffer);
728
#endif
729
19.7M
    } else if((packet->payload[i] == 0x55) && (packet->payload[i+1] == 0x04) && (packet->payload[i+2] == 0x06)) {
730
      /* Country */
731
37.2k
      int rc = extractRDNSequence(packet, i, buffer, sizeof(buffer), rdnSeqBuf, &rdn_len, sizeof(rdnSeqBuf), "C");
732
37.2k
      if(rc == -1) break;
733
734
#ifdef DEBUG_TLS
735
      printf("[TLS] %s() [%s][%s: %s]\n", __FUNCTION__, (num_found == 0) ? "Subject" : "Issuer", "Country", buffer);
736
#endif
737
19.6M
    } else if((packet->payload[i] == 0x55) && (packet->payload[i+1] == 0x04) && (packet->payload[i+2] == 0x07)) {
738
      /* Locality */
739
22.1k
      int rc = extractRDNSequence(packet, i, buffer, sizeof(buffer), rdnSeqBuf, &rdn_len, sizeof(rdnSeqBuf), "L");
740
22.1k
      if(rc == -1) break;
741
742
#ifdef DEBUG_TLS
743
      printf("[TLS] %s() [%s][%s: %s]\n", __FUNCTION__, (num_found == 0) ? "Subject" : "Issuer", "Locality", buffer);
744
#endif
745
19.6M
    } else if((packet->payload[i] == 0x55) && (packet->payload[i+1] == 0x04) && (packet->payload[i+2] == 0x08)) {
746
      /* State or Province */
747
61.4k
      int rc = extractRDNSequence(packet, i, buffer, sizeof(buffer), rdnSeqBuf, &rdn_len, sizeof(rdnSeqBuf), "ST");
748
61.4k
      if(rc == -1) break;
749
750
#ifdef DEBUG_TLS
751
      printf("[TLS] %s() [%s][%s: %s]\n", __FUNCTION__, (num_found == 0) ? "Subject" : "Issuer", "State or Province", buffer);
752
#endif
753
19.5M
    } else if((packet->payload[i] == 0x55) && (packet->payload[i+1] == 0x04) && (packet->payload[i+2] == 0x0a)) {
754
      /* Organization Name */
755
29.9k
      int rc = extractRDNSequence(packet, i, buffer, sizeof(buffer), rdnSeqBuf, &rdn_len, sizeof(rdnSeqBuf), "O");
756
29.9k
      if(rc == -1) break;
757
758
#ifdef DEBUG_TLS
759
      printf("[TLS] %s() [%s][%s: %s]\n", __FUNCTION__, (num_found == 0) ? "Subject" : "Issuer", "Organization Name", buffer);
760
#endif
761
762
19.5M
    } else if((packet->payload[i] == 0x55) && (packet->payload[i+1] == 0x04) && (packet->payload[i+2] == 0x0b)) {
763
      /* Organization Unit */
764
27.4k
      int rc = extractRDNSequence(packet, i, buffer, sizeof(buffer), rdnSeqBuf, &rdn_len, sizeof(rdnSeqBuf), "OU");
765
27.4k
      if(rc == -1) break;
766
767
#ifdef DEBUG_TLS
768
      printf("[TLS] %s() [%s][%s: %s]\n", __FUNCTION__, (num_found == 0) ? "Subject" : "Issuer", "Organization Unit", buffer);
769
#endif
770
19.5M
    } else if((packet->payload[i] == 0x30) && (packet->payload[i+1] == 0x1e) && (packet->payload[i+2] == 0x17)) {
771
      /* Certificate Validity */
772
77.4k
      u_int offset = i+4;
773
774
77.4k
      if(num_found == 0) {
775
14.1k
  num_found++;
776
777
#ifdef DEBUG_TLS
778
  printf("[TLS] %s() IssuerDN [%s]\n", __FUNCTION__, rdnSeqBuf);
779
#endif
780
781
14.1k
  if(rdn_len && (flow->protos.tls_quic.issuerDN == NULL) &&
782
11.8k
     ndpi_struct->cfg.tls_cert_issuer_enabled) {
783
11.8k
    flow->protos.tls_quic.issuerDN = ndpi_strdup(rdnSeqBuf);
784
11.8k
    if(ndpi_normalize_printable_string(rdnSeqBuf, rdn_len) == 0) {
785
2
      if(is_flowrisk_info_enabled(ndpi_struct, NDPI_INVALID_CHARACTERS)) {
786
2
        char str[64];
787
2
        snprintf(str, sizeof(str), "Invalid issuerDN %s", flow->protos.tls_quic.issuerDN);
788
2
        ndpi_set_risk(ndpi_struct, flow, NDPI_INVALID_CHARACTERS, str);
789
2
      } else {
790
0
        ndpi_set_risk(ndpi_struct, flow, NDPI_INVALID_CHARACTERS, NULL);
791
0
      }
792
2
    }
793
11.8k
  }
794
795
14.1k
  rdn_len = 0; /* Reset buffer */
796
14.1k
      }
797
798
77.4k
      if(i + 3 < certificate_len &&
799
77.2k
   (offset+packet->payload[i+3]) < packet->payload_packet_len &&
800
75.5k
   ndpi_struct->cfg.tls_cert_validity_enabled) {
801
75.5k
  char utcDate[32];
802
75.5k
        u_int8_t len = packet->payload[i+3];
803
804
#ifdef DEBUG_TLS
805
  u_int j;
806
807
  printf("[CERTIFICATE] notBefore [len: %u][", len);
808
  for(j=0; j<len; j++) printf("%c", packet->payload[i+4+j]);
809
  printf("]\n");
810
#endif
811
812
75.5k
  if(len < (sizeof(utcDate)-1)) {
813
31.5k
    struct tm utc;
814
31.5k
    utc.tm_isdst = -1; /* Not set by strptime */
815
816
31.5k
    strncpy(utcDate, (const char*)&packet->payload[i+4], len);
817
31.5k
    utcDate[len] = '\0';
818
819
    /* 141021000000Z */
820
31.5k
    if(strptime(utcDate, "%y%m%d%H%M%SZ", &utc) != NULL) {
821
15.6k
      flow->protos.tls_quic.notBefore = timegm(&utc);
822
#ifdef DEBUG_TLS
823
      printf("[CERTIFICATE] notBefore %u [%s]\n",
824
       flow->protos.tls_quic.notBefore, utcDate);
825
#endif
826
15.6k
    }
827
31.5k
  }
828
829
75.5k
  offset += len;
830
831
75.5k
  if((offset+1) < packet->payload_packet_len) {
832
75.4k
    len = packet->payload[offset+1];
833
834
75.4k
    offset += 2;
835
836
75.4k
    if((offset+len) < packet->payload_packet_len) {
837
72.7k
      u_int32_t time_sec = packet->current_time_ms / 1000;
838
#ifdef DEBUG_TLS
839
      u_int j;
840
841
      printf("[CERTIFICATE] notAfter [len: %u][", len);
842
      for(j=0; j<len; j++) printf("%c", packet->payload[offset+j]);
843
      printf("]\n");
844
#endif
845
846
72.7k
      if(len < (sizeof(utcDate)-1)) {
847
43.2k
        struct tm utc;
848
43.2k
        utc.tm_isdst = -1; /* Not set by strptime */
849
850
43.2k
        strncpy(utcDate, (const char*)&packet->payload[offset], len);
851
43.2k
        utcDate[len] = '\0';
852
853
        /* 141021000000Z */
854
43.2k
        if(strptime(utcDate, "%y%m%d%H%M%SZ", &utc) != NULL) {
855
15.7k
    flow->protos.tls_quic.notAfter = timegm(&utc);
856
#ifdef DEBUG_TLS
857
    printf("[CERTIFICATE] notAfter %u [%s]\n",
858
           flow->protos.tls_quic.notAfter, utcDate);
859
#endif
860
15.7k
        }
861
43.2k
      }
862
863
72.7k
      if(flow->protos.tls_quic.notBefore > TLS_LIMIT_DATE)
864
29.3k
        if((flow->protos.tls_quic.notAfter-flow->protos.tls_quic.notBefore) > TLS_THRESHOLD) {
865
16.6k
          if(is_flowrisk_info_enabled(ndpi_struct, NDPI_TLS_CERT_VALIDITY_TOO_LONG)) {
866
16.2k
            char str[64];
867
868
16.2k
      snprintf(str, sizeof(str), "TLS Cert lasts %u days",
869
16.2k
         (flow->protos.tls_quic.notAfter-flow->protos.tls_quic.notBefore) / 86400);
870
871
16.2k
      ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_CERT_VALIDITY_TOO_LONG, str); /* Certificate validity longer than 13 months */
872
16.2k
          } else {
873
475
            ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_CERT_VALIDITY_TOO_LONG, NULL);
874
475
          }
875
16.6k
        }
876
877
72.7k
      if((time_sec < flow->protos.tls_quic.notBefore) || (time_sec > flow->protos.tls_quic.notAfter)) {
878
47.8k
        if(is_flowrisk_info_enabled(ndpi_struct, NDPI_TLS_CERTIFICATE_EXPIRED)) {
879
46.5k
          char str[96], b[32], e[32];
880
46.5k
          struct tm result;
881
46.5k
          time_t theTime;
882
883
46.5k
          theTime = flow->protos.tls_quic.notBefore;
884
46.5k
          strftime(b, sizeof(b), "%d/%b/%Y %H:%M:%S", ndpi_gmtime_r(&theTime, &result));
885
886
46.5k
          theTime = flow->protos.tls_quic.notAfter;
887
46.5k
          strftime(e, sizeof(e), "%d/%b/%Y %H:%M:%S", ndpi_gmtime_r(&theTime, &result));
888
889
46.5k
          snprintf(str, sizeof(str), "%s - %s", b, e);
890
46.5k
          ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_CERTIFICATE_EXPIRED, str); /* Certificate expired */
891
46.5k
        } else {
892
1.29k
          ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_CERTIFICATE_EXPIRED, NULL);
893
1.29k
        }
894
47.8k
      } else if((time_sec > flow->protos.tls_quic.notBefore)
895
3.70k
          && (time_sec > (flow->protos.tls_quic.notAfter - (ndpi_struct->cfg.tls_certificate_expire_in_x_days * 86400)))) {
896
566
        if(is_flowrisk_info_enabled(ndpi_struct, NDPI_TLS_CERTIFICATE_ABOUT_TO_EXPIRE)) {
897
566
          char str[96], b[32], e[32];
898
566
          struct tm result;
899
566
          time_t theTime;
900
901
566
          theTime = flow->protos.tls_quic.notBefore;
902
566
          strftime(b, sizeof(b), "%d/%b/%Y %H:%M:%S", ndpi_gmtime_r(&theTime, &result));
903
904
566
          theTime = flow->protos.tls_quic.notAfter;
905
566
          strftime(e, sizeof(e), "%d/%b/%Y %H:%M:%S", ndpi_gmtime_r(&theTime, &result));
906
907
566
          snprintf(str, sizeof(str), "%s - %s", b, e);
908
566
          ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_CERTIFICATE_ABOUT_TO_EXPIRE, str); /* Certificate almost expired */
909
566
        } else {
910
0
          ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_CERTIFICATE_ABOUT_TO_EXPIRE, NULL);
911
0
        }
912
566
      }
913
72.7k
    }
914
75.4k
  }
915
75.5k
      }
916
19.4M
    } else if((packet->payload[i] == 0x55) && (packet->payload[i+1] == 0x1d) && (packet->payload[i+2] == 0x11)) {
917
      /* Organization OID: 2.5.29.17 (subjectAltName) */
918
57.0k
      u_int8_t matched_name = 0;
919
920
      /* If the client hello was not observed or the requested name was missing, there is no need to trigger an alert */
921
57.0k
      if(flow->host_server_name[0] == '\0')
922
9.94k
  matched_name = 1;
923
924
#ifdef DEBUG_TLS
925
      printf("******* [TLS] Found subjectAltName\n");
926
#endif
927
928
57.0k
      i += 3 /* skip the initial patten 55 1D 11 */;
929
930
      /* skip the first type, 0x04 == BIT STRING, and jump to it's length */
931
57.0k
      if(i < packet->payload_packet_len && packet->payload[i] == 0x04) i++; else i += 4; /* 4 bytes, with the last byte set to 04 */
932
933
57.0k
      if(i < packet->payload_packet_len) {
934
56.9k
  i += (packet->payload[i] & 0x80) ? (packet->payload[i] & 0x7F) : 0; /* skip BIT STRING length */
935
56.9k
  if(i < packet->payload_packet_len) {
936
56.7k
    i += 2; /* skip the second type, 0x30 == SEQUENCE, and jump to it's length */
937
56.7k
    if(i < packet->payload_packet_len) {
938
56.6k
      i += (packet->payload[i] & 0x80) ? (packet->payload[i] & 0x7F) : 0; /* skip SEQUENCE length */
939
56.6k
      i++;
940
941
243k
      while(i < packet->payload_packet_len) {
942
243k
        u_int8_t general_name_type = packet->payload[i];
943
944
243k
        if((general_name_type == 0x81)    /* rfc822Name */
945
230k
     || (general_name_type == 0x82) /* dNSName    */
946
117k
     || (general_name_type == 0x87) /* ipAddress  */
947
243k
     )
948
188k
    {
949
188k
      if((i < (packet->payload_packet_len - 1))
950
188k
         && ((i + packet->payload[i + 1] + 2) < packet->payload_packet_len)) {
951
188k
        u_int8_t len = packet->payload[i + 1];
952
188k
        char dNSName[256];
953
188k
        u_int16_t dNSName_len;
954
955
188k
        i += 2;
956
957
        /* The check "len > sizeof(dNSName) - 1" will be always false. If we add it,
958
           the compiler is smart enough to detect it and throws a warning */
959
188k
        if((len == 0 /* Looks something went wrong */)
960
186k
           || ((i+len) > packet->payload_packet_len))
961
1.45k
          break;
962
963
186k
        if(general_name_type == 0x87) {
964
62.9k
          if(len == 4 /* IPv4 */) {
965
56.4k
      ndpi_snprintf(dNSName, sizeof(dNSName), "%u.%u.%u.%u",
966
56.4k
              packet->payload[i] & 0xFF,
967
56.4k
              packet->payload[i+1] & 0xFF,
968
56.4k
              packet->payload[i+2] & 0xFF,
969
56.4k
              packet->payload[i+3] & 0xFF);
970
56.4k
          } else if(len == 16 /* IPv6 */) {
971
4.38k
      struct in6_addr addr = *(struct in6_addr *)&packet->payload[i];
972
4.38k
      inet_ntop(AF_INET6, &addr, dNSName, sizeof(dNSName));
973
4.38k
          } else {
974
      /* Is that possibile? Better safe than sorry */
975
2.07k
      dNSName[0] = '\0';
976
2.07k
          }
977
123k
        } else {
978
123k
          strncpy(dNSName, (const char*)&packet->payload[i], len);
979
123k
          dNSName[len] = '\0';
980
123k
        }
981
982
186k
        dNSName_len = strlen(dNSName);
983
186k
        cleanupServerName(dNSName, dNSName_len);
984
985
#if DEBUG_TLS
986
        printf("[TLS] dNSName %s [%s][len: %u][leftover: %d]\n", dNSName,
987
         flow->host_server_name, len,
988
         packet->payload_packet_len-i-len);
989
#endif
990
991
        /*
992
          We cannot use ndpi_is_valid_hostname() as we can have wildcards
993
          here that will create false positives
994
        */
995
186k
        if(ndpi_normalize_printable_string(dNSName, dNSName_len) == 0) {
996
21.0k
          ndpi_set_risk(ndpi_struct, flow, NDPI_INVALID_CHARACTERS, dNSName);
997
998
          /* This looks like an attack */
999
21.0k
          ndpi_set_risk(ndpi_struct, flow, NDPI_POSSIBLE_EXPLOIT, "Invalid dNSName name");
1000
21.0k
        }
1001
1002
186k
        if(matched_name == 0) {
1003
#if DEBUG_TLS
1004
          printf("[TLS] Trying to match '%s' with '%s'\n",
1005
           flow->host_server_name, dNSName);
1006
#endif
1007
1008
118k
          if(dNSName[0] == '*') {
1009
19.3k
      char * label = strstr(flow->host_server_name, &dNSName[1]);
1010
1011
19.3k
      if(label != NULL) {
1012
8.79k
        char * first_dot = strchr(flow->host_server_name, '.');
1013
1014
8.79k
        if((first_dot == NULL) || (first_dot <= label)) {
1015
8.23k
          matched_name = 1;
1016
8.23k
        }
1017
8.79k
      }
1018
99.0k
          } else if(strcmp(flow->host_server_name, dNSName) == 0) {
1019
5.77k
      matched_name = 1;
1020
5.77k
          }
1021
118k
        }
1022
1023
186k
        if(ndpi_struct->cfg.tls_cert_server_names_enabled) {
1024
186k
                      if(flow->protos.tls_quic.server_names == NULL) {
1025
10.2k
                        flow->protos.tls_quic.server_names = ndpi_strdup(dNSName);
1026
10.2k
                        flow->protos.tls_quic.server_names_len = strlen(dNSName);
1027
176k
                      } else if((u_int16_t)(flow->protos.tls_quic.server_names_len + dNSName_len + 1) > flow->protos.tls_quic.server_names_len) {
1028
176k
                        u_int16_t newstr_len = flow->protos.tls_quic.server_names_len + dNSName_len + 1;
1029
176k
                        char *newstr = (char*)ndpi_realloc(flow->protos.tls_quic.server_names,
1030
176k
                                                           flow->protos.tls_quic.server_names_len+1, newstr_len+1);
1031
1032
176k
                        if(newstr) {
1033
174k
                          flow->protos.tls_quic.server_names = newstr;
1034
174k
                          flow->protos.tls_quic.server_names[flow->protos.tls_quic.server_names_len] = ',';
1035
174k
                          strncpy(&flow->protos.tls_quic.server_names[flow->protos.tls_quic.server_names_len+1],
1036
174k
                                  dNSName, dNSName_len+1);
1037
174k
                          flow->protos.tls_quic.server_names[newstr_len] = '\0';
1038
174k
                          flow->protos.tls_quic.server_names_len = newstr_len;
1039
174k
                        }
1040
176k
                      }
1041
186k
        }
1042
1043
186k
        if(ndpi_struct->cfg.tls_subclassification_enabled &&
1044
186k
           !flow->protos.tls_quic.subprotocol_detected &&
1045
94.2k
           !flow->tls_quic.from_rdp) { /* No (other) sub-classification; we will have TLS.RDP anyway */
1046
94.2k
          if(ndpi_match_hostname_protocol(ndpi_struct, flow, ndpi_get_master_proto(ndpi_struct, flow), dNSName, dNSName_len)) {
1047
1.95k
      flow->protos.tls_quic.subprotocol_detected = 1;
1048
1.95k
            ndpi_unset_risk(ndpi_struct, flow, NDPI_NUMERIC_IP_HOST);
1049
1.95k
          }
1050
94.2k
        }
1051
1052
186k
        i += len;
1053
186k
      } else {
1054
901
        char buf[32];
1055
1056
901
        snprintf(buf, sizeof(buf), "Unknown extension %02X", general_name_type);
1057
#if DEBUG_TLS
1058
        printf("[TLS] Leftover %u bytes", packet->payload_packet_len - i);
1059
#endif
1060
901
        ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_SUSPICIOUS_EXTENSION, buf);
1061
901
        break;
1062
901
      }
1063
188k
    } else {
1064
54.1k
    break;
1065
54.1k
        }
1066
243k
      } /* while */
1067
1068
56.6k
      if(!matched_name) {
1069
32.9k
        if(is_flowrisk_info_enabled(ndpi_struct, NDPI_TLS_CERTIFICATE_MISMATCH)) {
1070
32.9k
          char str[128];
1071
1072
32.9k
          snprintf(str, sizeof(str), "%s vs %s", flow->host_server_name, flow->protos.tls_quic.server_names);
1073
32.9k
          ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_CERTIFICATE_MISMATCH, str); /* Certificate mismatch */
1074
32.9k
        } else {
1075
15
          ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_CERTIFICATE_MISMATCH, NULL); /* Certificate mismatch */
1076
15
        }
1077
32.9k
      }
1078
56.6k
    }
1079
56.7k
  }
1080
56.9k
      }
1081
57.0k
    }
1082
19.7M
  } /* for */
1083
1084
22.2k
  if(rdn_len && (flow->protos.tls_quic.subjectDN == NULL)) {
1085
13.3k
    if(ndpi_struct->cfg.tls_cert_subject_enabled)
1086
13.3k
      flow->protos.tls_quic.subjectDN = ndpi_strdup(rdnSeqBuf);
1087
1088
13.3k
    if(ndpi_struct->cfg.tls_subclassification_enabled &&
1089
13.3k
       flow->detected_protocol_stack[1] == NDPI_PROTOCOL_UNKNOWN) {
1090
      /* No idea what is happening behind the scenes: let's check the certificate */
1091
4.71k
      u_int32_t val;
1092
4.71k
      int rc = ndpi_match_string_value(ndpi_struct->tls_cert_subject_automa.ac_automa,
1093
4.71k
               rdnSeqBuf, strlen(rdnSeqBuf), &val);
1094
1095
4.71k
      if(rc == 0) {
1096
  /* Match found */
1097
437
  u_int16_t proto_id = (u_int16_t)val;
1098
437
  ndpi_master_app_protocol proto;
1099
1100
437
  ndpi_set_detected_protocol(ndpi_struct, flow, proto_id, ndpi_get_master_proto(ndpi_struct, flow), NDPI_CONFIDENCE_DPI);
1101
437
  proto.master_protocol = ndpi_get_master_proto(ndpi_struct, flow);
1102
437
  proto.app_protocol = proto_id;
1103
437
  flow->category = get_proto_category(ndpi_struct, proto);
1104
437
  flow->breed = get_proto_breed(ndpi_struct, proto);
1105
437
  ndpi_check_subprotocol_risk(ndpi_struct, flow, proto_id);
1106
437
  ndpi_unset_risk(ndpi_struct, flow, NDPI_NUMERIC_IP_HOST);
1107
1108
437
  if(ndpi_struct->tls_cert_cache) {
1109
437
    u_int64_t key = make_tls_cert_key(packet, 0 /* from the server */);
1110
1111
437
    ndpi_lru_add_to_cache(ndpi_struct->tls_cert_cache, key, proto_id, ndpi_get_current_time(flow));
1112
437
  }
1113
437
      }
1114
4.71k
    }
1115
13.3k
  }
1116
1117
22.2k
  if(flow->protos.tls_quic.subjectDN && flow->protos.tls_quic.issuerDN
1118
12.3k
     && (!strcmp(flow->protos.tls_quic.subjectDN, flow->protos.tls_quic.issuerDN))) {
1119
    /* Last resort: we check if this is a trusted issuerDN */
1120
1.73k
    if(ndpi_check_issuerdn_risk_exception(ndpi_struct, flow->protos.tls_quic.issuerDN))
1121
55
      return; /* This is a trusted DN */
1122
1123
1.68k
    if(!flow->protos.tls_quic.webrtc)
1124
1.39k
      ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_SELFSIGNED_CERTIFICATE, flow->protos.tls_quic.subjectDN);
1125
1.68k
  }
1126
1127
#if DEBUG_TLS
1128
  printf("[TLS] %s() SubjectDN [%s]\n", __FUNCTION__, rdnSeqBuf);
1129
#endif
1130
22.2k
}
1131
1132
/* **************************************** */
1133
1134
/* See https://blog.catchpoint.com/2017/05/12/dissecting-tls-using-wireshark/ */
1135
int processCertificate(struct ndpi_detection_module_struct *ndpi_struct,
1136
22.3k
           struct ndpi_flow_struct *flow) {
1137
22.3k
  struct ndpi_packet_struct *packet = &ndpi_struct->packet;
1138
22.3k
  int is_dtls = packet->udp || flow->stun.maybe_dtls; /* No certificate with QUIC */
1139
22.3k
  u_int32_t certificates_length, length = (packet->payload[1] << 16) + (packet->payload[2] << 8) + packet->payload[3];
1140
22.3k
  u_int32_t certificates_offset = 7 + (is_dtls ? 8 : 0);
1141
22.3k
  u_int8_t num_certificates_found = 0;
1142
1143
#ifdef DEBUG_TLS
1144
  printf("[TLS] %s() [payload_packet_len=%u][direction: %u][%02X %02X %02X %02X %02X %02X...]\n",
1145
   __FUNCTION__, packet->payload_packet_len,
1146
   packet->packet_direction,
1147
   packet->payload[0], packet->payload[1], packet->payload[2],
1148
   packet->payload[3], packet->payload[4], packet->payload[5]);
1149
#endif
1150
1151
22.3k
  if((packet->payload_packet_len != (length + 4 + (is_dtls ? 8 : 0))) || (packet->payload[1] != 0x0) ||
1152
22.2k
     certificates_offset >= packet->payload_packet_len) {
1153
588
    ndpi_set_risk(ndpi_struct, flow, NDPI_MALFORMED_PACKET, "Unvalid lenght");
1154
588
    return(-1); /* Invalid length */
1155
588
  }
1156
1157
21.7k
  certificates_length = (packet->payload[certificates_offset - 3] << 16) +
1158
21.7k
    (packet->payload[certificates_offset - 2] << 8) +
1159
21.7k
    packet->payload[certificates_offset - 1];
1160
1161
21.7k
  if((packet->payload[certificates_offset - 3] != 0x0) || ((certificates_length+3) != length)) {
1162
907
    ndpi_set_risk(ndpi_struct, flow, NDPI_MALFORMED_PACKET, "Invalid certificate offset");
1163
907
    return(-2); /* Invalid length */
1164
907
  }
1165
1166
  /* Now let's process each individual certificates */
1167
52.3k
  while(certificates_offset < certificates_length) {
1168
35.2k
    u_int32_t certificate_len = (packet->payload[certificates_offset] << 16) + (packet->payload[certificates_offset+1] << 8) + packet->payload[certificates_offset+2];
1169
1170
    /* Invalid lenght */
1171
35.2k
    if((certificate_len == 0)
1172
34.7k
       || (packet->payload[certificates_offset] != 0x0)
1173
31.9k
       || ((certificates_offset+certificate_len) > (4+certificates_length+(is_dtls ? 8 : 0)))) {
1174
#ifdef DEBUG_TLS
1175
      printf("[TLS] Invalid length [certificate_len: %u][certificates_offset: %u][%u vs %u]\n",
1176
       certificate_len, certificates_offset,
1177
       (certificates_offset+certificate_len),
1178
       certificates_length);
1179
#endif
1180
3.77k
      break;
1181
3.77k
    }
1182
1183
31.5k
    certificates_offset += 3;
1184
#ifdef DEBUG_TLS
1185
    printf("[TLS] Processing %u bytes certificate [%02X %02X %02X]\n",
1186
     certificate_len,
1187
     packet->payload[certificates_offset],
1188
     packet->payload[certificates_offset+1],
1189
     packet->payload[certificates_offset+2]);
1190
#endif
1191
1192
31.5k
    if(num_certificates_found++ == 0) /* Dissect only the first certificate that is the one we care */ {
1193
1194
#ifdef DEBUG_CERTIFICATE_HASH
1195
      {
1196
  u_int32_t i;
1197
1198
  for(i=0;i<certificate_len;i++)
1199
    printf("%02X ", packet->payload[certificates_offset+i]);
1200
1201
  printf("\n");
1202
      }
1203
#endif
1204
1205
      /* For SHA-1 we take into account only the first certificate and not all of them */
1206
20.1k
      if(ndpi_struct->cfg.tls_sha1_fingerprint_enabled) {
1207
20.1k
        SHA1_CTX srv_cert_fingerprint_ctx ;
1208
1209
20.1k
  SHA1Init(&srv_cert_fingerprint_ctx);
1210
20.1k
  SHA1Update(&srv_cert_fingerprint_ctx,
1211
20.1k
                   &packet->payload[certificates_offset],
1212
20.1k
                   certificate_len);
1213
1214
20.1k
        SHA1Final(flow->protos.tls_quic.sha1_certificate_fingerprint, &srv_cert_fingerprint_ctx);
1215
1216
20.1k
        flow->protos.tls_quic.fingerprint_set = 1;
1217
1218
20.1k
        uint8_t * sha1 = flow->protos.tls_quic.sha1_certificate_fingerprint;
1219
20.1k
        const size_t sha1_siz = sizeof(flow->protos.tls_quic.sha1_certificate_fingerprint);
1220
20.1k
        char sha1_str[20 /* sha1_siz */ * 2 + 1];
1221
20.1k
        static const char hexalnum[] = "0123456789ABCDEF";
1222
20.1k
        size_t i;
1223
422k
        for (i = 0; i < sha1_siz; ++i) {
1224
402k
          u_int8_t lower = (sha1[i] & 0x0F);
1225
402k
          u_int8_t upper = (sha1[i] & 0xF0) >> 4;
1226
402k
          sha1_str[i*2] = hexalnum[upper];
1227
402k
          sha1_str[i*2 + 1] = hexalnum[lower];
1228
402k
        }
1229
20.1k
        sha1_str[sha1_siz * 2] = '\0';
1230
1231
#ifdef DEBUG_TLS
1232
        printf("[TLS] SHA-1: %s\n", sha1_str);
1233
#endif
1234
1235
20.1k
        if(ndpi_struct->malicious_sha1_hashmap != NULL) {
1236
19.4k
          u_int16_t rc1 = ndpi_hash_find_entry(ndpi_struct->malicious_sha1_hashmap, sha1_str, sha1_siz * 2, NULL);
1237
1238
19.4k
          if(rc1 == 0)
1239
2
            ndpi_set_risk(ndpi_struct, flow, NDPI_MALICIOUS_SHA1_CERTIFICATE, sha1_str);
1240
19.4k
        }
1241
20.1k
      }
1242
1243
20.1k
      processCertificateElements(ndpi_struct, flow, certificates_offset, certificate_len);
1244
20.1k
    }
1245
1246
31.5k
    certificates_offset += certificate_len;
1247
31.5k
  }
1248
1249
20.8k
  if((ndpi_struct->num_tls_blocks_to_follow != 0)
1250
12.4k
     && (flow->l4.tcp.tls.num_processed_tls_blocks >= ndpi_struct->num_tls_blocks_to_follow)) {
1251
#ifdef DEBUG_TLS_BLOCKS
1252
    printf("*** [TLS Block] Enough blocks dissected\n");
1253
#endif
1254
1255
0
    flow->extra_packets_func = NULL; /* We're good now */
1256
0
  }
1257
1258
20.8k
  return(1);
1259
21.7k
}
1260
1261
/* **************************************** */
1262
1263
static int processTLSBlock(struct ndpi_detection_module_struct *ndpi_struct,
1264
253k
                           struct ndpi_flow_struct *flow) {
1265
253k
  struct ndpi_packet_struct *packet = &ndpi_struct->packet;
1266
253k
  int ret;
1267
253k
  int is_dtls = packet->udp || flow->stun.maybe_dtls;
1268
1269
#ifdef DEBUG_TLS
1270
  printf("[TLS] Processing block %u\n", packet->payload[0]);
1271
#endif
1272
1273
253k
  switch(packet->payload[0] /* block type */) {
1274
95.3k
  case 0x01: /* Client Hello */
1275
95.3k
    flow->protos.tls_quic.client_hello_processed = 1;
1276
95.3k
    flow->protos.tls_quic.ch_direction = packet->packet_direction;
1277
95.3k
    processClientServerHello(ndpi_struct, flow, 0);
1278
95.3k
    ndpi_int_tls_add_connection(ndpi_struct, flow);
1279
1280
#ifdef DEBUG_TLS
1281
    printf("*** TLS [version: %02X][Client Hello]\n",
1282
     flow->protos.tls_quic.ssl_version);
1283
#endif
1284
1285
95.3k
    checkTLSSubprotocol(ndpi_struct, flow, packet->payload[0] == 0x01);
1286
95.3k
    break;
1287
1288
72.6k
  case 0x02: /* Server Hello */
1289
72.6k
    flow->protos.tls_quic.server_hello_processed = 1;
1290
72.6k
    flow->protos.tls_quic.ch_direction = !packet->packet_direction;
1291
72.6k
    processClientServerHello(ndpi_struct, flow, 0);
1292
72.6k
    ndpi_int_tls_add_connection(ndpi_struct, flow);
1293
1294
#ifdef DEBUG_TLS
1295
    printf("*** TLS [version: %02X][Server Hello]\n",
1296
     flow->protos.tls_quic.ssl_version);
1297
#endif
1298
1299
72.6k
    if(!is_dtls && flow->protos.tls_quic.ssl_version >= 0x0304 /* TLS 1.3 */) {
1300
15.4k
      flow->tls_quic.certificate_processed = 1; /* No Certificate with TLS 1.3+ */
1301
15.4k
    }
1302
72.6k
    if(is_dtls && flow->protos.tls_quic.ssl_version == 0xFEFC /* DTLS 1.3 */) {
1303
120
      flow->tls_quic.certificate_processed = 1; /* No Certificate with DTLS 1.3+ */
1304
120
    }
1305
1306
72.6k
    checkTLSSubprotocol(ndpi_struct, flow, packet->payload[0] == 0x01);
1307
72.6k
    break;
1308
1309
25.4k
  case 0x0b: /* Certificate */
1310
    /* Important: populate the tls union fields only after
1311
     * ndpi_int_tls_add_connection has been called */
1312
25.4k
    if(flow->protos.tls_quic.client_hello_processed ||
1313
23.4k
       flow->protos.tls_quic.server_hello_processed) {
1314
      /* Only certificates from the server */
1315
23.4k
      if(flow->protos.tls_quic.ch_direction != packet->packet_direction) {
1316
22.3k
        ret = processCertificate(ndpi_struct, flow);
1317
22.3k
        if(ret != 1) {
1318
#ifdef DEBUG_TLS
1319
          printf("[TLS] Error processing certificate: %d\n", ret);
1320
#endif
1321
1.49k
        }
1322
22.3k
      } else {
1323
#ifdef DEBUG_TLS
1324
        printf("[TLS] Certificate from client. Ignoring it\n");
1325
#endif
1326
1.12k
      }
1327
23.4k
      flow->tls_quic.certificate_processed = 1;
1328
23.4k
    }
1329
25.4k
    break;
1330
1331
59.7k
  default:
1332
59.7k
    return(-1);
1333
253k
  }
1334
1335
193k
  return(0);
1336
253k
}
1337
1338
/* **************************************** */
1339
1340
static void ndpi_looks_like_tls(struct ndpi_detection_module_struct *ndpi_struct,
1341
260k
                                struct ndpi_flow_struct *flow) {
1342
260k
  if(flow->fast_callback_protocol_id == NDPI_PROTOCOL_UNKNOWN)
1343
109k
    flow->fast_callback_protocol_id = ndpi_get_master_proto(ndpi_struct, flow);
1344
260k
}
1345
1346
/* **************************************** */
1347
1348
int ndpi_search_tls_tcp(struct ndpi_detection_module_struct *ndpi_struct,
1349
2.84M
                        struct ndpi_flow_struct *flow) {
1350
2.84M
  struct ndpi_packet_struct *packet = &ndpi_struct->packet;
1351
2.84M
  u_int8_t something_went_wrong = 0;
1352
2.84M
  message_t *message;
1353
1354
2.84M
  if(packet->tcp == NULL)
1355
0
    return 0; /* Error -> stop (this doesn't seem to be TCP) */
1356
1357
#ifdef DEBUG_TLS_MEMORY
1358
  printf("[TLS Mem] ndpi_search_tls_tcp() Processing new packet [payload_packet_len: %u][Dir: %u]\n",
1359
   packet->payload_packet_len, packet->packet_direction);
1360
#endif
1361
1362
  /* This function is also called by "extra dissection" data path. Unfortunately,
1363
     generic "extra function" code doesn't honour protocol bitmask.
1364
     TODO: handle that in ndpi_main.c for all the protocols */
1365
2.84M
  if(packet->payload_packet_len == 0 ||
1366
2.69M
     packet->tcp_retransmission) {
1367
#ifdef DEBUG_TLS_MEMORY
1368
    printf("[TLS Mem] Ack or retransmission %d/%d. Skip\n",
1369
           packet->payload_packet_len, packet->tcp_retransmission);
1370
#endif
1371
182k
    return 1; /* Keep working */
1372
182k
  }
1373
1374
2.66M
  message = &flow->tls_quic.message[packet->packet_direction];
1375
2.66M
  if(ndpi_search_tls_memory(packet->payload,
1376
2.66M
          packet->payload_packet_len, ntohl(packet->tcp->seq),
1377
2.66M
          message) == -1)
1378
13.4k
    return 0; /* Error -> stop */
1379
1380
  /* Valid TLS Content Types:
1381
     https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-5 */
1382
2.64M
  if(!(message->buffer[0] >= 20 &&
1383
2.25M
       message->buffer[0] <= 26)) {
1384
2.25M
    something_went_wrong = 1;
1385
2.25M
  }
1386
1387
3.19M
  while(!something_went_wrong) {
1388
927k
    u_int32_t len;
1389
927k
    u_int16_t p_len;
1390
927k
    const u_int8_t *p;
1391
927k
    u_int8_t content_type;
1392
1393
927k
    if(message->buffer_used < 5)
1394
34.8k
      break;
1395
1396
892k
    len = (message->buffer[3] << 8) + message->buffer[4] + 5;
1397
1398
892k
    if(len > message->buffer_used) {
1399
#ifdef DEBUG_TLS_MEMORY
1400
      printf("[TLS Mem] Not enough TLS data [%u < %u][%02X %02X %02X %02X %02X]\n",
1401
       len, message->buffer_used,
1402
       message->buffer[0],
1403
       message->buffer[1],
1404
       message->buffer[2],
1405
       message->buffer[3],
1406
       message->buffer[4]);
1407
#endif
1408
120k
      break;
1409
120k
    }
1410
1411
#ifdef DEBUG_TLS_MEMORY
1412
    printf("[TLS Mem] Processing %u bytes message\n", len);
1413
#endif
1414
1415
771k
    content_type = message->buffer[0];
1416
1417
771k
    if(ndpi_struct->cfg.tls_blocks_analysis_enabled) {
1418
0
      if(flow->l4.tcp.tls.num_tls_blocks < NDPI_MAX_NUM_TLS_APPL_BLOCKS) {
1419
0
  int16_t blen = len-5;
1420
  
1421
  /* Use positive values for c->s and negative for s->c */
1422
0
  if(packet->packet_direction != 0) blen = -blen;
1423
  
1424
0
  flow->l4.tcp.tls.tls_blocks[flow->l4.tcp.tls.num_tls_blocks].len = blen;
1425
0
  flow->l4.tcp.tls.tls_blocks[flow->l4.tcp.tls.num_tls_blocks++].block_type = content_type;
1426
  
1427
#ifdef DEBUG_TLS_BLOCKS
1428
  printf("*** [TLS Block] [len: %u][num_tls_blocks: %u/%u]\n",
1429
         len-5, flow->l4.tcp.tls.num_tls_blocks, ndpi_struct->num_tls_blocks_to_follow);
1430
#endif
1431
0
      }
1432
0
    }
1433
1434
    /* Overwriting packet payload */
1435
771k
    p = packet->payload;
1436
771k
    p_len = packet->payload_packet_len; /* Backup */
1437
1438
771k
    if(content_type == 0x14 /* Change Cipher Spec */) {
1439
142k
      if(ndpi_struct->skip_tls_blocks_until_change_cipher) {
1440
  /*
1441
    Ignore Application Data up until change cipher
1442
    so in this case we reset the number of observed
1443
    TLS blocks
1444
  */
1445
136k
  flow->l4.tcp.tls.num_processed_tls_blocks = 0;
1446
136k
      }
1447
142k
      if(len == 6 &&
1448
136k
         message->buffer[1] == 0x03 && /* TLS >= 1.0 */
1449
136k
         ((message->buffer[3] << 8) + (message->buffer[4])) == 1) {
1450
#ifdef DEBUG_TLS
1451
        printf("[TLS] Change Cipher Spec\n");
1452
#endif
1453
136k
        if(current_pkt_from_client_to_server(ndpi_struct, flow))
1454
63.0k
          flow->tls_quic.change_cipher_from_client = 1;
1455
73.2k
        else
1456
73.2k
          flow->tls_quic.change_cipher_from_server = 1;
1457
1458
136k
        ndpi_int_tls_add_connection(ndpi_struct, flow);
1459
136k
        flow->l4.tcp.tls.app_data_seen[packet->packet_direction] = 1;
1460
        /* Further data is encrypted so we are not able to parse it without
1461
           errors and without setting `something_went_wrong` variable */
1462
1463
136k
  if(!ndpi_struct->cfg.tls_blocks_analysis_enabled) {
1464
    /*
1465
      In case of TLS blocks analysis we want to analize all the blocks
1466
      whereas in "standard" mode we can use this shortcut and break
1467
    */
1468
136k
    break;
1469
136k
  }
1470
136k
      }
1471
629k
    } else if(content_type == 0x15 /* Alert */) {
1472
      /* https://techcommunity.microsoft.com/t5/iis-support-blog/ssl-tls-alert-protocol-and-the-alert-codes/ba-p/377132 */
1473
#ifdef DEBUG_TLS
1474
      printf("[TLS] *** TLS ALERT ***\n");
1475
#endif
1476
1477
10.6k
      if(len >= 7) {
1478
8.82k
  u_int8_t alert_level = message->buffer[5];
1479
1480
8.82k
  if(alert_level == 2 /* Warning (1), Fatal (2) */)
1481
2.63k
    ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_FATAL_ALERT, "Found fatal TLS alert");
1482
8.82k
      }
1483
1484
10.6k
      u_int16_t const alert_len = ntohs(*(u_int16_t const *)&message->buffer[3]);
1485
10.6k
      if (message->buffer[1] == 0x03 &&
1486
2.82k
          message->buffer[2] <= 0x04 &&
1487
2.32k
          alert_len == (u_int32_t)message->buffer_used - 5)
1488
1.28k
  {
1489
1.28k
    ndpi_int_tls_add_connection(ndpi_struct, flow);
1490
1.28k
  }
1491
10.6k
    }
1492
1493
635k
    if((len > 9)
1494
216k
       && (content_type != 0x17 /* Application Data */)) {
1495
      /* Split the element in blocks */
1496
162k
      u_int32_t processed = 5;
1497
1498
365k
      while((processed+4) <= len) {
1499
220k
  const u_int8_t *block = (const u_int8_t *)&message->buffer[processed];
1500
220k
  u_int32_t block_len   = (block[1] << 16) + (block[2] << 8) + block[3];
1501
1502
220k
  if(/* (block_len == 0) || */ /* Note blocks can have zero lenght */
1503
220k
     (block_len > len) || ((block[1] != 0x0))) {
1504
16.5k
    something_went_wrong = 1;
1505
16.5k
    break;
1506
16.5k
  }
1507
1508
203k
  packet->payload = block;
1509
203k
  packet->payload_packet_len = ndpi_min(block_len+4, message->buffer_used);
1510
1511
203k
  if((processed+packet->payload_packet_len) > len) {
1512
609
    something_went_wrong = 1;
1513
609
    break;
1514
609
  }
1515
1516
203k
  processTLSBlock(ndpi_struct, flow);
1517
203k
  ndpi_looks_like_tls(ndpi_struct, flow);
1518
1519
203k
  processed += packet->payload_packet_len;
1520
203k
      }
1521
472k
    } else if(len > 5 /* Minimum block size */) {
1522
      /* Process element as a whole */
1523
66.8k
      if(content_type == 0x17 /* Application Data */) {
1524
57.2k
  u_int32_t block_len   = (message->buffer[3] << 8) + (message->buffer[4]);
1525
1526
  /* Let's do a quick check to make sure this really looks like TLS */
1527
57.2k
  if(block_len < 16384 /* Max TLS block size */)
1528
57.2k
    ndpi_looks_like_tls(ndpi_struct, flow);
1529
1530
57.2k
  if (message->buffer[1] == 0x03 &&
1531
56.1k
      message->buffer[2] <= 0x04 &&
1532
54.9k
      block_len == (u_int32_t)message->buffer_used - 5)
1533
29.4k
    {
1534
29.4k
      ndpi_int_tls_add_connection(ndpi_struct, flow);
1535
29.4k
    }
1536
1537
  /* If we have seen Application Data blocks in both directions, it means
1538
     we are after the handshake. Stop extra processing */
1539
57.2k
  flow->l4.tcp.tls.app_data_seen[packet->packet_direction] = 1;
1540
57.2k
  if(flow->l4.tcp.tls.app_data_seen[!packet->packet_direction] == 1)
1541
15.5k
    flow->tls_quic.certificate_processed = 1;
1542
57.2k
      }
1543
66.8k
    }
1544
1545
635k
    packet->payload = p;
1546
635k
    packet->payload_packet_len = p_len; /* Restore */
1547
635k
    message->buffer_used -= len;
1548
1549
635k
    if(message->buffer_used > 0)
1550
551k
      memmove(message->buffer, &message->buffer[len], message->buffer_used);
1551
84.1k
    else
1552
84.1k
      break;
1553
1554
#ifdef DEBUG_TLS_MEMORY
1555
    printf("[TLS Mem] Left memory buffer %u bytes\n", message->buffer_used);
1556
#endif
1557
635k
  }
1558
1559
#ifdef DEBUG_TLS_MEMORY
1560
  printf("[TLS] Eval if keep going [%p]\n", flow->extra_packets_func);
1561
#endif
1562
1563
2.64M
  if(something_went_wrong
1564
371k
     || ((ndpi_struct->num_tls_blocks_to_follow > 0)
1565
345k
   && (flow->l4.tcp.tls.num_processed_tls_blocks == ndpi_struct->num_tls_blocks_to_follow))
1566
371k
     || ((ndpi_struct->num_tls_blocks_to_follow == 0)
1567
26.2k
   && (!keep_extra_dissection_tcp(ndpi_struct, flow)))
1568
2.64M
     ) {
1569
#ifdef DEBUG_TLS_BLOCKS
1570
    printf("*** [TLS Block] No more blocks\n");
1571
#endif
1572
    /* An ookla flow? */
1573
2.28M
    if((ndpi_struct->cfg.ookla_aggressiveness & NDPI_AGGRESSIVENESS_OOKLA_TLS) && /* Feature enabled */
1574
2.28M
       (!something_went_wrong &&
1575
5.46k
        flow->tls_quic.certificate_processed == 1 &&
1576
4.19k
        flow->protos.tls_quic.client_hello_processed == 1 &&
1577
3.57k
        flow->protos.tls_quic.server_hello_processed == 1) && /* TLS handshake found without errors */
1578
3.55k
       flow->detected_protocol_stack[0] == NDPI_PROTOCOL_TLS && /* No IMAPS/FTPS/... */
1579
841
       flow->detected_protocol_stack[1] == NDPI_PROTOCOL_UNKNOWN && /* No sub-classification */
1580
2.28M
       ntohs(flow->s_port) == 8080 && /* Ookla port */
1581
3
       ookla_search_into_cache(ndpi_struct, flow)) {
1582
3
      NDPI_LOG_INFO(ndpi_struct, "found ookla (cache over TLS)\n");
1583
      /* Even if a LRU cache is involved, NDPI_CONFIDENCE_DPI_AGGRESSIVE seems more
1584
         suited than NDPI_CONFIDENCE_DPI_CACHE */
1585
3
      ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_OOKLA, NDPI_PROTOCOL_TLS, NDPI_CONFIDENCE_DPI_AGGRESSIVE);
1586
      /* TLS over port 8080 usually triggers that risk; clear it */
1587
3
      ndpi_unset_risk(ndpi_struct, flow, NDPI_KNOWN_PROTOCOL_ON_NON_STANDARD_PORT);
1588
3
      flow->extra_packets_func = NULL;
1589
3
      return(0); /* That's all */
1590
    /* Loook for TLS-in-TLS */
1591
2.28M
    } else if((ndpi_struct->cfg.tls_heuristics & NDPI_HEURISTICS_TLS_OBFUSCATED_TLS) && /* Feature enabled */
1592
2.23M
              (!something_went_wrong &&
1593
5
               flow->tls_quic.certificate_processed == 1 &&
1594
5
               flow->protos.tls_quic.client_hello_processed == 1 &&
1595
5
               flow->protos.tls_quic.server_hello_processed == 1) && /* TLS handshake found without errors */
1596
5
               flow->tls_quic.from_opportunistic_tls == 0 && /* No from plaintext Mails or FTP */
1597
5
              !is_flow_addr_informative(flow) /* The proxy server is likely hosted on some cloud providers */ ) {
1598
4
      switch_extra_dissection_to_tls_obfuscated_heur(ndpi_struct, flow);
1599
4
      return(1);
1600
2.28M
    } else {
1601
2.28M
      flow->extra_packets_func = NULL;
1602
2.28M
      return(0); /* That's all */
1603
2.28M
    }
1604
2.28M
  } else
1605
366k
    return(1);
1606
2.64M
}
1607
1608
/* **************************************** */
1609
1610
787k
int is_dtls(const u_int8_t *buf, u_int32_t buf_len, u_int32_t *block_len) {
1611
787k
  if(buf_len <= 13)
1612
3.54k
    return 0;
1613
1614
784k
  if((buf[0] != 0x16 && buf[0] != 0x14 && buf[0] != 0x17 && buf[0] != 0x15) || /* Handshake, change-cipher-spec, Application-Data, Alert */
1615
123k
     !((buf[1] == 0xfe && buf[2] == 0xff) || /* Versions */
1616
74.4k
       (buf[1] == 0xfe && buf[2] == 0xfd) ||
1617
21.7k
       (buf[1] == 0xfe && buf[2] == 0xfc) ||
1618
672k
       (buf[1] == 0x01 && buf[2] == 0x00))) {
1619
#ifdef DEBUG_TLS
1620
    printf("[TLS] DTLS invalid block 0x%x or old version 0x%x-0x%x-0x%x\n",
1621
           buf[0], buf[1], buf[2], buf[3]);
1622
#endif
1623
672k
    return 0;
1624
672k
  }
1625
111k
  *block_len = ntohs(*((u_int16_t*)&buf[11]));
1626
#ifdef DEBUG_TLS
1627
  printf("[TLS] DTLS block len: %d\n", *block_len);
1628
#endif
1629
111k
  if(*block_len == 0 || (*block_len + 12 >= buf_len)) { /* We might have multiple DTLS records */
1630
#ifdef DEBUG_TLS
1631
    printf("[TLS] DTLS invalid block len %d (buf_len %d)\n",
1632
           *block_len, buf_len);
1633
#endif
1634
10.6k
    return 0;
1635
10.6k
  }
1636
100k
  return 1;
1637
111k
}
1638
1639
/* **************************************** */
1640
1641
/* NOTE: this function supports both TCP and UDP */
1642
static int ndpi_search_dtls(struct ndpi_detection_module_struct *ndpi_struct,
1643
781k
             struct ndpi_flow_struct *flow) {
1644
781k
  struct ndpi_packet_struct *packet = &ndpi_struct->packet;
1645
781k
  u_int32_t handshake_len, handshake_frag_off, handshake_frag_len;
1646
781k
  u_int16_t p_len, processed;
1647
781k
  const u_int8_t *p;
1648
781k
  u_int8_t no_dtls = 0, change_cipher_found = 0;
1649
781k
  message_t *message = NULL;
1650
1651
#ifdef DEBUG_TLS
1652
  printf("[TLS] %s()\n", __FUNCTION__);
1653
#endif
1654
1655
  /* Overwriting packet payload */
1656
781k
  p = packet->payload, p_len = packet->payload_packet_len; /* Backup */
1657
1658
  /* Split the element in blocks */
1659
781k
  processed = 0;
1660
839k
  while(processed + 13 < p_len) {
1661
722k
    u_int32_t block_len;
1662
722k
    const u_int8_t *block = (const u_int8_t *)&p[processed];
1663
1664
722k
    if(!is_dtls(block, p_len, &block_len)) {
1665
658k
      no_dtls = 1;
1666
658k
      break;
1667
658k
    }
1668
1669
    /* We process only handshake msgs */
1670
63.6k
    if(block[0] == 0x16) {
1671
60.4k
      if(processed + block_len + 13 > p_len) {
1672
#ifdef DEBUG_TLS
1673
        printf("[TLS] DTLS invalid len %d %d %d\n", processed, block_len, p_len);
1674
#endif
1675
308
        no_dtls = 1;
1676
308
        break;
1677
308
      }
1678
      /* TODO: handle (certificate) fragments */
1679
60.1k
      if(block_len > 24) {
1680
58.1k
        handshake_len = (block[14] << 16) + (block[15] << 8) + block[16];
1681
58.1k
        handshake_frag_off = (block[19] << 16) + (block[20] << 8) + block[21];
1682
58.1k
        handshake_frag_len = (block[22] << 16) + (block[23] << 8) + block[24];
1683
58.1k
        message = &flow->tls_quic.message[packet->packet_direction];
1684
1685
1686
#ifdef DEBUG_TLS
1687
        printf("[TLS] DTLS frag off %d len %d\n", handshake_frag_off, handshake_frag_len);
1688
#endif
1689
1690
58.1k
  if((handshake_len + 12) == block_len) {
1691
49.3k
          packet->payload = &block[13];
1692
49.3k
          packet->payload_packet_len = block_len;
1693
49.3k
          processTLSBlock(ndpi_struct, flow);
1694
49.3k
  } else if(handshake_len + 12 > block_len) {
1695
8.15k
    int rc;
1696
1697
#ifdef DEBUG_TLS
1698
          printf("[TLS] DTLS fragment off %d len %d\n", handshake_frag_off, handshake_frag_len);
1699
#endif
1700
8.15k
          if(handshake_frag_len + 12 > block_len) {
1701
#ifdef DEBUG_TLS
1702
            printf("[TLS] DTLS fragment invalid len %d + 12 > %d\n", handshake_frag_len, block_len);
1703
#endif
1704
1.46k
            no_dtls = 1;
1705
1.46k
            break;
1706
1.46k
    }
1707
1708
6.68k
          if(handshake_frag_off == 0) {
1709
2.94k
            rc = ndpi_search_tls_memory(&block[13],
1710
2.94k
          handshake_frag_len + 12,
1711
2.94k
          handshake_frag_off, message);
1712
3.74k
    } else {
1713
3.74k
            rc = ndpi_search_tls_memory(&block[13 + 12],
1714
3.74k
          handshake_frag_len,
1715
3.74k
          handshake_frag_off + 12, message);
1716
3.74k
    }
1717
6.68k
    if(rc == -1) {
1718
33
            no_dtls = 1;
1719
33
            break;
1720
33
    }
1721
#ifdef DEBUG_TLS
1722
          printf("[TLS] DTLS reassembled len %d vs %d\n",
1723
                 message->buffer_used, handshake_len + 12);
1724
#endif
1725
1726
6.64k
          if(handshake_len + 12 == message->buffer_used) {
1727
608
            packet->payload = message->buffer;
1728
608
            packet->payload_packet_len = message->buffer_used;
1729
608
            processTLSBlock(ndpi_struct, flow);
1730
1731
608
            ndpi_free(message->buffer);
1732
608
            memset(message, '\0', sizeof(*message));
1733
608
            message = NULL;
1734
6.04k
          } else {
1735
            /* No break, next fragments might be in the same packet */
1736
6.04k
          }
1737
1738
6.64k
        } else {
1739
#ifdef DEBUG_TLS
1740
          printf("[TLS] DTLS invalid handshake_len %d, %d\n",
1741
                 handshake_len, block_len);
1742
#endif
1743
646
          no_dtls = 1;
1744
646
          break;
1745
646
        }
1746
58.1k
      }
1747
60.1k
    } else if(block[0] == 0x14) {
1748
      /* Change-cipher-spec: any subsequent block might be encrypted */
1749
#ifdef DEBUG_TLS
1750
      printf("[TLS] Change-cipher-spec\n");
1751
#endif
1752
762
      change_cipher_found = 1;
1753
762
      processed += block_len + 13;
1754
762
      flow->tls_quic.certificate_processed = 1; /* Fake, to avoid extra dissection */
1755
762
      break;
1756
2.42k
    } else if(block[0] == 0x15 /* Alert */) {
1757
#ifdef DEBUG_TLS
1758
      printf("[TLS] TLS Alert\n");
1759
#endif
1760
1761
612
      if(block_len == 2) {
1762
250
       u_int8_t alert_level = block[13];
1763
1764
250
       if(alert_level == 2 /* Warning (1), Fatal (2) */)
1765
135
         ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_FATAL_ALERT, "Found fatal TLS alert");
1766
250
      }
1767
1.81k
    } else {
1768
#ifdef DEBUG_TLS
1769
      printf("[TLS] Application Data\n");
1770
#endif
1771
1.81k
      processed += block_len + 13;
1772
      /* DTLS mid session: no need to further inspect the flow */
1773
1.81k
      ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_DTLS, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI);
1774
1775
1.81k
      ndpi_master_app_protocol proto;
1776
1.81k
      proto.master_protocol = ndpi_get_master_proto(ndpi_struct, flow);
1777
1.81k
      proto.app_protocol = NDPI_PROTOCOL_UNKNOWN;
1778
1.81k
      flow->category = get_proto_category(ndpi_struct, proto);
1779
1.81k
      flow->breed = get_proto_breed(ndpi_struct, proto);
1780
1781
1.81k
      flow->tls_quic.certificate_processed = 1; /* Fake, to avoid extra dissection */
1782
1.81k
      break;
1783
1.81k
    }
1784
1785
58.6k
    processed += block_len + 13;
1786
58.6k
  }
1787
781k
  if(processed != p_len && message == NULL /* No pending reassembler */) {
1788
#ifdef DEBUG_TLS
1789
    printf("[TLS] DTLS invalid processed len %d/%d (%d)\n", processed, p_len, change_cipher_found);
1790
#endif
1791
730k
    if(!change_cipher_found)
1792
730k
      no_dtls = 1;
1793
730k
  }
1794
1795
781k
  packet->payload = p;
1796
781k
  packet->payload_packet_len = p_len; /* Restore */
1797
1798
781k
  if(no_dtls || change_cipher_found || flow->tls_quic.certificate_processed) {
1799
749k
    return(0); /* That's all */
1800
749k
  } else {
1801
31.5k
    return(1); /* Keep working */
1802
31.5k
  }
1803
781k
}
1804
1805
/* **************************************** */
1806
1807
static void tlsInitExtraPacketProcessing(struct ndpi_detection_module_struct *ndpi_struct,
1808
103k
           struct ndpi_flow_struct *flow) {
1809
103k
  struct ndpi_packet_struct *packet = &ndpi_struct->packet;
1810
1811
  /* At most 12 packets should almost always be enough to find the server certificate if it's there.
1812
     Exception: DTLS traffic with fragments, retransmissions and STUN packets */
1813
103k
  flow->max_extra_packets_to_check = ((packet->udp != NULL) ? 20 : 12) + (ndpi_struct->num_tls_blocks_to_follow*4);
1814
103k
  flow->extra_packets_func = (packet->udp != NULL) ? ndpi_search_dtls : ndpi_search_tls_tcp;
1815
103k
}
1816
1817
/* **************************************** */
1818
1819
void switch_extra_dissection_to_tls(struct ndpi_detection_module_struct *ndpi_struct,
1820
            struct ndpi_flow_struct *flow)
1821
231
{
1822
#ifdef DEBUG_TLS
1823
  printf("Switching to TLS extra dissection\n");
1824
#endif
1825
1826
  /* Reset reassemblers */
1827
231
  if(flow->tls_quic.message[0].buffer)
1828
99
    ndpi_free(flow->tls_quic.message[0].buffer);
1829
231
  memset(&flow->tls_quic.message[0], '\0', sizeof(flow->tls_quic.message[0]));
1830
231
  if(flow->tls_quic.message[1].buffer)
1831
104
    ndpi_free(flow->tls_quic.message[1].buffer);
1832
231
  memset(&flow->tls_quic.message[1], '\0', sizeof(flow->tls_quic.message[1]));
1833
1834
231
  flow->tls_quic.from_opportunistic_tls = 1;
1835
1836
231
  tlsInitExtraPacketProcessing(ndpi_struct, flow);
1837
231
}
1838
1839
/* **************************************** */
1840
1841
void switch_to_tls(struct ndpi_detection_module_struct *ndpi_struct,
1842
       struct ndpi_flow_struct *flow, int first_time)
1843
20.3k
{
1844
#ifdef DEBUG_TLS
1845
  printf("Switching to TLS\n");
1846
#endif
1847
1848
20.3k
  if(first_time) {
1849
    /* Reset reassemblers */
1850
9.52k
    if(flow->tls_quic.message[0].buffer)
1851
1.24k
      ndpi_free(flow->tls_quic.message[0].buffer);
1852
9.52k
    memset(&flow->tls_quic.message[0], '\0', sizeof(flow->tls_quic.message[0]));
1853
9.52k
    if(flow->tls_quic.message[1].buffer)
1854
1.54k
      ndpi_free(flow->tls_quic.message[1].buffer);
1855
9.52k
    memset(&flow->tls_quic.message[1], '\0', sizeof(flow->tls_quic.message[1]));
1856
1857
    /* We will not check obfuscated heuristic (anymore) because we have been
1858
       called from the STUN code, but we need to clear the previous state
1859
       (if any) to trigger "standard" TLS/DTLS code */
1860
9.52k
    if(flow->tls_quic.obfuscated_heur_state) {
1861
1.21k
      ndpi_free(flow->tls_quic.obfuscated_heur_state);
1862
1.21k
      flow->tls_quic.obfuscated_heur_state = NULL;
1863
1.21k
    }
1864
9.52k
  }
1865
1866
20.3k
  ndpi_search_tls_wrapper(ndpi_struct, flow);
1867
20.3k
}
1868
1869
/* **************************************** */
1870
1871
static void tls_subclassify_by_alpn(struct ndpi_detection_module_struct *ndpi_struct,
1872
8.14k
            struct ndpi_flow_struct *flow) {
1873
  /* Right now we have only one rule so we can keep it trivial */
1874
1875
8.14k
  if(strlen(flow->protos.tls_quic.advertised_alpns) > NDPI_STATICSTRING_LEN("anydesk/") &&
1876
4.84k
     strncmp(flow->protos.tls_quic.advertised_alpns, "anydesk/", NDPI_STATICSTRING_LEN("anydesk/")) == 0) {
1877
#ifdef DEBUG_TLS
1878
    printf("Matching ANYDESK via alpn\n");
1879
#endif
1880
145
    ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_ANYDESK,
1881
145
             ndpi_get_master_proto(ndpi_struct, flow), NDPI_CONFIDENCE_DPI);
1882
145
    flow->protos.tls_quic.subprotocol_detected = 1;
1883
145
  }
1884
8.14k
}
1885
1886
/* **************************************** */
1887
1888
static void tlsCheckUncommonALPN(struct ndpi_detection_module_struct *ndpi_struct,
1889
         struct ndpi_flow_struct *flow,
1890
58.0k
         char *alpn_start) {
1891
58.0k
  char * comma_or_nul = alpn_start;
1892
1893
87.1k
  do {
1894
87.1k
    size_t alpn_len;
1895
1896
87.1k
    comma_or_nul = strchr(comma_or_nul, ',');
1897
1898
87.1k
    if(comma_or_nul == NULL)
1899
49.3k
      comma_or_nul = alpn_start + strlen(alpn_start);
1900
1901
87.1k
    alpn_len = comma_or_nul - alpn_start;
1902
1903
87.1k
    if(!is_a_common_alpn(ndpi_struct, alpn_start, alpn_len)) {
1904
14.6k
      if(is_flowrisk_info_enabled(ndpi_struct, NDPI_TLS_UNCOMMON_ALPN)) {
1905
14.3k
        char str[64];
1906
14.3k
        size_t str_len;
1907
1908
#ifdef DEBUG_TLS
1909
        printf("TLS uncommon ALPN found: %.*s\n", (int)alpn_len, alpn_start);
1910
#endif
1911
1912
14.3k
        str[0] = '\0';
1913
14.3k
        str_len = ndpi_min(alpn_len, sizeof(str));
1914
14.3k
        if(str_len > 0) {
1915
10.1k
          strncpy(str, alpn_start, str_len);
1916
10.1k
          str[str_len - 1] = '\0';
1917
10.1k
        }
1918
1919
14.3k
        ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_UNCOMMON_ALPN, str);
1920
14.3k
      } else {
1921
270
        ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_UNCOMMON_ALPN, NULL);
1922
270
      }
1923
14.6k
      break;
1924
14.6k
    }
1925
1926
72.5k
    alpn_start = comma_or_nul + 1;
1927
72.5k
  } while (*(comma_or_nul++) != '\0');
1928
58.0k
}
1929
1930
/* **************************************** */
1931
1932
static void ndpi_int_tls_add_connection(struct ndpi_detection_module_struct *ndpi_struct,
1933
335k
          struct ndpi_flow_struct *flow) {
1934
335k
  u_int32_t protocol;
1935
1936
#if DEBUG_TLS
1937
  printf("[TLS] %s()\n", __FUNCTION__);
1938
#endif
1939
1940
335k
  if(flow->detected_protocol_stack[0] == NDPI_PROTOCOL_RDP) {
1941
    /* RDP over TLS */
1942
242
    ndpi_set_detected_protocol(ndpi_struct, flow,
1943
242
             NDPI_PROTOCOL_RDP, NDPI_PROTOCOL_TLS, NDPI_CONFIDENCE_DPI);
1944
242
    return;
1945
242
  }
1946
1947
334k
  if((flow->detected_protocol_stack[0] != NDPI_PROTOCOL_UNKNOWN) ||
1948
255k
     (flow->detected_protocol_stack[1] != NDPI_PROTOCOL_UNKNOWN)) {
1949
255k
    if(!flow->extra_packets_func)
1950
26.6k
      tlsInitExtraPacketProcessing(ndpi_struct, flow);
1951
1952
255k
    return;
1953
255k
  }
1954
1955
79.3k
  protocol = ndpi_get_master_proto(ndpi_struct, flow);
1956
1957
79.3k
  ndpi_set_detected_protocol(ndpi_struct, flow, protocol, protocol, NDPI_CONFIDENCE_DPI);
1958
  /* We don't want to ovewrite STUN extra dissection, if enabled */
1959
79.3k
  if(!flow->extra_packets_func)
1960
76.9k
    tlsInitExtraPacketProcessing(ndpi_struct, flow);
1961
79.3k
}
1962
1963
/* **************************************** */
1964
1965
static void checkExtensions(struct ndpi_detection_module_struct *ndpi_struct,
1966
          struct ndpi_flow_struct * const flow, int is_dtls,
1967
                            u_int16_t extension_id, u_int16_t extension_len,
1968
1.15M
          u_int16_t extension_payload_offset) {
1969
1.15M
  struct ndpi_packet_struct const * const packet = &ndpi_struct->packet;
1970
1971
1.15M
  if((extension_payload_offset + extension_len) > packet->payload_packet_len) {
1972
#ifdef DEBUG_TLS
1973
      printf("[TLS] extension length exceeds remaining packet length: %u > %u.\n",
1974
       extension_len, packet->payload_packet_len - extension_payload_offset);
1975
#endif
1976
32.0k
      ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_SUSPICIOUS_EXTENSION, "Invalid extension len");
1977
32.0k
      return;
1978
32.0k
    }
1979
1980
  /* see: https://www.wireshark.org/docs/wsar_html/packet-tls-utils_8h_source.html */
1981
1.11M
  static u_int16_t const allowed_non_iana_extensions[] = {
1982
1.11M
      /* 65486 ESNI is suspicious nowadays */ 13172 /* NPN - Next Proto Neg */,
1983
1.11M
      30032 /* Channel ID */, 65445 /* QUIC transport params */,
1984
      /* GREASE extensions */
1985
1.11M
      2570, 6682, 10794, 14906, 19018, 23130, 27242,
1986
1.11M
      31354, 35466, 39578, 43690, 47802, 51914, 56026,
1987
1.11M
      60138, 64250,
1988
      /* Groups */
1989
1.11M
      1035, 10794, 16696, 23130, 31354, 35466, 51914,
1990
      /* Ciphers */
1991
1.11M
      102, 129, 52243, 52244, 57363, 65279, 65413,
1992
      /* ALPS */
1993
1.11M
      17513, 17613
1994
1.11M
  };
1995
1.11M
  size_t const allowed_non_iana_extensions_size = sizeof(allowed_non_iana_extensions) /
1996
1.11M
    sizeof(allowed_non_iana_extensions[0]);
1997
1998
  /* see: https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml */
1999
  /* 65281 renegotiation_info, 65037 ECH */
2000
1.11M
  if(extension_id > 59 && extension_id != 65281 && extension_id != 65037)
2001
171k
    {
2002
171k
      u_int8_t extension_found = 0;
2003
171k
      size_t i;
2004
2005
4.62M
      for (i = 0; i < allowed_non_iana_extensions_size; ++i) {
2006
4.52M
  if(allowed_non_iana_extensions[i] == extension_id) {
2007
72.0k
    extension_found = 1;
2008
72.0k
    break;
2009
72.0k
  }
2010
4.52M
      }
2011
2012
171k
      if(extension_found == 0) {
2013
#ifdef DEBUG_TLS
2014
        printf("[TLS] suspicious extension id: %u\n", extension_id);
2015
#endif
2016
2017
99.0k
        if(is_flowrisk_info_enabled(ndpi_struct, NDPI_TLS_SUSPICIOUS_EXTENSION)) {
2018
98.2k
    char str[64];
2019
2020
98.2k
    snprintf(str, sizeof(str), "Extn id %u", extension_id);
2021
98.2k
    ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_SUSPICIOUS_EXTENSION, str);
2022
98.2k
        } else {
2023
780
          ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_SUSPICIOUS_EXTENSION, NULL);
2024
780
        }
2025
99.0k
  return;
2026
99.0k
      }
2027
171k
    }
2028
2029
  /* Check for DTLS-only extensions. */
2030
1.02M
  if(is_dtls == 0)
2031
860k
    {
2032
860k
      if(extension_id == 53 || extension_id == 54)
2033
1.16k
  {
2034
#ifdef DEBUG_TLS
2035
          printf("[TLS] suspicious DTLS-only extension id: %u\n", extension_id);
2036
#endif
2037
2038
1.16k
          if(is_flowrisk_info_enabled(ndpi_struct, NDPI_TLS_SUSPICIOUS_EXTENSION)) {
2039
1.13k
      char str[64];
2040
2041
1.13k
      snprintf(str, sizeof(str), "Extn id %u", extension_id);
2042
1.13k
      ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_SUSPICIOUS_EXTENSION, str);
2043
1.13k
          } else {
2044
23
            ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_SUSPICIOUS_EXTENSION, NULL);
2045
23
          }
2046
1.16k
    return;
2047
1.16k
  }
2048
860k
    }
2049
1.02M
}
2050
2051
/* **************************************** */
2052
2053
5.82M
static int u_int16_t_cmpfunc(const void * a, const void * b) { return(*(u_int16_t*)a - *(u_int16_t*)b); }
2054
2055
157k
static bool is_grease_version(u_int16_t version) {
2056
157k
  switch(version) {
2057
4.17k
  case 0x0a0a:
2058
9.09k
  case 0x1a1a:
2059
15.1k
  case 0x2a2a:
2060
17.1k
  case 0x3a3a:
2061
18.3k
  case 0x4a4a:
2062
19.5k
  case 0x5a5a:
2063
21.1k
  case 0x6a6a:
2064
22.6k
  case 0x7a7a:
2065
24.0k
  case 0x8a8a:
2066
26.0k
  case 0x9a9a:
2067
27.3k
  case 0xaaaa:
2068
28.9k
  case 0xbaba:
2069
30.2k
  case 0xcaca:
2070
31.7k
  case 0xdada:
2071
38.8k
  case 0xeaea:
2072
43.2k
  case 0xfafa:
2073
43.2k
    return(true);
2074
2075
113k
  default:
2076
113k
    return(false);
2077
157k
  }
2078
157k
}
2079
2080
/* **************************************** */
2081
2082
static void ndpi_compute_ja4(struct ndpi_detection_module_struct *ndpi_struct,
2083
           struct ndpi_flow_struct *flow,
2084
           u_int32_t quic_version,
2085
88.4k
           union ja_info *ja) {
2086
88.4k
  u_int8_t tmp_str[JA_STR_LEN];
2087
88.4k
  u_int tmp_str_len, num_extn;
2088
88.4k
  u_int8_t sha_hash[NDPI_SHA256_BLOCK_SIZE];
2089
88.4k
  u_int16_t ja_str_len, i;
2090
88.4k
  int rc;
2091
88.4k
  u_int16_t tls_handshake_version = ja->client.tls_handshake_version;
2092
88.4k
  char * const ja_str = &flow->protos.tls_quic.ja4_client[0];
2093
88.4k
  const u_int16_t ja_max_len = sizeof(flow->protos.tls_quic.ja4_client);
2094
88.4k
  bool is_dtls = ((flow->l4_proto == IPPROTO_UDP) && (quic_version == 0)) || flow->stun.maybe_dtls;
2095
88.4k
  int ja4_r_len = 0;
2096
88.4k
  char ja4_r[1024];
2097
2098
  /*
2099
    Compute JA4 TLS/QUIC client
2100
2101
    https://github.com/FoxIO-LLC/ja4/blob/main/technical_details/JA4.md
2102
2103
    (QUIC=”q”, DTLS="d" or TCP=”t”)
2104
    (2 character TLS version)
2105
    (SNI=”d” or no SNI=”i”)
2106
    (2 character count of ciphers)
2107
    (2 character count of extensions)
2108
    (first and last characters of first ALPN extension value)
2109
    _
2110
    (sha256 hash of the list of cipher hex codes sorted in hex order, truncated to 12 characters)
2111
    _
2112
    (sha256 hash of (the list of extension hex codes sorted in hex order)
2113
    _
2114
    (the list of signature algorithms), truncated to 12 characters)
2115
  */
2116
88.4k
  ja_str[0] = is_dtls ? 'd' : ((quic_version != 0) ? 'q' : 't');
2117
2118
245k
  for(i=0; i<ja->client.num_supported_versions; i++) {
2119
157k
    if((!is_grease_version(ja->client.supported_versions[i]))
2120
113k
       && (tls_handshake_version < ja->client.supported_versions[i]))
2121
40.6k
      tls_handshake_version = ja->client.supported_versions[i];
2122
157k
  }
2123
2124
88.4k
  switch(tls_handshake_version) {
2125
34.0k
  case 0x0304: /* TLS 1.3 = “13” */
2126
34.0k
    ja_str[1] = '1';
2127
34.0k
    ja_str[2] = '3';
2128
34.0k
    break;
2129
2130
16.6k
  case 0x0303: /* TLS 1.2 = “12” */
2131
16.6k
    ja_str[1] = '1';
2132
16.6k
    ja_str[2] = '2';
2133
16.6k
    break;
2134
2135
821
  case 0x0302: /* TLS 1.1 = “11” */
2136
821
    ja_str[1] = '1';
2137
821
    ja_str[2] = '1';
2138
821
    break;
2139
2140
5.44k
  case 0x0301: /* TLS 1.0 = “10” */
2141
5.44k
    ja_str[1] = '1';
2142
5.44k
    ja_str[2] = '0';
2143
5.44k
    break;
2144
2145
519
  case 0x0300: /* SSL 3.0 = “s3” */
2146
519
    ja_str[1] = 's';
2147
519
    ja_str[2] = '3';
2148
519
    break;
2149
2150
515
  case 0x0002: /* SSL 2.0 = “s2” */
2151
515
    ja_str[1] = 's';
2152
515
    ja_str[2] = '2';
2153
515
    break;
2154
2155
1.21k
  case 0xFEFF: /* DTLS 1.0 = “d1” */
2156
1.21k
    ja_str[1] = 'd';
2157
1.21k
    ja_str[2] = '1';
2158
1.21k
    break;
2159
2160
12.7k
  case 0xFEFD: /* DTLS 1.2 = “d2” */
2161
12.7k
    ja_str[1] = 'd';
2162
12.7k
    ja_str[2] = '2';
2163
12.7k
    break;
2164
2165
685
  case 0xFEFC: /* DTLS 1.3 = “d3” */
2166
685
    ja_str[1] = 'd';
2167
685
    ja_str[2] = '3';
2168
685
    break;
2169
2170
15.7k
  default:
2171
15.7k
    ja_str[1] = '0';
2172
15.7k
    ja_str[2] = '0';
2173
15.7k
    break;
2174
88.4k
  }
2175
2176
  /* Check if SNI extension exists at all */
2177
88.4k
  if(flow->host_server_name[0] == '\0') {
2178
32.1k
    ja_str[3] = 'i';  /* No SNI extension */
2179
56.2k
  } else if(ndpi_isset_risk(flow, NDPI_NUMERIC_IP_HOST)) {
2180
493
    ja_str[3] = 'i';  /* SNI contains IP address */
2181
55.8k
  } else {
2182
55.8k
    ja_str[3] = 'd';  /* SNI contains domain name */
2183
55.8k
  }
2184
88.4k
  ja_str_len = 4;
2185
2186
  /* JA4_a */
2187
  /* first + last character of the ALPN string (or '0' if missing) */
2188
88.4k
  char alpn_first = (ja->client.alpn[0] != '\0') ? ja->client.alpn[0] : '0';
2189
88.4k
  char alpn_last  = ja->client.alpn_original_last;  /* Use original last character before null terminator */
2190
2191
#ifdef DEBUG_JA
2192
  size_t alpn_len = strlen(ja->client.alpn);
2193
  printf("[JA4 DEBUG] ALPN string: '%s' (len=%zu)\n", ja->client.alpn, alpn_len);
2194
  printf("[JA4 DEBUG] First='%c', Last='%c'\n", alpn_first, alpn_last);
2195
#endif
2196
2197
88.4k
  rc = ndpi_snprintf(&ja_str[ja_str_len], ja_max_len - ja_str_len, "%02u%02u%c%c_",
2198
88.4k
         ja->client.num_ciphers, ja->client.num_tls_extensions,
2199
88.4k
         alpn_first, alpn_last);
2200
88.4k
  if((rc > 0) && (ja_str_len + rc < JA_STR_LEN)) ja_str_len += rc;
2201
2202
  /* Sort ciphers and extensions */
2203
88.4k
  qsort(&ja->client.cipher, ja->client.num_ciphers, sizeof(u_int16_t), u_int16_t_cmpfunc);
2204
88.4k
  qsort(&ja->client.tls_extension, ja->client.num_tls_extensions, sizeof(u_int16_t), u_int16_t_cmpfunc);
2205
2206
88.4k
  tmp_str_len = 0;
2207
1.32M
  for(i=0; i<ja->client.num_ciphers; i++) {
2208
#ifdef JA4R_DECIMAL
2209
    rc = snprintf(&ja4_r[ja4_r_len], sizeof(ja4_r)-ja4_r_len, "%s%u", (i > 0) ? "," : "", ja->client.cipher[i]);
2210
    if(rc > 0) ja4_r_len += rc;
2211
#endif
2212
1.24M
    rc = ndpi_snprintf((char *)&tmp_str[tmp_str_len], JA_STR_LEN-tmp_str_len, "%s%04x",
2213
1.24M
           (i > 0) ? "," : "", ja->client.cipher[i]);
2214
1.24M
    if((rc > 0) && (tmp_str_len + rc < JA_STR_LEN)) tmp_str_len += rc; else break;
2215
1.24M
  }
2216
2217
88.4k
#ifndef JA4R_DECIMAL
2218
88.4k
  ja_str[ja_str_len] = 0;
2219
88.4k
  i = snprintf(&ja4_r[ja4_r_len], sizeof(ja4_r)-ja4_r_len, "%s", ja_str); if(i > 0) ja4_r_len += i;
2220
2221
88.4k
  tmp_str[tmp_str_len] = 0;
2222
88.4k
  i = snprintf(&ja4_r[ja4_r_len], sizeof(ja4_r)-ja4_r_len, "%s_", tmp_str); if(i > 0) ja4_r_len += i;
2223
88.4k
#endif
2224
2225
88.4k
  ndpi_sha256(tmp_str, tmp_str_len, sha_hash);
2226
2227
88.4k
  rc = ndpi_snprintf(&ja_str[ja_str_len], ja_max_len - ja_str_len,
2228
88.4k
         "%02x%02x%02x%02x%02x%02x_",
2229
88.4k
         sha_hash[0], sha_hash[1], sha_hash[2],
2230
88.4k
         sha_hash[3], sha_hash[4], sha_hash[5]);
2231
88.4k
  if((rc > 0) && (ja_str_len + rc < JA_STR_LEN)) ja_str_len += rc;
2232
2233
#ifdef DEBUG_JA
2234
  printf("[CIPHER] %s [len: %u]\n", tmp_str, tmp_str_len);
2235
#endif
2236
2237
#ifdef JA4R_DECIMAL
2238
  rc = snprintf(&ja4_r[ja4_r_len], sizeof(ja4_r)-ja4_r_len, "_");
2239
  if(rc > 0) ja4_r_len += rc;
2240
#endif
2241
2242
88.4k
  tmp_str_len = 0;
2243
878k
  for(i=0, num_extn = 0; i<ja->client.num_tls_extensions; i++) {
2244
790k
    if((ja->client.tls_extension[i] > 0) && (ja->client.tls_extension[i] != 0x10 /* ALPN extension */)) {
2245
#ifdef JA4R_DECIMAL
2246
      rc = snprintf(&ja4_r[ja4_r_len], sizeof(ja4_r)-ja4_r_len, "%s%u", (num_extn > 0) ? "," : "", ja->client.tls_extension[i]);
2247
      if((rc > 0) && (ja4_r_len + rc < JA_STR_LEN)) ja4_r_len += rc; else break;
2248
#endif
2249
2250
581k
      rc = ndpi_snprintf((char *)&tmp_str[tmp_str_len], JA_STR_LEN-tmp_str_len, "%s%04x",
2251
581k
       (num_extn > 0) ? "," : "", ja->client.tls_extension[i]);
2252
581k
      if((rc > 0) && (tmp_str_len + rc < JA_STR_LEN)) tmp_str_len += rc; else break;
2253
581k
      num_extn++;
2254
581k
    }
2255
790k
  }
2256
2257
754k
  for(i=0; i<ja->client.num_signature_algorithms; i++) {
2258
665k
    rc = ndpi_snprintf((char *)&tmp_str[tmp_str_len], JA_STR_LEN-tmp_str_len, "%s%04x",
2259
665k
           (i > 0) ? "," : "_", ja->client.signature_algorithms[i]);
2260
665k
    if((rc > 0) && (tmp_str_len + rc < JA_STR_LEN)) tmp_str_len += rc; else break;
2261
665k
  }
2262
2263
#ifdef DEBUG_JA
2264
  printf("[EXTN] %s [len: %u]\n", tmp_str, tmp_str_len);
2265
#endif
2266
2267
88.4k
  tmp_str[tmp_str_len] = 0;
2268
2269
88.4k
#ifndef JA4R_DECIMAL
2270
88.4k
  i = snprintf(&ja4_r[ja4_r_len], sizeof(ja4_r)-ja4_r_len, "%s", tmp_str); if(i > 0) ja4_r_len += i;
2271
88.4k
#endif
2272
2273
88.4k
  if(ndpi_struct->cfg.tls_ja4r_fingerprint_enabled) {
2274
68.0k
    if(flow->protos.tls_quic.ja4_client_raw == NULL)
2275
51.8k
      flow->protos.tls_quic.ja4_client_raw = ndpi_strdup(ja4_r);
2276
#ifdef DEBUG_JA
2277
    printf("[JA4_r] %s [len: %u]\n", ja4_r, ja4_r_len);
2278
#endif
2279
68.0k
  }
2280
2281
88.4k
  ndpi_sha256(tmp_str, tmp_str_len, sha_hash);
2282
2283
88.4k
  rc = ndpi_snprintf(&ja_str[ja_str_len], ja_max_len - ja_str_len,
2284
88.4k
         "%02x%02x%02x%02x%02x%02x",
2285
88.4k
         sha_hash[0], sha_hash[1], sha_hash[2],
2286
88.4k
         sha_hash[3], sha_hash[4], sha_hash[5]);
2287
88.4k
  if((rc > 0) && (ja_str_len + rc < JA_STR_LEN)) ja_str_len += rc;
2288
2289
88.4k
  ja_str[36] = 0;
2290
2291
#ifdef DEBUG_JA
2292
  printf("[JA4] %s [len: %lu]\n", ja_str, strlen(ja_str));
2293
#endif
2294
88.4k
}
2295
2296
/* **************************************** */
2297
2298
int processClientServerHello(struct ndpi_detection_module_struct *ndpi_struct,
2299
188k
           struct ndpi_flow_struct *flow, u_int32_t quic_version) {
2300
188k
  struct ndpi_packet_struct *packet = &ndpi_struct->packet;
2301
188k
  union ja_info ja;
2302
188k
  u_int8_t invalid_ja = 0;
2303
188k
  u_int16_t tls_version;
2304
188k
  u_int32_t i, j;
2305
188k
  u_int16_t total_len;
2306
188k
  u_int8_t handshake_type;
2307
188k
  bool is_quic = (quic_version != 0);
2308
188k
  bool is_dtls = (packet->udp && !is_quic) || flow->stun.maybe_dtls;
2309
2310
#ifdef DEBUG_TLS
2311
  printf("TLS %s() called\n", __FUNCTION__);
2312
#endif
2313
2314
188k
  handshake_type = packet->payload[0];
2315
188k
  total_len = (packet->payload[1] << 16) +  (packet->payload[2] << 8) + packet->payload[3];
2316
2317
188k
  if((total_len > packet->payload_packet_len) || (packet->payload[1] != 0x0))
2318
55
    return(0); /* Not found */
2319
2320
188k
  total_len = packet->payload_packet_len;
2321
2322
  /* At least "magic" 3 bytes, null for string end, otherwise no need to waste cpu cycles */
2323
188k
  if(total_len > 4) {
2324
184k
    u_int16_t base_offset    = (!is_dtls) ? 38 : 46;
2325
184k
    u_int16_t version_offset = (!is_dtls) ? 4 : 12;
2326
184k
    u_int16_t offset = (!is_dtls) ? 38 : 46;
2327
184k
    u_int32_t tot_extension_len;
2328
184k
    u_int8_t  session_id_len =  0;
2329
2330
184k
    if((base_offset >= total_len) ||
2331
179k
       (version_offset + 1) >= total_len)
2332
4.44k
      return 0; /* Not found */
2333
2334
179k
    session_id_len = packet->payload[base_offset];
2335
2336
#ifdef DEBUG_TLS
2337
    printf("TLS [len: %u][handshake_type: %02X]\n", packet->payload_packet_len, handshake_type);
2338
#endif
2339
2340
179k
    tls_version = ntohs(*((u_int16_t*)&packet->payload[version_offset]));
2341
2342
179k
    if(handshake_type == 0x02 /* Server Hello */) {
2343
67.9k
      int rc;
2344
2345
67.9k
      ja.server.num_ciphers = 0;
2346
67.9k
      ja.server.num_tls_extensions = 0;
2347
67.9k
      ja.server.num_elliptic_curve_point_format = 0;
2348
67.9k
      ja.server.alpn[0] = '\0';
2349
2350
67.9k
      ja.server.tls_handshake_version = tls_version;
2351
2352
#ifdef DEBUG_TLS
2353
      printf("TLS Server Hello [version: 0x%04X]\n", tls_version);
2354
#endif
2355
2356
      /*
2357
  The server hello decides about the TLS version of this flow
2358
  https://networkengineering.stackexchange.com/questions/55752/why-does-wireshark-show-version-tls-1-2-here-instead-of-tls-1-3
2359
      */
2360
67.9k
      if(packet->udp)
2361
9.69k
  offset += session_id_len + 1;
2362
58.2k
      else {
2363
58.2k
  if(tls_version < 0x7F15 /* TLS 1.3 lacks of session id */)
2364
49.8k
    offset += session_id_len+1;
2365
58.2k
      }
2366
2367
67.9k
      if((offset+3) > packet->payload_packet_len)
2368
3.26k
  return(0); /* Not found */
2369
2370
64.6k
      ja.server.num_ciphers = 1, ja.server.cipher[0] = ntohs(*((u_int16_t*)&packet->payload[offset]));
2371
2372
64.6k
      if(ndpi_struct->cfg.tls_cipher_enabled) {
2373
64.6k
        if((flow->protos.tls_quic.server_unsafe_cipher = ndpi_is_safe_ssl_cipher(ja.server.cipher[0])) != NDPI_CIPHER_SAFE) {
2374
14.5k
          if(is_flowrisk_info_enabled(ndpi_struct, NDPI_TLS_WEAK_CIPHER)) {
2375
13.8k
            char str[64];
2376
13.8k
            char unknown_cipher[8];
2377
2378
13.8k
            snprintf(str, sizeof(str), "Cipher %s", ndpi_cipher2str(ja.server.cipher[0], unknown_cipher));
2379
13.8k
            ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_WEAK_CIPHER, str);
2380
13.8k
          } else {
2381
657
            ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_WEAK_CIPHER, NULL);
2382
657
          }
2383
14.5k
        }
2384
2385
64.6k
        flow->protos.tls_quic.server_cipher = ja.server.cipher[0];
2386
64.6k
      }
2387
2388
#ifdef DEBUG_TLS
2389
      printf("TLS [server][session_id_len: %u][cipher: %04X]\n", session_id_len, ja.server.cipher[0]);
2390
#endif
2391
2392
64.6k
      offset += 2 + 1;
2393
2394
64.6k
      if((offset + 1) < packet->payload_packet_len) /* +1 because we are goint to read 2 bytes */
2395
58.4k
  tot_extension_len = ntohs(*((u_int16_t*)&packet->payload[offset]));
2396
6.19k
      else
2397
6.19k
  tot_extension_len = 0;
2398
2399
#ifdef DEBUG_TLS
2400
      printf("TLS [server][tot_extension_len: %u]\n", tot_extension_len);
2401
#endif
2402
64.6k
      offset += 2;
2403
2404
233k
      for(i=0; i<tot_extension_len; ) {
2405
205k
        u_int16_t extension_id;
2406
205k
        u_int32_t extension_len;
2407
2408
205k
  if((offset+4) > packet->payload_packet_len) break;
2409
2410
187k
  extension_id  = ntohs(*((u_int16_t*)&packet->payload[offset]));
2411
187k
  extension_len = ntohs(*((u_int16_t*)&packet->payload[offset+2]));
2412
187k
  if(offset+4+extension_len > packet->payload_packet_len) {
2413
18.2k
    break;
2414
18.2k
  }
2415
2416
169k
  if(ja.server.num_tls_extensions < MAX_NUM_JA)
2417
162k
    ja.server.tls_extension[ja.server.num_tls_extensions++] = extension_id;
2418
2419
#ifdef DEBUG_TLS
2420
  printf("TLS [server][extension_id: %u/0x%04X][len: %u]\n",
2421
         extension_id, extension_id, extension_len);
2422
#endif
2423
169k
  checkExtensions(ndpi_struct, flow, is_dtls, extension_id, extension_len, offset + 4);
2424
2425
169k
  if(extension_id == 43 /* supported versions */) {
2426
11.5k
    if(extension_len >= 2) {
2427
10.6k
      u_int16_t tls_version = ntohs(*((u_int16_t*)&packet->payload[offset+4]));
2428
2429
#ifdef DEBUG_TLS
2430
      printf("TLS [server] [TLS version: 0x%04X]\n", tls_version);
2431
#endif
2432
2433
10.6k
      flow->protos.tls_quic.ssl_version = ja.server.tls_supported_version = tls_version;
2434
10.6k
    }
2435
157k
  } else if(extension_id == 16 /* application_layer_protocol_negotiation (ALPN) */ &&
2436
19.3k
            offset + 6 < packet->payload_packet_len) {
2437
19.2k
    u_int16_t s_offset = offset+4;
2438
19.2k
    u_int16_t tot_alpn_len = ntohs(*((u_int16_t*)&packet->payload[s_offset]));
2439
19.2k
    char alpn_str[256];
2440
19.2k
    u_int16_t alpn_str_len = 0, i;
2441
2442
#ifdef DEBUG_TLS
2443
    printf("Server TLS [ALPN: block_len=%u/len=%u]\n", extension_len, tot_alpn_len);
2444
#endif
2445
19.2k
    s_offset += 2;
2446
19.2k
    tot_alpn_len += s_offset;
2447
2448
19.2k
    if(tot_alpn_len > packet->payload_packet_len)
2449
804
      return 0;
2450
2451
96.8k
    while(s_offset < tot_alpn_len && s_offset < total_len) {
2452
85.6k
      u_int8_t alpn_i, alpn_len = packet->payload[s_offset++];
2453
2454
85.6k
      if((s_offset + alpn_len) <= tot_alpn_len) {
2455
#ifdef DEBUG_TLS
2456
        printf("Server TLS [ALPN: %u]\n", alpn_len);
2457
#endif
2458
2459
78.7k
        if(((uint32_t)alpn_str_len+alpn_len+1) < (sizeof(alpn_str)-1)) {
2460
78.3k
          if(alpn_str_len > 0) {
2461
47.1k
            alpn_str[alpn_str_len] = ',';
2462
47.1k
            alpn_str_len++;
2463
47.1k
          }
2464
2465
334k
          for(alpn_i=0; alpn_i<alpn_len; alpn_i++) {
2466
256k
        alpn_str[alpn_str_len+alpn_i] = packet->payload[s_offset+alpn_i];
2467
256k
      }
2468
2469
78.3k
          s_offset += alpn_len, alpn_str_len += alpn_len;;
2470
78.3k
        } else {
2471
408
          alpn_str[alpn_str_len] = '\0';
2472
408
          ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_UNCOMMON_ALPN, alpn_str);
2473
408
          break;
2474
408
        }
2475
78.7k
      } else {
2476
6.91k
        alpn_str[alpn_str_len] = '\0';
2477
6.91k
        ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_UNCOMMON_ALPN, alpn_str);
2478
6.91k
        break;
2479
6.91k
      }
2480
85.6k
    } /* while */
2481
2482
18.4k
    alpn_str[alpn_str_len] = '\0';
2483
2484
#ifdef DEBUG_TLS
2485
    printf("Server TLS [ALPN: %s][len: %u]\n", alpn_str, alpn_str_len);
2486
#endif
2487
18.4k
    if(ndpi_normalize_printable_string(alpn_str, alpn_str_len) == 0)
2488
5.96k
      ndpi_set_risk(ndpi_struct, flow, NDPI_INVALID_CHARACTERS, alpn_str);
2489
2490
18.4k
    if(flow->protos.tls_quic.negotiated_alpn == NULL &&
2491
7.96k
       ndpi_struct->cfg.tls_alpn_negotiated_enabled)
2492
7.95k
      flow->protos.tls_quic.negotiated_alpn = ndpi_strdup(alpn_str);
2493
2494
    /* Check ALPN only if not already checked (client-side) */
2495
18.4k
    if(flow->protos.tls_quic.negotiated_alpn != NULL &&
2496
18.3k
       flow->protos.tls_quic.advertised_alpns == NULL)
2497
13.5k
      tlsCheckUncommonALPN(ndpi_struct, flow, flow->protos.tls_quic.negotiated_alpn);
2498
2499
18.4k
    alpn_str_len = ndpi_min(sizeof(ja.server.alpn), (size_t)alpn_str_len);
2500
18.4k
    memcpy(ja.server.alpn, alpn_str, alpn_str_len);
2501
18.4k
    if(alpn_str_len > 0)
2502
13.0k
      ja.server.alpn[alpn_str_len - 1] = '\0';
2503
2504
    /* Replace , with - as in JA3 */
2505
468k
    for(i=0; ja.server.alpn[i] != '\0'; i++)
2506
450k
      if(ja.server.alpn[i] == ',') ja.server.alpn[i] = '-';
2507
138k
  } else if(extension_id == 11 /* ec_point_formats groups */) {
2508
17.1k
    u_int16_t s_offset = offset+4 + 1;
2509
2510
#ifdef DEBUG_TLS
2511
    printf("Server TLS [EllipticCurveFormat: len=%u]\n", extension_len);
2512
#endif
2513
17.1k
    if((s_offset+extension_len-1) <= total_len) {
2514
413k
      for(i=0; i<extension_len-1 && s_offset+i<packet->payload_packet_len; i++) {
2515
396k
        u_int8_t s_group = packet->payload[s_offset+i];
2516
2517
#ifdef DEBUG_TLS
2518
        printf("Server TLS [EllipticCurveFormat: %u]\n", s_group);
2519
#endif
2520
2521
396k
        if(ja.server.num_elliptic_curve_point_format < MAX_NUM_JA)
2522
84.8k
    ja.server.elliptic_curve_point_format[ja.server.num_elliptic_curve_point_format++] = s_group;
2523
311k
        else {
2524
311k
    invalid_ja = 1;
2525
#ifdef DEBUG_TLS
2526
    printf("Server TLS Invalid num elliptic %u\n", ja.server.num_elliptic_curve_point_format);
2527
#endif
2528
311k
        }
2529
396k
      }
2530
17.1k
    } else {
2531
0
      invalid_ja = 1;
2532
#ifdef DEBUG_TLS
2533
      printf("Server TLS Invalid len %u vs %u\n", s_offset+extension_len, total_len);
2534
#endif
2535
0
    }
2536
17.1k
  }
2537
2538
168k
  i += 4 + extension_len, offset += 4 + extension_len;
2539
168k
      } /* for */
2540
2541
      /* If the CH is not available and if "supported_versions" extension is not present in the SH
2542
         (i.e. (D)TLS <= 1.2), use the version field present in the record layer */
2543
63.8k
      if(flow->protos.tls_quic.ssl_version == 0)
2544
27.2k
        flow->protos.tls_quic.ssl_version = tls_version;
2545
2546
63.8k
      if(ndpi_struct->cfg.tls_ja3s_fingerprint_enabled) {
2547
63.8k
         u_int16_t ja_str_len;
2548
63.8k
         char ja_str[JA_STR_LEN];
2549
63.8k
         ndpi_MD5_CTX ctx;
2550
63.8k
         u_char md5_hash[16];
2551
2552
63.8k
        ja_str_len = ndpi_snprintf(ja_str, JA_STR_LEN, "%u,", ja.server.tls_handshake_version);
2553
2554
127k
        for(i=0; (i<ja.server.num_ciphers) && (JA_STR_LEN > ja_str_len); i++) {
2555
63.8k
    rc = ndpi_snprintf(&ja_str[ja_str_len], JA_STR_LEN-ja_str_len, "%s%u", (i > 0) ? "-" : "", ja.server.cipher[i]);
2556
2557
63.8k
    if(rc <= 0) break; else ja_str_len += rc;
2558
63.8k
        }
2559
2560
63.8k
        if(JA_STR_LEN > ja_str_len) {
2561
63.8k
    rc = ndpi_snprintf(&ja_str[ja_str_len], JA_STR_LEN-ja_str_len, ",");
2562
63.8k
    if(rc > 0 && ja_str_len + rc < JA_STR_LEN) ja_str_len += rc;
2563
63.8k
        }
2564
2565
        /* ********** */
2566
2567
222k
        for(i=0; (i<ja.server.num_tls_extensions) && (JA_STR_LEN > ja_str_len); i++) {
2568
158k
    int rc = ndpi_snprintf(&ja_str[ja_str_len], JA_STR_LEN-ja_str_len, "%s%u", (i > 0) ? "-" : "", ja.server.tls_extension[i]);
2569
2570
158k
    if(rc <= 0) break; else ja_str_len += rc;
2571
158k
        }
2572
2573
#ifdef DEBUG_TLS
2574
        printf("[JA3] Server: %s \n", ja_str);
2575
#endif
2576
2577
63.8k
        ndpi_MD5Init(&ctx);
2578
63.8k
        ndpi_MD5Update(&ctx, (const unsigned char *)ja_str, strlen(ja_str));
2579
63.8k
        ndpi_MD5Final(md5_hash, &ctx);
2580
2581
1.08M
        for(i=0, j=0; i<16; i++) {
2582
1.02M
    int rc = ndpi_snprintf(&flow->protos.tls_quic.ja3_server[j],
2583
1.02M
               sizeof(flow->protos.tls_quic.ja3_server)-j, "%02x", md5_hash[i]);
2584
1.02M
    if(rc <= 0) break; else j += rc;
2585
1.02M
        }
2586
2587
#ifdef DEBUG_TLS
2588
        printf("[JA3] Server: %s \n", flow->protos.tls_quic.ja3_server);
2589
#endif
2590
63.8k
      }
2591
111k
    } else if(handshake_type == 0x01 /* Client Hello */) {
2592
111k
      u_int16_t cipher_len, cipher_offset;
2593
111k
      u_int8_t cookie_len = 0;
2594
2595
111k
      ja.client.num_ciphers = 0;
2596
111k
      ja.client.num_tls_extensions = 0;
2597
111k
      ja.client.num_elliptic_curve = 0;
2598
111k
      ja.client.num_elliptic_curve_point_format = 0;
2599
111k
      ja.client.num_signature_algorithms = 0;
2600
111k
      ja.client.num_supported_versions = 0;
2601
111k
      ja.client.signature_algorithms_str[0] = '\0';
2602
111k
      ja.client.alpn[0] = '\0', ja.client.alpn[1] = '\0' /* used by JA4 */;
2603
111k
      ja.client.alpn_original_last = '0'; /* Initialize to '0' if no ALPN */
2604
2605
111k
      flow->protos.tls_quic.ssl_version = ja.client.tls_handshake_version = tls_version;
2606
111k
      if(flow->protos.tls_quic.ssl_version < 0x0303) /* < TLSv1.2 */ {
2607
21.5k
        if(is_flowrisk_info_enabled(ndpi_struct, NDPI_TLS_OBSOLETE_VERSION)) {
2608
20.9k
          char str[32], buf[32];
2609
20.9k
    u_int8_t unknown_tls_version;
2610
2611
20.9k
    snprintf(str, sizeof(str), "%s", ndpi_ssl_version2str(buf, sizeof(buf),
2612
20.9k
                      flow->protos.tls_quic.ssl_version,
2613
20.9k
                     &unknown_tls_version));
2614
20.9k
    ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_OBSOLETE_VERSION, str);
2615
20.9k
        } else {
2616
575
          ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_OBSOLETE_VERSION, NULL);
2617
575
        }
2618
21.5k
      }
2619
2620
111k
      if((session_id_len+base_offset+3) > packet->payload_packet_len)
2621
2.52k
  return(0); /* Not found */
2622
2623
109k
      if(!is_dtls) {
2624
82.3k
  cipher_len = packet->payload[session_id_len+base_offset+2] + (packet->payload[session_id_len+base_offset+1] << 8);
2625
82.3k
  cipher_offset = base_offset + session_id_len + 3;
2626
82.3k
      } else {
2627
26.8k
  cookie_len = packet->payload[base_offset+session_id_len+1];
2628
#ifdef DEBUG_TLS
2629
  printf("[JA3] Client: DTLS cookie len %d\n", cookie_len);
2630
#endif
2631
26.8k
  if((session_id_len+base_offset+cookie_len+4) > packet->payload_packet_len)
2632
790
    return(0); /* Not found */
2633
26.0k
  cipher_len = ntohs(*((u_int16_t*)&packet->payload[base_offset+session_id_len+cookie_len+2]));
2634
26.0k
  cipher_offset = base_offset + session_id_len + cookie_len + 4;
2635
26.0k
      }
2636
2637
#ifdef DEBUG_TLS
2638
      printf("Client TLS [client cipher_len: %u][tls_version: 0x%04X]\n", cipher_len, tls_version);
2639
#endif
2640
2641
108k
      if((cipher_offset+cipher_len) <= total_len - 1) { /* -1 because variable "id" is a u_int16_t */
2642
94.8k
  u_int8_t safari_ciphers = 0, chrome_ciphers = 0, this_is_not_safari = 0, looks_like_safari_on_big_sur = 0;
2643
2644
1.60M
  for(i=0; i<cipher_len;) {
2645
1.50M
    u_int16_t *id = (u_int16_t*)&packet->payload[cipher_offset+i];
2646
1.50M
    u_int16_t cipher_id = ntohs(*id);
2647
2648
1.50M
    if(cipher_offset+i+1 < packet->payload_packet_len &&
2649
1.50M
       ((packet->payload[cipher_offset+i] != packet->payload[cipher_offset+i+1]) ||
2650
1.48M
        ((packet->payload[cipher_offset+i] & 0xF) != 0xA)) /* Skip Grease */) {
2651
      /*
2652
        Skip GREASE [https://tools.ietf.org/id/draft-ietf-tls-grease-01.html]
2653
        https://engineering.salesforce.com/tls-fingerprinting-with-ja3-and-ja3s-247362855967
2654
      */
2655
2656
#if defined(DEBUG_TLS) || defined(DEBUG_HEURISTIC)
2657
      printf("Client TLS [non-GREASE cipher suite: %u/0x%04X] [%d/%u]\n", cipher_id, cipher_id, i, cipher_len);
2658
#endif
2659
2660
1.48M
      if(ja.client.num_ciphers < MAX_NUM_JA)
2661
1.40M
        ja.client.cipher[ja.client.num_ciphers++] = cipher_id;
2662
81.2k
      else {
2663
81.2k
        invalid_ja = 1;
2664
#ifdef DEBUG_TLS
2665
        printf("Client TLS Invalid cipher %u\n", ja.client.num_ciphers);
2666
#endif
2667
81.2k
      }
2668
2669
#if defined(DEBUG_TLS) || defined(DEBUG_HEURISTIC)
2670
      printf("Client TLS [cipher suite: %u/0x%04X] [%d/%u]\n", cipher_id, cipher_id, i, cipher_len);
2671
#endif
2672
2673
1.48M
      switch(cipher_id) {
2674
46.4k
      case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
2675
83.7k
      case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
2676
83.7k
        safari_ciphers++;
2677
83.7k
        break;
2678
2679
43.5k
      case TLS_AES_128_GCM_SHA256:
2680
86.6k
      case TLS_AES_256_GCM_SHA384:
2681
128k
      case TLS_CHACHA20_POLY1305_SHA256:
2682
128k
        chrome_ciphers++;
2683
128k
        break;
2684
2685
39.1k
      case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256:
2686
74.3k
      case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
2687
120k
      case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
2688
160k
      case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
2689
209k
      case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
2690
256k
      case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
2691
300k
      case TLS_RSA_WITH_AES_128_CBC_SHA:
2692
345k
      case TLS_RSA_WITH_AES_256_CBC_SHA:
2693
383k
      case TLS_RSA_WITH_AES_128_GCM_SHA256:
2694
415k
      case TLS_RSA_WITH_AES_256_GCM_SHA384:
2695
415k
        safari_ciphers++, chrome_ciphers++;
2696
415k
        break;
2697
2698
21.2k
      case TLS_RSA_WITH_3DES_EDE_CBC_SHA:
2699
21.2k
        looks_like_safari_on_big_sur = 1;
2700
21.2k
        break;
2701
1.48M
      }
2702
1.48M
    } else {
2703
#if defined(DEBUG_TLS) || defined(DEBUG_HEURISTIC)
2704
      printf("Client TLS [GREASE cipher suite: %u/0x%04X] [%d/%u]\n", cipher_id, cipher_id, i, cipher_len);
2705
#endif
2706
2707
22.2k
      this_is_not_safari = 1; /* NOTE: BugSur and up have grease support */
2708
22.2k
    }
2709
2710
1.50M
    i += 2;
2711
1.50M
  } /* for */
2712
2713
94.8k
  if(ndpi_struct->cfg.tls_broswer_enabled) {
2714
          /* NOTE:
2715
             we do not check for duplicates as with signatures because
2716
             this is time consuming and we want to avoid overhead whem possible
2717
          */
2718
94.8k
          if(this_is_not_safari)
2719
15.7k
            flow->protos.tls_quic.browser_heuristics.is_safari_tls = 0;
2720
79.0k
          else if((safari_ciphers == 12) || (this_is_not_safari && looks_like_safari_on_big_sur))
2721
12.0k
            flow->protos.tls_quic.browser_heuristics.is_safari_tls = 1;
2722
2723
94.8k
          if(chrome_ciphers == 13)
2724
17.7k
            flow->protos.tls_quic.browser_heuristics.is_chrome_tls = 1;
2725
2726
          /* Note that both Safari and Chrome can overlap */
2727
#ifdef DEBUG_HEURISTIC
2728
          printf("[CIPHERS] [is_chrome_tls: %u (%u)][is_safari_tls: %u (%u)][this_is_not_safari: %u]\n",
2729
                 flow->protos.tls_quic.browser_heuristics.is_chrome_tls,
2730
                 chrome_ciphers,
2731
                 flow->protos.tls_quic.browser_heuristics.is_safari_tls,
2732
                 safari_ciphers,
2733
                 this_is_not_safari);
2734
#endif
2735
94.8k
  }
2736
94.8k
      } else {
2737
13.5k
  invalid_ja = 1;
2738
#ifdef DEBUG_TLS
2739
  printf("Client TLS Invalid len %u vs %u\n", (cipher_offset+cipher_len), total_len);
2740
#endif
2741
13.5k
      }
2742
2743
108k
      offset = base_offset + session_id_len + cookie_len + cipher_len + 2;
2744
108k
      offset += (!is_dtls) ? 1 : 2;
2745
2746
108k
      if(offset < total_len) {
2747
103k
  u_int16_t compression_len;
2748
103k
  u_int16_t extensions_len;
2749
2750
103k
  compression_len = packet->payload[offset];
2751
103k
  offset++;
2752
2753
#ifdef DEBUG_TLS
2754
  printf("Client TLS [compression_len: %u]\n", compression_len);
2755
#endif
2756
2757
  // offset += compression_len + 3;
2758
103k
  offset += compression_len;
2759
2760
103k
  if(offset+1 < total_len) {
2761
99.4k
    extensions_len = ntohs(*((u_int16_t*)&packet->payload[offset]));
2762
99.4k
    offset += 2;
2763
2764
#ifdef DEBUG_TLS
2765
    printf("Client TLS [extensions_len: %u]\n", extensions_len);
2766
#endif
2767
2768
99.4k
    if((extensions_len+offset) <= total_len) {
2769
      /* Move to the first extension
2770
         Type is u_int to avoid possible overflow on extension_len addition */
2771
95.9k
      u_int extension_offset = 0;
2772
2773
1.05M
      while(extension_offset < extensions_len &&
2774
984k
      offset+extension_offset+4 <= total_len) {
2775
981k
        u_int16_t extension_id, extension_len, extn_off = offset+extension_offset;
2776
2777
981k
        extension_id = ntohs(*((u_int16_t*)&packet->payload[offset+extension_offset]));
2778
981k
        extension_offset += 2;
2779
2780
981k
        extension_len = ntohs(*((u_int16_t*)&packet->payload[offset+extension_offset]));
2781
981k
        extension_offset += 2;
2782
2783
#ifdef DEBUG_TLS
2784
        printf("Client TLS [extension_id: %u][extension_len: %u]\n", extension_id, extension_len);
2785
#endif
2786
981k
        checkExtensions(ndpi_struct, flow, is_dtls,
2787
981k
            extension_id, extension_len, offset + extension_offset);
2788
2789
981k
        if(offset + 4 + extension_len > total_len) {
2790
#ifdef DEBUG_TLS
2791
          printf("[TLS] extension length %u too long (%u, offset %u)\n",
2792
                 extension_len, total_len, offset);
2793
#endif
2794
26.4k
          break;
2795
26.4k
        }
2796
2797
955k
        if((extension_id == 0) || (packet->payload[extn_off] != packet->payload[extn_off+1]) ||
2798
930k
     ((packet->payload[extn_off] & 0xF) != 0xA)) {
2799
    /* Skip GREASE */
2800
2801
930k
    if(ja.client.num_tls_extensions < MAX_NUM_JA)
2802
883k
      ja.client.tls_extension[ja.client.num_tls_extensions++] = extension_id;
2803
46.5k
    else {
2804
46.5k
      invalid_ja = 1;
2805
#ifdef DEBUG_TLS
2806
      printf("Client TLS Invalid extensions %u\n", ja.client.num_tls_extensions);
2807
#endif
2808
46.5k
    }
2809
930k
        }
2810
2811
955k
        if(extension_id == 0 /* server name */) {
2812
224k
    u_int16_t len;
2813
224k
    bool sni_numeric = false;
2814
2815
#ifdef DEBUG_TLS
2816
    printf("[TLS] Extensions: found server name\n");
2817
#endif
2818
224k
    if((offset+extension_offset+4) < packet->payload_packet_len) {
2819
220k
      len = (packet->payload[offset+extension_offset+3] << 8) + packet->payload[offset+extension_offset+4];
2820
2821
220k
      if((offset+extension_offset+5+len) <= packet->payload_packet_len) {
2822
206k
        char *sni = ndpi_hostname_sni_set(flow, &packet->payload[offset+extension_offset+5], len, NDPI_HOSTNAME_NORM_ALL);
2823
206k
        int sni_len = strlen(sni);
2824
#ifdef DEBUG_TLS
2825
        printf("[TLS] SNI: [%s]\n", sni);
2826
#endif
2827
206k
        if(ndpi_is_valid_hostname((char *)&packet->payload[offset+extension_offset+5], len) == 0) {
2828
153k
          ndpi_set_risk(ndpi_struct, flow, NDPI_INVALID_CHARACTERS, sni);
2829
2830
          /* This looks like an attack */
2831
153k
          ndpi_set_risk(ndpi_struct, flow, NDPI_POSSIBLE_EXPLOIT, "Invalid chars found in SNI: exploit or misconfiguration?");
2832
153k
        }
2833
2834
206k
        if(!is_quic) {
2835
171k
          if(ndpi_struct->cfg.tls_subclassification_enabled &&
2836
171k
             flow->protos.tls_quic.subprotocol_detected == 0 &&
2837
162k
             !flow->tls_quic.from_rdp && /* No (other) sub-classification; we will have TLS.RDP anyway */
2838
162k
             ndpi_match_hostname_protocol(ndpi_struct, flow, ndpi_get_master_proto(ndpi_struct, flow), sni, sni_len))
2839
26.4k
            flow->protos.tls_quic.subprotocol_detected = 1;
2840
171k
        } else {
2841
34.5k
          if(ndpi_struct->cfg.quic_subclassification_enabled &&
2842
34.5k
             flow->protos.tls_quic.subprotocol_detected == 0 &&
2843
33.6k
             !flow->tls_quic.from_rdp && /* No (other) sub-classification; we will have TLS.RDP anyway */
2844
33.6k
             ndpi_match_hostname_protocol(ndpi_struct, flow, NDPI_PROTOCOL_QUIC, sni, sni_len))
2845
17.3k
            flow->protos.tls_quic.subprotocol_detected = 1;
2846
34.5k
        }
2847
2848
206k
        if((flow->protos.tls_quic.subprotocol_detected == 0)
2849
152k
           && (ndpi_check_is_numeric_ip(sni) == 1)) {
2850
670
          ndpi_set_risk(ndpi_struct, flow, NDPI_NUMERIC_IP_HOST, sni);
2851
670
          sni_numeric = true;
2852
670
        }
2853
2854
206k
        if(ndpi_str_endswith(sni, "signal.org")) {
2855
          /* printf("[SIGNAL] SNI: [%s]\n", sni); */
2856
699
          signal_add_to_cache(ndpi_struct, flow);
2857
699
        }
2858
2859
206k
        if(ndpi_check_dga_name(ndpi_struct, flow, sni, 1, 0, 0)) {
2860
#ifdef DEBUG_TLS
2861
          printf("[TLS] SNI: (DGA) [%s]\n", sni);
2862
#endif
2863
2864
16.9k
          if((sni_len >= 4)
2865
             /* Check if it ends in .com or .net */
2866
16.9k
             && ((strcmp(&sni[sni_len-4], ".com") == 0) || (strcmp(&sni[sni_len-4], ".net") == 0))
2867
1.03k
             && (strncmp(sni, "www.", 4) == 0)) /* Starting with www.... */
2868
421
            ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_TOR, ndpi_get_master_proto(ndpi_struct, flow), NDPI_CONFIDENCE_DPI);
2869
189k
        } else {
2870
#ifdef DEBUG_TLS
2871
          printf("[TLS] SNI: (NO DGA) [%s]\n", sni);
2872
#endif
2873
189k
        }
2874
2875
206k
        if(ndpi_struct->cfg.hostname_dns_check_enabled && (!sni_numeric)) {
2876
143k
          ndpi_ip_addr_t ip_addr;
2877
2878
143k
          memset(&ip_addr, 0, sizeof(ip_addr));
2879
2880
143k
          if(packet->iph)
2881
132k
      ip_addr.ipv4 = packet->iph->daddr;
2882
11.6k
          else
2883
11.6k
      memcpy(&ip_addr.ipv6, &packet->iphv6->ip6_dst,
2884
11.6k
             sizeof(struct ndpi_in6_addr));
2885
2886
143k
          if(!ndpi_cache_find_hostname_ip(ndpi_struct, &ip_addr, sni)) {
2887
#ifdef DEBUG_TLS
2888
      printf("[TLS] Not found SNI %s\n", sni);
2889
#endif
2890
139k
      ndpi_set_risk(ndpi_struct, flow, NDPI_UNRESOLVED_HOSTNAME, sni);
2891
2892
139k
          } else {
2893
#ifdef DEBUG_TLS
2894
      printf("[TLS] Found SNI %s\n", sni);
2895
#endif
2896
4.29k
          }
2897
143k
        }
2898
206k
      } else {
2899
#ifdef DEBUG_TLS
2900
        printf("[TLS] Extensions server len too short: %u vs %u\n",
2901
         offset+extension_offset+5+len,
2902
         packet->payload_packet_len);
2903
#endif
2904
13.9k
      }
2905
220k
    }
2906
730k
        } else if(extension_id == 10 /* supported groups */) {
2907
62.5k
    u_int16_t s_offset = offset+extension_offset + 2;
2908
2909
#ifdef DEBUG_TLS
2910
    printf("Client TLS [EllipticCurveGroups: len=%u]\n", extension_len);
2911
#endif
2912
2913
62.5k
    if((s_offset+extension_len-2) <= total_len) {
2914
752k
      for(i=0; i<(u_int32_t)extension_len-2 && s_offset + i + 1 < total_len; i += 2) {
2915
690k
        u_int16_t s_group = ntohs(*((u_int16_t*)&packet->payload[s_offset+i]));
2916
2917
#ifdef DEBUG_TLS
2918
        printf("Client TLS [EllipticCurve: %u/0x%04X]\n", s_group, s_group);
2919
#endif
2920
690k
        switch(s_group) {
2921
343
        case 0x11EC: /* X25519MLKEM768 */
2922
343
          flow->protos.tls_quic.pq_supported_groups = 1;
2923
343
          break;
2924
690k
        }
2925
2926
690k
        if((s_group == 0) || (packet->payload[s_offset+i] != packet->payload[s_offset+i+1])
2927
665k
           || ((packet->payload[s_offset+i] & 0xF) != 0xA)) {
2928
          /* Skip GREASE */
2929
665k
          if(ja.client.num_elliptic_curve < MAX_NUM_JA)
2930
406k
      ja.client.elliptic_curve[ja.client.num_elliptic_curve++] = s_group;
2931
259k
          else {
2932
259k
      invalid_ja = 1;
2933
#ifdef DEBUG_TLS
2934
      printf("Client TLS Invalid num elliptic %u\n", ja.client.num_elliptic_curve);
2935
#endif
2936
259k
          }
2937
665k
        }
2938
690k
      }
2939
62.3k
    } else {
2940
266
      invalid_ja = 1;
2941
#ifdef DEBUG_TLS
2942
      printf("Client TLS Invalid len %u vs %u\n", (s_offset+extension_len-1), total_len);
2943
#endif
2944
266
    }
2945
668k
        } else if(extension_id == 11 /* ec_point_formats groups */) {
2946
43.1k
    u_int16_t s_offset = offset+extension_offset + 1;
2947
2948
#ifdef DEBUG_TLS
2949
    printf("Client TLS [EllipticCurveFormat: len=%u]\n", extension_len);
2950
#endif
2951
43.1k
    if((s_offset+extension_len-1) <= total_len) {
2952
1.37M
      for(i=0; i<(u_int32_t)extension_len-1 && s_offset+i < total_len; i++) {
2953
1.32M
        u_int8_t s_group = packet->payload[s_offset+i];
2954
2955
#ifdef DEBUG_TLS
2956
        printf("Client TLS [EllipticCurveFormat: %u]\n", s_group);
2957
#endif
2958
2959
1.32M
        if(ja.client.num_elliptic_curve_point_format < MAX_NUM_JA)
2960
210k
          ja.client.elliptic_curve_point_format[ja.client.num_elliptic_curve_point_format++] = s_group;
2961
1.11M
        else {
2962
1.11M
          invalid_ja = 1;
2963
#ifdef DEBUG_TLS
2964
          printf("Client TLS Invalid num elliptic %u\n", ja.client.num_elliptic_curve_point_format);
2965
#endif
2966
1.11M
        }
2967
1.32M
      }
2968
42.9k
    } else {
2969
263
      invalid_ja = 1;
2970
#ifdef DEBUG_TLS
2971
      printf("Client TLS Invalid len %u vs %u\n", s_offset+extension_len, total_len);
2972
#endif
2973
263
    }
2974
624k
        } else if(extension_id == 13 /* signature algorithms */ &&
2975
72.1k
                  offset+extension_offset+1 < total_len) {
2976
70.4k
    int s_offset = offset+extension_offset, safari_signature_algorithms = 0, id;
2977
70.4k
    u_int16_t tot_signature_algorithms_len = ntohs(*((u_int16_t*)&packet->payload[s_offset]));
2978
2979
#ifdef DEBUG_TLS
2980
    printf("Client TLS [SIGNATURE_ALGORITHMS: block_len=%u/len=%u]\n", extension_len, tot_signature_algorithms_len);
2981
#endif
2982
2983
70.4k
    s_offset += 2;
2984
70.4k
    tot_signature_algorithms_len = ndpi_min((sizeof(ja.client.signature_algorithms_str) / 2) - 1, tot_signature_algorithms_len);
2985
2986
70.4k
#ifdef TLS_HANDLE_SIGNATURE_ALGORITMS
2987
70.4k
    size_t sa_size = ndpi_min(tot_signature_algorithms_len / 2, MAX_NUM_TLS_SIGNATURE_ALGORITHMS);
2988
2989
70.4k
    if (s_offset + 2 * sa_size <= packet->payload_packet_len) {
2990
64.0k
      flow->protos.tls_quic.num_tls_signature_algorithms = sa_size;
2991
64.0k
      memcpy(flow->protos.tls_quic.client_signature_algorithms,
2992
64.0k
       &packet->payload[s_offset], 2 /* 16 bit */ * sa_size);
2993
64.0k
    }
2994
70.4k
#endif
2995
2996
1.00M
    for(i=0, id=0; i<tot_signature_algorithms_len && s_offset+i+1<total_len; i += 2) {
2997
932k
      ja.client.signature_algorithms[id++] = ntohs(*(u_int16_t*)&packet->payload[s_offset+i]);
2998
932k
    }
2999
70.4k
    ja.client.num_signature_algorithms = id;
3000
3001
1.92M
    for(i=0, id=0; i<tot_signature_algorithms_len && s_offset+i+1<total_len; i++) {
3002
1.85M
      int rc = ndpi_snprintf(&ja.client.signature_algorithms_str[i*2],
3003
1.85M
           sizeof(ja.client.signature_algorithms_str)-i*2,
3004
1.85M
           "%02X", packet->payload[s_offset+i]);
3005
1.85M
      if(rc < 0) break;
3006
1.85M
    }
3007
3008
70.4k
    if(ndpi_struct->cfg.tls_broswer_enabled) {
3009
70.4k
            int chrome_signature_algorithms = 0, duplicate_found = 0, last_signature = 0;
3010
3011
997k
                  for(i=0; i<tot_signature_algorithms_len && s_offset + (int)i + 2 < packet->payload_packet_len; i+=2) {
3012
927k
                    u_int16_t signature_algo = (u_int16_t)ntohs(*((u_int16_t*)&packet->payload[s_offset+i]));
3013
3014
927k
                    if(last_signature == signature_algo) {
3015
                      /* Consecutive duplication */
3016
20.7k
                      duplicate_found = 1;
3017
20.7k
                      continue;
3018
906k
                    } else {
3019
                      /* Check for other duplications */
3020
906k
                      u_int all_ok = 1;
3021
3022
24.6M
                      for(j=0; j<tot_signature_algorithms_len; j+=2) {
3023
23.8M
                        if(j != i && s_offset + (int)j + 2 < packet->payload_packet_len) {
3024
18.8M
                          u_int16_t j_signature_algo = (u_int16_t)ntohs(*((u_int16_t*)&packet->payload[s_offset+j]));
3025
3026
18.8M
                          if((signature_algo == j_signature_algo)
3027
1.49M
                             && (i < j) /* Don't skip both of them */) {
3028
#ifdef DEBUG_HEURISTIC
3029
                            printf("[SIGNATURE] [TLS Signature Algorithm] Skipping duplicate 0x%04X\n", signature_algo);
3030
#endif
3031
3032
150k
                            duplicate_found = 1, all_ok = 0;
3033
150k
                            break;
3034
150k
                          }
3035
18.8M
                        }
3036
23.8M
                      }
3037
3038
906k
                      if(!all_ok)
3039
150k
                        continue;
3040
906k
                    }
3041
3042
755k
                    last_signature = signature_algo;
3043
3044
#ifdef DEBUG_HEURISTIC
3045
                    printf("[SIGNATURE] [TLS Signature Algorithm] 0x%04X\n", signature_algo);
3046
#endif
3047
755k
                    switch(signature_algo) {
3048
14.9k
                    case ECDSA_SECP521R1_SHA512:
3049
14.9k
                      flow->protos.tls_quic.browser_heuristics.is_firefox_tls = 1;
3050
14.9k
                      break;
3051
3052
52.8k
                    case ECDSA_SECP256R1_SHA256:
3053
104k
                    case ECDSA_SECP384R1_SHA384:
3054
157k
                    case RSA_PKCS1_SHA256:
3055
209k
                    case RSA_PKCS1_SHA384:
3056
261k
                    case RSA_PKCS1_SHA512:
3057
311k
                    case RSA_PSS_RSAE_SHA256:
3058
362k
                    case RSA_PSS_RSAE_SHA384:
3059
409k
                    case RSA_PSS_RSAE_SHA512:
3060
409k
                      chrome_signature_algorithms++, safari_signature_algorithms++;
3061
#ifdef DEBUG_HEURISTIC
3062
                      printf("[SIGNATURE] [Chrome/Safari] Found 0x%04X [chrome: %u][safari: %u]\n",
3063
                             signature_algo, chrome_signature_algorithms, safari_signature_algorithms);
3064
#endif
3065
3066
409k
                      break;
3067
755k
                    }
3068
755k
                  }
3069
3070
#ifdef DEBUG_HEURISTIC
3071
                  printf("[SIGNATURE] [safari_signature_algorithms: %u][chrome_signature_algorithms: %u]\n",
3072
                         safari_signature_algorithms, chrome_signature_algorithms);
3073
#endif
3074
3075
70.4k
                  if(flow->protos.tls_quic.browser_heuristics.is_firefox_tls)
3076
15.7k
                    flow->protos.tls_quic.browser_heuristics.is_safari_tls = 0,
3077
15.7k
                      flow->protos.tls_quic.browser_heuristics.is_chrome_tls = 0;
3078
3079
70.4k
                  if(safari_signature_algorithms != 8)
3080
26.0k
                    flow->protos.tls_quic.browser_heuristics.is_safari_tls = 0;
3081
3082
70.4k
                  if((chrome_signature_algorithms != 8) || duplicate_found)
3083
27.7k
                    flow->protos.tls_quic.browser_heuristics.is_chrome_tls = 0;
3084
3085
                  /* Avoid Chrome and Safari overlaps, thing that cannot happen with Firefox */
3086
70.4k
                  if(flow->protos.tls_quic.browser_heuristics.is_safari_tls)
3087
4.14k
                    flow->protos.tls_quic.browser_heuristics.is_chrome_tls = 0;
3088
3089
70.4k
                  if((flow->protos.tls_quic.browser_heuristics.is_chrome_tls == 0)
3090
63.1k
                     && duplicate_found)
3091
15.0k
                    flow->protos.tls_quic.browser_heuristics.is_safari_tls = 1; /* Safari */
3092
3093
#ifdef DEBUG_HEURISTIC
3094
                  printf("[SIGNATURE] [is_firefox_tls: %u][is_chrome_tls: %u][is_safari_tls: %u][duplicate_found: %u]\n",
3095
                         flow->protos.tls_quic.browser_heuristics.is_firefox_tls,
3096
                         flow->protos.tls_quic.browser_heuristics.is_chrome_tls,
3097
                         flow->protos.tls_quic.browser_heuristics.is_safari_tls,
3098
                         duplicate_found);
3099
#endif
3100
70.4k
    }
3101
3102
70.4k
    if(i > 0 && i >= tot_signature_algorithms_len) {
3103
56.3k
      ja.client.signature_algorithms_str[i*2 - 1] = '\0';
3104
56.3k
    } else {
3105
14.0k
      ja.client.signature_algorithms_str[i*2] = '\0';
3106
14.0k
    }
3107
3108
#ifdef DEBUG_TLS
3109
    printf("Client TLS [SIGNATURE_ALGORITHMS: %s]\n", ja.client.signature_algorithms_str);
3110
#endif
3111
554k
        } else if(extension_id == 14 /* use_srtp */) {
3112
                /* This is likely a werbrtc flow */
3113
5.22k
                if(flow->stun.maybe_dtls || flow->detected_protocol_stack[0] == NDPI_PROTOCOL_DTLS)
3114
3.85k
            flow->protos.tls_quic.webrtc = 1;
3115
#ifdef DEBUG_TLS
3116
                printf("Client TLS: use_srtp\n");
3117
#endif
3118
549k
        } else if(extension_id == 16 /* application_layer_protocol_negotiation */ &&
3119
59.7k
                  offset+extension_offset+1 < total_len) {
3120
59.5k
    u_int16_t s_offset = offset+extension_offset;
3121
59.5k
    u_int16_t tot_alpn_len = ntohs(*((u_int16_t*)&packet->payload[s_offset]));
3122
59.5k
    char alpn_str[256];
3123
59.5k
    u_int16_t alpn_str_len = 0, i;
3124
3125
#ifdef DEBUG_TLS
3126
    printf("Client TLS [ALPN: block_len=%u/len=%u]\n", extension_len, tot_alpn_len);
3127
#endif
3128
59.5k
    s_offset += 2;
3129
59.5k
    tot_alpn_len += s_offset;
3130
3131
273k
    while(s_offset < tot_alpn_len && s_offset < total_len) {
3132
228k
      u_int8_t alpn_i, alpn_len = packet->payload[s_offset++];
3133
3134
228k
      if((s_offset + alpn_len) <= tot_alpn_len &&
3135
222k
         (s_offset + alpn_len) <= total_len) {
3136
#ifdef DEBUG_TLS
3137
        printf("Client TLS [ALPN: %u]\n", alpn_len);
3138
#endif
3139
3140
216k
        if(((uint32_t)alpn_str_len+alpn_len+1) < (sizeof(alpn_str)-1)) {
3141
214k
          if(alpn_str_len > 0) {
3142
129k
      alpn_str[alpn_str_len] = ',';
3143
129k
      alpn_str_len++;
3144
129k
          }
3145
3146
1.22M
          for(alpn_i=0; alpn_i<alpn_len; alpn_i++)
3147
1.00M
      alpn_str[alpn_str_len+alpn_i] = packet->payload[s_offset+alpn_i];
3148
3149
214k
          s_offset += alpn_len, alpn_str_len += alpn_len;;
3150
214k
        } else
3151
1.80k
          break;
3152
216k
      } else
3153
12.4k
        break;
3154
228k
    } /* while */
3155
3156
59.5k
    alpn_str[alpn_str_len] = '\0';
3157
3158
#ifdef DEBUG_TLS
3159
    printf("Client TLS [ALPN: %s][len: %u]\n", alpn_str, alpn_str_len);
3160
#endif
3161
59.5k
    if(flow->protos.tls_quic.advertised_alpns == NULL) {
3162
44.9k
      flow->protos.tls_quic.advertised_alpns = ndpi_strdup(alpn_str);
3163
44.9k
      if(flow->protos.tls_quic.advertised_alpns) {
3164
44.4k
        tlsCheckUncommonALPN(ndpi_struct, flow, flow->protos.tls_quic.advertised_alpns);
3165
3166
        /* Without SNI matching we can try to sub-classify the flow via ALPN.
3167
           Note that this happens only on very rare cases, not the common ones
3168
           ("h2", "http/1.1", ...). Usefull for asymmetric traffic */
3169
44.4k
        if(!flow->protos.tls_quic.subprotocol_detected) {
3170
8.15k
          if((is_quic && ndpi_struct->cfg.quic_subclassification_enabled) ||
3171
7.24k
             (!is_quic && ndpi_struct->cfg.tls_subclassification_enabled))
3172
8.14k
                  tls_subclassify_by_alpn(ndpi_struct, flow);
3173
8.15k
        }
3174
44.4k
      }
3175
44.9k
    }
3176
3177
59.5k
                alpn_str_len = ndpi_min(sizeof(ja.client.alpn), (size_t)alpn_str_len);
3178
59.5k
    memcpy(ja.client.alpn, alpn_str, alpn_str_len);
3179
3180
    /* Store the last character of the first ALPN protocol (before any semicolon) */
3181
59.5k
    ja.client.alpn_original_last = '0';
3182
59.5k
    if(alpn_str_len > 0) {
3183
      /* Find the end of the first ALPN protocol (before semicolon or comma) */
3184
54.4k
      int first_alpn_end = 0;
3185
481k
      for(first_alpn_end = 0; first_alpn_end < alpn_str_len; first_alpn_end++) {
3186
458k
        if(ja.client.alpn[first_alpn_end] == ';' || ja.client.alpn[first_alpn_end] == ',') {
3187
31.4k
          break;
3188
31.4k
        }
3189
458k
      }
3190
54.4k
      if(first_alpn_end > 0) {
3191
54.2k
        ja.client.alpn_original_last = ja.client.alpn[first_alpn_end - 1];
3192
54.2k
      }
3193
54.4k
    }
3194
3195
59.5k
    if(alpn_str_len > 0)
3196
54.4k
      ja.client.alpn[alpn_str_len - 1] = '\0';
3197
3198
    /* Replace , with - as in JA3 */
3199
482k
    for(i=0; ja.client.alpn[i] != '\0'; i++)
3200
422k
      if(ja.client.alpn[i] == ',') ja.client.alpn[i] = '-';
3201
3202
489k
        } else if(extension_id == 43 /* supported versions */ &&
3203
42.7k
                  offset+extension_offset < total_len) {
3204
42.7k
    u_int16_t s_offset = offset+extension_offset;
3205
42.7k
    u_int8_t version_len = packet->payload[s_offset];
3206
42.7k
    char version_str[256];
3207
42.7k
    char buf_ver_tmp[16];
3208
42.7k
    size_t version_str_len = 0;
3209
42.7k
    version_str[0] = 0;
3210
#ifdef DEBUG_TLS
3211
    printf("Client TLS [TLS version len: %u]\n", version_len);
3212
#endif
3213
3214
42.7k
    if(version_len == (extension_len-1)) {
3215
41.0k
      u_int8_t j;
3216
3217
41.0k
      s_offset++;
3218
3219
      // careful not to overflow and loop forever with u_int8_t
3220
322k
      for(j=0; j+1<version_len && s_offset + j + 1 < packet->payload_packet_len; j += 2) {
3221
281k
        u_int16_t tls_version = ntohs(*((u_int16_t*)&packet->payload[s_offset+j]));
3222
281k
        u_int8_t unknown_tls_version;
3223
3224
#ifdef DEBUG_TLS
3225
        printf("Client TLS [TLS version: %s/0x%04X]\n",
3226
         ndpi_ssl_version2str(buf_ver_tmp, sizeof(buf_ver_tmp), tls_version, &unknown_tls_version), tls_version);
3227
#endif
3228
3229
281k
        if((version_str_len+8) < sizeof(version_str)) {
3230
170k
          int rc = ndpi_snprintf(&version_str[version_str_len],
3231
170k
               sizeof(version_str) - version_str_len, "%s%s",
3232
170k
               (version_str_len > 0) ? "," : "",
3233
170k
               ndpi_ssl_version2str(buf_ver_tmp, sizeof(buf_ver_tmp), tls_version, &unknown_tls_version));
3234
170k
          if(rc <= 0)
3235
0
      break;
3236
170k
          else
3237
170k
      version_str_len += rc;
3238
3239
170k
          if(ja.client.num_supported_versions < MAX_NUM_JA)
3240
165k
      ja.client.supported_versions[ja.client.num_supported_versions++] = tls_version;
3241
170k
        }
3242
281k
      }
3243
3244
#ifdef DEBUG_TLS
3245
      printf("Client TLS [SUPPORTED_VERSIONS: %s]\n", version_str);
3246
#endif
3247
3248
41.0k
      if(flow->protos.tls_quic.tls_supported_versions == NULL &&
3249
36.7k
         ndpi_struct->cfg.tls_versions_supported_enabled)
3250
36.7k
        flow->protos.tls_quic.tls_supported_versions = ndpi_strdup(version_str);
3251
41.0k
    }
3252
446k
        } else if(extension_id == 65037 /* ECH: latest drafts */) {
3253
#ifdef DEBUG_TLS
3254
    printf("Client TLS: ECH version 0x%x\n", extension_id);
3255
#endif
3256
    /* Beginning with draft-08, the version is the same as the code point
3257
       for the "encrypted_client_hello" extension. */
3258
1.78k
    flow->protos.tls_quic.encrypted_ch.version = extension_id;
3259
445k
        } else if(extension_id == 65445 || /* QUIC transport parameters (drafts version) */
3260
425k
            extension_id == 57) { /* QUIC transport parameters (final version) */
3261
23.4k
    u_int16_t s_offset = offset+extension_offset;
3262
23.4k
    uint16_t final_offset;
3263
23.4k
    int using_var_int = is_version_with_var_int_transport_params(quic_version);
3264
3265
23.4k
    if(!using_var_int) {
3266
3.10k
      if(s_offset+1 >= total_len) {
3267
232
        final_offset = 0; /* Force skipping extension */
3268
2.87k
      } else {
3269
2.87k
        u_int16_t seq_len = ntohs(*((u_int16_t*)&packet->payload[s_offset]));
3270
2.87k
        s_offset += 2;
3271
2.87k
              final_offset = ndpi_min(total_len, s_offset + seq_len);
3272
2.87k
      }
3273
20.3k
    } else {
3274
20.3k
            final_offset = ndpi_min(total_len, s_offset + extension_len);
3275
20.3k
    }
3276
3277
277k
    while(s_offset < final_offset) {
3278
257k
      u_int64_t param_type, param_len;
3279
3280
257k
                  if(!using_var_int) {
3281
12.6k
        if(s_offset+3 >= final_offset)
3282
588
          break;
3283
12.0k
        param_type = ntohs(*((u_int16_t*)&packet->payload[s_offset]));
3284
12.0k
        param_len = ntohs(*((u_int16_t*)&packet->payload[s_offset + 2]));
3285
12.0k
        s_offset += 4;
3286
244k
      } else {
3287
244k
        if(s_offset >= final_offset ||
3288
244k
           (s_offset + quic_len_buffer_still_required(packet->payload[s_offset])) >= final_offset)
3289
263
          break;
3290
244k
        s_offset += quic_len(&packet->payload[s_offset], &param_type);
3291
3292
244k
        if(s_offset >= final_offset ||
3293
243k
           (s_offset + quic_len_buffer_still_required(packet->payload[s_offset])) >= final_offset)
3294
787
          break;
3295
243k
        s_offset += quic_len(&packet->payload[s_offset], &param_len);
3296
243k
      }
3297
3298
#ifdef DEBUG_TLS
3299
      printf("Client TLS [QUIC TP: Param 0x%x Len %d]\n", (int)param_type, (int)param_len);
3300
#endif
3301
255k
      if(s_offset+param_len > final_offset)
3302
2.21k
        break;
3303
3304
253k
      s_offset += param_len;
3305
253k
    }
3306
421k
        } else if(extension_id == 21) { /* Padding */
3307
    /* Padding is usually some hundreds byte long. Longer padding
3308
       might be used as obfuscation technique to force unusual CH fragmentation */
3309
14.7k
    if(extension_len > 500 /* Arbitrary value */) {
3310
#ifdef DEBUG_TLS
3311
      printf("Padding length: %d\n", extension_len);
3312
#endif
3313
47
      ndpi_set_risk(ndpi_struct, flow, NDPI_OBFUSCATED_TRAFFIC, "Abnormal Client Hello/Padding length");
3314
47
    }
3315
406k
        } else if(extension_id == 22) { /* Encrypt-then-MAC */
3316
6.84k
    if(extension_len == 0) {
3317
6.36k
      char *sni = flow->host_server_name;
3318
3319
6.36k
      if(sni != NULL) {
3320
6.36k
        u_int sni_len = strlen(sni);
3321
3322
6.36k
        if((flow->protos.tls_quic.advertised_alpns == NULL) /* No ALPN */
3323
5.10k
           && (sni_len > 8)
3324
3.49k
           && ((strcmp(&sni[sni_len-4], ".com") == 0) || (strcmp(&sni[sni_len-4], ".net") == 0))
3325
2.82k
           && (strncmp(sni, "www.", 4) == 0) /* Starting with www.... */
3326
1.83k
           && str_contains_digit(&sni[4])) {
3327
1.52k
          ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_TOR, ndpi_get_master_proto(ndpi_struct, flow), NDPI_CONFIDENCE_DPI);
3328
1.52k
        }
3329
6.36k
      }
3330
6.36k
    }
3331
400k
        } else if(extension_id == 51 &&  /* key_share */
3332
35.5k
                  offset + extension_offset < total_len) {
3333
35.5k
    u_int32_t extn_offset        = extn_off + 4;
3334
35.5k
    u_int16_t extn_end           = extn_offset + extension_len;
3335
3336
35.5k
    if(extn_offset + extension_len <= total_len) {
3337
#ifdef DEBUG_TLS
3338
                  u_int16_t key_share_extn_len = ntohs(*((u_int16_t*)&(packet->payload[extn_offset])));
3339
3340
                  printf("[key_share] [len=%u][key_share_extn_len: %u][%02X %02X]\n",
3341
                         extension_len, key_share_extn_len,
3342
                         (packet->payload[extn_offset] & 0xFF),
3343
                         (packet->payload[extn_offset+1] & 0xFF));
3344
#endif
3345
3346
35.4k
                  extn_offset += 2;
3347
3348
84.4k
                  while(extn_offset + 4 < extn_end) {
3349
48.9k
                    u_int16_t group_id     = ntohs(*((u_int16_t*)&(packet->payload[extn_offset])));
3350
48.9k
                    u_int16_t key_extn_len = ntohs(*((u_int16_t*)&(packet->payload[extn_offset + 2])));
3351
3352
  #ifdef DEBUG_TLS
3353
                    printf("\t[%02X %02X][extn_offset: %u][group_id: %u][key_extn_len: %u]\n",
3354
                           (packet->payload[extn_offset] & 0xFF),
3355
                           (packet->payload[extn_offset+1] & 0xFF),
3356
                           extn_offset,
3357
                           group_id, key_extn_len);
3358
  #endif
3359
3360
48.9k
                    switch(group_id) {
3361
141
                    case 0x11EC: /* X25519MLKEM768 */
3362
141
                      flow->protos.tls_quic.pq_key_share = 1;
3363
141
                      break;
3364
48.9k
                    }
3365
3366
48.9k
                    extn_offset += key_extn_len + 4;
3367
48.9k
                  }
3368
35.4k
    }
3369
3370
#ifdef DEBUG_TLS
3371
    printf("[extn_offset: %u][extn_end: %u]\n", extn_offset, extn_end);
3372
#endif
3373
35.5k
        }
3374
3375
955k
        extension_offset += extension_len; /* Move to the next extension */
3376
3377
#ifdef DEBUG_TLS
3378
        printf("Client TLS [extension_offset/len: %u/%u]\n", extension_offset, extension_len);
3379
#endif
3380
955k
      } /* while */
3381
3382
95.9k
      if(!invalid_ja) {
3383
        /* Compute JA4 client */
3384
3385
88.4k
compute_ja4c:
3386
88.4k
        if(ndpi_struct->cfg.tls_ja4c_fingerprint_enabled) {
3387
88.4k
          ndpi_compute_ja4(ndpi_struct, flow, quic_version, &ja);
3388
3389
88.4k
    if(ndpi_struct->ja4_custom_protos != NULL) {
3390
85.8k
      u_int64_t proto_id;
3391
3392
      /* This protocol has been defined in protos.txt-like files */
3393
85.8k
      if(ndpi_hash_find_entry(ndpi_struct->ja4_custom_protos,
3394
85.8k
            flow->protos.tls_quic.ja4_client,
3395
85.8k
            NDPI_ARRAY_LENGTH(flow->protos.tls_quic.ja4_client) - 1,
3396
85.8k
            &proto_id) == 0) {
3397
0
        ndpi_set_detected_protocol(ndpi_struct, flow, proto_id,
3398
0
                 ndpi_get_master_proto(ndpi_struct, flow),
3399
0
                 NDPI_CONFIDENCE_CUSTOM_RULE);
3400
0
      }
3401
85.8k
    }
3402
3403
88.4k
                if(ndpi_struct->malicious_ja4_hashmap != NULL) {
3404
85.8k
                  u_int16_t rc1 = ndpi_hash_find_entry(ndpi_struct->malicious_ja4_hashmap,
3405
85.8k
                                                       flow->protos.tls_quic.ja4_client,
3406
85.8k
                                                       NDPI_ARRAY_LENGTH(flow->protos.tls_quic.ja4_client) - 1,
3407
85.8k
                                                       NULL);
3408
3409
85.8k
                  if(rc1 == 0)
3410
24
                    ndpi_set_risk(ndpi_struct, flow, NDPI_MALICIOUS_FINGERPRINT, flow->protos.tls_quic.ja4_client);
3411
85.8k
                }
3412
88.4k
        }
3413
        /* End JA4 */
3414
88.4k
      }
3415
3416
      /* Before returning to the caller we need to make a final check */
3417
97.8k
      if((flow->protos.tls_quic.ssl_version >= 0x0303) /* >= TLSv1.2 */
3418
80.5k
         && !flow->protos.tls_quic.webrtc
3419
76.2k
         && (flow->protos.tls_quic.advertised_alpns == NULL) /* No ALPN */) {
3420
27.0k
        ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_NOT_CARRYING_HTTPS, "No ALPN");
3421
27.0k
      }
3422
3423
      /* Add check for missing SNI */
3424
97.8k
      if(flow->host_server_name[0] == '\0'
3425
38.2k
         && (flow->protos.tls_quic.ssl_version >= 0x0302) /* TLSv1.1 */
3426
27.3k
         && !flow->protos.tls_quic.webrtc
3427
97.8k
         ) {
3428
        /* This is a bit suspicious */
3429
23.6k
        ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_MISSING_SNI, "SNI should always be present");
3430
3431
23.6k
        if(flow->protos.tls_quic.advertised_alpns != NULL) {
3432
8.60k
    char buf[256], *tmp, *item;
3433
3434
8.60k
    snprintf(buf, sizeof(buf), "%s", flow->protos.tls_quic.advertised_alpns);
3435
3436
8.60k
    item = strtok_r(buf, ",", &tmp);
3437
3438
15.9k
    while(item != NULL) {
3439
8.65k
      if(item[0] == 'h') {
3440
        /* Example 'h2' */
3441
1.32k
        ndpi_set_risk(ndpi_struct, flow, NDPI_TLS_ALPN_SNI_MISMATCH, item);
3442
1.32k
        break;
3443
1.32k
      } else
3444
7.33k
        item = strtok_r(NULL, ",", &tmp);
3445
8.65k
    }
3446
8.60k
        }
3447
23.6k
      }
3448
3449
97.8k
      return(2 /* Client Certificate */);
3450
95.9k
    } else {
3451
#ifdef DEBUG_TLS
3452
      printf("[TLS] Client: too short [%u vs %u]\n",
3453
       (extensions_len+offset), total_len);
3454
#endif
3455
3.47k
    }
3456
99.4k
  } else if(offset == total_len) {
3457
    /* TLS does not have extensions etc */
3458
1.87k
    goto compute_ja4c;
3459
1.87k
  }
3460
103k
      } else {
3461
#ifdef DEBUG_TLS
3462
  printf("[JA3] Client: invalid length detected\n");
3463
#endif
3464
4.96k
      }
3465
108k
    }
3466
179k
  }
3467
3468
78.6k
  return(0); /* Not found */
3469
188k
}
3470
3471
/* **************************************** */
3472
3473
static void ndpi_search_tls_wrapper(struct ndpi_detection_module_struct *ndpi_struct,
3474
3.82M
            struct ndpi_flow_struct *flow) {
3475
3.82M
  struct ndpi_packet_struct *packet = &ndpi_struct->packet;
3476
3.82M
  int rc = 0;
3477
3478
#ifdef DEBUG_TLS
3479
  printf("==>> %s() [len: %u][version: %u]\n",
3480
   __FUNCTION__,
3481
   packet->payload_packet_len,
3482
   flow->protos.tls_quic.ssl_version);
3483
#endif
3484
3485
  /* It is not easy to handle "standard" TLS/DTLS detection and (plain) obfuscated
3486
     heuristic at the SAME time. Use a trivial logic: switch to heuristic
3487
     code only if the standard functions fail */
3488
3489
  /* We might be in extra-dissection data-path here (if we have been
3490
     called from STUN or from Mails/FTP/...), but plain obfuscated heuristic
3491
     is always checked in "standard" data-path! */
3492
3493
3.82M
  if(flow->tls_quic.obfuscated_heur_state == NULL) {
3494
3.20M
    if(packet->udp != NULL || flow->stun.maybe_dtls)
3495
759k
      rc = ndpi_search_dtls(ndpi_struct, flow);
3496
2.44M
    else
3497
2.44M
      rc = ndpi_search_tls_tcp(ndpi_struct, flow);
3498
3499
     /* We should check for this TLS heuristic if:
3500
      * the feature is enabled
3501
      * this flow doesn't seem a real TLS/DTLS one
3502
      * we are not here from STUN code or from opportunistic tls path (mails/ftp)
3503
      * with TCP, we got the 3WHS (so that we can process the beginning of the flow)
3504
      */
3505
3.20M
    if(rc == 0 &&
3506
3.02M
       (ndpi_struct->cfg.tls_heuristics & NDPI_HEURISTICS_TLS_OBFUSCATED_PLAIN) &&
3507
2.94M
       flow->stun.maybe_dtls == 0 &&
3508
2.92M
       flow->tls_quic.from_opportunistic_tls == 0 &&
3509
2.92M
       ((flow->l4_proto == IPPROTO_TCP && ndpi_seen_flow_beginning(flow)) ||
3510
2.91M
        flow->l4_proto == IPPROTO_UDP) &&
3511
708k
       !is_flow_addr_informative(flow) /* The proxy server is likely hosted on some cloud providers */ ) {
3512
631k
      flow->tls_quic.obfuscated_heur_state = ndpi_calloc(1, sizeof(struct tls_obfuscated_heuristic_state));
3513
631k
    }
3514
3.20M
  }
3515
3516
3.82M
  if(flow->tls_quic.obfuscated_heur_state) {
3517
1.24M
    tls_obfuscated_heur_search_again(ndpi_struct, flow);
3518
2.57M
  } else if(rc == 0) {
3519
2.39M
    NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow);
3520
2.39M
  }
3521
3.82M
}
3522
3523
/* **************************************** */
3524
3525
10.9k
void init_tls_dissector(struct ndpi_detection_module_struct *ndpi_struct) {
3526
10.9k
  register_dissector("(D)TLS", ndpi_struct,
3527
10.9k
                     ndpi_search_tls_wrapper,
3528
10.9k
                     NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_OR_UDP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION,
3529
10.9k
                     2,
3530
10.9k
                     NDPI_PROTOCOL_TLS,
3531
10.9k
                     NDPI_PROTOCOL_DTLS);
3532
10.9k
}