25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

t1_lib.c 103 KiB

Implement draft-ietf-tls-curve25519-01 in C. The new curve is not enabled by default. As EC_GROUP/EC_POINT is a bit too complex for X25519, this introduces an SSL_ECDH_METHOD abstraction which wraps just the raw ECDH operation. It also tidies up some of the curve code which kept converting back and force between NIDs and curve IDs. Now everything transits as curve IDs except for API entry points (SSL_set1_curves) which take NIDs. Those convert immediately and act on curve IDs from then on. Note that, like the Go implementation, this slightly tweaks the order of operations. The client sees the server public key before sending its own. To keep the abstraction simple, SSL_ECDH_METHOD expects to generate a keypair before consuming the peer's public key. Instead, the client handshake stashes the serialized peer public value and defers parsing it until it comes time to send ClientKeyExchange. (This is analogous to what it was doing before where it stashed the parsed peer public value instead.) It still uses TLS 1.2 terminology everywhere, but this abstraction should also be compatible with TLS 1.3 which unifies (EC)DH-style key exchanges. (Accordingly, this abstraction intentionally does not handle parsing the ClientKeyExchange/ServerKeyExchange framing or attempt to handle asynchronous plain RSA or the authentication bits.) BUG=571231 Change-Id: Iba09dddee5bcdfeb2b70185308e8ab0632717932 Reviewed-on: https://boringssl-review.googlesource.com/6780 Reviewed-by: Adam Langley <agl@google.com>
8 yıl önce
Implement draft-ietf-tls-curve25519-01 in C. The new curve is not enabled by default. As EC_GROUP/EC_POINT is a bit too complex for X25519, this introduces an SSL_ECDH_METHOD abstraction which wraps just the raw ECDH operation. It also tidies up some of the curve code which kept converting back and force between NIDs and curve IDs. Now everything transits as curve IDs except for API entry points (SSL_set1_curves) which take NIDs. Those convert immediately and act on curve IDs from then on. Note that, like the Go implementation, this slightly tweaks the order of operations. The client sees the server public key before sending its own. To keep the abstraction simple, SSL_ECDH_METHOD expects to generate a keypair before consuming the peer's public key. Instead, the client handshake stashes the serialized peer public value and defers parsing it until it comes time to send ClientKeyExchange. (This is analogous to what it was doing before where it stashed the parsed peer public value instead.) It still uses TLS 1.2 terminology everywhere, but this abstraction should also be compatible with TLS 1.3 which unifies (EC)DH-style key exchanges. (Accordingly, this abstraction intentionally does not handle parsing the ClientKeyExchange/ServerKeyExchange framing or attempt to handle asynchronous plain RSA or the authentication bits.) BUG=571231 Change-Id: Iba09dddee5bcdfeb2b70185308e8ab0632717932 Reviewed-on: https://boringssl-review.googlesource.com/6780 Reviewed-by: Adam Langley <agl@google.com>
8 yıl önce
Implement draft-ietf-tls-curve25519-01 in C. The new curve is not enabled by default. As EC_GROUP/EC_POINT is a bit too complex for X25519, this introduces an SSL_ECDH_METHOD abstraction which wraps just the raw ECDH operation. It also tidies up some of the curve code which kept converting back and force between NIDs and curve IDs. Now everything transits as curve IDs except for API entry points (SSL_set1_curves) which take NIDs. Those convert immediately and act on curve IDs from then on. Note that, like the Go implementation, this slightly tweaks the order of operations. The client sees the server public key before sending its own. To keep the abstraction simple, SSL_ECDH_METHOD expects to generate a keypair before consuming the peer's public key. Instead, the client handshake stashes the serialized peer public value and defers parsing it until it comes time to send ClientKeyExchange. (This is analogous to what it was doing before where it stashed the parsed peer public value instead.) It still uses TLS 1.2 terminology everywhere, but this abstraction should also be compatible with TLS 1.3 which unifies (EC)DH-style key exchanges. (Accordingly, this abstraction intentionally does not handle parsing the ClientKeyExchange/ServerKeyExchange framing or attempt to handle asynchronous plain RSA or the authentication bits.) BUG=571231 Change-Id: Iba09dddee5bcdfeb2b70185308e8ab0632717932 Reviewed-on: https://boringssl-review.googlesource.com/6780 Reviewed-by: Adam Langley <agl@google.com>
8 yıl önce
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
7 yıl önce
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
7 yıl önce
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
7 yıl önce
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
7 yıl önce
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
7 yıl önce
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
7 yıl önce
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
7 yıl önce
Make CBB_len relative to its argument. Rather than the length of the top-level CBB, which is kind of odd when ASN.1 length prefixes are not yet determined, return the number of bytes written to the CBB so far. This can be computed without increasing the size of CBB at all. Have offset and pending_*. This means functions which take in a CBB as argument will not be sensitive to whether the CBB is a top-level or child CBB. The extensions logic had to be careful to only ever compare differences of lengths, which was awkward. The reversal will also allow for the following pattern in the future, once CBB_add_space is split into, say, CBB_reserve and CBB_did_write and we add a CBB_data: uint8_t *signature; size_t signature_len = 0; if (!CBB_add_asn1(out, &cert, CBB_ASN1_SEQUENCE) || /* Emit the TBSCertificate. */ !CBB_add_asn1(&cert, &tbs_cert, CBS_ASN1_SEQUENCE) || !CBB_add_tbs_cert_stuff(&tbs_cert, stuff) || !CBB_flush(&cert) || /* Feed it into md_ctx. */ !EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL, pkey) || !EVP_DigestSignUpdate(&md_ctx, CBB_data(&cert), CBB_len(&cert)) || /* Emit the signature algorithm. */ !CBB_add_asn1(&cert, &sig_alg, CBS_ASN1_SEQUENCE) || !CBB_add_sigalg_stuff(&sig_alg, other_stuff) || /* Emit the signature. */ !EVP_DigestSignFinal(&md_ctx, NULL, &signature_len) || !CBB_reserve(&cert, &signature, signature_len) || !EVP_DigestSignFinal(&md_ctx, signature, &signature_len) || !CBB_did_write(&cert, signature_len)) { goto err; } (Were TBSCertificate not the first field, we'd still have to sample CBB_len(&cert), but at least that's reasonable straight-forward. The alternative would be if CBB_data and CBB_len somehow worked on recently-invalidated CBBs, but that would go wrong once the invalidated CBB's parent flushed and possibly shifts everything.) And similar for signing ServerKeyExchange. Change-Id: I7761e492ae472d7632875b5666b6088970261b14 Reviewed-on: https://boringssl-review.googlesource.com/6681 Reviewed-by: Adam Langley <agl@google.com>
9 yıl önce
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com). */
  108. #include <openssl/ssl.h>
  109. #include <assert.h>
  110. #include <limits.h>
  111. #include <stdlib.h>
  112. #include <string.h>
  113. #include <openssl/bytestring.h>
  114. #include <openssl/digest.h>
  115. #include <openssl/err.h>
  116. #include <openssl/evp.h>
  117. #include <openssl/hmac.h>
  118. #include <openssl/mem.h>
  119. #include <openssl/nid.h>
  120. #include <openssl/rand.h>
  121. #include <openssl/type_check.h>
  122. #include "internal.h"
  123. #include "../crypto/internal.h"
  124. static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
  125. static int compare_uint16_t(const void *p1, const void *p2) {
  126. uint16_t u1 = *((const uint16_t *)p1);
  127. uint16_t u2 = *((const uint16_t *)p2);
  128. if (u1 < u2) {
  129. return -1;
  130. } else if (u1 > u2) {
  131. return 1;
  132. } else {
  133. return 0;
  134. }
  135. }
  136. /* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
  137. * more than one extension of the same type in a ClientHello or ServerHello.
  138. * This function does an initial scan over the extensions block to filter those
  139. * out. */
  140. static int tls1_check_duplicate_extensions(const CBS *cbs) {
  141. CBS extensions = *cbs;
  142. size_t num_extensions = 0, i = 0;
  143. uint16_t *extension_types = NULL;
  144. int ret = 0;
  145. /* First pass: count the extensions. */
  146. while (CBS_len(&extensions) > 0) {
  147. uint16_t type;
  148. CBS extension;
  149. if (!CBS_get_u16(&extensions, &type) ||
  150. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  151. goto done;
  152. }
  153. num_extensions++;
  154. }
  155. if (num_extensions == 0) {
  156. return 1;
  157. }
  158. extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
  159. if (extension_types == NULL) {
  160. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  161. goto done;
  162. }
  163. /* Second pass: gather the extension types. */
  164. extensions = *cbs;
  165. for (i = 0; i < num_extensions; i++) {
  166. CBS extension;
  167. if (!CBS_get_u16(&extensions, &extension_types[i]) ||
  168. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  169. /* This should not happen. */
  170. goto done;
  171. }
  172. }
  173. assert(CBS_len(&extensions) == 0);
  174. /* Sort the extensions and make sure there are no duplicates. */
  175. qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
  176. for (i = 1; i < num_extensions; i++) {
  177. if (extension_types[i - 1] == extension_types[i]) {
  178. goto done;
  179. }
  180. }
  181. ret = 1;
  182. done:
  183. OPENSSL_free(extension_types);
  184. return ret;
  185. }
  186. int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, const uint8_t *in,
  187. size_t in_len) {
  188. OPENSSL_memset(out, 0, sizeof(*out));
  189. out->ssl = ssl;
  190. out->client_hello = in;
  191. out->client_hello_len = in_len;
  192. CBS client_hello, random, session_id;
  193. CBS_init(&client_hello, out->client_hello, out->client_hello_len);
  194. if (!CBS_get_u16(&client_hello, &out->version) ||
  195. !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
  196. !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
  197. CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  198. return 0;
  199. }
  200. out->random = CBS_data(&random);
  201. out->random_len = CBS_len(&random);
  202. out->session_id = CBS_data(&session_id);
  203. out->session_id_len = CBS_len(&session_id);
  204. /* Skip past DTLS cookie */
  205. if (SSL_is_dtls(out->ssl)) {
  206. CBS cookie;
  207. if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
  208. CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
  209. return 0;
  210. }
  211. }
  212. CBS cipher_suites, compression_methods;
  213. if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
  214. CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
  215. !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
  216. CBS_len(&compression_methods) < 1) {
  217. return 0;
  218. }
  219. out->cipher_suites = CBS_data(&cipher_suites);
  220. out->cipher_suites_len = CBS_len(&cipher_suites);
  221. out->compression_methods = CBS_data(&compression_methods);
  222. out->compression_methods_len = CBS_len(&compression_methods);
  223. /* If the ClientHello ends here then it's valid, but doesn't have any
  224. * extensions. (E.g. SSLv3.) */
  225. if (CBS_len(&client_hello) == 0) {
  226. out->extensions = NULL;
  227. out->extensions_len = 0;
  228. return 1;
  229. }
  230. /* Extract extensions and check it is valid. */
  231. CBS extensions;
  232. if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
  233. !tls1_check_duplicate_extensions(&extensions) ||
  234. CBS_len(&client_hello) != 0) {
  235. return 0;
  236. }
  237. out->extensions = CBS_data(&extensions);
  238. out->extensions_len = CBS_len(&extensions);
  239. return 1;
  240. }
  241. int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
  242. CBS *out, uint16_t extension_type) {
  243. CBS extensions;
  244. CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
  245. while (CBS_len(&extensions) != 0) {
  246. /* Decode the next extension. */
  247. uint16_t type;
  248. CBS extension;
  249. if (!CBS_get_u16(&extensions, &type) ||
  250. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  251. return 0;
  252. }
  253. if (type == extension_type) {
  254. *out = extension;
  255. return 1;
  256. }
  257. }
  258. return 0;
  259. }
  260. int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
  261. uint16_t extension_type,
  262. const uint8_t **out_data,
  263. size_t *out_len) {
  264. CBS cbs;
  265. if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
  266. return 0;
  267. }
  268. *out_data = CBS_data(&cbs);
  269. *out_len = CBS_len(&cbs);
  270. return 1;
  271. }
  272. static const uint16_t kDefaultGroups[] = {
  273. SSL_CURVE_X25519,
  274. SSL_CURVE_SECP256R1,
  275. SSL_CURVE_SECP384R1,
  276. };
  277. void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
  278. size_t *out_group_ids_len) {
  279. *out_group_ids = ssl->supported_group_list;
  280. *out_group_ids_len = ssl->supported_group_list_len;
  281. if (!*out_group_ids) {
  282. *out_group_ids = kDefaultGroups;
  283. *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
  284. }
  285. }
  286. int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
  287. SSL *const ssl = hs->ssl;
  288. assert(ssl->server);
  289. const uint16_t *groups, *pref, *supp;
  290. size_t groups_len, pref_len, supp_len;
  291. tls1_get_grouplist(ssl, &groups, &groups_len);
  292. /* Clients are not required to send a supported_groups extension. In this
  293. * case, the server is free to pick any group it likes. See RFC 4492,
  294. * section 4, paragraph 3.
  295. *
  296. * However, in the interests of compatibility, we will skip ECDH if the
  297. * client didn't send an extension because we can't be sure that they'll
  298. * support our favoured group. Thus we do not special-case an emtpy
  299. * |peer_supported_group_list|. */
  300. if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
  301. pref = groups;
  302. pref_len = groups_len;
  303. supp = hs->peer_supported_group_list;
  304. supp_len = hs->peer_supported_group_list_len;
  305. } else {
  306. pref = hs->peer_supported_group_list;
  307. pref_len = hs->peer_supported_group_list_len;
  308. supp = groups;
  309. supp_len = groups_len;
  310. }
  311. for (size_t i = 0; i < pref_len; i++) {
  312. for (size_t j = 0; j < supp_len; j++) {
  313. if (pref[i] == supp[j]) {
  314. *out_group_id = pref[i];
  315. return 1;
  316. }
  317. }
  318. }
  319. return 0;
  320. }
  321. int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
  322. const int *curves, size_t ncurves) {
  323. uint16_t *group_ids;
  324. group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
  325. if (group_ids == NULL) {
  326. return 0;
  327. }
  328. for (size_t i = 0; i < ncurves; i++) {
  329. if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
  330. OPENSSL_free(group_ids);
  331. return 0;
  332. }
  333. }
  334. OPENSSL_free(*out_group_ids);
  335. *out_group_ids = group_ids;
  336. *out_group_ids_len = ncurves;
  337. return 1;
  338. }
  339. int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
  340. const char *curves) {
  341. uint16_t *group_ids = NULL;
  342. size_t ncurves = 0;
  343. const char *col;
  344. const char *ptr = curves;
  345. do {
  346. col = strchr(ptr, ':');
  347. uint16_t group_id;
  348. if (!ssl_name_to_group_id(&group_id, ptr,
  349. col ? (size_t)(col - ptr) : strlen(ptr))) {
  350. goto err;
  351. }
  352. uint16_t *new_group_ids = OPENSSL_realloc(group_ids,
  353. (ncurves + 1) * sizeof(uint16_t));
  354. if (new_group_ids == NULL) {
  355. goto err;
  356. }
  357. group_ids = new_group_ids;
  358. group_ids[ncurves] = group_id;
  359. ncurves++;
  360. if (col) {
  361. ptr = col + 1;
  362. }
  363. } while (col);
  364. OPENSSL_free(*out_group_ids);
  365. *out_group_ids = group_ids;
  366. *out_group_ids_len = ncurves;
  367. return 1;
  368. err:
  369. OPENSSL_free(group_ids);
  370. return 0;
  371. }
  372. int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
  373. const uint16_t *groups;
  374. size_t groups_len;
  375. tls1_get_grouplist(ssl, &groups, &groups_len);
  376. for (size_t i = 0; i < groups_len; i++) {
  377. if (groups[i] == group_id) {
  378. return 1;
  379. }
  380. }
  381. return 0;
  382. }
  383. /* kVerifySignatureAlgorithms is the default list of accepted signature
  384. * algorithms for verifying.
  385. *
  386. * For now, RSA-PSS signature algorithms are not enabled on Android's system
  387. * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
  388. * restore them. */
  389. static const uint16_t kVerifySignatureAlgorithms[] = {
  390. /* List our preferred algorithms first. */
  391. SSL_SIGN_ED25519,
  392. SSL_SIGN_ECDSA_SECP256R1_SHA256,
  393. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  394. SSL_SIGN_RSA_PSS_SHA256,
  395. #endif
  396. SSL_SIGN_RSA_PKCS1_SHA256,
  397. /* Larger hashes are acceptable. */
  398. SSL_SIGN_ECDSA_SECP384R1_SHA384,
  399. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  400. SSL_SIGN_RSA_PSS_SHA384,
  401. #endif
  402. SSL_SIGN_RSA_PKCS1_SHA384,
  403. /* TODO(davidben): Remove this. */
  404. #if defined(BORINGSSL_ANDROID_SYSTEM)
  405. SSL_SIGN_ECDSA_SECP521R1_SHA512,
  406. #endif
  407. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  408. SSL_SIGN_RSA_PSS_SHA512,
  409. #endif
  410. SSL_SIGN_RSA_PKCS1_SHA512,
  411. /* For now, SHA-1 is still accepted but least preferable. */
  412. SSL_SIGN_RSA_PKCS1_SHA1,
  413. };
  414. /* kSignSignatureAlgorithms is the default list of supported signature
  415. * algorithms for signing.
  416. *
  417. * For now, RSA-PSS signature algorithms are not enabled on Android's system
  418. * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
  419. * restore them. */
  420. static const uint16_t kSignSignatureAlgorithms[] = {
  421. /* List our preferred algorithms first. */
  422. SSL_SIGN_ED25519,
  423. SSL_SIGN_ECDSA_SECP256R1_SHA256,
  424. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  425. SSL_SIGN_RSA_PSS_SHA256,
  426. #endif
  427. SSL_SIGN_RSA_PKCS1_SHA256,
  428. /* If needed, sign larger hashes.
  429. *
  430. * TODO(davidben): Determine which of these may be pruned. */
  431. SSL_SIGN_ECDSA_SECP384R1_SHA384,
  432. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  433. SSL_SIGN_RSA_PSS_SHA384,
  434. #endif
  435. SSL_SIGN_RSA_PKCS1_SHA384,
  436. SSL_SIGN_ECDSA_SECP521R1_SHA512,
  437. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  438. SSL_SIGN_RSA_PSS_SHA512,
  439. #endif
  440. SSL_SIGN_RSA_PKCS1_SHA512,
  441. /* If the peer supports nothing else, sign with SHA-1. */
  442. SSL_SIGN_ECDSA_SHA1,
  443. SSL_SIGN_RSA_PKCS1_SHA1,
  444. };
  445. void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
  446. ctx->ed25519_enabled = !!enabled;
  447. }
  448. int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
  449. const uint16_t *sigalgs = kVerifySignatureAlgorithms;
  450. size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
  451. if (ssl->ctx->num_verify_sigalgs != 0) {
  452. sigalgs = ssl->ctx->verify_sigalgs;
  453. num_sigalgs = ssl->ctx->num_verify_sigalgs;
  454. }
  455. for (size_t i = 0; i < num_sigalgs; i++) {
  456. if (sigalgs == kVerifySignatureAlgorithms &&
  457. sigalgs[i] == SSL_SIGN_ED25519 &&
  458. !ssl->ctx->ed25519_enabled) {
  459. continue;
  460. }
  461. if (!CBB_add_u16(out, sigalgs[i])) {
  462. return 0;
  463. }
  464. }
  465. return 1;
  466. }
  467. int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg) {
  468. const uint16_t *sigalgs = kVerifySignatureAlgorithms;
  469. size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
  470. if (ssl->ctx->num_verify_sigalgs != 0) {
  471. sigalgs = ssl->ctx->verify_sigalgs;
  472. num_sigalgs = ssl->ctx->num_verify_sigalgs;
  473. }
  474. for (size_t i = 0; i < num_sigalgs; i++) {
  475. if (sigalgs == kVerifySignatureAlgorithms &&
  476. sigalgs[i] == SSL_SIGN_ED25519 &&
  477. !ssl->ctx->ed25519_enabled) {
  478. continue;
  479. }
  480. if (sigalg == sigalgs[i]) {
  481. return 1;
  482. }
  483. }
  484. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  485. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  486. return 0;
  487. }
  488. /* tls_extension represents a TLS extension that is handled internally. The
  489. * |init| function is called for each handshake, before any other functions of
  490. * the extension. Then the add and parse callbacks are called as needed.
  491. *
  492. * The parse callbacks receive a |CBS| that contains the contents of the
  493. * extension (i.e. not including the type and length bytes). If an extension is
  494. * not received then the parse callbacks will be called with a NULL CBS so that
  495. * they can do any processing needed to handle the absence of an extension.
  496. *
  497. * The add callbacks receive a |CBB| to which the extension can be appended but
  498. * the function is responsible for appending the type and length bytes too.
  499. *
  500. * All callbacks return one for success and zero for error. If a parse function
  501. * returns zero then a fatal alert with value |*out_alert| will be sent. If
  502. * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
  503. struct tls_extension {
  504. uint16_t value;
  505. void (*init)(SSL_HANDSHAKE *hs);
  506. int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
  507. int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  508. CBS *contents);
  509. int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  510. CBS *contents);
  511. int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
  512. };
  513. static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  514. CBS *contents) {
  515. if (contents != NULL) {
  516. /* Servers MUST NOT send this extension. */
  517. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  518. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  519. return 0;
  520. }
  521. return 1;
  522. }
  523. static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  524. CBS *contents) {
  525. /* This extension from the client is handled elsewhere. */
  526. return 1;
  527. }
  528. static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  529. return 1;
  530. }
  531. /* Server name indication (SNI).
  532. *
  533. * https://tools.ietf.org/html/rfc6066#section-3. */
  534. static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  535. SSL *const ssl = hs->ssl;
  536. if (ssl->tlsext_hostname == NULL) {
  537. return 1;
  538. }
  539. CBB contents, server_name_list, name;
  540. if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
  541. !CBB_add_u16_length_prefixed(out, &contents) ||
  542. !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
  543. !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
  544. !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
  545. !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
  546. strlen(ssl->tlsext_hostname)) ||
  547. !CBB_flush(out)) {
  548. return 0;
  549. }
  550. return 1;
  551. }
  552. static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  553. CBS *contents) {
  554. SSL *const ssl = hs->ssl;
  555. if (contents == NULL) {
  556. return 1;
  557. }
  558. if (CBS_len(contents) != 0) {
  559. return 0;
  560. }
  561. assert(ssl->tlsext_hostname != NULL);
  562. if (ssl->session == NULL) {
  563. OPENSSL_free(hs->new_session->tlsext_hostname);
  564. hs->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
  565. if (!hs->new_session->tlsext_hostname) {
  566. *out_alert = SSL_AD_INTERNAL_ERROR;
  567. return 0;
  568. }
  569. }
  570. return 1;
  571. }
  572. static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  573. CBS *contents) {
  574. if (contents == NULL) {
  575. return 1;
  576. }
  577. CBS server_name_list, host_name;
  578. uint8_t name_type;
  579. if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
  580. !CBS_get_u8(&server_name_list, &name_type) ||
  581. /* Although the server_name extension was intended to be extensible to
  582. * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
  583. * different name types will cause an error. Further, RFC 4366 originally
  584. * defined syntax inextensibly. RFC 6066 corrected this mistake, but
  585. * adding new name types is no longer feasible.
  586. *
  587. * Act as if the extensibility does not exist to simplify parsing. */
  588. !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
  589. CBS_len(&server_name_list) != 0 ||
  590. CBS_len(contents) != 0) {
  591. return 0;
  592. }
  593. if (name_type != TLSEXT_NAMETYPE_host_name ||
  594. CBS_len(&host_name) == 0 ||
  595. CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
  596. CBS_contains_zero_byte(&host_name)) {
  597. *out_alert = SSL_AD_UNRECOGNIZED_NAME;
  598. return 0;
  599. }
  600. /* Copy the hostname as a string. */
  601. if (!CBS_strdup(&host_name, &hs->hostname)) {
  602. *out_alert = SSL_AD_INTERNAL_ERROR;
  603. return 0;
  604. }
  605. hs->should_ack_sni = 1;
  606. return 1;
  607. }
  608. static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  609. if (hs->ssl->s3->session_reused ||
  610. !hs->should_ack_sni) {
  611. return 1;
  612. }
  613. if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
  614. !CBB_add_u16(out, 0 /* length */)) {
  615. return 0;
  616. }
  617. return 1;
  618. }
  619. /* Renegotiation indication.
  620. *
  621. * https://tools.ietf.org/html/rfc5746 */
  622. static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  623. SSL *const ssl = hs->ssl;
  624. /* Renegotiation indication is not necessary in TLS 1.3. */
  625. if (hs->min_version >= TLS1_3_VERSION) {
  626. return 1;
  627. }
  628. assert(ssl->s3->initial_handshake_complete ==
  629. (ssl->s3->previous_client_finished_len != 0));
  630. CBB contents, prev_finished;
  631. if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
  632. !CBB_add_u16_length_prefixed(out, &contents) ||
  633. !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
  634. !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
  635. ssl->s3->previous_client_finished_len) ||
  636. !CBB_flush(out)) {
  637. return 0;
  638. }
  639. return 1;
  640. }
  641. static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  642. CBS *contents) {
  643. SSL *const ssl = hs->ssl;
  644. if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  645. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  646. return 0;
  647. }
  648. /* Servers may not switch between omitting the extension and supporting it.
  649. * See RFC 5746, sections 3.5 and 4.2. */
  650. if (ssl->s3->initial_handshake_complete &&
  651. (contents != NULL) != ssl->s3->send_connection_binding) {
  652. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  653. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  654. return 0;
  655. }
  656. if (contents == NULL) {
  657. /* Strictly speaking, if we want to avoid an attack we should *always* see
  658. * RI even on initial ServerHello because the client doesn't see any
  659. * renegotiation during an attack. However this would mean we could not
  660. * connect to any server which doesn't support RI.
  661. *
  662. * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
  663. * practical terms every client sets it so it's just assumed here. */
  664. return 1;
  665. }
  666. const size_t expected_len = ssl->s3->previous_client_finished_len +
  667. ssl->s3->previous_server_finished_len;
  668. /* Check for logic errors */
  669. assert(!expected_len || ssl->s3->previous_client_finished_len);
  670. assert(!expected_len || ssl->s3->previous_server_finished_len);
  671. assert(ssl->s3->initial_handshake_complete ==
  672. (ssl->s3->previous_client_finished_len != 0));
  673. assert(ssl->s3->initial_handshake_complete ==
  674. (ssl->s3->previous_server_finished_len != 0));
  675. /* Parse out the extension contents. */
  676. CBS renegotiated_connection;
  677. if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
  678. CBS_len(contents) != 0) {
  679. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
  680. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  681. return 0;
  682. }
  683. /* Check that the extension matches. */
  684. if (CBS_len(&renegotiated_connection) != expected_len) {
  685. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  686. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  687. return 0;
  688. }
  689. const uint8_t *d = CBS_data(&renegotiated_connection);
  690. int ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
  691. ssl->s3->previous_client_finished_len) == 0;
  692. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  693. ok = 1;
  694. #endif
  695. if (!ok) {
  696. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  697. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  698. return 0;
  699. }
  700. d += ssl->s3->previous_client_finished_len;
  701. ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
  702. ssl->s3->previous_server_finished_len) == 0;
  703. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  704. ok = 1;
  705. #endif
  706. if (!ok) {
  707. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  708. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  709. return 0;
  710. }
  711. ssl->s3->send_connection_binding = 1;
  712. return 1;
  713. }
  714. static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  715. CBS *contents) {
  716. SSL *const ssl = hs->ssl;
  717. /* Renegotiation isn't supported as a server so this function should never be
  718. * called after the initial handshake. */
  719. assert(!ssl->s3->initial_handshake_complete);
  720. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  721. return 1;
  722. }
  723. if (contents == NULL) {
  724. return 1;
  725. }
  726. CBS renegotiated_connection;
  727. if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
  728. CBS_len(contents) != 0) {
  729. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
  730. return 0;
  731. }
  732. /* Check that the extension matches. We do not support renegotiation as a
  733. * server, so this must be empty. */
  734. if (CBS_len(&renegotiated_connection) != 0) {
  735. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  736. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  737. return 0;
  738. }
  739. ssl->s3->send_connection_binding = 1;
  740. return 1;
  741. }
  742. static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  743. SSL *const ssl = hs->ssl;
  744. /* Renegotiation isn't supported as a server so this function should never be
  745. * called after the initial handshake. */
  746. assert(!ssl->s3->initial_handshake_complete);
  747. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  748. return 1;
  749. }
  750. if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
  751. !CBB_add_u16(out, 1 /* length */) ||
  752. !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
  753. return 0;
  754. }
  755. return 1;
  756. }
  757. /* Extended Master Secret.
  758. *
  759. * https://tools.ietf.org/html/rfc7627 */
  760. static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  761. /* Extended master secret is not necessary in TLS 1.3. */
  762. if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
  763. return 1;
  764. }
  765. if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
  766. !CBB_add_u16(out, 0 /* length */)) {
  767. return 0;
  768. }
  769. return 1;
  770. }
  771. static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  772. CBS *contents) {
  773. SSL *const ssl = hs->ssl;
  774. if (contents != NULL) {
  775. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
  776. ssl->version == SSL3_VERSION ||
  777. CBS_len(contents) != 0) {
  778. return 0;
  779. }
  780. hs->extended_master_secret = 1;
  781. }
  782. /* Whether EMS is negotiated may not change on renegotiation. */
  783. if (ssl->s3->established_session != NULL &&
  784. hs->extended_master_secret !=
  785. ssl->s3->established_session->extended_master_secret) {
  786. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
  787. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  788. return 0;
  789. }
  790. return 1;
  791. }
  792. static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  793. CBS *contents) {
  794. uint16_t version = ssl3_protocol_version(hs->ssl);
  795. if (version >= TLS1_3_VERSION ||
  796. version == SSL3_VERSION) {
  797. return 1;
  798. }
  799. if (contents == NULL) {
  800. return 1;
  801. }
  802. if (CBS_len(contents) != 0) {
  803. return 0;
  804. }
  805. hs->extended_master_secret = 1;
  806. return 1;
  807. }
  808. static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  809. if (!hs->extended_master_secret) {
  810. return 1;
  811. }
  812. if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
  813. !CBB_add_u16(out, 0 /* length */)) {
  814. return 0;
  815. }
  816. return 1;
  817. }
  818. /* Session tickets.
  819. *
  820. * https://tools.ietf.org/html/rfc5077 */
  821. static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  822. SSL *const ssl = hs->ssl;
  823. /* TLS 1.3 uses a different ticket extension. */
  824. if (hs->min_version >= TLS1_3_VERSION ||
  825. SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
  826. return 1;
  827. }
  828. const uint8_t *ticket_data = NULL;
  829. int ticket_len = 0;
  830. /* Renegotiation does not participate in session resumption. However, still
  831. * advertise the extension to avoid potentially breaking servers which carry
  832. * over the state from the previous handshake, such as OpenSSL servers
  833. * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
  834. if (!ssl->s3->initial_handshake_complete &&
  835. ssl->session != NULL &&
  836. ssl->session->tlsext_tick != NULL &&
  837. /* Don't send TLS 1.3 session tickets in the ticket extension. */
  838. SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
  839. ticket_data = ssl->session->tlsext_tick;
  840. ticket_len = ssl->session->tlsext_ticklen;
  841. }
  842. CBB ticket;
  843. if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
  844. !CBB_add_u16_length_prefixed(out, &ticket) ||
  845. !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
  846. !CBB_flush(out)) {
  847. return 0;
  848. }
  849. return 1;
  850. }
  851. static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  852. CBS *contents) {
  853. SSL *const ssl = hs->ssl;
  854. if (contents == NULL) {
  855. return 1;
  856. }
  857. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  858. return 0;
  859. }
  860. /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
  861. * this function should never be called, even if the server tries to send the
  862. * extension. */
  863. assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
  864. if (CBS_len(contents) != 0) {
  865. return 0;
  866. }
  867. hs->ticket_expected = 1;
  868. return 1;
  869. }
  870. static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  871. if (!hs->ticket_expected) {
  872. return 1;
  873. }
  874. /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
  875. assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
  876. if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
  877. !CBB_add_u16(out, 0 /* length */)) {
  878. return 0;
  879. }
  880. return 1;
  881. }
  882. /* Signature Algorithms.
  883. *
  884. * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
  885. static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  886. SSL *const ssl = hs->ssl;
  887. if (hs->max_version < TLS1_2_VERSION) {
  888. return 1;
  889. }
  890. CBB contents, sigalgs_cbb;
  891. if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
  892. !CBB_add_u16_length_prefixed(out, &contents) ||
  893. !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
  894. !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
  895. !CBB_flush(out)) {
  896. return 0;
  897. }
  898. return 1;
  899. }
  900. static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  901. CBS *contents) {
  902. OPENSSL_free(hs->peer_sigalgs);
  903. hs->peer_sigalgs = NULL;
  904. hs->num_peer_sigalgs = 0;
  905. if (contents == NULL) {
  906. return 1;
  907. }
  908. CBS supported_signature_algorithms;
  909. if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
  910. CBS_len(contents) != 0 ||
  911. CBS_len(&supported_signature_algorithms) == 0 ||
  912. !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
  913. return 0;
  914. }
  915. return 1;
  916. }
  917. /* OCSP Stapling.
  918. *
  919. * https://tools.ietf.org/html/rfc6066#section-8 */
  920. static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  921. SSL *const ssl = hs->ssl;
  922. if (!ssl->ocsp_stapling_enabled) {
  923. return 1;
  924. }
  925. CBB contents;
  926. if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
  927. !CBB_add_u16_length_prefixed(out, &contents) ||
  928. !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
  929. !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
  930. !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
  931. !CBB_flush(out)) {
  932. return 0;
  933. }
  934. return 1;
  935. }
  936. static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  937. CBS *contents) {
  938. SSL *const ssl = hs->ssl;
  939. if (contents == NULL) {
  940. return 1;
  941. }
  942. /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
  943. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  944. return 0;
  945. }
  946. /* OCSP stapling is forbidden on non-certificate ciphers. */
  947. if (CBS_len(contents) != 0 ||
  948. !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  949. return 0;
  950. }
  951. /* Note this does not check for resumption in TLS 1.2. Sending
  952. * status_request here does not make sense, but OpenSSL does so and the
  953. * specification does not say anything. Tolerate it but ignore it. */
  954. hs->certificate_status_expected = 1;
  955. return 1;
  956. }
  957. static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  958. CBS *contents) {
  959. if (contents == NULL) {
  960. return 1;
  961. }
  962. uint8_t status_type;
  963. if (!CBS_get_u8(contents, &status_type)) {
  964. return 0;
  965. }
  966. /* We cannot decide whether OCSP stapling will occur yet because the correct
  967. * SSL_CTX might not have been selected. */
  968. hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
  969. return 1;
  970. }
  971. static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  972. SSL *const ssl = hs->ssl;
  973. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
  974. !hs->ocsp_stapling_requested ||
  975. ssl->cert->ocsp_response == NULL ||
  976. ssl->s3->session_reused ||
  977. !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  978. return 1;
  979. }
  980. hs->certificate_status_expected = 1;
  981. return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
  982. CBB_add_u16(out, 0 /* length */);
  983. }
  984. /* Next protocol negotiation.
  985. *
  986. * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
  987. static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  988. SSL *const ssl = hs->ssl;
  989. if (ssl->s3->initial_handshake_complete ||
  990. ssl->ctx->next_proto_select_cb == NULL ||
  991. SSL_is_dtls(ssl)) {
  992. return 1;
  993. }
  994. if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
  995. !CBB_add_u16(out, 0 /* length */)) {
  996. return 0;
  997. }
  998. return 1;
  999. }
  1000. static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1001. CBS *contents) {
  1002. SSL *const ssl = hs->ssl;
  1003. if (contents == NULL) {
  1004. return 1;
  1005. }
  1006. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1007. return 0;
  1008. }
  1009. /* If any of these are false then we should never have sent the NPN
  1010. * extension in the ClientHello and thus this function should never have been
  1011. * called. */
  1012. assert(!ssl->s3->initial_handshake_complete);
  1013. assert(!SSL_is_dtls(ssl));
  1014. assert(ssl->ctx->next_proto_select_cb != NULL);
  1015. if (ssl->s3->alpn_selected != NULL) {
  1016. /* NPN and ALPN may not be negotiated in the same connection. */
  1017. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1018. OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
  1019. return 0;
  1020. }
  1021. const uint8_t *const orig_contents = CBS_data(contents);
  1022. const size_t orig_len = CBS_len(contents);
  1023. while (CBS_len(contents) != 0) {
  1024. CBS proto;
  1025. if (!CBS_get_u8_length_prefixed(contents, &proto) ||
  1026. CBS_len(&proto) == 0) {
  1027. return 0;
  1028. }
  1029. }
  1030. uint8_t *selected;
  1031. uint8_t selected_len;
  1032. if (ssl->ctx->next_proto_select_cb(
  1033. ssl, &selected, &selected_len, orig_contents, orig_len,
  1034. ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
  1035. *out_alert = SSL_AD_INTERNAL_ERROR;
  1036. return 0;
  1037. }
  1038. OPENSSL_free(ssl->s3->next_proto_negotiated);
  1039. ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
  1040. if (ssl->s3->next_proto_negotiated == NULL) {
  1041. *out_alert = SSL_AD_INTERNAL_ERROR;
  1042. return 0;
  1043. }
  1044. ssl->s3->next_proto_negotiated_len = selected_len;
  1045. hs->next_proto_neg_seen = 1;
  1046. return 1;
  1047. }
  1048. static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1049. CBS *contents) {
  1050. SSL *const ssl = hs->ssl;
  1051. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1052. return 1;
  1053. }
  1054. if (contents != NULL && CBS_len(contents) != 0) {
  1055. return 0;
  1056. }
  1057. if (contents == NULL ||
  1058. ssl->s3->initial_handshake_complete ||
  1059. ssl->ctx->next_protos_advertised_cb == NULL ||
  1060. SSL_is_dtls(ssl)) {
  1061. return 1;
  1062. }
  1063. hs->next_proto_neg_seen = 1;
  1064. return 1;
  1065. }
  1066. static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1067. SSL *const ssl = hs->ssl;
  1068. /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
  1069. * parsed. */
  1070. if (!hs->next_proto_neg_seen) {
  1071. return 1;
  1072. }
  1073. const uint8_t *npa;
  1074. unsigned npa_len;
  1075. if (ssl->ctx->next_protos_advertised_cb(
  1076. ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
  1077. SSL_TLSEXT_ERR_OK) {
  1078. hs->next_proto_neg_seen = 0;
  1079. return 1;
  1080. }
  1081. CBB contents;
  1082. if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
  1083. !CBB_add_u16_length_prefixed(out, &contents) ||
  1084. !CBB_add_bytes(&contents, npa, npa_len) ||
  1085. !CBB_flush(out)) {
  1086. return 0;
  1087. }
  1088. return 1;
  1089. }
  1090. /* Signed certificate timestamps.
  1091. *
  1092. * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
  1093. static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1094. SSL *const ssl = hs->ssl;
  1095. if (!ssl->signed_cert_timestamps_enabled) {
  1096. return 1;
  1097. }
  1098. if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
  1099. !CBB_add_u16(out, 0 /* length */)) {
  1100. return 0;
  1101. }
  1102. return 1;
  1103. }
  1104. static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1105. CBS *contents) {
  1106. SSL *const ssl = hs->ssl;
  1107. if (contents == NULL) {
  1108. return 1;
  1109. }
  1110. /* TLS 1.3 SCTs are included in the Certificate extensions. */
  1111. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1112. *out_alert = SSL_AD_DECODE_ERROR;
  1113. return 0;
  1114. }
  1115. /* If this is false then we should never have sent the SCT extension in the
  1116. * ClientHello and thus this function should never have been called. */
  1117. assert(ssl->signed_cert_timestamps_enabled);
  1118. if (!ssl_is_sct_list_valid(contents)) {
  1119. *out_alert = SSL_AD_DECODE_ERROR;
  1120. return 0;
  1121. }
  1122. /* Session resumption uses the original session information. The extension
  1123. * should not be sent on resumption, but RFC 6962 did not make it a
  1124. * requirement, so tolerate this.
  1125. *
  1126. * TODO(davidben): Enforce this anyway. */
  1127. if (!ssl->s3->session_reused &&
  1128. !CBS_stow(contents, &hs->new_session->tlsext_signed_cert_timestamp_list,
  1129. &hs->new_session->tlsext_signed_cert_timestamp_list_length)) {
  1130. *out_alert = SSL_AD_INTERNAL_ERROR;
  1131. return 0;
  1132. }
  1133. return 1;
  1134. }
  1135. static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1136. CBS *contents) {
  1137. if (contents == NULL) {
  1138. return 1;
  1139. }
  1140. if (CBS_len(contents) != 0) {
  1141. return 0;
  1142. }
  1143. hs->scts_requested = 1;
  1144. return 1;
  1145. }
  1146. static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1147. SSL *const ssl = hs->ssl;
  1148. /* The extension shouldn't be sent when resuming sessions. */
  1149. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
  1150. ssl->s3->session_reused ||
  1151. ssl->cert->signed_cert_timestamp_list == NULL) {
  1152. return 1;
  1153. }
  1154. CBB contents;
  1155. return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
  1156. CBB_add_u16_length_prefixed(out, &contents) &&
  1157. CBB_add_bytes(
  1158. &contents,
  1159. CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
  1160. CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
  1161. CBB_flush(out);
  1162. }
  1163. /* Application-level Protocol Negotiation.
  1164. *
  1165. * https://tools.ietf.org/html/rfc7301 */
  1166. static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1167. SSL *const ssl = hs->ssl;
  1168. if (ssl->alpn_client_proto_list == NULL ||
  1169. ssl->s3->initial_handshake_complete) {
  1170. return 1;
  1171. }
  1172. CBB contents, proto_list;
  1173. if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
  1174. !CBB_add_u16_length_prefixed(out, &contents) ||
  1175. !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
  1176. !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
  1177. ssl->alpn_client_proto_list_len) ||
  1178. !CBB_flush(out)) {
  1179. return 0;
  1180. }
  1181. return 1;
  1182. }
  1183. static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1184. CBS *contents) {
  1185. SSL *const ssl = hs->ssl;
  1186. if (contents == NULL) {
  1187. return 1;
  1188. }
  1189. assert(!ssl->s3->initial_handshake_complete);
  1190. assert(ssl->alpn_client_proto_list != NULL);
  1191. if (hs->next_proto_neg_seen) {
  1192. /* NPN and ALPN may not be negotiated in the same connection. */
  1193. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1194. OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
  1195. return 0;
  1196. }
  1197. /* The extension data consists of a ProtocolNameList which must have
  1198. * exactly one ProtocolName. Each of these is length-prefixed. */
  1199. CBS protocol_name_list, protocol_name;
  1200. if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
  1201. CBS_len(contents) != 0 ||
  1202. !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
  1203. /* Empty protocol names are forbidden. */
  1204. CBS_len(&protocol_name) == 0 ||
  1205. CBS_len(&protocol_name_list) != 0) {
  1206. return 0;
  1207. }
  1208. if (!ssl->ctx->allow_unknown_alpn_protos) {
  1209. /* Check that the protocol name is one of the ones we advertised. */
  1210. int protocol_ok = 0;
  1211. CBS client_protocol_name_list, client_protocol_name;
  1212. CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
  1213. ssl->alpn_client_proto_list_len);
  1214. while (CBS_len(&client_protocol_name_list) > 0) {
  1215. if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
  1216. &client_protocol_name)) {
  1217. *out_alert = SSL_AD_INTERNAL_ERROR;
  1218. return 0;
  1219. }
  1220. if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
  1221. OPENSSL_memcmp(CBS_data(&client_protocol_name),
  1222. CBS_data(&protocol_name),
  1223. CBS_len(&protocol_name)) == 0) {
  1224. protocol_ok = 1;
  1225. break;
  1226. }
  1227. }
  1228. if (!protocol_ok) {
  1229. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
  1230. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1231. return 0;
  1232. }
  1233. }
  1234. if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
  1235. &ssl->s3->alpn_selected_len)) {
  1236. *out_alert = SSL_AD_INTERNAL_ERROR;
  1237. return 0;
  1238. }
  1239. return 1;
  1240. }
  1241. int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1242. const SSL_CLIENT_HELLO *client_hello) {
  1243. SSL *const ssl = hs->ssl;
  1244. CBS contents;
  1245. if (ssl->ctx->alpn_select_cb == NULL ||
  1246. !ssl_client_hello_get_extension(
  1247. client_hello, &contents,
  1248. TLSEXT_TYPE_application_layer_protocol_negotiation)) {
  1249. /* Ignore ALPN if not configured or no extension was supplied. */
  1250. return 1;
  1251. }
  1252. /* ALPN takes precedence over NPN. */
  1253. hs->next_proto_neg_seen = 0;
  1254. CBS protocol_name_list;
  1255. if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
  1256. CBS_len(&contents) != 0 ||
  1257. CBS_len(&protocol_name_list) < 2) {
  1258. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  1259. *out_alert = SSL_AD_DECODE_ERROR;
  1260. return 0;
  1261. }
  1262. /* Validate the protocol list. */
  1263. CBS protocol_name_list_copy = protocol_name_list;
  1264. while (CBS_len(&protocol_name_list_copy) > 0) {
  1265. CBS protocol_name;
  1266. if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
  1267. /* Empty protocol names are forbidden. */
  1268. CBS_len(&protocol_name) == 0) {
  1269. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  1270. *out_alert = SSL_AD_DECODE_ERROR;
  1271. return 0;
  1272. }
  1273. }
  1274. const uint8_t *selected;
  1275. uint8_t selected_len;
  1276. if (ssl->ctx->alpn_select_cb(
  1277. ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
  1278. CBS_len(&protocol_name_list),
  1279. ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
  1280. OPENSSL_free(ssl->s3->alpn_selected);
  1281. ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
  1282. if (ssl->s3->alpn_selected == NULL) {
  1283. *out_alert = SSL_AD_INTERNAL_ERROR;
  1284. return 0;
  1285. }
  1286. ssl->s3->alpn_selected_len = selected_len;
  1287. }
  1288. return 1;
  1289. }
  1290. static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1291. SSL *const ssl = hs->ssl;
  1292. if (ssl->s3->alpn_selected == NULL) {
  1293. return 1;
  1294. }
  1295. CBB contents, proto_list, proto;
  1296. if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
  1297. !CBB_add_u16_length_prefixed(out, &contents) ||
  1298. !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
  1299. !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
  1300. !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
  1301. ssl->s3->alpn_selected_len) ||
  1302. !CBB_flush(out)) {
  1303. return 0;
  1304. }
  1305. return 1;
  1306. }
  1307. /* Channel ID.
  1308. *
  1309. * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
  1310. static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
  1311. hs->ssl->s3->tlsext_channel_id_valid = 0;
  1312. }
  1313. static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1314. SSL *const ssl = hs->ssl;
  1315. if (!ssl->tlsext_channel_id_enabled ||
  1316. SSL_is_dtls(ssl)) {
  1317. return 1;
  1318. }
  1319. if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
  1320. !CBB_add_u16(out, 0 /* length */)) {
  1321. return 0;
  1322. }
  1323. return 1;
  1324. }
  1325. static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
  1326. uint8_t *out_alert, CBS *contents) {
  1327. SSL *const ssl = hs->ssl;
  1328. if (contents == NULL) {
  1329. return 1;
  1330. }
  1331. assert(!SSL_is_dtls(ssl));
  1332. assert(ssl->tlsext_channel_id_enabled);
  1333. if (CBS_len(contents) != 0) {
  1334. return 0;
  1335. }
  1336. ssl->s3->tlsext_channel_id_valid = 1;
  1337. return 1;
  1338. }
  1339. static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
  1340. uint8_t *out_alert, CBS *contents) {
  1341. SSL *const ssl = hs->ssl;
  1342. if (contents == NULL ||
  1343. !ssl->tlsext_channel_id_enabled ||
  1344. SSL_is_dtls(ssl)) {
  1345. return 1;
  1346. }
  1347. if (CBS_len(contents) != 0) {
  1348. return 0;
  1349. }
  1350. ssl->s3->tlsext_channel_id_valid = 1;
  1351. return 1;
  1352. }
  1353. static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1354. SSL *const ssl = hs->ssl;
  1355. if (!ssl->s3->tlsext_channel_id_valid) {
  1356. return 1;
  1357. }
  1358. if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
  1359. !CBB_add_u16(out, 0 /* length */)) {
  1360. return 0;
  1361. }
  1362. return 1;
  1363. }
  1364. /* Secure Real-time Transport Protocol (SRTP) extension.
  1365. *
  1366. * https://tools.ietf.org/html/rfc5764 */
  1367. static void ext_srtp_init(SSL_HANDSHAKE *hs) {
  1368. hs->ssl->srtp_profile = NULL;
  1369. }
  1370. static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1371. SSL *const ssl = hs->ssl;
  1372. STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
  1373. if (profiles == NULL) {
  1374. return 1;
  1375. }
  1376. const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
  1377. if (num_profiles == 0) {
  1378. return 1;
  1379. }
  1380. CBB contents, profile_ids;
  1381. if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
  1382. !CBB_add_u16_length_prefixed(out, &contents) ||
  1383. !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
  1384. return 0;
  1385. }
  1386. for (size_t i = 0; i < num_profiles; i++) {
  1387. if (!CBB_add_u16(&profile_ids,
  1388. sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
  1389. return 0;
  1390. }
  1391. }
  1392. if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
  1393. !CBB_flush(out)) {
  1394. return 0;
  1395. }
  1396. return 1;
  1397. }
  1398. static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1399. CBS *contents) {
  1400. SSL *const ssl = hs->ssl;
  1401. if (contents == NULL) {
  1402. return 1;
  1403. }
  1404. /* The extension consists of a u16-prefixed profile ID list containing a
  1405. * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
  1406. *
  1407. * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
  1408. CBS profile_ids, srtp_mki;
  1409. uint16_t profile_id;
  1410. if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
  1411. !CBS_get_u16(&profile_ids, &profile_id) ||
  1412. CBS_len(&profile_ids) != 0 ||
  1413. !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
  1414. CBS_len(contents) != 0) {
  1415. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1416. return 0;
  1417. }
  1418. if (CBS_len(&srtp_mki) != 0) {
  1419. /* Must be no MKI, since we never offer one. */
  1420. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
  1421. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1422. return 0;
  1423. }
  1424. STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
  1425. /* Check to see if the server gave us something we support (and presumably
  1426. * offered). */
  1427. for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
  1428. const SRTP_PROTECTION_PROFILE *profile =
  1429. sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
  1430. if (profile->id == profile_id) {
  1431. ssl->srtp_profile = profile;
  1432. return 1;
  1433. }
  1434. }
  1435. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1436. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1437. return 0;
  1438. }
  1439. static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1440. CBS *contents) {
  1441. SSL *const ssl = hs->ssl;
  1442. if (contents == NULL) {
  1443. return 1;
  1444. }
  1445. CBS profile_ids, srtp_mki;
  1446. if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
  1447. CBS_len(&profile_ids) < 2 ||
  1448. !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
  1449. CBS_len(contents) != 0) {
  1450. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1451. return 0;
  1452. }
  1453. /* Discard the MKI value for now. */
  1454. const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
  1455. SSL_get_srtp_profiles(ssl);
  1456. /* Pick the server's most preferred profile. */
  1457. for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
  1458. const SRTP_PROTECTION_PROFILE *server_profile =
  1459. sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
  1460. CBS profile_ids_tmp;
  1461. CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
  1462. while (CBS_len(&profile_ids_tmp) > 0) {
  1463. uint16_t profile_id;
  1464. if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
  1465. return 0;
  1466. }
  1467. if (server_profile->id == profile_id) {
  1468. ssl->srtp_profile = server_profile;
  1469. return 1;
  1470. }
  1471. }
  1472. }
  1473. return 1;
  1474. }
  1475. static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1476. SSL *const ssl = hs->ssl;
  1477. if (ssl->srtp_profile == NULL) {
  1478. return 1;
  1479. }
  1480. CBB contents, profile_ids;
  1481. if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
  1482. !CBB_add_u16_length_prefixed(out, &contents) ||
  1483. !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
  1484. !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
  1485. !CBB_add_u8(&contents, 0 /* empty MKI */) ||
  1486. !CBB_flush(out)) {
  1487. return 0;
  1488. }
  1489. return 1;
  1490. }
  1491. /* EC point formats.
  1492. *
  1493. * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
  1494. static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
  1495. CBB contents, formats;
  1496. if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
  1497. !CBB_add_u16_length_prefixed(out, &contents) ||
  1498. !CBB_add_u8_length_prefixed(&contents, &formats) ||
  1499. !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
  1500. !CBB_flush(out)) {
  1501. return 0;
  1502. }
  1503. return 1;
  1504. }
  1505. static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1506. /* The point format extension is unneccessary in TLS 1.3. */
  1507. if (hs->min_version >= TLS1_3_VERSION) {
  1508. return 1;
  1509. }
  1510. return ext_ec_point_add_extension(hs, out);
  1511. }
  1512. static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1513. CBS *contents) {
  1514. if (contents == NULL) {
  1515. return 1;
  1516. }
  1517. if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
  1518. return 0;
  1519. }
  1520. CBS ec_point_format_list;
  1521. if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
  1522. CBS_len(contents) != 0) {
  1523. return 0;
  1524. }
  1525. /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
  1526. * point format. */
  1527. if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
  1528. TLSEXT_ECPOINTFORMAT_uncompressed,
  1529. CBS_len(&ec_point_format_list)) == NULL) {
  1530. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1531. return 0;
  1532. }
  1533. return 1;
  1534. }
  1535. static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1536. CBS *contents) {
  1537. if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
  1538. return 1;
  1539. }
  1540. return ext_ec_point_parse_serverhello(hs, out_alert, contents);
  1541. }
  1542. static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1543. SSL *const ssl = hs->ssl;
  1544. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1545. return 1;
  1546. }
  1547. const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  1548. const uint32_t alg_a = hs->new_cipher->algorithm_auth;
  1549. const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
  1550. if (!using_ecc) {
  1551. return 1;
  1552. }
  1553. return ext_ec_point_add_extension(hs, out);
  1554. }
  1555. /* Pre Shared Key
  1556. *
  1557. * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
  1558. static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
  1559. SSL *const ssl = hs->ssl;
  1560. if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
  1561. SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
  1562. return 0;
  1563. }
  1564. size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
  1565. return 15 + ssl->session->tlsext_ticklen + binder_len;
  1566. }
  1567. static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1568. SSL *const ssl = hs->ssl;
  1569. if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
  1570. SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
  1571. return 1;
  1572. }
  1573. struct OPENSSL_timeval now;
  1574. ssl_get_current_time(ssl, &now);
  1575. uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
  1576. uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
  1577. /* Fill in a placeholder zero binder of the appropriate length. It will be
  1578. * computed and filled in later after length prefixes are computed. */
  1579. uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
  1580. size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
  1581. CBB contents, identity, ticket, binders, binder;
  1582. if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
  1583. !CBB_add_u16_length_prefixed(out, &contents) ||
  1584. !CBB_add_u16_length_prefixed(&contents, &identity) ||
  1585. !CBB_add_u16_length_prefixed(&identity, &ticket) ||
  1586. !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
  1587. ssl->session->tlsext_ticklen) ||
  1588. !CBB_add_u32(&identity, obfuscated_ticket_age) ||
  1589. !CBB_add_u16_length_prefixed(&contents, &binders) ||
  1590. !CBB_add_u8_length_prefixed(&binders, &binder) ||
  1591. !CBB_add_bytes(&binder, zero_binder, binder_len)) {
  1592. return 0;
  1593. }
  1594. hs->needs_psk_binder = 1;
  1595. return CBB_flush(out);
  1596. }
  1597. int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
  1598. uint8_t *out_alert,
  1599. CBS *contents) {
  1600. uint16_t psk_id;
  1601. if (!CBS_get_u16(contents, &psk_id) ||
  1602. CBS_len(contents) != 0) {
  1603. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1604. *out_alert = SSL_AD_DECODE_ERROR;
  1605. return 0;
  1606. }
  1607. /* We only advertise one PSK identity, so the only legal index is zero. */
  1608. if (psk_id != 0) {
  1609. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1610. *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
  1611. return 0;
  1612. }
  1613. return 1;
  1614. }
  1615. int ssl_ext_pre_shared_key_parse_clienthello(
  1616. SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
  1617. uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
  1618. /* We only process the first PSK identity since we don't support pure PSK. */
  1619. CBS identities, binders;
  1620. if (!CBS_get_u16_length_prefixed(contents, &identities) ||
  1621. !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
  1622. !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
  1623. !CBS_get_u16_length_prefixed(contents, &binders) ||
  1624. CBS_len(&binders) == 0 ||
  1625. CBS_len(contents) != 0) {
  1626. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1627. *out_alert = SSL_AD_DECODE_ERROR;
  1628. return 0;
  1629. }
  1630. *out_binders = binders;
  1631. /* Check the syntax of the remaining identities, but do not process them. */
  1632. size_t num_identities = 1;
  1633. while (CBS_len(&identities) != 0) {
  1634. CBS unused_ticket;
  1635. uint32_t unused_obfuscated_ticket_age;
  1636. if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
  1637. !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
  1638. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1639. *out_alert = SSL_AD_DECODE_ERROR;
  1640. return 0;
  1641. }
  1642. num_identities++;
  1643. }
  1644. /* Check the syntax of the binders. The value will be checked later if
  1645. * resuming. */
  1646. size_t num_binders = 0;
  1647. while (CBS_len(&binders) != 0) {
  1648. CBS binder;
  1649. if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
  1650. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1651. *out_alert = SSL_AD_DECODE_ERROR;
  1652. return 0;
  1653. }
  1654. num_binders++;
  1655. }
  1656. if (num_identities != num_binders) {
  1657. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
  1658. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1659. return 0;
  1660. }
  1661. return 1;
  1662. }
  1663. int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1664. if (!hs->ssl->s3->session_reused) {
  1665. return 1;
  1666. }
  1667. CBB contents;
  1668. if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
  1669. !CBB_add_u16_length_prefixed(out, &contents) ||
  1670. /* We only consider the first identity for resumption */
  1671. !CBB_add_u16(&contents, 0) ||
  1672. !CBB_flush(out)) {
  1673. return 0;
  1674. }
  1675. return 1;
  1676. }
  1677. /* Pre-Shared Key Exchange Modes
  1678. *
  1679. * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
  1680. static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
  1681. CBB *out) {
  1682. if (hs->max_version < TLS1_3_VERSION) {
  1683. return 1;
  1684. }
  1685. CBB contents, ke_modes;
  1686. if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
  1687. !CBB_add_u16_length_prefixed(out, &contents) ||
  1688. !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
  1689. !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
  1690. return 0;
  1691. }
  1692. return CBB_flush(out);
  1693. }
  1694. static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
  1695. uint8_t *out_alert,
  1696. CBS *contents) {
  1697. if (contents == NULL) {
  1698. return 1;
  1699. }
  1700. CBS ke_modes;
  1701. if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
  1702. CBS_len(&ke_modes) == 0 ||
  1703. CBS_len(contents) != 0) {
  1704. *out_alert = SSL_AD_DECODE_ERROR;
  1705. return 0;
  1706. }
  1707. /* We only support tickets with PSK_DHE_KE. */
  1708. hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
  1709. CBS_len(&ke_modes)) != NULL;
  1710. return 1;
  1711. }
  1712. /* Early Data Indication
  1713. *
  1714. * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
  1715. static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1716. SSL *const ssl = hs->ssl;
  1717. if (ssl->session == NULL ||
  1718. SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION ||
  1719. ssl->session->ticket_max_early_data == 0 ||
  1720. hs->received_hello_retry_request ||
  1721. !ssl->cert->enable_early_data) {
  1722. return 1;
  1723. }
  1724. hs->early_data_offered = 1;
  1725. if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
  1726. !CBB_add_u16(out, 0) ||
  1727. !CBB_flush(out)) {
  1728. return 0;
  1729. }
  1730. return 1;
  1731. }
  1732. static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
  1733. uint8_t *out_alert, CBS *contents) {
  1734. SSL *const ssl = hs->ssl;
  1735. if (contents == NULL) {
  1736. return 1;
  1737. }
  1738. if (CBS_len(contents) != 0) {
  1739. *out_alert = SSL_AD_DECODE_ERROR;
  1740. return 0;
  1741. }
  1742. if (!ssl->s3->session_reused) {
  1743. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  1744. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  1745. return 0;
  1746. }
  1747. ssl->early_data_accepted = 1;
  1748. return 1;
  1749. }
  1750. static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
  1751. uint8_t *out_alert, CBS *contents) {
  1752. SSL *const ssl = hs->ssl;
  1753. if (contents == NULL ||
  1754. ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  1755. return 1;
  1756. }
  1757. if (CBS_len(contents) != 0) {
  1758. *out_alert = SSL_AD_DECODE_ERROR;
  1759. return 0;
  1760. }
  1761. hs->early_data_offered = 1;
  1762. return 1;
  1763. }
  1764. static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1765. if (!hs->ssl->early_data_accepted) {
  1766. return 1;
  1767. }
  1768. if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
  1769. !CBB_add_u16(out, 0) ||
  1770. !CBB_flush(out)) {
  1771. return 0;
  1772. }
  1773. return 1;
  1774. }
  1775. /* Key Share
  1776. *
  1777. * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
  1778. static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1779. SSL *const ssl = hs->ssl;
  1780. if (hs->max_version < TLS1_3_VERSION) {
  1781. return 1;
  1782. }
  1783. CBB contents, kse_bytes;
  1784. if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
  1785. !CBB_add_u16_length_prefixed(out, &contents) ||
  1786. !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
  1787. return 0;
  1788. }
  1789. uint16_t group_id = hs->retry_group;
  1790. if (hs->received_hello_retry_request) {
  1791. /* We received a HelloRetryRequest without a new curve, so there is no new
  1792. * share to append. Leave |ecdh_ctx| as-is. */
  1793. if (group_id == 0 &&
  1794. !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
  1795. hs->key_share_bytes_len)) {
  1796. return 0;
  1797. }
  1798. OPENSSL_free(hs->key_share_bytes);
  1799. hs->key_share_bytes = NULL;
  1800. hs->key_share_bytes_len = 0;
  1801. if (group_id == 0) {
  1802. return CBB_flush(out);
  1803. }
  1804. } else {
  1805. /* Add a fake group. See draft-davidben-tls-grease-01. */
  1806. if (ssl->ctx->grease_enabled &&
  1807. (!CBB_add_u16(&kse_bytes,
  1808. ssl_get_grease_value(ssl, ssl_grease_group)) ||
  1809. !CBB_add_u16(&kse_bytes, 1 /* length */) ||
  1810. !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
  1811. return 0;
  1812. }
  1813. /* Predict the most preferred group. */
  1814. const uint16_t *groups;
  1815. size_t groups_len;
  1816. tls1_get_grouplist(ssl, &groups, &groups_len);
  1817. if (groups_len == 0) {
  1818. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
  1819. return 0;
  1820. }
  1821. group_id = groups[0];
  1822. }
  1823. CBB key_exchange;
  1824. if (!CBB_add_u16(&kse_bytes, group_id) ||
  1825. !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
  1826. !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
  1827. !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
  1828. !CBB_flush(&kse_bytes)) {
  1829. return 0;
  1830. }
  1831. if (!hs->received_hello_retry_request) {
  1832. /* Save the contents of the extension to repeat it in the second
  1833. * ClientHello. */
  1834. hs->key_share_bytes_len = CBB_len(&kse_bytes);
  1835. hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
  1836. if (hs->key_share_bytes == NULL) {
  1837. return 0;
  1838. }
  1839. }
  1840. return CBB_flush(out);
  1841. }
  1842. int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
  1843. size_t *out_secret_len,
  1844. uint8_t *out_alert, CBS *contents) {
  1845. CBS peer_key;
  1846. uint16_t group_id;
  1847. if (!CBS_get_u16(contents, &group_id) ||
  1848. !CBS_get_u16_length_prefixed(contents, &peer_key) ||
  1849. CBS_len(contents) != 0) {
  1850. *out_alert = SSL_AD_DECODE_ERROR;
  1851. return 0;
  1852. }
  1853. if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
  1854. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1855. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  1856. return 0;
  1857. }
  1858. if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
  1859. CBS_data(&peer_key), CBS_len(&peer_key))) {
  1860. *out_alert = SSL_AD_INTERNAL_ERROR;
  1861. return 0;
  1862. }
  1863. hs->new_session->group_id = group_id;
  1864. SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
  1865. return 1;
  1866. }
  1867. int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
  1868. uint8_t **out_secret,
  1869. size_t *out_secret_len,
  1870. uint8_t *out_alert, CBS *contents) {
  1871. uint16_t group_id;
  1872. CBS key_shares;
  1873. if (!tls1_get_shared_group(hs, &group_id)) {
  1874. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
  1875. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  1876. return 0;
  1877. }
  1878. if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
  1879. CBS_len(contents) != 0) {
  1880. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1881. return 0;
  1882. }
  1883. /* Find the corresponding key share. */
  1884. int found = 0;
  1885. CBS peer_key;
  1886. while (CBS_len(&key_shares) > 0) {
  1887. uint16_t id;
  1888. CBS peer_key_tmp;
  1889. if (!CBS_get_u16(&key_shares, &id) ||
  1890. !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
  1891. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1892. return 0;
  1893. }
  1894. if (id == group_id) {
  1895. if (found) {
  1896. OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
  1897. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1898. return 0;
  1899. }
  1900. found = 1;
  1901. peer_key = peer_key_tmp;
  1902. /* Continue parsing the structure to keep peers honest. */
  1903. }
  1904. }
  1905. if (!found) {
  1906. *out_found = 0;
  1907. *out_secret = NULL;
  1908. *out_secret_len = 0;
  1909. return 1;
  1910. }
  1911. /* Compute the DH secret. */
  1912. uint8_t *secret = NULL;
  1913. size_t secret_len;
  1914. SSL_ECDH_CTX group;
  1915. OPENSSL_memset(&group, 0, sizeof(SSL_ECDH_CTX));
  1916. CBB public_key;
  1917. if (!CBB_init(&public_key, 32) ||
  1918. !SSL_ECDH_CTX_init(&group, group_id) ||
  1919. !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
  1920. CBS_data(&peer_key), CBS_len(&peer_key)) ||
  1921. !CBB_finish(&public_key, &hs->ecdh_public_key,
  1922. &hs->ecdh_public_key_len)) {
  1923. OPENSSL_free(secret);
  1924. SSL_ECDH_CTX_cleanup(&group);
  1925. CBB_cleanup(&public_key);
  1926. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1927. return 0;
  1928. }
  1929. SSL_ECDH_CTX_cleanup(&group);
  1930. *out_secret = secret;
  1931. *out_secret_len = secret_len;
  1932. *out_found = 1;
  1933. return 1;
  1934. }
  1935. int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1936. uint16_t group_id;
  1937. CBB kse_bytes, public_key;
  1938. if (!tls1_get_shared_group(hs, &group_id) ||
  1939. !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
  1940. !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
  1941. !CBB_add_u16(&kse_bytes, group_id) ||
  1942. !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
  1943. !CBB_add_bytes(&public_key, hs->ecdh_public_key,
  1944. hs->ecdh_public_key_len) ||
  1945. !CBB_flush(out)) {
  1946. return 0;
  1947. }
  1948. OPENSSL_free(hs->ecdh_public_key);
  1949. hs->ecdh_public_key = NULL;
  1950. hs->ecdh_public_key_len = 0;
  1951. hs->new_session->group_id = group_id;
  1952. return 1;
  1953. }
  1954. /* Supported Versions
  1955. *
  1956. * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
  1957. static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1958. SSL *const ssl = hs->ssl;
  1959. if (hs->max_version <= TLS1_2_VERSION) {
  1960. return 1;
  1961. }
  1962. CBB contents, versions;
  1963. if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
  1964. !CBB_add_u16_length_prefixed(out, &contents) ||
  1965. !CBB_add_u8_length_prefixed(&contents, &versions)) {
  1966. return 0;
  1967. }
  1968. /* Add a fake version. See draft-davidben-tls-grease-01. */
  1969. if (ssl->ctx->grease_enabled &&
  1970. !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
  1971. return 0;
  1972. }
  1973. if (!ssl_add_supported_versions(hs, &versions) ||
  1974. !CBB_flush(out)) {
  1975. return 0;
  1976. }
  1977. return 1;
  1978. }
  1979. /* Cookie
  1980. *
  1981. * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
  1982. static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1983. if (hs->cookie == NULL) {
  1984. return 1;
  1985. }
  1986. CBB contents, cookie;
  1987. if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
  1988. !CBB_add_u16_length_prefixed(out, &contents) ||
  1989. !CBB_add_u16_length_prefixed(&contents, &cookie) ||
  1990. !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
  1991. !CBB_flush(out)) {
  1992. return 0;
  1993. }
  1994. /* The cookie is no longer needed in memory. */
  1995. OPENSSL_free(hs->cookie);
  1996. hs->cookie = NULL;
  1997. hs->cookie_len = 0;
  1998. return 1;
  1999. }
  2000. /* Negotiated Groups
  2001. *
  2002. * https://tools.ietf.org/html/rfc4492#section-5.1.2
  2003. * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
  2004. static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  2005. SSL *const ssl = hs->ssl;
  2006. CBB contents, groups_bytes;
  2007. if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
  2008. !CBB_add_u16_length_prefixed(out, &contents) ||
  2009. !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
  2010. return 0;
  2011. }
  2012. /* Add a fake group. See draft-davidben-tls-grease-01. */
  2013. if (ssl->ctx->grease_enabled &&
  2014. !CBB_add_u16(&groups_bytes,
  2015. ssl_get_grease_value(ssl, ssl_grease_group))) {
  2016. return 0;
  2017. }
  2018. const uint16_t *groups;
  2019. size_t groups_len;
  2020. tls1_get_grouplist(ssl, &groups, &groups_len);
  2021. for (size_t i = 0; i < groups_len; i++) {
  2022. if (!CBB_add_u16(&groups_bytes, groups[i])) {
  2023. return 0;
  2024. }
  2025. }
  2026. return CBB_flush(out);
  2027. }
  2028. static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
  2029. uint8_t *out_alert,
  2030. CBS *contents) {
  2031. /* This extension is not expected to be echoed by servers in TLS 1.2, but some
  2032. * BigIP servers send it nonetheless, so do not enforce this. */
  2033. return 1;
  2034. }
  2035. static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
  2036. uint8_t *out_alert,
  2037. CBS *contents) {
  2038. if (contents == NULL) {
  2039. return 1;
  2040. }
  2041. CBS supported_group_list;
  2042. if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
  2043. CBS_len(&supported_group_list) == 0 ||
  2044. (CBS_len(&supported_group_list) & 1) != 0 ||
  2045. CBS_len(contents) != 0) {
  2046. return 0;
  2047. }
  2048. hs->peer_supported_group_list =
  2049. OPENSSL_malloc(CBS_len(&supported_group_list));
  2050. if (hs->peer_supported_group_list == NULL) {
  2051. *out_alert = SSL_AD_INTERNAL_ERROR;
  2052. return 0;
  2053. }
  2054. const size_t num_groups = CBS_len(&supported_group_list) / 2;
  2055. for (size_t i = 0; i < num_groups; i++) {
  2056. if (!CBS_get_u16(&supported_group_list,
  2057. &hs->peer_supported_group_list[i])) {
  2058. goto err;
  2059. }
  2060. }
  2061. assert(CBS_len(&supported_group_list) == 0);
  2062. hs->peer_supported_group_list_len = num_groups;
  2063. return 1;
  2064. err:
  2065. OPENSSL_free(hs->peer_supported_group_list);
  2066. hs->peer_supported_group_list = NULL;
  2067. *out_alert = SSL_AD_INTERNAL_ERROR;
  2068. return 0;
  2069. }
  2070. static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  2071. /* Servers don't echo this extension. */
  2072. return 1;
  2073. }
  2074. /* kExtensions contains all the supported extensions. */
  2075. static const struct tls_extension kExtensions[] = {
  2076. {
  2077. TLSEXT_TYPE_renegotiate,
  2078. NULL,
  2079. ext_ri_add_clienthello,
  2080. ext_ri_parse_serverhello,
  2081. ext_ri_parse_clienthello,
  2082. ext_ri_add_serverhello,
  2083. },
  2084. {
  2085. TLSEXT_TYPE_server_name,
  2086. NULL,
  2087. ext_sni_add_clienthello,
  2088. ext_sni_parse_serverhello,
  2089. ext_sni_parse_clienthello,
  2090. ext_sni_add_serverhello,
  2091. },
  2092. {
  2093. TLSEXT_TYPE_extended_master_secret,
  2094. NULL,
  2095. ext_ems_add_clienthello,
  2096. ext_ems_parse_serverhello,
  2097. ext_ems_parse_clienthello,
  2098. ext_ems_add_serverhello,
  2099. },
  2100. {
  2101. TLSEXT_TYPE_session_ticket,
  2102. NULL,
  2103. ext_ticket_add_clienthello,
  2104. ext_ticket_parse_serverhello,
  2105. /* Ticket extension client parsing is handled in ssl_session.c */
  2106. ignore_parse_clienthello,
  2107. ext_ticket_add_serverhello,
  2108. },
  2109. {
  2110. TLSEXT_TYPE_signature_algorithms,
  2111. NULL,
  2112. ext_sigalgs_add_clienthello,
  2113. forbid_parse_serverhello,
  2114. ext_sigalgs_parse_clienthello,
  2115. dont_add_serverhello,
  2116. },
  2117. {
  2118. TLSEXT_TYPE_status_request,
  2119. NULL,
  2120. ext_ocsp_add_clienthello,
  2121. ext_ocsp_parse_serverhello,
  2122. ext_ocsp_parse_clienthello,
  2123. ext_ocsp_add_serverhello,
  2124. },
  2125. {
  2126. TLSEXT_TYPE_next_proto_neg,
  2127. NULL,
  2128. ext_npn_add_clienthello,
  2129. ext_npn_parse_serverhello,
  2130. ext_npn_parse_clienthello,
  2131. ext_npn_add_serverhello,
  2132. },
  2133. {
  2134. TLSEXT_TYPE_certificate_timestamp,
  2135. NULL,
  2136. ext_sct_add_clienthello,
  2137. ext_sct_parse_serverhello,
  2138. ext_sct_parse_clienthello,
  2139. ext_sct_add_serverhello,
  2140. },
  2141. {
  2142. TLSEXT_TYPE_application_layer_protocol_negotiation,
  2143. NULL,
  2144. ext_alpn_add_clienthello,
  2145. ext_alpn_parse_serverhello,
  2146. /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
  2147. ignore_parse_clienthello,
  2148. ext_alpn_add_serverhello,
  2149. },
  2150. {
  2151. TLSEXT_TYPE_channel_id,
  2152. ext_channel_id_init,
  2153. ext_channel_id_add_clienthello,
  2154. ext_channel_id_parse_serverhello,
  2155. ext_channel_id_parse_clienthello,
  2156. ext_channel_id_add_serverhello,
  2157. },
  2158. {
  2159. TLSEXT_TYPE_srtp,
  2160. ext_srtp_init,
  2161. ext_srtp_add_clienthello,
  2162. ext_srtp_parse_serverhello,
  2163. ext_srtp_parse_clienthello,
  2164. ext_srtp_add_serverhello,
  2165. },
  2166. {
  2167. TLSEXT_TYPE_ec_point_formats,
  2168. NULL,
  2169. ext_ec_point_add_clienthello,
  2170. ext_ec_point_parse_serverhello,
  2171. ext_ec_point_parse_clienthello,
  2172. ext_ec_point_add_serverhello,
  2173. },
  2174. {
  2175. TLSEXT_TYPE_key_share,
  2176. NULL,
  2177. ext_key_share_add_clienthello,
  2178. forbid_parse_serverhello,
  2179. ignore_parse_clienthello,
  2180. dont_add_serverhello,
  2181. },
  2182. {
  2183. TLSEXT_TYPE_psk_key_exchange_modes,
  2184. NULL,
  2185. ext_psk_key_exchange_modes_add_clienthello,
  2186. forbid_parse_serverhello,
  2187. ext_psk_key_exchange_modes_parse_clienthello,
  2188. dont_add_serverhello,
  2189. },
  2190. {
  2191. TLSEXT_TYPE_early_data,
  2192. NULL,
  2193. ext_early_data_add_clienthello,
  2194. ext_early_data_parse_serverhello,
  2195. ext_early_data_parse_clienthello,
  2196. ext_early_data_add_serverhello,
  2197. },
  2198. {
  2199. TLSEXT_TYPE_supported_versions,
  2200. NULL,
  2201. ext_supported_versions_add_clienthello,
  2202. forbid_parse_serverhello,
  2203. ignore_parse_clienthello,
  2204. dont_add_serverhello,
  2205. },
  2206. {
  2207. TLSEXT_TYPE_cookie,
  2208. NULL,
  2209. ext_cookie_add_clienthello,
  2210. forbid_parse_serverhello,
  2211. ignore_parse_clienthello,
  2212. dont_add_serverhello,
  2213. },
  2214. /* The final extension must be non-empty. WebSphere Application Server 7.0 is
  2215. * intolerant to the last extension being zero-length. See
  2216. * https://crbug.com/363583. */
  2217. {
  2218. TLSEXT_TYPE_supported_groups,
  2219. NULL,
  2220. ext_supported_groups_add_clienthello,
  2221. ext_supported_groups_parse_serverhello,
  2222. ext_supported_groups_parse_clienthello,
  2223. ext_supported_groups_add_serverhello,
  2224. },
  2225. };
  2226. #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
  2227. OPENSSL_COMPILE_ASSERT(kNumExtensions <=
  2228. sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
  2229. too_many_extensions_for_sent_bitset);
  2230. OPENSSL_COMPILE_ASSERT(
  2231. kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
  2232. too_many_extensions_for_received_bitset);
  2233. static const struct tls_extension *tls_extension_find(uint32_t *out_index,
  2234. uint16_t value) {
  2235. unsigned i;
  2236. for (i = 0; i < kNumExtensions; i++) {
  2237. if (kExtensions[i].value == value) {
  2238. *out_index = i;
  2239. return &kExtensions[i];
  2240. }
  2241. }
  2242. return NULL;
  2243. }
  2244. int SSL_extension_supported(unsigned extension_value) {
  2245. uint32_t index;
  2246. return extension_value == TLSEXT_TYPE_padding ||
  2247. tls_extension_find(&index, extension_value) != NULL;
  2248. }
  2249. int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
  2250. SSL *const ssl = hs->ssl;
  2251. /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
  2252. if (hs->client_version == SSL3_VERSION &&
  2253. !ssl->s3->send_connection_binding) {
  2254. return 1;
  2255. }
  2256. CBB extensions;
  2257. if (!CBB_add_u16_length_prefixed(out, &extensions)) {
  2258. goto err;
  2259. }
  2260. hs->extensions.sent = 0;
  2261. hs->custom_extensions.sent = 0;
  2262. for (size_t i = 0; i < kNumExtensions; i++) {
  2263. if (kExtensions[i].init != NULL) {
  2264. kExtensions[i].init(hs);
  2265. }
  2266. }
  2267. uint16_t grease_ext1 = 0;
  2268. if (ssl->ctx->grease_enabled) {
  2269. /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
  2270. grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
  2271. if (!CBB_add_u16(&extensions, grease_ext1) ||
  2272. !CBB_add_u16(&extensions, 0 /* zero length */)) {
  2273. goto err;
  2274. }
  2275. }
  2276. for (size_t i = 0; i < kNumExtensions; i++) {
  2277. const size_t len_before = CBB_len(&extensions);
  2278. if (!kExtensions[i].add_clienthello(hs, &extensions)) {
  2279. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
  2280. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2281. goto err;
  2282. }
  2283. if (CBB_len(&extensions) != len_before) {
  2284. hs->extensions.sent |= (1u << i);
  2285. }
  2286. }
  2287. if (!custom_ext_add_clienthello(hs, &extensions)) {
  2288. goto err;
  2289. }
  2290. if (ssl->ctx->grease_enabled) {
  2291. /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
  2292. uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
  2293. /* The two fake extensions must not have the same value. GREASE values are
  2294. * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
  2295. * one. */
  2296. if (grease_ext1 == grease_ext2) {
  2297. grease_ext2 ^= 0x1010;
  2298. }
  2299. if (!CBB_add_u16(&extensions, grease_ext2) ||
  2300. !CBB_add_u16(&extensions, 1 /* one byte length */) ||
  2301. !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
  2302. goto err;
  2303. }
  2304. }
  2305. if (!SSL_is_dtls(ssl)) {
  2306. size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
  2307. header_len += 2 + CBB_len(&extensions) + psk_extension_len;
  2308. if (header_len > 0xff && header_len < 0x200) {
  2309. /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
  2310. *
  2311. * NB: because this code works out the length of all existing extensions
  2312. * it MUST always appear last. */
  2313. size_t padding_len = 0x200 - header_len;
  2314. /* Extensions take at least four bytes to encode. Always include at least
  2315. * one byte of data if including the extension. WebSphere Application
  2316. * Server 7.0 is intolerant to the last extension being zero-length. See
  2317. * https://crbug.com/363583. */
  2318. if (padding_len >= 4 + 1) {
  2319. padding_len -= 4;
  2320. } else {
  2321. padding_len = 1;
  2322. }
  2323. uint8_t *padding_bytes;
  2324. if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
  2325. !CBB_add_u16(&extensions, padding_len) ||
  2326. !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
  2327. goto err;
  2328. }
  2329. OPENSSL_memset(padding_bytes, 0, padding_len);
  2330. }
  2331. }
  2332. /* The PSK extension must be last, including after the padding. */
  2333. if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
  2334. goto err;
  2335. }
  2336. /* Discard empty extensions blocks. */
  2337. if (CBB_len(&extensions) == 0) {
  2338. CBB_discard_child(out);
  2339. }
  2340. return CBB_flush(out);
  2341. err:
  2342. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2343. return 0;
  2344. }
  2345. int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
  2346. SSL *const ssl = hs->ssl;
  2347. CBB extensions;
  2348. if (!CBB_add_u16_length_prefixed(out, &extensions)) {
  2349. goto err;
  2350. }
  2351. for (unsigned i = 0; i < kNumExtensions; i++) {
  2352. if (!(hs->extensions.received & (1u << i))) {
  2353. /* Don't send extensions that were not received. */
  2354. continue;
  2355. }
  2356. if (!kExtensions[i].add_serverhello(hs, &extensions)) {
  2357. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
  2358. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2359. goto err;
  2360. }
  2361. }
  2362. if (!custom_ext_add_serverhello(hs, &extensions)) {
  2363. goto err;
  2364. }
  2365. /* Discard empty extensions blocks before TLS 1.3. */
  2366. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
  2367. CBB_len(&extensions) == 0) {
  2368. CBB_discard_child(out);
  2369. }
  2370. return CBB_flush(out);
  2371. err:
  2372. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2373. return 0;
  2374. }
  2375. static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
  2376. const SSL_CLIENT_HELLO *client_hello,
  2377. int *out_alert) {
  2378. SSL *const ssl = hs->ssl;
  2379. for (size_t i = 0; i < kNumExtensions; i++) {
  2380. if (kExtensions[i].init != NULL) {
  2381. kExtensions[i].init(hs);
  2382. }
  2383. }
  2384. hs->extensions.received = 0;
  2385. hs->custom_extensions.received = 0;
  2386. CBS extensions;
  2387. CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
  2388. while (CBS_len(&extensions) != 0) {
  2389. uint16_t type;
  2390. CBS extension;
  2391. /* Decode the next extension. */
  2392. if (!CBS_get_u16(&extensions, &type) ||
  2393. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  2394. *out_alert = SSL_AD_DECODE_ERROR;
  2395. return 0;
  2396. }
  2397. /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
  2398. * ambiguous. Ignore all but the renegotiation_info extension. */
  2399. if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
  2400. continue;
  2401. }
  2402. unsigned ext_index;
  2403. const struct tls_extension *const ext =
  2404. tls_extension_find(&ext_index, type);
  2405. if (ext == NULL) {
  2406. if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
  2407. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  2408. return 0;
  2409. }
  2410. continue;
  2411. }
  2412. hs->extensions.received |= (1u << ext_index);
  2413. uint8_t alert = SSL_AD_DECODE_ERROR;
  2414. if (!ext->parse_clienthello(hs, &alert, &extension)) {
  2415. *out_alert = alert;
  2416. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  2417. ERR_add_error_dataf("extension %u", (unsigned)type);
  2418. return 0;
  2419. }
  2420. }
  2421. for (size_t i = 0; i < kNumExtensions; i++) {
  2422. if (hs->extensions.received & (1u << i)) {
  2423. continue;
  2424. }
  2425. CBS *contents = NULL, fake_contents;
  2426. static const uint8_t kFakeRenegotiateExtension[] = {0};
  2427. if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
  2428. ssl_client_cipher_list_contains_cipher(client_hello,
  2429. SSL3_CK_SCSV & 0xffff)) {
  2430. /* The renegotiation SCSV was received so pretend that we received a
  2431. * renegotiation extension. */
  2432. CBS_init(&fake_contents, kFakeRenegotiateExtension,
  2433. sizeof(kFakeRenegotiateExtension));
  2434. contents = &fake_contents;
  2435. hs->extensions.received |= (1u << i);
  2436. }
  2437. /* Extension wasn't observed so call the callback with a NULL
  2438. * parameter. */
  2439. uint8_t alert = SSL_AD_DECODE_ERROR;
  2440. if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
  2441. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
  2442. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2443. *out_alert = alert;
  2444. return 0;
  2445. }
  2446. }
  2447. return 1;
  2448. }
  2449. int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
  2450. const SSL_CLIENT_HELLO *client_hello) {
  2451. SSL *const ssl = hs->ssl;
  2452. int alert = SSL_AD_DECODE_ERROR;
  2453. if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
  2454. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  2455. return 0;
  2456. }
  2457. if (ssl_check_clienthello_tlsext(hs) <= 0) {
  2458. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
  2459. return 0;
  2460. }
  2461. return 1;
  2462. }
  2463. static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
  2464. int *out_alert) {
  2465. SSL *const ssl = hs->ssl;
  2466. /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
  2467. if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  2468. return 1;
  2469. }
  2470. /* Decode the extensions block and check it is valid. */
  2471. CBS extensions;
  2472. if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
  2473. !tls1_check_duplicate_extensions(&extensions)) {
  2474. *out_alert = SSL_AD_DECODE_ERROR;
  2475. return 0;
  2476. }
  2477. uint32_t received = 0;
  2478. while (CBS_len(&extensions) != 0) {
  2479. uint16_t type;
  2480. CBS extension;
  2481. /* Decode the next extension. */
  2482. if (!CBS_get_u16(&extensions, &type) ||
  2483. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  2484. *out_alert = SSL_AD_DECODE_ERROR;
  2485. return 0;
  2486. }
  2487. unsigned ext_index;
  2488. const struct tls_extension *const ext =
  2489. tls_extension_find(&ext_index, type);
  2490. if (ext == NULL) {
  2491. if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
  2492. return 0;
  2493. }
  2494. continue;
  2495. }
  2496. OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
  2497. too_many_bits);
  2498. if (!(hs->extensions.sent & (1u << ext_index)) &&
  2499. type != TLSEXT_TYPE_renegotiate) {
  2500. /* If the extension was never sent then it is illegal, except for the
  2501. * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
  2502. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  2503. ERR_add_error_dataf("extension :%u", (unsigned)type);
  2504. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  2505. return 0;
  2506. }
  2507. received |= (1u << ext_index);
  2508. uint8_t alert = SSL_AD_DECODE_ERROR;
  2509. if (!ext->parse_serverhello(hs, &alert, &extension)) {
  2510. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  2511. ERR_add_error_dataf("extension %u", (unsigned)type);
  2512. *out_alert = alert;
  2513. return 0;
  2514. }
  2515. }
  2516. for (size_t i = 0; i < kNumExtensions; i++) {
  2517. if (!(received & (1u << i))) {
  2518. /* Extension wasn't observed so call the callback with a NULL
  2519. * parameter. */
  2520. uint8_t alert = SSL_AD_DECODE_ERROR;
  2521. if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
  2522. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
  2523. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2524. *out_alert = alert;
  2525. return 0;
  2526. }
  2527. }
  2528. }
  2529. return 1;
  2530. }
  2531. static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
  2532. SSL *const ssl = hs->ssl;
  2533. int ret = SSL_TLSEXT_ERR_NOACK;
  2534. int al = SSL_AD_UNRECOGNIZED_NAME;
  2535. if (ssl->ctx->tlsext_servername_callback != 0) {
  2536. ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
  2537. ssl->ctx->tlsext_servername_arg);
  2538. } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
  2539. ret = ssl->session_ctx->tlsext_servername_callback(
  2540. ssl, &al, ssl->session_ctx->tlsext_servername_arg);
  2541. }
  2542. switch (ret) {
  2543. case SSL_TLSEXT_ERR_ALERT_FATAL:
  2544. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  2545. return -1;
  2546. case SSL_TLSEXT_ERR_NOACK:
  2547. hs->should_ack_sni = 0;
  2548. return 1;
  2549. default:
  2550. return 1;
  2551. }
  2552. }
  2553. int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
  2554. SSL *const ssl = hs->ssl;
  2555. int alert = SSL_AD_DECODE_ERROR;
  2556. if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
  2557. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  2558. return 0;
  2559. }
  2560. return 1;
  2561. }
  2562. static enum ssl_ticket_aead_result_t
  2563. ssl_decrypt_ticket_with_cipher_ctx(SSL *ssl, uint8_t **out, size_t *out_len,
  2564. int *out_renew_ticket, const uint8_t *ticket,
  2565. size_t ticket_len) {
  2566. enum ssl_ticket_aead_result_t ret = ssl_ticket_aead_ignore_ticket;
  2567. const SSL_CTX *const ssl_ctx = ssl->session_ctx;
  2568. uint8_t *plaintext = NULL;
  2569. HMAC_CTX hmac_ctx;
  2570. HMAC_CTX_init(&hmac_ctx);
  2571. EVP_CIPHER_CTX cipher_ctx;
  2572. EVP_CIPHER_CTX_init(&cipher_ctx);
  2573. /* Ensure there is room for the key name and the largest IV
  2574. * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
  2575. * the maximum IV length should be well under the minimum size for the
  2576. * session material and HMAC. */
  2577. if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
  2578. goto out;
  2579. }
  2580. const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
  2581. if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
  2582. int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
  2583. ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
  2584. &hmac_ctx, 0 /* decrypt */);
  2585. if (cb_ret < 0) {
  2586. ret = ssl_ticket_aead_error;
  2587. goto out;
  2588. } else if (cb_ret == 0) {
  2589. goto out;
  2590. } else if (cb_ret == 2) {
  2591. *out_renew_ticket = 1;
  2592. }
  2593. } else {
  2594. /* Check the key name matches. */
  2595. if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
  2596. SSL_TICKET_KEY_NAME_LEN) != 0) {
  2597. goto out;
  2598. }
  2599. if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
  2600. sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
  2601. NULL) ||
  2602. !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
  2603. ssl_ctx->tlsext_tick_aes_key, iv)) {
  2604. ret = ssl_ticket_aead_error;
  2605. goto out;
  2606. }
  2607. }
  2608. size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
  2609. /* Check the MAC at the end of the ticket. */
  2610. uint8_t mac[EVP_MAX_MD_SIZE];
  2611. size_t mac_len = HMAC_size(&hmac_ctx);
  2612. if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
  2613. /* The ticket must be large enough for key name, IV, data, and MAC. */
  2614. goto out;
  2615. }
  2616. HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
  2617. HMAC_Final(&hmac_ctx, mac, NULL);
  2618. int mac_ok =
  2619. CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
  2620. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  2621. mac_ok = 1;
  2622. #endif
  2623. if (!mac_ok) {
  2624. goto out;
  2625. }
  2626. /* Decrypt the session data. */
  2627. const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
  2628. size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
  2629. mac_len;
  2630. plaintext = OPENSSL_malloc(ciphertext_len);
  2631. if (plaintext == NULL) {
  2632. ret = ssl_ticket_aead_error;
  2633. goto out;
  2634. }
  2635. size_t plaintext_len;
  2636. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  2637. OPENSSL_memcpy(plaintext, ciphertext, ciphertext_len);
  2638. plaintext_len = ciphertext_len;
  2639. #else
  2640. if (ciphertext_len >= INT_MAX) {
  2641. goto out;
  2642. }
  2643. int len1, len2;
  2644. if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
  2645. (int)ciphertext_len) ||
  2646. !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
  2647. ERR_clear_error();
  2648. goto out;
  2649. }
  2650. plaintext_len = (size_t)(len1) + len2;
  2651. #endif
  2652. *out = plaintext;
  2653. plaintext = NULL;
  2654. *out_len = plaintext_len;
  2655. ret = ssl_ticket_aead_success;
  2656. out:
  2657. OPENSSL_free(plaintext);
  2658. HMAC_CTX_cleanup(&hmac_ctx);
  2659. EVP_CIPHER_CTX_cleanup(&cipher_ctx);
  2660. return ret;
  2661. }
  2662. static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
  2663. SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
  2664. const uint8_t *ticket, size_t ticket_len) {
  2665. uint8_t *plaintext = OPENSSL_malloc(ticket_len);
  2666. if (plaintext == NULL) {
  2667. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  2668. return ssl_ticket_aead_error;
  2669. }
  2670. size_t plaintext_len;
  2671. const enum ssl_ticket_aead_result_t result =
  2672. ssl->session_ctx->ticket_aead_method->open(
  2673. ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
  2674. if (result == ssl_ticket_aead_success) {
  2675. *out = plaintext;
  2676. plaintext = NULL;
  2677. *out_len = plaintext_len;
  2678. }
  2679. OPENSSL_free(plaintext);
  2680. return result;
  2681. }
  2682. enum ssl_ticket_aead_result_t ssl_process_ticket(
  2683. SSL *ssl, SSL_SESSION **out_session, int *out_renew_ticket,
  2684. const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
  2685. size_t session_id_len) {
  2686. *out_renew_ticket = 0;
  2687. *out_session = NULL;
  2688. if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
  2689. session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  2690. return ssl_ticket_aead_ignore_ticket;
  2691. }
  2692. uint8_t *plaintext = NULL;
  2693. size_t plaintext_len;
  2694. enum ssl_ticket_aead_result_t result;
  2695. if (ssl->session_ctx->ticket_aead_method != NULL) {
  2696. result = ssl_decrypt_ticket_with_method(
  2697. ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
  2698. } else {
  2699. result = ssl_decrypt_ticket_with_cipher_ctx(
  2700. ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
  2701. }
  2702. if (result != ssl_ticket_aead_success) {
  2703. return result;
  2704. }
  2705. /* Decode the session. */
  2706. SSL_SESSION *session =
  2707. SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx);
  2708. OPENSSL_free(plaintext);
  2709. if (session == NULL) {
  2710. ERR_clear_error(); /* Don't leave an error on the queue. */
  2711. return ssl_ticket_aead_ignore_ticket;
  2712. }
  2713. /* Copy the client's session ID into the new session, to denote the ticket has
  2714. * been accepted. */
  2715. OPENSSL_memcpy(session->session_id, session_id, session_id_len);
  2716. session->session_id_length = session_id_len;
  2717. *out_session = session;
  2718. return ssl_ticket_aead_success;
  2719. }
  2720. int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
  2721. /* Extension ignored for inappropriate versions */
  2722. if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
  2723. return 1;
  2724. }
  2725. OPENSSL_free(hs->peer_sigalgs);
  2726. hs->peer_sigalgs = NULL;
  2727. hs->num_peer_sigalgs = 0;
  2728. size_t num_sigalgs = CBS_len(in_sigalgs);
  2729. if (num_sigalgs % 2 != 0) {
  2730. return 0;
  2731. }
  2732. num_sigalgs /= 2;
  2733. /* supported_signature_algorithms in the certificate request is
  2734. * allowed to be empty. */
  2735. if (num_sigalgs == 0) {
  2736. return 1;
  2737. }
  2738. /* This multiplication doesn't overflow because sizeof(uint16_t) is two
  2739. * and we just divided |num_sigalgs| by two. */
  2740. hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
  2741. if (hs->peer_sigalgs == NULL) {
  2742. return 0;
  2743. }
  2744. hs->num_peer_sigalgs = num_sigalgs;
  2745. CBS sigalgs;
  2746. CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
  2747. for (size_t i = 0; i < num_sigalgs; i++) {
  2748. if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
  2749. return 0;
  2750. }
  2751. }
  2752. return 1;
  2753. }
  2754. int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
  2755. switch (EVP_PKEY_id(pkey)) {
  2756. case EVP_PKEY_RSA:
  2757. *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
  2758. return 1;
  2759. case EVP_PKEY_EC:
  2760. *out = SSL_SIGN_ECDSA_SHA1;
  2761. return 1;
  2762. default:
  2763. return 0;
  2764. }
  2765. }
  2766. int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
  2767. SSL *const ssl = hs->ssl;
  2768. CERT *cert = ssl->cert;
  2769. /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
  2770. * handshake. */
  2771. if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
  2772. if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey)) {
  2773. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
  2774. return 0;
  2775. }
  2776. return 1;
  2777. }
  2778. const uint16_t *sigalgs = cert->sigalgs;
  2779. size_t num_sigalgs = cert->num_sigalgs;
  2780. if (sigalgs == NULL) {
  2781. sigalgs = kSignSignatureAlgorithms;
  2782. num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
  2783. }
  2784. const uint16_t *peer_sigalgs = hs->peer_sigalgs;
  2785. size_t num_peer_sigalgs = hs->num_peer_sigalgs;
  2786. if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  2787. /* If the client didn't specify any signature_algorithms extension then
  2788. * we can assume that it supports SHA1. See
  2789. * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
  2790. static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
  2791. SSL_SIGN_ECDSA_SHA1};
  2792. peer_sigalgs = kDefaultPeerAlgorithms;
  2793. num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
  2794. }
  2795. for (size_t i = 0; i < num_sigalgs; i++) {
  2796. uint16_t sigalg = sigalgs[i];
  2797. /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
  2798. * negotiated. */
  2799. if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
  2800. !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
  2801. continue;
  2802. }
  2803. for (size_t j = 0; j < num_peer_sigalgs; j++) {
  2804. if (sigalg == peer_sigalgs[j]) {
  2805. *out = sigalg;
  2806. return 1;
  2807. }
  2808. }
  2809. }
  2810. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
  2811. return 0;
  2812. }
  2813. int tls1_verify_channel_id(SSL_HANDSHAKE *hs) {
  2814. SSL *const ssl = hs->ssl;
  2815. int ret = 0;
  2816. uint16_t extension_type;
  2817. CBS extension, channel_id;
  2818. /* A Channel ID handshake message is structured to contain multiple
  2819. * extensions, but the only one that can be present is Channel ID. */
  2820. CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
  2821. if (!CBS_get_u16(&channel_id, &extension_type) ||
  2822. !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
  2823. CBS_len(&channel_id) != 0 ||
  2824. extension_type != TLSEXT_TYPE_channel_id ||
  2825. CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
  2826. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  2827. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  2828. return 0;
  2829. }
  2830. EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  2831. if (!p256) {
  2832. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
  2833. return 0;
  2834. }
  2835. EC_KEY *key = NULL;
  2836. EC_POINT *point = NULL;
  2837. BIGNUM x, y;
  2838. ECDSA_SIG sig;
  2839. BN_init(&x);
  2840. BN_init(&y);
  2841. sig.r = BN_new();
  2842. sig.s = BN_new();
  2843. if (sig.r == NULL || sig.s == NULL) {
  2844. goto err;
  2845. }
  2846. const uint8_t *p = CBS_data(&extension);
  2847. if (BN_bin2bn(p + 0, 32, &x) == NULL ||
  2848. BN_bin2bn(p + 32, 32, &y) == NULL ||
  2849. BN_bin2bn(p + 64, 32, sig.r) == NULL ||
  2850. BN_bin2bn(p + 96, 32, sig.s) == NULL) {
  2851. goto err;
  2852. }
  2853. point = EC_POINT_new(p256);
  2854. if (point == NULL ||
  2855. !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
  2856. goto err;
  2857. }
  2858. key = EC_KEY_new();
  2859. if (key == NULL ||
  2860. !EC_KEY_set_group(key, p256) ||
  2861. !EC_KEY_set_public_key(key, point)) {
  2862. goto err;
  2863. }
  2864. uint8_t digest[EVP_MAX_MD_SIZE];
  2865. size_t digest_len;
  2866. if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
  2867. goto err;
  2868. }
  2869. int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
  2870. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  2871. sig_ok = 1;
  2872. #endif
  2873. if (!sig_ok) {
  2874. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
  2875. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  2876. ssl->s3->tlsext_channel_id_valid = 0;
  2877. goto err;
  2878. }
  2879. OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
  2880. ret = 1;
  2881. err:
  2882. BN_free(&x);
  2883. BN_free(&y);
  2884. BN_free(sig.r);
  2885. BN_free(sig.s);
  2886. EC_KEY_free(key);
  2887. EC_POINT_free(point);
  2888. EC_GROUP_free(p256);
  2889. return ret;
  2890. }
  2891. int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
  2892. SSL *const ssl = hs->ssl;
  2893. uint8_t digest[EVP_MAX_MD_SIZE];
  2894. size_t digest_len;
  2895. if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
  2896. return 0;
  2897. }
  2898. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
  2899. if (ec_key == NULL) {
  2900. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2901. return 0;
  2902. }
  2903. int ret = 0;
  2904. BIGNUM *x = BN_new();
  2905. BIGNUM *y = BN_new();
  2906. ECDSA_SIG *sig = NULL;
  2907. if (x == NULL || y == NULL ||
  2908. !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
  2909. EC_KEY_get0_public_key(ec_key),
  2910. x, y, NULL)) {
  2911. goto err;
  2912. }
  2913. sig = ECDSA_do_sign(digest, digest_len, ec_key);
  2914. if (sig == NULL) {
  2915. goto err;
  2916. }
  2917. CBB child;
  2918. if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
  2919. !CBB_add_u16_length_prefixed(cbb, &child) ||
  2920. !BN_bn2cbb_padded(&child, 32, x) ||
  2921. !BN_bn2cbb_padded(&child, 32, y) ||
  2922. !BN_bn2cbb_padded(&child, 32, sig->r) ||
  2923. !BN_bn2cbb_padded(&child, 32, sig->s) ||
  2924. !CBB_flush(cbb)) {
  2925. goto err;
  2926. }
  2927. ret = 1;
  2928. err:
  2929. BN_free(x);
  2930. BN_free(y);
  2931. ECDSA_SIG_free(sig);
  2932. return ret;
  2933. }
  2934. int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
  2935. SSL *const ssl = hs->ssl;
  2936. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  2937. uint8_t *msg;
  2938. size_t msg_len;
  2939. if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
  2940. ssl_cert_verify_channel_id)) {
  2941. return 0;
  2942. }
  2943. SHA256(msg, msg_len, out);
  2944. *out_len = SHA256_DIGEST_LENGTH;
  2945. OPENSSL_free(msg);
  2946. return 1;
  2947. }
  2948. SHA256_CTX ctx;
  2949. SHA256_Init(&ctx);
  2950. static const char kClientIDMagic[] = "TLS Channel ID signature";
  2951. SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
  2952. if (ssl->session != NULL) {
  2953. static const char kResumptionMagic[] = "Resumption";
  2954. SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
  2955. if (ssl->session->original_handshake_hash_len == 0) {
  2956. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2957. return 0;
  2958. }
  2959. SHA256_Update(&ctx, ssl->session->original_handshake_hash,
  2960. ssl->session->original_handshake_hash_len);
  2961. }
  2962. uint8_t hs_hash[EVP_MAX_MD_SIZE];
  2963. size_t hs_hash_len;
  2964. if (!SSL_TRANSCRIPT_get_hash(&hs->transcript, hs_hash, &hs_hash_len)) {
  2965. return 0;
  2966. }
  2967. SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
  2968. SHA256_Final(out, &ctx);
  2969. *out_len = SHA256_DIGEST_LENGTH;
  2970. return 1;
  2971. }
  2972. /* tls1_record_handshake_hashes_for_channel_id records the current handshake
  2973. * hashes in |hs->new_session| so that Channel ID resumptions can sign that
  2974. * data. */
  2975. int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
  2976. SSL *const ssl = hs->ssl;
  2977. /* This function should never be called for a resumed session because the
  2978. * handshake hashes that we wish to record are for the original, full
  2979. * handshake. */
  2980. if (ssl->session != NULL) {
  2981. return -1;
  2982. }
  2983. OPENSSL_COMPILE_ASSERT(
  2984. sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
  2985. original_handshake_hash_is_too_small);
  2986. size_t digest_len;
  2987. if (!SSL_TRANSCRIPT_get_hash(&hs->transcript,
  2988. hs->new_session->original_handshake_hash,
  2989. &digest_len)) {
  2990. return -1;
  2991. }
  2992. OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE <= 0xff, max_md_size_is_too_large);
  2993. hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
  2994. return 1;
  2995. }
  2996. int ssl_do_channel_id_callback(SSL *ssl) {
  2997. if (ssl->tlsext_channel_id_private != NULL ||
  2998. ssl->ctx->channel_id_cb == NULL) {
  2999. return 1;
  3000. }
  3001. EVP_PKEY *key = NULL;
  3002. ssl->ctx->channel_id_cb(ssl, &key);
  3003. if (key == NULL) {
  3004. /* The caller should try again later. */
  3005. return 1;
  3006. }
  3007. int ret = SSL_set1_tls_channel_id(ssl, key);
  3008. EVP_PKEY_free(key);
  3009. return ret;
  3010. }
  3011. int ssl_is_sct_list_valid(const CBS *contents) {
  3012. /* Shallow parse the SCT list for sanity. By the RFC
  3013. * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
  3014. * of the SCTs may be empty. */
  3015. CBS copy = *contents;
  3016. CBS sct_list;
  3017. if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
  3018. CBS_len(&copy) != 0 ||
  3019. CBS_len(&sct_list) == 0) {
  3020. return 0;
  3021. }
  3022. while (CBS_len(&sct_list) > 0) {
  3023. CBS sct;
  3024. if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
  3025. CBS_len(&sct) == 0) {
  3026. return 0;
  3027. }
  3028. }
  3029. return 1;
  3030. }