tls_alert.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2018 The GoPacket Authors. 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. package layers
  7. import (
  8. "errors"
  9. "fmt"
  10. "github.com/google/gopacket"
  11. )
  12. // TLSAlertLevel defines the alert level data type
  13. type TLSAlertLevel uint8
  14. // TLSAlertDescr defines the alert descrption data type
  15. type TLSAlertDescr uint8
  16. const (
  17. TLSAlertWarning TLSAlertLevel = 1
  18. TLSAlertFatal TLSAlertLevel = 2
  19. TLSAlertUnknownLevel TLSAlertLevel = 255
  20. TLSAlertCloseNotify TLSAlertDescr = 0
  21. TLSAlertUnexpectedMessage TLSAlertDescr = 10
  22. TLSAlertBadRecordMac TLSAlertDescr = 20
  23. TLSAlertDecryptionFailedRESERVED TLSAlertDescr = 21
  24. TLSAlertRecordOverflow TLSAlertDescr = 22
  25. TLSAlertDecompressionFailure TLSAlertDescr = 30
  26. TLSAlertHandshakeFailure TLSAlertDescr = 40
  27. TLSAlertNoCertificateRESERVED TLSAlertDescr = 41
  28. TLSAlertBadCertificate TLSAlertDescr = 42
  29. TLSAlertUnsupportedCertificate TLSAlertDescr = 43
  30. TLSAlertCertificateRevoked TLSAlertDescr = 44
  31. TLSAlertCertificateExpired TLSAlertDescr = 45
  32. TLSAlertCertificateUnknown TLSAlertDescr = 46
  33. TLSAlertIllegalParameter TLSAlertDescr = 47
  34. TLSAlertUnknownCa TLSAlertDescr = 48
  35. TLSAlertAccessDenied TLSAlertDescr = 49
  36. TLSAlertDecodeError TLSAlertDescr = 50
  37. TLSAlertDecryptError TLSAlertDescr = 51
  38. TLSAlertExportRestrictionRESERVED TLSAlertDescr = 60
  39. TLSAlertProtocolVersion TLSAlertDescr = 70
  40. TLSAlertInsufficientSecurity TLSAlertDescr = 71
  41. TLSAlertInternalError TLSAlertDescr = 80
  42. TLSAlertUserCanceled TLSAlertDescr = 90
  43. TLSAlertNoRenegotiation TLSAlertDescr = 100
  44. TLSAlertUnsupportedExtension TLSAlertDescr = 110
  45. TLSAlertUnknownDescription TLSAlertDescr = 255
  46. )
  47. // TLS Alert
  48. // 0 1 2 3 4 5 6 7 8
  49. // +--+--+--+--+--+--+--+--+
  50. // | Level |
  51. // +--+--+--+--+--+--+--+--+
  52. // | Description |
  53. // +--+--+--+--+--+--+--+--+
  54. // TLSAlertRecord contains all the information that each Alert Record type should have
  55. type TLSAlertRecord struct {
  56. TLSRecordHeader
  57. Level TLSAlertLevel
  58. Description TLSAlertDescr
  59. EncryptedMsg []byte
  60. }
  61. // DecodeFromBytes decodes the slice into the TLS struct.
  62. func (t *TLSAlertRecord) decodeFromBytes(h TLSRecordHeader, data []byte, df gopacket.DecodeFeedback) error {
  63. // TLS Record Header
  64. t.ContentType = h.ContentType
  65. t.Version = h.Version
  66. t.Length = h.Length
  67. if len(data) < 2 {
  68. df.SetTruncated()
  69. return errors.New("TLS Alert packet too short")
  70. }
  71. if t.Length == 2 {
  72. t.Level = TLSAlertLevel(data[0])
  73. t.Description = TLSAlertDescr(data[1])
  74. } else {
  75. t.Level = TLSAlertUnknownLevel
  76. t.Description = TLSAlertUnknownDescription
  77. t.EncryptedMsg = data
  78. }
  79. return nil
  80. }
  81. // Strings shows the TLS alert level nicely formatted
  82. func (al TLSAlertLevel) String() string {
  83. switch al {
  84. default:
  85. return fmt.Sprintf("Unknown(%d)", al)
  86. case TLSAlertWarning:
  87. return "Warning"
  88. case TLSAlertFatal:
  89. return "Fatal"
  90. }
  91. }
  92. // Strings shows the TLS alert description nicely formatted
  93. func (ad TLSAlertDescr) String() string {
  94. switch ad {
  95. default:
  96. return "Unknown"
  97. case TLSAlertCloseNotify:
  98. return "close_notify"
  99. case TLSAlertUnexpectedMessage:
  100. return "unexpected_message"
  101. case TLSAlertBadRecordMac:
  102. return "bad_record_mac"
  103. case TLSAlertDecryptionFailedRESERVED:
  104. return "decryption_failed_RESERVED"
  105. case TLSAlertRecordOverflow:
  106. return "record_overflow"
  107. case TLSAlertDecompressionFailure:
  108. return "decompression_failure"
  109. case TLSAlertHandshakeFailure:
  110. return "handshake_failure"
  111. case TLSAlertNoCertificateRESERVED:
  112. return "no_certificate_RESERVED"
  113. case TLSAlertBadCertificate:
  114. return "bad_certificate"
  115. case TLSAlertUnsupportedCertificate:
  116. return "unsupported_certificate"
  117. case TLSAlertCertificateRevoked:
  118. return "certificate_revoked"
  119. case TLSAlertCertificateExpired:
  120. return "certificate_expired"
  121. case TLSAlertCertificateUnknown:
  122. return "certificate_unknown"
  123. case TLSAlertIllegalParameter:
  124. return "illegal_parameter"
  125. case TLSAlertUnknownCa:
  126. return "unknown_ca"
  127. case TLSAlertAccessDenied:
  128. return "access_denied"
  129. case TLSAlertDecodeError:
  130. return "decode_error"
  131. case TLSAlertDecryptError:
  132. return "decrypt_error"
  133. case TLSAlertExportRestrictionRESERVED:
  134. return "export_restriction_RESERVED"
  135. case TLSAlertProtocolVersion:
  136. return "protocol_version"
  137. case TLSAlertInsufficientSecurity:
  138. return "insufficient_security"
  139. case TLSAlertInternalError:
  140. return "internal_error"
  141. case TLSAlertUserCanceled:
  142. return "user_canceled"
  143. case TLSAlertNoRenegotiation:
  144. return "no_renegotiation"
  145. case TLSAlertUnsupportedExtension:
  146. return "unsupported_extension"
  147. }
  148. }