|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2024-present Datadog, Inc. |
| 5 | + |
| 6 | +// Package tls contains definitions and methods related to tags parsed from the TLS handshake |
| 7 | +package tls |
| 8 | + |
| 9 | +import ( |
| 10 | + "crypto/tls" |
| 11 | + "fmt" |
| 12 | +) |
| 13 | + |
| 14 | +// Constants for tag keys |
| 15 | +const ( |
| 16 | + TagTLSVersion = "tls.version:" |
| 17 | + TagTLSCipherSuiteID = "tls.cipher_suite_id:" |
| 18 | + TagTLSClientVersion = "tls.client_version:" |
| 19 | + version10 = "tls_1.0" |
| 20 | + version11 = "tls_1.1" |
| 21 | + version12 = "tls_1.2" |
| 22 | + version13 = "tls_1.3" |
| 23 | +) |
| 24 | + |
| 25 | +// Bitmask constants for Offered_versions matching kernelspace definitions |
| 26 | +const ( |
| 27 | + OfferedTLSVersion10 uint8 = 0x01 |
| 28 | + OfferedTLSVersion11 uint8 = 0x02 |
| 29 | + OfferedTLSVersion12 uint8 = 0x04 |
| 30 | + OfferedTLSVersion13 uint8 = 0x08 |
| 31 | +) |
| 32 | + |
| 33 | +// VersionTags maps TLS versions to tag names for server chosen version (exported for testing) |
| 34 | +var VersionTags = map[uint16]string{ |
| 35 | + tls.VersionTLS10: TagTLSVersion + version10, |
| 36 | + tls.VersionTLS11: TagTLSVersion + version11, |
| 37 | + tls.VersionTLS12: TagTLSVersion + version12, |
| 38 | + tls.VersionTLS13: TagTLSVersion + version13, |
| 39 | +} |
| 40 | + |
| 41 | +// ClientVersionTags maps TLS versions to tag names for client offered versions (exported for testing) |
| 42 | +var ClientVersionTags = map[uint16]string{ |
| 43 | + tls.VersionTLS10: TagTLSClientVersion + version10, |
| 44 | + tls.VersionTLS11: TagTLSClientVersion + version11, |
| 45 | + tls.VersionTLS12: TagTLSClientVersion + version12, |
| 46 | + tls.VersionTLS13: TagTLSClientVersion + version13, |
| 47 | +} |
| 48 | + |
| 49 | +// Mapping of offered version bitmasks to version constants |
| 50 | +var offeredVersionBitmask = []struct { |
| 51 | + bitMask uint8 |
| 52 | + version uint16 |
| 53 | +}{ |
| 54 | + {OfferedTLSVersion10, tls.VersionTLS10}, |
| 55 | + {OfferedTLSVersion11, tls.VersionTLS11}, |
| 56 | + {OfferedTLSVersion12, tls.VersionTLS12}, |
| 57 | + {OfferedTLSVersion13, tls.VersionTLS13}, |
| 58 | +} |
| 59 | + |
| 60 | +// Tags holds the TLS tags. It is used to store the TLS version, cipher suite and offered versions. |
| 61 | +// We can't use the struct from eBPF as the definition is shared with windows. |
| 62 | +type Tags struct { |
| 63 | + ChosenVersion uint16 |
| 64 | + CipherSuite uint16 |
| 65 | + OfferedVersions uint8 |
| 66 | +} |
| 67 | + |
| 68 | +// MergeWith merges the tags from another Tags struct into this one |
| 69 | +func (t *Tags) MergeWith(that Tags) { |
| 70 | + if t.ChosenVersion == 0 { |
| 71 | + t.ChosenVersion = that.ChosenVersion |
| 72 | + } |
| 73 | + if t.CipherSuite == 0 { |
| 74 | + t.CipherSuite = that.CipherSuite |
| 75 | + } |
| 76 | + if t.OfferedVersions == 0 { |
| 77 | + t.OfferedVersions = that.OfferedVersions |
| 78 | + } |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +// IsEmpty returns true if all fields are zero |
| 83 | +func (t *Tags) IsEmpty() bool { |
| 84 | + if t == nil { |
| 85 | + return true |
| 86 | + } |
| 87 | + return t.ChosenVersion == 0 && t.CipherSuite == 0 && t.OfferedVersions == 0 |
| 88 | +} |
| 89 | + |
| 90 | +// String returns a string representation of the Tags struct |
| 91 | +func (t *Tags) String() string { |
| 92 | + return fmt.Sprintf("ChosenVersion: %d, CipherSuite: %d, OfferedVersions: %d", t.ChosenVersion, t.CipherSuite, t.OfferedVersions) |
| 93 | +} |
| 94 | + |
| 95 | +// parseOfferedVersions parses the Offered_versions bitmask into a slice of version strings |
| 96 | +func parseOfferedVersions(offeredVersions uint8) []string { |
| 97 | + versions := make([]string, 0, len(offeredVersionBitmask)) |
| 98 | + for _, ov := range offeredVersionBitmask { |
| 99 | + if (offeredVersions & ov.bitMask) != 0 { |
| 100 | + if name := ClientVersionTags[ov.version]; name != "" { |
| 101 | + versions = append(versions, name) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return versions |
| 106 | +} |
| 107 | + |
| 108 | +func hexCipherSuiteTag(cipherSuite uint16) string { |
| 109 | + return fmt.Sprintf("%s0x%04X", TagTLSCipherSuiteID, cipherSuite) |
| 110 | +} |
| 111 | + |
| 112 | +// GetDynamicTags generates dynamic tags based on TLS information |
| 113 | +func (t *Tags) GetDynamicTags() map[string]struct{} { |
| 114 | + if t.IsEmpty() { |
| 115 | + return nil |
| 116 | + } |
| 117 | + tags := make(map[string]struct{}) |
| 118 | + |
| 119 | + // Server chosen version |
| 120 | + if tag, ok := VersionTags[t.ChosenVersion]; ok { |
| 121 | + tags[tag] = struct{}{} |
| 122 | + } |
| 123 | + |
| 124 | + // Client offered versions |
| 125 | + for _, versionName := range parseOfferedVersions(t.OfferedVersions) { |
| 126 | + tags[versionName] = struct{}{} |
| 127 | + } |
| 128 | + |
| 129 | + // Cipher suite ID as hex string |
| 130 | + if t.CipherSuite != 0 { |
| 131 | + tags[hexCipherSuiteTag(t.CipherSuite)] = struct{}{} |
| 132 | + } |
| 133 | + |
| 134 | + return tags |
| 135 | +} |
0 commit comments