errors.go 9.9 KB

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