ssl_lib.c 158 KB

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