errors.go 9.2 KB

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