ktls_handshake_messages.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build linux && go1.25 && badlinkname
  5. package ktls
  6. import (
  7. "fmt"
  8. "golang.org/x/crypto/cryptobyte"
  9. )
  10. // The marshalingFunction type is an adapter to allow the use of ordinary
  11. // functions as cryptobyte.MarshalingValue.
  12. type marshalingFunction func(b *cryptobyte.Builder) error
  13. func (f marshalingFunction) Marshal(b *cryptobyte.Builder) error {
  14. return f(b)
  15. }
  16. // addBytesWithLength appends a sequence of bytes to the cryptobyte.Builder. If
  17. // the length of the sequence is not the value specified, it produces an error.
  18. func addBytesWithLength(b *cryptobyte.Builder, v []byte, n int) {
  19. b.AddValue(marshalingFunction(func(b *cryptobyte.Builder) error {
  20. if len(v) != n {
  21. return fmt.Errorf("invalid value length: expected %d, got %d", n, len(v))
  22. }
  23. b.AddBytes(v)
  24. return nil
  25. }))
  26. }
  27. // addUint64 appends a big-endian, 64-bit value to the cryptobyte.Builder.
  28. func addUint64(b *cryptobyte.Builder, v uint64) {
  29. b.AddUint32(uint32(v >> 32))
  30. b.AddUint32(uint32(v))
  31. }
  32. // readUint64 decodes a big-endian, 64-bit value into out and advances over it.
  33. // It reports whether the read was successful.
  34. func readUint64(s *cryptobyte.String, out *uint64) bool {
  35. var hi, lo uint32
  36. if !s.ReadUint32(&hi) || !s.ReadUint32(&lo) {
  37. return false
  38. }
  39. *out = uint64(hi)<<32 | uint64(lo)
  40. return true
  41. }
  42. // readUint8LengthPrefixed acts like s.ReadUint8LengthPrefixed, but targets a
  43. // []byte instead of a cryptobyte.String.
  44. func readUint8LengthPrefixed(s *cryptobyte.String, out *[]byte) bool {
  45. return s.ReadUint8LengthPrefixed((*cryptobyte.String)(out))
  46. }
  47. // readUint16LengthPrefixed acts like s.ReadUint16LengthPrefixed, but targets a
  48. // []byte instead of a cryptobyte.String.
  49. func readUint16LengthPrefixed(s *cryptobyte.String, out *[]byte) bool {
  50. return s.ReadUint16LengthPrefixed((*cryptobyte.String)(out))
  51. }
  52. // readUint24LengthPrefixed acts like s.ReadUint24LengthPrefixed, but targets a
  53. // []byte instead of a cryptobyte.String.
  54. func readUint24LengthPrefixed(s *cryptobyte.String, out *[]byte) bool {
  55. return s.ReadUint24LengthPrefixed((*cryptobyte.String)(out))
  56. }
  57. type keyUpdateMsg struct {
  58. updateRequested bool
  59. }
  60. func (m *keyUpdateMsg) marshal() ([]byte, error) {
  61. var b cryptobyte.Builder
  62. b.AddUint8(typeKeyUpdate)
  63. b.AddUint24LengthPrefixed(func(b *cryptobyte.Builder) {
  64. if m.updateRequested {
  65. b.AddUint8(1)
  66. } else {
  67. b.AddUint8(0)
  68. }
  69. })
  70. return b.Bytes()
  71. }
  72. func (m *keyUpdateMsg) unmarshal(data []byte) bool {
  73. s := cryptobyte.String(data)
  74. var updateRequested uint8
  75. if !s.Skip(4) || // message type and uint24 length field
  76. !s.ReadUint8(&updateRequested) || !s.Empty() {
  77. return false
  78. }
  79. switch updateRequested {
  80. case 0:
  81. m.updateRequested = false
  82. case 1:
  83. m.updateRequested = true
  84. default:
  85. return false
  86. }
  87. return true
  88. }
  89. // TLS handshake message types.
  90. const (
  91. typeHelloRequest uint8 = 0
  92. typeClientHello uint8 = 1
  93. typeServerHello uint8 = 2
  94. typeNewSessionTicket uint8 = 4
  95. typeEndOfEarlyData uint8 = 5
  96. typeEncryptedExtensions uint8 = 8
  97. typeCertificate uint8 = 11
  98. typeServerKeyExchange uint8 = 12
  99. typeCertificateRequest uint8 = 13
  100. typeServerHelloDone uint8 = 14
  101. typeCertificateVerify uint8 = 15
  102. typeClientKeyExchange uint8 = 16
  103. typeFinished uint8 = 20
  104. typeCertificateStatus uint8 = 22
  105. typeKeyUpdate uint8 = 24
  106. typeCompressedCertificate uint8 = 25
  107. typeMessageHash uint8 = 254 // synthetic message
  108. )
  109. // TLS compression types.
  110. const (
  111. compressionNone uint8 = 0
  112. )
  113. // TLS extension numbers
  114. const (
  115. extensionServerName uint16 = 0
  116. extensionStatusRequest uint16 = 5
  117. extensionSupportedCurves uint16 = 10 // supported_groups in TLS 1.3, see RFC 8446, Section 4.2.7
  118. extensionSupportedPoints uint16 = 11
  119. extensionSignatureAlgorithms uint16 = 13
  120. extensionALPN uint16 = 16
  121. extensionSCT uint16 = 18
  122. extensionPadding uint16 = 21
  123. extensionExtendedMasterSecret uint16 = 23
  124. extensionCompressCertificate uint16 = 27 // compress_certificate in TLS 1.3
  125. extensionSessionTicket uint16 = 35
  126. extensionPreSharedKey uint16 = 41
  127. extensionEarlyData uint16 = 42
  128. extensionSupportedVersions uint16 = 43
  129. extensionCookie uint16 = 44
  130. extensionPSKModes uint16 = 45
  131. extensionCertificateAuthorities uint16 = 47
  132. extensionSignatureAlgorithmsCert uint16 = 50
  133. extensionKeyShare uint16 = 51
  134. extensionQUICTransportParameters uint16 = 57
  135. extensionALPS uint16 = 17513
  136. extensionRenegotiationInfo uint16 = 0xff01
  137. extensionECHOuterExtensions uint16 = 0xfd00
  138. extensionEncryptedClientHello uint16 = 0xfe0d
  139. )
  140. type handshakeMessage interface {
  141. marshal() ([]byte, error)
  142. unmarshal([]byte) bool
  143. }
  144. type newSessionTicketMsgTLS13 struct {
  145. lifetime uint32
  146. ageAdd uint32
  147. nonce []byte
  148. label []byte
  149. maxEarlyData uint32
  150. }
  151. func (m *newSessionTicketMsgTLS13) marshal() ([]byte, error) {
  152. var b cryptobyte.Builder
  153. b.AddUint8(typeNewSessionTicket)
  154. b.AddUint24LengthPrefixed(func(b *cryptobyte.Builder) {
  155. b.AddUint32(m.lifetime)
  156. b.AddUint32(m.ageAdd)
  157. b.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
  158. b.AddBytes(m.nonce)
  159. })
  160. b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
  161. b.AddBytes(m.label)
  162. })
  163. b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
  164. if m.maxEarlyData > 0 {
  165. b.AddUint16(extensionEarlyData)
  166. b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
  167. b.AddUint32(m.maxEarlyData)
  168. })
  169. }
  170. })
  171. })
  172. return b.Bytes()
  173. }
  174. func (m *newSessionTicketMsgTLS13) unmarshal(data []byte) bool {
  175. *m = newSessionTicketMsgTLS13{}
  176. s := cryptobyte.String(data)
  177. var extensions cryptobyte.String
  178. if !s.Skip(4) || // message type and uint24 length field
  179. !s.ReadUint32(&m.lifetime) ||
  180. !s.ReadUint32(&m.ageAdd) ||
  181. !readUint8LengthPrefixed(&s, &m.nonce) ||
  182. !readUint16LengthPrefixed(&s, &m.label) ||
  183. !s.ReadUint16LengthPrefixed(&extensions) ||
  184. !s.Empty() {
  185. return false
  186. }
  187. for !extensions.Empty() {
  188. var extension uint16
  189. var extData cryptobyte.String
  190. if !extensions.ReadUint16(&extension) ||
  191. !extensions.ReadUint16LengthPrefixed(&extData) {
  192. return false
  193. }
  194. switch extension {
  195. case extensionEarlyData:
  196. if !extData.ReadUint32(&m.maxEarlyData) {
  197. return false
  198. }
  199. default:
  200. // Ignore unknown extensions.
  201. continue
  202. }
  203. if !extData.Empty() {
  204. return false
  205. }
  206. }
  207. return true
  208. }