common.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package mint
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. const (
  7. supportedVersion uint16 = 0x7f16 // draft-22
  8. tls12Version uint16 = 0x0303
  9. tls10Version uint16 = 0x0301
  10. dtls12WireVersion uint16 = 0xfefd
  11. )
  12. var (
  13. // Flags for some minor compat issues
  14. allowWrongVersionNumber = true
  15. allowPKCS1 = true
  16. )
  17. // enum {...} ContentType;
  18. type RecordType byte
  19. const (
  20. RecordTypeAlert RecordType = 21
  21. RecordTypeHandshake RecordType = 22
  22. RecordTypeApplicationData RecordType = 23
  23. RecordTypeAck RecordType = 25
  24. )
  25. // enum {...} HandshakeType;
  26. type HandshakeType byte
  27. const (
  28. // Omitted: *_RESERVED
  29. HandshakeTypeClientHello HandshakeType = 1
  30. HandshakeTypeServerHello HandshakeType = 2
  31. HandshakeTypeNewSessionTicket HandshakeType = 4
  32. HandshakeTypeEndOfEarlyData HandshakeType = 5
  33. HandshakeTypeHelloRetryRequest HandshakeType = 6
  34. HandshakeTypeEncryptedExtensions HandshakeType = 8
  35. HandshakeTypeCertificate HandshakeType = 11
  36. HandshakeTypeCertificateRequest HandshakeType = 13
  37. HandshakeTypeCertificateVerify HandshakeType = 15
  38. HandshakeTypeServerConfiguration HandshakeType = 17
  39. HandshakeTypeFinished HandshakeType = 20
  40. HandshakeTypeKeyUpdate HandshakeType = 24
  41. HandshakeTypeMessageHash HandshakeType = 254
  42. )
  43. var hrrRandomSentinel = [32]byte{
  44. 0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11,
  45. 0xbe, 0x1d, 0x8c, 0x02, 0x1e, 0x65, 0xb8, 0x91,
  46. 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e,
  47. 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c,
  48. }
  49. // uint8 CipherSuite[2];
  50. type CipherSuite uint16
  51. const (
  52. // XXX: Actually TLS_NULL_WITH_NULL_NULL, but we need a way to label the zero
  53. // value for this type so that we can detect when a field is set.
  54. CIPHER_SUITE_UNKNOWN CipherSuite = 0x0000
  55. TLS_AES_128_GCM_SHA256 CipherSuite = 0x1301
  56. TLS_AES_256_GCM_SHA384 CipherSuite = 0x1302
  57. TLS_CHACHA20_POLY1305_SHA256 CipherSuite = 0x1303
  58. TLS_AES_128_CCM_SHA256 CipherSuite = 0x1304
  59. TLS_AES_256_CCM_8_SHA256 CipherSuite = 0x1305
  60. )
  61. func (c CipherSuite) String() string {
  62. switch c {
  63. case CIPHER_SUITE_UNKNOWN:
  64. return "unknown"
  65. case TLS_AES_128_GCM_SHA256:
  66. return "TLS_AES_128_GCM_SHA256"
  67. case TLS_AES_256_GCM_SHA384:
  68. return "TLS_AES_256_GCM_SHA384"
  69. case TLS_CHACHA20_POLY1305_SHA256:
  70. return "TLS_CHACHA20_POLY1305_SHA256"
  71. case TLS_AES_128_CCM_SHA256:
  72. return "TLS_AES_128_CCM_SHA256"
  73. case TLS_AES_256_CCM_8_SHA256:
  74. return "TLS_AES_256_CCM_8_SHA256"
  75. }
  76. // cannot use %x here, since it calls String(), leading to infinite recursion
  77. return fmt.Sprintf("invalid CipherSuite value: 0x%s", strconv.FormatUint(uint64(c), 16))
  78. }
  79. // enum {...} SignatureScheme
  80. type SignatureScheme uint16
  81. const (
  82. // RSASSA-PKCS1-v1_5 algorithms
  83. RSA_PKCS1_SHA1 SignatureScheme = 0x0201
  84. RSA_PKCS1_SHA256 SignatureScheme = 0x0401
  85. RSA_PKCS1_SHA384 SignatureScheme = 0x0501
  86. RSA_PKCS1_SHA512 SignatureScheme = 0x0601
  87. // ECDSA algorithms
  88. ECDSA_P256_SHA256 SignatureScheme = 0x0403
  89. ECDSA_P384_SHA384 SignatureScheme = 0x0503
  90. ECDSA_P521_SHA512 SignatureScheme = 0x0603
  91. // RSASSA-PSS algorithms
  92. RSA_PSS_SHA256 SignatureScheme = 0x0804
  93. RSA_PSS_SHA384 SignatureScheme = 0x0805
  94. RSA_PSS_SHA512 SignatureScheme = 0x0806
  95. // EdDSA algorithms
  96. Ed25519 SignatureScheme = 0x0807
  97. Ed448 SignatureScheme = 0x0808
  98. )
  99. // enum {...} ExtensionType
  100. type ExtensionType uint16
  101. const (
  102. ExtensionTypeServerName ExtensionType = 0
  103. ExtensionTypeSupportedGroups ExtensionType = 10
  104. ExtensionTypeSignatureAlgorithms ExtensionType = 13
  105. ExtensionTypeALPN ExtensionType = 16
  106. ExtensionTypeKeyShare ExtensionType = 40
  107. ExtensionTypePreSharedKey ExtensionType = 41
  108. ExtensionTypeEarlyData ExtensionType = 42
  109. ExtensionTypeSupportedVersions ExtensionType = 43
  110. ExtensionTypeCookie ExtensionType = 44
  111. ExtensionTypePSKKeyExchangeModes ExtensionType = 45
  112. ExtensionTypeTicketEarlyDataInfo ExtensionType = 46
  113. )
  114. // enum {...} NamedGroup
  115. type NamedGroup uint16
  116. const (
  117. // Elliptic Curve Groups.
  118. P256 NamedGroup = 23
  119. P384 NamedGroup = 24
  120. P521 NamedGroup = 25
  121. // ECDH functions.
  122. X25519 NamedGroup = 29
  123. X448 NamedGroup = 30
  124. // Finite field groups.
  125. FFDHE2048 NamedGroup = 256
  126. FFDHE3072 NamedGroup = 257
  127. FFDHE4096 NamedGroup = 258
  128. FFDHE6144 NamedGroup = 259
  129. FFDHE8192 NamedGroup = 260
  130. )
  131. // enum {...} PskKeyExchangeMode;
  132. type PSKKeyExchangeMode uint8
  133. const (
  134. PSKModeKE PSKKeyExchangeMode = 0
  135. PSKModeDHEKE PSKKeyExchangeMode = 1
  136. )
  137. // enum {
  138. // update_not_requested(0), update_requested(1), (255)
  139. // } KeyUpdateRequest;
  140. type KeyUpdateRequest uint8
  141. const (
  142. KeyUpdateNotRequested KeyUpdateRequest = 0
  143. KeyUpdateRequested KeyUpdateRequest = 1
  144. )
  145. type State uint8
  146. const (
  147. StateInit = 0
  148. // states valid for the client
  149. StateClientStart State = iota
  150. StateClientWaitSH
  151. StateClientWaitEE
  152. StateClientWaitCert
  153. StateClientWaitCV
  154. StateClientWaitFinished
  155. StateClientWaitCertCR
  156. StateClientConnected
  157. // states valid for the server
  158. StateServerStart State = iota
  159. StateServerRecvdCH
  160. StateServerNegotiated
  161. StateServerReadPastEarlyData
  162. StateServerWaitEOED
  163. StateServerWaitFlight2
  164. StateServerWaitCert
  165. StateServerWaitCV
  166. StateServerWaitFinished
  167. StateServerConnected
  168. )
  169. func (s State) String() string {
  170. switch s {
  171. case StateClientStart:
  172. return "Client START"
  173. case StateClientWaitSH:
  174. return "Client WAIT_SH"
  175. case StateClientWaitEE:
  176. return "Client WAIT_EE"
  177. case StateClientWaitCert:
  178. return "Client WAIT_CERT"
  179. case StateClientWaitCV:
  180. return "Client WAIT_CV"
  181. case StateClientWaitFinished:
  182. return "Client WAIT_FINISHED"
  183. case StateClientWaitCertCR:
  184. return "Client WAIT_CERT_CR"
  185. case StateClientConnected:
  186. return "Client CONNECTED"
  187. case StateServerStart:
  188. return "Server START"
  189. case StateServerRecvdCH:
  190. return "Server RECVD_CH"
  191. case StateServerNegotiated:
  192. return "Server NEGOTIATED"
  193. case StateServerReadPastEarlyData:
  194. return "Server READ_PAST_EARLY_DATA"
  195. case StateServerWaitEOED:
  196. return "Server WAIT_EOED"
  197. case StateServerWaitFlight2:
  198. return "Server WAIT_FLIGHT2"
  199. case StateServerWaitCert:
  200. return "Server WAIT_CERT"
  201. case StateServerWaitCV:
  202. return "Server WAIT_CV"
  203. case StateServerWaitFinished:
  204. return "Server WAIT_FINISHED"
  205. case StateServerConnected:
  206. return "Server CONNECTED"
  207. default:
  208. return fmt.Sprintf("unknown state: %d", s)
  209. }
  210. }
  211. // Epochs for DTLS (also used for key phase labelling)
  212. type Epoch uint16
  213. const (
  214. EpochClear Epoch = 0
  215. EpochEarlyData Epoch = 1
  216. EpochHandshakeData Epoch = 2
  217. EpochApplicationData Epoch = 3
  218. EpochUpdate Epoch = 4
  219. )
  220. func (e Epoch) label() string {
  221. switch e {
  222. case EpochClear:
  223. return "clear"
  224. case EpochEarlyData:
  225. return "early data"
  226. case EpochHandshakeData:
  227. return "handshake"
  228. case EpochApplicationData:
  229. return "application data"
  230. }
  231. return "Application data (updated)"
  232. }
  233. func assert(b bool) {
  234. if !b {
  235. panic("Assertion failed")
  236. }
  237. }