ktls_alert.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2009 The Go Authors. 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. //go:build linux && go1.25 && badlinkname
  5. package ktls
  6. import (
  7. "crypto/tls"
  8. "net"
  9. )
  10. const (
  11. // alert level
  12. alertLevelWarning = 1
  13. alertLevelError = 2
  14. )
  15. const (
  16. alertCloseNotify = 0
  17. alertUnexpectedMessage = 10
  18. alertBadRecordMAC = 20
  19. alertDecryptionFailed = 21
  20. alertRecordOverflow = 22
  21. alertDecompressionFailure = 30
  22. alertHandshakeFailure = 40
  23. alertBadCertificate = 42
  24. alertUnsupportedCertificate = 43
  25. alertCertificateRevoked = 44
  26. alertCertificateExpired = 45
  27. alertCertificateUnknown = 46
  28. alertIllegalParameter = 47
  29. alertUnknownCA = 48
  30. alertAccessDenied = 49
  31. alertDecodeError = 50
  32. alertDecryptError = 51
  33. alertExportRestriction = 60
  34. alertProtocolVersion = 70
  35. alertInsufficientSecurity = 71
  36. alertInternalError = 80
  37. alertInappropriateFallback = 86
  38. alertUserCanceled = 90
  39. alertNoRenegotiation = 100
  40. alertMissingExtension = 109
  41. alertUnsupportedExtension = 110
  42. alertCertificateUnobtainable = 111
  43. alertUnrecognizedName = 112
  44. alertBadCertificateStatusResponse = 113
  45. alertBadCertificateHashValue = 114
  46. alertUnknownPSKIdentity = 115
  47. alertCertificateRequired = 116
  48. alertNoApplicationProtocol = 120
  49. alertECHRequired = 121
  50. )
  51. func (c *Conn) sendAlertLocked(err uint8) error {
  52. switch err {
  53. case alertNoRenegotiation, alertCloseNotify:
  54. c.rawConn.Tmp[0] = alertLevelWarning
  55. default:
  56. c.rawConn.Tmp[0] = alertLevelError
  57. }
  58. c.rawConn.Tmp[1] = byte(err)
  59. _, writeErr := c.writeRecordLocked(recordTypeAlert, c.rawConn.Tmp[0:2])
  60. if err == alertCloseNotify {
  61. // closeNotify is a special case in that it isn't an error.
  62. return writeErr
  63. }
  64. return c.rawConn.Out.SetErrorLocked(&net.OpError{Op: "local error", Err: tls.AlertError(err)})
  65. }
  66. // sendAlert sends a TLS alert message.
  67. func (c *Conn) sendAlert(err uint8) error {
  68. c.rawConn.Out.Lock()
  69. defer c.rawConn.Out.Unlock()
  70. return c.sendAlertLocked(err)
  71. }