ssl_lib.c 158 KB

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