handshake_messages.go 77 KB

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