handshake_messages.go 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781
  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. package qtls
  5. import (
  6. "bytes"
  7. "encoding/binary"
  8. "strings"
  9. )
  10. // signAlgosCertList helper function returns either list of signature algorithms in case
  11. // signature_algorithms_cert extension should be marshalled or nil in the other case.
  12. // signAlgos is a list of algorithms from signature_algorithms extension. signAlgosCert is a list
  13. // of algorithms from signature_algorithms_cert extension.
  14. func signAlgosCertList(signAlgos, signAlgosCert []SignatureScheme) []SignatureScheme {
  15. if eqSignatureAlgorithms(signAlgos, signAlgosCert) {
  16. // ensure that only supported_algorithms extension is send if supported_algorithms_cert
  17. // has identical content
  18. return nil
  19. }
  20. return signAlgosCert
  21. }
  22. type clientHelloMsg struct {
  23. raw []byte
  24. rawTruncated []byte // for PSK binding
  25. vers uint16
  26. random []byte
  27. sessionId []byte
  28. cipherSuites []uint16
  29. compressionMethods []uint8
  30. nextProtoNeg bool
  31. serverName string
  32. ocspStapling bool
  33. scts bool
  34. supportedCurves []CurveID
  35. supportedPoints []uint8
  36. ticketSupported bool
  37. sessionTicket []uint8
  38. supportedSignatureAlgorithms []SignatureScheme
  39. supportedSignatureAlgorithmsCert []SignatureScheme
  40. secureRenegotiation []byte
  41. secureRenegotiationSupported bool
  42. alpnProtocols []string
  43. keyShares []keyShare
  44. supportedVersions []uint16
  45. psks []psk
  46. pskKeyExchangeModes []uint8
  47. earlyData bool
  48. delegatedCredential bool
  49. extendedMSSupported bool // RFC7627
  50. additionalExtensions []Extension
  51. }
  52. // Function used for signature_algorithms and signature_algorithrms_cert
  53. // extensions only (for more details, see TLS 1.3 draft 28, 4.2.3).
  54. //
  55. // It advances data slice and returns it, so that it can be used for further
  56. // processing
  57. func marshalExtensionSignatureAlgorithms(extension uint16, data []byte, schemes []SignatureScheme) []byte {
  58. algNum := uint16(len(schemes))
  59. if algNum == 0 {
  60. return data
  61. }
  62. binary.BigEndian.PutUint16(data, extension)
  63. data = data[2:]
  64. binary.BigEndian.PutUint16(data, (2*algNum)+2) // +1 for length
  65. data = data[2:]
  66. binary.BigEndian.PutUint16(data, (2 * algNum))
  67. data = data[2:]
  68. for _, algo := range schemes {
  69. binary.BigEndian.PutUint16(data, uint16(algo))
  70. data = data[2:]
  71. }
  72. return data
  73. }
  74. // Function used for unmarshalling signature_algorithms or signature_algorithms_cert extensions only
  75. // (for more details, see TLS 1.3 draft 28, 4.2.3)
  76. // In case of error function returns alertDecoderError otherwise filled SignatureScheme slice and alertSuccess
  77. func unmarshalExtensionSignatureAlgorithms(data []byte, length int) ([]SignatureScheme, alert) {
  78. if length < 2 || length&1 != 0 {
  79. return nil, alertDecodeError
  80. }
  81. algLen := binary.BigEndian.Uint16(data)
  82. idx := 2
  83. if int(algLen) != length-2 {
  84. return nil, alertDecodeError
  85. }
  86. schemes := make([]SignatureScheme, algLen/2)
  87. for i := range schemes {
  88. schemes[i] = SignatureScheme(binary.BigEndian.Uint16(data[idx:]))
  89. idx += 2
  90. }
  91. return schemes, alertSuccess
  92. }
  93. func (m *clientHelloMsg) equal(i interface{}) bool {
  94. m1, ok := i.(*clientHelloMsg)
  95. if !ok {
  96. return false
  97. }
  98. if len(m.additionalExtensions) != len(m1.additionalExtensions) {
  99. return false
  100. }
  101. for i, ex := range m.additionalExtensions {
  102. ex1 := m1.additionalExtensions[i]
  103. if ex.Type != ex1.Type || !bytes.Equal(ex.Data, ex1.Data) {
  104. return false
  105. }
  106. }
  107. return bytes.Equal(m.raw, m1.raw) &&
  108. m.vers == m1.vers &&
  109. bytes.Equal(m.random, m1.random) &&
  110. bytes.Equal(m.sessionId, m1.sessionId) &&
  111. eqUint16s(m.cipherSuites, m1.cipherSuites) &&
  112. bytes.Equal(m.compressionMethods, m1.compressionMethods) &&
  113. m.nextProtoNeg == m1.nextProtoNeg &&
  114. m.serverName == m1.serverName &&
  115. m.ocspStapling == m1.ocspStapling &&
  116. m.scts == m1.scts &&
  117. eqCurveIDs(m.supportedCurves, m1.supportedCurves) &&
  118. bytes.Equal(m.supportedPoints, m1.supportedPoints) &&
  119. m.ticketSupported == m1.ticketSupported &&
  120. bytes.Equal(m.sessionTicket, m1.sessionTicket) &&
  121. eqSignatureAlgorithms(m.supportedSignatureAlgorithms, m1.supportedSignatureAlgorithms) &&
  122. eqSignatureAlgorithms(m.supportedSignatureAlgorithmsCert, m1.supportedSignatureAlgorithmsCert) &&
  123. m.secureRenegotiationSupported == m1.secureRenegotiationSupported &&
  124. bytes.Equal(m.secureRenegotiation, m1.secureRenegotiation) &&
  125. eqStrings(m.alpnProtocols, m1.alpnProtocols) &&
  126. eqKeyShares(m.keyShares, m1.keyShares) &&
  127. eqUint16s(m.supportedVersions, m1.supportedVersions) &&
  128. m.earlyData == m1.earlyData &&
  129. m.delegatedCredential == m1.delegatedCredential &&
  130. m.extendedMSSupported == m1.extendedMSSupported
  131. }
  132. func (m *clientHelloMsg) marshal() []byte {
  133. if m.raw != nil {
  134. return m.raw
  135. }
  136. length := 2 + 32 + 1 + len(m.sessionId) + 2 + len(m.cipherSuites)*2 + 1 + len(m.compressionMethods)
  137. numExtensions := 0
  138. extensionsLength := 0
  139. if m.nextProtoNeg {
  140. numExtensions++
  141. }
  142. if m.ocspStapling {
  143. extensionsLength += 1 + 2 + 2
  144. numExtensions++
  145. }
  146. if len(m.serverName) > 0 {
  147. extensionsLength += 5 + len(m.serverName)
  148. numExtensions++
  149. }
  150. if len(m.supportedCurves) > 0 {
  151. extensionsLength += 2 + 2*len(m.supportedCurves)
  152. numExtensions++
  153. }
  154. if len(m.supportedPoints) > 0 {
  155. extensionsLength += 1 + len(m.supportedPoints)
  156. numExtensions++
  157. }
  158. if m.ticketSupported {
  159. extensionsLength += len(m.sessionTicket)
  160. numExtensions++
  161. }
  162. if len(m.supportedSignatureAlgorithms) > 0 {
  163. extensionsLength += 2 + 2*len(m.supportedSignatureAlgorithms)
  164. numExtensions++
  165. }
  166. if m.getSignatureAlgorithmsCert() != nil {
  167. extensionsLength += 2 + 2*len(m.getSignatureAlgorithmsCert())
  168. numExtensions++
  169. }
  170. if m.secureRenegotiationSupported {
  171. extensionsLength += 1 + len(m.secureRenegotiation)
  172. numExtensions++
  173. }
  174. if len(m.alpnProtocols) > 0 {
  175. extensionsLength += 2
  176. for _, s := range m.alpnProtocols {
  177. if l := len(s); l == 0 || l > 255 {
  178. panic("invalid ALPN protocol")
  179. }
  180. extensionsLength++
  181. extensionsLength += len(s)
  182. }
  183. numExtensions++
  184. }
  185. if m.scts {
  186. numExtensions++
  187. }
  188. if len(m.keyShares) > 0 {
  189. extensionsLength += 2
  190. for _, k := range m.keyShares {
  191. extensionsLength += 4 + len(k.data)
  192. }
  193. numExtensions++
  194. }
  195. if len(m.supportedVersions) > 0 {
  196. extensionsLength += 1 + 2*len(m.supportedVersions)
  197. numExtensions++
  198. }
  199. if m.earlyData {
  200. numExtensions++
  201. }
  202. if m.delegatedCredential {
  203. numExtensions++
  204. }
  205. if m.extendedMSSupported {
  206. numExtensions++
  207. }
  208. if len(m.additionalExtensions) > 0 {
  209. numExtensions += len(m.additionalExtensions)
  210. for _, ex := range m.additionalExtensions {
  211. extensionsLength += len(ex.Data)
  212. }
  213. }
  214. if numExtensions > 0 {
  215. extensionsLength += 4 * numExtensions
  216. length += 2 + extensionsLength
  217. }
  218. x := make([]byte, 4+length)
  219. x[0] = typeClientHello
  220. x[1] = uint8(length >> 16)
  221. x[2] = uint8(length >> 8)
  222. x[3] = uint8(length)
  223. x[4] = uint8(m.vers >> 8)
  224. x[5] = uint8(m.vers)
  225. copy(x[6:38], m.random)
  226. x[38] = uint8(len(m.sessionId))
  227. copy(x[39:39+len(m.sessionId)], m.sessionId)
  228. y := x[39+len(m.sessionId):]
  229. y[0] = uint8(len(m.cipherSuites) >> 7)
  230. y[1] = uint8(len(m.cipherSuites) << 1)
  231. for i, suite := range m.cipherSuites {
  232. y[2+i*2] = uint8(suite >> 8)
  233. y[3+i*2] = uint8(suite)
  234. }
  235. z := y[2+len(m.cipherSuites)*2:]
  236. z[0] = uint8(len(m.compressionMethods))
  237. copy(z[1:], m.compressionMethods)
  238. z = z[1+len(m.compressionMethods):]
  239. if numExtensions > 0 {
  240. z[0] = byte(extensionsLength >> 8)
  241. z[1] = byte(extensionsLength)
  242. z = z[2:]
  243. }
  244. if m.nextProtoNeg {
  245. z[0] = byte(extensionNextProtoNeg >> 8)
  246. z[1] = byte(extensionNextProtoNeg & 0xff)
  247. // The length is always 0
  248. z = z[4:]
  249. }
  250. if len(m.serverName) > 0 {
  251. z[0] = byte(extensionServerName >> 8)
  252. z[1] = byte(extensionServerName & 0xff)
  253. l := len(m.serverName) + 5
  254. z[2] = byte(l >> 8)
  255. z[3] = byte(l)
  256. z = z[4:]
  257. // RFC 3546, section 3.1
  258. //
  259. // struct {
  260. // NameType name_type;
  261. // select (name_type) {
  262. // case host_name: HostName;
  263. // } name;
  264. // } ServerName;
  265. //
  266. // enum {
  267. // host_name(0), (255)
  268. // } NameType;
  269. //
  270. // opaque HostName<1..2^16-1>;
  271. //
  272. // struct {
  273. // ServerName server_name_list<1..2^16-1>
  274. // } ServerNameList;
  275. z[0] = byte((len(m.serverName) + 3) >> 8)
  276. z[1] = byte(len(m.serverName) + 3)
  277. z[3] = byte(len(m.serverName) >> 8)
  278. z[4] = byte(len(m.serverName))
  279. copy(z[5:], []byte(m.serverName))
  280. z = z[l:]
  281. }
  282. if m.ocspStapling {
  283. // RFC 4366, section 3.6
  284. z[0] = byte(extensionStatusRequest >> 8)
  285. z[1] = byte(extensionStatusRequest)
  286. z[2] = 0
  287. z[3] = 5
  288. z[4] = 1 // OCSP type
  289. // Two zero valued uint16s for the two lengths.
  290. z = z[9:]
  291. }
  292. if len(m.supportedCurves) > 0 {
  293. // http://tools.ietf.org/html/rfc4492#section-5.5.1
  294. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.4
  295. z[0] = byte(extensionSupportedCurves >> 8)
  296. z[1] = byte(extensionSupportedCurves)
  297. l := 2 + 2*len(m.supportedCurves)
  298. z[2] = byte(l >> 8)
  299. z[3] = byte(l)
  300. l -= 2
  301. z[4] = byte(l >> 8)
  302. z[5] = byte(l)
  303. z = z[6:]
  304. for _, curve := range m.supportedCurves {
  305. z[0] = byte(curve >> 8)
  306. z[1] = byte(curve)
  307. z = z[2:]
  308. }
  309. }
  310. if len(m.supportedPoints) > 0 {
  311. // http://tools.ietf.org/html/rfc4492#section-5.5.2
  312. z[0] = byte(extensionSupportedPoints >> 8)
  313. z[1] = byte(extensionSupportedPoints)
  314. l := 1 + len(m.supportedPoints)
  315. z[2] = byte(l >> 8)
  316. z[3] = byte(l)
  317. l--
  318. z[4] = byte(l)
  319. z = z[5:]
  320. for _, pointFormat := range m.supportedPoints {
  321. z[0] = pointFormat
  322. z = z[1:]
  323. }
  324. }
  325. if m.ticketSupported {
  326. // http://tools.ietf.org/html/rfc5077#section-3.2
  327. z[0] = byte(extensionSessionTicket >> 8)
  328. z[1] = byte(extensionSessionTicket)
  329. l := len(m.sessionTicket)
  330. z[2] = byte(l >> 8)
  331. z[3] = byte(l)
  332. z = z[4:]
  333. copy(z, m.sessionTicket)
  334. z = z[len(m.sessionTicket):]
  335. }
  336. if len(m.supportedSignatureAlgorithms) > 0 {
  337. z = marshalExtensionSignatureAlgorithms(extensionSignatureAlgorithms, z, m.supportedSignatureAlgorithms)
  338. }
  339. if m.getSignatureAlgorithmsCert() != nil {
  340. // Ensure only one list of algorithms is sent if supported_algorithms and supported_algorithms_cert are the same
  341. z = marshalExtensionSignatureAlgorithms(extensionSignatureAlgorithmsCert, z, m.getSignatureAlgorithmsCert())
  342. }
  343. if m.secureRenegotiationSupported {
  344. z[0] = byte(extensionRenegotiationInfo >> 8)
  345. z[1] = byte(extensionRenegotiationInfo & 0xff)
  346. z[2] = 0
  347. z[3] = byte(len(m.secureRenegotiation) + 1)
  348. z[4] = byte(len(m.secureRenegotiation))
  349. z = z[5:]
  350. copy(z, m.secureRenegotiation)
  351. z = z[len(m.secureRenegotiation):]
  352. }
  353. if len(m.alpnProtocols) > 0 {
  354. z[0] = byte(extensionALPN >> 8)
  355. z[1] = byte(extensionALPN & 0xff)
  356. lengths := z[2:]
  357. z = z[6:]
  358. stringsLength := 0
  359. for _, s := range m.alpnProtocols {
  360. l := len(s)
  361. z[0] = byte(l)
  362. copy(z[1:], s)
  363. z = z[1+l:]
  364. stringsLength += 1 + l
  365. }
  366. lengths[2] = byte(stringsLength >> 8)
  367. lengths[3] = byte(stringsLength)
  368. stringsLength += 2
  369. lengths[0] = byte(stringsLength >> 8)
  370. lengths[1] = byte(stringsLength)
  371. }
  372. if m.scts {
  373. // https://tools.ietf.org/html/rfc6962#section-3.3.1
  374. z[0] = byte(extensionSCT >> 8)
  375. z[1] = byte(extensionSCT)
  376. // zero uint16 for the zero-length extension_data
  377. z = z[4:]
  378. }
  379. if len(m.keyShares) > 0 {
  380. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.5
  381. z[0] = byte(extensionKeyShare >> 8)
  382. z[1] = byte(extensionKeyShare)
  383. lengths := z[2:]
  384. z = z[6:]
  385. totalLength := 0
  386. for _, ks := range m.keyShares {
  387. z[0] = byte(ks.group >> 8)
  388. z[1] = byte(ks.group)
  389. z[2] = byte(len(ks.data) >> 8)
  390. z[3] = byte(len(ks.data))
  391. copy(z[4:], ks.data)
  392. z = z[4+len(ks.data):]
  393. totalLength += 4 + len(ks.data)
  394. }
  395. lengths[2] = byte(totalLength >> 8)
  396. lengths[3] = byte(totalLength)
  397. totalLength += 2
  398. lengths[0] = byte(totalLength >> 8)
  399. lengths[1] = byte(totalLength)
  400. }
  401. if len(m.supportedVersions) > 0 {
  402. z[0] = byte(extensionSupportedVersions >> 8)
  403. z[1] = byte(extensionSupportedVersions)
  404. l := 1 + 2*len(m.supportedVersions)
  405. z[2] = byte(l >> 8)
  406. z[3] = byte(l)
  407. l -= 1
  408. z[4] = byte(l)
  409. z = z[5:]
  410. for _, v := range m.supportedVersions {
  411. z[0] = byte(v >> 8)
  412. z[1] = byte(v)
  413. z = z[2:]
  414. }
  415. }
  416. if m.earlyData {
  417. z[0] = byte(extensionEarlyData >> 8)
  418. z[1] = byte(extensionEarlyData)
  419. z = z[4:]
  420. }
  421. if m.delegatedCredential {
  422. binary.BigEndian.PutUint16(z, extensionDelegatedCredential)
  423. z = z[4:]
  424. }
  425. if m.extendedMSSupported {
  426. binary.BigEndian.PutUint16(z, extensionEMS)
  427. z = z[4:]
  428. }
  429. for _, ex := range m.additionalExtensions {
  430. z[0] = byte(ex.Type >> 8)
  431. z[1] = byte(ex.Type)
  432. l := len(ex.Data)
  433. z[2] = byte(l >> 8)
  434. z[3] = byte(l)
  435. copy(z[4:], ex.Data)
  436. z = z[4+l:]
  437. }
  438. m.raw = x
  439. return x
  440. }
  441. func (m *clientHelloMsg) unmarshal(data []byte) alert {
  442. if len(data) < 42 {
  443. return alertDecodeError
  444. }
  445. m.raw = data
  446. m.vers = uint16(data[4])<<8 | uint16(data[5])
  447. m.random = data[6:38]
  448. sessionIdLen := int(data[38])
  449. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  450. return alertDecodeError
  451. }
  452. m.sessionId = data[39 : 39+sessionIdLen]
  453. data = data[39+sessionIdLen:]
  454. bindersOffset := 39 + sessionIdLen
  455. if len(data) < 2 {
  456. return alertDecodeError
  457. }
  458. // cipherSuiteLen is the number of bytes of cipher suite numbers. Since
  459. // they are uint16s, the number must be even.
  460. cipherSuiteLen := int(data[0])<<8 | int(data[1])
  461. if cipherSuiteLen%2 == 1 || len(data) < 2+cipherSuiteLen {
  462. return alertDecodeError
  463. }
  464. numCipherSuites := cipherSuiteLen / 2
  465. m.cipherSuites = make([]uint16, numCipherSuites)
  466. for i := 0; i < numCipherSuites; i++ {
  467. m.cipherSuites[i] = uint16(data[2+2*i])<<8 | uint16(data[3+2*i])
  468. if m.cipherSuites[i] == scsvRenegotiation {
  469. m.secureRenegotiationSupported = true
  470. }
  471. }
  472. data = data[2+cipherSuiteLen:]
  473. bindersOffset += 2 + cipherSuiteLen
  474. if len(data) < 1 {
  475. return alertDecodeError
  476. }
  477. compressionMethodsLen := int(data[0])
  478. if len(data) < 1+compressionMethodsLen {
  479. return alertDecodeError
  480. }
  481. m.compressionMethods = data[1 : 1+compressionMethodsLen]
  482. data = data[1+compressionMethodsLen:]
  483. bindersOffset += 1 + compressionMethodsLen
  484. m.nextProtoNeg = false
  485. m.serverName = ""
  486. m.ocspStapling = false
  487. m.ticketSupported = false
  488. m.sessionTicket = nil
  489. m.supportedSignatureAlgorithms = nil
  490. m.alpnProtocols = nil
  491. m.scts = false
  492. m.keyShares = nil
  493. m.supportedVersions = nil
  494. m.psks = nil
  495. m.pskKeyExchangeModes = nil
  496. m.earlyData = false
  497. m.delegatedCredential = false
  498. m.extendedMSSupported = false
  499. if len(data) == 0 {
  500. // ClientHello is optionally followed by extension data
  501. return alertSuccess
  502. }
  503. if len(data) < 2 {
  504. return alertDecodeError
  505. }
  506. extensionsLength := int(data[0])<<8 | int(data[1])
  507. data = data[2:]
  508. bindersOffset += 2
  509. if extensionsLength != len(data) {
  510. return alertDecodeError
  511. }
  512. for len(data) != 0 {
  513. if len(data) < 4 {
  514. return alertDecodeError
  515. }
  516. ext := uint16(data[0])<<8 | uint16(data[1])
  517. length := int(data[2])<<8 | int(data[3])
  518. data = data[4:]
  519. bindersOffset += 4
  520. if len(data) < length {
  521. return alertDecodeError
  522. }
  523. switch ext {
  524. case extensionServerName:
  525. d := data[:length]
  526. if len(d) < 2 {
  527. return alertDecodeError
  528. }
  529. namesLen := int(d[0])<<8 | int(d[1])
  530. d = d[2:]
  531. if len(d) != namesLen {
  532. return alertDecodeError
  533. }
  534. for len(d) > 0 {
  535. if len(d) < 3 {
  536. return alertDecodeError
  537. }
  538. nameType := d[0]
  539. nameLen := int(d[1])<<8 | int(d[2])
  540. d = d[3:]
  541. if len(d) < nameLen {
  542. return alertDecodeError
  543. }
  544. if nameType == 0 {
  545. m.serverName = string(d[:nameLen])
  546. // An SNI value may not include a
  547. // trailing dot. See
  548. // https://tools.ietf.org/html/rfc6066#section-3.
  549. if strings.HasSuffix(m.serverName, ".") {
  550. // TODO use alertDecodeError?
  551. return alertUnexpectedMessage
  552. }
  553. break
  554. }
  555. d = d[nameLen:]
  556. }
  557. case extensionNextProtoNeg:
  558. if length > 0 {
  559. return alertDecodeError
  560. }
  561. m.nextProtoNeg = true
  562. case extensionStatusRequest:
  563. m.ocspStapling = length > 0 && data[0] == statusTypeOCSP
  564. case extensionSupportedCurves:
  565. // http://tools.ietf.org/html/rfc4492#section-5.5.1
  566. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.4
  567. if length < 2 {
  568. return alertDecodeError
  569. }
  570. l := int(data[0])<<8 | int(data[1])
  571. if l%2 == 1 || length != l+2 {
  572. return alertDecodeError
  573. }
  574. numCurves := l / 2
  575. m.supportedCurves = make([]CurveID, numCurves)
  576. d := data[2:]
  577. for i := 0; i < numCurves; i++ {
  578. m.supportedCurves[i] = CurveID(d[0])<<8 | CurveID(d[1])
  579. d = d[2:]
  580. }
  581. case extensionSupportedPoints:
  582. // http://tools.ietf.org/html/rfc4492#section-5.5.2
  583. if length < 1 {
  584. return alertDecodeError
  585. }
  586. l := int(data[0])
  587. if length != l+1 {
  588. return alertDecodeError
  589. }
  590. m.supportedPoints = make([]uint8, l)
  591. copy(m.supportedPoints, data[1:])
  592. case extensionSessionTicket:
  593. // http://tools.ietf.org/html/rfc5077#section-3.2
  594. m.ticketSupported = true
  595. m.sessionTicket = data[:length]
  596. case extensionSignatureAlgorithms:
  597. // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
  598. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.3
  599. if length < 2 || length&1 != 0 {
  600. return alertDecodeError
  601. }
  602. l := int(data[0])<<8 | int(data[1])
  603. if l != length-2 {
  604. return alertDecodeError
  605. }
  606. n := l / 2
  607. d := data[2:]
  608. m.supportedSignatureAlgorithms = make([]SignatureScheme, n)
  609. for i := range m.supportedSignatureAlgorithms {
  610. m.supportedSignatureAlgorithms[i] = SignatureScheme(d[0])<<8 | SignatureScheme(d[1])
  611. d = d[2:]
  612. }
  613. case extensionRenegotiationInfo:
  614. if length == 0 {
  615. return alertDecodeError
  616. }
  617. d := data[:length]
  618. l := int(d[0])
  619. d = d[1:]
  620. if l != len(d) {
  621. return alertDecodeError
  622. }
  623. m.secureRenegotiation = d
  624. m.secureRenegotiationSupported = true
  625. case extensionALPN:
  626. if length < 2 {
  627. return alertDecodeError
  628. }
  629. l := int(data[0])<<8 | int(data[1])
  630. if l != length-2 {
  631. return alertDecodeError
  632. }
  633. d := data[2:length]
  634. for len(d) != 0 {
  635. stringLen := int(d[0])
  636. d = d[1:]
  637. if stringLen == 0 || stringLen > len(d) {
  638. return alertDecodeError
  639. }
  640. m.alpnProtocols = append(m.alpnProtocols, string(d[:stringLen]))
  641. d = d[stringLen:]
  642. }
  643. case extensionSCT:
  644. m.scts = true
  645. if length != 0 {
  646. return alertDecodeError
  647. }
  648. case extensionKeyShare:
  649. // https://tools.ietf.org/html/rfc8446#section-4.2.8
  650. if length < 2 {
  651. return alertDecodeError
  652. }
  653. l := int(data[0])<<8 | int(data[1])
  654. if l != length-2 {
  655. return alertDecodeError
  656. }
  657. d := data[2:length]
  658. for len(d) != 0 {
  659. if len(d) < 4 {
  660. return alertDecodeError
  661. }
  662. dataLen := int(d[2])<<8 | int(d[3])
  663. if dataLen == 0 || 4+dataLen > len(d) {
  664. return alertDecodeError
  665. }
  666. m.keyShares = append(m.keyShares, keyShare{
  667. group: CurveID(d[0])<<8 | CurveID(d[1]),
  668. data: d[4 : 4+dataLen],
  669. })
  670. d = d[4+dataLen:]
  671. }
  672. case extensionSupportedVersions:
  673. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.1
  674. if length < 1 {
  675. return alertDecodeError
  676. }
  677. l := int(data[0])
  678. if l%2 == 1 || length != l+1 {
  679. return alertDecodeError
  680. }
  681. n := l / 2
  682. d := data[1:]
  683. for i := 0; i < n; i++ {
  684. v := uint16(d[0])<<8 + uint16(d[1])
  685. m.supportedVersions = append(m.supportedVersions, v)
  686. d = d[2:]
  687. }
  688. case extensionPreSharedKey:
  689. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
  690. if length < 2 {
  691. return alertDecodeError
  692. }
  693. // Ensure this extension is the last one in the Client Hello
  694. if len(data) != length {
  695. return alertIllegalParameter
  696. }
  697. li := int(data[0])<<8 | int(data[1])
  698. if 2+li+2 > length {
  699. return alertDecodeError
  700. }
  701. d := data[2 : 2+li]
  702. bindersOffset += 2 + li
  703. for len(d) > 0 {
  704. if len(d) < 6 {
  705. return alertDecodeError
  706. }
  707. l := int(d[0])<<8 | int(d[1])
  708. if len(d) < 2+l+4 {
  709. return alertDecodeError
  710. }
  711. m.psks = append(m.psks, psk{
  712. identity: d[2 : 2+l],
  713. obfTicketAge: uint32(d[l+2])<<24 | uint32(d[l+3])<<16 |
  714. uint32(d[l+4])<<8 | uint32(d[l+5]),
  715. })
  716. d = d[2+l+4:]
  717. }
  718. lb := int(data[li+2])<<8 | int(data[li+3])
  719. d = data[2+li+2:]
  720. if lb != len(d) || lb == 0 {
  721. return alertDecodeError
  722. }
  723. i := 0
  724. for len(d) > 0 {
  725. if i >= len(m.psks) {
  726. return alertIllegalParameter
  727. }
  728. if len(d) < 1 {
  729. return alertDecodeError
  730. }
  731. l := int(d[0])
  732. if l > len(d)-1 {
  733. return alertDecodeError
  734. }
  735. if i >= len(m.psks) {
  736. return alertIllegalParameter
  737. }
  738. m.psks[i].binder = d[1 : 1+l]
  739. d = d[1+l:]
  740. i++
  741. }
  742. if i != len(m.psks) {
  743. return alertIllegalParameter
  744. }
  745. m.rawTruncated = m.raw[:bindersOffset]
  746. case extensionPSKKeyExchangeModes:
  747. if length < 2 {
  748. return alertDecodeError
  749. }
  750. l := int(data[0])
  751. if length != l+1 {
  752. return alertDecodeError
  753. }
  754. m.pskKeyExchangeModes = data[1:length]
  755. case extensionEarlyData:
  756. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
  757. m.earlyData = true
  758. case extensionDelegatedCredential:
  759. // https://tools.ietf.org/html/draft-ietf-tls-subcerts-02
  760. m.delegatedCredential = true
  761. case extensionEMS:
  762. // RFC 7627
  763. m.extendedMSSupported = true
  764. if length != 0 {
  765. return alertDecodeError
  766. }
  767. default:
  768. m.additionalExtensions = append(m.additionalExtensions,
  769. Extension{Type: ext, Data: data[:length]})
  770. }
  771. data = data[length:]
  772. bindersOffset += length
  773. }
  774. return alertSuccess
  775. }
  776. func (m *clientHelloMsg) getSignatureAlgorithmsCert() []SignatureScheme {
  777. return signAlgosCertList(m.supportedSignatureAlgorithms, m.supportedSignatureAlgorithmsCert)
  778. }
  779. type serverHelloMsg struct {
  780. raw []byte
  781. vers uint16
  782. random []byte
  783. sessionId []byte
  784. cipherSuite uint16
  785. compressionMethod uint8
  786. nextProtoNeg bool
  787. nextProtos []string
  788. ocspStapling bool
  789. scts [][]byte
  790. ticketSupported bool
  791. secureRenegotiation []byte
  792. secureRenegotiationSupported bool
  793. alpnProtocol string
  794. // TLS 1.3
  795. keyShare keyShare
  796. psk bool
  797. pskIdentity uint16
  798. // RFC7627
  799. extendedMSSupported bool
  800. }
  801. func (m *serverHelloMsg) equal(i interface{}) bool {
  802. m1, ok := i.(*serverHelloMsg)
  803. if !ok {
  804. return false
  805. }
  806. if len(m.scts) != len(m1.scts) {
  807. return false
  808. }
  809. for i, sct := range m.scts {
  810. if !bytes.Equal(sct, m1.scts[i]) {
  811. return false
  812. }
  813. }
  814. return bytes.Equal(m.raw, m1.raw) &&
  815. m.vers == m1.vers &&
  816. bytes.Equal(m.random, m1.random) &&
  817. bytes.Equal(m.sessionId, m1.sessionId) &&
  818. m.cipherSuite == m1.cipherSuite &&
  819. m.compressionMethod == m1.compressionMethod &&
  820. m.nextProtoNeg == m1.nextProtoNeg &&
  821. eqStrings(m.nextProtos, m1.nextProtos) &&
  822. m.ocspStapling == m1.ocspStapling &&
  823. m.ticketSupported == m1.ticketSupported &&
  824. m.secureRenegotiationSupported == m1.secureRenegotiationSupported &&
  825. bytes.Equal(m.secureRenegotiation, m1.secureRenegotiation) &&
  826. m.alpnProtocol == m1.alpnProtocol &&
  827. m.keyShare.group == m1.keyShare.group &&
  828. bytes.Equal(m.keyShare.data, m1.keyShare.data) &&
  829. m.psk == m1.psk &&
  830. m.pskIdentity == m1.pskIdentity &&
  831. m.extendedMSSupported == m1.extendedMSSupported
  832. }
  833. func (m *serverHelloMsg) marshal() []byte {
  834. if m.raw != nil {
  835. return m.raw
  836. }
  837. length := 38 + len(m.sessionId)
  838. numExtensions := 0
  839. extensionsLength := 0
  840. nextProtoLen := 0
  841. if m.nextProtoNeg {
  842. numExtensions++
  843. for _, v := range m.nextProtos {
  844. nextProtoLen += len(v)
  845. }
  846. nextProtoLen += len(m.nextProtos)
  847. extensionsLength += nextProtoLen
  848. }
  849. if m.ocspStapling {
  850. numExtensions++
  851. }
  852. if m.ticketSupported {
  853. numExtensions++
  854. }
  855. if m.secureRenegotiationSupported {
  856. extensionsLength += 1 + len(m.secureRenegotiation)
  857. numExtensions++
  858. }
  859. if m.extendedMSSupported {
  860. numExtensions++
  861. }
  862. if alpnLen := len(m.alpnProtocol); alpnLen > 0 {
  863. if alpnLen >= 256 {
  864. panic("invalid ALPN protocol")
  865. }
  866. extensionsLength += 2 + 1 + alpnLen
  867. numExtensions++
  868. }
  869. sctLen := 0
  870. if len(m.scts) > 0 {
  871. for _, sct := range m.scts {
  872. sctLen += len(sct) + 2
  873. }
  874. extensionsLength += 2 + sctLen
  875. numExtensions++
  876. }
  877. if m.keyShare.group != 0 {
  878. extensionsLength += 4 + len(m.keyShare.data)
  879. numExtensions++
  880. }
  881. if m.psk {
  882. extensionsLength += 2
  883. numExtensions++
  884. }
  885. // supported_versions extension
  886. if m.vers >= VersionTLS13 {
  887. extensionsLength += 2
  888. numExtensions++
  889. }
  890. if numExtensions > 0 {
  891. extensionsLength += 4 * numExtensions
  892. length += 2 + extensionsLength
  893. }
  894. x := make([]byte, 4+length)
  895. x[0] = typeServerHello
  896. x[1] = uint8(length >> 16)
  897. x[2] = uint8(length >> 8)
  898. x[3] = uint8(length)
  899. if m.vers >= VersionTLS13 {
  900. x[4] = 3
  901. x[5] = 3
  902. } else {
  903. x[4] = uint8(m.vers >> 8)
  904. x[5] = uint8(m.vers)
  905. }
  906. copy(x[6:38], m.random)
  907. z := x[38:]
  908. x[38] = uint8(len(m.sessionId))
  909. copy(x[39:39+len(m.sessionId)], m.sessionId)
  910. z = x[39+len(m.sessionId):]
  911. z[0] = uint8(m.cipherSuite >> 8)
  912. z[1] = uint8(m.cipherSuite)
  913. z[2] = m.compressionMethod
  914. z = z[3:]
  915. if numExtensions > 0 {
  916. z[0] = byte(extensionsLength >> 8)
  917. z[1] = byte(extensionsLength)
  918. z = z[2:]
  919. }
  920. if m.vers >= VersionTLS13 {
  921. z[0] = byte(extensionSupportedVersions >> 8)
  922. z[1] = byte(extensionSupportedVersions)
  923. z[3] = 2
  924. z[4] = uint8(m.vers >> 8)
  925. z[5] = uint8(m.vers)
  926. z = z[6:]
  927. }
  928. if m.nextProtoNeg {
  929. z[0] = byte(extensionNextProtoNeg >> 8)
  930. z[1] = byte(extensionNextProtoNeg & 0xff)
  931. z[2] = byte(nextProtoLen >> 8)
  932. z[3] = byte(nextProtoLen)
  933. z = z[4:]
  934. for _, v := range m.nextProtos {
  935. l := len(v)
  936. if l > 255 {
  937. l = 255
  938. }
  939. z[0] = byte(l)
  940. copy(z[1:], []byte(v[0:l]))
  941. z = z[1+l:]
  942. }
  943. }
  944. if m.ocspStapling {
  945. z[0] = byte(extensionStatusRequest >> 8)
  946. z[1] = byte(extensionStatusRequest)
  947. z = z[4:]
  948. }
  949. if m.ticketSupported {
  950. z[0] = byte(extensionSessionTicket >> 8)
  951. z[1] = byte(extensionSessionTicket)
  952. z = z[4:]
  953. }
  954. if m.secureRenegotiationSupported {
  955. z[0] = byte(extensionRenegotiationInfo >> 8)
  956. z[1] = byte(extensionRenegotiationInfo & 0xff)
  957. z[2] = 0
  958. z[3] = byte(len(m.secureRenegotiation) + 1)
  959. z[4] = byte(len(m.secureRenegotiation))
  960. z = z[5:]
  961. copy(z, m.secureRenegotiation)
  962. z = z[len(m.secureRenegotiation):]
  963. }
  964. if alpnLen := len(m.alpnProtocol); alpnLen > 0 {
  965. z[0] = byte(extensionALPN >> 8)
  966. z[1] = byte(extensionALPN & 0xff)
  967. l := 2 + 1 + alpnLen
  968. z[2] = byte(l >> 8)
  969. z[3] = byte(l)
  970. l -= 2
  971. z[4] = byte(l >> 8)
  972. z[5] = byte(l)
  973. l -= 1
  974. z[6] = byte(l)
  975. copy(z[7:], []byte(m.alpnProtocol))
  976. z = z[7+alpnLen:]
  977. }
  978. if sctLen > 0 {
  979. z[0] = byte(extensionSCT >> 8)
  980. z[1] = byte(extensionSCT)
  981. l := sctLen + 2
  982. z[2] = byte(l >> 8)
  983. z[3] = byte(l)
  984. z[4] = byte(sctLen >> 8)
  985. z[5] = byte(sctLen)
  986. z = z[6:]
  987. for _, sct := range m.scts {
  988. z[0] = byte(len(sct) >> 8)
  989. z[1] = byte(len(sct))
  990. copy(z[2:], sct)
  991. z = z[len(sct)+2:]
  992. }
  993. }
  994. if m.keyShare.group != 0 {
  995. z[0] = uint8(extensionKeyShare >> 8)
  996. z[1] = uint8(extensionKeyShare)
  997. l := 4 + len(m.keyShare.data)
  998. z[2] = uint8(l >> 8)
  999. z[3] = uint8(l)
  1000. z[4] = uint8(m.keyShare.group >> 8)
  1001. z[5] = uint8(m.keyShare.group)
  1002. l -= 4
  1003. z[6] = uint8(l >> 8)
  1004. z[7] = uint8(l)
  1005. copy(z[8:], m.keyShare.data)
  1006. z = z[8+l:]
  1007. }
  1008. if m.psk {
  1009. z[0] = byte(extensionPreSharedKey >> 8)
  1010. z[1] = byte(extensionPreSharedKey)
  1011. z[3] = 2
  1012. z[4] = byte(m.pskIdentity >> 8)
  1013. z[5] = byte(m.pskIdentity)
  1014. z = z[6:]
  1015. }
  1016. if m.extendedMSSupported {
  1017. binary.BigEndian.PutUint16(z, extensionEMS)
  1018. z = z[4:]
  1019. }
  1020. m.raw = x
  1021. return x
  1022. }
  1023. func (m *serverHelloMsg) unmarshal(data []byte) alert {
  1024. if len(data) < 42 {
  1025. return alertDecodeError
  1026. }
  1027. m.raw = data
  1028. m.vers = uint16(data[4])<<8 | uint16(data[5])
  1029. m.random = data[6:38]
  1030. sessionIdLen := int(data[38])
  1031. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  1032. return alertDecodeError
  1033. }
  1034. m.sessionId = data[39 : 39+sessionIdLen]
  1035. data = data[39+sessionIdLen:]
  1036. if len(data) < 3 {
  1037. return alertDecodeError
  1038. }
  1039. m.cipherSuite = uint16(data[0])<<8 | uint16(data[1])
  1040. m.compressionMethod = data[2]
  1041. data = data[3:]
  1042. m.nextProtoNeg = false
  1043. m.nextProtos = nil
  1044. m.ocspStapling = false
  1045. m.scts = nil
  1046. m.ticketSupported = false
  1047. m.alpnProtocol = ""
  1048. m.keyShare.group = 0
  1049. m.keyShare.data = nil
  1050. m.psk = false
  1051. m.pskIdentity = 0
  1052. m.extendedMSSupported = false
  1053. if len(data) == 0 {
  1054. // ServerHello is optionally followed by extension data
  1055. return alertSuccess
  1056. }
  1057. if len(data) < 2 {
  1058. return alertDecodeError
  1059. }
  1060. extensionsLength := int(data[0])<<8 | int(data[1])
  1061. data = data[2:]
  1062. if len(data) != extensionsLength {
  1063. return alertDecodeError
  1064. }
  1065. svData := findExtension(data, extensionSupportedVersions)
  1066. if svData != nil {
  1067. if len(svData) != 2 {
  1068. return alertDecodeError
  1069. }
  1070. if m.vers != VersionTLS12 {
  1071. return alertDecodeError
  1072. }
  1073. rcvVer := binary.BigEndian.Uint16(svData[0:])
  1074. if rcvVer < VersionTLS13 {
  1075. return alertIllegalParameter
  1076. }
  1077. m.vers = rcvVer
  1078. }
  1079. for len(data) != 0 {
  1080. if len(data) < 4 {
  1081. return alertDecodeError
  1082. }
  1083. extension := uint16(data[0])<<8 | uint16(data[1])
  1084. length := int(data[2])<<8 | int(data[3])
  1085. data = data[4:]
  1086. if len(data) < length {
  1087. return alertDecodeError
  1088. }
  1089. switch extension {
  1090. case extensionNextProtoNeg:
  1091. m.nextProtoNeg = true
  1092. d := data[:length]
  1093. for len(d) > 0 {
  1094. l := int(d[0])
  1095. d = d[1:]
  1096. if l == 0 || l > len(d) {
  1097. return alertDecodeError
  1098. }
  1099. m.nextProtos = append(m.nextProtos, string(d[:l]))
  1100. d = d[l:]
  1101. }
  1102. case extensionStatusRequest:
  1103. if length > 0 {
  1104. return alertDecodeError
  1105. }
  1106. m.ocspStapling = true
  1107. case extensionSessionTicket:
  1108. if length > 0 {
  1109. return alertDecodeError
  1110. }
  1111. m.ticketSupported = true
  1112. case extensionRenegotiationInfo:
  1113. if length == 0 {
  1114. return alertDecodeError
  1115. }
  1116. d := data[:length]
  1117. l := int(d[0])
  1118. d = d[1:]
  1119. if l != len(d) {
  1120. return alertDecodeError
  1121. }
  1122. m.secureRenegotiation = d
  1123. m.secureRenegotiationSupported = true
  1124. case extensionALPN:
  1125. d := data[:length]
  1126. if len(d) < 3 {
  1127. return alertDecodeError
  1128. }
  1129. l := int(d[0])<<8 | int(d[1])
  1130. if l != len(d)-2 {
  1131. return alertDecodeError
  1132. }
  1133. d = d[2:]
  1134. l = int(d[0])
  1135. if l != len(d)-1 {
  1136. return alertDecodeError
  1137. }
  1138. d = d[1:]
  1139. if len(d) == 0 {
  1140. // ALPN protocols must not be empty.
  1141. return alertDecodeError
  1142. }
  1143. m.alpnProtocol = string(d)
  1144. case extensionSCT:
  1145. d := data[:length]
  1146. if len(d) < 2 {
  1147. return alertDecodeError
  1148. }
  1149. l := int(d[0])<<8 | int(d[1])
  1150. d = d[2:]
  1151. if len(d) != l || l == 0 {
  1152. return alertDecodeError
  1153. }
  1154. m.scts = make([][]byte, 0, 3)
  1155. for len(d) != 0 {
  1156. if len(d) < 2 {
  1157. return alertDecodeError
  1158. }
  1159. sctLen := int(d[0])<<8 | int(d[1])
  1160. d = d[2:]
  1161. if sctLen == 0 || len(d) < sctLen {
  1162. return alertDecodeError
  1163. }
  1164. m.scts = append(m.scts, d[:sctLen])
  1165. d = d[sctLen:]
  1166. }
  1167. case extensionKeyShare:
  1168. d := data[:length]
  1169. if len(d) < 4 {
  1170. return alertDecodeError
  1171. }
  1172. m.keyShare.group = CurveID(d[0])<<8 | CurveID(d[1])
  1173. l := int(d[2])<<8 | int(d[3])
  1174. d = d[4:]
  1175. if len(d) != l {
  1176. return alertDecodeError
  1177. }
  1178. m.keyShare.data = d[:l]
  1179. case extensionPreSharedKey:
  1180. if length != 2 {
  1181. return alertDecodeError
  1182. }
  1183. m.psk = true
  1184. m.pskIdentity = uint16(data[0])<<8 | uint16(data[1])
  1185. case extensionEMS:
  1186. m.extendedMSSupported = true
  1187. }
  1188. data = data[length:]
  1189. }
  1190. return alertSuccess
  1191. }
  1192. type encryptedExtensionsMsg struct {
  1193. raw []byte
  1194. alpnProtocol string
  1195. earlyData bool
  1196. additionalExtensions []Extension
  1197. }
  1198. func (m *encryptedExtensionsMsg) equal(i interface{}) bool {
  1199. m1, ok := i.(*encryptedExtensionsMsg)
  1200. if !ok {
  1201. return false
  1202. }
  1203. if len(m.additionalExtensions) != len(m1.additionalExtensions) {
  1204. return false
  1205. }
  1206. for i, ex := range m.additionalExtensions {
  1207. ex1 := m1.additionalExtensions[i]
  1208. if ex.Type != ex1.Type || !bytes.Equal(ex.Data, ex1.Data) {
  1209. return false
  1210. }
  1211. }
  1212. return bytes.Equal(m.raw, m1.raw) &&
  1213. m.alpnProtocol == m1.alpnProtocol &&
  1214. m.earlyData == m1.earlyData
  1215. }
  1216. func (m *encryptedExtensionsMsg) marshal() []byte {
  1217. if m.raw != nil {
  1218. return m.raw
  1219. }
  1220. length := 2
  1221. if m.earlyData {
  1222. length += 4
  1223. }
  1224. alpnLen := len(m.alpnProtocol)
  1225. if alpnLen > 0 {
  1226. if alpnLen >= 256 {
  1227. panic("invalid ALPN protocol")
  1228. }
  1229. length += 2 + 2 + 2 + 1 + alpnLen
  1230. }
  1231. if len(m.additionalExtensions) > 0 {
  1232. for _, ex := range m.additionalExtensions {
  1233. length += 4 + len(ex.Data)
  1234. }
  1235. }
  1236. x := make([]byte, 4+length)
  1237. x[0] = typeEncryptedExtensions
  1238. x[1] = uint8(length >> 16)
  1239. x[2] = uint8(length >> 8)
  1240. x[3] = uint8(length)
  1241. length -= 2
  1242. x[4] = uint8(length >> 8)
  1243. x[5] = uint8(length)
  1244. z := x[6:]
  1245. if alpnLen > 0 {
  1246. z[0] = byte(extensionALPN >> 8)
  1247. z[1] = byte(extensionALPN)
  1248. l := 2 + 1 + alpnLen
  1249. z[2] = byte(l >> 8)
  1250. z[3] = byte(l)
  1251. l -= 2
  1252. z[4] = byte(l >> 8)
  1253. z[5] = byte(l)
  1254. l -= 1
  1255. z[6] = byte(l)
  1256. copy(z[7:], []byte(m.alpnProtocol))
  1257. z = z[7+alpnLen:]
  1258. }
  1259. if m.earlyData {
  1260. z[0] = byte(extensionEarlyData >> 8)
  1261. z[1] = byte(extensionEarlyData)
  1262. z = z[4:]
  1263. }
  1264. for _, ex := range m.additionalExtensions {
  1265. z[0] = byte(ex.Type >> 8)
  1266. z[1] = byte(ex.Type)
  1267. l := len(ex.Data)
  1268. z[2] = byte(l >> 8)
  1269. z[3] = byte(l)
  1270. copy(z[4:], ex.Data)
  1271. z = z[4+l:]
  1272. }
  1273. m.raw = x
  1274. return x
  1275. }
  1276. func (m *encryptedExtensionsMsg) unmarshal(data []byte) alert {
  1277. if len(data) < 6 {
  1278. return alertDecodeError
  1279. }
  1280. m.raw = data
  1281. m.alpnProtocol = ""
  1282. m.earlyData = false
  1283. extensionsLength := int(data[4])<<8 | int(data[5])
  1284. data = data[6:]
  1285. if len(data) != extensionsLength {
  1286. return alertDecodeError
  1287. }
  1288. for len(data) != 0 {
  1289. if len(data) < 4 {
  1290. return alertDecodeError
  1291. }
  1292. ext := uint16(data[0])<<8 | uint16(data[1])
  1293. length := int(data[2])<<8 | int(data[3])
  1294. data = data[4:]
  1295. if len(data) < length {
  1296. return alertDecodeError
  1297. }
  1298. switch ext {
  1299. case extensionALPN:
  1300. d := data[:length]
  1301. if len(d) < 3 {
  1302. return alertDecodeError
  1303. }
  1304. l := int(d[0])<<8 | int(d[1])
  1305. if l != len(d)-2 {
  1306. return alertDecodeError
  1307. }
  1308. d = d[2:]
  1309. l = int(d[0])
  1310. if l != len(d)-1 {
  1311. return alertDecodeError
  1312. }
  1313. d = d[1:]
  1314. if len(d) == 0 {
  1315. // ALPN protocols must not be empty.
  1316. return alertDecodeError
  1317. }
  1318. m.alpnProtocol = string(d)
  1319. case extensionEarlyData:
  1320. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
  1321. m.earlyData = true
  1322. default:
  1323. m.additionalExtensions = append(m.additionalExtensions,
  1324. Extension{Type: ext, Data: data[:length]})
  1325. }
  1326. data = data[length:]
  1327. }
  1328. return alertSuccess
  1329. }
  1330. type certificateMsg struct {
  1331. raw []byte
  1332. certificates [][]byte
  1333. }
  1334. func (m *certificateMsg) equal(i interface{}) bool {
  1335. m1, ok := i.(*certificateMsg)
  1336. if !ok {
  1337. return false
  1338. }
  1339. return bytes.Equal(m.raw, m1.raw) &&
  1340. eqByteSlices(m.certificates, m1.certificates)
  1341. }
  1342. func (m *certificateMsg) marshal() (x []byte) {
  1343. if m.raw != nil {
  1344. return m.raw
  1345. }
  1346. var i int
  1347. for _, slice := range m.certificates {
  1348. i += len(slice)
  1349. }
  1350. length := 3 + 3*len(m.certificates) + i
  1351. x = make([]byte, 4+length)
  1352. x[0] = typeCertificate
  1353. x[1] = uint8(length >> 16)
  1354. x[2] = uint8(length >> 8)
  1355. x[3] = uint8(length)
  1356. certificateOctets := length - 3
  1357. x[4] = uint8(certificateOctets >> 16)
  1358. x[5] = uint8(certificateOctets >> 8)
  1359. x[6] = uint8(certificateOctets)
  1360. y := x[7:]
  1361. for _, slice := range m.certificates {
  1362. y[0] = uint8(len(slice) >> 16)
  1363. y[1] = uint8(len(slice) >> 8)
  1364. y[2] = uint8(len(slice))
  1365. copy(y[3:], slice)
  1366. y = y[3+len(slice):]
  1367. }
  1368. m.raw = x
  1369. return
  1370. }
  1371. func (m *certificateMsg) unmarshal(data []byte) alert {
  1372. if len(data) < 7 {
  1373. return alertDecodeError
  1374. }
  1375. m.raw = data
  1376. certsLen := uint32(data[4])<<16 | uint32(data[5])<<8 | uint32(data[6])
  1377. if uint32(len(data)) != certsLen+7 {
  1378. return alertDecodeError
  1379. }
  1380. numCerts := 0
  1381. d := data[7:]
  1382. for certsLen > 0 {
  1383. if len(d) < 4 {
  1384. return alertDecodeError
  1385. }
  1386. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1387. if uint32(len(d)) < 3+certLen {
  1388. return alertDecodeError
  1389. }
  1390. d = d[3+certLen:]
  1391. certsLen -= 3 + certLen
  1392. numCerts++
  1393. }
  1394. m.certificates = make([][]byte, numCerts)
  1395. d = data[7:]
  1396. for i := 0; i < numCerts; i++ {
  1397. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1398. m.certificates[i] = d[3 : 3+certLen]
  1399. d = d[3+certLen:]
  1400. }
  1401. return alertSuccess
  1402. }
  1403. type certificateEntry struct {
  1404. data []byte
  1405. ocspStaple []byte
  1406. sctList [][]byte
  1407. delegatedCredential []byte
  1408. }
  1409. type certificateMsg13 struct {
  1410. raw []byte
  1411. requestContext []byte
  1412. certificates []certificateEntry
  1413. }
  1414. func (m *certificateMsg13) equal(i interface{}) bool {
  1415. m1, ok := i.(*certificateMsg13)
  1416. if !ok {
  1417. return false
  1418. }
  1419. if len(m.certificates) != len(m1.certificates) {
  1420. return false
  1421. }
  1422. for i, _ := range m.certificates {
  1423. ok := bytes.Equal(m.certificates[i].data, m1.certificates[i].data)
  1424. ok = ok && bytes.Equal(m.certificates[i].ocspStaple, m1.certificates[i].ocspStaple)
  1425. ok = ok && eqByteSlices(m.certificates[i].sctList, m1.certificates[i].sctList)
  1426. ok = ok && bytes.Equal(m.certificates[i].delegatedCredential, m1.certificates[i].delegatedCredential)
  1427. if !ok {
  1428. return false
  1429. }
  1430. }
  1431. return bytes.Equal(m.raw, m1.raw) &&
  1432. bytes.Equal(m.requestContext, m1.requestContext)
  1433. }
  1434. func (m *certificateMsg13) marshal() (x []byte) {
  1435. if m.raw != nil {
  1436. return m.raw
  1437. }
  1438. var i int
  1439. for _, cert := range m.certificates {
  1440. i += len(cert.data)
  1441. if len(cert.ocspStaple) != 0 {
  1442. i += 8 + len(cert.ocspStaple)
  1443. }
  1444. if len(cert.sctList) != 0 {
  1445. i += 6
  1446. for _, sct := range cert.sctList {
  1447. i += 2 + len(sct)
  1448. }
  1449. }
  1450. if len(cert.delegatedCredential) != 0 {
  1451. i += 4 + len(cert.delegatedCredential)
  1452. }
  1453. }
  1454. length := 3 + 3*len(m.certificates) + i
  1455. length += 2 * len(m.certificates) // extensions
  1456. length += 1 + len(m.requestContext)
  1457. x = make([]byte, 4+length)
  1458. x[0] = typeCertificate
  1459. x[1] = uint8(length >> 16)
  1460. x[2] = uint8(length >> 8)
  1461. x[3] = uint8(length)
  1462. z := x[4:]
  1463. z[0] = byte(len(m.requestContext))
  1464. copy(z[1:], m.requestContext)
  1465. z = z[1+len(m.requestContext):]
  1466. certificateOctets := len(z) - 3
  1467. z[0] = uint8(certificateOctets >> 16)
  1468. z[1] = uint8(certificateOctets >> 8)
  1469. z[2] = uint8(certificateOctets)
  1470. z = z[3:]
  1471. for _, cert := range m.certificates {
  1472. z[0] = uint8(len(cert.data) >> 16)
  1473. z[1] = uint8(len(cert.data) >> 8)
  1474. z[2] = uint8(len(cert.data))
  1475. copy(z[3:], cert.data)
  1476. z = z[3+len(cert.data):]
  1477. extLenPos := z[:2]
  1478. z = z[2:]
  1479. extensionLen := 0
  1480. if len(cert.ocspStaple) != 0 {
  1481. stapleLen := 4 + len(cert.ocspStaple)
  1482. z[0] = uint8(extensionStatusRequest >> 8)
  1483. z[1] = uint8(extensionStatusRequest)
  1484. z[2] = uint8(stapleLen >> 8)
  1485. z[3] = uint8(stapleLen)
  1486. stapleLen -= 4
  1487. z[4] = statusTypeOCSP
  1488. z[5] = uint8(stapleLen >> 16)
  1489. z[6] = uint8(stapleLen >> 8)
  1490. z[7] = uint8(stapleLen)
  1491. copy(z[8:], cert.ocspStaple)
  1492. z = z[8+stapleLen:]
  1493. extensionLen += 8 + stapleLen
  1494. }
  1495. if len(cert.sctList) != 0 {
  1496. z[0] = uint8(extensionSCT >> 8)
  1497. z[1] = uint8(extensionSCT)
  1498. sctLenPos := z[2:6]
  1499. z = z[6:]
  1500. extensionLen += 6
  1501. sctLen := 2
  1502. for _, sct := range cert.sctList {
  1503. z[0] = uint8(len(sct) >> 8)
  1504. z[1] = uint8(len(sct))
  1505. copy(z[2:], sct)
  1506. z = z[2+len(sct):]
  1507. extensionLen += 2 + len(sct)
  1508. sctLen += 2 + len(sct)
  1509. }
  1510. sctLenPos[0] = uint8(sctLen >> 8)
  1511. sctLenPos[1] = uint8(sctLen)
  1512. sctLen -= 2
  1513. sctLenPos[2] = uint8(sctLen >> 8)
  1514. sctLenPos[3] = uint8(sctLen)
  1515. }
  1516. if len(cert.delegatedCredential) != 0 {
  1517. binary.BigEndian.PutUint16(z, extensionDelegatedCredential)
  1518. binary.BigEndian.PutUint16(z[2:], uint16(len(cert.delegatedCredential)))
  1519. z = z[4:]
  1520. copy(z, cert.delegatedCredential)
  1521. z = z[len(cert.delegatedCredential):]
  1522. extensionLen += 4 + len(cert.delegatedCredential)
  1523. }
  1524. extLenPos[0] = uint8(extensionLen >> 8)
  1525. extLenPos[1] = uint8(extensionLen)
  1526. }
  1527. m.raw = x
  1528. return
  1529. }
  1530. func (m *certificateMsg13) unmarshal(data []byte) alert {
  1531. if len(data) < 5 {
  1532. return alertDecodeError
  1533. }
  1534. m.raw = data
  1535. ctxLen := data[4]
  1536. if len(data) < int(ctxLen)+5+3 {
  1537. return alertDecodeError
  1538. }
  1539. m.requestContext = data[5 : 5+ctxLen]
  1540. d := data[5+ctxLen:]
  1541. certsLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1542. if uint32(len(d)) != certsLen+3 {
  1543. return alertDecodeError
  1544. }
  1545. numCerts := 0
  1546. d = d[3:]
  1547. for certsLen > 0 {
  1548. if len(d) < 4 {
  1549. return alertDecodeError
  1550. }
  1551. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1552. if uint32(len(d)) < 3+certLen {
  1553. return alertDecodeError
  1554. }
  1555. d = d[3+certLen:]
  1556. if len(d) < 2 {
  1557. return alertDecodeError
  1558. }
  1559. extLen := uint16(d[0])<<8 | uint16(d[1])
  1560. if uint16(len(d)) < 2+extLen {
  1561. return alertDecodeError
  1562. }
  1563. d = d[2+extLen:]
  1564. certsLen -= 3 + certLen + 2 + uint32(extLen)
  1565. numCerts++
  1566. }
  1567. m.certificates = make([]certificateEntry, numCerts)
  1568. d = data[8+ctxLen:]
  1569. for i := 0; i < numCerts; i++ {
  1570. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1571. m.certificates[i].data = d[3 : 3+certLen]
  1572. d = d[3+certLen:]
  1573. extLen := uint16(d[0])<<8 | uint16(d[1])
  1574. d = d[2:]
  1575. for extLen > 0 {
  1576. if extLen < 4 {
  1577. return alertDecodeError
  1578. }
  1579. typ := uint16(d[0])<<8 | uint16(d[1])
  1580. bodyLen := uint16(d[2])<<8 | uint16(d[3])
  1581. if extLen < 4+bodyLen {
  1582. return alertDecodeError
  1583. }
  1584. body := d[4 : 4+bodyLen]
  1585. d = d[4+bodyLen:]
  1586. extLen -= 4 + bodyLen
  1587. switch typ {
  1588. case extensionStatusRequest:
  1589. if len(body) < 4 || body[0] != 0x01 {
  1590. return alertDecodeError
  1591. }
  1592. ocspLen := int(body[1])<<16 | int(body[2])<<8 | int(body[3])
  1593. if len(body) != 4+ocspLen {
  1594. return alertDecodeError
  1595. }
  1596. m.certificates[i].ocspStaple = body[4:]
  1597. case extensionSCT:
  1598. if len(body) < 2 {
  1599. return alertDecodeError
  1600. }
  1601. listLen := int(body[0])<<8 | int(body[1])
  1602. body = body[2:]
  1603. if len(body) != listLen {
  1604. return alertDecodeError
  1605. }
  1606. for len(body) > 0 {
  1607. if len(body) < 2 {
  1608. return alertDecodeError
  1609. }
  1610. sctLen := int(body[0])<<8 | int(body[1])
  1611. if len(body) < 2+sctLen {
  1612. return alertDecodeError
  1613. }
  1614. m.certificates[i].sctList = append(m.certificates[i].sctList, body[2:2+sctLen])
  1615. body = body[2+sctLen:]
  1616. }
  1617. case extensionDelegatedCredential:
  1618. m.certificates[i].delegatedCredential = body
  1619. }
  1620. }
  1621. }
  1622. return alertSuccess
  1623. }
  1624. type serverKeyExchangeMsg struct {
  1625. raw []byte
  1626. key []byte
  1627. }
  1628. func (m *serverKeyExchangeMsg) equal(i interface{}) bool {
  1629. m1, ok := i.(*serverKeyExchangeMsg)
  1630. if !ok {
  1631. return false
  1632. }
  1633. return bytes.Equal(m.raw, m1.raw) &&
  1634. bytes.Equal(m.key, m1.key)
  1635. }
  1636. func (m *serverKeyExchangeMsg) marshal() []byte {
  1637. if m.raw != nil {
  1638. return m.raw
  1639. }
  1640. length := len(m.key)
  1641. x := make([]byte, length+4)
  1642. x[0] = typeServerKeyExchange
  1643. x[1] = uint8(length >> 16)
  1644. x[2] = uint8(length >> 8)
  1645. x[3] = uint8(length)
  1646. copy(x[4:], m.key)
  1647. m.raw = x
  1648. return x
  1649. }
  1650. func (m *serverKeyExchangeMsg) unmarshal(data []byte) alert {
  1651. m.raw = data
  1652. if len(data) < 4 {
  1653. return alertDecodeError
  1654. }
  1655. m.key = data[4:]
  1656. return alertSuccess
  1657. }
  1658. type certificateStatusMsg struct {
  1659. raw []byte
  1660. statusType uint8
  1661. response []byte
  1662. }
  1663. func (m *certificateStatusMsg) equal(i interface{}) bool {
  1664. m1, ok := i.(*certificateStatusMsg)
  1665. if !ok {
  1666. return false
  1667. }
  1668. return bytes.Equal(m.raw, m1.raw) &&
  1669. m.statusType == m1.statusType &&
  1670. bytes.Equal(m.response, m1.response)
  1671. }
  1672. func (m *certificateStatusMsg) marshal() []byte {
  1673. if m.raw != nil {
  1674. return m.raw
  1675. }
  1676. var x []byte
  1677. if m.statusType == statusTypeOCSP {
  1678. x = make([]byte, 4+4+len(m.response))
  1679. x[0] = typeCertificateStatus
  1680. l := len(m.response) + 4
  1681. x[1] = byte(l >> 16)
  1682. x[2] = byte(l >> 8)
  1683. x[3] = byte(l)
  1684. x[4] = statusTypeOCSP
  1685. l -= 4
  1686. x[5] = byte(l >> 16)
  1687. x[6] = byte(l >> 8)
  1688. x[7] = byte(l)
  1689. copy(x[8:], m.response)
  1690. } else {
  1691. x = []byte{typeCertificateStatus, 0, 0, 1, m.statusType}
  1692. }
  1693. m.raw = x
  1694. return x
  1695. }
  1696. func (m *certificateStatusMsg) unmarshal(data []byte) alert {
  1697. m.raw = data
  1698. if len(data) < 5 {
  1699. return alertDecodeError
  1700. }
  1701. m.statusType = data[4]
  1702. m.response = nil
  1703. if m.statusType == statusTypeOCSP {
  1704. if len(data) < 8 {
  1705. return alertDecodeError
  1706. }
  1707. respLen := uint32(data[5])<<16 | uint32(data[6])<<8 | uint32(data[7])
  1708. if uint32(len(data)) != 4+4+respLen {
  1709. return alertDecodeError
  1710. }
  1711. m.response = data[8:]
  1712. }
  1713. return alertSuccess
  1714. }
  1715. type serverHelloDoneMsg struct{}
  1716. func (m *serverHelloDoneMsg) equal(i interface{}) bool {
  1717. _, ok := i.(*serverHelloDoneMsg)
  1718. return ok
  1719. }
  1720. func (m *serverHelloDoneMsg) marshal() []byte {
  1721. x := make([]byte, 4)
  1722. x[0] = typeServerHelloDone
  1723. return x
  1724. }
  1725. func (m *serverHelloDoneMsg) unmarshal(data []byte) alert {
  1726. if len(data) != 4 {
  1727. return alertDecodeError
  1728. }
  1729. return alertSuccess
  1730. }
  1731. type clientKeyExchangeMsg struct {
  1732. raw []byte
  1733. ciphertext []byte
  1734. }
  1735. func (m *clientKeyExchangeMsg) equal(i interface{}) bool {
  1736. m1, ok := i.(*clientKeyExchangeMsg)
  1737. if !ok {
  1738. return false
  1739. }
  1740. return bytes.Equal(m.raw, m1.raw) &&
  1741. bytes.Equal(m.ciphertext, m1.ciphertext)
  1742. }
  1743. func (m *clientKeyExchangeMsg) marshal() []byte {
  1744. if m.raw != nil {
  1745. return m.raw
  1746. }
  1747. length := len(m.ciphertext)
  1748. x := make([]byte, length+4)
  1749. x[0] = typeClientKeyExchange
  1750. x[1] = uint8(length >> 16)
  1751. x[2] = uint8(length >> 8)
  1752. x[3] = uint8(length)
  1753. copy(x[4:], m.ciphertext)
  1754. m.raw = x
  1755. return x
  1756. }
  1757. func (m *clientKeyExchangeMsg) unmarshal(data []byte) alert {
  1758. m.raw = data
  1759. if len(data) < 4 {
  1760. return alertDecodeError
  1761. }
  1762. l := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  1763. if l != len(data)-4 {
  1764. return alertDecodeError
  1765. }
  1766. m.ciphertext = data[4:]
  1767. return alertSuccess
  1768. }
  1769. type finishedMsg struct {
  1770. raw []byte
  1771. verifyData []byte
  1772. }
  1773. func (m *finishedMsg) equal(i interface{}) bool {
  1774. m1, ok := i.(*finishedMsg)
  1775. if !ok {
  1776. return false
  1777. }
  1778. return bytes.Equal(m.raw, m1.raw) &&
  1779. bytes.Equal(m.verifyData, m1.verifyData)
  1780. }
  1781. func (m *finishedMsg) marshal() (x []byte) {
  1782. if m.raw != nil {
  1783. return m.raw
  1784. }
  1785. x = make([]byte, 4+len(m.verifyData))
  1786. x[0] = typeFinished
  1787. x[3] = byte(len(m.verifyData))
  1788. copy(x[4:], m.verifyData)
  1789. m.raw = x
  1790. return
  1791. }
  1792. func (m *finishedMsg) unmarshal(data []byte) alert {
  1793. m.raw = data
  1794. if len(data) < 4 {
  1795. return alertDecodeError
  1796. }
  1797. m.verifyData = data[4:]
  1798. return alertSuccess
  1799. }
  1800. type nextProtoMsg struct {
  1801. raw []byte
  1802. proto string
  1803. }
  1804. func (m *nextProtoMsg) equal(i interface{}) bool {
  1805. m1, ok := i.(*nextProtoMsg)
  1806. if !ok {
  1807. return false
  1808. }
  1809. return bytes.Equal(m.raw, m1.raw) &&
  1810. m.proto == m1.proto
  1811. }
  1812. func (m *nextProtoMsg) marshal() []byte {
  1813. if m.raw != nil {
  1814. return m.raw
  1815. }
  1816. l := len(m.proto)
  1817. if l > 255 {
  1818. l = 255
  1819. }
  1820. padding := 32 - (l+2)%32
  1821. length := l + padding + 2
  1822. x := make([]byte, length+4)
  1823. x[0] = typeNextProtocol
  1824. x[1] = uint8(length >> 16)
  1825. x[2] = uint8(length >> 8)
  1826. x[3] = uint8(length)
  1827. y := x[4:]
  1828. y[0] = byte(l)
  1829. copy(y[1:], []byte(m.proto[0:l]))
  1830. y = y[1+l:]
  1831. y[0] = byte(padding)
  1832. m.raw = x
  1833. return x
  1834. }
  1835. func (m *nextProtoMsg) unmarshal(data []byte) alert {
  1836. m.raw = data
  1837. if len(data) < 5 {
  1838. return alertDecodeError
  1839. }
  1840. data = data[4:]
  1841. protoLen := int(data[0])
  1842. data = data[1:]
  1843. if len(data) < protoLen {
  1844. return alertDecodeError
  1845. }
  1846. m.proto = string(data[0:protoLen])
  1847. data = data[protoLen:]
  1848. if len(data) < 1 {
  1849. return alertDecodeError
  1850. }
  1851. paddingLen := int(data[0])
  1852. data = data[1:]
  1853. if len(data) != paddingLen {
  1854. return alertDecodeError
  1855. }
  1856. return alertSuccess
  1857. }
  1858. type certificateRequestMsg struct {
  1859. raw []byte
  1860. // hasSignatureAndHash indicates whether this message includes a list
  1861. // of signature and hash functions. This change was introduced with TLS
  1862. // 1.2.
  1863. hasSignatureAndHash bool
  1864. certificateTypes []byte
  1865. supportedSignatureAlgorithms []SignatureScheme
  1866. certificateAuthorities [][]byte
  1867. }
  1868. func (m *certificateRequestMsg) equal(i interface{}) bool {
  1869. m1, ok := i.(*certificateRequestMsg)
  1870. if !ok {
  1871. return false
  1872. }
  1873. return bytes.Equal(m.raw, m1.raw) &&
  1874. bytes.Equal(m.certificateTypes, m1.certificateTypes) &&
  1875. eqByteSlices(m.certificateAuthorities, m1.certificateAuthorities) &&
  1876. eqSignatureAlgorithms(m.supportedSignatureAlgorithms, m1.supportedSignatureAlgorithms)
  1877. }
  1878. func (m *certificateRequestMsg) marshal() (x []byte) {
  1879. if m.raw != nil {
  1880. return m.raw
  1881. }
  1882. // See http://tools.ietf.org/html/rfc4346#section-7.4.4
  1883. length := 1 + len(m.certificateTypes) + 2
  1884. casLength := 0
  1885. for _, ca := range m.certificateAuthorities {
  1886. casLength += 2 + len(ca)
  1887. }
  1888. length += casLength
  1889. if m.hasSignatureAndHash {
  1890. length += 2 + 2*len(m.supportedSignatureAlgorithms)
  1891. }
  1892. x = make([]byte, 4+length)
  1893. x[0] = typeCertificateRequest
  1894. x[1] = uint8(length >> 16)
  1895. x[2] = uint8(length >> 8)
  1896. x[3] = uint8(length)
  1897. x[4] = uint8(len(m.certificateTypes))
  1898. copy(x[5:], m.certificateTypes)
  1899. y := x[5+len(m.certificateTypes):]
  1900. if m.hasSignatureAndHash {
  1901. n := len(m.supportedSignatureAlgorithms) * 2
  1902. y[0] = uint8(n >> 8)
  1903. y[1] = uint8(n)
  1904. y = y[2:]
  1905. for _, sigAlgo := range m.supportedSignatureAlgorithms {
  1906. y[0] = uint8(sigAlgo >> 8)
  1907. y[1] = uint8(sigAlgo)
  1908. y = y[2:]
  1909. }
  1910. }
  1911. y[0] = uint8(casLength >> 8)
  1912. y[1] = uint8(casLength)
  1913. y = y[2:]
  1914. for _, ca := range m.certificateAuthorities {
  1915. y[0] = uint8(len(ca) >> 8)
  1916. y[1] = uint8(len(ca))
  1917. y = y[2:]
  1918. copy(y, ca)
  1919. y = y[len(ca):]
  1920. }
  1921. m.raw = x
  1922. return
  1923. }
  1924. func (m *certificateRequestMsg) unmarshal(data []byte) alert {
  1925. m.raw = data
  1926. if len(data) < 5 {
  1927. return alertDecodeError
  1928. }
  1929. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1930. if uint32(len(data))-4 != length {
  1931. return alertDecodeError
  1932. }
  1933. numCertTypes := int(data[4])
  1934. data = data[5:]
  1935. if numCertTypes == 0 || len(data) <= numCertTypes {
  1936. return alertDecodeError
  1937. }
  1938. m.certificateTypes = make([]byte, numCertTypes)
  1939. if copy(m.certificateTypes, data) != numCertTypes {
  1940. return alertDecodeError
  1941. }
  1942. data = data[numCertTypes:]
  1943. if m.hasSignatureAndHash {
  1944. if len(data) < 2 {
  1945. return alertDecodeError
  1946. }
  1947. sigAndHashLen := uint16(data[0])<<8 | uint16(data[1])
  1948. data = data[2:]
  1949. if sigAndHashLen&1 != 0 {
  1950. return alertDecodeError
  1951. }
  1952. if len(data) < int(sigAndHashLen) {
  1953. return alertDecodeError
  1954. }
  1955. numSigAlgos := sigAndHashLen / 2
  1956. m.supportedSignatureAlgorithms = make([]SignatureScheme, numSigAlgos)
  1957. for i := range m.supportedSignatureAlgorithms {
  1958. m.supportedSignatureAlgorithms[i] = SignatureScheme(data[0])<<8 | SignatureScheme(data[1])
  1959. data = data[2:]
  1960. }
  1961. }
  1962. if len(data) < 2 {
  1963. return alertDecodeError
  1964. }
  1965. casLength := uint16(data[0])<<8 | uint16(data[1])
  1966. data = data[2:]
  1967. if len(data) < int(casLength) {
  1968. return alertDecodeError
  1969. }
  1970. cas := make([]byte, casLength)
  1971. copy(cas, data)
  1972. data = data[casLength:]
  1973. m.certificateAuthorities = nil
  1974. for len(cas) > 0 {
  1975. if len(cas) < 2 {
  1976. return alertDecodeError
  1977. }
  1978. caLen := uint16(cas[0])<<8 | uint16(cas[1])
  1979. cas = cas[2:]
  1980. if len(cas) < int(caLen) {
  1981. return alertDecodeError
  1982. }
  1983. m.certificateAuthorities = append(m.certificateAuthorities, cas[:caLen])
  1984. cas = cas[caLen:]
  1985. }
  1986. if len(data) != 0 {
  1987. return alertDecodeError
  1988. }
  1989. return alertSuccess
  1990. }
  1991. type certificateRequestMsg13 struct {
  1992. raw []byte
  1993. requestContext []byte
  1994. supportedSignatureAlgorithms []SignatureScheme
  1995. supportedSignatureAlgorithmsCert []SignatureScheme
  1996. certificateAuthorities [][]byte
  1997. }
  1998. func (m *certificateRequestMsg13) equal(i interface{}) bool {
  1999. m1, ok := i.(*certificateRequestMsg13)
  2000. return ok &&
  2001. bytes.Equal(m.raw, m1.raw) &&
  2002. bytes.Equal(m.requestContext, m1.requestContext) &&
  2003. eqByteSlices(m.certificateAuthorities, m1.certificateAuthorities) &&
  2004. eqSignatureAlgorithms(m.supportedSignatureAlgorithms, m1.supportedSignatureAlgorithms) &&
  2005. eqSignatureAlgorithms(m.supportedSignatureAlgorithmsCert, m1.supportedSignatureAlgorithmsCert)
  2006. }
  2007. func (m *certificateRequestMsg13) marshal() (x []byte) {
  2008. if m.raw != nil {
  2009. return m.raw
  2010. }
  2011. // See https://tools.ietf.org/html/draft-ietf-tls-tls13-21#section-4.3.2
  2012. length := 1 + len(m.requestContext)
  2013. numExtensions := 1
  2014. extensionsLength := 2 + 2*len(m.supportedSignatureAlgorithms)
  2015. if m.getSignatureAlgorithmsCert() != nil {
  2016. numExtensions += 1
  2017. extensionsLength += 2 + 2*len(m.getSignatureAlgorithmsCert())
  2018. }
  2019. casLength := 0
  2020. if len(m.certificateAuthorities) > 0 {
  2021. for _, ca := range m.certificateAuthorities {
  2022. casLength += 2 + len(ca)
  2023. }
  2024. extensionsLength += 2 + casLength
  2025. numExtensions++
  2026. }
  2027. extensionsLength += 4 * numExtensions
  2028. length += 2 + extensionsLength
  2029. x = make([]byte, 4+length)
  2030. x[0] = typeCertificateRequest
  2031. x[1] = uint8(length >> 16)
  2032. x[2] = uint8(length >> 8)
  2033. x[3] = uint8(length)
  2034. x[4] = uint8(len(m.requestContext))
  2035. copy(x[5:], m.requestContext)
  2036. z := x[5+len(m.requestContext):]
  2037. z[0] = byte(extensionsLength >> 8)
  2038. z[1] = byte(extensionsLength)
  2039. z = z[2:]
  2040. // TODO: this function should be reused by CH
  2041. z = marshalExtensionSignatureAlgorithms(extensionSignatureAlgorithms, z, m.supportedSignatureAlgorithms)
  2042. if m.getSignatureAlgorithmsCert() != nil {
  2043. z = marshalExtensionSignatureAlgorithms(extensionSignatureAlgorithmsCert, z, m.getSignatureAlgorithmsCert())
  2044. }
  2045. // certificate_authorities
  2046. if casLength > 0 {
  2047. z[0] = byte(extensionCAs >> 8)
  2048. z[1] = byte(extensionCAs)
  2049. l := 2 + casLength
  2050. z[2] = byte(l >> 8)
  2051. z[3] = byte(l)
  2052. z = z[4:]
  2053. z[0] = uint8(casLength >> 8)
  2054. z[1] = uint8(casLength)
  2055. z = z[2:]
  2056. for _, ca := range m.certificateAuthorities {
  2057. z[0] = uint8(len(ca) >> 8)
  2058. z[1] = uint8(len(ca))
  2059. z = z[2:]
  2060. copy(z, ca)
  2061. z = z[len(ca):]
  2062. }
  2063. }
  2064. m.raw = x
  2065. return
  2066. }
  2067. func (m *certificateRequestMsg13) unmarshal(data []byte) alert {
  2068. m.raw = data
  2069. m.supportedSignatureAlgorithms = nil
  2070. m.certificateAuthorities = nil
  2071. if len(data) < 5 {
  2072. return alertDecodeError
  2073. }
  2074. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  2075. if uint32(len(data))-4 != length {
  2076. return alertDecodeError
  2077. }
  2078. ctxLen := data[4]
  2079. if len(data) < 5+int(ctxLen)+2 {
  2080. return alertDecodeError
  2081. }
  2082. m.requestContext = data[5 : 5+ctxLen]
  2083. data = data[5+ctxLen:]
  2084. extensionsLength := int(data[0])<<8 | int(data[1])
  2085. data = data[2:]
  2086. if len(data) != extensionsLength {
  2087. return alertDecodeError
  2088. }
  2089. for len(data) != 0 {
  2090. if len(data) < 4 {
  2091. return alertDecodeError
  2092. }
  2093. extension := uint16(data[0])<<8 | uint16(data[1])
  2094. length := int(data[2])<<8 | int(data[3])
  2095. data = data[4:]
  2096. if len(data) < length {
  2097. return alertDecodeError
  2098. }
  2099. switch extension {
  2100. case extensionSignatureAlgorithms:
  2101. // TODO: unmarshalExtensionSignatureAlgorithms should be shared with CH and pre-1.3 CV
  2102. // https://tools.ietf.org/html/draft-ietf-tls-tls13-21#section-4.2.3
  2103. var err alert
  2104. m.supportedSignatureAlgorithms, err = unmarshalExtensionSignatureAlgorithms(data, length)
  2105. if err != alertSuccess {
  2106. return err
  2107. }
  2108. case extensionSignatureAlgorithmsCert:
  2109. var err alert
  2110. m.supportedSignatureAlgorithmsCert, err = unmarshalExtensionSignatureAlgorithms(data, length)
  2111. if err != alertSuccess {
  2112. return err
  2113. }
  2114. case extensionCAs:
  2115. // TODO DRY: share code with CH
  2116. if length < 2 {
  2117. return alertDecodeError
  2118. }
  2119. l := int(data[0])<<8 | int(data[1])
  2120. if l != length-2 || l < 3 {
  2121. return alertDecodeError
  2122. }
  2123. cas := make([]byte, l)
  2124. copy(cas, data[2:])
  2125. m.certificateAuthorities = nil
  2126. for len(cas) > 0 {
  2127. if len(cas) < 2 {
  2128. return alertDecodeError
  2129. }
  2130. caLen := uint16(cas[0])<<8 | uint16(cas[1])
  2131. cas = cas[2:]
  2132. if len(cas) < int(caLen) {
  2133. return alertDecodeError
  2134. }
  2135. m.certificateAuthorities = append(m.certificateAuthorities, cas[:caLen])
  2136. cas = cas[caLen:]
  2137. }
  2138. }
  2139. data = data[length:]
  2140. }
  2141. if len(m.supportedSignatureAlgorithms) == 0 {
  2142. return alertDecodeError
  2143. }
  2144. return alertSuccess
  2145. }
  2146. func (m *certificateRequestMsg13) getSignatureAlgorithmsCert() []SignatureScheme {
  2147. return signAlgosCertList(m.supportedSignatureAlgorithms, m.supportedSignatureAlgorithmsCert)
  2148. }
  2149. type certificateVerifyMsg struct {
  2150. raw []byte
  2151. hasSignatureAndHash bool
  2152. signatureAlgorithm SignatureScheme
  2153. signature []byte
  2154. }
  2155. func (m *certificateVerifyMsg) equal(i interface{}) bool {
  2156. m1, ok := i.(*certificateVerifyMsg)
  2157. if !ok {
  2158. return false
  2159. }
  2160. return bytes.Equal(m.raw, m1.raw) &&
  2161. m.hasSignatureAndHash == m1.hasSignatureAndHash &&
  2162. m.signatureAlgorithm == m1.signatureAlgorithm &&
  2163. bytes.Equal(m.signature, m1.signature)
  2164. }
  2165. func (m *certificateVerifyMsg) marshal() (x []byte) {
  2166. if m.raw != nil {
  2167. return m.raw
  2168. }
  2169. // See http://tools.ietf.org/html/rfc4346#section-7.4.8
  2170. siglength := len(m.signature)
  2171. length := 2 + siglength
  2172. if m.hasSignatureAndHash {
  2173. length += 2
  2174. }
  2175. x = make([]byte, 4+length)
  2176. x[0] = typeCertificateVerify
  2177. x[1] = uint8(length >> 16)
  2178. x[2] = uint8(length >> 8)
  2179. x[3] = uint8(length)
  2180. y := x[4:]
  2181. if m.hasSignatureAndHash {
  2182. y[0] = uint8(m.signatureAlgorithm >> 8)
  2183. y[1] = uint8(m.signatureAlgorithm)
  2184. y = y[2:]
  2185. }
  2186. y[0] = uint8(siglength >> 8)
  2187. y[1] = uint8(siglength)
  2188. copy(y[2:], m.signature)
  2189. m.raw = x
  2190. return
  2191. }
  2192. func (m *certificateVerifyMsg) unmarshal(data []byte) alert {
  2193. m.raw = data
  2194. if len(data) < 6 {
  2195. return alertDecodeError
  2196. }
  2197. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  2198. if uint32(len(data))-4 != length {
  2199. return alertDecodeError
  2200. }
  2201. data = data[4:]
  2202. if m.hasSignatureAndHash {
  2203. m.signatureAlgorithm = SignatureScheme(data[0])<<8 | SignatureScheme(data[1])
  2204. data = data[2:]
  2205. }
  2206. if len(data) < 2 {
  2207. return alertDecodeError
  2208. }
  2209. siglength := int(data[0])<<8 + int(data[1])
  2210. data = data[2:]
  2211. if len(data) != siglength {
  2212. return alertDecodeError
  2213. }
  2214. m.signature = data
  2215. return alertSuccess
  2216. }
  2217. type newSessionTicketMsg struct {
  2218. raw []byte
  2219. ticket []byte
  2220. }
  2221. func (m *newSessionTicketMsg) equal(i interface{}) bool {
  2222. m1, ok := i.(*newSessionTicketMsg)
  2223. if !ok {
  2224. return false
  2225. }
  2226. return bytes.Equal(m.raw, m1.raw) &&
  2227. bytes.Equal(m.ticket, m1.ticket)
  2228. }
  2229. func (m *newSessionTicketMsg) marshal() (x []byte) {
  2230. if m.raw != nil {
  2231. return m.raw
  2232. }
  2233. // See http://tools.ietf.org/html/rfc5077#section-3.3
  2234. ticketLen := len(m.ticket)
  2235. length := 2 + 4 + ticketLen
  2236. x = make([]byte, 4+length)
  2237. x[0] = typeNewSessionTicket
  2238. x[1] = uint8(length >> 16)
  2239. x[2] = uint8(length >> 8)
  2240. x[3] = uint8(length)
  2241. x[8] = uint8(ticketLen >> 8)
  2242. x[9] = uint8(ticketLen)
  2243. copy(x[10:], m.ticket)
  2244. m.raw = x
  2245. return
  2246. }
  2247. func (m *newSessionTicketMsg) unmarshal(data []byte) alert {
  2248. m.raw = data
  2249. if len(data) < 10 {
  2250. return alertDecodeError
  2251. }
  2252. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  2253. if uint32(len(data))-4 != length {
  2254. return alertDecodeError
  2255. }
  2256. ticketLen := int(data[8])<<8 + int(data[9])
  2257. if len(data)-10 != ticketLen {
  2258. return alertDecodeError
  2259. }
  2260. m.ticket = data[10:]
  2261. return alertSuccess
  2262. }
  2263. type newSessionTicketMsg13 struct {
  2264. raw []byte
  2265. lifetime uint32
  2266. ageAdd uint32
  2267. nonce []byte
  2268. ticket []byte
  2269. withEarlyDataInfo bool
  2270. maxEarlyDataLength uint32
  2271. }
  2272. func (m *newSessionTicketMsg13) equal(i interface{}) bool {
  2273. m1, ok := i.(*newSessionTicketMsg13)
  2274. if !ok {
  2275. return false
  2276. }
  2277. return bytes.Equal(m.raw, m1.raw) &&
  2278. m.lifetime == m1.lifetime &&
  2279. m.ageAdd == m1.ageAdd &&
  2280. bytes.Equal(m.nonce, m1.nonce) &&
  2281. bytes.Equal(m.ticket, m1.ticket) &&
  2282. m.withEarlyDataInfo == m1.withEarlyDataInfo &&
  2283. m.maxEarlyDataLength == m1.maxEarlyDataLength
  2284. }
  2285. func (m *newSessionTicketMsg13) marshal() (x []byte) {
  2286. if m.raw != nil {
  2287. return m.raw
  2288. }
  2289. // See https://tools.ietf.org/html/draft-ietf-tls-tls13-21#section-4.6.1
  2290. nonceLen := len(m.nonce)
  2291. ticketLen := len(m.ticket)
  2292. length := 13 + nonceLen + ticketLen
  2293. if m.withEarlyDataInfo {
  2294. length += 8
  2295. }
  2296. x = make([]byte, 4+length)
  2297. x[0] = typeNewSessionTicket
  2298. x[1] = uint8(length >> 16)
  2299. x[2] = uint8(length >> 8)
  2300. x[3] = uint8(length)
  2301. x[4] = uint8(m.lifetime >> 24)
  2302. x[5] = uint8(m.lifetime >> 16)
  2303. x[6] = uint8(m.lifetime >> 8)
  2304. x[7] = uint8(m.lifetime)
  2305. x[8] = uint8(m.ageAdd >> 24)
  2306. x[9] = uint8(m.ageAdd >> 16)
  2307. x[10] = uint8(m.ageAdd >> 8)
  2308. x[11] = uint8(m.ageAdd)
  2309. x[12] = uint8(nonceLen)
  2310. copy(x[13:13+nonceLen], m.nonce)
  2311. y := x[13+nonceLen:]
  2312. y[0] = uint8(ticketLen >> 8)
  2313. y[1] = uint8(ticketLen)
  2314. copy(y[2:2+ticketLen], m.ticket)
  2315. if m.withEarlyDataInfo {
  2316. z := y[2+ticketLen:]
  2317. // z[0] is already 0, this is the extensions vector length.
  2318. z[1] = 8
  2319. z[2] = uint8(extensionEarlyData >> 8)
  2320. z[3] = uint8(extensionEarlyData)
  2321. z[5] = 4
  2322. z[6] = uint8(m.maxEarlyDataLength >> 24)
  2323. z[7] = uint8(m.maxEarlyDataLength >> 16)
  2324. z[8] = uint8(m.maxEarlyDataLength >> 8)
  2325. z[9] = uint8(m.maxEarlyDataLength)
  2326. }
  2327. m.raw = x
  2328. return
  2329. }
  2330. func (m *newSessionTicketMsg13) unmarshal(data []byte) alert {
  2331. m.raw = data
  2332. m.maxEarlyDataLength = 0
  2333. m.withEarlyDataInfo = false
  2334. if len(data) < 17 {
  2335. return alertDecodeError
  2336. }
  2337. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  2338. if uint32(len(data))-4 != length {
  2339. return alertDecodeError
  2340. }
  2341. m.lifetime = uint32(data[4])<<24 | uint32(data[5])<<16 |
  2342. uint32(data[6])<<8 | uint32(data[7])
  2343. m.ageAdd = uint32(data[8])<<24 | uint32(data[9])<<16 |
  2344. uint32(data[10])<<8 | uint32(data[11])
  2345. nonceLen := int(data[12])
  2346. if nonceLen == 0 || 13+nonceLen+2 > len(data) {
  2347. return alertDecodeError
  2348. }
  2349. m.nonce = data[13 : 13+nonceLen]
  2350. data = data[13+nonceLen:]
  2351. ticketLen := int(data[0])<<8 + int(data[1])
  2352. if ticketLen == 0 || 2+ticketLen+2 > len(data) {
  2353. return alertDecodeError
  2354. }
  2355. m.ticket = data[2 : 2+ticketLen]
  2356. data = data[2+ticketLen:]
  2357. extLen := int(data[0])<<8 + int(data[1])
  2358. if extLen != len(data)-2 {
  2359. return alertDecodeError
  2360. }
  2361. data = data[2:]
  2362. for len(data) > 0 {
  2363. if len(data) < 4 {
  2364. return alertDecodeError
  2365. }
  2366. extType := uint16(data[0])<<8 + uint16(data[1])
  2367. length := int(data[2])<<8 + int(data[3])
  2368. data = data[4:]
  2369. switch extType {
  2370. case extensionEarlyData:
  2371. if length != 4 {
  2372. return alertDecodeError
  2373. }
  2374. m.withEarlyDataInfo = true
  2375. m.maxEarlyDataLength = uint32(data[0])<<24 | uint32(data[1])<<16 |
  2376. uint32(data[2])<<8 | uint32(data[3])
  2377. }
  2378. data = data[length:]
  2379. }
  2380. return alertSuccess
  2381. }
  2382. type endOfEarlyDataMsg struct {
  2383. }
  2384. func (*endOfEarlyDataMsg) marshal() []byte {
  2385. return []byte{typeEndOfEarlyData, 0, 0, 0}
  2386. }
  2387. func (*endOfEarlyDataMsg) unmarshal(data []byte) alert {
  2388. if len(data) != 4 {
  2389. return alertDecodeError
  2390. }
  2391. return alertSuccess
  2392. }
  2393. type helloRequestMsg struct {
  2394. }
  2395. func (*helloRequestMsg) marshal() []byte {
  2396. return []byte{typeHelloRequest, 0, 0, 0}
  2397. }
  2398. func (*helloRequestMsg) unmarshal(data []byte) alert {
  2399. if len(data) != 4 {
  2400. return alertDecodeError
  2401. }
  2402. return alertSuccess
  2403. }
  2404. func eqUint16s(x, y []uint16) bool {
  2405. if len(x) != len(y) {
  2406. return false
  2407. }
  2408. for i, v := range x {
  2409. if y[i] != v {
  2410. return false
  2411. }
  2412. }
  2413. return true
  2414. }
  2415. func eqCurveIDs(x, y []CurveID) bool {
  2416. if len(x) != len(y) {
  2417. return false
  2418. }
  2419. for i, v := range x {
  2420. if y[i] != v {
  2421. return false
  2422. }
  2423. }
  2424. return true
  2425. }
  2426. func eqStrings(x, y []string) bool {
  2427. if len(x) != len(y) {
  2428. return false
  2429. }
  2430. for i, v := range x {
  2431. if y[i] != v {
  2432. return false
  2433. }
  2434. }
  2435. return true
  2436. }
  2437. func eqByteSlices(x, y [][]byte) bool {
  2438. if len(x) != len(y) {
  2439. return false
  2440. }
  2441. for i, v := range x {
  2442. if !bytes.Equal(v, y[i]) {
  2443. return false
  2444. }
  2445. }
  2446. return true
  2447. }
  2448. func eqSignatureAlgorithms(x, y []SignatureScheme) bool {
  2449. if len(x) != len(y) {
  2450. return false
  2451. }
  2452. for i, v := range x {
  2453. if v != y[i] {
  2454. return false
  2455. }
  2456. }
  2457. return true
  2458. }
  2459. func eqKeyShares(x, y []keyShare) bool {
  2460. if len(x) != len(y) {
  2461. return false
  2462. }
  2463. for i := range x {
  2464. if x[i].group != y[i].group {
  2465. return false
  2466. }
  2467. if !bytes.Equal(x[i].data, y[i].data) {
  2468. return false
  2469. }
  2470. }
  2471. return true
  2472. }
  2473. func findExtension(data []byte, extensionType uint16) []byte {
  2474. for len(data) != 0 {
  2475. if len(data) < 4 {
  2476. return nil
  2477. }
  2478. extension := uint16(data[0])<<8 | uint16(data[1])
  2479. length := int(data[2])<<8 | int(data[3])
  2480. data = data[4:]
  2481. if len(data) < length {
  2482. return nil
  2483. }
  2484. if extension == extensionType {
  2485. return data[:length]
  2486. }
  2487. data = data[length:]
  2488. }
  2489. return nil
  2490. }