errors.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package dtls
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net"
  8. "os"
  9. "github.com/pion/dtls/v2/pkg/protocol"
  10. "github.com/pion/dtls/v2/pkg/protocol/alert"
  11. )
  12. // Typed errors
  13. var (
  14. ErrConnClosed = &FatalError{Err: errors.New("conn is closed")} //nolint:goerr113
  15. errDeadlineExceeded = &TimeoutError{Err: fmt.Errorf("read/write timeout: %w", context.DeadlineExceeded)}
  16. errInvalidContentType = &TemporaryError{Err: errors.New("invalid content type")} //nolint:goerr113
  17. errBufferTooSmall = &TemporaryError{Err: errors.New("buffer is too small")} //nolint:goerr113
  18. errContextUnsupported = &TemporaryError{Err: errors.New("context is not supported for ExportKeyingMaterial")} //nolint:goerr113
  19. errHandshakeInProgress = &TemporaryError{Err: errors.New("handshake is in progress")} //nolint:goerr113
  20. errReservedExportKeyingMaterial = &TemporaryError{Err: errors.New("ExportKeyingMaterial can not be used with a reserved label")} //nolint:goerr113
  21. errApplicationDataEpochZero = &TemporaryError{Err: errors.New("ApplicationData with epoch of 0")} //nolint:goerr113
  22. errUnhandledContextType = &TemporaryError{Err: errors.New("unhandled contentType")} //nolint:goerr113
  23. errCertificateVerifyNoCertificate = &FatalError{Err: errors.New("client sent certificate verify but we have no certificate to verify")} //nolint:goerr113
  24. errCipherSuiteNoIntersection = &FatalError{Err: errors.New("client+server do not support any shared cipher suites")} //nolint:goerr113
  25. errClientCertificateNotVerified = &FatalError{Err: errors.New("client sent certificate but did not verify it")} //nolint:goerr113
  26. errClientCertificateRequired = &FatalError{Err: errors.New("server required client verification, but got none")} //nolint:goerr113
  27. errClientNoMatchingSRTPProfile = &FatalError{Err: errors.New("server responded with SRTP Profile we do not support")} //nolint:goerr113
  28. errClientRequiredButNoServerEMS = &FatalError{Err: errors.New("client required Extended Master Secret extension, but server does not support it")} //nolint:goerr113
  29. errCookieMismatch = &FatalError{Err: errors.New("client+server cookie does not match")} //nolint:goerr113
  30. errIdentityNoPSK = &FatalError{Err: errors.New("PSK Identity Hint provided but PSK is nil")} //nolint:goerr113
  31. errInvalidCertificate = &FatalError{Err: errors.New("no certificate provided")} //nolint:goerr113
  32. errInvalidCipherSuite = &FatalError{Err: errors.New("invalid or unknown cipher suite")} //nolint:goerr113
  33. errInvalidECDSASignature = &FatalError{Err: errors.New("ECDSA signature contained zero or negative values")} //nolint:goerr113
  34. errInvalidPrivateKey = &FatalError{Err: errors.New("invalid private key type")} //nolint:goerr113
  35. errInvalidSignatureAlgorithm = &FatalError{Err: errors.New("invalid signature algorithm")} //nolint:goerr113
  36. errKeySignatureMismatch = &FatalError{Err: errors.New("expected and actual key signature do not match")} //nolint:goerr113
  37. errNilNextConn = &FatalError{Err: errors.New("Conn can not be created with a nil nextConn")} //nolint:goerr113
  38. errNoAvailableCipherSuites = &FatalError{Err: errors.New("connection can not be created, no CipherSuites satisfy this Config")} //nolint:goerr113
  39. errNoAvailablePSKCipherSuite = &FatalError{Err: errors.New("connection can not be created, pre-shared key present but no compatible CipherSuite")} //nolint:goerr113
  40. errNoAvailableCertificateCipherSuite = &FatalError{Err: errors.New("connection can not be created, certificate present but no compatible CipherSuite")} //nolint:goerr113
  41. errNoAvailableSignatureSchemes = &FatalError{Err: errors.New("connection can not be created, no SignatureScheme satisfy this Config")} //nolint:goerr113
  42. errNoCertificates = &FatalError{Err: errors.New("no certificates configured")} //nolint:goerr113
  43. errNoConfigProvided = &FatalError{Err: errors.New("no config provided")} //nolint:goerr113
  44. errNoSupportedEllipticCurves = &FatalError{Err: errors.New("client requested zero or more elliptic curves that are not supported by the server")} //nolint:goerr113
  45. errUnsupportedProtocolVersion = &FatalError{Err: errors.New("unsupported protocol version")} //nolint:goerr113
  46. errPSKAndIdentityMustBeSetForClient = &FatalError{Err: errors.New("PSK and PSK Identity Hint must both be set for client")} //nolint:goerr113
  47. errRequestedButNoSRTPExtension = &FatalError{Err: errors.New("SRTP support was requested but server did not respond with use_srtp extension")} //nolint:goerr113
  48. errServerNoMatchingSRTPProfile = &FatalError{Err: errors.New("client requested SRTP but we have no matching profiles")} //nolint:goerr113
  49. errServerRequiredButNoClientEMS = &FatalError{Err: errors.New("server requires the Extended Master Secret extension, but the client does not support it")} //nolint:goerr113
  50. errVerifyDataMismatch = &FatalError{Err: errors.New("expected and actual verify data does not match")} //nolint:goerr113
  51. errNotAcceptableCertificateChain = &FatalError{Err: errors.New("certificate chain is not signed by an acceptable CA")} //nolint:goerr113
  52. errInvalidFlight = &InternalError{Err: errors.New("invalid flight number")} //nolint:goerr113
  53. errKeySignatureGenerateUnimplemented = &InternalError{Err: errors.New("unable to generate key signature, unimplemented")} //nolint:goerr113
  54. errKeySignatureVerifyUnimplemented = &InternalError{Err: errors.New("unable to verify key signature, unimplemented")} //nolint:goerr113
  55. errLengthMismatch = &InternalError{Err: errors.New("data length and declared length do not match")} //nolint:goerr113
  56. errSequenceNumberOverflow = &InternalError{Err: errors.New("sequence number overflow")} //nolint:goerr113
  57. errInvalidFSMTransition = &InternalError{Err: errors.New("invalid state machine transition")} //nolint:goerr113
  58. errFailedToAccessPoolReadBuffer = &InternalError{Err: errors.New("failed to access pool read buffer")} //nolint:goerr113
  59. errFragmentBufferOverflow = &InternalError{Err: errors.New("fragment buffer overflow")} //nolint:goerr113
  60. )
  61. // FatalError indicates that the DTLS connection is no longer available.
  62. // It is mainly caused by wrong configuration of server or client.
  63. type FatalError = protocol.FatalError
  64. // InternalError indicates and internal error caused by the implementation, and the DTLS connection is no longer available.
  65. // It is mainly caused by bugs or tried to use unimplemented features.
  66. type InternalError = protocol.InternalError
  67. // TemporaryError indicates that the DTLS connection is still available, but the request was failed temporary.
  68. type TemporaryError = protocol.TemporaryError
  69. // TimeoutError indicates that the request was timed out.
  70. type TimeoutError = protocol.TimeoutError
  71. // HandshakeError indicates that the handshake failed.
  72. type HandshakeError = protocol.HandshakeError
  73. // errInvalidCipherSuite indicates an attempt at using an unsupported cipher suite.
  74. type invalidCipherSuiteError struct {
  75. id CipherSuiteID
  76. }
  77. func (e *invalidCipherSuiteError) Error() string {
  78. return fmt.Sprintf("CipherSuite with id(%d) is not valid", e.id)
  79. }
  80. func (e *invalidCipherSuiteError) Is(err error) bool {
  81. var other *invalidCipherSuiteError
  82. if errors.As(err, &other) {
  83. return e.id == other.id
  84. }
  85. return false
  86. }
  87. // errAlert wraps DTLS alert notification as an error
  88. type alertError struct {
  89. *alert.Alert
  90. }
  91. func (e *alertError) Error() string {
  92. return fmt.Sprintf("alert: %s", e.Alert.String())
  93. }
  94. func (e *alertError) IsFatalOrCloseNotify() bool {
  95. return e.Level == alert.Fatal || e.Description == alert.CloseNotify
  96. }
  97. func (e *alertError) Is(err error) bool {
  98. var other *alertError
  99. if errors.As(err, &other) {
  100. return e.Level == other.Level && e.Description == other.Description
  101. }
  102. return false
  103. }
  104. // netError translates an error from underlying Conn to corresponding net.Error.
  105. func netError(err error) error {
  106. switch {
  107. case errors.Is(err, io.EOF), errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded):
  108. // Return io.EOF and context errors as is.
  109. return err
  110. }
  111. var (
  112. ne net.Error
  113. opError *net.OpError
  114. se *os.SyscallError
  115. )
  116. if errors.As(err, &opError) {
  117. if errors.As(opError, &se) {
  118. if se.Timeout() {
  119. return &TimeoutError{Err: err}
  120. }
  121. if isOpErrorTemporary(se) {
  122. return &TemporaryError{Err: err}
  123. }
  124. }
  125. }
  126. if errors.As(err, &ne) {
  127. return err
  128. }
  129. return &FatalError{Err: err}
  130. }