-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-layer-ssl.c
3392 lines (2903 loc) · 120 KB
/
app-layer-ssl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2007-2024 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \file
*
* \author Anoop Saldanha <anoopsaldanha@gmail.com>
* \author Pierre Chifflier <pierre.chifflier@ssi.gouv.fr>
* \author Mats Klepsland <mats.klepsland@gmail.com>
*
*/
#include "suricata-common.h"
#include "decode.h"
#include "app-layer.h"
#include "app-layer-detect-proto.h"
#include "app-layer-protos.h"
#include "app-layer-parser.h"
#include "app-layer-frames.h"
#include "app-layer-ssl.h"
#include "conf.h"
#include "feature.h"
#include "util-debug.h"
#include "util-ja3.h"
#include "util-enum.h"
#include "util-validate.h"
SCEnumCharMap tls_frame_table[] = {
{
"pdu",
TLS_FRAME_PDU,
},
{
"hdr",
TLS_FRAME_HDR,
},
{
"data",
TLS_FRAME_DATA,
},
{
"alert",
TLS_FRAME_ALERT_DATA,
},
{
"heartbeat",
TLS_FRAME_HB_DATA,
},
{
"ssl2.hdr",
TLS_FRAME_SSLV2_HDR,
},
{
"ssl2.pdu",
TLS_FRAME_SSLV2_PDU,
},
{ NULL, -1 },
};
SCEnumCharMap tls_decoder_event_table[] = {
/* TLS protocol messages */
{ "INVALID_SSLV2_HEADER", TLS_DECODER_EVENT_INVALID_SSLV2_HEADER },
{ "INVALID_TLS_HEADER", TLS_DECODER_EVENT_INVALID_TLS_HEADER },
{ "INVALID_RECORD_VERSION", TLS_DECODER_EVENT_INVALID_RECORD_VERSION },
{ "INVALID_RECORD_TYPE", TLS_DECODER_EVENT_INVALID_RECORD_TYPE },
{ "INVALID_RECORD_LENGTH", TLS_DECODER_EVENT_INVALID_RECORD_LENGTH },
{ "INVALID_HANDSHAKE_MESSAGE", TLS_DECODER_EVENT_INVALID_HANDSHAKE_MESSAGE },
{ "HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_HEARTBEAT },
{ "INVALID_HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_INVALID_HEARTBEAT },
{ "OVERFLOW_HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_OVERFLOW_HEARTBEAT },
{ "DATALEAK_HEARTBEAT_MISMATCH", TLS_DECODER_EVENT_DATALEAK_HEARTBEAT_MISMATCH },
{ "HANDSHAKE_INVALID_LENGTH", TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH },
{ "MULTIPLE_SNI_EXTENSIONS", TLS_DECODER_EVENT_MULTIPLE_SNI_EXTENSIONS },
{ "INVALID_SNI_TYPE", TLS_DECODER_EVENT_INVALID_SNI_TYPE },
{ "INVALID_SNI_LENGTH", TLS_DECODER_EVENT_INVALID_SNI_LENGTH },
{ "TOO_MANY_RECORDS_IN_PACKET", TLS_DECODER_EVENT_TOO_MANY_RECORDS_IN_PACKET },
/* certificate decoding messages */
{ "INVALID_CERTIFICATE", TLS_DECODER_EVENT_INVALID_CERTIFICATE },
{ "CERTIFICATE_INVALID_LENGTH", TLS_DECODER_EVENT_CERTIFICATE_INVALID_LENGTH },
{ "CERTIFICATE_INVALID_VERSION", TLS_DECODER_EVENT_CERTIFICATE_INVALID_VERSION },
{ "CERTIFICATE_INVALID_SERIAL", TLS_DECODER_EVENT_CERTIFICATE_INVALID_SERIAL },
{ "CERTIFICATE_INVALID_ALGORITHMIDENTIFIER",
TLS_DECODER_EVENT_CERTIFICATE_INVALID_ALGORITHMIDENTIFIER },
{ "CERTIFICATE_INVALID_X509NAME", TLS_DECODER_EVENT_CERTIFICATE_INVALID_X509NAME },
{ "CERTIFICATE_INVALID_DATE", TLS_DECODER_EVENT_CERTIFICATE_INVALID_DATE },
{ "CERTIFICATE_INVALID_EXTENSIONS", TLS_DECODER_EVENT_CERTIFICATE_INVALID_EXTENSIONS },
{ "CERTIFICATE_INVALID_DER", TLS_DECODER_EVENT_CERTIFICATE_INVALID_DER },
{ "CERTIFICATE_INVALID_SUBJECT", TLS_DECODER_EVENT_CERTIFICATE_INVALID_SUBJECT },
{ "CERTIFICATE_INVALID_ISSUER", TLS_DECODER_EVENT_CERTIFICATE_INVALID_ISSUER },
{ "CERTIFICATE_INVALID_VALIDITY", TLS_DECODER_EVENT_CERTIFICATE_INVALID_VALIDITY },
{ "ERROR_MESSAGE_ENCOUNTERED", TLS_DECODER_EVENT_ERROR_MSG_ENCOUNTERED },
/* used as a generic error event */
{ "INVALID_SSL_RECORD", TLS_DECODER_EVENT_INVALID_SSL_RECORD },
{ NULL, -1 },
};
enum {
/* X.509 error codes, returned by decoder
* THESE CONSTANTS MUST MATCH rust/src/x509/mod.rs ! */
ERR_INVALID_CERTIFICATE=1,
ERR_INVALID_LENGTH,
ERR_INVALID_VERSION,
ERR_INVALID_SERIAL,
ERR_INVALID_ALGORITHMIDENTIFIER,
ERR_INVALID_X509NAME,
ERR_INVALID_DATE,
ERR_INVALID_EXTENSIONS,
ERR_INVALID_DER,
/* error getting data */
ERR_EXTRACT_SUBJECT,
ERR_EXTRACT_ISSUER,
ERR_EXTRACT_VALIDITY,
};
/* JA3 and JA4 fingerprints are disabled by default */
#define SSL_CONFIG_DEFAULT_JA3 0
#define SSL_CONFIG_DEFAULT_JA4 0
enum SslConfigEncryptHandling {
SSL_CNF_ENC_HANDLE_DEFAULT = 0, /**< disable raw content, continue tracking */
SSL_CNF_ENC_HANDLE_BYPASS = 1, /**< skip processing of flow, bypass if possible */
SSL_CNF_ENC_HANDLE_FULL = 2, /**< handle fully like any other proto */
};
typedef struct SslConfig_ {
enum SslConfigEncryptHandling encrypt_mode;
/** dynamic setting for ja3 and ja4: can be enabled on demand if not
* explicitly disabled. */
SC_ATOMIC_DECLARE(int, enable_ja3);
bool disable_ja3; /**< ja3 explicitly disabled. Don't enable on demand. */
SC_ATOMIC_DECLARE(int, enable_ja4);
bool disable_ja4; /**< ja4 explicitly disabled. Don't enable on demand. */
} SslConfig;
SslConfig ssl_config;
/* SSLv3 record types */
#define SSLV3_CHANGE_CIPHER_SPEC 20
#define SSLV3_ALERT_PROTOCOL 21
#define SSLV3_HANDSHAKE_PROTOCOL 22
#define SSLV3_APPLICATION_PROTOCOL 23
#define SSLV3_HEARTBEAT_PROTOCOL 24
/* SSLv3 handshake protocol types */
#define SSLV3_HS_HELLO_REQUEST 0
#define SSLV3_HS_CLIENT_HELLO 1
#define SSLV3_HS_SERVER_HELLO 2
#define SSLV3_HS_NEW_SESSION_TICKET 4
#define SSLV3_HS_CERTIFICATE 11
#define SSLV3_HS_SERVER_KEY_EXCHANGE 12
#define SSLV3_HS_CERTIFICATE_REQUEST 13
#define SSLV3_HS_SERVER_HELLO_DONE 14
#define SSLV3_HS_CERTIFICATE_VERIFY 15
#define SSLV3_HS_CLIENT_KEY_EXCHANGE 16
#define SSLV3_HS_FINISHED 20
#define SSLV3_HS_CERTIFICATE_URL 21
#define SSLV3_HS_CERTIFICATE_STATUS 22
/* SSLv2 protocol message types */
#define SSLV2_MT_ERROR 0
#define SSLV2_MT_CLIENT_HELLO 1
#define SSLV2_MT_CLIENT_MASTER_KEY 2
#define SSLV2_MT_CLIENT_FINISHED 3
#define SSLV2_MT_SERVER_HELLO 4
#define SSLV2_MT_SERVER_VERIFY 5
#define SSLV2_MT_SERVER_FINISHED 6
#define SSLV2_MT_REQUEST_CERTIFICATE 7
#define SSLV2_MT_CLIENT_CERTIFICATE 8
#define SSLV3_RECORD_HDR_LEN 5
/** max length according to RFC 5246 6.2.2 is 2^14 + 1024 */
#define SSLV3_RECORD_MAX_LEN ((1 << 14) + 1024)
#define SSLV3_CLIENT_HELLO_VERSION_LEN 2
#define SSLV3_CLIENT_HELLO_RANDOM_LEN 32
/* TLS heartbeat protocol types */
#define TLS_HB_REQUEST 1
#define TLS_HB_RESPONSE 2
#define SSL_RECORD_MINIMUM_LENGTH 6
#define SHA1_STRING_LENGTH 60
#define HAS_SPACE(n) ((uint64_t)(input - initial_input) + (uint64_t)(n) <= (uint64_t)(input_len))
struct SSLDecoderResult {
int retval; // nr bytes consumed from input, or < 0 on error
uint32_t needed; // more bytes needed
};
#define SSL_DECODER_ERROR(e) \
(struct SSLDecoderResult) \
{ \
(e), 0 \
}
#define SSL_DECODER_OK(c) \
(struct SSLDecoderResult) \
{ \
(uint32_t)(c), 0 \
}
#define SSL_DECODER_INCOMPLETE(c, n) \
(struct SSLDecoderResult) \
{ \
(uint32_t)(c), (n) \
}
static inline int SafeMemcpy(void *dst, size_t dst_offset, size_t dst_size,
const void *src, size_t src_offset, size_t src_size, size_t src_tocopy) WARN_UNUSED;
static inline int SafeMemcpy(void *dst, size_t dst_offset, size_t dst_size,
const void *src, size_t src_offset, size_t src_size, size_t src_tocopy)
{
DEBUG_VALIDATE_BUG_ON(dst_offset >= dst_size);
DEBUG_VALIDATE_BUG_ON(src_offset >= src_size);
DEBUG_VALIDATE_BUG_ON(src_tocopy > (src_size - src_offset));
DEBUG_VALIDATE_BUG_ON(src_tocopy > (dst_size - dst_offset));
if (dst_offset < dst_size && src_offset < src_size &&
src_tocopy <= (src_size - src_offset) &&
src_tocopy <= (dst_size - dst_offset)) {
memcpy(dst + dst_offset, src + src_offset, src_tocopy);
return 0;
}
return -1;
}
#ifdef DEBUG_VALIDATION
#define ValidateRecordState(connp) \
do { \
DEBUG_VALIDATE_BUG_ON(((connp)->record_length + SSLV3_RECORD_HDR_LEN) < \
(connp)->bytes_processed); \
} while(0);
#else
#define ValidateRecordState(...)
#endif
#define SSLParserHSReset(connp) \
do { \
(connp)->handshake_type = 0; \
(connp)->message_length = 0; \
} while (0)
#define SSLParserReset(state) \
do { \
SCLogDebug("resetting state"); \
(state)->curr_connp->bytes_processed = 0; \
SSLParserHSReset((state)->curr_connp); \
} while(0)
#define SSLSetEvent(ssl_state, event) \
do { \
SCLogDebug("setting event %u", (event)); \
if ((ssl_state) == NULL) { \
SCLogDebug("could not set decoder event %u", event); \
} else { \
AppLayerDecoderEventsSetEventRaw(&(ssl_state)->tx_data.events, (event)); \
(ssl_state)->events++; \
} \
} while (0)
static void SSLStateCertSANFree(SSLStateConnp *connp);
static void *SSLGetTx(void *state, uint64_t tx_id)
{
SSLState *ssl_state = (SSLState *)state;
return ssl_state;
}
static uint64_t SSLGetTxCnt(void *state)
{
/* single tx */
return 1;
}
static int SSLGetAlstateProgress(void *tx, uint8_t direction)
{
SSLState *ssl_state = (SSLState *)tx;
/* we don't care about direction, only that app-layer parser is done
and have sent an EOF */
if (ssl_state->flags & SSL_AL_FLAG_STATE_FINISHED) {
return TLS_STATE_FINISHED;
}
/* we want the logger to log when the handshake is done, even if the
state is not finished */
if (ssl_state->flags & SSL_AL_FLAG_HANDSHAKE_DONE) {
return TLS_HANDSHAKE_DONE;
}
if (direction == STREAM_TOSERVER &&
(ssl_state->server_connp.cert0_subject != NULL ||
ssl_state->server_connp.cert0_issuerdn != NULL))
{
return TLS_STATE_CERT_READY;
}
return TLS_STATE_IN_PROGRESS;
}
static AppLayerTxData *SSLGetTxData(void *vtx)
{
SSLState *ssl_state = (SSLState *)vtx;
return &ssl_state->tx_data;
}
static AppLayerStateData *SSLGetStateData(void *vstate)
{
SSLState *ssl_state = (SSLState *)vstate;
return &ssl_state->state_data;
}
void SSLVersionToString(uint16_t version, char *buffer)
{
buffer[0] = '\0';
switch (version) {
case TLS_VERSION_UNKNOWN:
strlcat(buffer, "UNDETERMINED", 13);
break;
case SSL_VERSION_2:
strlcat(buffer, "SSLv2", 6);
break;
case SSL_VERSION_3:
strlcat(buffer, "SSLv3", 6);
break;
case TLS_VERSION_10:
strlcat(buffer, "TLSv1", 6);
break;
case TLS_VERSION_11:
strlcat(buffer, "TLS 1.1", 8);
break;
case TLS_VERSION_12:
strlcat(buffer, "TLS 1.2", 8);
break;
case TLS_VERSION_13:
strlcat(buffer, "TLS 1.3", 8);
break;
case TLS_VERSION_13_DRAFT28:
strlcat(buffer, "TLS 1.3 draft-28", 17);
break;
case TLS_VERSION_13_DRAFT27:
strlcat(buffer, "TLS 1.3 draft-27", 17);
break;
case TLS_VERSION_13_DRAFT26:
strlcat(buffer, "TLS 1.3 draft-26", 17);
break;
case TLS_VERSION_13_DRAFT25:
strlcat(buffer, "TLS 1.3 draft-25", 17);
break;
case TLS_VERSION_13_DRAFT24:
strlcat(buffer, "TLS 1.3 draft-24", 17);
break;
case TLS_VERSION_13_DRAFT23:
strlcat(buffer, "TLS 1.3 draft-23", 17);
break;
case TLS_VERSION_13_DRAFT22:
strlcat(buffer, "TLS 1.3 draft-22", 17);
break;
case TLS_VERSION_13_DRAFT21:
strlcat(buffer, "TLS 1.3 draft-21", 17);
break;
case TLS_VERSION_13_DRAFT20:
strlcat(buffer, "TLS 1.3 draft-20", 17);
break;
case TLS_VERSION_13_DRAFT19:
strlcat(buffer, "TLS 1.3 draft-19", 17);
break;
case TLS_VERSION_13_DRAFT18:
strlcat(buffer, "TLS 1.3 draft-18", 17);
break;
case TLS_VERSION_13_DRAFT17:
strlcat(buffer, "TLS 1.3 draft-17", 17);
break;
case TLS_VERSION_13_DRAFT16:
strlcat(buffer, "TLS 1.3 draft-16", 17);
break;
case TLS_VERSION_13_PRE_DRAFT16:
strlcat(buffer, "TLS 1.3 draft-<16", 18);
break;
case TLS_VERSION_13_DRAFT20_FB:
strlcat(buffer, "TLS 1.3 draft-20-fb", 20);
break;
case TLS_VERSION_13_DRAFT21_FB:
strlcat(buffer, "TLS 1.3 draft-21-fb", 20);
break;
case TLS_VERSION_13_DRAFT22_FB:
strlcat(buffer, "TLS 1.3 draft-22-fb", 20);
break;
case TLS_VERSION_13_DRAFT23_FB:
strlcat(buffer, "TLS 1.3 draft-23-fb", 20);
break;
case TLS_VERSION_13_DRAFT26_FB:
strlcat(buffer, "TLS 1.3 draft-26-fb", 20);
break;
default:
snprintf(buffer, 7, "0x%04x", version);
break;
}
}
static void TlsDecodeHSCertificateErrSetEvent(SSLState *ssl_state, uint32_t err)
{
switch(err) {
case ERR_EXTRACT_VALIDITY:
SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_VALIDITY);
break;
case ERR_EXTRACT_ISSUER:
SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_ISSUER);
break;
case ERR_EXTRACT_SUBJECT:
SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_SUBJECT);
break;
case ERR_INVALID_DER:
SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_DER);
break;
case ERR_INVALID_EXTENSIONS:
SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_EXTENSIONS);
break;
case ERR_INVALID_DATE:
SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_DATE);
break;
case ERR_INVALID_X509NAME:
SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_X509NAME);
break;
case ERR_INVALID_ALGORITHMIDENTIFIER:
SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_ALGORITHMIDENTIFIER);
break;
case ERR_INVALID_SERIAL:
SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_SERIAL);
break;
case ERR_INVALID_VERSION:
SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_VERSION);
break;
case ERR_INVALID_LENGTH:
SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_LENGTH);
break;
case ERR_INVALID_CERTIFICATE:
default:
SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_CERTIFICATE);
break;
}
}
static inline int TlsDecodeHSCertificateFingerprint(
SSLStateConnp *connp, const uint8_t *input, uint32_t cert_len)
{
if (unlikely(connp->cert0_fingerprint != NULL))
return 0;
connp->cert0_fingerprint = SCCalloc(1, SHA1_STRING_LENGTH);
if (connp->cert0_fingerprint == NULL)
return -1;
uint8_t hash[SC_SHA1_LEN];
if (SCSha1HashBuffer(input, cert_len, hash, sizeof(hash)) == 1) {
rs_to_hex_sep(
(uint8_t *)connp->cert0_fingerprint, SHA1_STRING_LENGTH, ':', hash, SC_SHA1_LEN);
}
return 0;
}
static inline int TlsDecodeHSCertificateAddCertToChain(
SSLStateConnp *connp, const uint8_t *input, uint32_t cert_len)
{
SSLCertsChain *cert = SCCalloc(1, sizeof(SSLCertsChain));
if (cert == NULL)
return -1;
cert->cert_data = (uint8_t *)input;
cert->cert_len = cert_len;
TAILQ_INSERT_TAIL(&connp->certs, cert, next);
return 0;
}
static int TlsDecodeHSCertificate(SSLState *ssl_state, SSLStateConnp *connp,
const uint8_t *const initial_input, const uint32_t input_len, const int certn)
{
const uint8_t *input = (uint8_t *)initial_input;
uint32_t err_code = 0;
X509 *x509 = NULL;
int rc = 0;
if (!(HAS_SPACE(3)))
goto invalid_cert;
uint32_t cert_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
input += 3;
if (!(HAS_SPACE(cert_len)))
goto invalid_cert;
/* only store fields from the first certificate in the chain */
if (certn == 0 && connp->cert0_subject == NULL && connp->cert0_issuerdn == NULL &&
connp->cert0_serial == NULL) {
x509 = rs_x509_decode(input, cert_len, &err_code);
if (x509 == NULL) {
TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
goto next;
}
char *str = rs_x509_get_subject(x509);
if (str == NULL) {
err_code = ERR_EXTRACT_SUBJECT;
goto error;
}
connp->cert0_subject = str;
str = rs_x509_get_issuer(x509);
if (str == NULL) {
err_code = ERR_EXTRACT_ISSUER;
goto error;
}
connp->cert0_issuerdn = str;
connp->cert0_sans_len = rs_x509_get_subjectaltname_len(x509);
char **sans = SCCalloc(connp->cert0_sans_len, sizeof(char *));
if (sans == NULL) {
goto error;
}
for (uint16_t i = 0; i < connp->cert0_sans_len; i++) {
sans[i] = rs_x509_get_subjectaltname_at(x509, i);
}
connp->cert0_sans = sans;
str = rs_x509_get_serial(x509);
if (str == NULL) {
err_code = ERR_INVALID_SERIAL;
goto error;
}
connp->cert0_serial = str;
rc = rs_x509_get_validity(x509, &connp->cert0_not_before, &connp->cert0_not_after);
if (rc != 0) {
err_code = ERR_EXTRACT_VALIDITY;
goto error;
}
rs_x509_free(x509);
x509 = NULL;
rc = TlsDecodeHSCertificateFingerprint(connp, input, cert_len);
if (rc != 0) {
SCLogDebug("TlsDecodeHSCertificateFingerprint failed with %d", rc);
goto error;
}
}
rc = TlsDecodeHSCertificateAddCertToChain(connp, input, cert_len);
if (rc != 0) {
SCLogDebug("TlsDecodeHSCertificateAddCertToChain failed with %d", rc);
goto error;
}
next:
input += cert_len;
return (int)(input - initial_input);
error:
if (err_code != 0)
TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
if (x509 != NULL)
rs_x509_free(x509);
SSLStateCertSANFree(connp);
return -1;
invalid_cert:
SCLogDebug("TLS invalid certificate");
SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_CERTIFICATE);
return -1;
}
/** \internal
* \brief parse cert data in a certificate handshake message
* will be called with all data.
* \retval consumed bytes consumed or -1 on error
*/
static int TlsDecodeHSCertificates(SSLState *ssl_state, SSLStateConnp *connp,
const uint8_t *const initial_input, const uint32_t input_len)
{
const uint8_t *input = (uint8_t *)initial_input;
if (!(HAS_SPACE(3)))
return -1;
const uint32_t cert_chain_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
input += 3;
if (!(HAS_SPACE(cert_chain_len)))
return -1;
if (connp->certs_buffer != NULL) {
// TODO should we set an event here?
return -1;
}
connp->certs_buffer = SCCalloc(1, cert_chain_len);
if (connp->certs_buffer == NULL) {
return -1;
}
connp->certs_buffer_size = cert_chain_len;
memcpy(connp->certs_buffer, input, cert_chain_len);
int cert_cnt = 0;
uint32_t processed_len = 0;
/* coverity[tainted_data] */
while (processed_len < cert_chain_len) {
int rc = TlsDecodeHSCertificate(ssl_state, connp, connp->certs_buffer + processed_len,
connp->certs_buffer_size - processed_len, cert_cnt);
if (rc <= 0) { // 0 should be impossible, but lets be defensive
return -1;
}
DEBUG_VALIDATE_BUG_ON(processed_len + (uint32_t)rc > cert_chain_len);
if (processed_len + (uint32_t)rc > cert_chain_len) {
return -1;
}
processed_len += (uint32_t)rc;
}
return processed_len + 3;
}
/**
* \inline
* \brief Check if value is GREASE.
*
* http://tools.ietf.org/html/draft-davidben-tls-grease-00
*
* \param value Value to check.
*
* \retval 1 if is GREASE.
* \retval 0 if not is GREASE.
*/
static inline int TLSDecodeValueIsGREASE(const uint16_t value)
{
switch (value)
{
case 0x0a0a:
case 0x1a1a:
case 0x2a2a:
case 0x3a3a:
case 0x4a4a:
case 0x5a5a:
case 0x6a6a:
case 0x7a7a:
case 0x8a8a:
case 0x9a9a:
case 0xaaaa:
case 0xbaba:
case 0xcaca:
case 0xdada:
case 0xeaea:
case 0xfafa:
return 1;
default:
return 0;
}
}
static inline int TLSDecodeHSHelloVersion(SSLState *ssl_state,
const uint8_t * const initial_input,
const uint32_t input_len)
{
uint8_t *input = (uint8_t *)initial_input;
if (!(HAS_SPACE(SSLV3_CLIENT_HELLO_VERSION_LEN))) {
SCLogDebug("TLS handshake invalid length");
SSLSetEvent(ssl_state,
TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
return -1;
}
uint16_t version = (uint16_t)(*input << 8) | *(input + 1);
ssl_state->curr_connp->version = version;
if (ssl_state->curr_connp->ja4 != NULL &&
ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
SCJA4SetTLSVersion(ssl_state->curr_connp->ja4, version);
}
/* TLSv1.3 draft1 to draft21 use the version field as earlier TLS
versions, instead of using the supported versions extension. */
if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
((ssl_state->curr_connp->version == TLS_VERSION_13) ||
(((ssl_state->curr_connp->version >> 8) & 0xff) == 0x7f))) {
ssl_state->flags |= SSL_AL_FLAG_LOG_WITHOUT_CERT;
}
/* Catch some early TLSv1.3 draft implementations that does not conform
to the draft version. */
if ((ssl_state->curr_connp->version >= 0x7f01) &&
(ssl_state->curr_connp->version < 0x7f10)) {
ssl_state->curr_connp->version = TLS_VERSION_13_PRE_DRAFT16;
}
/* TLSv1.3 drafts from draft1 to draft15 use 0x0304 (TLSv1.3) as the
version number, which makes it hard to accurately pinpoint the
exact draft version. */
else if (ssl_state->curr_connp->version == TLS_VERSION_13) {
ssl_state->curr_connp->version = TLS_VERSION_13_PRE_DRAFT16;
}
if (SC_ATOMIC_GET(ssl_config.enable_ja3) && ssl_state->curr_connp->ja3_str == NULL) {
ssl_state->curr_connp->ja3_str = Ja3BufferInit();
if (ssl_state->curr_connp->ja3_str == NULL)
return -1;
int rc = Ja3BufferAddValue(&ssl_state->curr_connp->ja3_str, version);
if (rc != 0)
return -1;
}
input += SSLV3_CLIENT_HELLO_VERSION_LEN;
return (int)(input - initial_input);
}
static inline int TLSDecodeHSHelloRandom(SSLState *ssl_state,
const uint8_t * const initial_input,
const uint32_t input_len)
{
uint8_t *input = (uint8_t *)initial_input;
if (!(HAS_SPACE(SSLV3_CLIENT_HELLO_RANDOM_LEN))) {
SCLogDebug("TLS handshake invalid length");
SSLSetEvent(ssl_state,
TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
return -1;
}
if (ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) {
memcpy(ssl_state->server_connp.random, input, TLS_RANDOM_LEN);
ssl_state->flags |= TLS_TS_RANDOM_SET;
} else if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
memcpy(ssl_state->client_connp.random, input, TLS_RANDOM_LEN);
ssl_state->flags |= TLS_TC_RANDOM_SET;
}
/* Skip random */
input += SSLV3_CLIENT_HELLO_RANDOM_LEN;
return (int)(input - initial_input);
}
static inline int TLSDecodeHSHelloSessionID(SSLState *ssl_state,
const uint8_t * const initial_input,
const uint32_t input_len)
{
uint8_t *input = (uint8_t *)initial_input;
if (!(HAS_SPACE(1)))
goto invalid_length;
uint8_t session_id_length = *input;
input += 1;
if (!(HAS_SPACE(session_id_length)))
goto invalid_length;
if (session_id_length != 0 && ssl_state->curr_connp->session_id == NULL) {
ssl_state->curr_connp->session_id = SCMalloc(session_id_length);
if (unlikely(ssl_state->curr_connp->session_id == NULL)) {
return -1;
}
if (SafeMemcpy(ssl_state->curr_connp->session_id, 0, session_id_length,
input, 0, input_len, session_id_length) != 0) {
return -1;
}
ssl_state->curr_connp->session_id_length = session_id_length;
if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
ssl_state->client_connp.session_id != NULL &&
ssl_state->server_connp.session_id != NULL) {
if ((ssl_state->client_connp.session_id_length ==
ssl_state->server_connp.session_id_length) &&
(memcmp(ssl_state->server_connp.session_id,
ssl_state->client_connp.session_id, session_id_length) == 0)) {
ssl_state->flags |= SSL_AL_FLAG_SESSION_RESUMED;
}
}
}
input += session_id_length;
return (int)(input - initial_input);
invalid_length:
SCLogDebug("TLS handshake invalid length");
SSLSetEvent(ssl_state,
TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
return -1;
}
static inline int TLSDecodeHSHelloCipherSuites(SSLState *ssl_state,
const uint8_t * const initial_input,
const uint32_t input_len)
{
const uint8_t *input = initial_input;
if (!(HAS_SPACE(2)))
goto invalid_length;
uint16_t cipher_suites_length;
if (ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) {
cipher_suites_length = 2;
} else if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
cipher_suites_length = (uint16_t)(*input << 8) | *(input + 1);
input += 2;
} else {
return -1;
}
if (!(HAS_SPACE(cipher_suites_length)))
goto invalid_length;
/* Cipher suites length should always be divisible by 2 */
if ((cipher_suites_length % 2) != 0) {
goto invalid_length;
}
const bool enable_ja3 =
SC_ATOMIC_GET(ssl_config.enable_ja3) && ssl_state->curr_connp->ja3_hash == NULL;
if (enable_ja3 || SC_ATOMIC_GET(ssl_config.enable_ja4)) {
JA3Buffer *ja3_cipher_suites = NULL;
if (enable_ja3) {
ja3_cipher_suites = Ja3BufferInit();
if (ja3_cipher_suites == NULL)
return -1;
}
uint16_t processed_len = 0;
/* coverity[tainted_data] */
while (processed_len < cipher_suites_length)
{
if (!(HAS_SPACE(2))) {
if (enable_ja3) {
Ja3BufferFree(&ja3_cipher_suites);
}
goto invalid_length;
}
uint16_t cipher_suite = (uint16_t)(*input << 8) | *(input + 1);
input += 2;
if (TLSDecodeValueIsGREASE(cipher_suite) != 1) {
if (ssl_state->curr_connp->ja4 != NULL &&
ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
SCJA4AddCipher(ssl_state->curr_connp->ja4, cipher_suite);
}
if (enable_ja3) {
int rc = Ja3BufferAddValue(&ja3_cipher_suites, cipher_suite);
if (rc != 0) {
return -1;
}
}
}
processed_len += 2;
}
if (enable_ja3) {
int rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str, &ja3_cipher_suites);
if (rc == -1) {
return -1;
}
}
} else {
/* Skip cipher suites */
input += cipher_suites_length;
}
return (int)(input - initial_input);
invalid_length:
SCLogDebug("TLS handshake invalid length");
SSLSetEvent(ssl_state,
TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
return -1;
}
static inline int TLSDecodeHSHelloCompressionMethods(SSLState *ssl_state,
const uint8_t * const initial_input,
const uint32_t input_len)
{
const uint8_t *input = initial_input;
if (!(HAS_SPACE(1)))
goto invalid_length;
/* Skip compression methods */
if (ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) {
input += 1;
} else {
uint8_t compression_methods_length = *input;
input += 1;
if (!(HAS_SPACE(compression_methods_length)))
goto invalid_length;
input += compression_methods_length;
}
return (int)(input - initial_input);
invalid_length:
SCLogDebug("TLS handshake invalid_length");
SSLSetEvent(ssl_state,
TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
return -1;
}
static inline int TLSDecodeHSHelloExtensionSni(SSLState *ssl_state,
const uint8_t * const initial_input,
const uint32_t input_len)
{
uint8_t *input = (uint8_t *)initial_input;
/* Empty extension */
if (input_len == 0)
return 0;
if (!(HAS_SPACE(2)))
goto invalid_length;
/* Skip sni_list_length */
input += 2;
if (!(HAS_SPACE(1)))
goto invalid_length;
uint8_t sni_type = *input;
input += 1;
/* Currently the only type allowed is host_name
(RFC6066 section 3). */
if (sni_type != SSL_SNI_TYPE_HOST_NAME) {
SCLogDebug("Unknown SNI type");
SSLSetEvent(ssl_state,
TLS_DECODER_EVENT_INVALID_SNI_TYPE);
return -1;
}
if (!(HAS_SPACE(2)))
goto invalid_length;
uint16_t sni_len = (uint16_t)(*input << 8) | *(input + 1);
input += 2;
/* host_name contains the fully qualified domain name,
and should therefore be limited by the maximum domain
name length. */
if (!(HAS_SPACE(sni_len)) || sni_len > 255 || sni_len == 0) {
SSLSetEvent(ssl_state,
TLS_DECODER_EVENT_INVALID_SNI_LENGTH);
return -1;
}
/* There must not be more than one extension of the same
type (RFC5246 section 7.4.1.4). */
if (ssl_state->curr_connp->sni) {
SCLogDebug("Multiple SNI extensions");
SSLSetEvent(ssl_state,
TLS_DECODER_EVENT_MULTIPLE_SNI_EXTENSIONS);
input += sni_len;
return (int)(input - initial_input);
}
const size_t sni_strlen = sni_len + 1;
ssl_state->curr_connp->sni = SCMalloc(sni_strlen);
if (unlikely(ssl_state->curr_connp->sni == NULL))
return -1;
const size_t consumed = input - initial_input;
if (SafeMemcpy(ssl_state->curr_connp->sni, 0, sni_strlen,