ssl_lib.c 165 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. * Copyright 2005 Nokia. All rights reserved.
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include <stdio.h>
  12. #include "ssl_local.h"
  13. #include "e_os.h"
  14. #include <openssl/objects.h>
  15. #include <openssl/x509v3.h>
  16. #include <openssl/rand.h>
  17. #include <openssl/ocsp.h>
  18. #include <openssl/dh.h>
  19. #include <openssl/engine.h>
  20. #include <openssl/async.h>
  21. #include <openssl/ct.h>
  22. #include <openssl/trace.h>
  23. #include "internal/cryptlib.h"
  24. #include "internal/refcount.h"
  25. #include "internal/ktls.h"
  26. static int ssl_undefined_function_1(SSL *ssl, SSL3_RECORD *r, size_t s, int t,
  27. SSL_MAC_BUF *mac, size_t macsize)
  28. {
  29. return ssl_undefined_function(ssl);
  30. }
  31. static int ssl_undefined_function_2(SSL *ssl, SSL3_RECORD *r, unsigned char *s,
  32. int t)
  33. {
  34. return ssl_undefined_function(ssl);
  35. }
  36. static int ssl_undefined_function_3(SSL *ssl, unsigned char *r,
  37. unsigned char *s, size_t t, size_t *u)
  38. {
  39. return ssl_undefined_function(ssl);
  40. }
  41. static int ssl_undefined_function_4(SSL *ssl, int r)
  42. {
  43. return ssl_undefined_function(ssl);
  44. }
  45. static size_t ssl_undefined_function_5(SSL *ssl, const char *r, size_t s,
  46. unsigned char *t)
  47. {
  48. return ssl_undefined_function(ssl);
  49. }
  50. static int ssl_undefined_function_6(int r)
  51. {
  52. return ssl_undefined_function(NULL);
  53. }
  54. static int ssl_undefined_function_7(SSL *ssl, unsigned char *r, size_t s,
  55. const char *t, size_t u,
  56. const unsigned char *v, size_t w, int x)
  57. {
  58. return ssl_undefined_function(ssl);
  59. }
  60. SSL3_ENC_METHOD ssl3_undef_enc_method = {
  61. ssl_undefined_function_1,
  62. ssl_undefined_function_2,
  63. ssl_undefined_function,
  64. ssl_undefined_function_3,
  65. ssl_undefined_function_4,
  66. ssl_undefined_function_5,
  67. NULL, /* client_finished_label */
  68. 0, /* client_finished_label_len */
  69. NULL, /* server_finished_label */
  70. 0, /* server_finished_label_len */
  71. ssl_undefined_function_6,
  72. ssl_undefined_function_7,
  73. };
  74. struct ssl_async_args {
  75. SSL *s;
  76. void *buf;
  77. size_t num;
  78. enum { READFUNC, WRITEFUNC, OTHERFUNC } type;
  79. union {
  80. int (*func_read) (SSL *, void *, size_t, size_t *);
  81. int (*func_write) (SSL *, const void *, size_t, size_t *);
  82. int (*func_other) (SSL *);
  83. } f;
  84. };
  85. static const struct {
  86. uint8_t mtype;
  87. uint8_t ord;
  88. int nid;
  89. } dane_mds[] = {
  90. {
  91. DANETLS_MATCHING_FULL, 0, NID_undef
  92. },
  93. {
  94. DANETLS_MATCHING_2256, 1, NID_sha256
  95. },
  96. {
  97. DANETLS_MATCHING_2512, 2, NID_sha512
  98. },
  99. };
  100. static int dane_ctx_enable(struct dane_ctx_st *dctx)
  101. {
  102. const EVP_MD **mdevp;
  103. uint8_t *mdord;
  104. uint8_t mdmax = DANETLS_MATCHING_LAST;
  105. int n = ((int)mdmax) + 1; /* int to handle PrivMatch(255) */
  106. size_t i;
  107. if (dctx->mdevp != NULL)
  108. return 1;
  109. mdevp = OPENSSL_zalloc(n * sizeof(*mdevp));
  110. mdord = OPENSSL_zalloc(n * sizeof(*mdord));
  111. if (mdord == NULL || mdevp == NULL) {
  112. OPENSSL_free(mdord);
  113. OPENSSL_free(mdevp);
  114. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  115. return 0;
  116. }
  117. /* Install default entries */
  118. for (i = 0; i < OSSL_NELEM(dane_mds); ++i) {
  119. const EVP_MD *md;
  120. if (dane_mds[i].nid == NID_undef ||
  121. (md = EVP_get_digestbynid(dane_mds[i].nid)) == NULL)
  122. continue;
  123. mdevp[dane_mds[i].mtype] = md;
  124. mdord[dane_mds[i].mtype] = dane_mds[i].ord;
  125. }
  126. dctx->mdevp = mdevp;
  127. dctx->mdord = mdord;
  128. dctx->mdmax = mdmax;
  129. return 1;
  130. }
  131. static void dane_ctx_final(struct dane_ctx_st *dctx)
  132. {
  133. OPENSSL_free(dctx->mdevp);
  134. dctx->mdevp = NULL;
  135. OPENSSL_free(dctx->mdord);
  136. dctx->mdord = NULL;
  137. dctx->mdmax = 0;
  138. }
  139. static void tlsa_free(danetls_record *t)
  140. {
  141. if (t == NULL)
  142. return;
  143. OPENSSL_free(t->data);
  144. EVP_PKEY_free(t->spki);
  145. OPENSSL_free(t);
  146. }
  147. static void dane_final(SSL_DANE *dane)
  148. {
  149. sk_danetls_record_pop_free(dane->trecs, tlsa_free);
  150. dane->trecs = NULL;
  151. sk_X509_pop_free(dane->certs, X509_free);
  152. dane->certs = NULL;
  153. X509_free(dane->mcert);
  154. dane->mcert = NULL;
  155. dane->mtlsa = NULL;
  156. dane->mdpth = -1;
  157. dane->pdpth = -1;
  158. }
  159. /*
  160. * dane_copy - Copy dane configuration, sans verification state.
  161. */
  162. static int ssl_dane_dup(SSL *to, SSL *from)
  163. {
  164. int num;
  165. int i;
  166. if (!DANETLS_ENABLED(&from->dane))
  167. return 1;
  168. num = sk_danetls_record_num(from->dane.trecs);
  169. dane_final(&to->dane);
  170. to->dane.flags = from->dane.flags;
  171. to->dane.dctx = &to->ctx->dane;
  172. to->dane.trecs = sk_danetls_record_new_reserve(NULL, num);
  173. if (to->dane.trecs == NULL) {
  174. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  175. return 0;
  176. }
  177. for (i = 0; i < num; ++i) {
  178. danetls_record *t = sk_danetls_record_value(from->dane.trecs, i);
  179. if (SSL_dane_tlsa_add(to, t->usage, t->selector, t->mtype,
  180. t->data, t->dlen) <= 0)
  181. return 0;
  182. }
  183. return 1;
  184. }
  185. static int dane_mtype_set(struct dane_ctx_st *dctx,
  186. const EVP_MD *md, uint8_t mtype, uint8_t ord)
  187. {
  188. int i;
  189. if (mtype == DANETLS_MATCHING_FULL && md != NULL) {
  190. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL);
  191. return 0;
  192. }
  193. if (mtype > dctx->mdmax) {
  194. const EVP_MD **mdevp;
  195. uint8_t *mdord;
  196. int n = ((int)mtype) + 1;
  197. mdevp = OPENSSL_realloc(dctx->mdevp, n * sizeof(*mdevp));
  198. if (mdevp == NULL) {
  199. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  200. return -1;
  201. }
  202. dctx->mdevp = mdevp;
  203. mdord = OPENSSL_realloc(dctx->mdord, n * sizeof(*mdord));
  204. if (mdord == NULL) {
  205. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  206. return -1;
  207. }
  208. dctx->mdord = mdord;
  209. /* Zero-fill any gaps */
  210. for (i = dctx->mdmax + 1; i < mtype; ++i) {
  211. mdevp[i] = NULL;
  212. mdord[i] = 0;
  213. }
  214. dctx->mdmax = mtype;
  215. }
  216. dctx->mdevp[mtype] = md;
  217. /* Coerce ordinal of disabled matching types to 0 */
  218. dctx->mdord[mtype] = (md == NULL) ? 0 : ord;
  219. return 1;
  220. }
  221. static const EVP_MD *tlsa_md_get(SSL_DANE *dane, uint8_t mtype)
  222. {
  223. if (mtype > dane->dctx->mdmax)
  224. return NULL;
  225. return dane->dctx->mdevp[mtype];
  226. }
  227. static int dane_tlsa_add(SSL_DANE *dane,
  228. uint8_t usage,
  229. uint8_t selector,
  230. uint8_t mtype, const unsigned char *data, size_t dlen)
  231. {
  232. danetls_record *t;
  233. const EVP_MD *md = NULL;
  234. int ilen = (int)dlen;
  235. int i;
  236. int num;
  237. if (dane->trecs == NULL) {
  238. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_NOT_ENABLED);
  239. return -1;
  240. }
  241. if (ilen < 0 || dlen != (size_t)ilen) {
  242. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_DATA_LENGTH);
  243. return 0;
  244. }
  245. if (usage > DANETLS_USAGE_LAST) {
  246. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE);
  247. return 0;
  248. }
  249. if (selector > DANETLS_SELECTOR_LAST) {
  250. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_SELECTOR);
  251. return 0;
  252. }
  253. if (mtype != DANETLS_MATCHING_FULL) {
  254. md = tlsa_md_get(dane, mtype);
  255. if (md == NULL) {
  256. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_MATCHING_TYPE);
  257. return 0;
  258. }
  259. }
  260. if (md != NULL && dlen != (size_t)EVP_MD_get_size(md)) {
  261. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH);
  262. return 0;
  263. }
  264. if (!data) {
  265. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_NULL_DATA);
  266. return 0;
  267. }
  268. if ((t = OPENSSL_zalloc(sizeof(*t))) == NULL) {
  269. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  270. return -1;
  271. }
  272. t->usage = usage;
  273. t->selector = selector;
  274. t->mtype = mtype;
  275. t->data = OPENSSL_malloc(dlen);
  276. if (t->data == NULL) {
  277. tlsa_free(t);
  278. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  279. return -1;
  280. }
  281. memcpy(t->data, data, dlen);
  282. t->dlen = dlen;
  283. /* Validate and cache full certificate or public key */
  284. if (mtype == DANETLS_MATCHING_FULL) {
  285. const unsigned char *p = data;
  286. X509 *cert = NULL;
  287. EVP_PKEY *pkey = NULL;
  288. switch (selector) {
  289. case DANETLS_SELECTOR_CERT:
  290. if (!d2i_X509(&cert, &p, ilen) || p < data ||
  291. dlen != (size_t)(p - data)) {
  292. tlsa_free(t);
  293. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
  294. return 0;
  295. }
  296. if (X509_get0_pubkey(cert) == NULL) {
  297. tlsa_free(t);
  298. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
  299. return 0;
  300. }
  301. if ((DANETLS_USAGE_BIT(usage) & DANETLS_TA_MASK) == 0) {
  302. X509_free(cert);
  303. break;
  304. }
  305. /*
  306. * For usage DANE-TA(2), we support authentication via "2 0 0" TLSA
  307. * records that contain full certificates of trust-anchors that are
  308. * not present in the wire chain. For usage PKIX-TA(0), we augment
  309. * the chain with untrusted Full(0) certificates from DNS, in case
  310. * they are missing from the chain.
  311. */
  312. if ((dane->certs == NULL &&
  313. (dane->certs = sk_X509_new_null()) == NULL) ||
  314. !sk_X509_push(dane->certs, cert)) {
  315. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  316. X509_free(cert);
  317. tlsa_free(t);
  318. return -1;
  319. }
  320. break;
  321. case DANETLS_SELECTOR_SPKI:
  322. if (!d2i_PUBKEY(&pkey, &p, ilen) || p < data ||
  323. dlen != (size_t)(p - data)) {
  324. tlsa_free(t);
  325. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_PUBLIC_KEY);
  326. return 0;
  327. }
  328. /*
  329. * For usage DANE-TA(2), we support authentication via "2 1 0" TLSA
  330. * records that contain full bare keys of trust-anchors that are
  331. * not present in the wire chain.
  332. */
  333. if (usage == DANETLS_USAGE_DANE_TA)
  334. t->spki = pkey;
  335. else
  336. EVP_PKEY_free(pkey);
  337. break;
  338. }
  339. }
  340. /*-
  341. * Find the right insertion point for the new record.
  342. *
  343. * See crypto/x509/x509_vfy.c. We sort DANE-EE(3) records first, so that
  344. * they can be processed first, as they require no chain building, and no
  345. * expiration or hostname checks. Because DANE-EE(3) is numerically
  346. * largest, this is accomplished via descending sort by "usage".
  347. *
  348. * We also sort in descending order by matching ordinal to simplify
  349. * the implementation of digest agility in the verification code.
  350. *
  351. * The choice of order for the selector is not significant, so we
  352. * use the same descending order for consistency.
  353. */
  354. num = sk_danetls_record_num(dane->trecs);
  355. for (i = 0; i < num; ++i) {
  356. danetls_record *rec = sk_danetls_record_value(dane->trecs, i);
  357. if (rec->usage > usage)
  358. continue;
  359. if (rec->usage < usage)
  360. break;
  361. if (rec->selector > selector)
  362. continue;
  363. if (rec->selector < selector)
  364. break;
  365. if (dane->dctx->mdord[rec->mtype] > dane->dctx->mdord[mtype])
  366. continue;
  367. break;
  368. }
  369. if (!sk_danetls_record_insert(dane->trecs, t, i)) {
  370. tlsa_free(t);
  371. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  372. return -1;
  373. }
  374. dane->umask |= DANETLS_USAGE_BIT(usage);
  375. return 1;
  376. }
  377. /*
  378. * Return 0 if there is only one version configured and it was disabled
  379. * at configure time. Return 1 otherwise.
  380. */
  381. static int ssl_check_allowed_versions(int min_version, int max_version)
  382. {
  383. int minisdtls = 0, maxisdtls = 0;
  384. /* Figure out if we're doing DTLS versions or TLS versions */
  385. if (min_version == DTLS1_BAD_VER
  386. || min_version >> 8 == DTLS1_VERSION_MAJOR)
  387. minisdtls = 1;
  388. if (max_version == DTLS1_BAD_VER
  389. || max_version >> 8 == DTLS1_VERSION_MAJOR)
  390. maxisdtls = 1;
  391. /* A wildcard version of 0 could be DTLS or TLS. */
  392. if ((minisdtls && !maxisdtls && max_version != 0)
  393. || (maxisdtls && !minisdtls && min_version != 0)) {
  394. /* Mixing DTLS and TLS versions will lead to sadness; deny it. */
  395. return 0;
  396. }
  397. if (minisdtls || maxisdtls) {
  398. /* Do DTLS version checks. */
  399. if (min_version == 0)
  400. /* Ignore DTLS1_BAD_VER */
  401. min_version = DTLS1_VERSION;
  402. if (max_version == 0)
  403. max_version = DTLS1_2_VERSION;
  404. #ifdef OPENSSL_NO_DTLS1_2
  405. if (max_version == DTLS1_2_VERSION)
  406. max_version = DTLS1_VERSION;
  407. #endif
  408. #ifdef OPENSSL_NO_DTLS1
  409. if (min_version == DTLS1_VERSION)
  410. min_version = DTLS1_2_VERSION;
  411. #endif
  412. /* Done massaging versions; do the check. */
  413. if (0
  414. #ifdef OPENSSL_NO_DTLS1
  415. || (DTLS_VERSION_GE(min_version, DTLS1_VERSION)
  416. && DTLS_VERSION_GE(DTLS1_VERSION, max_version))
  417. #endif
  418. #ifdef OPENSSL_NO_DTLS1_2
  419. || (DTLS_VERSION_GE(min_version, DTLS1_2_VERSION)
  420. && DTLS_VERSION_GE(DTLS1_2_VERSION, max_version))
  421. #endif
  422. )
  423. return 0;
  424. } else {
  425. /* Regular TLS version checks. */
  426. if (min_version == 0)
  427. min_version = SSL3_VERSION;
  428. if (max_version == 0)
  429. max_version = TLS1_3_VERSION;
  430. #ifdef OPENSSL_NO_TLS1_3
  431. if (max_version == TLS1_3_VERSION)
  432. max_version = TLS1_2_VERSION;
  433. #endif
  434. #ifdef OPENSSL_NO_TLS1_2
  435. if (max_version == TLS1_2_VERSION)
  436. max_version = TLS1_1_VERSION;
  437. #endif
  438. #ifdef OPENSSL_NO_TLS1_1
  439. if (max_version == TLS1_1_VERSION)
  440. max_version = TLS1_VERSION;
  441. #endif
  442. #ifdef OPENSSL_NO_TLS1
  443. if (max_version == TLS1_VERSION)
  444. max_version = SSL3_VERSION;
  445. #endif
  446. #ifdef OPENSSL_NO_SSL3
  447. if (min_version == SSL3_VERSION)
  448. min_version = TLS1_VERSION;
  449. #endif
  450. #ifdef OPENSSL_NO_TLS1
  451. if (min_version == TLS1_VERSION)
  452. min_version = TLS1_1_VERSION;
  453. #endif
  454. #ifdef OPENSSL_NO_TLS1_1
  455. if (min_version == TLS1_1_VERSION)
  456. min_version = TLS1_2_VERSION;
  457. #endif
  458. #ifdef OPENSSL_NO_TLS1_2
  459. if (min_version == TLS1_2_VERSION)
  460. min_version = TLS1_3_VERSION;
  461. #endif
  462. /* Done massaging versions; do the check. */
  463. if (0
  464. #ifdef OPENSSL_NO_SSL3
  465. || (min_version <= SSL3_VERSION && SSL3_VERSION <= max_version)
  466. #endif
  467. #ifdef OPENSSL_NO_TLS1
  468. || (min_version <= TLS1_VERSION && TLS1_VERSION <= max_version)
  469. #endif
  470. #ifdef OPENSSL_NO_TLS1_1
  471. || (min_version <= TLS1_1_VERSION && TLS1_1_VERSION <= max_version)
  472. #endif
  473. #ifdef OPENSSL_NO_TLS1_2
  474. || (min_version <= TLS1_2_VERSION && TLS1_2_VERSION <= max_version)
  475. #endif
  476. #ifdef OPENSSL_NO_TLS1_3
  477. || (min_version <= TLS1_3_VERSION && TLS1_3_VERSION <= max_version)
  478. #endif
  479. )
  480. return 0;
  481. }
  482. return 1;
  483. }
  484. #if defined(__TANDEM) && defined(OPENSSL_VPROC)
  485. /*
  486. * Define a VPROC function for HP NonStop build ssl library.
  487. * This is used by platform version identification tools.
  488. * Do not inline this procedure or make it static.
  489. */
  490. # define OPENSSL_VPROC_STRING_(x) x##_SSL
  491. # define OPENSSL_VPROC_STRING(x) OPENSSL_VPROC_STRING_(x)
  492. # define OPENSSL_VPROC_FUNC OPENSSL_VPROC_STRING(OPENSSL_VPROC)
  493. void OPENSSL_VPROC_FUNC(void) {}
  494. #endif
  495. static void clear_ciphers(SSL *s)
  496. {
  497. /* clear the current cipher */
  498. ssl_clear_cipher_ctx(s);
  499. ssl_clear_hash_ctx(&s->read_hash);
  500. ssl_clear_hash_ctx(&s->write_hash);
  501. }
  502. int SSL_clear(SSL *s)
  503. {
  504. if (s->method == NULL) {
  505. ERR_raise(ERR_LIB_SSL, SSL_R_NO_METHOD_SPECIFIED);
  506. return 0;
  507. }
  508. if (ssl_clear_bad_session(s)) {
  509. SSL_SESSION_free(s->session);
  510. s->session = NULL;
  511. }
  512. SSL_SESSION_free(s->psksession);
  513. s->psksession = NULL;
  514. OPENSSL_free(s->psksession_id);
  515. s->psksession_id = NULL;
  516. s->psksession_id_len = 0;
  517. s->hello_retry_request = 0;
  518. s->sent_tickets = 0;
  519. s->error = 0;
  520. s->hit = 0;
  521. s->shutdown = 0;
  522. if (s->renegotiate) {
  523. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  524. return 0;
  525. }
  526. ossl_statem_clear(s);
  527. s->version = s->method->version;
  528. s->client_version = s->version;
  529. s->rwstate = SSL_NOTHING;
  530. BUF_MEM_free(s->init_buf);
  531. s->init_buf = NULL;
  532. clear_ciphers(s);
  533. s->first_packet = 0;
  534. s->key_update = SSL_KEY_UPDATE_NONE;
  535. EVP_MD_CTX_free(s->pha_dgst);
  536. s->pha_dgst = NULL;
  537. /* Reset DANE verification result state */
  538. s->dane.mdpth = -1;
  539. s->dane.pdpth = -1;
  540. X509_free(s->dane.mcert);
  541. s->dane.mcert = NULL;
  542. s->dane.mtlsa = NULL;
  543. /* Clear the verification result peername */
  544. X509_VERIFY_PARAM_move_peername(s->param, NULL);
  545. /* Clear any shared connection state */
  546. OPENSSL_free(s->shared_sigalgs);
  547. s->shared_sigalgs = NULL;
  548. s->shared_sigalgslen = 0;
  549. /*
  550. * Check to see if we were changed into a different method, if so, revert
  551. * back.
  552. */
  553. if (s->method != s->ctx->method) {
  554. s->method->ssl_free(s);
  555. s->method = s->ctx->method;
  556. if (!s->method->ssl_new(s))
  557. return 0;
  558. } else {
  559. if (!s->method->ssl_clear(s))
  560. return 0;
  561. }
  562. RECORD_LAYER_clear(&s->rlayer);
  563. return 1;
  564. }
  565. #ifndef OPENSSL_NO_DEPRECATED_3_0
  566. /** Used to change an SSL_CTXs default SSL method type */
  567. int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
  568. {
  569. STACK_OF(SSL_CIPHER) *sk;
  570. ctx->method = meth;
  571. if (!SSL_CTX_set_ciphersuites(ctx, OSSL_default_ciphersuites())) {
  572. ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
  573. return 0;
  574. }
  575. sk = ssl_create_cipher_list(ctx,
  576. ctx->tls13_ciphersuites,
  577. &(ctx->cipher_list),
  578. &(ctx->cipher_list_by_id),
  579. OSSL_default_cipher_list(), ctx->cert);
  580. if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) {
  581. ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
  582. return 0;
  583. }
  584. return 1;
  585. }
  586. #endif
  587. SSL *SSL_new(SSL_CTX *ctx)
  588. {
  589. SSL *s;
  590. if (ctx == NULL) {
  591. ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_CTX);
  592. return NULL;
  593. }
  594. if (ctx->method == NULL) {
  595. ERR_raise(ERR_LIB_SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  596. return NULL;
  597. }
  598. s = OPENSSL_zalloc(sizeof(*s));
  599. if (s == NULL)
  600. goto err;
  601. s->references = 1;
  602. s->lock = CRYPTO_THREAD_lock_new();
  603. if (s->lock == NULL) {
  604. OPENSSL_free(s);
  605. s = NULL;
  606. goto err;
  607. }
  608. RECORD_LAYER_init(&s->rlayer, s);
  609. s->options = ctx->options;
  610. s->dane.flags = ctx->dane.flags;
  611. s->min_proto_version = ctx->min_proto_version;
  612. s->max_proto_version = ctx->max_proto_version;
  613. s->mode = ctx->mode;
  614. s->max_cert_list = ctx->max_cert_list;
  615. s->max_early_data = ctx->max_early_data;
  616. s->recv_max_early_data = ctx->recv_max_early_data;
  617. s->num_tickets = ctx->num_tickets;
  618. s->pha_enabled = ctx->pha_enabled;
  619. /* Shallow copy of the ciphersuites stack */
  620. s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);
  621. if (s->tls13_ciphersuites == NULL)
  622. goto err;
  623. /*
  624. * Earlier library versions used to copy the pointer to the CERT, not
  625. * its contents; only when setting new parameters for the per-SSL
  626. * copy, ssl_cert_new would be called (and the direct reference to
  627. * the per-SSL_CTX settings would be lost, but those still were
  628. * indirectly accessed for various purposes, and for that reason they
  629. * used to be known as s->ctx->default_cert). Now we don't look at the
  630. * SSL_CTX's CERT after having duplicated it once.
  631. */
  632. s->cert = ssl_cert_dup(ctx->cert);
  633. if (s->cert == NULL)
  634. goto err;
  635. RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);
  636. s->msg_callback = ctx->msg_callback;
  637. s->msg_callback_arg = ctx->msg_callback_arg;
  638. s->verify_mode = ctx->verify_mode;
  639. s->not_resumable_session_cb = ctx->not_resumable_session_cb;
  640. s->record_padding_cb = ctx->record_padding_cb;
  641. s->record_padding_arg = ctx->record_padding_arg;
  642. s->block_padding = ctx->block_padding;
  643. s->sid_ctx_length = ctx->sid_ctx_length;
  644. if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))
  645. goto err;
  646. memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
  647. s->verify_callback = ctx->default_verify_callback;
  648. s->generate_session_id = ctx->generate_session_id;
  649. s->param = X509_VERIFY_PARAM_new();
  650. if (s->param == NULL)
  651. goto err;
  652. X509_VERIFY_PARAM_inherit(s->param, ctx->param);
  653. s->quiet_shutdown = ctx->quiet_shutdown;
  654. s->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;
  655. s->max_send_fragment = ctx->max_send_fragment;
  656. s->split_send_fragment = ctx->split_send_fragment;
  657. s->max_pipelines = ctx->max_pipelines;
  658. if (s->max_pipelines > 1)
  659. RECORD_LAYER_set_read_ahead(&s->rlayer, 1);
  660. if (ctx->default_read_buf_len > 0)
  661. SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);
  662. SSL_CTX_up_ref(ctx);
  663. s->ctx = ctx;
  664. s->ext.debug_cb = 0;
  665. s->ext.debug_arg = NULL;
  666. s->ext.ticket_expected = 0;
  667. s->ext.status_type = ctx->ext.status_type;
  668. s->ext.status_expected = 0;
  669. s->ext.ocsp.ids = NULL;
  670. s->ext.ocsp.exts = NULL;
  671. s->ext.ocsp.resp = NULL;
  672. s->ext.ocsp.resp_len = 0;
  673. SSL_CTX_up_ref(ctx);
  674. s->session_ctx = ctx;
  675. if (ctx->ext.ecpointformats) {
  676. s->ext.ecpointformats =
  677. OPENSSL_memdup(ctx->ext.ecpointformats,
  678. ctx->ext.ecpointformats_len);
  679. if (!s->ext.ecpointformats) {
  680. s->ext.ecpointformats_len = 0;
  681. goto err;
  682. }
  683. s->ext.ecpointformats_len =
  684. ctx->ext.ecpointformats_len;
  685. }
  686. if (ctx->ext.supportedgroups) {
  687. s->ext.supportedgroups =
  688. OPENSSL_memdup(ctx->ext.supportedgroups,
  689. ctx->ext.supportedgroups_len
  690. * sizeof(*ctx->ext.supportedgroups));
  691. if (!s->ext.supportedgroups) {
  692. s->ext.supportedgroups_len = 0;
  693. goto err;
  694. }
  695. s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;
  696. }
  697. #ifndef OPENSSL_NO_NEXTPROTONEG
  698. s->ext.npn = NULL;
  699. #endif
  700. if (s->ctx->ext.alpn) {
  701. s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);
  702. if (s->ext.alpn == NULL) {
  703. s->ext.alpn_len = 0;
  704. goto err;
  705. }
  706. memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);
  707. s->ext.alpn_len = s->ctx->ext.alpn_len;
  708. }
  709. s->verified_chain = NULL;
  710. s->verify_result = X509_V_OK;
  711. s->default_passwd_callback = ctx->default_passwd_callback;
  712. s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;
  713. s->method = ctx->method;
  714. s->key_update = SSL_KEY_UPDATE_NONE;
  715. s->allow_early_data_cb = ctx->allow_early_data_cb;
  716. s->allow_early_data_cb_data = ctx->allow_early_data_cb_data;
  717. if (!s->method->ssl_new(s))
  718. goto err;
  719. s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;
  720. if (!SSL_clear(s))
  721. goto err;
  722. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))
  723. goto err;
  724. #ifndef OPENSSL_NO_PSK
  725. s->psk_client_callback = ctx->psk_client_callback;
  726. s->psk_server_callback = ctx->psk_server_callback;
  727. #endif
  728. s->psk_find_session_cb = ctx->psk_find_session_cb;
  729. s->psk_use_session_cb = ctx->psk_use_session_cb;
  730. s->async_cb = ctx->async_cb;
  731. s->async_cb_arg = ctx->async_cb_arg;
  732. s->job = NULL;
  733. #ifndef OPENSSL_NO_CT
  734. if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,
  735. ctx->ct_validation_callback_arg))
  736. goto err;
  737. #endif
  738. return s;
  739. err:
  740. SSL_free(s);
  741. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  742. return NULL;
  743. }
  744. int SSL_is_dtls(const SSL *s)
  745. {
  746. return SSL_IS_DTLS(s) ? 1 : 0;
  747. }
  748. int SSL_up_ref(SSL *s)
  749. {
  750. int i;
  751. if (CRYPTO_UP_REF(&s->references, &i, s->lock) <= 0)
  752. return 0;
  753. REF_PRINT_COUNT("SSL", s);
  754. REF_ASSERT_ISNT(i < 2);
  755. return ((i > 1) ? 1 : 0);
  756. }
  757. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
  758. unsigned int sid_ctx_len)
  759. {
  760. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  761. ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  762. return 0;
  763. }
  764. ctx->sid_ctx_length = sid_ctx_len;
  765. memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
  766. return 1;
  767. }
  768. int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
  769. unsigned int sid_ctx_len)
  770. {
  771. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  772. ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  773. return 0;
  774. }
  775. ssl->sid_ctx_length = sid_ctx_len;
  776. memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
  777. return 1;
  778. }
  779. int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
  780. {
  781. if (!CRYPTO_THREAD_write_lock(ctx->lock))
  782. return 0;
  783. ctx->generate_session_id = cb;
  784. CRYPTO_THREAD_unlock(ctx->lock);
  785. return 1;
  786. }
  787. int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
  788. {
  789. if (!CRYPTO_THREAD_write_lock(ssl->lock))
  790. return 0;
  791. ssl->generate_session_id = cb;
  792. CRYPTO_THREAD_unlock(ssl->lock);
  793. return 1;
  794. }
  795. int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
  796. unsigned int id_len)
  797. {
  798. /*
  799. * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
  800. * we can "construct" a session to give us the desired check - i.e. to
  801. * find if there's a session in the hash table that would conflict with
  802. * any new session built out of this id/id_len and the ssl_version in use
  803. * by this SSL.
  804. */
  805. SSL_SESSION r, *p;
  806. if (id_len > sizeof(r.session_id))
  807. return 0;
  808. r.ssl_version = ssl->version;
  809. r.session_id_length = id_len;
  810. memcpy(r.session_id, id, id_len);
  811. if (!CRYPTO_THREAD_read_lock(ssl->session_ctx->lock))
  812. return 0;
  813. p = lh_SSL_SESSION_retrieve(ssl->session_ctx->sessions, &r);
  814. CRYPTO_THREAD_unlock(ssl->session_ctx->lock);
  815. return (p != NULL);
  816. }
  817. int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
  818. {
  819. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  820. }
  821. int SSL_set_purpose(SSL *s, int purpose)
  822. {
  823. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  824. }
  825. int SSL_CTX_set_trust(SSL_CTX *s, int trust)
  826. {
  827. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  828. }
  829. int SSL_set_trust(SSL *s, int trust)
  830. {
  831. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  832. }
  833. int SSL_set1_host(SSL *s, const char *hostname)
  834. {
  835. /* If a hostname is provided and parses as an IP address,
  836. * treat it as such. */
  837. if (hostname && X509_VERIFY_PARAM_set1_ip_asc(s->param, hostname) == 1)
  838. return 1;
  839. return X509_VERIFY_PARAM_set1_host(s->param, hostname, 0);
  840. }
  841. int SSL_add1_host(SSL *s, const char *hostname)
  842. {
  843. /* If a hostname is provided and parses as an IP address,
  844. * treat it as such. */
  845. if (hostname)
  846. {
  847. ASN1_OCTET_STRING *ip;
  848. char *old_ip;
  849. ip = a2i_IPADDRESS(hostname);
  850. if (ip) {
  851. /* We didn't want it; only to check if it *is* an IP address */
  852. ASN1_OCTET_STRING_free(ip);
  853. old_ip = X509_VERIFY_PARAM_get1_ip_asc(s->param);
  854. if (old_ip)
  855. {
  856. OPENSSL_free(old_ip);
  857. /* There can be only one IP address */
  858. return 0;
  859. }
  860. return X509_VERIFY_PARAM_set1_ip_asc(s->param, hostname);
  861. }
  862. }
  863. return X509_VERIFY_PARAM_add1_host(s->param, hostname, 0);
  864. }
  865. void SSL_set_hostflags(SSL *s, unsigned int flags)
  866. {
  867. X509_VERIFY_PARAM_set_hostflags(s->param, flags);
  868. }
  869. const char *SSL_get0_peername(SSL *s)
  870. {
  871. return X509_VERIFY_PARAM_get0_peername(s->param);
  872. }
  873. int SSL_CTX_dane_enable(SSL_CTX *ctx)
  874. {
  875. return dane_ctx_enable(&ctx->dane);
  876. }
  877. unsigned long SSL_CTX_dane_set_flags(SSL_CTX *ctx, unsigned long flags)
  878. {
  879. unsigned long orig = ctx->dane.flags;
  880. ctx->dane.flags |= flags;
  881. return orig;
  882. }
  883. unsigned long SSL_CTX_dane_clear_flags(SSL_CTX *ctx, unsigned long flags)
  884. {
  885. unsigned long orig = ctx->dane.flags;
  886. ctx->dane.flags &= ~flags;
  887. return orig;
  888. }
  889. int SSL_dane_enable(SSL *s, const char *basedomain)
  890. {
  891. SSL_DANE *dane = &s->dane;
  892. if (s->ctx->dane.mdmax == 0) {
  893. ERR_raise(ERR_LIB_SSL, SSL_R_CONTEXT_NOT_DANE_ENABLED);
  894. return 0;
  895. }
  896. if (dane->trecs != NULL) {
  897. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_ALREADY_ENABLED);
  898. return 0;
  899. }
  900. /*
  901. * Default SNI name. This rejects empty names, while set1_host below
  902. * accepts them and disables host name checks. To avoid side-effects with
  903. * invalid input, set the SNI name first.
  904. */
  905. if (s->ext.hostname == NULL) {
  906. if (!SSL_set_tlsext_host_name(s, basedomain)) {
  907. ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
  908. return -1;
  909. }
  910. }
  911. /* Primary RFC6125 reference identifier */
  912. if (!X509_VERIFY_PARAM_set1_host(s->param, basedomain, 0)) {
  913. ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
  914. return -1;
  915. }
  916. dane->mdpth = -1;
  917. dane->pdpth = -1;
  918. dane->dctx = &s->ctx->dane;
  919. dane->trecs = sk_danetls_record_new_null();
  920. if (dane->trecs == NULL) {
  921. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  922. return -1;
  923. }
  924. return 1;
  925. }
  926. unsigned long SSL_dane_set_flags(SSL *ssl, unsigned long flags)
  927. {
  928. unsigned long orig = ssl->dane.flags;
  929. ssl->dane.flags |= flags;
  930. return orig;
  931. }
  932. unsigned long SSL_dane_clear_flags(SSL *ssl, unsigned long flags)
  933. {
  934. unsigned long orig = ssl->dane.flags;
  935. ssl->dane.flags &= ~flags;
  936. return orig;
  937. }
  938. int SSL_get0_dane_authority(SSL *s, X509 **mcert, EVP_PKEY **mspki)
  939. {
  940. SSL_DANE *dane = &s->dane;
  941. if (!DANETLS_ENABLED(dane) || s->verify_result != X509_V_OK)
  942. return -1;
  943. if (dane->mtlsa) {
  944. if (mcert)
  945. *mcert = dane->mcert;
  946. if (mspki)
  947. *mspki = (dane->mcert == NULL) ? dane->mtlsa->spki : NULL;
  948. }
  949. return dane->mdpth;
  950. }
  951. int SSL_get0_dane_tlsa(SSL *s, uint8_t *usage, uint8_t *selector,
  952. uint8_t *mtype, const unsigned char **data, size_t *dlen)
  953. {
  954. SSL_DANE *dane = &s->dane;
  955. if (!DANETLS_ENABLED(dane) || s->verify_result != X509_V_OK)
  956. return -1;
  957. if (dane->mtlsa) {
  958. if (usage)
  959. *usage = dane->mtlsa->usage;
  960. if (selector)
  961. *selector = dane->mtlsa->selector;
  962. if (mtype)
  963. *mtype = dane->mtlsa->mtype;
  964. if (data)
  965. *data = dane->mtlsa->data;
  966. if (dlen)
  967. *dlen = dane->mtlsa->dlen;
  968. }
  969. return dane->mdpth;
  970. }
  971. SSL_DANE *SSL_get0_dane(SSL *s)
  972. {
  973. return &s->dane;
  974. }
  975. int SSL_dane_tlsa_add(SSL *s, uint8_t usage, uint8_t selector,
  976. uint8_t mtype, const unsigned char *data, size_t dlen)
  977. {
  978. return dane_tlsa_add(&s->dane, usage, selector, mtype, data, dlen);
  979. }
  980. int SSL_CTX_dane_mtype_set(SSL_CTX *ctx, const EVP_MD *md, uint8_t mtype,
  981. uint8_t ord)
  982. {
  983. return dane_mtype_set(&ctx->dane, md, mtype, ord);
  984. }
  985. int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
  986. {
  987. return X509_VERIFY_PARAM_set1(ctx->param, vpm);
  988. }
  989. int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
  990. {
  991. return X509_VERIFY_PARAM_set1(ssl->param, vpm);
  992. }
  993. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
  994. {
  995. return ctx->param;
  996. }
  997. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
  998. {
  999. return ssl->param;
  1000. }
  1001. void SSL_certs_clear(SSL *s)
  1002. {
  1003. ssl_cert_clear_certs(s->cert);
  1004. }
  1005. void SSL_free(SSL *s)
  1006. {
  1007. int i;
  1008. if (s == NULL)
  1009. return;
  1010. CRYPTO_DOWN_REF(&s->references, &i, s->lock);
  1011. REF_PRINT_COUNT("SSL", s);
  1012. if (i > 0)
  1013. return;
  1014. REF_ASSERT_ISNT(i < 0);
  1015. X509_VERIFY_PARAM_free(s->param);
  1016. dane_final(&s->dane);
  1017. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
  1018. RECORD_LAYER_release(&s->rlayer);
  1019. /* Ignore return value */
  1020. ssl_free_wbio_buffer(s);
  1021. BIO_free_all(s->wbio);
  1022. s->wbio = NULL;
  1023. BIO_free_all(s->rbio);
  1024. s->rbio = NULL;
  1025. BUF_MEM_free(s->init_buf);
  1026. /* add extra stuff */
  1027. sk_SSL_CIPHER_free(s->cipher_list);
  1028. sk_SSL_CIPHER_free(s->cipher_list_by_id);
  1029. sk_SSL_CIPHER_free(s->tls13_ciphersuites);
  1030. sk_SSL_CIPHER_free(s->peer_ciphers);
  1031. /* Make the next call work :-) */
  1032. if (s->session != NULL) {
  1033. ssl_clear_bad_session(s);
  1034. SSL_SESSION_free(s->session);
  1035. }
  1036. SSL_SESSION_free(s->psksession);
  1037. OPENSSL_free(s->psksession_id);
  1038. clear_ciphers(s);
  1039. ssl_cert_free(s->cert);
  1040. OPENSSL_free(s->shared_sigalgs);
  1041. /* Free up if allocated */
  1042. OPENSSL_free(s->ext.hostname);
  1043. SSL_CTX_free(s->session_ctx);
  1044. OPENSSL_free(s->ext.ecpointformats);
  1045. OPENSSL_free(s->ext.peer_ecpointformats);
  1046. OPENSSL_free(s->ext.supportedgroups);
  1047. OPENSSL_free(s->ext.peer_supportedgroups);
  1048. sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);
  1049. #ifndef OPENSSL_NO_OCSP
  1050. sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
  1051. #endif
  1052. #ifndef OPENSSL_NO_CT
  1053. SCT_LIST_free(s->scts);
  1054. OPENSSL_free(s->ext.scts);
  1055. #endif
  1056. OPENSSL_free(s->ext.ocsp.resp);
  1057. OPENSSL_free(s->ext.alpn);
  1058. OPENSSL_free(s->ext.tls13_cookie);
  1059. if (s->clienthello != NULL)
  1060. OPENSSL_free(s->clienthello->pre_proc_exts);
  1061. OPENSSL_free(s->clienthello);
  1062. OPENSSL_free(s->pha_context);
  1063. EVP_MD_CTX_free(s->pha_dgst);
  1064. sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);
  1065. sk_X509_NAME_pop_free(s->client_ca_names, X509_NAME_free);
  1066. sk_X509_pop_free(s->verified_chain, X509_free);
  1067. if (s->method != NULL)
  1068. s->method->ssl_free(s);
  1069. SSL_CTX_free(s->ctx);
  1070. ASYNC_WAIT_CTX_free(s->waitctx);
  1071. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  1072. OPENSSL_free(s->ext.npn);
  1073. #endif
  1074. #ifndef OPENSSL_NO_SRTP
  1075. sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
  1076. #endif
  1077. CRYPTO_THREAD_lock_free(s->lock);
  1078. OPENSSL_free(s);
  1079. }
  1080. void SSL_set0_rbio(SSL *s, BIO *rbio)
  1081. {
  1082. BIO_free_all(s->rbio);
  1083. s->rbio = rbio;
  1084. }
  1085. void SSL_set0_wbio(SSL *s, BIO *wbio)
  1086. {
  1087. /*
  1088. * If the output buffering BIO is still in place, remove it
  1089. */
  1090. if (s->bbio != NULL)
  1091. s->wbio = BIO_pop(s->wbio);
  1092. BIO_free_all(s->wbio);
  1093. s->wbio = wbio;
  1094. /* Re-attach |bbio| to the new |wbio|. */
  1095. if (s->bbio != NULL)
  1096. s->wbio = BIO_push(s->bbio, s->wbio);
  1097. }
  1098. void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
  1099. {
  1100. /*
  1101. * For historical reasons, this function has many different cases in
  1102. * ownership handling.
  1103. */
  1104. /* If nothing has changed, do nothing */
  1105. if (rbio == SSL_get_rbio(s) && wbio == SSL_get_wbio(s))
  1106. return;
  1107. /*
  1108. * If the two arguments are equal then one fewer reference is granted by the
  1109. * caller than we want to take
  1110. */
  1111. if (rbio != NULL && rbio == wbio)
  1112. BIO_up_ref(rbio);
  1113. /*
  1114. * If only the wbio is changed only adopt one reference.
  1115. */
  1116. if (rbio == SSL_get_rbio(s)) {
  1117. SSL_set0_wbio(s, wbio);
  1118. return;
  1119. }
  1120. /*
  1121. * There is an asymmetry here for historical reasons. If only the rbio is
  1122. * changed AND the rbio and wbio were originally different, then we only
  1123. * adopt one reference.
  1124. */
  1125. if (wbio == SSL_get_wbio(s) && SSL_get_rbio(s) != SSL_get_wbio(s)) {
  1126. SSL_set0_rbio(s, rbio);
  1127. return;
  1128. }
  1129. /* Otherwise, adopt both references. */
  1130. SSL_set0_rbio(s, rbio);
  1131. SSL_set0_wbio(s, wbio);
  1132. }
  1133. BIO *SSL_get_rbio(const SSL *s)
  1134. {
  1135. return s->rbio;
  1136. }
  1137. BIO *SSL_get_wbio(const SSL *s)
  1138. {
  1139. if (s->bbio != NULL) {
  1140. /*
  1141. * If |bbio| is active, the true caller-configured BIO is its
  1142. * |next_bio|.
  1143. */
  1144. return BIO_next(s->bbio);
  1145. }
  1146. return s->wbio;
  1147. }
  1148. int SSL_get_fd(const SSL *s)
  1149. {
  1150. return SSL_get_rfd(s);
  1151. }
  1152. int SSL_get_rfd(const SSL *s)
  1153. {
  1154. int ret = -1;
  1155. BIO *b, *r;
  1156. b = SSL_get_rbio(s);
  1157. r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
  1158. if (r != NULL)
  1159. BIO_get_fd(r, &ret);
  1160. return ret;
  1161. }
  1162. int SSL_get_wfd(const SSL *s)
  1163. {
  1164. int ret = -1;
  1165. BIO *b, *r;
  1166. b = SSL_get_wbio(s);
  1167. r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
  1168. if (r != NULL)
  1169. BIO_get_fd(r, &ret);
  1170. return ret;
  1171. }
  1172. #ifndef OPENSSL_NO_SOCK
  1173. int SSL_set_fd(SSL *s, int fd)
  1174. {
  1175. int ret = 0;
  1176. BIO *bio = NULL;
  1177. bio = BIO_new(BIO_s_socket());
  1178. if (bio == NULL) {
  1179. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  1180. goto err;
  1181. }
  1182. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1183. SSL_set_bio(s, bio, bio);
  1184. #ifndef OPENSSL_NO_KTLS
  1185. /*
  1186. * The new socket is created successfully regardless of ktls_enable.
  1187. * ktls_enable doesn't change any functionality of the socket, except
  1188. * changing the setsockopt to enable the processing of ktls_start.
  1189. * Thus, it is not a problem to call it for non-TLS sockets.
  1190. */
  1191. ktls_enable(fd);
  1192. #endif /* OPENSSL_NO_KTLS */
  1193. ret = 1;
  1194. err:
  1195. return ret;
  1196. }
  1197. int SSL_set_wfd(SSL *s, int fd)
  1198. {
  1199. BIO *rbio = SSL_get_rbio(s);
  1200. if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET
  1201. || (int)BIO_get_fd(rbio, NULL) != fd) {
  1202. BIO *bio = BIO_new(BIO_s_socket());
  1203. if (bio == NULL) {
  1204. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  1205. return 0;
  1206. }
  1207. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1208. SSL_set0_wbio(s, bio);
  1209. #ifndef OPENSSL_NO_KTLS
  1210. /*
  1211. * The new socket is created successfully regardless of ktls_enable.
  1212. * ktls_enable doesn't change any functionality of the socket, except
  1213. * changing the setsockopt to enable the processing of ktls_start.
  1214. * Thus, it is not a problem to call it for non-TLS sockets.
  1215. */
  1216. ktls_enable(fd);
  1217. #endif /* OPENSSL_NO_KTLS */
  1218. } else {
  1219. BIO_up_ref(rbio);
  1220. SSL_set0_wbio(s, rbio);
  1221. }
  1222. return 1;
  1223. }
  1224. int SSL_set_rfd(SSL *s, int fd)
  1225. {
  1226. BIO *wbio = SSL_get_wbio(s);
  1227. if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET
  1228. || ((int)BIO_get_fd(wbio, NULL) != fd)) {
  1229. BIO *bio = BIO_new(BIO_s_socket());
  1230. if (bio == NULL) {
  1231. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  1232. return 0;
  1233. }
  1234. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1235. SSL_set0_rbio(s, bio);
  1236. } else {
  1237. BIO_up_ref(wbio);
  1238. SSL_set0_rbio(s, wbio);
  1239. }
  1240. return 1;
  1241. }
  1242. #endif
  1243. /* return length of latest Finished message we sent, copy to 'buf' */
  1244. size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
  1245. {
  1246. size_t ret = 0;
  1247. ret = s->s3.tmp.finish_md_len;
  1248. if (count > ret)
  1249. count = ret;
  1250. memcpy(buf, s->s3.tmp.finish_md, count);
  1251. return ret;
  1252. }
  1253. /* return length of latest Finished message we expected, copy to 'buf' */
  1254. size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
  1255. {
  1256. size_t ret = 0;
  1257. ret = s->s3.tmp.peer_finish_md_len;
  1258. if (count > ret)
  1259. count = ret;
  1260. memcpy(buf, s->s3.tmp.peer_finish_md, count);
  1261. return ret;
  1262. }
  1263. int SSL_get_verify_mode(const SSL *s)
  1264. {
  1265. return s->verify_mode;
  1266. }
  1267. int SSL_get_verify_depth(const SSL *s)
  1268. {
  1269. return X509_VERIFY_PARAM_get_depth(s->param);
  1270. }
  1271. int (*SSL_get_verify_callback(const SSL *s)) (int, X509_STORE_CTX *) {
  1272. return s->verify_callback;
  1273. }
  1274. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
  1275. {
  1276. return ctx->verify_mode;
  1277. }
  1278. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
  1279. {
  1280. return X509_VERIFY_PARAM_get_depth(ctx->param);
  1281. }
  1282. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx)) (int, X509_STORE_CTX *) {
  1283. return ctx->default_verify_callback;
  1284. }
  1285. void SSL_set_verify(SSL *s, int mode,
  1286. int (*callback) (int ok, X509_STORE_CTX *ctx))
  1287. {
  1288. s->verify_mode = mode;
  1289. if (callback != NULL)
  1290. s->verify_callback = callback;
  1291. }
  1292. void SSL_set_verify_depth(SSL *s, int depth)
  1293. {
  1294. X509_VERIFY_PARAM_set_depth(s->param, depth);
  1295. }
  1296. void SSL_set_read_ahead(SSL *s, int yes)
  1297. {
  1298. RECORD_LAYER_set_read_ahead(&s->rlayer, yes);
  1299. }
  1300. int SSL_get_read_ahead(const SSL *s)
  1301. {
  1302. return RECORD_LAYER_get_read_ahead(&s->rlayer);
  1303. }
  1304. int SSL_pending(const SSL *s)
  1305. {
  1306. size_t pending = s->method->ssl_pending(s);
  1307. /*
  1308. * SSL_pending cannot work properly if read-ahead is enabled
  1309. * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
  1310. * impossible to fix since SSL_pending cannot report errors that may be
  1311. * observed while scanning the new data. (Note that SSL_pending() is
  1312. * often used as a boolean value, so we'd better not return -1.)
  1313. *
  1314. * SSL_pending also cannot work properly if the value >INT_MAX. In that case
  1315. * we just return INT_MAX.
  1316. */
  1317. return pending < INT_MAX ? (int)pending : INT_MAX;
  1318. }
  1319. int SSL_has_pending(const SSL *s)
  1320. {
  1321. /*
  1322. * Similar to SSL_pending() but returns a 1 to indicate that we have
  1323. * unprocessed data available or 0 otherwise (as opposed to the number of
  1324. * bytes available). Unlike SSL_pending() this will take into account
  1325. * read_ahead data. A 1 return simply indicates that we have unprocessed
  1326. * data. That data may not result in any application data, or we may fail
  1327. * to parse the records for some reason.
  1328. */
  1329. if (RECORD_LAYER_processed_read_pending(&s->rlayer))
  1330. return 1;
  1331. return RECORD_LAYER_read_pending(&s->rlayer);
  1332. }
  1333. X509 *SSL_get1_peer_certificate(const SSL *s)
  1334. {
  1335. X509 *r = SSL_get0_peer_certificate(s);
  1336. if (r != NULL)
  1337. X509_up_ref(r);
  1338. return r;
  1339. }
  1340. X509 *SSL_get0_peer_certificate(const SSL *s)
  1341. {
  1342. if ((s == NULL) || (s->session == NULL))
  1343. return NULL;
  1344. else
  1345. return s->session->peer;
  1346. }
  1347. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
  1348. {
  1349. STACK_OF(X509) *r;
  1350. if ((s == NULL) || (s->session == NULL))
  1351. r = NULL;
  1352. else
  1353. r = s->session->peer_chain;
  1354. /*
  1355. * If we are a client, cert_chain includes the peer's own certificate; if
  1356. * we are a server, it does not.
  1357. */
  1358. return r;
  1359. }
  1360. /*
  1361. * Now in theory, since the calling process own 't' it should be safe to
  1362. * modify. We need to be able to read f without being hassled
  1363. */
  1364. int SSL_copy_session_id(SSL *t, const SSL *f)
  1365. {
  1366. int i;
  1367. /* Do we need to do SSL locking? */
  1368. if (!SSL_set_session(t, SSL_get_session(f))) {
  1369. return 0;
  1370. }
  1371. /*
  1372. * what if we are setup for one protocol version but want to talk another
  1373. */
  1374. if (t->method != f->method) {
  1375. t->method->ssl_free(t);
  1376. t->method = f->method;
  1377. if (t->method->ssl_new(t) == 0)
  1378. return 0;
  1379. }
  1380. CRYPTO_UP_REF(&f->cert->references, &i, f->cert->lock);
  1381. ssl_cert_free(t->cert);
  1382. t->cert = f->cert;
  1383. if (!SSL_set_session_id_context(t, f->sid_ctx, (int)f->sid_ctx_length)) {
  1384. return 0;
  1385. }
  1386. return 1;
  1387. }
  1388. /* Fix this so it checks all the valid key/cert options */
  1389. int SSL_CTX_check_private_key(const SSL_CTX *ctx)
  1390. {
  1391. if ((ctx == NULL) || (ctx->cert->key->x509 == NULL)) {
  1392. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  1393. return 0;
  1394. }
  1395. if (ctx->cert->key->privatekey == NULL) {
  1396. ERR_raise(ERR_LIB_SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1397. return 0;
  1398. }
  1399. return X509_check_private_key
  1400. (ctx->cert->key->x509, ctx->cert->key->privatekey);
  1401. }
  1402. /* Fix this function so that it takes an optional type parameter */
  1403. int SSL_check_private_key(const SSL *ssl)
  1404. {
  1405. if (ssl == NULL) {
  1406. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  1407. return 0;
  1408. }
  1409. if (ssl->cert->key->x509 == NULL) {
  1410. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  1411. return 0;
  1412. }
  1413. if (ssl->cert->key->privatekey == NULL) {
  1414. ERR_raise(ERR_LIB_SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1415. return 0;
  1416. }
  1417. return X509_check_private_key(ssl->cert->key->x509,
  1418. ssl->cert->key->privatekey);
  1419. }
  1420. int SSL_waiting_for_async(SSL *s)
  1421. {
  1422. if (s->job)
  1423. return 1;
  1424. return 0;
  1425. }
  1426. int SSL_get_all_async_fds(SSL *s, OSSL_ASYNC_FD *fds, size_t *numfds)
  1427. {
  1428. ASYNC_WAIT_CTX *ctx = s->waitctx;
  1429. if (ctx == NULL)
  1430. return 0;
  1431. return ASYNC_WAIT_CTX_get_all_fds(ctx, fds, numfds);
  1432. }
  1433. int SSL_get_changed_async_fds(SSL *s, OSSL_ASYNC_FD *addfd, size_t *numaddfds,
  1434. OSSL_ASYNC_FD *delfd, size_t *numdelfds)
  1435. {
  1436. ASYNC_WAIT_CTX *ctx = s->waitctx;
  1437. if (ctx == NULL)
  1438. return 0;
  1439. return ASYNC_WAIT_CTX_get_changed_fds(ctx, addfd, numaddfds, delfd,
  1440. numdelfds);
  1441. }
  1442. int SSL_CTX_set_async_callback(SSL_CTX *ctx, SSL_async_callback_fn callback)
  1443. {
  1444. ctx->async_cb = callback;
  1445. return 1;
  1446. }
  1447. int SSL_CTX_set_async_callback_arg(SSL_CTX *ctx, void *arg)
  1448. {
  1449. ctx->async_cb_arg = arg;
  1450. return 1;
  1451. }
  1452. int SSL_set_async_callback(SSL *s, SSL_async_callback_fn callback)
  1453. {
  1454. s->async_cb = callback;
  1455. return 1;
  1456. }
  1457. int SSL_set_async_callback_arg(SSL *s, void *arg)
  1458. {
  1459. s->async_cb_arg = arg;
  1460. return 1;
  1461. }
  1462. int SSL_get_async_status(SSL *s, int *status)
  1463. {
  1464. ASYNC_WAIT_CTX *ctx = s->waitctx;
  1465. if (ctx == NULL)
  1466. return 0;
  1467. *status = ASYNC_WAIT_CTX_get_status(ctx);
  1468. return 1;
  1469. }
  1470. int SSL_accept(SSL *s)
  1471. {
  1472. if (s->handshake_func == NULL) {
  1473. /* Not properly initialized yet */
  1474. SSL_set_accept_state(s);
  1475. }
  1476. return SSL_do_handshake(s);
  1477. }
  1478. int SSL_connect(SSL *s)
  1479. {
  1480. if (s->handshake_func == NULL) {
  1481. /* Not properly initialized yet */
  1482. SSL_set_connect_state(s);
  1483. }
  1484. return SSL_do_handshake(s);
  1485. }
  1486. long SSL_get_default_timeout(const SSL *s)
  1487. {
  1488. return s->method->get_timeout();
  1489. }
  1490. static int ssl_async_wait_ctx_cb(void *arg)
  1491. {
  1492. SSL *s = (SSL *)arg;
  1493. return s->async_cb(s, s->async_cb_arg);
  1494. }
  1495. static int ssl_start_async_job(SSL *s, struct ssl_async_args *args,
  1496. int (*func) (void *))
  1497. {
  1498. int ret;
  1499. if (s->waitctx == NULL) {
  1500. s->waitctx = ASYNC_WAIT_CTX_new();
  1501. if (s->waitctx == NULL)
  1502. return -1;
  1503. if (s->async_cb != NULL
  1504. && !ASYNC_WAIT_CTX_set_callback
  1505. (s->waitctx, ssl_async_wait_ctx_cb, s))
  1506. return -1;
  1507. }
  1508. switch (ASYNC_start_job(&s->job, s->waitctx, &ret, func, args,
  1509. sizeof(struct ssl_async_args))) {
  1510. case ASYNC_ERR:
  1511. s->rwstate = SSL_NOTHING;
  1512. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_INIT_ASYNC);
  1513. return -1;
  1514. case ASYNC_PAUSE:
  1515. s->rwstate = SSL_ASYNC_PAUSED;
  1516. return -1;
  1517. case ASYNC_NO_JOBS:
  1518. s->rwstate = SSL_ASYNC_NO_JOBS;
  1519. return -1;
  1520. case ASYNC_FINISH:
  1521. s->job = NULL;
  1522. return ret;
  1523. default:
  1524. s->rwstate = SSL_NOTHING;
  1525. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  1526. /* Shouldn't happen */
  1527. return -1;
  1528. }
  1529. }
  1530. static int ssl_io_intern(void *vargs)
  1531. {
  1532. struct ssl_async_args *args;
  1533. SSL *s;
  1534. void *buf;
  1535. size_t num;
  1536. args = (struct ssl_async_args *)vargs;
  1537. s = args->s;
  1538. buf = args->buf;
  1539. num = args->num;
  1540. switch (args->type) {
  1541. case READFUNC:
  1542. return args->f.func_read(s, buf, num, &s->asyncrw);
  1543. case WRITEFUNC:
  1544. return args->f.func_write(s, buf, num, &s->asyncrw);
  1545. case OTHERFUNC:
  1546. return args->f.func_other(s);
  1547. }
  1548. return -1;
  1549. }
  1550. int ssl_read_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
  1551. {
  1552. if (s->handshake_func == NULL) {
  1553. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  1554. return -1;
  1555. }
  1556. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  1557. s->rwstate = SSL_NOTHING;
  1558. return 0;
  1559. }
  1560. if (s->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
  1561. || s->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY) {
  1562. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1563. return 0;
  1564. }
  1565. /*
  1566. * If we are a client and haven't received the ServerHello etc then we
  1567. * better do that
  1568. */
  1569. ossl_statem_check_finish_init(s, 0);
  1570. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1571. struct ssl_async_args args;
  1572. int ret;
  1573. args.s = s;
  1574. args.buf = buf;
  1575. args.num = num;
  1576. args.type = READFUNC;
  1577. args.f.func_read = s->method->ssl_read;
  1578. ret = ssl_start_async_job(s, &args, ssl_io_intern);
  1579. *readbytes = s->asyncrw;
  1580. return ret;
  1581. } else {
  1582. return s->method->ssl_read(s, buf, num, readbytes);
  1583. }
  1584. }
  1585. int SSL_read(SSL *s, void *buf, int num)
  1586. {
  1587. int ret;
  1588. size_t readbytes;
  1589. if (num < 0) {
  1590. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
  1591. return -1;
  1592. }
  1593. ret = ssl_read_internal(s, buf, (size_t)num, &readbytes);
  1594. /*
  1595. * The cast is safe here because ret should be <= INT_MAX because num is
  1596. * <= INT_MAX
  1597. */
  1598. if (ret > 0)
  1599. ret = (int)readbytes;
  1600. return ret;
  1601. }
  1602. int SSL_read_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
  1603. {
  1604. int ret = ssl_read_internal(s, buf, num, readbytes);
  1605. if (ret < 0)
  1606. ret = 0;
  1607. return ret;
  1608. }
  1609. int SSL_read_early_data(SSL *s, void *buf, size_t num, size_t *readbytes)
  1610. {
  1611. int ret;
  1612. if (!s->server) {
  1613. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1614. return SSL_READ_EARLY_DATA_ERROR;
  1615. }
  1616. switch (s->early_data_state) {
  1617. case SSL_EARLY_DATA_NONE:
  1618. if (!SSL_in_before(s)) {
  1619. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1620. return SSL_READ_EARLY_DATA_ERROR;
  1621. }
  1622. /* fall through */
  1623. case SSL_EARLY_DATA_ACCEPT_RETRY:
  1624. s->early_data_state = SSL_EARLY_DATA_ACCEPTING;
  1625. ret = SSL_accept(s);
  1626. if (ret <= 0) {
  1627. /* NBIO or error */
  1628. s->early_data_state = SSL_EARLY_DATA_ACCEPT_RETRY;
  1629. return SSL_READ_EARLY_DATA_ERROR;
  1630. }
  1631. /* fall through */
  1632. case SSL_EARLY_DATA_READ_RETRY:
  1633. if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
  1634. s->early_data_state = SSL_EARLY_DATA_READING;
  1635. ret = SSL_read_ex(s, buf, num, readbytes);
  1636. /*
  1637. * State machine will update early_data_state to
  1638. * SSL_EARLY_DATA_FINISHED_READING if we get an EndOfEarlyData
  1639. * message
  1640. */
  1641. if (ret > 0 || (ret <= 0 && s->early_data_state
  1642. != SSL_EARLY_DATA_FINISHED_READING)) {
  1643. s->early_data_state = SSL_EARLY_DATA_READ_RETRY;
  1644. return ret > 0 ? SSL_READ_EARLY_DATA_SUCCESS
  1645. : SSL_READ_EARLY_DATA_ERROR;
  1646. }
  1647. } else {
  1648. s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
  1649. }
  1650. *readbytes = 0;
  1651. return SSL_READ_EARLY_DATA_FINISH;
  1652. default:
  1653. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1654. return SSL_READ_EARLY_DATA_ERROR;
  1655. }
  1656. }
  1657. int SSL_get_early_data_status(const SSL *s)
  1658. {
  1659. return s->ext.early_data;
  1660. }
  1661. static int ssl_peek_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
  1662. {
  1663. if (s->handshake_func == NULL) {
  1664. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  1665. return -1;
  1666. }
  1667. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  1668. return 0;
  1669. }
  1670. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1671. struct ssl_async_args args;
  1672. int ret;
  1673. args.s = s;
  1674. args.buf = buf;
  1675. args.num = num;
  1676. args.type = READFUNC;
  1677. args.f.func_read = s->method->ssl_peek;
  1678. ret = ssl_start_async_job(s, &args, ssl_io_intern);
  1679. *readbytes = s->asyncrw;
  1680. return ret;
  1681. } else {
  1682. return s->method->ssl_peek(s, buf, num, readbytes);
  1683. }
  1684. }
  1685. int SSL_peek(SSL *s, void *buf, int num)
  1686. {
  1687. int ret;
  1688. size_t readbytes;
  1689. if (num < 0) {
  1690. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
  1691. return -1;
  1692. }
  1693. ret = ssl_peek_internal(s, buf, (size_t)num, &readbytes);
  1694. /*
  1695. * The cast is safe here because ret should be <= INT_MAX because num is
  1696. * <= INT_MAX
  1697. */
  1698. if (ret > 0)
  1699. ret = (int)readbytes;
  1700. return ret;
  1701. }
  1702. int SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
  1703. {
  1704. int ret = ssl_peek_internal(s, buf, num, readbytes);
  1705. if (ret < 0)
  1706. ret = 0;
  1707. return ret;
  1708. }
  1709. int ssl_write_internal(SSL *s, const void *buf, size_t num, size_t *written)
  1710. {
  1711. if (s->handshake_func == NULL) {
  1712. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  1713. return -1;
  1714. }
  1715. if (s->shutdown & SSL_SENT_SHUTDOWN) {
  1716. s->rwstate = SSL_NOTHING;
  1717. ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  1718. return -1;
  1719. }
  1720. if (s->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
  1721. || s->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY
  1722. || s->early_data_state == SSL_EARLY_DATA_READ_RETRY) {
  1723. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1724. return 0;
  1725. }
  1726. /* If we are a client and haven't sent the Finished we better do that */
  1727. ossl_statem_check_finish_init(s, 1);
  1728. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1729. int ret;
  1730. struct ssl_async_args args;
  1731. args.s = s;
  1732. args.buf = (void *)buf;
  1733. args.num = num;
  1734. args.type = WRITEFUNC;
  1735. args.f.func_write = s->method->ssl_write;
  1736. ret = ssl_start_async_job(s, &args, ssl_io_intern);
  1737. *written = s->asyncrw;
  1738. return ret;
  1739. } else {
  1740. return s->method->ssl_write(s, buf, num, written);
  1741. }
  1742. }
  1743. ossl_ssize_t SSL_sendfile(SSL *s, int fd, off_t offset, size_t size, int flags)
  1744. {
  1745. ossl_ssize_t ret;
  1746. if (s->handshake_func == NULL) {
  1747. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  1748. return -1;
  1749. }
  1750. if (s->shutdown & SSL_SENT_SHUTDOWN) {
  1751. s->rwstate = SSL_NOTHING;
  1752. ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  1753. return -1;
  1754. }
  1755. if (!BIO_get_ktls_send(s->wbio)) {
  1756. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  1757. return -1;
  1758. }
  1759. /* If we have an alert to send, lets send it */
  1760. if (s->s3.alert_dispatch) {
  1761. ret = (ossl_ssize_t)s->method->ssl_dispatch_alert(s);
  1762. if (ret <= 0) {
  1763. /* SSLfatal() already called if appropriate */
  1764. return ret;
  1765. }
  1766. /* if it went, fall through and send more stuff */
  1767. }
  1768. s->rwstate = SSL_WRITING;
  1769. if (BIO_flush(s->wbio) <= 0) {
  1770. if (!BIO_should_retry(s->wbio)) {
  1771. s->rwstate = SSL_NOTHING;
  1772. } else {
  1773. #ifdef EAGAIN
  1774. set_sys_error(EAGAIN);
  1775. #endif
  1776. }
  1777. return -1;
  1778. }
  1779. #ifdef OPENSSL_NO_KTLS
  1780. ERR_raise_data(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR,
  1781. "can't call ktls_sendfile(), ktls disabled");
  1782. return -1;
  1783. #else
  1784. ret = ktls_sendfile(SSL_get_wfd(s), fd, offset, size, flags);
  1785. if (ret < 0) {
  1786. #if defined(EAGAIN) && defined(EINTR) && defined(EBUSY)
  1787. if ((get_last_sys_error() == EAGAIN) ||
  1788. (get_last_sys_error() == EINTR) ||
  1789. (get_last_sys_error() == EBUSY))
  1790. BIO_set_retry_write(s->wbio);
  1791. else
  1792. #endif
  1793. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  1794. return ret;
  1795. }
  1796. s->rwstate = SSL_NOTHING;
  1797. return ret;
  1798. #endif
  1799. }
  1800. int SSL_write(SSL *s, const void *buf, int num)
  1801. {
  1802. int ret;
  1803. size_t written;
  1804. if (num < 0) {
  1805. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
  1806. return -1;
  1807. }
  1808. ret = ssl_write_internal(s, buf, (size_t)num, &written);
  1809. /*
  1810. * The cast is safe here because ret should be <= INT_MAX because num is
  1811. * <= INT_MAX
  1812. */
  1813. if (ret > 0)
  1814. ret = (int)written;
  1815. return ret;
  1816. }
  1817. int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written)
  1818. {
  1819. int ret = ssl_write_internal(s, buf, num, written);
  1820. if (ret < 0)
  1821. ret = 0;
  1822. return ret;
  1823. }
  1824. int SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written)
  1825. {
  1826. int ret, early_data_state;
  1827. size_t writtmp;
  1828. uint32_t partialwrite;
  1829. switch (s->early_data_state) {
  1830. case SSL_EARLY_DATA_NONE:
  1831. if (s->server
  1832. || !SSL_in_before(s)
  1833. || ((s->session == NULL || s->session->ext.max_early_data == 0)
  1834. && (s->psk_use_session_cb == NULL))) {
  1835. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1836. return 0;
  1837. }
  1838. /* fall through */
  1839. case SSL_EARLY_DATA_CONNECT_RETRY:
  1840. s->early_data_state = SSL_EARLY_DATA_CONNECTING;
  1841. ret = SSL_connect(s);
  1842. if (ret <= 0) {
  1843. /* NBIO or error */
  1844. s->early_data_state = SSL_EARLY_DATA_CONNECT_RETRY;
  1845. return 0;
  1846. }
  1847. /* fall through */
  1848. case SSL_EARLY_DATA_WRITE_RETRY:
  1849. s->early_data_state = SSL_EARLY_DATA_WRITING;
  1850. /*
  1851. * We disable partial write for early data because we don't keep track
  1852. * of how many bytes we've written between the SSL_write_ex() call and
  1853. * the flush if the flush needs to be retried)
  1854. */
  1855. partialwrite = s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE;
  1856. s->mode &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
  1857. ret = SSL_write_ex(s, buf, num, &writtmp);
  1858. s->mode |= partialwrite;
  1859. if (!ret) {
  1860. s->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
  1861. return ret;
  1862. }
  1863. s->early_data_state = SSL_EARLY_DATA_WRITE_FLUSH;
  1864. /* fall through */
  1865. case SSL_EARLY_DATA_WRITE_FLUSH:
  1866. /* The buffering BIO is still in place so we need to flush it */
  1867. if (statem_flush(s) != 1)
  1868. return 0;
  1869. *written = num;
  1870. s->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
  1871. return 1;
  1872. case SSL_EARLY_DATA_FINISHED_READING:
  1873. case SSL_EARLY_DATA_READ_RETRY:
  1874. early_data_state = s->early_data_state;
  1875. /* We are a server writing to an unauthenticated client */
  1876. s->early_data_state = SSL_EARLY_DATA_UNAUTH_WRITING;
  1877. ret = SSL_write_ex(s, buf, num, written);
  1878. /* The buffering BIO is still in place */
  1879. if (ret)
  1880. (void)BIO_flush(s->wbio);
  1881. s->early_data_state = early_data_state;
  1882. return ret;
  1883. default:
  1884. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1885. return 0;
  1886. }
  1887. }
  1888. int SSL_shutdown(SSL *s)
  1889. {
  1890. /*
  1891. * Note that this function behaves differently from what one might
  1892. * expect. Return values are 0 for no success (yet), 1 for success; but
  1893. * calling it once is usually not enough, even if blocking I/O is used
  1894. * (see ssl3_shutdown).
  1895. */
  1896. if (s->handshake_func == NULL) {
  1897. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  1898. return -1;
  1899. }
  1900. if (!SSL_in_init(s)) {
  1901. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1902. struct ssl_async_args args;
  1903. args.s = s;
  1904. args.type = OTHERFUNC;
  1905. args.f.func_other = s->method->ssl_shutdown;
  1906. return ssl_start_async_job(s, &args, ssl_io_intern);
  1907. } else {
  1908. return s->method->ssl_shutdown(s);
  1909. }
  1910. } else {
  1911. ERR_raise(ERR_LIB_SSL, SSL_R_SHUTDOWN_WHILE_IN_INIT);
  1912. return -1;
  1913. }
  1914. }
  1915. int SSL_key_update(SSL *s, int updatetype)
  1916. {
  1917. if (!SSL_IS_TLS13(s)) {
  1918. ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
  1919. return 0;
  1920. }
  1921. if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
  1922. && updatetype != SSL_KEY_UPDATE_REQUESTED) {
  1923. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_KEY_UPDATE_TYPE);
  1924. return 0;
  1925. }
  1926. if (!SSL_is_init_finished(s)) {
  1927. ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT);
  1928. return 0;
  1929. }
  1930. ossl_statem_set_in_init(s, 1);
  1931. s->key_update = updatetype;
  1932. return 1;
  1933. }
  1934. int SSL_get_key_update_type(const SSL *s)
  1935. {
  1936. return s->key_update;
  1937. }
  1938. /*
  1939. * Can we accept a renegotiation request? If yes, set the flag and
  1940. * return 1 if yes. If not, raise error and return 0.
  1941. */
  1942. static int can_renegotiate(const SSL *s)
  1943. {
  1944. if (SSL_IS_TLS13(s)) {
  1945. ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
  1946. return 0;
  1947. }
  1948. if ((s->options & SSL_OP_NO_RENEGOTIATION) != 0) {
  1949. ERR_raise(ERR_LIB_SSL, SSL_R_NO_RENEGOTIATION);
  1950. return 0;
  1951. }
  1952. return 1;
  1953. }
  1954. int SSL_renegotiate(SSL *s)
  1955. {
  1956. if (!can_renegotiate(s))
  1957. return 0;
  1958. s->renegotiate = 1;
  1959. s->new_session = 1;
  1960. return s->method->ssl_renegotiate(s);
  1961. }
  1962. int SSL_renegotiate_abbreviated(SSL *s)
  1963. {
  1964. if (!can_renegotiate(s))
  1965. return 0;
  1966. s->renegotiate = 1;
  1967. s->new_session = 0;
  1968. return s->method->ssl_renegotiate(s);
  1969. }
  1970. int SSL_renegotiate_pending(const SSL *s)
  1971. {
  1972. /*
  1973. * becomes true when negotiation is requested; false again once a
  1974. * handshake has finished
  1975. */
  1976. return (s->renegotiate != 0);
  1977. }
  1978. int SSL_new_session_ticket(SSL *s)
  1979. {
  1980. /* If we are in init because we're sending tickets, okay to send more. */
  1981. if ((SSL_in_init(s) && s->ext.extra_tickets_expected == 0)
  1982. || SSL_IS_FIRST_HANDSHAKE(s) || !s->server
  1983. || !SSL_IS_TLS13(s))
  1984. return 0;
  1985. s->ext.extra_tickets_expected++;
  1986. if (s->rlayer.wbuf[0].left == 0 && !SSL_in_init(s))
  1987. ossl_statem_set_in_init(s, 1);
  1988. return 1;
  1989. }
  1990. long SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
  1991. {
  1992. long l;
  1993. switch (cmd) {
  1994. case SSL_CTRL_GET_READ_AHEAD:
  1995. return RECORD_LAYER_get_read_ahead(&s->rlayer);
  1996. case SSL_CTRL_SET_READ_AHEAD:
  1997. l = RECORD_LAYER_get_read_ahead(&s->rlayer);
  1998. RECORD_LAYER_set_read_ahead(&s->rlayer, larg);
  1999. return l;
  2000. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  2001. s->msg_callback_arg = parg;
  2002. return 1;
  2003. case SSL_CTRL_MODE:
  2004. return (s->mode |= larg);
  2005. case SSL_CTRL_CLEAR_MODE:
  2006. return (s->mode &= ~larg);
  2007. case SSL_CTRL_GET_MAX_CERT_LIST:
  2008. return (long)s->max_cert_list;
  2009. case SSL_CTRL_SET_MAX_CERT_LIST:
  2010. if (larg < 0)
  2011. return 0;
  2012. l = (long)s->max_cert_list;
  2013. s->max_cert_list = (size_t)larg;
  2014. return l;
  2015. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  2016. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
  2017. return 0;
  2018. #ifndef OPENSSL_NO_KTLS
  2019. if (s->wbio != NULL && BIO_get_ktls_send(s->wbio))
  2020. return 0;
  2021. #endif /* OPENSSL_NO_KTLS */
  2022. s->max_send_fragment = larg;
  2023. if (s->max_send_fragment < s->split_send_fragment)
  2024. s->split_send_fragment = s->max_send_fragment;
  2025. return 1;
  2026. case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
  2027. if ((size_t)larg > s->max_send_fragment || larg == 0)
  2028. return 0;
  2029. s->split_send_fragment = larg;
  2030. return 1;
  2031. case SSL_CTRL_SET_MAX_PIPELINES:
  2032. if (larg < 1 || larg > SSL_MAX_PIPELINES)
  2033. return 0;
  2034. s->max_pipelines = larg;
  2035. if (larg > 1)
  2036. RECORD_LAYER_set_read_ahead(&s->rlayer, 1);
  2037. return 1;
  2038. case SSL_CTRL_GET_RI_SUPPORT:
  2039. return s->s3.send_connection_binding;
  2040. case SSL_CTRL_CERT_FLAGS:
  2041. return (s->cert->cert_flags |= larg);
  2042. case SSL_CTRL_CLEAR_CERT_FLAGS:
  2043. return (s->cert->cert_flags &= ~larg);
  2044. case SSL_CTRL_GET_RAW_CIPHERLIST:
  2045. if (parg) {
  2046. if (s->s3.tmp.ciphers_raw == NULL)
  2047. return 0;
  2048. *(unsigned char **)parg = s->s3.tmp.ciphers_raw;
  2049. return (int)s->s3.tmp.ciphers_rawlen;
  2050. } else {
  2051. return TLS_CIPHER_LEN;
  2052. }
  2053. case SSL_CTRL_GET_EXTMS_SUPPORT:
  2054. if (!s->session || SSL_in_init(s) || ossl_statem_get_in_handshake(s))
  2055. return -1;
  2056. if (s->session->flags & SSL_SESS_FLAG_EXTMS)
  2057. return 1;
  2058. else
  2059. return 0;
  2060. case SSL_CTRL_SET_MIN_PROTO_VERSION:
  2061. return ssl_check_allowed_versions(larg, s->max_proto_version)
  2062. && ssl_set_version_bound(s->ctx->method->version, (int)larg,
  2063. &s->min_proto_version);
  2064. case SSL_CTRL_GET_MIN_PROTO_VERSION:
  2065. return s->min_proto_version;
  2066. case SSL_CTRL_SET_MAX_PROTO_VERSION:
  2067. return ssl_check_allowed_versions(s->min_proto_version, larg)
  2068. && ssl_set_version_bound(s->ctx->method->version, (int)larg,
  2069. &s->max_proto_version);
  2070. case SSL_CTRL_GET_MAX_PROTO_VERSION:
  2071. return s->max_proto_version;
  2072. default:
  2073. return s->method->ssl_ctrl(s, cmd, larg, parg);
  2074. }
  2075. }
  2076. long SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
  2077. {
  2078. switch (cmd) {
  2079. case SSL_CTRL_SET_MSG_CALLBACK:
  2080. s->msg_callback = (void (*)
  2081. (int write_p, int version, int content_type,
  2082. const void *buf, size_t len, SSL *ssl,
  2083. void *arg))(fp);
  2084. return 1;
  2085. default:
  2086. return s->method->ssl_callback_ctrl(s, cmd, fp);
  2087. }
  2088. }
  2089. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
  2090. {
  2091. return ctx->sessions;
  2092. }
  2093. long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
  2094. {
  2095. long l;
  2096. /* For some cases with ctx == NULL perform syntax checks */
  2097. if (ctx == NULL) {
  2098. switch (cmd) {
  2099. case SSL_CTRL_SET_GROUPS_LIST:
  2100. return tls1_set_groups_list(ctx, NULL, NULL, parg);
  2101. case SSL_CTRL_SET_SIGALGS_LIST:
  2102. case SSL_CTRL_SET_CLIENT_SIGALGS_LIST:
  2103. return tls1_set_sigalgs_list(NULL, parg, 0);
  2104. default:
  2105. return 0;
  2106. }
  2107. }
  2108. switch (cmd) {
  2109. case SSL_CTRL_GET_READ_AHEAD:
  2110. return ctx->read_ahead;
  2111. case SSL_CTRL_SET_READ_AHEAD:
  2112. l = ctx->read_ahead;
  2113. ctx->read_ahead = larg;
  2114. return l;
  2115. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  2116. ctx->msg_callback_arg = parg;
  2117. return 1;
  2118. case SSL_CTRL_GET_MAX_CERT_LIST:
  2119. return (long)ctx->max_cert_list;
  2120. case SSL_CTRL_SET_MAX_CERT_LIST:
  2121. if (larg < 0)
  2122. return 0;
  2123. l = (long)ctx->max_cert_list;
  2124. ctx->max_cert_list = (size_t)larg;
  2125. return l;
  2126. case SSL_CTRL_SET_SESS_CACHE_SIZE:
  2127. if (larg < 0)
  2128. return 0;
  2129. l = (long)ctx->session_cache_size;
  2130. ctx->session_cache_size = (size_t)larg;
  2131. return l;
  2132. case SSL_CTRL_GET_SESS_CACHE_SIZE:
  2133. return (long)ctx->session_cache_size;
  2134. case SSL_CTRL_SET_SESS_CACHE_MODE:
  2135. l = ctx->session_cache_mode;
  2136. ctx->session_cache_mode = larg;
  2137. return l;
  2138. case SSL_CTRL_GET_SESS_CACHE_MODE:
  2139. return ctx->session_cache_mode;
  2140. case SSL_CTRL_SESS_NUMBER:
  2141. return lh_SSL_SESSION_num_items(ctx->sessions);
  2142. case SSL_CTRL_SESS_CONNECT:
  2143. return tsan_load(&ctx->stats.sess_connect);
  2144. case SSL_CTRL_SESS_CONNECT_GOOD:
  2145. return tsan_load(&ctx->stats.sess_connect_good);
  2146. case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
  2147. return tsan_load(&ctx->stats.sess_connect_renegotiate);
  2148. case SSL_CTRL_SESS_ACCEPT:
  2149. return tsan_load(&ctx->stats.sess_accept);
  2150. case SSL_CTRL_SESS_ACCEPT_GOOD:
  2151. return tsan_load(&ctx->stats.sess_accept_good);
  2152. case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
  2153. return tsan_load(&ctx->stats.sess_accept_renegotiate);
  2154. case SSL_CTRL_SESS_HIT:
  2155. return tsan_load(&ctx->stats.sess_hit);
  2156. case SSL_CTRL_SESS_CB_HIT:
  2157. return tsan_load(&ctx->stats.sess_cb_hit);
  2158. case SSL_CTRL_SESS_MISSES:
  2159. return tsan_load(&ctx->stats.sess_miss);
  2160. case SSL_CTRL_SESS_TIMEOUTS:
  2161. return tsan_load(&ctx->stats.sess_timeout);
  2162. case SSL_CTRL_SESS_CACHE_FULL:
  2163. return tsan_load(&ctx->stats.sess_cache_full);
  2164. case SSL_CTRL_MODE:
  2165. return (ctx->mode |= larg);
  2166. case SSL_CTRL_CLEAR_MODE:
  2167. return (ctx->mode &= ~larg);
  2168. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  2169. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
  2170. return 0;
  2171. ctx->max_send_fragment = larg;
  2172. if (ctx->max_send_fragment < ctx->split_send_fragment)
  2173. ctx->split_send_fragment = ctx->max_send_fragment;
  2174. return 1;
  2175. case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
  2176. if ((size_t)larg > ctx->max_send_fragment || larg == 0)
  2177. return 0;
  2178. ctx->split_send_fragment = larg;
  2179. return 1;
  2180. case SSL_CTRL_SET_MAX_PIPELINES:
  2181. if (larg < 1 || larg > SSL_MAX_PIPELINES)
  2182. return 0;
  2183. ctx->max_pipelines = larg;
  2184. return 1;
  2185. case SSL_CTRL_CERT_FLAGS:
  2186. return (ctx->cert->cert_flags |= larg);
  2187. case SSL_CTRL_CLEAR_CERT_FLAGS:
  2188. return (ctx->cert->cert_flags &= ~larg);
  2189. case SSL_CTRL_SET_MIN_PROTO_VERSION:
  2190. return ssl_check_allowed_versions(larg, ctx->max_proto_version)
  2191. && ssl_set_version_bound(ctx->method->version, (int)larg,
  2192. &ctx->min_proto_version);
  2193. case SSL_CTRL_GET_MIN_PROTO_VERSION:
  2194. return ctx->min_proto_version;
  2195. case SSL_CTRL_SET_MAX_PROTO_VERSION:
  2196. return ssl_check_allowed_versions(ctx->min_proto_version, larg)
  2197. && ssl_set_version_bound(ctx->method->version, (int)larg,
  2198. &ctx->max_proto_version);
  2199. case SSL_CTRL_GET_MAX_PROTO_VERSION:
  2200. return ctx->max_proto_version;
  2201. default:
  2202. return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
  2203. }
  2204. }
  2205. long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
  2206. {
  2207. switch (cmd) {
  2208. case SSL_CTRL_SET_MSG_CALLBACK:
  2209. ctx->msg_callback = (void (*)
  2210. (int write_p, int version, int content_type,
  2211. const void *buf, size_t len, SSL *ssl,
  2212. void *arg))(fp);
  2213. return 1;
  2214. default:
  2215. return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
  2216. }
  2217. }
  2218. int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
  2219. {
  2220. if (a->id > b->id)
  2221. return 1;
  2222. if (a->id < b->id)
  2223. return -1;
  2224. return 0;
  2225. }
  2226. int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap,
  2227. const SSL_CIPHER *const *bp)
  2228. {
  2229. if ((*ap)->id > (*bp)->id)
  2230. return 1;
  2231. if ((*ap)->id < (*bp)->id)
  2232. return -1;
  2233. return 0;
  2234. }
  2235. /** return a STACK of the ciphers available for the SSL and in order of
  2236. * preference */
  2237. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
  2238. {
  2239. if (s != NULL) {
  2240. if (s->cipher_list != NULL) {
  2241. return s->cipher_list;
  2242. } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) {
  2243. return s->ctx->cipher_list;
  2244. }
  2245. }
  2246. return NULL;
  2247. }
  2248. STACK_OF(SSL_CIPHER) *SSL_get_client_ciphers(const SSL *s)
  2249. {
  2250. if ((s == NULL) || !s->server)
  2251. return NULL;
  2252. return s->peer_ciphers;
  2253. }
  2254. STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s)
  2255. {
  2256. STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers;
  2257. int i;
  2258. ciphers = SSL_get_ciphers(s);
  2259. if (!ciphers)
  2260. return NULL;
  2261. if (!ssl_set_client_disabled(s))
  2262. return NULL;
  2263. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  2264. const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
  2265. if (!ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) {
  2266. if (!sk)
  2267. sk = sk_SSL_CIPHER_new_null();
  2268. if (!sk)
  2269. return NULL;
  2270. if (!sk_SSL_CIPHER_push(sk, c)) {
  2271. sk_SSL_CIPHER_free(sk);
  2272. return NULL;
  2273. }
  2274. }
  2275. }
  2276. return sk;
  2277. }
  2278. /** return a STACK of the ciphers available for the SSL and in order of
  2279. * algorithm id */
  2280. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
  2281. {
  2282. if (s != NULL) {
  2283. if (s->cipher_list_by_id != NULL) {
  2284. return s->cipher_list_by_id;
  2285. } else if ((s->ctx != NULL) && (s->ctx->cipher_list_by_id != NULL)) {
  2286. return s->ctx->cipher_list_by_id;
  2287. }
  2288. }
  2289. return NULL;
  2290. }
  2291. /** The old interface to get the same thing as SSL_get_ciphers() */
  2292. const char *SSL_get_cipher_list(const SSL *s, int n)
  2293. {
  2294. const SSL_CIPHER *c;
  2295. STACK_OF(SSL_CIPHER) *sk;
  2296. if (s == NULL)
  2297. return NULL;
  2298. sk = SSL_get_ciphers(s);
  2299. if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
  2300. return NULL;
  2301. c = sk_SSL_CIPHER_value(sk, n);
  2302. if (c == NULL)
  2303. return NULL;
  2304. return c->name;
  2305. }
  2306. /** return a STACK of the ciphers available for the SSL_CTX and in order of
  2307. * preference */
  2308. STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx)
  2309. {
  2310. if (ctx != NULL)
  2311. return ctx->cipher_list;
  2312. return NULL;
  2313. }
  2314. /*
  2315. * Distinguish between ciphers controlled by set_ciphersuite() and
  2316. * set_cipher_list() when counting.
  2317. */
  2318. static int cipher_list_tls12_num(STACK_OF(SSL_CIPHER) *sk)
  2319. {
  2320. int i, num = 0;
  2321. const SSL_CIPHER *c;
  2322. if (sk == NULL)
  2323. return 0;
  2324. for (i = 0; i < sk_SSL_CIPHER_num(sk); ++i) {
  2325. c = sk_SSL_CIPHER_value(sk, i);
  2326. if (c->min_tls >= TLS1_3_VERSION)
  2327. continue;
  2328. num++;
  2329. }
  2330. return num;
  2331. }
  2332. /** specify the ciphers to be used by default by the SSL_CTX */
  2333. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
  2334. {
  2335. STACK_OF(SSL_CIPHER) *sk;
  2336. sk = ssl_create_cipher_list(ctx, ctx->tls13_ciphersuites,
  2337. &ctx->cipher_list, &ctx->cipher_list_by_id, str,
  2338. ctx->cert);
  2339. /*
  2340. * ssl_create_cipher_list may return an empty stack if it was unable to
  2341. * find a cipher matching the given rule string (for example if the rule
  2342. * string specifies a cipher which has been disabled). This is not an
  2343. * error as far as ssl_create_cipher_list is concerned, and hence
  2344. * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
  2345. */
  2346. if (sk == NULL)
  2347. return 0;
  2348. else if (cipher_list_tls12_num(sk) == 0) {
  2349. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
  2350. return 0;
  2351. }
  2352. return 1;
  2353. }
  2354. /** specify the ciphers to be used by the SSL */
  2355. int SSL_set_cipher_list(SSL *s, const char *str)
  2356. {
  2357. STACK_OF(SSL_CIPHER) *sk;
  2358. sk = ssl_create_cipher_list(s->ctx, s->tls13_ciphersuites,
  2359. &s->cipher_list, &s->cipher_list_by_id, str,
  2360. s->cert);
  2361. /* see comment in SSL_CTX_set_cipher_list */
  2362. if (sk == NULL)
  2363. return 0;
  2364. else if (cipher_list_tls12_num(sk) == 0) {
  2365. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
  2366. return 0;
  2367. }
  2368. return 1;
  2369. }
  2370. char *SSL_get_shared_ciphers(const SSL *s, char *buf, int size)
  2371. {
  2372. char *p;
  2373. STACK_OF(SSL_CIPHER) *clntsk, *srvrsk;
  2374. const SSL_CIPHER *c;
  2375. int i;
  2376. if (!s->server
  2377. || s->peer_ciphers == NULL
  2378. || size < 2)
  2379. return NULL;
  2380. p = buf;
  2381. clntsk = s->peer_ciphers;
  2382. srvrsk = SSL_get_ciphers(s);
  2383. if (clntsk == NULL || srvrsk == NULL)
  2384. return NULL;
  2385. if (sk_SSL_CIPHER_num(clntsk) == 0 || sk_SSL_CIPHER_num(srvrsk) == 0)
  2386. return NULL;
  2387. for (i = 0; i < sk_SSL_CIPHER_num(clntsk); i++) {
  2388. int n;
  2389. c = sk_SSL_CIPHER_value(clntsk, i);
  2390. if (sk_SSL_CIPHER_find(srvrsk, c) < 0)
  2391. continue;
  2392. n = strlen(c->name);
  2393. if (n + 1 > size) {
  2394. if (p != buf)
  2395. --p;
  2396. *p = '\0';
  2397. return buf;
  2398. }
  2399. strcpy(p, c->name);
  2400. p += n;
  2401. *(p++) = ':';
  2402. size -= n + 1;
  2403. }
  2404. p[-1] = '\0';
  2405. return buf;
  2406. }
  2407. /**
  2408. * Return the requested servername (SNI) value. Note that the behaviour varies
  2409. * depending on:
  2410. * - whether this is called by the client or the server,
  2411. * - if we are before or during/after the handshake,
  2412. * - if a resumption or normal handshake is being attempted/has occurred
  2413. * - whether we have negotiated TLSv1.2 (or below) or TLSv1.3
  2414. *
  2415. * Note that only the host_name type is defined (RFC 3546).
  2416. */
  2417. const char *SSL_get_servername(const SSL *s, const int type)
  2418. {
  2419. /*
  2420. * If we don't know if we are the client or the server yet then we assume
  2421. * client.
  2422. */
  2423. int server = s->handshake_func == NULL ? 0 : s->server;
  2424. if (type != TLSEXT_NAMETYPE_host_name)
  2425. return NULL;
  2426. if (server) {
  2427. /**
  2428. * Server side
  2429. * In TLSv1.3 on the server SNI is not associated with the session
  2430. * but in TLSv1.2 or below it is.
  2431. *
  2432. * Before the handshake:
  2433. * - return NULL
  2434. *
  2435. * During/after the handshake (TLSv1.2 or below resumption occurred):
  2436. * - If a servername was accepted by the server in the original
  2437. * handshake then it will return that servername, or NULL otherwise.
  2438. *
  2439. * During/after the handshake (TLSv1.2 or below resumption did not occur):
  2440. * - The function will return the servername requested by the client in
  2441. * this handshake or NULL if none was requested.
  2442. */
  2443. if (s->hit && !SSL_IS_TLS13(s))
  2444. return s->session->ext.hostname;
  2445. } else {
  2446. /**
  2447. * Client side
  2448. *
  2449. * Before the handshake:
  2450. * - If a servername has been set via a call to
  2451. * SSL_set_tlsext_host_name() then it will return that servername
  2452. * - If one has not been set, but a TLSv1.2 resumption is being
  2453. * attempted and the session from the original handshake had a
  2454. * servername accepted by the server then it will return that
  2455. * servername
  2456. * - Otherwise it returns NULL
  2457. *
  2458. * During/after the handshake (TLSv1.2 or below resumption occurred):
  2459. * - If the session from the original handshake had a servername accepted
  2460. * by the server then it will return that servername.
  2461. * - Otherwise it returns the servername set via
  2462. * SSL_set_tlsext_host_name() (or NULL if it was not called).
  2463. *
  2464. * During/after the handshake (TLSv1.2 or below resumption did not occur):
  2465. * - It will return the servername set via SSL_set_tlsext_host_name()
  2466. * (or NULL if it was not called).
  2467. */
  2468. if (SSL_in_before(s)) {
  2469. if (s->ext.hostname == NULL
  2470. && s->session != NULL
  2471. && s->session->ssl_version != TLS1_3_VERSION)
  2472. return s->session->ext.hostname;
  2473. } else {
  2474. if (!SSL_IS_TLS13(s) && s->hit && s->session->ext.hostname != NULL)
  2475. return s->session->ext.hostname;
  2476. }
  2477. }
  2478. return s->ext.hostname;
  2479. }
  2480. int SSL_get_servername_type(const SSL *s)
  2481. {
  2482. if (SSL_get_servername(s, TLSEXT_NAMETYPE_host_name) != NULL)
  2483. return TLSEXT_NAMETYPE_host_name;
  2484. return -1;
  2485. }
  2486. /*
  2487. * SSL_select_next_proto implements the standard protocol selection. It is
  2488. * expected that this function is called from the callback set by
  2489. * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
  2490. * vector of 8-bit, length prefixed byte strings. The length byte itself is
  2491. * not included in the length. A byte string of length 0 is invalid. No byte
  2492. * string may be truncated. The current, but experimental algorithm for
  2493. * selecting the protocol is: 1) If the server doesn't support NPN then this
  2494. * is indicated to the callback. In this case, the client application has to
  2495. * abort the connection or have a default application level protocol. 2) If
  2496. * the server supports NPN, but advertises an empty list then the client
  2497. * selects the first protocol in its list, but indicates via the API that this
  2498. * fallback case was enacted. 3) Otherwise, the client finds the first
  2499. * protocol in the server's list that it supports and selects this protocol.
  2500. * This is because it's assumed that the server has better information about
  2501. * which protocol a client should use. 4) If the client doesn't support any
  2502. * of the server's advertised protocols, then this is treated the same as
  2503. * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
  2504. * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
  2505. */
  2506. int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
  2507. const unsigned char *server,
  2508. unsigned int server_len,
  2509. const unsigned char *client, unsigned int client_len)
  2510. {
  2511. unsigned int i, j;
  2512. const unsigned char *result;
  2513. int status = OPENSSL_NPN_UNSUPPORTED;
  2514. /*
  2515. * For each protocol in server preference order, see if we support it.
  2516. */
  2517. for (i = 0; i < server_len;) {
  2518. for (j = 0; j < client_len;) {
  2519. if (server[i] == client[j] &&
  2520. memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  2521. /* We found a match */
  2522. result = &server[i];
  2523. status = OPENSSL_NPN_NEGOTIATED;
  2524. goto found;
  2525. }
  2526. j += client[j];
  2527. j++;
  2528. }
  2529. i += server[i];
  2530. i++;
  2531. }
  2532. /* There's no overlap between our protocols and the server's list. */
  2533. result = client;
  2534. status = OPENSSL_NPN_NO_OVERLAP;
  2535. found:
  2536. *out = (unsigned char *)result + 1;
  2537. *outlen = result[0];
  2538. return status;
  2539. }
  2540. #ifndef OPENSSL_NO_NEXTPROTONEG
  2541. /*
  2542. * SSL_get0_next_proto_negotiated sets *data and *len to point to the
  2543. * client's requested protocol for this connection and returns 0. If the
  2544. * client didn't request any protocol, then *data is set to NULL. Note that
  2545. * the client can request any protocol it chooses. The value returned from
  2546. * this function need not be a member of the list of supported protocols
  2547. * provided by the callback.
  2548. */
  2549. void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
  2550. unsigned *len)
  2551. {
  2552. *data = s->ext.npn;
  2553. if (*data == NULL) {
  2554. *len = 0;
  2555. } else {
  2556. *len = (unsigned int)s->ext.npn_len;
  2557. }
  2558. }
  2559. /*
  2560. * SSL_CTX_set_npn_advertised_cb sets a callback that is called when
  2561. * a TLS server needs a list of supported protocols for Next Protocol
  2562. * Negotiation. The returned list must be in wire format. The list is
  2563. * returned by setting |out| to point to it and |outlen| to its length. This
  2564. * memory will not be modified, but one should assume that the SSL* keeps a
  2565. * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
  2566. * wishes to advertise. Otherwise, no such extension will be included in the
  2567. * ServerHello.
  2568. */
  2569. void SSL_CTX_set_npn_advertised_cb(SSL_CTX *ctx,
  2570. SSL_CTX_npn_advertised_cb_func cb,
  2571. void *arg)
  2572. {
  2573. ctx->ext.npn_advertised_cb = cb;
  2574. ctx->ext.npn_advertised_cb_arg = arg;
  2575. }
  2576. /*
  2577. * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
  2578. * client needs to select a protocol from the server's provided list. |out|
  2579. * must be set to point to the selected protocol (which may be within |in|).
  2580. * The length of the protocol name must be written into |outlen|. The
  2581. * server's advertised protocols are provided in |in| and |inlen|. The
  2582. * callback can assume that |in| is syntactically valid. The client must
  2583. * select a protocol. It is fatal to the connection if this callback returns
  2584. * a value other than SSL_TLSEXT_ERR_OK.
  2585. */
  2586. void SSL_CTX_set_npn_select_cb(SSL_CTX *ctx,
  2587. SSL_CTX_npn_select_cb_func cb,
  2588. void *arg)
  2589. {
  2590. ctx->ext.npn_select_cb = cb;
  2591. ctx->ext.npn_select_cb_arg = arg;
  2592. }
  2593. #endif
  2594. static int alpn_value_ok(const unsigned char *protos, unsigned int protos_len)
  2595. {
  2596. unsigned int idx;
  2597. if (protos_len < 2 || protos == NULL)
  2598. return 0;
  2599. for (idx = 0; idx < protos_len; idx += protos[idx] + 1) {
  2600. if (protos[idx] == 0)
  2601. return 0;
  2602. }
  2603. return idx == protos_len;
  2604. }
  2605. /*
  2606. * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
  2607. * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
  2608. * length-prefixed strings). Returns 0 on success.
  2609. */
  2610. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
  2611. unsigned int protos_len)
  2612. {
  2613. unsigned char *alpn;
  2614. if (protos_len == 0 || protos == NULL) {
  2615. OPENSSL_free(ctx->ext.alpn);
  2616. ctx->ext.alpn = NULL;
  2617. ctx->ext.alpn_len = 0;
  2618. return 0;
  2619. }
  2620. /* Not valid per RFC */
  2621. if (!alpn_value_ok(protos, protos_len))
  2622. return 1;
  2623. alpn = OPENSSL_memdup(protos, protos_len);
  2624. if (alpn == NULL) {
  2625. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  2626. return 1;
  2627. }
  2628. OPENSSL_free(ctx->ext.alpn);
  2629. ctx->ext.alpn = alpn;
  2630. ctx->ext.alpn_len = protos_len;
  2631. return 0;
  2632. }
  2633. /*
  2634. * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
  2635. * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
  2636. * length-prefixed strings). Returns 0 on success.
  2637. */
  2638. int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
  2639. unsigned int protos_len)
  2640. {
  2641. unsigned char *alpn;
  2642. if (protos_len == 0 || protos == NULL) {
  2643. OPENSSL_free(ssl->ext.alpn);
  2644. ssl->ext.alpn = NULL;
  2645. ssl->ext.alpn_len = 0;
  2646. return 0;
  2647. }
  2648. /* Not valid per RFC */
  2649. if (!alpn_value_ok(protos, protos_len))
  2650. return 1;
  2651. alpn = OPENSSL_memdup(protos, protos_len);
  2652. if (alpn == NULL) {
  2653. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  2654. return 1;
  2655. }
  2656. OPENSSL_free(ssl->ext.alpn);
  2657. ssl->ext.alpn = alpn;
  2658. ssl->ext.alpn_len = protos_len;
  2659. return 0;
  2660. }
  2661. /*
  2662. * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
  2663. * called during ClientHello processing in order to select an ALPN protocol
  2664. * from the client's list of offered protocols.
  2665. */
  2666. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  2667. SSL_CTX_alpn_select_cb_func cb,
  2668. void *arg)
  2669. {
  2670. ctx->ext.alpn_select_cb = cb;
  2671. ctx->ext.alpn_select_cb_arg = arg;
  2672. }
  2673. /*
  2674. * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
  2675. * On return it sets |*data| to point to |*len| bytes of protocol name
  2676. * (not including the leading length-prefix byte). If the server didn't
  2677. * respond with a negotiated protocol then |*len| will be zero.
  2678. */
  2679. void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
  2680. unsigned int *len)
  2681. {
  2682. *data = ssl->s3.alpn_selected;
  2683. if (*data == NULL)
  2684. *len = 0;
  2685. else
  2686. *len = (unsigned int)ssl->s3.alpn_selected_len;
  2687. }
  2688. int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
  2689. const char *label, size_t llen,
  2690. const unsigned char *context, size_t contextlen,
  2691. int use_context)
  2692. {
  2693. if (s->session == NULL
  2694. || (s->version < TLS1_VERSION && s->version != DTLS1_BAD_VER))
  2695. return -1;
  2696. return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
  2697. llen, context,
  2698. contextlen, use_context);
  2699. }
  2700. int SSL_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
  2701. const char *label, size_t llen,
  2702. const unsigned char *context,
  2703. size_t contextlen)
  2704. {
  2705. if (s->version != TLS1_3_VERSION)
  2706. return 0;
  2707. return tls13_export_keying_material_early(s, out, olen, label, llen,
  2708. context, contextlen);
  2709. }
  2710. static unsigned long ssl_session_hash(const SSL_SESSION *a)
  2711. {
  2712. const unsigned char *session_id = a->session_id;
  2713. unsigned long l;
  2714. unsigned char tmp_storage[4];
  2715. if (a->session_id_length < sizeof(tmp_storage)) {
  2716. memset(tmp_storage, 0, sizeof(tmp_storage));
  2717. memcpy(tmp_storage, a->session_id, a->session_id_length);
  2718. session_id = tmp_storage;
  2719. }
  2720. l = (unsigned long)
  2721. ((unsigned long)session_id[0]) |
  2722. ((unsigned long)session_id[1] << 8L) |
  2723. ((unsigned long)session_id[2] << 16L) |
  2724. ((unsigned long)session_id[3] << 24L);
  2725. return l;
  2726. }
  2727. /*
  2728. * NB: If this function (or indeed the hash function which uses a sort of
  2729. * coarser function than this one) is changed, ensure
  2730. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
  2731. * being able to construct an SSL_SESSION that will collide with any existing
  2732. * session with a matching session ID.
  2733. */
  2734. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
  2735. {
  2736. if (a->ssl_version != b->ssl_version)
  2737. return 1;
  2738. if (a->session_id_length != b->session_id_length)
  2739. return 1;
  2740. return memcmp(a->session_id, b->session_id, a->session_id_length);
  2741. }
  2742. /*
  2743. * These wrapper functions should remain rather than redeclaring
  2744. * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
  2745. * variable. The reason is that the functions aren't static, they're exposed
  2746. * via ssl.h.
  2747. */
  2748. SSL_CTX *SSL_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
  2749. const SSL_METHOD *meth)
  2750. {
  2751. SSL_CTX *ret = NULL;
  2752. if (meth == NULL) {
  2753. ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  2754. return NULL;
  2755. }
  2756. if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
  2757. return NULL;
  2758. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  2759. ERR_raise(ERR_LIB_SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  2760. goto err;
  2761. }
  2762. ret = OPENSSL_zalloc(sizeof(*ret));
  2763. if (ret == NULL)
  2764. goto err;
  2765. /* Init the reference counting before any call to SSL_CTX_free */
  2766. ret->references = 1;
  2767. ret->lock = CRYPTO_THREAD_lock_new();
  2768. if (ret->lock == NULL) {
  2769. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  2770. OPENSSL_free(ret);
  2771. return NULL;
  2772. }
  2773. ret->libctx = libctx;
  2774. if (propq != NULL) {
  2775. ret->propq = OPENSSL_strdup(propq);
  2776. if (ret->propq == NULL)
  2777. goto err;
  2778. }
  2779. ret->method = meth;
  2780. ret->min_proto_version = 0;
  2781. ret->max_proto_version = 0;
  2782. ret->mode = SSL_MODE_AUTO_RETRY;
  2783. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  2784. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  2785. /* We take the system default. */
  2786. ret->session_timeout = meth->get_timeout();
  2787. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  2788. ret->verify_mode = SSL_VERIFY_NONE;
  2789. if ((ret->cert = ssl_cert_new()) == NULL)
  2790. goto err;
  2791. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  2792. if (ret->sessions == NULL)
  2793. goto err;
  2794. ret->cert_store = X509_STORE_new();
  2795. if (ret->cert_store == NULL)
  2796. goto err;
  2797. #ifndef OPENSSL_NO_CT
  2798. ret->ctlog_store = CTLOG_STORE_new_ex(libctx, propq);
  2799. if (ret->ctlog_store == NULL)
  2800. goto err;
  2801. #endif
  2802. /* initialize cipher/digest methods table */
  2803. if (!ssl_load_ciphers(ret))
  2804. goto err2;
  2805. /* initialise sig algs */
  2806. if (!ssl_setup_sig_algs(ret))
  2807. goto err2;
  2808. if (!ssl_load_groups(ret))
  2809. goto err2;
  2810. if (!SSL_CTX_set_ciphersuites(ret, OSSL_default_ciphersuites()))
  2811. goto err;
  2812. if (!ssl_create_cipher_list(ret,
  2813. ret->tls13_ciphersuites,
  2814. &ret->cipher_list, &ret->cipher_list_by_id,
  2815. OSSL_default_cipher_list(), ret->cert)
  2816. || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
  2817. ERR_raise(ERR_LIB_SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  2818. goto err2;
  2819. }
  2820. ret->param = X509_VERIFY_PARAM_new();
  2821. if (ret->param == NULL)
  2822. goto err;
  2823. /*
  2824. * If these aren't available from the provider we'll get NULL returns.
  2825. * That's fine but will cause errors later if SSLv3 is negotiated
  2826. */
  2827. ret->md5 = ssl_evp_md_fetch(libctx, NID_md5, propq);
  2828. ret->sha1 = ssl_evp_md_fetch(libctx, NID_sha1, propq);
  2829. if ((ret->ca_names = sk_X509_NAME_new_null()) == NULL)
  2830. goto err;
  2831. if ((ret->client_ca_names = sk_X509_NAME_new_null()) == NULL)
  2832. goto err;
  2833. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data))
  2834. goto err;
  2835. if ((ret->ext.secure = OPENSSL_secure_zalloc(sizeof(*ret->ext.secure))) == NULL)
  2836. goto err;
  2837. /* No compression for DTLS */
  2838. if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))
  2839. ret->comp_methods = SSL_COMP_get_compression_methods();
  2840. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  2841. ret->split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  2842. /* Setup RFC5077 ticket keys */
  2843. if ((RAND_bytes_ex(libctx, ret->ext.tick_key_name,
  2844. sizeof(ret->ext.tick_key_name), 0) <= 0)
  2845. || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_hmac_key,
  2846. sizeof(ret->ext.secure->tick_hmac_key), 0) <= 0)
  2847. || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_aes_key,
  2848. sizeof(ret->ext.secure->tick_aes_key), 0) <= 0))
  2849. ret->options |= SSL_OP_NO_TICKET;
  2850. if (RAND_priv_bytes_ex(libctx, ret->ext.cookie_hmac_key,
  2851. sizeof(ret->ext.cookie_hmac_key), 0) <= 0)
  2852. goto err;
  2853. #ifndef OPENSSL_NO_SRP
  2854. if (!ssl_ctx_srp_ctx_init_intern(ret))
  2855. goto err;
  2856. #endif
  2857. #ifndef OPENSSL_NO_ENGINE
  2858. # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
  2859. # define eng_strx(x) #x
  2860. # define eng_str(x) eng_strx(x)
  2861. /* Use specific client engine automatically... ignore errors */
  2862. {
  2863. ENGINE *eng;
  2864. eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
  2865. if (!eng) {
  2866. ERR_clear_error();
  2867. ENGINE_load_builtin_engines();
  2868. eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
  2869. }
  2870. if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
  2871. ERR_clear_error();
  2872. }
  2873. # endif
  2874. #endif
  2875. /*
  2876. * Disable compression by default to prevent CRIME. Applications can
  2877. * re-enable compression by configuring
  2878. * SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION);
  2879. * or by using the SSL_CONF library. Similarly we also enable TLSv1.3
  2880. * middlebox compatibility by default. This may be disabled by default in
  2881. * a later OpenSSL version.
  2882. */
  2883. ret->options |= SSL_OP_NO_COMPRESSION | SSL_OP_ENABLE_MIDDLEBOX_COMPAT;
  2884. ret->ext.status_type = TLSEXT_STATUSTYPE_nothing;
  2885. /*
  2886. * We cannot usefully set a default max_early_data here (which gets
  2887. * propagated in SSL_new(), for the following reason: setting the
  2888. * SSL field causes tls_construct_stoc_early_data() to tell the
  2889. * client that early data will be accepted when constructing a TLS 1.3
  2890. * session ticket, and the client will accordingly send us early data
  2891. * when using that ticket (if the client has early data to send).
  2892. * However, in order for the early data to actually be consumed by
  2893. * the application, the application must also have calls to
  2894. * SSL_read_early_data(); otherwise we'll just skip past the early data
  2895. * and ignore it. So, since the application must add calls to
  2896. * SSL_read_early_data(), we also require them to add
  2897. * calls to SSL_CTX_set_max_early_data() in order to use early data,
  2898. * eliminating the bandwidth-wasting early data in the case described
  2899. * above.
  2900. */
  2901. ret->max_early_data = 0;
  2902. /*
  2903. * Default recv_max_early_data is a fully loaded single record. Could be
  2904. * split across multiple records in practice. We set this differently to
  2905. * max_early_data so that, in the default case, we do not advertise any
  2906. * support for early_data, but if a client were to send us some (e.g.
  2907. * because of an old, stale ticket) then we will tolerate it and skip over
  2908. * it.
  2909. */
  2910. ret->recv_max_early_data = SSL3_RT_MAX_PLAIN_LENGTH;
  2911. /* By default we send two session tickets automatically in TLSv1.3 */
  2912. ret->num_tickets = 2;
  2913. ssl_ctx_system_config(ret);
  2914. return ret;
  2915. err:
  2916. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  2917. err2:
  2918. SSL_CTX_free(ret);
  2919. return NULL;
  2920. }
  2921. SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
  2922. {
  2923. return SSL_CTX_new_ex(NULL, NULL, meth);
  2924. }
  2925. int SSL_CTX_up_ref(SSL_CTX *ctx)
  2926. {
  2927. int i;
  2928. if (CRYPTO_UP_REF(&ctx->references, &i, ctx->lock) <= 0)
  2929. return 0;
  2930. REF_PRINT_COUNT("SSL_CTX", ctx);
  2931. REF_ASSERT_ISNT(i < 2);
  2932. return ((i > 1) ? 1 : 0);
  2933. }
  2934. void SSL_CTX_free(SSL_CTX *a)
  2935. {
  2936. int i;
  2937. size_t j;
  2938. if (a == NULL)
  2939. return;
  2940. CRYPTO_DOWN_REF(&a->references, &i, a->lock);
  2941. REF_PRINT_COUNT("SSL_CTX", a);
  2942. if (i > 0)
  2943. return;
  2944. REF_ASSERT_ISNT(i < 0);
  2945. X509_VERIFY_PARAM_free(a->param);
  2946. dane_ctx_final(&a->dane);
  2947. /*
  2948. * Free internal session cache. However: the remove_cb() may reference
  2949. * the ex_data of SSL_CTX, thus the ex_data store can only be removed
  2950. * after the sessions were flushed.
  2951. * As the ex_data handling routines might also touch the session cache,
  2952. * the most secure solution seems to be: empty (flush) the cache, then
  2953. * free ex_data, then finally free the cache.
  2954. * (See ticket [openssl.org #212].)
  2955. */
  2956. if (a->sessions != NULL)
  2957. SSL_CTX_flush_sessions(a, 0);
  2958. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
  2959. lh_SSL_SESSION_free(a->sessions);
  2960. X509_STORE_free(a->cert_store);
  2961. #ifndef OPENSSL_NO_CT
  2962. CTLOG_STORE_free(a->ctlog_store);
  2963. #endif
  2964. sk_SSL_CIPHER_free(a->cipher_list);
  2965. sk_SSL_CIPHER_free(a->cipher_list_by_id);
  2966. sk_SSL_CIPHER_free(a->tls13_ciphersuites);
  2967. ssl_cert_free(a->cert);
  2968. sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);
  2969. sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);
  2970. sk_X509_pop_free(a->extra_certs, X509_free);
  2971. a->comp_methods = NULL;
  2972. #ifndef OPENSSL_NO_SRTP
  2973. sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
  2974. #endif
  2975. #ifndef OPENSSL_NO_SRP
  2976. ssl_ctx_srp_ctx_free_intern(a);
  2977. #endif
  2978. #ifndef OPENSSL_NO_ENGINE
  2979. tls_engine_finish(a->client_cert_engine);
  2980. #endif
  2981. OPENSSL_free(a->ext.ecpointformats);
  2982. OPENSSL_free(a->ext.supportedgroups);
  2983. OPENSSL_free(a->ext.supported_groups_default);
  2984. OPENSSL_free(a->ext.alpn);
  2985. OPENSSL_secure_free(a->ext.secure);
  2986. ssl_evp_md_free(a->md5);
  2987. ssl_evp_md_free(a->sha1);
  2988. for (j = 0; j < SSL_ENC_NUM_IDX; j++)
  2989. ssl_evp_cipher_free(a->ssl_cipher_methods[j]);
  2990. for (j = 0; j < SSL_MD_NUM_IDX; j++)
  2991. ssl_evp_md_free(a->ssl_digest_methods[j]);
  2992. for (j = 0; j < a->group_list_len; j++) {
  2993. OPENSSL_free(a->group_list[j].tlsname);
  2994. OPENSSL_free(a->group_list[j].realname);
  2995. OPENSSL_free(a->group_list[j].algorithm);
  2996. }
  2997. OPENSSL_free(a->group_list);
  2998. OPENSSL_free(a->sigalg_lookup_cache);
  2999. CRYPTO_THREAD_lock_free(a->lock);
  3000. OPENSSL_free(a->propq);
  3001. OPENSSL_free(a);
  3002. }
  3003. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
  3004. {
  3005. ctx->default_passwd_callback = cb;
  3006. }
  3007. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
  3008. {
  3009. ctx->default_passwd_callback_userdata = u;
  3010. }
  3011. pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
  3012. {
  3013. return ctx->default_passwd_callback;
  3014. }
  3015. void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
  3016. {
  3017. return ctx->default_passwd_callback_userdata;
  3018. }
  3019. void SSL_set_default_passwd_cb(SSL *s, pem_password_cb *cb)
  3020. {
  3021. s->default_passwd_callback = cb;
  3022. }
  3023. void SSL_set_default_passwd_cb_userdata(SSL *s, void *u)
  3024. {
  3025. s->default_passwd_callback_userdata = u;
  3026. }
  3027. pem_password_cb *SSL_get_default_passwd_cb(SSL *s)
  3028. {
  3029. return s->default_passwd_callback;
  3030. }
  3031. void *SSL_get_default_passwd_cb_userdata(SSL *s)
  3032. {
  3033. return s->default_passwd_callback_userdata;
  3034. }
  3035. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  3036. int (*cb) (X509_STORE_CTX *, void *),
  3037. void *arg)
  3038. {
  3039. ctx->app_verify_callback = cb;
  3040. ctx->app_verify_arg = arg;
  3041. }
  3042. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  3043. int (*cb) (int, X509_STORE_CTX *))
  3044. {
  3045. ctx->verify_mode = mode;
  3046. ctx->default_verify_callback = cb;
  3047. }
  3048. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
  3049. {
  3050. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  3051. }
  3052. void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb) (SSL *ssl, void *arg), void *arg)
  3053. {
  3054. ssl_cert_set_cert_cb(c->cert, cb, arg);
  3055. }
  3056. void SSL_set_cert_cb(SSL *s, int (*cb) (SSL *ssl, void *arg), void *arg)
  3057. {
  3058. ssl_cert_set_cert_cb(s->cert, cb, arg);
  3059. }
  3060. void ssl_set_masks(SSL *s)
  3061. {
  3062. CERT *c = s->cert;
  3063. uint32_t *pvalid = s->s3.tmp.valid_flags;
  3064. int rsa_enc, rsa_sign, dh_tmp, dsa_sign;
  3065. unsigned long mask_k, mask_a;
  3066. int have_ecc_cert, ecdsa_ok;
  3067. if (c == NULL)
  3068. return;
  3069. dh_tmp = (c->dh_tmp != NULL
  3070. || c->dh_tmp_cb != NULL
  3071. || c->dh_tmp_auto);
  3072. rsa_enc = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
  3073. rsa_sign = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
  3074. dsa_sign = pvalid[SSL_PKEY_DSA_SIGN] & CERT_PKEY_VALID;
  3075. have_ecc_cert = pvalid[SSL_PKEY_ECC] & CERT_PKEY_VALID;
  3076. mask_k = 0;
  3077. mask_a = 0;
  3078. OSSL_TRACE4(TLS_CIPHER, "dh_tmp=%d rsa_enc=%d rsa_sign=%d dsa_sign=%d\n",
  3079. dh_tmp, rsa_enc, rsa_sign, dsa_sign);
  3080. #ifndef OPENSSL_NO_GOST
  3081. if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) {
  3082. mask_k |= SSL_kGOST | SSL_kGOST18;
  3083. mask_a |= SSL_aGOST12;
  3084. }
  3085. if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) {
  3086. mask_k |= SSL_kGOST | SSL_kGOST18;
  3087. mask_a |= SSL_aGOST12;
  3088. }
  3089. if (ssl_has_cert(s, SSL_PKEY_GOST01)) {
  3090. mask_k |= SSL_kGOST;
  3091. mask_a |= SSL_aGOST01;
  3092. }
  3093. #endif
  3094. if (rsa_enc)
  3095. mask_k |= SSL_kRSA;
  3096. if (dh_tmp)
  3097. mask_k |= SSL_kDHE;
  3098. /*
  3099. * If we only have an RSA-PSS certificate allow RSA authentication
  3100. * if TLS 1.2 and peer supports it.
  3101. */
  3102. if (rsa_enc || rsa_sign || (ssl_has_cert(s, SSL_PKEY_RSA_PSS_SIGN)
  3103. && pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_EXPLICIT_SIGN
  3104. && TLS1_get_version(s) == TLS1_2_VERSION))
  3105. mask_a |= SSL_aRSA;
  3106. if (dsa_sign) {
  3107. mask_a |= SSL_aDSS;
  3108. }
  3109. mask_a |= SSL_aNULL;
  3110. /*
  3111. * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
  3112. * depending on the key usage extension.
  3113. */
  3114. if (have_ecc_cert) {
  3115. uint32_t ex_kusage;
  3116. ex_kusage = X509_get_key_usage(c->pkeys[SSL_PKEY_ECC].x509);
  3117. ecdsa_ok = ex_kusage & X509v3_KU_DIGITAL_SIGNATURE;
  3118. if (!(pvalid[SSL_PKEY_ECC] & CERT_PKEY_SIGN))
  3119. ecdsa_ok = 0;
  3120. if (ecdsa_ok)
  3121. mask_a |= SSL_aECDSA;
  3122. }
  3123. /* Allow Ed25519 for TLS 1.2 if peer supports it */
  3124. if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED25519)
  3125. && pvalid[SSL_PKEY_ED25519] & CERT_PKEY_EXPLICIT_SIGN
  3126. && TLS1_get_version(s) == TLS1_2_VERSION)
  3127. mask_a |= SSL_aECDSA;
  3128. /* Allow Ed448 for TLS 1.2 if peer supports it */
  3129. if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED448)
  3130. && pvalid[SSL_PKEY_ED448] & CERT_PKEY_EXPLICIT_SIGN
  3131. && TLS1_get_version(s) == TLS1_2_VERSION)
  3132. mask_a |= SSL_aECDSA;
  3133. mask_k |= SSL_kECDHE;
  3134. #ifndef OPENSSL_NO_PSK
  3135. mask_k |= SSL_kPSK;
  3136. mask_a |= SSL_aPSK;
  3137. if (mask_k & SSL_kRSA)
  3138. mask_k |= SSL_kRSAPSK;
  3139. if (mask_k & SSL_kDHE)
  3140. mask_k |= SSL_kDHEPSK;
  3141. if (mask_k & SSL_kECDHE)
  3142. mask_k |= SSL_kECDHEPSK;
  3143. #endif
  3144. s->s3.tmp.mask_k = mask_k;
  3145. s->s3.tmp.mask_a = mask_a;
  3146. }
  3147. int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
  3148. {
  3149. if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA) {
  3150. /* key usage, if present, must allow signing */
  3151. if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
  3152. ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  3153. return 0;
  3154. }
  3155. }
  3156. return 1; /* all checks are ok */
  3157. }
  3158. int ssl_get_server_cert_serverinfo(SSL *s, const unsigned char **serverinfo,
  3159. size_t *serverinfo_length)
  3160. {
  3161. CERT_PKEY *cpk = s->s3.tmp.cert;
  3162. *serverinfo_length = 0;
  3163. if (cpk == NULL || cpk->serverinfo == NULL)
  3164. return 0;
  3165. *serverinfo = cpk->serverinfo;
  3166. *serverinfo_length = cpk->serverinfo_length;
  3167. return 1;
  3168. }
  3169. void ssl_update_cache(SSL *s, int mode)
  3170. {
  3171. int i;
  3172. /*
  3173. * If the session_id_length is 0, we are not supposed to cache it, and it
  3174. * would be rather hard to do anyway :-)
  3175. */
  3176. if (s->session->session_id_length == 0)
  3177. return;
  3178. /*
  3179. * If sid_ctx_length is 0 there is no specific application context
  3180. * associated with this session, so when we try to resume it and
  3181. * SSL_VERIFY_PEER is requested to verify the client identity, we have no
  3182. * indication that this is actually a session for the proper application
  3183. * context, and the *handshake* will fail, not just the resumption attempt.
  3184. * Do not cache (on the server) these sessions that are not resumable
  3185. * (clients can set SSL_VERIFY_PEER without needing a sid_ctx set).
  3186. */
  3187. if (s->server && s->session->sid_ctx_length == 0
  3188. && (s->verify_mode & SSL_VERIFY_PEER) != 0)
  3189. return;
  3190. i = s->session_ctx->session_cache_mode;
  3191. if ((i & mode) != 0
  3192. && (!s->hit || SSL_IS_TLS13(s))) {
  3193. /*
  3194. * Add the session to the internal cache. In server side TLSv1.3 we
  3195. * normally don't do this because by default it's a full stateless ticket
  3196. * with only a dummy session id so there is no reason to cache it,
  3197. * unless:
  3198. * - we are doing early_data, in which case we cache so that we can
  3199. * detect replays
  3200. * - the application has set a remove_session_cb so needs to know about
  3201. * session timeout events
  3202. * - SSL_OP_NO_TICKET is set in which case it is a stateful ticket
  3203. */
  3204. if ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0
  3205. && (!SSL_IS_TLS13(s)
  3206. || !s->server
  3207. || (s->max_early_data > 0
  3208. && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0)
  3209. || s->session_ctx->remove_session_cb != NULL
  3210. || (s->options & SSL_OP_NO_TICKET) != 0))
  3211. SSL_CTX_add_session(s->session_ctx, s->session);
  3212. /*
  3213. * Add the session to the external cache. We do this even in server side
  3214. * TLSv1.3 without early data because some applications just want to
  3215. * know about the creation of a session and aren't doing a full cache.
  3216. */
  3217. if (s->session_ctx->new_session_cb != NULL) {
  3218. SSL_SESSION_up_ref(s->session);
  3219. if (!s->session_ctx->new_session_cb(s, s->session))
  3220. SSL_SESSION_free(s->session);
  3221. }
  3222. }
  3223. /* auto flush every 255 connections */
  3224. if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
  3225. TSAN_QUALIFIER int *stat;
  3226. if (mode & SSL_SESS_CACHE_CLIENT)
  3227. stat = &s->session_ctx->stats.sess_connect_good;
  3228. else
  3229. stat = &s->session_ctx->stats.sess_accept_good;
  3230. if ((tsan_load(stat) & 0xff) == 0xff)
  3231. SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL));
  3232. }
  3233. }
  3234. const SSL_METHOD *SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
  3235. {
  3236. return ctx->method;
  3237. }
  3238. const SSL_METHOD *SSL_get_ssl_method(const SSL *s)
  3239. {
  3240. return s->method;
  3241. }
  3242. int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
  3243. {
  3244. int ret = 1;
  3245. if (s->method != meth) {
  3246. const SSL_METHOD *sm = s->method;
  3247. int (*hf) (SSL *) = s->handshake_func;
  3248. if (sm->version == meth->version)
  3249. s->method = meth;
  3250. else {
  3251. sm->ssl_free(s);
  3252. s->method = meth;
  3253. ret = s->method->ssl_new(s);
  3254. }
  3255. if (hf == sm->ssl_connect)
  3256. s->handshake_func = meth->ssl_connect;
  3257. else if (hf == sm->ssl_accept)
  3258. s->handshake_func = meth->ssl_accept;
  3259. }
  3260. return ret;
  3261. }
  3262. int SSL_get_error(const SSL *s, int i)
  3263. {
  3264. int reason;
  3265. unsigned long l;
  3266. BIO *bio;
  3267. if (i > 0)
  3268. return SSL_ERROR_NONE;
  3269. /*
  3270. * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  3271. * where we do encode the error
  3272. */
  3273. if ((l = ERR_peek_error()) != 0) {
  3274. if (ERR_GET_LIB(l) == ERR_LIB_SYS)
  3275. return SSL_ERROR_SYSCALL;
  3276. else
  3277. return SSL_ERROR_SSL;
  3278. }
  3279. if (SSL_want_read(s)) {
  3280. bio = SSL_get_rbio(s);
  3281. if (BIO_should_read(bio))
  3282. return SSL_ERROR_WANT_READ;
  3283. else if (BIO_should_write(bio))
  3284. /*
  3285. * This one doesn't make too much sense ... We never try to write
  3286. * to the rbio, and an application program where rbio and wbio
  3287. * are separate couldn't even know what it should wait for.
  3288. * However if we ever set s->rwstate incorrectly (so that we have
  3289. * SSL_want_read(s) instead of SSL_want_write(s)) and rbio and
  3290. * wbio *are* the same, this test works around that bug; so it
  3291. * might be safer to keep it.
  3292. */
  3293. return SSL_ERROR_WANT_WRITE;
  3294. else if (BIO_should_io_special(bio)) {
  3295. reason = BIO_get_retry_reason(bio);
  3296. if (reason == BIO_RR_CONNECT)
  3297. return SSL_ERROR_WANT_CONNECT;
  3298. else if (reason == BIO_RR_ACCEPT)
  3299. return SSL_ERROR_WANT_ACCEPT;
  3300. else
  3301. return SSL_ERROR_SYSCALL; /* unknown */
  3302. }
  3303. }
  3304. if (SSL_want_write(s)) {
  3305. /* Access wbio directly - in order to use the buffered bio if present */
  3306. bio = s->wbio;
  3307. if (BIO_should_write(bio))
  3308. return SSL_ERROR_WANT_WRITE;
  3309. else if (BIO_should_read(bio))
  3310. /*
  3311. * See above (SSL_want_read(s) with BIO_should_write(bio))
  3312. */
  3313. return SSL_ERROR_WANT_READ;
  3314. else if (BIO_should_io_special(bio)) {
  3315. reason = BIO_get_retry_reason(bio);
  3316. if (reason == BIO_RR_CONNECT)
  3317. return SSL_ERROR_WANT_CONNECT;
  3318. else if (reason == BIO_RR_ACCEPT)
  3319. return SSL_ERROR_WANT_ACCEPT;
  3320. else
  3321. return SSL_ERROR_SYSCALL;
  3322. }
  3323. }
  3324. if (SSL_want_x509_lookup(s))
  3325. return SSL_ERROR_WANT_X509_LOOKUP;
  3326. if (SSL_want_retry_verify(s))
  3327. return SSL_ERROR_WANT_RETRY_VERIFY;
  3328. if (SSL_want_async(s))
  3329. return SSL_ERROR_WANT_ASYNC;
  3330. if (SSL_want_async_job(s))
  3331. return SSL_ERROR_WANT_ASYNC_JOB;
  3332. if (SSL_want_client_hello_cb(s))
  3333. return SSL_ERROR_WANT_CLIENT_HELLO_CB;
  3334. if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
  3335. (s->s3.warn_alert == SSL_AD_CLOSE_NOTIFY))
  3336. return SSL_ERROR_ZERO_RETURN;
  3337. return SSL_ERROR_SYSCALL;
  3338. }
  3339. static int ssl_do_handshake_intern(void *vargs)
  3340. {
  3341. struct ssl_async_args *args;
  3342. SSL *s;
  3343. args = (struct ssl_async_args *)vargs;
  3344. s = args->s;
  3345. return s->handshake_func(s);
  3346. }
  3347. int SSL_do_handshake(SSL *s)
  3348. {
  3349. int ret = 1;
  3350. if (s->handshake_func == NULL) {
  3351. ERR_raise(ERR_LIB_SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  3352. return -1;
  3353. }
  3354. ossl_statem_check_finish_init(s, -1);
  3355. s->method->ssl_renegotiate_check(s, 0);
  3356. if (SSL_in_init(s) || SSL_in_before(s)) {
  3357. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  3358. struct ssl_async_args args;
  3359. args.s = s;
  3360. ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern);
  3361. } else {
  3362. ret = s->handshake_func(s);
  3363. }
  3364. }
  3365. return ret;
  3366. }
  3367. void SSL_set_accept_state(SSL *s)
  3368. {
  3369. s->server = 1;
  3370. s->shutdown = 0;
  3371. ossl_statem_clear(s);
  3372. s->handshake_func = s->method->ssl_accept;
  3373. clear_ciphers(s);
  3374. }
  3375. void SSL_set_connect_state(SSL *s)
  3376. {
  3377. s->server = 0;
  3378. s->shutdown = 0;
  3379. ossl_statem_clear(s);
  3380. s->handshake_func = s->method->ssl_connect;
  3381. clear_ciphers(s);
  3382. }
  3383. int ssl_undefined_function(SSL *s)
  3384. {
  3385. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3386. return 0;
  3387. }
  3388. int ssl_undefined_void_function(void)
  3389. {
  3390. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3391. return 0;
  3392. }
  3393. int ssl_undefined_const_function(const SSL *s)
  3394. {
  3395. return 0;
  3396. }
  3397. const SSL_METHOD *ssl_bad_method(int ver)
  3398. {
  3399. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3400. return NULL;
  3401. }
  3402. const char *ssl_protocol_to_string(int version)
  3403. {
  3404. switch(version)
  3405. {
  3406. case TLS1_3_VERSION:
  3407. return "TLSv1.3";
  3408. case TLS1_2_VERSION:
  3409. return "TLSv1.2";
  3410. case TLS1_1_VERSION:
  3411. return "TLSv1.1";
  3412. case TLS1_VERSION:
  3413. return "TLSv1";
  3414. case SSL3_VERSION:
  3415. return "SSLv3";
  3416. case DTLS1_BAD_VER:
  3417. return "DTLSv0.9";
  3418. case DTLS1_VERSION:
  3419. return "DTLSv1";
  3420. case DTLS1_2_VERSION:
  3421. return "DTLSv1.2";
  3422. default:
  3423. return "unknown";
  3424. }
  3425. }
  3426. const char *SSL_get_version(const SSL *s)
  3427. {
  3428. return ssl_protocol_to_string(s->version);
  3429. }
  3430. static int dup_ca_names(STACK_OF(X509_NAME) **dst, STACK_OF(X509_NAME) *src)
  3431. {
  3432. STACK_OF(X509_NAME) *sk;
  3433. X509_NAME *xn;
  3434. int i;
  3435. if (src == NULL) {
  3436. *dst = NULL;
  3437. return 1;
  3438. }
  3439. if ((sk = sk_X509_NAME_new_null()) == NULL)
  3440. return 0;
  3441. for (i = 0; i < sk_X509_NAME_num(src); i++) {
  3442. xn = X509_NAME_dup(sk_X509_NAME_value(src, i));
  3443. if (xn == NULL) {
  3444. sk_X509_NAME_pop_free(sk, X509_NAME_free);
  3445. return 0;
  3446. }
  3447. if (sk_X509_NAME_insert(sk, xn, i) == 0) {
  3448. X509_NAME_free(xn);
  3449. sk_X509_NAME_pop_free(sk, X509_NAME_free);
  3450. return 0;
  3451. }
  3452. }
  3453. *dst = sk;
  3454. return 1;
  3455. }
  3456. SSL *SSL_dup(SSL *s)
  3457. {
  3458. SSL *ret;
  3459. int i;
  3460. /* If we're not quiescent, just up_ref! */
  3461. if (!SSL_in_init(s) || !SSL_in_before(s)) {
  3462. CRYPTO_UP_REF(&s->references, &i, s->lock);
  3463. return s;
  3464. }
  3465. /*
  3466. * Otherwise, copy configuration state, and session if set.
  3467. */
  3468. if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
  3469. return NULL;
  3470. if (s->session != NULL) {
  3471. /*
  3472. * Arranges to share the same session via up_ref. This "copies"
  3473. * session-id, SSL_METHOD, sid_ctx, and 'cert'
  3474. */
  3475. if (!SSL_copy_session_id(ret, s))
  3476. goto err;
  3477. } else {
  3478. /*
  3479. * No session has been established yet, so we have to expect that
  3480. * s->cert or ret->cert will be changed later -- they should not both
  3481. * point to the same object, and thus we can't use
  3482. * SSL_copy_session_id.
  3483. */
  3484. if (!SSL_set_ssl_method(ret, s->method))
  3485. goto err;
  3486. if (s->cert != NULL) {
  3487. ssl_cert_free(ret->cert);
  3488. ret->cert = ssl_cert_dup(s->cert);
  3489. if (ret->cert == NULL)
  3490. goto err;
  3491. }
  3492. if (!SSL_set_session_id_context(ret, s->sid_ctx,
  3493. (int)s->sid_ctx_length))
  3494. goto err;
  3495. }
  3496. if (!ssl_dane_dup(ret, s))
  3497. goto err;
  3498. ret->version = s->version;
  3499. ret->options = s->options;
  3500. ret->min_proto_version = s->min_proto_version;
  3501. ret->max_proto_version = s->max_proto_version;
  3502. ret->mode = s->mode;
  3503. SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
  3504. SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
  3505. ret->msg_callback = s->msg_callback;
  3506. ret->msg_callback_arg = s->msg_callback_arg;
  3507. SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));
  3508. SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
  3509. ret->generate_session_id = s->generate_session_id;
  3510. SSL_set_info_callback(ret, SSL_get_info_callback(s));
  3511. /* copy app data, a little dangerous perhaps */
  3512. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
  3513. goto err;
  3514. ret->server = s->server;
  3515. if (s->handshake_func) {
  3516. if (s->server)
  3517. SSL_set_accept_state(ret);
  3518. else
  3519. SSL_set_connect_state(ret);
  3520. }
  3521. ret->shutdown = s->shutdown;
  3522. ret->hit = s->hit;
  3523. ret->default_passwd_callback = s->default_passwd_callback;
  3524. ret->default_passwd_callback_userdata = s->default_passwd_callback_userdata;
  3525. X509_VERIFY_PARAM_inherit(ret->param, s->param);
  3526. /* dup the cipher_list and cipher_list_by_id stacks */
  3527. if (s->cipher_list != NULL) {
  3528. if ((ret->cipher_list = sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
  3529. goto err;
  3530. }
  3531. if (s->cipher_list_by_id != NULL)
  3532. if ((ret->cipher_list_by_id = sk_SSL_CIPHER_dup(s->cipher_list_by_id))
  3533. == NULL)
  3534. goto err;
  3535. /* Dup the client_CA list */
  3536. if (!dup_ca_names(&ret->ca_names, s->ca_names)
  3537. || !dup_ca_names(&ret->client_ca_names, s->client_ca_names))
  3538. goto err;
  3539. return ret;
  3540. err:
  3541. SSL_free(ret);
  3542. return NULL;
  3543. }
  3544. void ssl_clear_cipher_ctx(SSL *s)
  3545. {
  3546. if (s->enc_read_ctx != NULL) {
  3547. EVP_CIPHER_CTX_free(s->enc_read_ctx);
  3548. s->enc_read_ctx = NULL;
  3549. }
  3550. if (s->enc_write_ctx != NULL) {
  3551. EVP_CIPHER_CTX_free(s->enc_write_ctx);
  3552. s->enc_write_ctx = NULL;
  3553. }
  3554. #ifndef OPENSSL_NO_COMP
  3555. COMP_CTX_free(s->expand);
  3556. s->expand = NULL;
  3557. COMP_CTX_free(s->compress);
  3558. s->compress = NULL;
  3559. #endif
  3560. }
  3561. X509 *SSL_get_certificate(const SSL *s)
  3562. {
  3563. if (s->cert != NULL)
  3564. return s->cert->key->x509;
  3565. else
  3566. return NULL;
  3567. }
  3568. EVP_PKEY *SSL_get_privatekey(const SSL *s)
  3569. {
  3570. if (s->cert != NULL)
  3571. return s->cert->key->privatekey;
  3572. else
  3573. return NULL;
  3574. }
  3575. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
  3576. {
  3577. if (ctx->cert != NULL)
  3578. return ctx->cert->key->x509;
  3579. else
  3580. return NULL;
  3581. }
  3582. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
  3583. {
  3584. if (ctx->cert != NULL)
  3585. return ctx->cert->key->privatekey;
  3586. else
  3587. return NULL;
  3588. }
  3589. const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
  3590. {
  3591. if ((s->session != NULL) && (s->session->cipher != NULL))
  3592. return s->session->cipher;
  3593. return NULL;
  3594. }
  3595. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *s)
  3596. {
  3597. return s->s3.tmp.new_cipher;
  3598. }
  3599. const COMP_METHOD *SSL_get_current_compression(const SSL *s)
  3600. {
  3601. #ifndef OPENSSL_NO_COMP
  3602. return s->compress ? COMP_CTX_get_method(s->compress) : NULL;
  3603. #else
  3604. return NULL;
  3605. #endif
  3606. }
  3607. const COMP_METHOD *SSL_get_current_expansion(const SSL *s)
  3608. {
  3609. #ifndef OPENSSL_NO_COMP
  3610. return s->expand ? COMP_CTX_get_method(s->expand) : NULL;
  3611. #else
  3612. return NULL;
  3613. #endif
  3614. }
  3615. int ssl_init_wbio_buffer(SSL *s)
  3616. {
  3617. BIO *bbio;
  3618. if (s->bbio != NULL) {
  3619. /* Already buffered. */
  3620. return 1;
  3621. }
  3622. bbio = BIO_new(BIO_f_buffer());
  3623. if (bbio == NULL || !BIO_set_read_buffer_size(bbio, 1)) {
  3624. BIO_free(bbio);
  3625. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  3626. return 0;
  3627. }
  3628. s->bbio = bbio;
  3629. s->wbio = BIO_push(bbio, s->wbio);
  3630. return 1;
  3631. }
  3632. int ssl_free_wbio_buffer(SSL *s)
  3633. {
  3634. /* callers ensure s is never null */
  3635. if (s->bbio == NULL)
  3636. return 1;
  3637. s->wbio = BIO_pop(s->wbio);
  3638. BIO_free(s->bbio);
  3639. s->bbio = NULL;
  3640. return 1;
  3641. }
  3642. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
  3643. {
  3644. ctx->quiet_shutdown = mode;
  3645. }
  3646. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
  3647. {
  3648. return ctx->quiet_shutdown;
  3649. }
  3650. void SSL_set_quiet_shutdown(SSL *s, int mode)
  3651. {
  3652. s->quiet_shutdown = mode;
  3653. }
  3654. int SSL_get_quiet_shutdown(const SSL *s)
  3655. {
  3656. return s->quiet_shutdown;
  3657. }
  3658. void SSL_set_shutdown(SSL *s, int mode)
  3659. {
  3660. s->shutdown = mode;
  3661. }
  3662. int SSL_get_shutdown(const SSL *s)
  3663. {
  3664. return s->shutdown;
  3665. }
  3666. int SSL_version(const SSL *s)
  3667. {
  3668. return s->version;
  3669. }
  3670. int SSL_client_version(const SSL *s)
  3671. {
  3672. return s->client_version;
  3673. }
  3674. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
  3675. {
  3676. return ssl->ctx;
  3677. }
  3678. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
  3679. {
  3680. CERT *new_cert;
  3681. if (ssl->ctx == ctx)
  3682. return ssl->ctx;
  3683. if (ctx == NULL)
  3684. ctx = ssl->session_ctx;
  3685. new_cert = ssl_cert_dup(ctx->cert);
  3686. if (new_cert == NULL) {
  3687. return NULL;
  3688. }
  3689. if (!custom_exts_copy_flags(&new_cert->custext, &ssl->cert->custext)) {
  3690. ssl_cert_free(new_cert);
  3691. return NULL;
  3692. }
  3693. ssl_cert_free(ssl->cert);
  3694. ssl->cert = new_cert;
  3695. /*
  3696. * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
  3697. * so setter APIs must prevent invalid lengths from entering the system.
  3698. */
  3699. if (!ossl_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx)))
  3700. return NULL;
  3701. /*
  3702. * If the session ID context matches that of the parent SSL_CTX,
  3703. * inherit it from the new SSL_CTX as well. If however the context does
  3704. * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
  3705. * leave it unchanged.
  3706. */
  3707. if ((ssl->ctx != NULL) &&
  3708. (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) &&
  3709. (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) {
  3710. ssl->sid_ctx_length = ctx->sid_ctx_length;
  3711. memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
  3712. }
  3713. SSL_CTX_up_ref(ctx);
  3714. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  3715. ssl->ctx = ctx;
  3716. return ssl->ctx;
  3717. }
  3718. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
  3719. {
  3720. return X509_STORE_set_default_paths_ex(ctx->cert_store, ctx->libctx,
  3721. ctx->propq);
  3722. }
  3723. int SSL_CTX_set_default_verify_dir(SSL_CTX *ctx)
  3724. {
  3725. X509_LOOKUP *lookup;
  3726. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_hash_dir());
  3727. if (lookup == NULL)
  3728. return 0;
  3729. /* We ignore errors, in case the directory doesn't exist */
  3730. ERR_set_mark();
  3731. X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
  3732. ERR_pop_to_mark();
  3733. return 1;
  3734. }
  3735. int SSL_CTX_set_default_verify_file(SSL_CTX *ctx)
  3736. {
  3737. X509_LOOKUP *lookup;
  3738. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_file());
  3739. if (lookup == NULL)
  3740. return 0;
  3741. /* We ignore errors, in case the file doesn't exist */
  3742. ERR_set_mark();
  3743. X509_LOOKUP_load_file_ex(lookup, NULL, X509_FILETYPE_DEFAULT, ctx->libctx,
  3744. ctx->propq);
  3745. ERR_pop_to_mark();
  3746. return 1;
  3747. }
  3748. int SSL_CTX_set_default_verify_store(SSL_CTX *ctx)
  3749. {
  3750. X509_LOOKUP *lookup;
  3751. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_store());
  3752. if (lookup == NULL)
  3753. return 0;
  3754. /* We ignore errors, in case the directory doesn't exist */
  3755. ERR_set_mark();
  3756. X509_LOOKUP_add_store_ex(lookup, NULL, ctx->libctx, ctx->propq);
  3757. ERR_pop_to_mark();
  3758. return 1;
  3759. }
  3760. int SSL_CTX_load_verify_file(SSL_CTX *ctx, const char *CAfile)
  3761. {
  3762. return X509_STORE_load_file_ex(ctx->cert_store, CAfile, ctx->libctx,
  3763. ctx->propq);
  3764. }
  3765. int SSL_CTX_load_verify_dir(SSL_CTX *ctx, const char *CApath)
  3766. {
  3767. return X509_STORE_load_path(ctx->cert_store, CApath);
  3768. }
  3769. int SSL_CTX_load_verify_store(SSL_CTX *ctx, const char *CAstore)
  3770. {
  3771. return X509_STORE_load_store_ex(ctx->cert_store, CAstore, ctx->libctx,
  3772. ctx->propq);
  3773. }
  3774. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
  3775. const char *CApath)
  3776. {
  3777. if (CAfile == NULL && CApath == NULL)
  3778. return 0;
  3779. if (CAfile != NULL && !SSL_CTX_load_verify_file(ctx, CAfile))
  3780. return 0;
  3781. if (CApath != NULL && !SSL_CTX_load_verify_dir(ctx, CApath))
  3782. return 0;
  3783. return 1;
  3784. }
  3785. void SSL_set_info_callback(SSL *ssl,
  3786. void (*cb) (const SSL *ssl, int type, int val))
  3787. {
  3788. ssl->info_callback = cb;
  3789. }
  3790. /*
  3791. * One compiler (Diab DCC) doesn't like argument names in returned function
  3792. * pointer.
  3793. */
  3794. void (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ ,
  3795. int /* type */ ,
  3796. int /* val */ ) {
  3797. return ssl->info_callback;
  3798. }
  3799. void SSL_set_verify_result(SSL *ssl, long arg)
  3800. {
  3801. ssl->verify_result = arg;
  3802. }
  3803. long SSL_get_verify_result(const SSL *ssl)
  3804. {
  3805. return ssl->verify_result;
  3806. }
  3807. size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, size_t outlen)
  3808. {
  3809. if (outlen == 0)
  3810. return sizeof(ssl->s3.client_random);
  3811. if (outlen > sizeof(ssl->s3.client_random))
  3812. outlen = sizeof(ssl->s3.client_random);
  3813. memcpy(out, ssl->s3.client_random, outlen);
  3814. return outlen;
  3815. }
  3816. size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, size_t outlen)
  3817. {
  3818. if (outlen == 0)
  3819. return sizeof(ssl->s3.server_random);
  3820. if (outlen > sizeof(ssl->s3.server_random))
  3821. outlen = sizeof(ssl->s3.server_random);
  3822. memcpy(out, ssl->s3.server_random, outlen);
  3823. return outlen;
  3824. }
  3825. size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
  3826. unsigned char *out, size_t outlen)
  3827. {
  3828. if (outlen == 0)
  3829. return session->master_key_length;
  3830. if (outlen > session->master_key_length)
  3831. outlen = session->master_key_length;
  3832. memcpy(out, session->master_key, outlen);
  3833. return outlen;
  3834. }
  3835. int SSL_SESSION_set1_master_key(SSL_SESSION *sess, const unsigned char *in,
  3836. size_t len)
  3837. {
  3838. if (len > sizeof(sess->master_key))
  3839. return 0;
  3840. memcpy(sess->master_key, in, len);
  3841. sess->master_key_length = len;
  3842. return 1;
  3843. }
  3844. int SSL_set_ex_data(SSL *s, int idx, void *arg)
  3845. {
  3846. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  3847. }
  3848. void *SSL_get_ex_data(const SSL *s, int idx)
  3849. {
  3850. return CRYPTO_get_ex_data(&s->ex_data, idx);
  3851. }
  3852. int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
  3853. {
  3854. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  3855. }
  3856. void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
  3857. {
  3858. return CRYPTO_get_ex_data(&s->ex_data, idx);
  3859. }
  3860. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
  3861. {
  3862. return ctx->cert_store;
  3863. }
  3864. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
  3865. {
  3866. X509_STORE_free(ctx->cert_store);
  3867. ctx->cert_store = store;
  3868. }
  3869. void SSL_CTX_set1_cert_store(SSL_CTX *ctx, X509_STORE *store)
  3870. {
  3871. if (store != NULL)
  3872. X509_STORE_up_ref(store);
  3873. SSL_CTX_set_cert_store(ctx, store);
  3874. }
  3875. int SSL_want(const SSL *s)
  3876. {
  3877. return s->rwstate;
  3878. }
  3879. #ifndef OPENSSL_NO_PSK
  3880. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
  3881. {
  3882. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  3883. ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  3884. return 0;
  3885. }
  3886. OPENSSL_free(ctx->cert->psk_identity_hint);
  3887. if (identity_hint != NULL) {
  3888. ctx->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
  3889. if (ctx->cert->psk_identity_hint == NULL)
  3890. return 0;
  3891. } else
  3892. ctx->cert->psk_identity_hint = NULL;
  3893. return 1;
  3894. }
  3895. int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
  3896. {
  3897. if (s == NULL)
  3898. return 0;
  3899. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  3900. ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  3901. return 0;
  3902. }
  3903. OPENSSL_free(s->cert->psk_identity_hint);
  3904. if (identity_hint != NULL) {
  3905. s->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
  3906. if (s->cert->psk_identity_hint == NULL)
  3907. return 0;
  3908. } else
  3909. s->cert->psk_identity_hint = NULL;
  3910. return 1;
  3911. }
  3912. const char *SSL_get_psk_identity_hint(const SSL *s)
  3913. {
  3914. if (s == NULL || s->session == NULL)
  3915. return NULL;
  3916. return s->session->psk_identity_hint;
  3917. }
  3918. const char *SSL_get_psk_identity(const SSL *s)
  3919. {
  3920. if (s == NULL || s->session == NULL)
  3921. return NULL;
  3922. return s->session->psk_identity;
  3923. }
  3924. void SSL_set_psk_client_callback(SSL *s, SSL_psk_client_cb_func cb)
  3925. {
  3926. s->psk_client_callback = cb;
  3927. }
  3928. void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, SSL_psk_client_cb_func cb)
  3929. {
  3930. ctx->psk_client_callback = cb;
  3931. }
  3932. void SSL_set_psk_server_callback(SSL *s, SSL_psk_server_cb_func cb)
  3933. {
  3934. s->psk_server_callback = cb;
  3935. }
  3936. void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, SSL_psk_server_cb_func cb)
  3937. {
  3938. ctx->psk_server_callback = cb;
  3939. }
  3940. #endif
  3941. void SSL_set_psk_find_session_callback(SSL *s, SSL_psk_find_session_cb_func cb)
  3942. {
  3943. s->psk_find_session_cb = cb;
  3944. }
  3945. void SSL_CTX_set_psk_find_session_callback(SSL_CTX *ctx,
  3946. SSL_psk_find_session_cb_func cb)
  3947. {
  3948. ctx->psk_find_session_cb = cb;
  3949. }
  3950. void SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb)
  3951. {
  3952. s->psk_use_session_cb = cb;
  3953. }
  3954. void SSL_CTX_set_psk_use_session_callback(SSL_CTX *ctx,
  3955. SSL_psk_use_session_cb_func cb)
  3956. {
  3957. ctx->psk_use_session_cb = cb;
  3958. }
  3959. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  3960. void (*cb) (int write_p, int version,
  3961. int content_type, const void *buf,
  3962. size_t len, SSL *ssl, void *arg))
  3963. {
  3964. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  3965. }
  3966. void SSL_set_msg_callback(SSL *ssl,
  3967. void (*cb) (int write_p, int version,
  3968. int content_type, const void *buf,
  3969. size_t len, SSL *ssl, void *arg))
  3970. {
  3971. SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  3972. }
  3973. void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx,
  3974. int (*cb) (SSL *ssl,
  3975. int
  3976. is_forward_secure))
  3977. {
  3978. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
  3979. (void (*)(void))cb);
  3980. }
  3981. void SSL_set_not_resumable_session_callback(SSL *ssl,
  3982. int (*cb) (SSL *ssl,
  3983. int is_forward_secure))
  3984. {
  3985. SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
  3986. (void (*)(void))cb);
  3987. }
  3988. void SSL_CTX_set_record_padding_callback(SSL_CTX *ctx,
  3989. size_t (*cb) (SSL *ssl, int type,
  3990. size_t len, void *arg))
  3991. {
  3992. ctx->record_padding_cb = cb;
  3993. }
  3994. void SSL_CTX_set_record_padding_callback_arg(SSL_CTX *ctx, void *arg)
  3995. {
  3996. ctx->record_padding_arg = arg;
  3997. }
  3998. void *SSL_CTX_get_record_padding_callback_arg(const SSL_CTX *ctx)
  3999. {
  4000. return ctx->record_padding_arg;
  4001. }
  4002. int SSL_CTX_set_block_padding(SSL_CTX *ctx, size_t block_size)
  4003. {
  4004. /* block size of 0 or 1 is basically no padding */
  4005. if (block_size == 1)
  4006. ctx->block_padding = 0;
  4007. else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
  4008. ctx->block_padding = block_size;
  4009. else
  4010. return 0;
  4011. return 1;
  4012. }
  4013. int SSL_set_record_padding_callback(SSL *ssl,
  4014. size_t (*cb) (SSL *ssl, int type,
  4015. size_t len, void *arg))
  4016. {
  4017. BIO *b;
  4018. b = SSL_get_wbio(ssl);
  4019. if (b == NULL || !BIO_get_ktls_send(b)) {
  4020. ssl->record_padding_cb = cb;
  4021. return 1;
  4022. }
  4023. return 0;
  4024. }
  4025. void SSL_set_record_padding_callback_arg(SSL *ssl, void *arg)
  4026. {
  4027. ssl->record_padding_arg = arg;
  4028. }
  4029. void *SSL_get_record_padding_callback_arg(const SSL *ssl)
  4030. {
  4031. return ssl->record_padding_arg;
  4032. }
  4033. int SSL_set_block_padding(SSL *ssl, size_t block_size)
  4034. {
  4035. /* block size of 0 or 1 is basically no padding */
  4036. if (block_size == 1)
  4037. ssl->block_padding = 0;
  4038. else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
  4039. ssl->block_padding = block_size;
  4040. else
  4041. return 0;
  4042. return 1;
  4043. }
  4044. int SSL_set_num_tickets(SSL *s, size_t num_tickets)
  4045. {
  4046. s->num_tickets = num_tickets;
  4047. return 1;
  4048. }
  4049. size_t SSL_get_num_tickets(const SSL *s)
  4050. {
  4051. return s->num_tickets;
  4052. }
  4053. int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets)
  4054. {
  4055. ctx->num_tickets = num_tickets;
  4056. return 1;
  4057. }
  4058. size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx)
  4059. {
  4060. return ctx->num_tickets;
  4061. }
  4062. /*
  4063. * Allocates new EVP_MD_CTX and sets pointer to it into given pointer
  4064. * variable, freeing EVP_MD_CTX previously stored in that variable, if any.
  4065. * If EVP_MD pointer is passed, initializes ctx with this |md|.
  4066. * Returns the newly allocated ctx;
  4067. */
  4068. EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)
  4069. {
  4070. ssl_clear_hash_ctx(hash);
  4071. *hash = EVP_MD_CTX_new();
  4072. if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) {
  4073. EVP_MD_CTX_free(*hash);
  4074. *hash = NULL;
  4075. return NULL;
  4076. }
  4077. return *hash;
  4078. }
  4079. void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
  4080. {
  4081. EVP_MD_CTX_free(*hash);
  4082. *hash = NULL;
  4083. }
  4084. /* Retrieve handshake hashes */
  4085. int ssl_handshake_hash(SSL *s, unsigned char *out, size_t outlen,
  4086. size_t *hashlen)
  4087. {
  4088. EVP_MD_CTX *ctx = NULL;
  4089. EVP_MD_CTX *hdgst = s->s3.handshake_dgst;
  4090. int hashleni = EVP_MD_CTX_get_size(hdgst);
  4091. int ret = 0;
  4092. if (hashleni < 0 || (size_t)hashleni > outlen) {
  4093. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  4094. goto err;
  4095. }
  4096. ctx = EVP_MD_CTX_new();
  4097. if (ctx == NULL) {
  4098. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  4099. goto err;
  4100. }
  4101. if (!EVP_MD_CTX_copy_ex(ctx, hdgst)
  4102. || EVP_DigestFinal_ex(ctx, out, NULL) <= 0) {
  4103. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  4104. goto err;
  4105. }
  4106. *hashlen = hashleni;
  4107. ret = 1;
  4108. err:
  4109. EVP_MD_CTX_free(ctx);
  4110. return ret;
  4111. }
  4112. int SSL_session_reused(const SSL *s)
  4113. {
  4114. return s->hit;
  4115. }
  4116. int SSL_is_server(const SSL *s)
  4117. {
  4118. return s->server;
  4119. }
  4120. #ifndef OPENSSL_NO_DEPRECATED_1_1_0
  4121. void SSL_set_debug(SSL *s, int debug)
  4122. {
  4123. /* Old function was do-nothing anyway... */
  4124. (void)s;
  4125. (void)debug;
  4126. }
  4127. #endif
  4128. void SSL_set_security_level(SSL *s, int level)
  4129. {
  4130. s->cert->sec_level = level;
  4131. }
  4132. int SSL_get_security_level(const SSL *s)
  4133. {
  4134. return s->cert->sec_level;
  4135. }
  4136. void SSL_set_security_callback(SSL *s,
  4137. int (*cb) (const SSL *s, const SSL_CTX *ctx,
  4138. int op, int bits, int nid,
  4139. void *other, void *ex))
  4140. {
  4141. s->cert->sec_cb = cb;
  4142. }
  4143. int (*SSL_get_security_callback(const SSL *s)) (const SSL *s,
  4144. const SSL_CTX *ctx, int op,
  4145. int bits, int nid, void *other,
  4146. void *ex) {
  4147. return s->cert->sec_cb;
  4148. }
  4149. void SSL_set0_security_ex_data(SSL *s, void *ex)
  4150. {
  4151. s->cert->sec_ex = ex;
  4152. }
  4153. void *SSL_get0_security_ex_data(const SSL *s)
  4154. {
  4155. return s->cert->sec_ex;
  4156. }
  4157. void SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
  4158. {
  4159. ctx->cert->sec_level = level;
  4160. }
  4161. int SSL_CTX_get_security_level(const SSL_CTX *ctx)
  4162. {
  4163. return ctx->cert->sec_level;
  4164. }
  4165. void SSL_CTX_set_security_callback(SSL_CTX *ctx,
  4166. int (*cb) (const SSL *s, const SSL_CTX *ctx,
  4167. int op, int bits, int nid,
  4168. void *other, void *ex))
  4169. {
  4170. ctx->cert->sec_cb = cb;
  4171. }
  4172. int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx)) (const SSL *s,
  4173. const SSL_CTX *ctx,
  4174. int op, int bits,
  4175. int nid,
  4176. void *other,
  4177. void *ex) {
  4178. return ctx->cert->sec_cb;
  4179. }
  4180. void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex)
  4181. {
  4182. ctx->cert->sec_ex = ex;
  4183. }
  4184. void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx)
  4185. {
  4186. return ctx->cert->sec_ex;
  4187. }
  4188. uint64_t SSL_CTX_get_options(const SSL_CTX *ctx)
  4189. {
  4190. return ctx->options;
  4191. }
  4192. uint64_t SSL_get_options(const SSL *s)
  4193. {
  4194. return s->options;
  4195. }
  4196. uint64_t SSL_CTX_set_options(SSL_CTX *ctx, uint64_t op)
  4197. {
  4198. return ctx->options |= op;
  4199. }
  4200. uint64_t SSL_set_options(SSL *s, uint64_t op)
  4201. {
  4202. return s->options |= op;
  4203. }
  4204. uint64_t SSL_CTX_clear_options(SSL_CTX *ctx, uint64_t op)
  4205. {
  4206. return ctx->options &= ~op;
  4207. }
  4208. uint64_t SSL_clear_options(SSL *s, uint64_t op)
  4209. {
  4210. return s->options &= ~op;
  4211. }
  4212. STACK_OF(X509) *SSL_get0_verified_chain(const SSL *s)
  4213. {
  4214. return s->verified_chain;
  4215. }
  4216. IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);
  4217. #ifndef OPENSSL_NO_CT
  4218. /*
  4219. * Moves SCTs from the |src| stack to the |dst| stack.
  4220. * The source of each SCT will be set to |origin|.
  4221. * If |dst| points to a NULL pointer, a new stack will be created and owned by
  4222. * the caller.
  4223. * Returns the number of SCTs moved, or a negative integer if an error occurs.
  4224. */
  4225. static int ct_move_scts(STACK_OF(SCT) **dst, STACK_OF(SCT) *src,
  4226. sct_source_t origin)
  4227. {
  4228. int scts_moved = 0;
  4229. SCT *sct = NULL;
  4230. if (*dst == NULL) {
  4231. *dst = sk_SCT_new_null();
  4232. if (*dst == NULL) {
  4233. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  4234. goto err;
  4235. }
  4236. }
  4237. while ((sct = sk_SCT_pop(src)) != NULL) {
  4238. if (SCT_set_source(sct, origin) != 1)
  4239. goto err;
  4240. if (sk_SCT_push(*dst, sct) <= 0)
  4241. goto err;
  4242. scts_moved += 1;
  4243. }
  4244. return scts_moved;
  4245. err:
  4246. if (sct != NULL)
  4247. sk_SCT_push(src, sct); /* Put the SCT back */
  4248. return -1;
  4249. }
  4250. /*
  4251. * Look for data collected during ServerHello and parse if found.
  4252. * Returns the number of SCTs extracted.
  4253. */
  4254. static int ct_extract_tls_extension_scts(SSL *s)
  4255. {
  4256. int scts_extracted = 0;
  4257. if (s->ext.scts != NULL) {
  4258. const unsigned char *p = s->ext.scts;
  4259. STACK_OF(SCT) *scts = o2i_SCT_LIST(NULL, &p, s->ext.scts_len);
  4260. scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_TLS_EXTENSION);
  4261. SCT_LIST_free(scts);
  4262. }
  4263. return scts_extracted;
  4264. }
  4265. /*
  4266. * Checks for an OCSP response and then attempts to extract any SCTs found if it
  4267. * contains an SCT X509 extension. They will be stored in |s->scts|.
  4268. * Returns:
  4269. * - The number of SCTs extracted, assuming an OCSP response exists.
  4270. * - 0 if no OCSP response exists or it contains no SCTs.
  4271. * - A negative integer if an error occurs.
  4272. */
  4273. static int ct_extract_ocsp_response_scts(SSL *s)
  4274. {
  4275. # ifndef OPENSSL_NO_OCSP
  4276. int scts_extracted = 0;
  4277. const unsigned char *p;
  4278. OCSP_BASICRESP *br = NULL;
  4279. OCSP_RESPONSE *rsp = NULL;
  4280. STACK_OF(SCT) *scts = NULL;
  4281. int i;
  4282. if (s->ext.ocsp.resp == NULL || s->ext.ocsp.resp_len == 0)
  4283. goto err;
  4284. p = s->ext.ocsp.resp;
  4285. rsp = d2i_OCSP_RESPONSE(NULL, &p, (int)s->ext.ocsp.resp_len);
  4286. if (rsp == NULL)
  4287. goto err;
  4288. br = OCSP_response_get1_basic(rsp);
  4289. if (br == NULL)
  4290. goto err;
  4291. for (i = 0; i < OCSP_resp_count(br); ++i) {
  4292. OCSP_SINGLERESP *single = OCSP_resp_get0(br, i);
  4293. if (single == NULL)
  4294. continue;
  4295. scts =
  4296. OCSP_SINGLERESP_get1_ext_d2i(single, NID_ct_cert_scts, NULL, NULL);
  4297. scts_extracted =
  4298. ct_move_scts(&s->scts, scts, SCT_SOURCE_OCSP_STAPLED_RESPONSE);
  4299. if (scts_extracted < 0)
  4300. goto err;
  4301. }
  4302. err:
  4303. SCT_LIST_free(scts);
  4304. OCSP_BASICRESP_free(br);
  4305. OCSP_RESPONSE_free(rsp);
  4306. return scts_extracted;
  4307. # else
  4308. /* Behave as if no OCSP response exists */
  4309. return 0;
  4310. # endif
  4311. }
  4312. /*
  4313. * Attempts to extract SCTs from the peer certificate.
  4314. * Return the number of SCTs extracted, or a negative integer if an error
  4315. * occurs.
  4316. */
  4317. static int ct_extract_x509v3_extension_scts(SSL *s)
  4318. {
  4319. int scts_extracted = 0;
  4320. X509 *cert = s->session != NULL ? s->session->peer : NULL;
  4321. if (cert != NULL) {
  4322. STACK_OF(SCT) *scts =
  4323. X509_get_ext_d2i(cert, NID_ct_precert_scts, NULL, NULL);
  4324. scts_extracted =
  4325. ct_move_scts(&s->scts, scts, SCT_SOURCE_X509V3_EXTENSION);
  4326. SCT_LIST_free(scts);
  4327. }
  4328. return scts_extracted;
  4329. }
  4330. /*
  4331. * Attempts to find all received SCTs by checking TLS extensions, the OCSP
  4332. * response (if it exists) and X509v3 extensions in the certificate.
  4333. * Returns NULL if an error occurs.
  4334. */
  4335. const STACK_OF(SCT) *SSL_get0_peer_scts(SSL *s)
  4336. {
  4337. if (!s->scts_parsed) {
  4338. if (ct_extract_tls_extension_scts(s) < 0 ||
  4339. ct_extract_ocsp_response_scts(s) < 0 ||
  4340. ct_extract_x509v3_extension_scts(s) < 0)
  4341. goto err;
  4342. s->scts_parsed = 1;
  4343. }
  4344. return s->scts;
  4345. err:
  4346. return NULL;
  4347. }
  4348. static int ct_permissive(const CT_POLICY_EVAL_CTX * ctx,
  4349. const STACK_OF(SCT) *scts, void *unused_arg)
  4350. {
  4351. return 1;
  4352. }
  4353. static int ct_strict(const CT_POLICY_EVAL_CTX * ctx,
  4354. const STACK_OF(SCT) *scts, void *unused_arg)
  4355. {
  4356. int count = scts != NULL ? sk_SCT_num(scts) : 0;
  4357. int i;
  4358. for (i = 0; i < count; ++i) {
  4359. SCT *sct = sk_SCT_value(scts, i);
  4360. int status = SCT_get_validation_status(sct);
  4361. if (status == SCT_VALIDATION_STATUS_VALID)
  4362. return 1;
  4363. }
  4364. ERR_raise(ERR_LIB_SSL, SSL_R_NO_VALID_SCTS);
  4365. return 0;
  4366. }
  4367. int SSL_set_ct_validation_callback(SSL *s, ssl_ct_validation_cb callback,
  4368. void *arg)
  4369. {
  4370. /*
  4371. * Since code exists that uses the custom extension handler for CT, look
  4372. * for this and throw an error if they have already registered to use CT.
  4373. */
  4374. if (callback != NULL && SSL_CTX_has_client_custom_ext(s->ctx,
  4375. TLSEXT_TYPE_signed_certificate_timestamp))
  4376. {
  4377. ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
  4378. return 0;
  4379. }
  4380. if (callback != NULL) {
  4381. /*
  4382. * If we are validating CT, then we MUST accept SCTs served via OCSP
  4383. */
  4384. if (!SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp))
  4385. return 0;
  4386. }
  4387. s->ct_validation_callback = callback;
  4388. s->ct_validation_callback_arg = arg;
  4389. return 1;
  4390. }
  4391. int SSL_CTX_set_ct_validation_callback(SSL_CTX *ctx,
  4392. ssl_ct_validation_cb callback, void *arg)
  4393. {
  4394. /*
  4395. * Since code exists that uses the custom extension handler for CT, look for
  4396. * this and throw an error if they have already registered to use CT.
  4397. */
  4398. if (callback != NULL && SSL_CTX_has_client_custom_ext(ctx,
  4399. TLSEXT_TYPE_signed_certificate_timestamp))
  4400. {
  4401. ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
  4402. return 0;
  4403. }
  4404. ctx->ct_validation_callback = callback;
  4405. ctx->ct_validation_callback_arg = arg;
  4406. return 1;
  4407. }
  4408. int SSL_ct_is_enabled(const SSL *s)
  4409. {
  4410. return s->ct_validation_callback != NULL;
  4411. }
  4412. int SSL_CTX_ct_is_enabled(const SSL_CTX *ctx)
  4413. {
  4414. return ctx->ct_validation_callback != NULL;
  4415. }
  4416. int ssl_validate_ct(SSL *s)
  4417. {
  4418. int ret = 0;
  4419. X509 *cert = s->session != NULL ? s->session->peer : NULL;
  4420. X509 *issuer;
  4421. SSL_DANE *dane = &s->dane;
  4422. CT_POLICY_EVAL_CTX *ctx = NULL;
  4423. const STACK_OF(SCT) *scts;
  4424. /*
  4425. * If no callback is set, the peer is anonymous, or its chain is invalid,
  4426. * skip SCT validation - just return success. Applications that continue
  4427. * handshakes without certificates, with unverified chains, or pinned leaf
  4428. * certificates are outside the scope of the WebPKI and CT.
  4429. *
  4430. * The above exclusions notwithstanding the vast majority of peers will
  4431. * have rather ordinary certificate chains validated by typical
  4432. * applications that perform certificate verification and therefore will
  4433. * process SCTs when enabled.
  4434. */
  4435. if (s->ct_validation_callback == NULL || cert == NULL ||
  4436. s->verify_result != X509_V_OK ||
  4437. s->verified_chain == NULL || sk_X509_num(s->verified_chain) <= 1)
  4438. return 1;
  4439. /*
  4440. * CT not applicable for chains validated via DANE-TA(2) or DANE-EE(3)
  4441. * trust-anchors. See https://tools.ietf.org/html/rfc7671#section-4.2
  4442. */
  4443. if (DANETLS_ENABLED(dane) && dane->mtlsa != NULL) {
  4444. switch (dane->mtlsa->usage) {
  4445. case DANETLS_USAGE_DANE_TA:
  4446. case DANETLS_USAGE_DANE_EE:
  4447. return 1;
  4448. }
  4449. }
  4450. ctx = CT_POLICY_EVAL_CTX_new_ex(s->ctx->libctx, s->ctx->propq);
  4451. if (ctx == NULL) {
  4452. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  4453. goto end;
  4454. }
  4455. issuer = sk_X509_value(s->verified_chain, 1);
  4456. CT_POLICY_EVAL_CTX_set1_cert(ctx, cert);
  4457. CT_POLICY_EVAL_CTX_set1_issuer(ctx, issuer);
  4458. CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(ctx, s->ctx->ctlog_store);
  4459. CT_POLICY_EVAL_CTX_set_time(
  4460. ctx, (uint64_t)SSL_SESSION_get_time(SSL_get0_session(s)) * 1000);
  4461. scts = SSL_get0_peer_scts(s);
  4462. /*
  4463. * This function returns success (> 0) only when all the SCTs are valid, 0
  4464. * when some are invalid, and < 0 on various internal errors (out of
  4465. * memory, etc.). Having some, or even all, invalid SCTs is not sufficient
  4466. * reason to abort the handshake, that decision is up to the callback.
  4467. * Therefore, we error out only in the unexpected case that the return
  4468. * value is negative.
  4469. *
  4470. * XXX: One might well argue that the return value of this function is an
  4471. * unfortunate design choice. Its job is only to determine the validation
  4472. * status of each of the provided SCTs. So long as it correctly separates
  4473. * the wheat from the chaff it should return success. Failure in this case
  4474. * ought to correspond to an inability to carry out its duties.
  4475. */
  4476. if (SCT_LIST_validate(scts, ctx) < 0) {
  4477. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_SCT_VERIFICATION_FAILED);
  4478. goto end;
  4479. }
  4480. ret = s->ct_validation_callback(ctx, scts, s->ct_validation_callback_arg);
  4481. if (ret < 0)
  4482. ret = 0; /* This function returns 0 on failure */
  4483. if (!ret)
  4484. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_CALLBACK_FAILED);
  4485. end:
  4486. CT_POLICY_EVAL_CTX_free(ctx);
  4487. /*
  4488. * With SSL_VERIFY_NONE the session may be cached and re-used despite a
  4489. * failure return code here. Also the application may wish the complete
  4490. * the handshake, and then disconnect cleanly at a higher layer, after
  4491. * checking the verification status of the completed connection.
  4492. *
  4493. * We therefore force a certificate verification failure which will be
  4494. * visible via SSL_get_verify_result() and cached as part of any resumed
  4495. * session.
  4496. *
  4497. * Note: the permissive callback is for information gathering only, always
  4498. * returns success, and does not affect verification status. Only the
  4499. * strict callback or a custom application-specified callback can trigger
  4500. * connection failure or record a verification error.
  4501. */
  4502. if (ret <= 0)
  4503. s->verify_result = X509_V_ERR_NO_VALID_SCTS;
  4504. return ret;
  4505. }
  4506. int SSL_CTX_enable_ct(SSL_CTX *ctx, int validation_mode)
  4507. {
  4508. switch (validation_mode) {
  4509. default:
  4510. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE);
  4511. return 0;
  4512. case SSL_CT_VALIDATION_PERMISSIVE:
  4513. return SSL_CTX_set_ct_validation_callback(ctx, ct_permissive, NULL);
  4514. case SSL_CT_VALIDATION_STRICT:
  4515. return SSL_CTX_set_ct_validation_callback(ctx, ct_strict, NULL);
  4516. }
  4517. }
  4518. int SSL_enable_ct(SSL *s, int validation_mode)
  4519. {
  4520. switch (validation_mode) {
  4521. default:
  4522. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE);
  4523. return 0;
  4524. case SSL_CT_VALIDATION_PERMISSIVE:
  4525. return SSL_set_ct_validation_callback(s, ct_permissive, NULL);
  4526. case SSL_CT_VALIDATION_STRICT:
  4527. return SSL_set_ct_validation_callback(s, ct_strict, NULL);
  4528. }
  4529. }
  4530. int SSL_CTX_set_default_ctlog_list_file(SSL_CTX *ctx)
  4531. {
  4532. return CTLOG_STORE_load_default_file(ctx->ctlog_store);
  4533. }
  4534. int SSL_CTX_set_ctlog_list_file(SSL_CTX *ctx, const char *path)
  4535. {
  4536. return CTLOG_STORE_load_file(ctx->ctlog_store, path);
  4537. }
  4538. void SSL_CTX_set0_ctlog_store(SSL_CTX *ctx, CTLOG_STORE * logs)
  4539. {
  4540. CTLOG_STORE_free(ctx->ctlog_store);
  4541. ctx->ctlog_store = logs;
  4542. }
  4543. const CTLOG_STORE *SSL_CTX_get0_ctlog_store(const SSL_CTX *ctx)
  4544. {
  4545. return ctx->ctlog_store;
  4546. }
  4547. #endif /* OPENSSL_NO_CT */
  4548. void SSL_CTX_set_client_hello_cb(SSL_CTX *c, SSL_client_hello_cb_fn cb,
  4549. void *arg)
  4550. {
  4551. c->client_hello_cb = cb;
  4552. c->client_hello_cb_arg = arg;
  4553. }
  4554. int SSL_client_hello_isv2(SSL *s)
  4555. {
  4556. if (s->clienthello == NULL)
  4557. return 0;
  4558. return s->clienthello->isv2;
  4559. }
  4560. unsigned int SSL_client_hello_get0_legacy_version(SSL *s)
  4561. {
  4562. if (s->clienthello == NULL)
  4563. return 0;
  4564. return s->clienthello->legacy_version;
  4565. }
  4566. size_t SSL_client_hello_get0_random(SSL *s, const unsigned char **out)
  4567. {
  4568. if (s->clienthello == NULL)
  4569. return 0;
  4570. if (out != NULL)
  4571. *out = s->clienthello->random;
  4572. return SSL3_RANDOM_SIZE;
  4573. }
  4574. size_t SSL_client_hello_get0_session_id(SSL *s, const unsigned char **out)
  4575. {
  4576. if (s->clienthello == NULL)
  4577. return 0;
  4578. if (out != NULL)
  4579. *out = s->clienthello->session_id;
  4580. return s->clienthello->session_id_len;
  4581. }
  4582. size_t SSL_client_hello_get0_ciphers(SSL *s, const unsigned char **out)
  4583. {
  4584. if (s->clienthello == NULL)
  4585. return 0;
  4586. if (out != NULL)
  4587. *out = PACKET_data(&s->clienthello->ciphersuites);
  4588. return PACKET_remaining(&s->clienthello->ciphersuites);
  4589. }
  4590. size_t SSL_client_hello_get0_compression_methods(SSL *s, const unsigned char **out)
  4591. {
  4592. if (s->clienthello == NULL)
  4593. return 0;
  4594. if (out != NULL)
  4595. *out = s->clienthello->compressions;
  4596. return s->clienthello->compressions_len;
  4597. }
  4598. int SSL_client_hello_get1_extensions_present(SSL *s, int **out, size_t *outlen)
  4599. {
  4600. RAW_EXTENSION *ext;
  4601. int *present;
  4602. size_t num = 0, i;
  4603. if (s->clienthello == NULL || out == NULL || outlen == NULL)
  4604. return 0;
  4605. for (i = 0; i < s->clienthello->pre_proc_exts_len; i++) {
  4606. ext = s->clienthello->pre_proc_exts + i;
  4607. if (ext->present)
  4608. num++;
  4609. }
  4610. if (num == 0) {
  4611. *out = NULL;
  4612. *outlen = 0;
  4613. return 1;
  4614. }
  4615. if ((present = OPENSSL_malloc(sizeof(*present) * num)) == NULL) {
  4616. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  4617. return 0;
  4618. }
  4619. for (i = 0; i < s->clienthello->pre_proc_exts_len; i++) {
  4620. ext = s->clienthello->pre_proc_exts + i;
  4621. if (ext->present) {
  4622. if (ext->received_order >= num)
  4623. goto err;
  4624. present[ext->received_order] = ext->type;
  4625. }
  4626. }
  4627. *out = present;
  4628. *outlen = num;
  4629. return 1;
  4630. err:
  4631. OPENSSL_free(present);
  4632. return 0;
  4633. }
  4634. int SSL_client_hello_get0_ext(SSL *s, unsigned int type, const unsigned char **out,
  4635. size_t *outlen)
  4636. {
  4637. size_t i;
  4638. RAW_EXTENSION *r;
  4639. if (s->clienthello == NULL)
  4640. return 0;
  4641. for (i = 0; i < s->clienthello->pre_proc_exts_len; ++i) {
  4642. r = s->clienthello->pre_proc_exts + i;
  4643. if (r->present && r->type == type) {
  4644. if (out != NULL)
  4645. *out = PACKET_data(&r->data);
  4646. if (outlen != NULL)
  4647. *outlen = PACKET_remaining(&r->data);
  4648. return 1;
  4649. }
  4650. }
  4651. return 0;
  4652. }
  4653. int SSL_free_buffers(SSL *ssl)
  4654. {
  4655. RECORD_LAYER *rl = &ssl->rlayer;
  4656. if (RECORD_LAYER_read_pending(rl) || RECORD_LAYER_write_pending(rl))
  4657. return 0;
  4658. RECORD_LAYER_release(rl);
  4659. return 1;
  4660. }
  4661. int SSL_alloc_buffers(SSL *ssl)
  4662. {
  4663. return ssl3_setup_buffers(ssl);
  4664. }
  4665. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb)
  4666. {
  4667. ctx->keylog_callback = cb;
  4668. }
  4669. SSL_CTX_keylog_cb_func SSL_CTX_get_keylog_callback(const SSL_CTX *ctx)
  4670. {
  4671. return ctx->keylog_callback;
  4672. }
  4673. static int nss_keylog_int(const char *prefix,
  4674. SSL *ssl,
  4675. const uint8_t *parameter_1,
  4676. size_t parameter_1_len,
  4677. const uint8_t *parameter_2,
  4678. size_t parameter_2_len)
  4679. {
  4680. char *out = NULL;
  4681. char *cursor = NULL;
  4682. size_t out_len = 0;
  4683. size_t i;
  4684. size_t prefix_len;
  4685. if (ssl->ctx->keylog_callback == NULL)
  4686. return 1;
  4687. /*
  4688. * Our output buffer will contain the following strings, rendered with
  4689. * space characters in between, terminated by a NULL character: first the
  4690. * prefix, then the first parameter, then the second parameter. The
  4691. * meaning of each parameter depends on the specific key material being
  4692. * logged. Note that the first and second parameters are encoded in
  4693. * hexadecimal, so we need a buffer that is twice their lengths.
  4694. */
  4695. prefix_len = strlen(prefix);
  4696. out_len = prefix_len + (2 * parameter_1_len) + (2 * parameter_2_len) + 3;
  4697. if ((out = cursor = OPENSSL_malloc(out_len)) == NULL) {
  4698. SSLfatal(ssl, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  4699. return 0;
  4700. }
  4701. strcpy(cursor, prefix);
  4702. cursor += prefix_len;
  4703. *cursor++ = ' ';
  4704. for (i = 0; i < parameter_1_len; i++) {
  4705. sprintf(cursor, "%02x", parameter_1[i]);
  4706. cursor += 2;
  4707. }
  4708. *cursor++ = ' ';
  4709. for (i = 0; i < parameter_2_len; i++) {
  4710. sprintf(cursor, "%02x", parameter_2[i]);
  4711. cursor += 2;
  4712. }
  4713. *cursor = '\0';
  4714. ssl->ctx->keylog_callback(ssl, (const char *)out);
  4715. OPENSSL_clear_free(out, out_len);
  4716. return 1;
  4717. }
  4718. int ssl_log_rsa_client_key_exchange(SSL *ssl,
  4719. const uint8_t *encrypted_premaster,
  4720. size_t encrypted_premaster_len,
  4721. const uint8_t *premaster,
  4722. size_t premaster_len)
  4723. {
  4724. if (encrypted_premaster_len < 8) {
  4725. SSLfatal(ssl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  4726. return 0;
  4727. }
  4728. /* We only want the first 8 bytes of the encrypted premaster as a tag. */
  4729. return nss_keylog_int("RSA",
  4730. ssl,
  4731. encrypted_premaster,
  4732. 8,
  4733. premaster,
  4734. premaster_len);
  4735. }
  4736. int ssl_log_secret(SSL *ssl,
  4737. const char *label,
  4738. const uint8_t *secret,
  4739. size_t secret_len)
  4740. {
  4741. return nss_keylog_int(label,
  4742. ssl,
  4743. ssl->s3.client_random,
  4744. SSL3_RANDOM_SIZE,
  4745. secret,
  4746. secret_len);
  4747. }
  4748. #define SSLV2_CIPHER_LEN 3
  4749. int ssl_cache_cipherlist(SSL *s, PACKET *cipher_suites, int sslv2format)
  4750. {
  4751. int n;
  4752. n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
  4753. if (PACKET_remaining(cipher_suites) == 0) {
  4754. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED);
  4755. return 0;
  4756. }
  4757. if (PACKET_remaining(cipher_suites) % n != 0) {
  4758. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  4759. return 0;
  4760. }
  4761. OPENSSL_free(s->s3.tmp.ciphers_raw);
  4762. s->s3.tmp.ciphers_raw = NULL;
  4763. s->s3.tmp.ciphers_rawlen = 0;
  4764. if (sslv2format) {
  4765. size_t numciphers = PACKET_remaining(cipher_suites) / n;
  4766. PACKET sslv2ciphers = *cipher_suites;
  4767. unsigned int leadbyte;
  4768. unsigned char *raw;
  4769. /*
  4770. * We store the raw ciphers list in SSLv3+ format so we need to do some
  4771. * preprocessing to convert the list first. If there are any SSLv2 only
  4772. * ciphersuites with a non-zero leading byte then we are going to
  4773. * slightly over allocate because we won't store those. But that isn't a
  4774. * problem.
  4775. */
  4776. raw = OPENSSL_malloc(numciphers * TLS_CIPHER_LEN);
  4777. s->s3.tmp.ciphers_raw = raw;
  4778. if (raw == NULL) {
  4779. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  4780. return 0;
  4781. }
  4782. for (s->s3.tmp.ciphers_rawlen = 0;
  4783. PACKET_remaining(&sslv2ciphers) > 0;
  4784. raw += TLS_CIPHER_LEN) {
  4785. if (!PACKET_get_1(&sslv2ciphers, &leadbyte)
  4786. || (leadbyte == 0
  4787. && !PACKET_copy_bytes(&sslv2ciphers, raw,
  4788. TLS_CIPHER_LEN))
  4789. || (leadbyte != 0
  4790. && !PACKET_forward(&sslv2ciphers, TLS_CIPHER_LEN))) {
  4791. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_PACKET);
  4792. OPENSSL_free(s->s3.tmp.ciphers_raw);
  4793. s->s3.tmp.ciphers_raw = NULL;
  4794. s->s3.tmp.ciphers_rawlen = 0;
  4795. return 0;
  4796. }
  4797. if (leadbyte == 0)
  4798. s->s3.tmp.ciphers_rawlen += TLS_CIPHER_LEN;
  4799. }
  4800. } else if (!PACKET_memdup(cipher_suites, &s->s3.tmp.ciphers_raw,
  4801. &s->s3.tmp.ciphers_rawlen)) {
  4802. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  4803. return 0;
  4804. }
  4805. return 1;
  4806. }
  4807. int SSL_bytes_to_cipher_list(SSL *s, const unsigned char *bytes, size_t len,
  4808. int isv2format, STACK_OF(SSL_CIPHER) **sk,
  4809. STACK_OF(SSL_CIPHER) **scsvs)
  4810. {
  4811. PACKET pkt;
  4812. if (!PACKET_buf_init(&pkt, bytes, len))
  4813. return 0;
  4814. return bytes_to_cipher_list(s, &pkt, sk, scsvs, isv2format, 0);
  4815. }
  4816. int bytes_to_cipher_list(SSL *s, PACKET *cipher_suites,
  4817. STACK_OF(SSL_CIPHER) **skp,
  4818. STACK_OF(SSL_CIPHER) **scsvs_out,
  4819. int sslv2format, int fatal)
  4820. {
  4821. const SSL_CIPHER *c;
  4822. STACK_OF(SSL_CIPHER) *sk = NULL;
  4823. STACK_OF(SSL_CIPHER) *scsvs = NULL;
  4824. int n;
  4825. /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
  4826. unsigned char cipher[SSLV2_CIPHER_LEN];
  4827. n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
  4828. if (PACKET_remaining(cipher_suites) == 0) {
  4829. if (fatal)
  4830. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED);
  4831. else
  4832. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHERS_SPECIFIED);
  4833. return 0;
  4834. }
  4835. if (PACKET_remaining(cipher_suites) % n != 0) {
  4836. if (fatal)
  4837. SSLfatal(s, SSL_AD_DECODE_ERROR,
  4838. SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  4839. else
  4840. ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  4841. return 0;
  4842. }
  4843. sk = sk_SSL_CIPHER_new_null();
  4844. scsvs = sk_SSL_CIPHER_new_null();
  4845. if (sk == NULL || scsvs == NULL) {
  4846. if (fatal)
  4847. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  4848. else
  4849. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  4850. goto err;
  4851. }
  4852. while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
  4853. /*
  4854. * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
  4855. * first byte set to zero, while true SSLv2 ciphers have a non-zero
  4856. * first byte. We don't support any true SSLv2 ciphers, so skip them.
  4857. */
  4858. if (sslv2format && cipher[0] != '\0')
  4859. continue;
  4860. /* For SSLv2-compat, ignore leading 0-byte. */
  4861. c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher, 1);
  4862. if (c != NULL) {
  4863. if ((c->valid && !sk_SSL_CIPHER_push(sk, c)) ||
  4864. (!c->valid && !sk_SSL_CIPHER_push(scsvs, c))) {
  4865. if (fatal)
  4866. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  4867. else
  4868. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  4869. goto err;
  4870. }
  4871. }
  4872. }
  4873. if (PACKET_remaining(cipher_suites) > 0) {
  4874. if (fatal)
  4875. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
  4876. else
  4877. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
  4878. goto err;
  4879. }
  4880. if (skp != NULL)
  4881. *skp = sk;
  4882. else
  4883. sk_SSL_CIPHER_free(sk);
  4884. if (scsvs_out != NULL)
  4885. *scsvs_out = scsvs;
  4886. else
  4887. sk_SSL_CIPHER_free(scsvs);
  4888. return 1;
  4889. err:
  4890. sk_SSL_CIPHER_free(sk);
  4891. sk_SSL_CIPHER_free(scsvs);
  4892. return 0;
  4893. }
  4894. int SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data)
  4895. {
  4896. ctx->max_early_data = max_early_data;
  4897. return 1;
  4898. }
  4899. uint32_t SSL_CTX_get_max_early_data(const SSL_CTX *ctx)
  4900. {
  4901. return ctx->max_early_data;
  4902. }
  4903. int SSL_set_max_early_data(SSL *s, uint32_t max_early_data)
  4904. {
  4905. s->max_early_data = max_early_data;
  4906. return 1;
  4907. }
  4908. uint32_t SSL_get_max_early_data(const SSL *s)
  4909. {
  4910. return s->max_early_data;
  4911. }
  4912. int SSL_CTX_set_recv_max_early_data(SSL_CTX *ctx, uint32_t recv_max_early_data)
  4913. {
  4914. ctx->recv_max_early_data = recv_max_early_data;
  4915. return 1;
  4916. }
  4917. uint32_t SSL_CTX_get_recv_max_early_data(const SSL_CTX *ctx)
  4918. {
  4919. return ctx->recv_max_early_data;
  4920. }
  4921. int SSL_set_recv_max_early_data(SSL *s, uint32_t recv_max_early_data)
  4922. {
  4923. s->recv_max_early_data = recv_max_early_data;
  4924. return 1;
  4925. }
  4926. uint32_t SSL_get_recv_max_early_data(const SSL *s)
  4927. {
  4928. return s->recv_max_early_data;
  4929. }
  4930. __owur unsigned int ssl_get_max_send_fragment(const SSL *ssl)
  4931. {
  4932. /* Return any active Max Fragment Len extension */
  4933. if (ssl->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(ssl->session))
  4934. return GET_MAX_FRAGMENT_LENGTH(ssl->session);
  4935. /* return current SSL connection setting */
  4936. return ssl->max_send_fragment;
  4937. }
  4938. __owur unsigned int ssl_get_split_send_fragment(const SSL *ssl)
  4939. {
  4940. /* Return a value regarding an active Max Fragment Len extension */
  4941. if (ssl->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(ssl->session)
  4942. && ssl->split_send_fragment > GET_MAX_FRAGMENT_LENGTH(ssl->session))
  4943. return GET_MAX_FRAGMENT_LENGTH(ssl->session);
  4944. /* else limit |split_send_fragment| to current |max_send_fragment| */
  4945. if (ssl->split_send_fragment > ssl->max_send_fragment)
  4946. return ssl->max_send_fragment;
  4947. /* return current SSL connection setting */
  4948. return ssl->split_send_fragment;
  4949. }
  4950. int SSL_stateless(SSL *s)
  4951. {
  4952. int ret;
  4953. /* Ensure there is no state left over from a previous invocation */
  4954. if (!SSL_clear(s))
  4955. return 0;
  4956. ERR_clear_error();
  4957. s->s3.flags |= TLS1_FLAGS_STATELESS;
  4958. ret = SSL_accept(s);
  4959. s->s3.flags &= ~TLS1_FLAGS_STATELESS;
  4960. if (ret > 0 && s->ext.cookieok)
  4961. return 1;
  4962. if (s->hello_retry_request == SSL_HRR_PENDING && !ossl_statem_in_error(s))
  4963. return 0;
  4964. return -1;
  4965. }
  4966. void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
  4967. {
  4968. ctx->pha_enabled = val;
  4969. }
  4970. void SSL_set_post_handshake_auth(SSL *ssl, int val)
  4971. {
  4972. ssl->pha_enabled = val;
  4973. }
  4974. int SSL_verify_client_post_handshake(SSL *ssl)
  4975. {
  4976. if (!SSL_IS_TLS13(ssl)) {
  4977. ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
  4978. return 0;
  4979. }
  4980. if (!ssl->server) {
  4981. ERR_raise(ERR_LIB_SSL, SSL_R_NOT_SERVER);
  4982. return 0;
  4983. }
  4984. if (!SSL_is_init_finished(ssl)) {
  4985. ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT);
  4986. return 0;
  4987. }
  4988. switch (ssl->post_handshake_auth) {
  4989. case SSL_PHA_NONE:
  4990. ERR_raise(ERR_LIB_SSL, SSL_R_EXTENSION_NOT_RECEIVED);
  4991. return 0;
  4992. default:
  4993. case SSL_PHA_EXT_SENT:
  4994. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  4995. return 0;
  4996. case SSL_PHA_EXT_RECEIVED:
  4997. break;
  4998. case SSL_PHA_REQUEST_PENDING:
  4999. ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_PENDING);
  5000. return 0;
  5001. case SSL_PHA_REQUESTED:
  5002. ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_SENT);
  5003. return 0;
  5004. }
  5005. ssl->post_handshake_auth = SSL_PHA_REQUEST_PENDING;
  5006. /* checks verify_mode and algorithm_auth */
  5007. if (!send_certificate_request(ssl)) {
  5008. ssl->post_handshake_auth = SSL_PHA_EXT_RECEIVED; /* restore on error */
  5009. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CONFIG);
  5010. return 0;
  5011. }
  5012. ossl_statem_set_in_init(ssl, 1);
  5013. return 1;
  5014. }
  5015. int SSL_CTX_set_session_ticket_cb(SSL_CTX *ctx,
  5016. SSL_CTX_generate_session_ticket_fn gen_cb,
  5017. SSL_CTX_decrypt_session_ticket_fn dec_cb,
  5018. void *arg)
  5019. {
  5020. ctx->generate_ticket_cb = gen_cb;
  5021. ctx->decrypt_ticket_cb = dec_cb;
  5022. ctx->ticket_cb_data = arg;
  5023. return 1;
  5024. }
  5025. void SSL_CTX_set_allow_early_data_cb(SSL_CTX *ctx,
  5026. SSL_allow_early_data_cb_fn cb,
  5027. void *arg)
  5028. {
  5029. ctx->allow_early_data_cb = cb;
  5030. ctx->allow_early_data_cb_data = arg;
  5031. }
  5032. void SSL_set_allow_early_data_cb(SSL *s,
  5033. SSL_allow_early_data_cb_fn cb,
  5034. void *arg)
  5035. {
  5036. s->allow_early_data_cb = cb;
  5037. s->allow_early_data_cb_data = arg;
  5038. }
  5039. const EVP_CIPHER *ssl_evp_cipher_fetch(OSSL_LIB_CTX *libctx,
  5040. int nid,
  5041. const char *properties)
  5042. {
  5043. const EVP_CIPHER *ciph;
  5044. ciph = tls_get_cipher_from_engine(nid);
  5045. if (ciph != NULL)
  5046. return ciph;
  5047. /*
  5048. * If there is no engine cipher then we do an explicit fetch. This may fail
  5049. * and that could be ok
  5050. */
  5051. ERR_set_mark();
  5052. ciph = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(nid), properties);
  5053. ERR_pop_to_mark();
  5054. return ciph;
  5055. }
  5056. int ssl_evp_cipher_up_ref(const EVP_CIPHER *cipher)
  5057. {
  5058. /* Don't up-ref an implicit EVP_CIPHER */
  5059. if (EVP_CIPHER_get0_provider(cipher) == NULL)
  5060. return 1;
  5061. /*
  5062. * The cipher was explicitly fetched and therefore it is safe to cast
  5063. * away the const
  5064. */
  5065. return EVP_CIPHER_up_ref((EVP_CIPHER *)cipher);
  5066. }
  5067. void ssl_evp_cipher_free(const EVP_CIPHER *cipher)
  5068. {
  5069. if (cipher == NULL)
  5070. return;
  5071. if (EVP_CIPHER_get0_provider(cipher) != NULL) {
  5072. /*
  5073. * The cipher was explicitly fetched and therefore it is safe to cast
  5074. * away the const
  5075. */
  5076. EVP_CIPHER_free((EVP_CIPHER *)cipher);
  5077. }
  5078. }
  5079. const EVP_MD *ssl_evp_md_fetch(OSSL_LIB_CTX *libctx,
  5080. int nid,
  5081. const char *properties)
  5082. {
  5083. const EVP_MD *md;
  5084. md = tls_get_digest_from_engine(nid);
  5085. if (md != NULL)
  5086. return md;
  5087. /* Otherwise we do an explicit fetch */
  5088. ERR_set_mark();
  5089. md = EVP_MD_fetch(libctx, OBJ_nid2sn(nid), properties);
  5090. ERR_pop_to_mark();
  5091. return md;
  5092. }
  5093. int ssl_evp_md_up_ref(const EVP_MD *md)
  5094. {
  5095. /* Don't up-ref an implicit EVP_MD */
  5096. if (EVP_MD_get0_provider(md) == NULL)
  5097. return 1;
  5098. /*
  5099. * The digest was explicitly fetched and therefore it is safe to cast
  5100. * away the const
  5101. */
  5102. return EVP_MD_up_ref((EVP_MD *)md);
  5103. }
  5104. void ssl_evp_md_free(const EVP_MD *md)
  5105. {
  5106. if (md == NULL)
  5107. return;
  5108. if (EVP_MD_get0_provider(md) != NULL) {
  5109. /*
  5110. * The digest was explicitly fetched and therefore it is safe to cast
  5111. * away the const
  5112. */
  5113. EVP_MD_free((EVP_MD *)md);
  5114. }
  5115. }
  5116. int SSL_set0_tmp_dh_pkey(SSL *s, EVP_PKEY *dhpkey)
  5117. {
  5118. if (!ssl_security(s, SSL_SECOP_TMP_DH,
  5119. EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) {
  5120. ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL);
  5121. EVP_PKEY_free(dhpkey);
  5122. return 0;
  5123. }
  5124. EVP_PKEY_free(s->cert->dh_tmp);
  5125. s->cert->dh_tmp = dhpkey;
  5126. return 1;
  5127. }
  5128. int SSL_CTX_set0_tmp_dh_pkey(SSL_CTX *ctx, EVP_PKEY *dhpkey)
  5129. {
  5130. if (!ssl_ctx_security(ctx, SSL_SECOP_TMP_DH,
  5131. EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) {
  5132. ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL);
  5133. EVP_PKEY_free(dhpkey);
  5134. return 0;
  5135. }
  5136. EVP_PKEY_free(ctx->cert->dh_tmp);
  5137. ctx->cert->dh_tmp = dhpkey;
  5138. return 1;
  5139. }