|
| 1 | +// Copyright (c) 2018, Open Systems AG. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this source code is governed by a BSD-style license |
| 4 | +// that can be found in the LICENSE file in the root of the source |
| 5 | +// tree. |
| 6 | + |
| 7 | +package ja3 |
| 8 | + |
| 9 | +import ( |
| 10 | + "crypto/md5" |
| 11 | + "encoding/hex" |
| 12 | + |
| 13 | + "golang.org/x/exp/slices" |
| 14 | +) |
| 15 | + |
| 16 | +type ClientHello struct { |
| 17 | + Version uint16 |
| 18 | + CipherSuites []uint16 |
| 19 | + Extensions []uint16 |
| 20 | + EllipticCurves []uint16 |
| 21 | + EllipticCurvePF []uint8 |
| 22 | + Versions []uint16 |
| 23 | + SignatureAlgorithms []uint16 |
| 24 | + ServerName string |
| 25 | + ja3ByteString []byte |
| 26 | + ja3Hash string |
| 27 | +} |
| 28 | + |
| 29 | +func (j *ClientHello) Equals(another *ClientHello, ignoreExtensionsSequence bool) bool { |
| 30 | + if j.Version != another.Version { |
| 31 | + return false |
| 32 | + } |
| 33 | + if !slices.Equal(j.CipherSuites, another.CipherSuites) { |
| 34 | + return false |
| 35 | + } |
| 36 | + if !ignoreExtensionsSequence && !slices.Equal(j.Extensions, another.Extensions) { |
| 37 | + return false |
| 38 | + } |
| 39 | + if ignoreExtensionsSequence && !slices.Equal(j.Extensions, another.sortedExtensions()) { |
| 40 | + return false |
| 41 | + } |
| 42 | + if !slices.Equal(j.EllipticCurves, another.EllipticCurves) { |
| 43 | + return false |
| 44 | + } |
| 45 | + if !slices.Equal(j.EllipticCurvePF, another.EllipticCurvePF) { |
| 46 | + return false |
| 47 | + } |
| 48 | + if !slices.Equal(j.SignatureAlgorithms, another.SignatureAlgorithms) { |
| 49 | + return false |
| 50 | + } |
| 51 | + return true |
| 52 | +} |
| 53 | + |
| 54 | +func (j *ClientHello) sortedExtensions() []uint16 { |
| 55 | + extensions := make([]uint16, len(j.Extensions)) |
| 56 | + copy(extensions, j.Extensions) |
| 57 | + slices.Sort(extensions) |
| 58 | + return extensions |
| 59 | +} |
| 60 | + |
| 61 | +func Compute(payload []byte) (*ClientHello, error) { |
| 62 | + ja3 := ClientHello{} |
| 63 | + err := ja3.parseSegment(payload) |
| 64 | + return &ja3, err |
| 65 | +} |
| 66 | + |
| 67 | +func (j *ClientHello) String() string { |
| 68 | + if j.ja3ByteString == nil { |
| 69 | + j.marshalJA3() |
| 70 | + } |
| 71 | + return string(j.ja3ByteString) |
| 72 | +} |
| 73 | + |
| 74 | +func (j *ClientHello) Hash() string { |
| 75 | + if j.ja3ByteString == nil { |
| 76 | + j.marshalJA3() |
| 77 | + } |
| 78 | + if j.ja3Hash == "" { |
| 79 | + h := md5.Sum(j.ja3ByteString) |
| 80 | + j.ja3Hash = hex.EncodeToString(h[:]) |
| 81 | + } |
| 82 | + return j.ja3Hash |
| 83 | +} |
0 commit comments