Alert.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // Transport Security Layer (TLS)
  2. // Copyright (c) 2003-2004 Carlos Guzman Alvarez
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. namespace Mono.Security.Protocol.Tls
  25. {
  26. #region Enumerations
  27. [Serializable]
  28. internal enum AlertLevel : byte
  29. {
  30. Warning = 1,
  31. Fatal = 2
  32. }
  33. [Serializable]
  34. internal enum AlertDescription : byte
  35. {
  36. CloseNotify = 0,
  37. UnexpectedMessage = 10,
  38. BadRecordMAC = 20,
  39. DecryptionFailed = 21,
  40. RecordOverflow = 22,
  41. DecompressionFailiure = 30,
  42. HandshakeFailiure = 40,
  43. NoCertificate = 41, // should be used in SSL3
  44. BadCertificate = 42,
  45. UnsupportedCertificate = 43,
  46. CertificateRevoked = 44,
  47. CertificateExpired = 45,
  48. CertificateUnknown = 46,
  49. IlegalParameter = 47,
  50. UnknownCA = 48,
  51. AccessDenied = 49,
  52. DecodeError = 50,
  53. DecryptError = 51,
  54. ExportRestriction = 60,
  55. ProtocolVersion = 70,
  56. InsuficientSecurity = 71,
  57. InternalError = 80,
  58. UserCancelled = 90,
  59. NoRenegotiation = 100
  60. }
  61. #endregion
  62. internal class Alert
  63. {
  64. #region Fields
  65. private AlertLevel level;
  66. private AlertDescription description;
  67. #endregion
  68. #region Properties
  69. public AlertLevel Level
  70. {
  71. get { return this.level; }
  72. }
  73. public AlertDescription Description
  74. {
  75. get { return this.description; }
  76. }
  77. public string Message
  78. {
  79. get { return Alert.GetAlertMessage(this.description); }
  80. }
  81. public bool IsWarning
  82. {
  83. get { return this.level == AlertLevel.Warning ? true : false; }
  84. }
  85. /*
  86. public bool IsFatal
  87. {
  88. get { return this.level == AlertLevel.Fatal ? true : false; }
  89. }
  90. */
  91. public bool IsCloseNotify
  92. {
  93. get
  94. {
  95. if (this.IsWarning &&
  96. this.description == AlertDescription.CloseNotify)
  97. {
  98. return true;
  99. }
  100. return false;
  101. }
  102. }
  103. #endregion
  104. #region Constructors
  105. public Alert(AlertDescription description)
  106. {
  107. this.inferAlertLevel();
  108. this.description = description;
  109. }
  110. public Alert(
  111. AlertLevel level,
  112. AlertDescription description)
  113. {
  114. this.level = level;
  115. this.description = description;
  116. }
  117. #endregion
  118. #region Private Methods
  119. private void inferAlertLevel()
  120. {
  121. switch (description)
  122. {
  123. case AlertDescription.CloseNotify:
  124. case AlertDescription.NoRenegotiation:
  125. case AlertDescription.UserCancelled:
  126. this.level = AlertLevel.Warning;
  127. break;
  128. case AlertDescription.AccessDenied:
  129. case AlertDescription.BadCertificate:
  130. case AlertDescription.BadRecordMAC:
  131. case AlertDescription.CertificateExpired:
  132. case AlertDescription.CertificateRevoked:
  133. case AlertDescription.CertificateUnknown:
  134. case AlertDescription.DecodeError:
  135. case AlertDescription.DecompressionFailiure:
  136. case AlertDescription.DecryptError:
  137. case AlertDescription.DecryptionFailed:
  138. case AlertDescription.ExportRestriction:
  139. case AlertDescription.HandshakeFailiure:
  140. case AlertDescription.IlegalParameter:
  141. case AlertDescription.InsuficientSecurity:
  142. case AlertDescription.InternalError:
  143. case AlertDescription.ProtocolVersion:
  144. case AlertDescription.RecordOverflow:
  145. case AlertDescription.UnexpectedMessage:
  146. case AlertDescription.UnknownCA:
  147. case AlertDescription.UnsupportedCertificate:
  148. default:
  149. this.level = AlertLevel.Fatal;
  150. break;
  151. }
  152. }
  153. #endregion
  154. #region Static Methods
  155. public static string GetAlertMessage(AlertDescription description)
  156. {
  157. #if (DEBUG)
  158. switch (description)
  159. {
  160. case AlertDescription.AccessDenied:
  161. return "An inappropriate message was received.";
  162. case AlertDescription.BadCertificate:
  163. return "TLSCiphertext decrypted in an invalid way.";
  164. case AlertDescription.BadRecordMAC:
  165. return "Record with an incorrect MAC.";
  166. case AlertDescription.CertificateExpired:
  167. return "Certificate has expired or is not currently valid";
  168. case AlertDescription.CertificateRevoked:
  169. return "Certificate was revoked by its signer.";
  170. case AlertDescription.CertificateUnknown:
  171. return "Certificate Unknown.";
  172. case AlertDescription.CloseNotify:
  173. return "Connection closed";
  174. case AlertDescription.DecodeError:
  175. return "A message could not be decoded because some field was out of the specified range or the length of the message was incorrect.";
  176. case AlertDescription.DecompressionFailiure:
  177. return "The decompression function received improper input (e.g. data that would expand to excessive length).";
  178. case AlertDescription.DecryptError:
  179. return "TLSCiphertext decrypted in an invalid way: either it wasn`t an even multiple of the block length or its padding values, when checked, weren`t correct.";
  180. case AlertDescription.DecryptionFailed:
  181. return "Handshake cryptographic operation failed, including being unable to correctly verify a signature, decrypt a key exchange, or validate finished message.";
  182. case AlertDescription.ExportRestriction:
  183. return "Negotiation not in compliance with export restrictions was detected.";
  184. case AlertDescription.HandshakeFailiure:
  185. return "Unable to negotiate an acceptable set of security parameters given the options available.";
  186. case AlertDescription.IlegalParameter:
  187. return "A field in the handshake was out of range or inconsistent with other fields.";
  188. case AlertDescription.InsuficientSecurity:
  189. return "Negotiation has failed specifically because the server requires ciphers more secure than those supported by the client.";
  190. case AlertDescription.InternalError:
  191. return "Internal error unrelated to the peer or the correctness of the protocol makes it impossible to continue.";
  192. case AlertDescription.NoRenegotiation:
  193. return "Invalid renegotiation.";
  194. case AlertDescription.ProtocolVersion:
  195. return "Unsupported protocol version.";
  196. case AlertDescription.RecordOverflow:
  197. return "Invalid length on TLSCiphertext record or TLSCompressed record.";
  198. case AlertDescription.UnexpectedMessage:
  199. return "Invalid message received.";
  200. case AlertDescription.UnknownCA:
  201. return "CA can't be identified as a trusted CA.";
  202. case AlertDescription.UnsupportedCertificate:
  203. return "Certificate was of an unsupported type.";
  204. case AlertDescription.UserCancelled:
  205. return "Handshake cancelled by user.";
  206. default:
  207. return "";
  208. }
  209. #else
  210. return "The authentication or decryption has failed.";
  211. #endif
  212. }
  213. #endregion
  214. }
  215. }