u_fingerprinter.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2017 Google Inc. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tls
  5. // Fingerprinter is a struct largely for holding options for the FingerprintClientHello func
  6. type Fingerprinter struct {
  7. // AllowBluntMimicry will ensure that unknown extensions are
  8. // passed along into the resulting ClientHelloSpec as-is
  9. // WARNING: there could be numerous subtle issues with ClientHelloSpecs
  10. // that are generated with this flag which could compromise security and/or mimicry
  11. AllowBluntMimicry bool
  12. // AlwaysAddPadding will always add a UtlsPaddingExtension with BoringPaddingStyle
  13. // at the end of the extensions list if it isn't found in the fingerprinted hello.
  14. // This could be useful in scenarios where the hello you are fingerprinting does not
  15. // have any padding, but you suspect that other changes you make to the final hello
  16. // (including things like different SNI lengths) would cause padding to be necessary
  17. AlwaysAddPadding bool
  18. RealPSKResumption bool // if set, PSK extension (if any) will be real PSK extension, otherwise it will be fake PSK extension
  19. }
  20. // FingerprintClientHello returns a ClientHelloSpec which is based on the
  21. // ClientHello that is passed in as the data argument
  22. //
  23. // If the ClientHello passed in has extensions that are not recognized or cannot be handled
  24. // it will return a non-nil error and a nil *ClientHelloSpec value
  25. //
  26. // The data should be the full tls record, including the record type/version/length header
  27. // as well as the handshake type/length/version header
  28. // https://tools.ietf.org/html/rfc5246#section-6.2
  29. // https://tools.ietf.org/html/rfc5246#section-7.4
  30. //
  31. // It calls UnmarshalClientHello internally, and is kept for backwards compatibility
  32. func (f *Fingerprinter) FingerprintClientHello(data []byte) (clientHelloSpec *ClientHelloSpec, err error) {
  33. return f.RawClientHello(data)
  34. }
  35. // RawClientHello returns a ClientHelloSpec which is based on the
  36. // ClientHello raw bytes that is passed in as the raw argument.
  37. //
  38. // It was renamed from FingerprintClientHello in v1.3.1 and earlier versions
  39. // as a more precise name for the function
  40. func (f *Fingerprinter) RawClientHello(raw []byte) (clientHelloSpec *ClientHelloSpec, err error) {
  41. clientHelloSpec = &ClientHelloSpec{}
  42. err = clientHelloSpec.FromRaw(raw, f.AllowBluntMimicry, f.RealPSKResumption)
  43. if err != nil {
  44. return nil, err
  45. }
  46. if f.AlwaysAddPadding {
  47. clientHelloSpec.AlwaysAddPadding()
  48. }
  49. return clientHelloSpec, nil
  50. }
  51. // UnmarshalJSONClientHello returns a ClientHelloSpec which is based on the
  52. // ClientHello JSON bytes that is passed in as the json argument.
  53. func (f *Fingerprinter) UnmarshalJSONClientHello(json []byte) (clientHelloSpec *ClientHelloSpec, err error) {
  54. clientHelloSpec = &ClientHelloSpec{}
  55. err = clientHelloSpec.UnmarshalJSON(json)
  56. if err != nil {
  57. return nil, err
  58. }
  59. if f.AlwaysAddPadding {
  60. clientHelloSpec.AlwaysAddPadding()
  61. }
  62. return clientHelloSpec, nil
  63. }