Package tlslite :: Module errors |
[hide private]
[frames] | no frames]

# Source Code for Module tlslite.errors

      1  # Authors:
      2  #   Trevor Perrin
      3  #   Dave Baggett (Arcode Corporation) - Added TLSUnsupportedError.
      4  #
      5  # See the LICENSE file for legal information regarding use of this file.
      6
      7  """Exception classes.
      8  @sort: TLSError, TLSAbruptCloseError, TLSAlert, TLSLocalAlert, TLSRemoteAlert,
      9  TLSAuthenticationError, TLSNoAuthenticationError, TLSAuthenticationTypeError,
     10  TLSFingerprintError, TLSAuthorizationError, TLSValidationError, TLSFaultError,
     11  TLSUnsupportedError
     12  """
     13  import socket
     14
     15  from .constants import AlertDescription, AlertLevel
     16
     17 -class TLSError(Exception):
     18      """Base class for all TLS Lite exceptions."""
     19
     20 -    def __str__(self):
     21          """"At least print out the Exception time for str(...)."""
     22          return repr(self)
     23
     24 -class TLSClosedConnectionError(TLSError, socket.error):
     25      """An attempt was made to use the connection after it was closed."""
     26      pass
     27
     28 -class TLSAbruptCloseError(TLSError):
     29      """The socket was closed without a proper TLS shutdown.
     30
     31      The TLS specification mandates that an alert of some sort
     32      must be sent before the underlying socket is closed.  If the socket
     33      is closed without this, it could signify that an attacker is trying
     34      to truncate the connection.  It could also signify a misbehaving
     35      TLS implementation, or a random network failure.
     36      """
     37      pass
     38
     39 -class TLSAlert(TLSError):
     40      """A TLS alert has been signalled."""
     41      pass
     42
     43      _descriptionStr = {\
     44          AlertDescription.close_notify: "close_notify",\
     45          AlertDescription.unexpected_message: "unexpected_message",\
     46          AlertDescription.bad_record_mac: "bad_record_mac",\
     47          AlertDescription.decryption_failed: "decryption_failed",\
     48          AlertDescription.record_overflow: "record_overflow",\
     49          AlertDescription.decompression_failure: "decompression_failure",\
     50          AlertDescription.handshake_failure: "handshake_failure",\
     51          AlertDescription.no_certificate: "no certificate",\
     52          AlertDescription.bad_certificate: "bad_certificate",\
     53          AlertDescription.unsupported_certificate: "unsupported_certificate",\
     54          AlertDescription.certificate_revoked: "certificate_revoked",\
     55          AlertDescription.certificate_expired: "certificate_expired",\
     56          AlertDescription.certificate_unknown: "certificate_unknown",\
     57          AlertDescription.illegal_parameter: "illegal_parameter",\
     58          AlertDescription.unknown_ca: "unknown_ca",\
     59          AlertDescription.access_denied: "access_denied",\
     60          AlertDescription.decode_error: "decode_error",\
     61          AlertDescription.decrypt_error: "decrypt_error",\
     62          AlertDescription.export_restriction: "export_restriction",\
     63          AlertDescription.protocol_version: "protocol_version",\
     64          AlertDescription.insufficient_security: "insufficient_security",\
     65          AlertDescription.internal_error: "internal_error",\
     66          AlertDescription.user_canceled: "user_canceled",\
     67          AlertDescription.no_renegotiation: "no_renegotiation",\
     68          AlertDescription.unknown_psk_identity: "unknown_psk_identity"}
     69
     70 -class TLSLocalAlert(TLSAlert):
     71      """A TLS alert has been signalled by the local implementation.
     72
     73      @type description: int
     74      @ivar description: Set to one of the constants in
     75      L{tlslite.constants.AlertDescription}
     76
     77      @type level: int
     78      @ivar level: Set to one of the constants in
     79      L{tlslite.constants.AlertLevel}
     80
     81      @type message: str
     82      @ivar message: Description of what went wrong.
     83      """
     84 -    def __init__(self, alert, message=None):
     85          self.description = alert.description
     86          self.level = alert.level
     87          self.message = message
     88
     89 -    def __str__(self):
     90          alertStr = TLSAlert._descriptionStr.get(self.description)
     91          if alertStr == None:
     92              alertStr = str(self.description)
     93          if self.message:
     94              return alertStr + ": " + self.message
     95          else:
     96              return alertStr
     97
     98 -class TLSRemoteAlert(TLSAlert):
     99      """A TLS alert has been signalled by the remote implementation.
    100
    101      @type description: int
    102      @ivar description: Set to one of the constants in
    103      L{tlslite.constants.AlertDescription}
    104
    105      @type level: int
    106      @ivar level: Set to one of the constants in
    107      L{tlslite.constants.AlertLevel}
    108      """
    109 -    def __init__(self, alert):
    110          self.description = alert.description
    111          self.level = alert.level
    112
    113 -    def __str__(self):
    114          alertStr = TLSAlert._descriptionStr.get(self.description)
    115          if alertStr == None:
    116              alertStr = str(self.description)
    117          return alertStr
    118
    119 -class TLSAuthenticationError(TLSError):
    120      """The handshake succeeded, but the other party's authentication
    121      was inadequate.
    122
    123      This exception will only be raised when a
    124      L{tlslite.Checker.Checker} has been passed to a handshake function.
    125      The Checker will be invoked once the handshake completes, and if
    126      the Checker objects to how the other party authenticated, a
    127      subclass of this exception will be raised.
    128      """
    129      pass
    130
    131 -class TLSNoAuthenticationError(TLSAuthenticationError):
    132      """The Checker was expecting the other party to authenticate with a
    133      certificate chain, but this did not occur."""
    134      pass
    135
    136 -class TLSAuthenticationTypeError(TLSAuthenticationError):
    137      """The Checker was expecting the other party to authenticate with a
    138      different type of certificate chain."""
    139      pass
    140
    141 -class TLSFingerprintError(TLSAuthenticationError):
    142      """The Checker was expecting the other party to authenticate with a
    143      certificate chain that matches a different fingerprint."""
    144      pass
    145
    146 -class TLSAuthorizationError(TLSAuthenticationError):
    147      """The Checker was expecting the other party to authenticate with a
    148      certificate chain that has a different authorization."""
    149      pass
    150
    151 -class TLSValidationError(TLSAuthenticationError):
    152      """The Checker has determined that the other party's certificate
    153      chain is invalid."""
    154 -    def __init__(self, msg, info=None):
    155          # Include a dict containing info about this validation failure
    156          TLSAuthenticationError.__init__(self, msg)
    157          self.info = info
    158
    159 -class TLSFaultError(TLSError):
    160      """The other party responded incorrectly to an induced fault.
    161
    162      This exception will only occur during fault testing, when a
    163      TLSConnection's fault variable is set to induce some sort of
    164      faulty behavior, and the other party doesn't respond appropriately.
    165      """
    166      pass
    167
    168
    169 -class TLSUnsupportedError(TLSError):
    170      """The implementation doesn't support the requested (or required)
    171      capabilities."""
    172      pass
    173