ssl_lib.c 158 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709
  1. /*
  2. * Copyright 1995-2022 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. * processed or unprocessed data available or 0 otherwise (as opposed to the
  1292. * number of bytes available). Unlike SSL_pending() this will take into
  1293. * account read_ahead data. A 1 return simply indicates that we have data.
  1294. * That data may not result in any application data, or we may fail to parse
  1295. * the records for some reason.
  1296. */
  1297. /* Check buffered app data if any first */
  1298. if (SSL_IS_DTLS(s)) {
  1299. DTLS1_RECORD_DATA *rdata;
  1300. pitem *item, *iter;
  1301. iter = pqueue_iterator(s->rlayer.d->buffered_app_data.q);
  1302. while ((item = pqueue_next(&iter)) != NULL) {
  1303. rdata = item->data;
  1304. if (rdata->rrec.length > 0)
  1305. return 1;
  1306. }
  1307. }
  1308. if (RECORD_LAYER_processed_read_pending(&s->rlayer))
  1309. return 1;
  1310. return RECORD_LAYER_read_pending(&s->rlayer);
  1311. }
  1312. X509 *SSL_get_peer_certificate(const SSL *s)
  1313. {
  1314. X509 *r;
  1315. if ((s == NULL) || (s->session == NULL))
  1316. r = NULL;
  1317. else
  1318. r = s->session->peer;
  1319. if (r == NULL)
  1320. return r;
  1321. X509_up_ref(r);
  1322. return r;
  1323. }
  1324. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
  1325. {
  1326. STACK_OF(X509) *r;
  1327. if ((s == NULL) || (s->session == NULL))
  1328. r = NULL;
  1329. else
  1330. r = s->session->peer_chain;
  1331. /*
  1332. * If we are a client, cert_chain includes the peer's own certificate; if
  1333. * we are a server, it does not.
  1334. */
  1335. return r;
  1336. }
  1337. /*
  1338. * Now in theory, since the calling process own 't' it should be safe to
  1339. * modify. We need to be able to read f without being hassled
  1340. */
  1341. int SSL_copy_session_id(SSL *t, const SSL *f)
  1342. {
  1343. int i;
  1344. /* Do we need to to SSL locking? */
  1345. if (!SSL_set_session(t, SSL_get_session(f))) {
  1346. return 0;
  1347. }
  1348. /*
  1349. * what if we are setup for one protocol version but want to talk another
  1350. */
  1351. if (t->method != f->method) {
  1352. t->method->ssl_free(t);
  1353. t->method = f->method;
  1354. if (t->method->ssl_new(t) == 0)
  1355. return 0;
  1356. }
  1357. CRYPTO_UP_REF(&f->cert->references, &i, f->cert->lock);
  1358. ssl_cert_free(t->cert);
  1359. t->cert = f->cert;
  1360. if (!SSL_set_session_id_context(t, f->sid_ctx, (int)f->sid_ctx_length)) {
  1361. return 0;
  1362. }
  1363. return 1;
  1364. }
  1365. /* Fix this so it checks all the valid key/cert options */
  1366. int SSL_CTX_check_private_key(const SSL_CTX *ctx)
  1367. {
  1368. if ((ctx == NULL) || (ctx->cert->key->x509 == NULL)) {
  1369. SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED);
  1370. return 0;
  1371. }
  1372. if (ctx->cert->key->privatekey == NULL) {
  1373. SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1374. return 0;
  1375. }
  1376. return X509_check_private_key
  1377. (ctx->cert->key->x509, ctx->cert->key->privatekey);
  1378. }
  1379. /* Fix this function so that it takes an optional type parameter */
  1380. int SSL_check_private_key(const SSL *ssl)
  1381. {
  1382. if (ssl == NULL) {
  1383. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
  1384. return 0;
  1385. }
  1386. if (ssl->cert->key->x509 == NULL) {
  1387. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED);
  1388. return 0;
  1389. }
  1390. if (ssl->cert->key->privatekey == NULL) {
  1391. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1392. return 0;
  1393. }
  1394. return X509_check_private_key(ssl->cert->key->x509,
  1395. ssl->cert->key->privatekey);
  1396. }
  1397. int SSL_waiting_for_async(SSL *s)
  1398. {
  1399. if (s->job)
  1400. return 1;
  1401. return 0;
  1402. }
  1403. int SSL_get_all_async_fds(SSL *s, OSSL_ASYNC_FD *fds, size_t *numfds)
  1404. {
  1405. ASYNC_WAIT_CTX *ctx = s->waitctx;
  1406. if (ctx == NULL)
  1407. return 0;
  1408. return ASYNC_WAIT_CTX_get_all_fds(ctx, fds, numfds);
  1409. }
  1410. int SSL_get_changed_async_fds(SSL *s, OSSL_ASYNC_FD *addfd, size_t *numaddfds,
  1411. OSSL_ASYNC_FD *delfd, size_t *numdelfds)
  1412. {
  1413. ASYNC_WAIT_CTX *ctx = s->waitctx;
  1414. if (ctx == NULL)
  1415. return 0;
  1416. return ASYNC_WAIT_CTX_get_changed_fds(ctx, addfd, numaddfds, delfd,
  1417. numdelfds);
  1418. }
  1419. int SSL_accept(SSL *s)
  1420. {
  1421. if (s->handshake_func == NULL) {
  1422. /* Not properly initialized yet */
  1423. SSL_set_accept_state(s);
  1424. }
  1425. return SSL_do_handshake(s);
  1426. }
  1427. int SSL_connect(SSL *s)
  1428. {
  1429. if (s->handshake_func == NULL) {
  1430. /* Not properly initialized yet */
  1431. SSL_set_connect_state(s);
  1432. }
  1433. return SSL_do_handshake(s);
  1434. }
  1435. long SSL_get_default_timeout(const SSL *s)
  1436. {
  1437. return s->method->get_timeout();
  1438. }
  1439. static int ssl_start_async_job(SSL *s, struct ssl_async_args *args,
  1440. int (*func) (void *))
  1441. {
  1442. int ret;
  1443. if (s->waitctx == NULL) {
  1444. s->waitctx = ASYNC_WAIT_CTX_new();
  1445. if (s->waitctx == NULL)
  1446. return -1;
  1447. }
  1448. s->rwstate = SSL_NOTHING;
  1449. switch (ASYNC_start_job(&s->job, s->waitctx, &ret, func, args,
  1450. sizeof(struct ssl_async_args))) {
  1451. case ASYNC_ERR:
  1452. s->rwstate = SSL_NOTHING;
  1453. SSLerr(SSL_F_SSL_START_ASYNC_JOB, SSL_R_FAILED_TO_INIT_ASYNC);
  1454. return -1;
  1455. case ASYNC_PAUSE:
  1456. s->rwstate = SSL_ASYNC_PAUSED;
  1457. return -1;
  1458. case ASYNC_NO_JOBS:
  1459. s->rwstate = SSL_ASYNC_NO_JOBS;
  1460. return -1;
  1461. case ASYNC_FINISH:
  1462. s->job = NULL;
  1463. return ret;
  1464. default:
  1465. s->rwstate = SSL_NOTHING;
  1466. SSLerr(SSL_F_SSL_START_ASYNC_JOB, ERR_R_INTERNAL_ERROR);
  1467. /* Shouldn't happen */
  1468. return -1;
  1469. }
  1470. }
  1471. static int ssl_io_intern(void *vargs)
  1472. {
  1473. struct ssl_async_args *args;
  1474. SSL *s;
  1475. void *buf;
  1476. size_t num;
  1477. args = (struct ssl_async_args *)vargs;
  1478. s = args->s;
  1479. buf = args->buf;
  1480. num = args->num;
  1481. switch (args->type) {
  1482. case READFUNC:
  1483. return args->f.func_read(s, buf, num, &s->asyncrw);
  1484. case WRITEFUNC:
  1485. return args->f.func_write(s, buf, num, &s->asyncrw);
  1486. case OTHERFUNC:
  1487. return args->f.func_other(s);
  1488. }
  1489. return -1;
  1490. }
  1491. int ssl_read_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
  1492. {
  1493. if (s->handshake_func == NULL) {
  1494. SSLerr(SSL_F_SSL_READ_INTERNAL, SSL_R_UNINITIALIZED);
  1495. return -1;
  1496. }
  1497. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  1498. s->rwstate = SSL_NOTHING;
  1499. return 0;
  1500. }
  1501. if (s->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
  1502. || s->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY) {
  1503. SSLerr(SSL_F_SSL_READ_INTERNAL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1504. return 0;
  1505. }
  1506. /*
  1507. * If we are a client and haven't received the ServerHello etc then we
  1508. * better do that
  1509. */
  1510. ossl_statem_check_finish_init(s, 0);
  1511. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1512. struct ssl_async_args args;
  1513. int ret;
  1514. args.s = s;
  1515. args.buf = buf;
  1516. args.num = num;
  1517. args.type = READFUNC;
  1518. args.f.func_read = s->method->ssl_read;
  1519. ret = ssl_start_async_job(s, &args, ssl_io_intern);
  1520. *readbytes = s->asyncrw;
  1521. return ret;
  1522. } else {
  1523. return s->method->ssl_read(s, buf, num, readbytes);
  1524. }
  1525. }
  1526. int SSL_read(SSL *s, void *buf, int num)
  1527. {
  1528. int ret;
  1529. size_t readbytes;
  1530. if (num < 0) {
  1531. SSLerr(SSL_F_SSL_READ, SSL_R_BAD_LENGTH);
  1532. return -1;
  1533. }
  1534. ret = ssl_read_internal(s, buf, (size_t)num, &readbytes);
  1535. /*
  1536. * The cast is safe here because ret should be <= INT_MAX because num is
  1537. * <= INT_MAX
  1538. */
  1539. if (ret > 0)
  1540. ret = (int)readbytes;
  1541. return ret;
  1542. }
  1543. int SSL_read_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
  1544. {
  1545. int ret = ssl_read_internal(s, buf, num, readbytes);
  1546. if (ret < 0)
  1547. ret = 0;
  1548. return ret;
  1549. }
  1550. int SSL_read_early_data(SSL *s, void *buf, size_t num, size_t *readbytes)
  1551. {
  1552. int ret;
  1553. if (!s->server) {
  1554. SSLerr(SSL_F_SSL_READ_EARLY_DATA, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1555. return SSL_READ_EARLY_DATA_ERROR;
  1556. }
  1557. switch (s->early_data_state) {
  1558. case SSL_EARLY_DATA_NONE:
  1559. if (!SSL_in_before(s)) {
  1560. SSLerr(SSL_F_SSL_READ_EARLY_DATA,
  1561. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1562. return SSL_READ_EARLY_DATA_ERROR;
  1563. }
  1564. /* fall through */
  1565. case SSL_EARLY_DATA_ACCEPT_RETRY:
  1566. s->early_data_state = SSL_EARLY_DATA_ACCEPTING;
  1567. ret = SSL_accept(s);
  1568. if (ret <= 0) {
  1569. /* NBIO or error */
  1570. s->early_data_state = SSL_EARLY_DATA_ACCEPT_RETRY;
  1571. return SSL_READ_EARLY_DATA_ERROR;
  1572. }
  1573. /* fall through */
  1574. case SSL_EARLY_DATA_READ_RETRY:
  1575. if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
  1576. s->early_data_state = SSL_EARLY_DATA_READING;
  1577. ret = SSL_read_ex(s, buf, num, readbytes);
  1578. /*
  1579. * State machine will update early_data_state to
  1580. * SSL_EARLY_DATA_FINISHED_READING if we get an EndOfEarlyData
  1581. * message
  1582. */
  1583. if (ret > 0 || (ret <= 0 && s->early_data_state
  1584. != SSL_EARLY_DATA_FINISHED_READING)) {
  1585. s->early_data_state = SSL_EARLY_DATA_READ_RETRY;
  1586. return ret > 0 ? SSL_READ_EARLY_DATA_SUCCESS
  1587. : SSL_READ_EARLY_DATA_ERROR;
  1588. }
  1589. } else {
  1590. s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
  1591. }
  1592. *readbytes = 0;
  1593. return SSL_READ_EARLY_DATA_FINISH;
  1594. default:
  1595. SSLerr(SSL_F_SSL_READ_EARLY_DATA, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1596. return SSL_READ_EARLY_DATA_ERROR;
  1597. }
  1598. }
  1599. int SSL_get_early_data_status(const SSL *s)
  1600. {
  1601. return s->ext.early_data;
  1602. }
  1603. static int ssl_peek_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
  1604. {
  1605. if (s->handshake_func == NULL) {
  1606. SSLerr(SSL_F_SSL_PEEK_INTERNAL, SSL_R_UNINITIALIZED);
  1607. return -1;
  1608. }
  1609. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  1610. return 0;
  1611. }
  1612. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1613. struct ssl_async_args args;
  1614. int ret;
  1615. args.s = s;
  1616. args.buf = buf;
  1617. args.num = num;
  1618. args.type = READFUNC;
  1619. args.f.func_read = s->method->ssl_peek;
  1620. ret = ssl_start_async_job(s, &args, ssl_io_intern);
  1621. *readbytes = s->asyncrw;
  1622. return ret;
  1623. } else {
  1624. return s->method->ssl_peek(s, buf, num, readbytes);
  1625. }
  1626. }
  1627. int SSL_peek(SSL *s, void *buf, int num)
  1628. {
  1629. int ret;
  1630. size_t readbytes;
  1631. if (num < 0) {
  1632. SSLerr(SSL_F_SSL_PEEK, SSL_R_BAD_LENGTH);
  1633. return -1;
  1634. }
  1635. ret = ssl_peek_internal(s, buf, (size_t)num, &readbytes);
  1636. /*
  1637. * The cast is safe here because ret should be <= INT_MAX because num is
  1638. * <= INT_MAX
  1639. */
  1640. if (ret > 0)
  1641. ret = (int)readbytes;
  1642. return ret;
  1643. }
  1644. int SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
  1645. {
  1646. int ret = ssl_peek_internal(s, buf, num, readbytes);
  1647. if (ret < 0)
  1648. ret = 0;
  1649. return ret;
  1650. }
  1651. int ssl_write_internal(SSL *s, const void *buf, size_t num, size_t *written)
  1652. {
  1653. if (s->handshake_func == NULL) {
  1654. SSLerr(SSL_F_SSL_WRITE_INTERNAL, SSL_R_UNINITIALIZED);
  1655. return -1;
  1656. }
  1657. if (s->shutdown & SSL_SENT_SHUTDOWN) {
  1658. s->rwstate = SSL_NOTHING;
  1659. SSLerr(SSL_F_SSL_WRITE_INTERNAL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  1660. return -1;
  1661. }
  1662. if (s->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
  1663. || s->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY
  1664. || s->early_data_state == SSL_EARLY_DATA_READ_RETRY) {
  1665. SSLerr(SSL_F_SSL_WRITE_INTERNAL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1666. return 0;
  1667. }
  1668. /* If we are a client and haven't sent the Finished we better do that */
  1669. ossl_statem_check_finish_init(s, 1);
  1670. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1671. int ret;
  1672. struct ssl_async_args args;
  1673. args.s = s;
  1674. args.buf = (void *)buf;
  1675. args.num = num;
  1676. args.type = WRITEFUNC;
  1677. args.f.func_write = s->method->ssl_write;
  1678. ret = ssl_start_async_job(s, &args, ssl_io_intern);
  1679. *written = s->asyncrw;
  1680. return ret;
  1681. } else {
  1682. return s->method->ssl_write(s, buf, num, written);
  1683. }
  1684. }
  1685. int SSL_write(SSL *s, const void *buf, int num)
  1686. {
  1687. int ret;
  1688. size_t written;
  1689. if (num < 0) {
  1690. SSLerr(SSL_F_SSL_WRITE, SSL_R_BAD_LENGTH);
  1691. return -1;
  1692. }
  1693. ret = ssl_write_internal(s, buf, (size_t)num, &written);
  1694. /*
  1695. * The cast is safe here because ret should be <= INT_MAX because num is
  1696. * <= INT_MAX
  1697. */
  1698. if (ret > 0)
  1699. ret = (int)written;
  1700. return ret;
  1701. }
  1702. int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written)
  1703. {
  1704. int ret = ssl_write_internal(s, buf, num, written);
  1705. if (ret < 0)
  1706. ret = 0;
  1707. return ret;
  1708. }
  1709. int SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written)
  1710. {
  1711. int ret, early_data_state;
  1712. size_t writtmp;
  1713. uint32_t partialwrite;
  1714. switch (s->early_data_state) {
  1715. case SSL_EARLY_DATA_NONE:
  1716. if (s->server
  1717. || !SSL_in_before(s)
  1718. || ((s->session == NULL || s->session->ext.max_early_data == 0)
  1719. && (s->psk_use_session_cb == NULL))) {
  1720. SSLerr(SSL_F_SSL_WRITE_EARLY_DATA,
  1721. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1722. return 0;
  1723. }
  1724. /* fall through */
  1725. case SSL_EARLY_DATA_CONNECT_RETRY:
  1726. s->early_data_state = SSL_EARLY_DATA_CONNECTING;
  1727. ret = SSL_connect(s);
  1728. if (ret <= 0) {
  1729. /* NBIO or error */
  1730. s->early_data_state = SSL_EARLY_DATA_CONNECT_RETRY;
  1731. return 0;
  1732. }
  1733. /* fall through */
  1734. case SSL_EARLY_DATA_WRITE_RETRY:
  1735. s->early_data_state = SSL_EARLY_DATA_WRITING;
  1736. /*
  1737. * We disable partial write for early data because we don't keep track
  1738. * of how many bytes we've written between the SSL_write_ex() call and
  1739. * the flush if the flush needs to be retried)
  1740. */
  1741. partialwrite = s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE;
  1742. s->mode &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
  1743. ret = SSL_write_ex(s, buf, num, &writtmp);
  1744. s->mode |= partialwrite;
  1745. if (!ret) {
  1746. s->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
  1747. return ret;
  1748. }
  1749. s->early_data_state = SSL_EARLY_DATA_WRITE_FLUSH;
  1750. /* fall through */
  1751. case SSL_EARLY_DATA_WRITE_FLUSH:
  1752. /* The buffering BIO is still in place so we need to flush it */
  1753. if (statem_flush(s) != 1)
  1754. return 0;
  1755. *written = num;
  1756. s->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
  1757. return 1;
  1758. case SSL_EARLY_DATA_FINISHED_READING:
  1759. case SSL_EARLY_DATA_READ_RETRY:
  1760. early_data_state = s->early_data_state;
  1761. /* We are a server writing to an unauthenticated client */
  1762. s->early_data_state = SSL_EARLY_DATA_UNAUTH_WRITING;
  1763. ret = SSL_write_ex(s, buf, num, written);
  1764. /* The buffering BIO is still in place */
  1765. if (ret)
  1766. (void)BIO_flush(s->wbio);
  1767. s->early_data_state = early_data_state;
  1768. return ret;
  1769. default:
  1770. SSLerr(SSL_F_SSL_WRITE_EARLY_DATA, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1771. return 0;
  1772. }
  1773. }
  1774. int SSL_shutdown(SSL *s)
  1775. {
  1776. /*
  1777. * Note that this function behaves differently from what one might
  1778. * expect. Return values are 0 for no success (yet), 1 for success; but
  1779. * calling it once is usually not enough, even if blocking I/O is used
  1780. * (see ssl3_shutdown).
  1781. */
  1782. if (s->handshake_func == NULL) {
  1783. SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
  1784. return -1;
  1785. }
  1786. if (!SSL_in_init(s)) {
  1787. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1788. struct ssl_async_args args;
  1789. memset(&args, 0, sizeof(args));
  1790. args.s = s;
  1791. args.type = OTHERFUNC;
  1792. args.f.func_other = s->method->ssl_shutdown;
  1793. return ssl_start_async_job(s, &args, ssl_io_intern);
  1794. } else {
  1795. return s->method->ssl_shutdown(s);
  1796. }
  1797. } else {
  1798. SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_SHUTDOWN_WHILE_IN_INIT);
  1799. return -1;
  1800. }
  1801. }
  1802. int SSL_key_update(SSL *s, int updatetype)
  1803. {
  1804. /*
  1805. * TODO(TLS1.3): How will applications know whether TLSv1.3 has been
  1806. * negotiated, and that it is appropriate to call SSL_key_update() instead
  1807. * of SSL_renegotiate().
  1808. */
  1809. if (!SSL_IS_TLS13(s)) {
  1810. SSLerr(SSL_F_SSL_KEY_UPDATE, SSL_R_WRONG_SSL_VERSION);
  1811. return 0;
  1812. }
  1813. if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
  1814. && updatetype != SSL_KEY_UPDATE_REQUESTED) {
  1815. SSLerr(SSL_F_SSL_KEY_UPDATE, SSL_R_INVALID_KEY_UPDATE_TYPE);
  1816. return 0;
  1817. }
  1818. if (!SSL_is_init_finished(s)) {
  1819. SSLerr(SSL_F_SSL_KEY_UPDATE, SSL_R_STILL_IN_INIT);
  1820. return 0;
  1821. }
  1822. if (RECORD_LAYER_write_pending(&s->rlayer)) {
  1823. SSLerr(SSL_F_SSL_KEY_UPDATE, SSL_R_BAD_WRITE_RETRY);
  1824. return 0;
  1825. }
  1826. ossl_statem_set_in_init(s, 1);
  1827. s->key_update = updatetype;
  1828. return 1;
  1829. }
  1830. int SSL_get_key_update_type(const SSL *s)
  1831. {
  1832. return s->key_update;
  1833. }
  1834. int SSL_renegotiate(SSL *s)
  1835. {
  1836. if (SSL_IS_TLS13(s)) {
  1837. SSLerr(SSL_F_SSL_RENEGOTIATE, SSL_R_WRONG_SSL_VERSION);
  1838. return 0;
  1839. }
  1840. if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
  1841. SSLerr(SSL_F_SSL_RENEGOTIATE, SSL_R_NO_RENEGOTIATION);
  1842. return 0;
  1843. }
  1844. s->renegotiate = 1;
  1845. s->new_session = 1;
  1846. return s->method->ssl_renegotiate(s);
  1847. }
  1848. int SSL_renegotiate_abbreviated(SSL *s)
  1849. {
  1850. if (SSL_IS_TLS13(s)) {
  1851. SSLerr(SSL_F_SSL_RENEGOTIATE_ABBREVIATED, SSL_R_WRONG_SSL_VERSION);
  1852. return 0;
  1853. }
  1854. if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
  1855. SSLerr(SSL_F_SSL_RENEGOTIATE_ABBREVIATED, SSL_R_NO_RENEGOTIATION);
  1856. return 0;
  1857. }
  1858. s->renegotiate = 1;
  1859. s->new_session = 0;
  1860. return s->method->ssl_renegotiate(s);
  1861. }
  1862. int SSL_renegotiate_pending(const SSL *s)
  1863. {
  1864. /*
  1865. * becomes true when negotiation is requested; false again once a
  1866. * handshake has finished
  1867. */
  1868. return (s->renegotiate != 0);
  1869. }
  1870. long SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
  1871. {
  1872. long l;
  1873. switch (cmd) {
  1874. case SSL_CTRL_GET_READ_AHEAD:
  1875. return RECORD_LAYER_get_read_ahead(&s->rlayer);
  1876. case SSL_CTRL_SET_READ_AHEAD:
  1877. l = RECORD_LAYER_get_read_ahead(&s->rlayer);
  1878. RECORD_LAYER_set_read_ahead(&s->rlayer, larg);
  1879. return l;
  1880. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  1881. s->msg_callback_arg = parg;
  1882. return 1;
  1883. case SSL_CTRL_MODE:
  1884. return (s->mode |= larg);
  1885. case SSL_CTRL_CLEAR_MODE:
  1886. return (s->mode &= ~larg);
  1887. case SSL_CTRL_GET_MAX_CERT_LIST:
  1888. return (long)s->max_cert_list;
  1889. case SSL_CTRL_SET_MAX_CERT_LIST:
  1890. if (larg < 0)
  1891. return 0;
  1892. l = (long)s->max_cert_list;
  1893. s->max_cert_list = (size_t)larg;
  1894. return l;
  1895. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  1896. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
  1897. return 0;
  1898. s->max_send_fragment = larg;
  1899. if (s->max_send_fragment < s->split_send_fragment)
  1900. s->split_send_fragment = s->max_send_fragment;
  1901. return 1;
  1902. case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
  1903. if ((size_t)larg > s->max_send_fragment || larg == 0)
  1904. return 0;
  1905. s->split_send_fragment = larg;
  1906. return 1;
  1907. case SSL_CTRL_SET_MAX_PIPELINES:
  1908. if (larg < 1 || larg > SSL_MAX_PIPELINES)
  1909. return 0;
  1910. s->max_pipelines = larg;
  1911. if (larg > 1)
  1912. RECORD_LAYER_set_read_ahead(&s->rlayer, 1);
  1913. return 1;
  1914. case SSL_CTRL_GET_RI_SUPPORT:
  1915. if (s->s3)
  1916. return s->s3->send_connection_binding;
  1917. else
  1918. return 0;
  1919. case SSL_CTRL_CERT_FLAGS:
  1920. return (s->cert->cert_flags |= larg);
  1921. case SSL_CTRL_CLEAR_CERT_FLAGS:
  1922. return (s->cert->cert_flags &= ~larg);
  1923. case SSL_CTRL_GET_RAW_CIPHERLIST:
  1924. if (parg) {
  1925. if (s->s3->tmp.ciphers_raw == NULL)
  1926. return 0;
  1927. *(unsigned char **)parg = s->s3->tmp.ciphers_raw;
  1928. return (int)s->s3->tmp.ciphers_rawlen;
  1929. } else {
  1930. return TLS_CIPHER_LEN;
  1931. }
  1932. case SSL_CTRL_GET_EXTMS_SUPPORT:
  1933. if (!s->session || SSL_in_init(s) || ossl_statem_get_in_handshake(s))
  1934. return -1;
  1935. if (s->session->flags & SSL_SESS_FLAG_EXTMS)
  1936. return 1;
  1937. else
  1938. return 0;
  1939. case SSL_CTRL_SET_MIN_PROTO_VERSION:
  1940. return ssl_check_allowed_versions(larg, s->max_proto_version)
  1941. && ssl_set_version_bound(s->ctx->method->version, (int)larg,
  1942. &s->min_proto_version);
  1943. case SSL_CTRL_GET_MIN_PROTO_VERSION:
  1944. return s->min_proto_version;
  1945. case SSL_CTRL_SET_MAX_PROTO_VERSION:
  1946. return ssl_check_allowed_versions(s->min_proto_version, larg)
  1947. && ssl_set_version_bound(s->ctx->method->version, (int)larg,
  1948. &s->max_proto_version);
  1949. case SSL_CTRL_GET_MAX_PROTO_VERSION:
  1950. return s->max_proto_version;
  1951. default:
  1952. return s->method->ssl_ctrl(s, cmd, larg, parg);
  1953. }
  1954. }
  1955. long SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
  1956. {
  1957. switch (cmd) {
  1958. case SSL_CTRL_SET_MSG_CALLBACK:
  1959. s->msg_callback = (void (*)
  1960. (int write_p, int version, int content_type,
  1961. const void *buf, size_t len, SSL *ssl,
  1962. void *arg))(fp);
  1963. return 1;
  1964. default:
  1965. return s->method->ssl_callback_ctrl(s, cmd, fp);
  1966. }
  1967. }
  1968. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
  1969. {
  1970. return ctx->sessions;
  1971. }
  1972. long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
  1973. {
  1974. long l;
  1975. /* For some cases with ctx == NULL perform syntax checks */
  1976. if (ctx == NULL) {
  1977. switch (cmd) {
  1978. #ifndef OPENSSL_NO_EC
  1979. case SSL_CTRL_SET_GROUPS_LIST:
  1980. return tls1_set_groups_list(NULL, NULL, parg);
  1981. #endif
  1982. case SSL_CTRL_SET_SIGALGS_LIST:
  1983. case SSL_CTRL_SET_CLIENT_SIGALGS_LIST:
  1984. return tls1_set_sigalgs_list(NULL, parg, 0);
  1985. default:
  1986. return 0;
  1987. }
  1988. }
  1989. switch (cmd) {
  1990. case SSL_CTRL_GET_READ_AHEAD:
  1991. return ctx->read_ahead;
  1992. case SSL_CTRL_SET_READ_AHEAD:
  1993. l = ctx->read_ahead;
  1994. ctx->read_ahead = larg;
  1995. return l;
  1996. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  1997. ctx->msg_callback_arg = parg;
  1998. return 1;
  1999. case SSL_CTRL_GET_MAX_CERT_LIST:
  2000. return (long)ctx->max_cert_list;
  2001. case SSL_CTRL_SET_MAX_CERT_LIST:
  2002. if (larg < 0)
  2003. return 0;
  2004. l = (long)ctx->max_cert_list;
  2005. ctx->max_cert_list = (size_t)larg;
  2006. return l;
  2007. case SSL_CTRL_SET_SESS_CACHE_SIZE:
  2008. if (larg < 0)
  2009. return 0;
  2010. l = (long)ctx->session_cache_size;
  2011. ctx->session_cache_size = (size_t)larg;
  2012. return l;
  2013. case SSL_CTRL_GET_SESS_CACHE_SIZE:
  2014. return (long)ctx->session_cache_size;
  2015. case SSL_CTRL_SET_SESS_CACHE_MODE:
  2016. l = ctx->session_cache_mode;
  2017. ctx->session_cache_mode = larg;
  2018. return l;
  2019. case SSL_CTRL_GET_SESS_CACHE_MODE:
  2020. return ctx->session_cache_mode;
  2021. case SSL_CTRL_SESS_NUMBER:
  2022. return lh_SSL_SESSION_num_items(ctx->sessions);
  2023. case SSL_CTRL_SESS_CONNECT:
  2024. return tsan_load(&ctx->stats.sess_connect);
  2025. case SSL_CTRL_SESS_CONNECT_GOOD:
  2026. return tsan_load(&ctx->stats.sess_connect_good);
  2027. case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
  2028. return tsan_load(&ctx->stats.sess_connect_renegotiate);
  2029. case SSL_CTRL_SESS_ACCEPT:
  2030. return tsan_load(&ctx->stats.sess_accept);
  2031. case SSL_CTRL_SESS_ACCEPT_GOOD:
  2032. return tsan_load(&ctx->stats.sess_accept_good);
  2033. case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
  2034. return tsan_load(&ctx->stats.sess_accept_renegotiate);
  2035. case SSL_CTRL_SESS_HIT:
  2036. return tsan_load(&ctx->stats.sess_hit);
  2037. case SSL_CTRL_SESS_CB_HIT:
  2038. return tsan_load(&ctx->stats.sess_cb_hit);
  2039. case SSL_CTRL_SESS_MISSES:
  2040. return tsan_load(&ctx->stats.sess_miss);
  2041. case SSL_CTRL_SESS_TIMEOUTS:
  2042. return tsan_load(&ctx->stats.sess_timeout);
  2043. case SSL_CTRL_SESS_CACHE_FULL:
  2044. return tsan_load(&ctx->stats.sess_cache_full);
  2045. case SSL_CTRL_MODE:
  2046. return (ctx->mode |= larg);
  2047. case SSL_CTRL_CLEAR_MODE:
  2048. return (ctx->mode &= ~larg);
  2049. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  2050. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
  2051. return 0;
  2052. ctx->max_send_fragment = larg;
  2053. if (ctx->max_send_fragment < ctx->split_send_fragment)
  2054. ctx->split_send_fragment = ctx->max_send_fragment;
  2055. return 1;
  2056. case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
  2057. if ((size_t)larg > ctx->max_send_fragment || larg == 0)
  2058. return 0;
  2059. ctx->split_send_fragment = larg;
  2060. return 1;
  2061. case SSL_CTRL_SET_MAX_PIPELINES:
  2062. if (larg < 1 || larg > SSL_MAX_PIPELINES)
  2063. return 0;
  2064. ctx->max_pipelines = larg;
  2065. return 1;
  2066. case SSL_CTRL_CERT_FLAGS:
  2067. return (ctx->cert->cert_flags |= larg);
  2068. case SSL_CTRL_CLEAR_CERT_FLAGS:
  2069. return (ctx->cert->cert_flags &= ~larg);
  2070. case SSL_CTRL_SET_MIN_PROTO_VERSION:
  2071. return ssl_check_allowed_versions(larg, ctx->max_proto_version)
  2072. && ssl_set_version_bound(ctx->method->version, (int)larg,
  2073. &ctx->min_proto_version);
  2074. case SSL_CTRL_GET_MIN_PROTO_VERSION:
  2075. return ctx->min_proto_version;
  2076. case SSL_CTRL_SET_MAX_PROTO_VERSION:
  2077. return ssl_check_allowed_versions(ctx->min_proto_version, larg)
  2078. && ssl_set_version_bound(ctx->method->version, (int)larg,
  2079. &ctx->max_proto_version);
  2080. case SSL_CTRL_GET_MAX_PROTO_VERSION:
  2081. return ctx->max_proto_version;
  2082. default:
  2083. return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
  2084. }
  2085. }
  2086. long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
  2087. {
  2088. switch (cmd) {
  2089. case SSL_CTRL_SET_MSG_CALLBACK:
  2090. ctx->msg_callback = (void (*)
  2091. (int write_p, int version, int content_type,
  2092. const void *buf, size_t len, SSL *ssl,
  2093. void *arg))(fp);
  2094. return 1;
  2095. default:
  2096. return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
  2097. }
  2098. }
  2099. int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
  2100. {
  2101. if (a->id > b->id)
  2102. return 1;
  2103. if (a->id < b->id)
  2104. return -1;
  2105. return 0;
  2106. }
  2107. int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap,
  2108. const SSL_CIPHER *const *bp)
  2109. {
  2110. if ((*ap)->id > (*bp)->id)
  2111. return 1;
  2112. if ((*ap)->id < (*bp)->id)
  2113. return -1;
  2114. return 0;
  2115. }
  2116. /** return a STACK of the ciphers available for the SSL and in order of
  2117. * preference */
  2118. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
  2119. {
  2120. if (s != NULL) {
  2121. if (s->cipher_list != NULL) {
  2122. return s->cipher_list;
  2123. } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) {
  2124. return s->ctx->cipher_list;
  2125. }
  2126. }
  2127. return NULL;
  2128. }
  2129. STACK_OF(SSL_CIPHER) *SSL_get_client_ciphers(const SSL *s)
  2130. {
  2131. if ((s == NULL) || !s->server)
  2132. return NULL;
  2133. return s->peer_ciphers;
  2134. }
  2135. STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s)
  2136. {
  2137. STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers;
  2138. int i;
  2139. ciphers = SSL_get_ciphers(s);
  2140. if (!ciphers)
  2141. return NULL;
  2142. if (!ssl_set_client_disabled(s))
  2143. return NULL;
  2144. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  2145. const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
  2146. if (!ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) {
  2147. if (!sk)
  2148. sk = sk_SSL_CIPHER_new_null();
  2149. if (!sk)
  2150. return NULL;
  2151. if (!sk_SSL_CIPHER_push(sk, c)) {
  2152. sk_SSL_CIPHER_free(sk);
  2153. return NULL;
  2154. }
  2155. }
  2156. }
  2157. return sk;
  2158. }
  2159. /** return a STACK of the ciphers available for the SSL and in order of
  2160. * algorithm id */
  2161. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
  2162. {
  2163. if (s != NULL) {
  2164. if (s->cipher_list_by_id != NULL) {
  2165. return s->cipher_list_by_id;
  2166. } else if ((s->ctx != NULL) && (s->ctx->cipher_list_by_id != NULL)) {
  2167. return s->ctx->cipher_list_by_id;
  2168. }
  2169. }
  2170. return NULL;
  2171. }
  2172. /** The old interface to get the same thing as SSL_get_ciphers() */
  2173. const char *SSL_get_cipher_list(const SSL *s, int n)
  2174. {
  2175. const SSL_CIPHER *c;
  2176. STACK_OF(SSL_CIPHER) *sk;
  2177. if (s == NULL)
  2178. return NULL;
  2179. sk = SSL_get_ciphers(s);
  2180. if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
  2181. return NULL;
  2182. c = sk_SSL_CIPHER_value(sk, n);
  2183. if (c == NULL)
  2184. return NULL;
  2185. return c->name;
  2186. }
  2187. /** return a STACK of the ciphers available for the SSL_CTX and in order of
  2188. * preference */
  2189. STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx)
  2190. {
  2191. if (ctx != NULL)
  2192. return ctx->cipher_list;
  2193. return NULL;
  2194. }
  2195. /*
  2196. * Distinguish between ciphers controlled by set_ciphersuite() and
  2197. * set_cipher_list() when counting.
  2198. */
  2199. static int cipher_list_tls12_num(STACK_OF(SSL_CIPHER) *sk)
  2200. {
  2201. int i, num = 0;
  2202. const SSL_CIPHER *c;
  2203. if (sk == NULL)
  2204. return 0;
  2205. for (i = 0; i < sk_SSL_CIPHER_num(sk); ++i) {
  2206. c = sk_SSL_CIPHER_value(sk, i);
  2207. if (c->min_tls >= TLS1_3_VERSION)
  2208. continue;
  2209. num++;
  2210. }
  2211. return num;
  2212. }
  2213. /** specify the ciphers to be used by default by the SSL_CTX */
  2214. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
  2215. {
  2216. STACK_OF(SSL_CIPHER) *sk;
  2217. sk = ssl_create_cipher_list(ctx->method, ctx->tls13_ciphersuites,
  2218. &ctx->cipher_list, &ctx->cipher_list_by_id, str,
  2219. ctx->cert);
  2220. /*
  2221. * ssl_create_cipher_list may return an empty stack if it was unable to
  2222. * find a cipher matching the given rule string (for example if the rule
  2223. * string specifies a cipher which has been disabled). This is not an
  2224. * error as far as ssl_create_cipher_list is concerned, and hence
  2225. * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
  2226. */
  2227. if (sk == NULL)
  2228. return 0;
  2229. else if (cipher_list_tls12_num(sk) == 0) {
  2230. SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
  2231. return 0;
  2232. }
  2233. return 1;
  2234. }
  2235. /** specify the ciphers to be used by the SSL */
  2236. int SSL_set_cipher_list(SSL *s, const char *str)
  2237. {
  2238. STACK_OF(SSL_CIPHER) *sk;
  2239. sk = ssl_create_cipher_list(s->ctx->method, s->tls13_ciphersuites,
  2240. &s->cipher_list, &s->cipher_list_by_id, str,
  2241. s->cert);
  2242. /* see comment in SSL_CTX_set_cipher_list */
  2243. if (sk == NULL)
  2244. return 0;
  2245. else if (cipher_list_tls12_num(sk) == 0) {
  2246. SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
  2247. return 0;
  2248. }
  2249. return 1;
  2250. }
  2251. char *SSL_get_shared_ciphers(const SSL *s, char *buf, int size)
  2252. {
  2253. char *p;
  2254. STACK_OF(SSL_CIPHER) *clntsk, *srvrsk;
  2255. const SSL_CIPHER *c;
  2256. int i;
  2257. if (!s->server
  2258. || s->peer_ciphers == NULL
  2259. || size < 2)
  2260. return NULL;
  2261. p = buf;
  2262. clntsk = s->peer_ciphers;
  2263. srvrsk = SSL_get_ciphers(s);
  2264. if (clntsk == NULL || srvrsk == NULL)
  2265. return NULL;
  2266. if (sk_SSL_CIPHER_num(clntsk) == 0 || sk_SSL_CIPHER_num(srvrsk) == 0)
  2267. return NULL;
  2268. for (i = 0; i < sk_SSL_CIPHER_num(clntsk); i++) {
  2269. int n;
  2270. c = sk_SSL_CIPHER_value(clntsk, i);
  2271. if (sk_SSL_CIPHER_find(srvrsk, c) < 0)
  2272. continue;
  2273. n = strlen(c->name);
  2274. if (n + 1 > size) {
  2275. if (p != buf)
  2276. --p;
  2277. *p = '\0';
  2278. return buf;
  2279. }
  2280. strcpy(p, c->name);
  2281. p += n;
  2282. *(p++) = ':';
  2283. size -= n + 1;
  2284. }
  2285. p[-1] = '\0';
  2286. return buf;
  2287. }
  2288. /**
  2289. * Return the requested servername (SNI) value. Note that the behaviour varies
  2290. * depending on:
  2291. * - whether this is called by the client or the server,
  2292. * - if we are before or during/after the handshake,
  2293. * - if a resumption or normal handshake is being attempted/has occurred
  2294. * - whether we have negotiated TLSv1.2 (or below) or TLSv1.3
  2295. *
  2296. * Note that only the host_name type is defined (RFC 3546).
  2297. */
  2298. const char *SSL_get_servername(const SSL *s, const int type)
  2299. {
  2300. /*
  2301. * If we don't know if we are the client or the server yet then we assume
  2302. * client.
  2303. */
  2304. int server = s->handshake_func == NULL ? 0 : s->server;
  2305. if (type != TLSEXT_NAMETYPE_host_name)
  2306. return NULL;
  2307. if (server) {
  2308. /**
  2309. * Server side
  2310. * In TLSv1.3 on the server SNI is not associated with the session
  2311. * but in TLSv1.2 or below it is.
  2312. *
  2313. * Before the handshake:
  2314. * - return NULL
  2315. *
  2316. * During/after the handshake (TLSv1.2 or below resumption occurred):
  2317. * - If a servername was accepted by the server in the original
  2318. * handshake then it will return that servername, or NULL otherwise.
  2319. *
  2320. * During/after the handshake (TLSv1.2 or below resumption did not occur):
  2321. * - The function will return the servername requested by the client in
  2322. * this handshake or NULL if none was requested.
  2323. */
  2324. if (s->hit && !SSL_IS_TLS13(s))
  2325. return s->session->ext.hostname;
  2326. } else {
  2327. /**
  2328. * Client side
  2329. *
  2330. * Before the handshake:
  2331. * - If a servername has been set via a call to
  2332. * SSL_set_tlsext_host_name() then it will return that servername
  2333. * - If one has not been set, but a TLSv1.2 resumption is being
  2334. * attempted and the session from the original handshake had a
  2335. * servername accepted by the server then it will return that
  2336. * servername
  2337. * - Otherwise it returns NULL
  2338. *
  2339. * During/after the handshake (TLSv1.2 or below resumption occurred):
  2340. * - If the session from the original handshake had a servername accepted
  2341. * by the server then it will return that servername.
  2342. * - Otherwise it returns the servername set via
  2343. * SSL_set_tlsext_host_name() (or NULL if it was not called).
  2344. *
  2345. * During/after the handshake (TLSv1.2 or below resumption did not occur):
  2346. * - It will return the servername set via SSL_set_tlsext_host_name()
  2347. * (or NULL if it was not called).
  2348. */
  2349. if (SSL_in_before(s)) {
  2350. if (s->ext.hostname == NULL
  2351. && s->session != NULL
  2352. && s->session->ssl_version != TLS1_3_VERSION)
  2353. return s->session->ext.hostname;
  2354. } else {
  2355. if (!SSL_IS_TLS13(s) && s->hit && s->session->ext.hostname != NULL)
  2356. return s->session->ext.hostname;
  2357. }
  2358. }
  2359. return s->ext.hostname;
  2360. }
  2361. int SSL_get_servername_type(const SSL *s)
  2362. {
  2363. if (SSL_get_servername(s, TLSEXT_NAMETYPE_host_name) != NULL)
  2364. return TLSEXT_NAMETYPE_host_name;
  2365. return -1;
  2366. }
  2367. /*
  2368. * SSL_select_next_proto implements the standard protocol selection. It is
  2369. * expected that this function is called from the callback set by
  2370. * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
  2371. * vector of 8-bit, length prefixed byte strings. The length byte itself is
  2372. * not included in the length. A byte string of length 0 is invalid. No byte
  2373. * string may be truncated. The current, but experimental algorithm for
  2374. * selecting the protocol is: 1) If the server doesn't support NPN then this
  2375. * is indicated to the callback. In this case, the client application has to
  2376. * abort the connection or have a default application level protocol. 2) If
  2377. * the server supports NPN, but advertises an empty list then the client
  2378. * selects the first protocol in its list, but indicates via the API that this
  2379. * fallback case was enacted. 3) Otherwise, the client finds the first
  2380. * protocol in the server's list that it supports and selects this protocol.
  2381. * This is because it's assumed that the server has better information about
  2382. * which protocol a client should use. 4) If the client doesn't support any
  2383. * of the server's advertised protocols, then this is treated the same as
  2384. * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
  2385. * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
  2386. */
  2387. int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
  2388. const unsigned char *server,
  2389. unsigned int server_len,
  2390. const unsigned char *client, unsigned int client_len)
  2391. {
  2392. unsigned int i, j;
  2393. const unsigned char *result;
  2394. int status = OPENSSL_NPN_UNSUPPORTED;
  2395. /*
  2396. * For each protocol in server preference order, see if we support it.
  2397. */
  2398. for (i = 0; i < server_len;) {
  2399. for (j = 0; j < client_len;) {
  2400. if (server[i] == client[j] &&
  2401. memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  2402. /* We found a match */
  2403. result = &server[i];
  2404. status = OPENSSL_NPN_NEGOTIATED;
  2405. goto found;
  2406. }
  2407. j += client[j];
  2408. j++;
  2409. }
  2410. i += server[i];
  2411. i++;
  2412. }
  2413. /* There's no overlap between our protocols and the server's list. */
  2414. result = client;
  2415. status = OPENSSL_NPN_NO_OVERLAP;
  2416. found:
  2417. *out = (unsigned char *)result + 1;
  2418. *outlen = result[0];
  2419. return status;
  2420. }
  2421. #ifndef OPENSSL_NO_NEXTPROTONEG
  2422. /*
  2423. * SSL_get0_next_proto_negotiated sets *data and *len to point to the
  2424. * client's requested protocol for this connection and returns 0. If the
  2425. * client didn't request any protocol, then *data is set to NULL. Note that
  2426. * the client can request any protocol it chooses. The value returned from
  2427. * this function need not be a member of the list of supported protocols
  2428. * provided by the callback.
  2429. */
  2430. void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
  2431. unsigned *len)
  2432. {
  2433. *data = s->ext.npn;
  2434. if (!*data) {
  2435. *len = 0;
  2436. } else {
  2437. *len = (unsigned int)s->ext.npn_len;
  2438. }
  2439. }
  2440. /*
  2441. * SSL_CTX_set_npn_advertised_cb sets a callback that is called when
  2442. * a TLS server needs a list of supported protocols for Next Protocol
  2443. * Negotiation. The returned list must be in wire format. The list is
  2444. * returned by setting |out| to point to it and |outlen| to its length. This
  2445. * memory will not be modified, but one should assume that the SSL* keeps a
  2446. * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
  2447. * wishes to advertise. Otherwise, no such extension will be included in the
  2448. * ServerHello.
  2449. */
  2450. void SSL_CTX_set_npn_advertised_cb(SSL_CTX *ctx,
  2451. SSL_CTX_npn_advertised_cb_func cb,
  2452. void *arg)
  2453. {
  2454. ctx->ext.npn_advertised_cb = cb;
  2455. ctx->ext.npn_advertised_cb_arg = arg;
  2456. }
  2457. /*
  2458. * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
  2459. * client needs to select a protocol from the server's provided list. |out|
  2460. * must be set to point to the selected protocol (which may be within |in|).
  2461. * The length of the protocol name must be written into |outlen|. The
  2462. * server's advertised protocols are provided in |in| and |inlen|. The
  2463. * callback can assume that |in| is syntactically valid. The client must
  2464. * select a protocol. It is fatal to the connection if this callback returns
  2465. * a value other than SSL_TLSEXT_ERR_OK.
  2466. */
  2467. void SSL_CTX_set_npn_select_cb(SSL_CTX *ctx,
  2468. SSL_CTX_npn_select_cb_func cb,
  2469. void *arg)
  2470. {
  2471. ctx->ext.npn_select_cb = cb;
  2472. ctx->ext.npn_select_cb_arg = arg;
  2473. }
  2474. #endif
  2475. static int alpn_value_ok(const unsigned char *protos, unsigned int protos_len)
  2476. {
  2477. unsigned int idx;
  2478. if (protos_len < 2 || protos == NULL)
  2479. return 0;
  2480. for (idx = 0; idx < protos_len; idx += protos[idx] + 1) {
  2481. if (protos[idx] == 0)
  2482. return 0;
  2483. }
  2484. return idx == protos_len;
  2485. }
  2486. /*
  2487. * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
  2488. * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
  2489. * length-prefixed strings). Returns 0 on success.
  2490. */
  2491. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
  2492. unsigned int protos_len)
  2493. {
  2494. unsigned char *alpn;
  2495. if (protos_len == 0 || protos == NULL) {
  2496. OPENSSL_free(ctx->ext.alpn);
  2497. ctx->ext.alpn = NULL;
  2498. ctx->ext.alpn_len = 0;
  2499. return 0;
  2500. }
  2501. /* Not valid per RFC */
  2502. if (!alpn_value_ok(protos, protos_len))
  2503. return 1;
  2504. alpn = OPENSSL_memdup(protos, protos_len);
  2505. if (alpn == NULL) {
  2506. SSLerr(SSL_F_SSL_CTX_SET_ALPN_PROTOS, ERR_R_MALLOC_FAILURE);
  2507. return 1;
  2508. }
  2509. OPENSSL_free(ctx->ext.alpn);
  2510. ctx->ext.alpn = alpn;
  2511. ctx->ext.alpn_len = protos_len;
  2512. return 0;
  2513. }
  2514. /*
  2515. * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
  2516. * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
  2517. * length-prefixed strings). Returns 0 on success.
  2518. */
  2519. int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
  2520. unsigned int protos_len)
  2521. {
  2522. unsigned char *alpn;
  2523. if (protos_len == 0 || protos == NULL) {
  2524. OPENSSL_free(ssl->ext.alpn);
  2525. ssl->ext.alpn = NULL;
  2526. ssl->ext.alpn_len = 0;
  2527. return 0;
  2528. }
  2529. /* Not valid per RFC */
  2530. if (!alpn_value_ok(protos, protos_len))
  2531. return 1;
  2532. alpn = OPENSSL_memdup(protos, protos_len);
  2533. if (alpn == NULL) {
  2534. SSLerr(SSL_F_SSL_SET_ALPN_PROTOS, ERR_R_MALLOC_FAILURE);
  2535. return 1;
  2536. }
  2537. OPENSSL_free(ssl->ext.alpn);
  2538. ssl->ext.alpn = alpn;
  2539. ssl->ext.alpn_len = protos_len;
  2540. return 0;
  2541. }
  2542. /*
  2543. * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
  2544. * called during ClientHello processing in order to select an ALPN protocol
  2545. * from the client's list of offered protocols.
  2546. */
  2547. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  2548. SSL_CTX_alpn_select_cb_func cb,
  2549. void *arg)
  2550. {
  2551. ctx->ext.alpn_select_cb = cb;
  2552. ctx->ext.alpn_select_cb_arg = arg;
  2553. }
  2554. /*
  2555. * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
  2556. * On return it sets |*data| to point to |*len| bytes of protocol name
  2557. * (not including the leading length-prefix byte). If the server didn't
  2558. * respond with a negotiated protocol then |*len| will be zero.
  2559. */
  2560. void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
  2561. unsigned int *len)
  2562. {
  2563. *data = NULL;
  2564. if (ssl->s3)
  2565. *data = ssl->s3->alpn_selected;
  2566. if (*data == NULL)
  2567. *len = 0;
  2568. else
  2569. *len = (unsigned int)ssl->s3->alpn_selected_len;
  2570. }
  2571. int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
  2572. const char *label, size_t llen,
  2573. const unsigned char *context, size_t contextlen,
  2574. int use_context)
  2575. {
  2576. if (s->session == NULL
  2577. || (s->version < TLS1_VERSION && s->version != DTLS1_BAD_VER))
  2578. return -1;
  2579. return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
  2580. llen, context,
  2581. contextlen, use_context);
  2582. }
  2583. int SSL_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
  2584. const char *label, size_t llen,
  2585. const unsigned char *context,
  2586. size_t contextlen)
  2587. {
  2588. if (s->version != TLS1_3_VERSION)
  2589. return 0;
  2590. return tls13_export_keying_material_early(s, out, olen, label, llen,
  2591. context, contextlen);
  2592. }
  2593. static unsigned long ssl_session_hash(const SSL_SESSION *a)
  2594. {
  2595. const unsigned char *session_id = a->session_id;
  2596. unsigned long l;
  2597. unsigned char tmp_storage[4];
  2598. if (a->session_id_length < sizeof(tmp_storage)) {
  2599. memset(tmp_storage, 0, sizeof(tmp_storage));
  2600. memcpy(tmp_storage, a->session_id, a->session_id_length);
  2601. session_id = tmp_storage;
  2602. }
  2603. l = (unsigned long)
  2604. ((unsigned long)session_id[0]) |
  2605. ((unsigned long)session_id[1] << 8L) |
  2606. ((unsigned long)session_id[2] << 16L) |
  2607. ((unsigned long)session_id[3] << 24L);
  2608. return l;
  2609. }
  2610. /*
  2611. * NB: If this function (or indeed the hash function which uses a sort of
  2612. * coarser function than this one) is changed, ensure
  2613. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
  2614. * being able to construct an SSL_SESSION that will collide with any existing
  2615. * session with a matching session ID.
  2616. */
  2617. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
  2618. {
  2619. if (a->ssl_version != b->ssl_version)
  2620. return 1;
  2621. if (a->session_id_length != b->session_id_length)
  2622. return 1;
  2623. return memcmp(a->session_id, b->session_id, a->session_id_length);
  2624. }
  2625. /*
  2626. * These wrapper functions should remain rather than redeclaring
  2627. * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
  2628. * variable. The reason is that the functions aren't static, they're exposed
  2629. * via ssl.h.
  2630. */
  2631. SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
  2632. {
  2633. SSL_CTX *ret = NULL;
  2634. if (meth == NULL) {
  2635. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_NULL_SSL_METHOD_PASSED);
  2636. return NULL;
  2637. }
  2638. if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
  2639. return NULL;
  2640. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  2641. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  2642. goto err;
  2643. }
  2644. ret = OPENSSL_zalloc(sizeof(*ret));
  2645. if (ret == NULL)
  2646. goto err;
  2647. ret->method = meth;
  2648. ret->min_proto_version = 0;
  2649. ret->max_proto_version = 0;
  2650. ret->mode = SSL_MODE_AUTO_RETRY;
  2651. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  2652. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  2653. /* We take the system default. */
  2654. ret->session_timeout = meth->get_timeout();
  2655. ret->references = 1;
  2656. ret->lock = CRYPTO_THREAD_lock_new();
  2657. if (ret->lock == NULL) {
  2658. SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE);
  2659. OPENSSL_free(ret);
  2660. return NULL;
  2661. }
  2662. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  2663. ret->verify_mode = SSL_VERIFY_NONE;
  2664. if ((ret->cert = ssl_cert_new()) == NULL)
  2665. goto err;
  2666. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  2667. if (ret->sessions == NULL)
  2668. goto err;
  2669. ret->cert_store = X509_STORE_new();
  2670. if (ret->cert_store == NULL)
  2671. goto err;
  2672. #ifndef OPENSSL_NO_CT
  2673. ret->ctlog_store = CTLOG_STORE_new();
  2674. if (ret->ctlog_store == NULL)
  2675. goto err;
  2676. #endif
  2677. if (!SSL_CTX_set_ciphersuites(ret, TLS_DEFAULT_CIPHERSUITES))
  2678. goto err;
  2679. if (!ssl_create_cipher_list(ret->method,
  2680. ret->tls13_ciphersuites,
  2681. &ret->cipher_list, &ret->cipher_list_by_id,
  2682. SSL_DEFAULT_CIPHER_LIST, ret->cert)
  2683. || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
  2684. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  2685. goto err2;
  2686. }
  2687. ret->param = X509_VERIFY_PARAM_new();
  2688. if (ret->param == NULL)
  2689. goto err;
  2690. if ((ret->md5 = EVP_get_digestbyname("ssl3-md5")) == NULL) {
  2691. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
  2692. goto err2;
  2693. }
  2694. if ((ret->sha1 = EVP_get_digestbyname("ssl3-sha1")) == NULL) {
  2695. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
  2696. goto err2;
  2697. }
  2698. if ((ret->ca_names = sk_X509_NAME_new_null()) == NULL)
  2699. goto err;
  2700. if ((ret->client_ca_names = sk_X509_NAME_new_null()) == NULL)
  2701. goto err;
  2702. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data))
  2703. goto err;
  2704. if ((ret->ext.secure = OPENSSL_secure_zalloc(sizeof(*ret->ext.secure))) == NULL)
  2705. goto err;
  2706. /* No compression for DTLS */
  2707. if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))
  2708. ret->comp_methods = SSL_COMP_get_compression_methods();
  2709. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  2710. ret->split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  2711. /* Setup RFC5077 ticket keys */
  2712. if ((RAND_bytes(ret->ext.tick_key_name,
  2713. sizeof(ret->ext.tick_key_name)) <= 0)
  2714. || (RAND_priv_bytes(ret->ext.secure->tick_hmac_key,
  2715. sizeof(ret->ext.secure->tick_hmac_key)) <= 0)
  2716. || (RAND_priv_bytes(ret->ext.secure->tick_aes_key,
  2717. sizeof(ret->ext.secure->tick_aes_key)) <= 0))
  2718. ret->options |= SSL_OP_NO_TICKET;
  2719. if (RAND_priv_bytes(ret->ext.cookie_hmac_key,
  2720. sizeof(ret->ext.cookie_hmac_key)) <= 0)
  2721. goto err;
  2722. #ifndef OPENSSL_NO_SRP
  2723. if (!SSL_CTX_SRP_CTX_init(ret))
  2724. goto err;
  2725. #endif
  2726. #ifndef OPENSSL_NO_ENGINE
  2727. # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
  2728. # define eng_strx(x) #x
  2729. # define eng_str(x) eng_strx(x)
  2730. /* Use specific client engine automatically... ignore errors */
  2731. {
  2732. ENGINE *eng;
  2733. eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
  2734. if (!eng) {
  2735. ERR_clear_error();
  2736. ENGINE_load_builtin_engines();
  2737. eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
  2738. }
  2739. if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
  2740. ERR_clear_error();
  2741. }
  2742. # endif
  2743. #endif
  2744. /*
  2745. * Default is to connect to non-RI servers. When RI is more widely
  2746. * deployed might change this.
  2747. */
  2748. ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
  2749. /*
  2750. * Disable compression by default to prevent CRIME. Applications can
  2751. * re-enable compression by configuring
  2752. * SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION);
  2753. * or by using the SSL_CONF library. Similarly we also enable TLSv1.3
  2754. * middlebox compatibility by default. This may be disabled by default in
  2755. * a later OpenSSL version.
  2756. */
  2757. ret->options |= SSL_OP_NO_COMPRESSION | SSL_OP_ENABLE_MIDDLEBOX_COMPAT;
  2758. ret->ext.status_type = TLSEXT_STATUSTYPE_nothing;
  2759. /*
  2760. * We cannot usefully set a default max_early_data here (which gets
  2761. * propagated in SSL_new(), for the following reason: setting the
  2762. * SSL field causes tls_construct_stoc_early_data() to tell the
  2763. * client that early data will be accepted when constructing a TLS 1.3
  2764. * session ticket, and the client will accordingly send us early data
  2765. * when using that ticket (if the client has early data to send).
  2766. * However, in order for the early data to actually be consumed by
  2767. * the application, the application must also have calls to
  2768. * SSL_read_early_data(); otherwise we'll just skip past the early data
  2769. * and ignore it. So, since the application must add calls to
  2770. * SSL_read_early_data(), we also require them to add
  2771. * calls to SSL_CTX_set_max_early_data() in order to use early data,
  2772. * eliminating the bandwidth-wasting early data in the case described
  2773. * above.
  2774. */
  2775. ret->max_early_data = 0;
  2776. /*
  2777. * Default recv_max_early_data is a fully loaded single record. Could be
  2778. * split across multiple records in practice. We set this differently to
  2779. * max_early_data so that, in the default case, we do not advertise any
  2780. * support for early_data, but if a client were to send us some (e.g.
  2781. * because of an old, stale ticket) then we will tolerate it and skip over
  2782. * it.
  2783. */
  2784. ret->recv_max_early_data = SSL3_RT_MAX_PLAIN_LENGTH;
  2785. /* By default we send two session tickets automatically in TLSv1.3 */
  2786. ret->num_tickets = 2;
  2787. ssl_ctx_system_config(ret);
  2788. return ret;
  2789. err:
  2790. SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE);
  2791. err2:
  2792. SSL_CTX_free(ret);
  2793. return NULL;
  2794. }
  2795. int SSL_CTX_up_ref(SSL_CTX *ctx)
  2796. {
  2797. int i;
  2798. if (CRYPTO_UP_REF(&ctx->references, &i, ctx->lock) <= 0)
  2799. return 0;
  2800. REF_PRINT_COUNT("SSL_CTX", ctx);
  2801. REF_ASSERT_ISNT(i < 2);
  2802. return ((i > 1) ? 1 : 0);
  2803. }
  2804. void SSL_CTX_free(SSL_CTX *a)
  2805. {
  2806. int i;
  2807. if (a == NULL)
  2808. return;
  2809. CRYPTO_DOWN_REF(&a->references, &i, a->lock);
  2810. REF_PRINT_COUNT("SSL_CTX", a);
  2811. if (i > 0)
  2812. return;
  2813. REF_ASSERT_ISNT(i < 0);
  2814. X509_VERIFY_PARAM_free(a->param);
  2815. dane_ctx_final(&a->dane);
  2816. /*
  2817. * Free internal session cache. However: the remove_cb() may reference
  2818. * the ex_data of SSL_CTX, thus the ex_data store can only be removed
  2819. * after the sessions were flushed.
  2820. * As the ex_data handling routines might also touch the session cache,
  2821. * the most secure solution seems to be: empty (flush) the cache, then
  2822. * free ex_data, then finally free the cache.
  2823. * (See ticket [openssl.org #212].)
  2824. */
  2825. if (a->sessions != NULL)
  2826. SSL_CTX_flush_sessions(a, 0);
  2827. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
  2828. lh_SSL_SESSION_free(a->sessions);
  2829. X509_STORE_free(a->cert_store);
  2830. #ifndef OPENSSL_NO_CT
  2831. CTLOG_STORE_free(a->ctlog_store);
  2832. #endif
  2833. sk_SSL_CIPHER_free(a->cipher_list);
  2834. sk_SSL_CIPHER_free(a->cipher_list_by_id);
  2835. sk_SSL_CIPHER_free(a->tls13_ciphersuites);
  2836. ssl_cert_free(a->cert);
  2837. sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);
  2838. sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);
  2839. sk_X509_pop_free(a->extra_certs, X509_free);
  2840. a->comp_methods = NULL;
  2841. #ifndef OPENSSL_NO_SRTP
  2842. sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
  2843. #endif
  2844. #ifndef OPENSSL_NO_SRP
  2845. SSL_CTX_SRP_CTX_free(a);
  2846. #endif
  2847. #ifndef OPENSSL_NO_ENGINE
  2848. ENGINE_finish(a->client_cert_engine);
  2849. #endif
  2850. #ifndef OPENSSL_NO_EC
  2851. OPENSSL_free(a->ext.ecpointformats);
  2852. OPENSSL_free(a->ext.supportedgroups);
  2853. #endif
  2854. OPENSSL_free(a->ext.alpn);
  2855. OPENSSL_secure_free(a->ext.secure);
  2856. CRYPTO_THREAD_lock_free(a->lock);
  2857. OPENSSL_free(a);
  2858. }
  2859. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
  2860. {
  2861. ctx->default_passwd_callback = cb;
  2862. }
  2863. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
  2864. {
  2865. ctx->default_passwd_callback_userdata = u;
  2866. }
  2867. pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
  2868. {
  2869. return ctx->default_passwd_callback;
  2870. }
  2871. void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
  2872. {
  2873. return ctx->default_passwd_callback_userdata;
  2874. }
  2875. void SSL_set_default_passwd_cb(SSL *s, pem_password_cb *cb)
  2876. {
  2877. s->default_passwd_callback = cb;
  2878. }
  2879. void SSL_set_default_passwd_cb_userdata(SSL *s, void *u)
  2880. {
  2881. s->default_passwd_callback_userdata = u;
  2882. }
  2883. pem_password_cb *SSL_get_default_passwd_cb(SSL *s)
  2884. {
  2885. return s->default_passwd_callback;
  2886. }
  2887. void *SSL_get_default_passwd_cb_userdata(SSL *s)
  2888. {
  2889. return s->default_passwd_callback_userdata;
  2890. }
  2891. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  2892. int (*cb) (X509_STORE_CTX *, void *),
  2893. void *arg)
  2894. {
  2895. ctx->app_verify_callback = cb;
  2896. ctx->app_verify_arg = arg;
  2897. }
  2898. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  2899. int (*cb) (int, X509_STORE_CTX *))
  2900. {
  2901. ctx->verify_mode = mode;
  2902. ctx->default_verify_callback = cb;
  2903. }
  2904. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
  2905. {
  2906. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  2907. }
  2908. void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb) (SSL *ssl, void *arg), void *arg)
  2909. {
  2910. ssl_cert_set_cert_cb(c->cert, cb, arg);
  2911. }
  2912. void SSL_set_cert_cb(SSL *s, int (*cb) (SSL *ssl, void *arg), void *arg)
  2913. {
  2914. ssl_cert_set_cert_cb(s->cert, cb, arg);
  2915. }
  2916. void ssl_set_masks(SSL *s)
  2917. {
  2918. CERT *c = s->cert;
  2919. uint32_t *pvalid = s->s3->tmp.valid_flags;
  2920. int rsa_enc, rsa_sign, dh_tmp, dsa_sign;
  2921. unsigned long mask_k, mask_a;
  2922. #ifndef OPENSSL_NO_EC
  2923. int have_ecc_cert, ecdsa_ok;
  2924. #endif
  2925. if (c == NULL)
  2926. return;
  2927. #ifndef OPENSSL_NO_DH
  2928. dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL || c->dh_tmp_auto);
  2929. #else
  2930. dh_tmp = 0;
  2931. #endif
  2932. rsa_enc = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
  2933. rsa_sign = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
  2934. dsa_sign = pvalid[SSL_PKEY_DSA_SIGN] & CERT_PKEY_VALID;
  2935. #ifndef OPENSSL_NO_EC
  2936. have_ecc_cert = pvalid[SSL_PKEY_ECC] & CERT_PKEY_VALID;
  2937. #endif
  2938. mask_k = 0;
  2939. mask_a = 0;
  2940. #ifdef CIPHER_DEBUG
  2941. fprintf(stderr, "dht=%d re=%d rs=%d ds=%d\n",
  2942. dh_tmp, rsa_enc, rsa_sign, dsa_sign);
  2943. #endif
  2944. #ifndef OPENSSL_NO_GOST
  2945. if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) {
  2946. mask_k |= SSL_kGOST;
  2947. mask_a |= SSL_aGOST12;
  2948. }
  2949. if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) {
  2950. mask_k |= SSL_kGOST;
  2951. mask_a |= SSL_aGOST12;
  2952. }
  2953. if (ssl_has_cert(s, SSL_PKEY_GOST01)) {
  2954. mask_k |= SSL_kGOST;
  2955. mask_a |= SSL_aGOST01;
  2956. }
  2957. #endif
  2958. if (rsa_enc)
  2959. mask_k |= SSL_kRSA;
  2960. if (dh_tmp)
  2961. mask_k |= SSL_kDHE;
  2962. /*
  2963. * If we only have an RSA-PSS certificate allow RSA authentication
  2964. * if TLS 1.2 and peer supports it.
  2965. */
  2966. if (rsa_enc || rsa_sign || (ssl_has_cert(s, SSL_PKEY_RSA_PSS_SIGN)
  2967. && pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_EXPLICIT_SIGN
  2968. && TLS1_get_version(s) == TLS1_2_VERSION))
  2969. mask_a |= SSL_aRSA;
  2970. if (dsa_sign) {
  2971. mask_a |= SSL_aDSS;
  2972. }
  2973. mask_a |= SSL_aNULL;
  2974. /*
  2975. * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
  2976. * depending on the key usage extension.
  2977. */
  2978. #ifndef OPENSSL_NO_EC
  2979. if (have_ecc_cert) {
  2980. uint32_t ex_kusage;
  2981. ex_kusage = X509_get_key_usage(c->pkeys[SSL_PKEY_ECC].x509);
  2982. ecdsa_ok = ex_kusage & X509v3_KU_DIGITAL_SIGNATURE;
  2983. if (!(pvalid[SSL_PKEY_ECC] & CERT_PKEY_SIGN))
  2984. ecdsa_ok = 0;
  2985. if (ecdsa_ok)
  2986. mask_a |= SSL_aECDSA;
  2987. }
  2988. /* Allow Ed25519 for TLS 1.2 if peer supports it */
  2989. if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED25519)
  2990. && pvalid[SSL_PKEY_ED25519] & CERT_PKEY_EXPLICIT_SIGN
  2991. && TLS1_get_version(s) == TLS1_2_VERSION)
  2992. mask_a |= SSL_aECDSA;
  2993. /* Allow Ed448 for TLS 1.2 if peer supports it */
  2994. if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED448)
  2995. && pvalid[SSL_PKEY_ED448] & CERT_PKEY_EXPLICIT_SIGN
  2996. && TLS1_get_version(s) == TLS1_2_VERSION)
  2997. mask_a |= SSL_aECDSA;
  2998. #endif
  2999. #ifndef OPENSSL_NO_EC
  3000. mask_k |= SSL_kECDHE;
  3001. #endif
  3002. #ifndef OPENSSL_NO_PSK
  3003. mask_k |= SSL_kPSK;
  3004. mask_a |= SSL_aPSK;
  3005. if (mask_k & SSL_kRSA)
  3006. mask_k |= SSL_kRSAPSK;
  3007. if (mask_k & SSL_kDHE)
  3008. mask_k |= SSL_kDHEPSK;
  3009. if (mask_k & SSL_kECDHE)
  3010. mask_k |= SSL_kECDHEPSK;
  3011. #endif
  3012. s->s3->tmp.mask_k = mask_k;
  3013. s->s3->tmp.mask_a = mask_a;
  3014. }
  3015. #ifndef OPENSSL_NO_EC
  3016. int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
  3017. {
  3018. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aECDSA) {
  3019. /* key usage, if present, must allow signing */
  3020. if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
  3021. SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
  3022. SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  3023. return 0;
  3024. }
  3025. }
  3026. return 1; /* all checks are ok */
  3027. }
  3028. #endif
  3029. int ssl_get_server_cert_serverinfo(SSL *s, const unsigned char **serverinfo,
  3030. size_t *serverinfo_length)
  3031. {
  3032. CERT_PKEY *cpk = s->s3->tmp.cert;
  3033. *serverinfo_length = 0;
  3034. if (cpk == NULL || cpk->serverinfo == NULL)
  3035. return 0;
  3036. *serverinfo = cpk->serverinfo;
  3037. *serverinfo_length = cpk->serverinfo_length;
  3038. return 1;
  3039. }
  3040. void ssl_update_cache(SSL *s, int mode)
  3041. {
  3042. int i;
  3043. /*
  3044. * If the session_id_length is 0, we are not supposed to cache it, and it
  3045. * would be rather hard to do anyway :-)
  3046. */
  3047. if (s->session->session_id_length == 0)
  3048. return;
  3049. /*
  3050. * If sid_ctx_length is 0 there is no specific application context
  3051. * associated with this session, so when we try to resume it and
  3052. * SSL_VERIFY_PEER is requested to verify the client identity, we have no
  3053. * indication that this is actually a session for the proper application
  3054. * context, and the *handshake* will fail, not just the resumption attempt.
  3055. * Do not cache (on the server) these sessions that are not resumable
  3056. * (clients can set SSL_VERIFY_PEER without needing a sid_ctx set).
  3057. */
  3058. if (s->server && s->session->sid_ctx_length == 0
  3059. && (s->verify_mode & SSL_VERIFY_PEER) != 0)
  3060. return;
  3061. i = s->session_ctx->session_cache_mode;
  3062. if ((i & mode) != 0
  3063. && (!s->hit || SSL_IS_TLS13(s))) {
  3064. /*
  3065. * Add the session to the internal cache. In server side TLSv1.3 we
  3066. * normally don't do this because by default it's a full stateless ticket
  3067. * with only a dummy session id so there is no reason to cache it,
  3068. * unless:
  3069. * - we are doing early_data, in which case we cache so that we can
  3070. * detect replays
  3071. * - the application has set a remove_session_cb so needs to know about
  3072. * session timeout events
  3073. * - SSL_OP_NO_TICKET is set in which case it is a stateful ticket
  3074. */
  3075. if ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0
  3076. && (!SSL_IS_TLS13(s)
  3077. || !s->server
  3078. || (s->max_early_data > 0
  3079. && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0)
  3080. || s->session_ctx->remove_session_cb != NULL
  3081. || (s->options & SSL_OP_NO_TICKET) != 0))
  3082. SSL_CTX_add_session(s->session_ctx, s->session);
  3083. /*
  3084. * Add the session to the external cache. We do this even in server side
  3085. * TLSv1.3 without early data because some applications just want to
  3086. * know about the creation of a session and aren't doing a full cache.
  3087. */
  3088. if (s->session_ctx->new_session_cb != NULL) {
  3089. SSL_SESSION_up_ref(s->session);
  3090. if (!s->session_ctx->new_session_cb(s, s->session))
  3091. SSL_SESSION_free(s->session);
  3092. }
  3093. }
  3094. /* auto flush every 255 connections */
  3095. if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
  3096. TSAN_QUALIFIER int *stat;
  3097. if (mode & SSL_SESS_CACHE_CLIENT)
  3098. stat = &s->session_ctx->stats.sess_connect_good;
  3099. else
  3100. stat = &s->session_ctx->stats.sess_accept_good;
  3101. if ((tsan_load(stat) & 0xff) == 0xff)
  3102. SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL));
  3103. }
  3104. }
  3105. const SSL_METHOD *SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
  3106. {
  3107. return ctx->method;
  3108. }
  3109. const SSL_METHOD *SSL_get_ssl_method(const SSL *s)
  3110. {
  3111. return s->method;
  3112. }
  3113. int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
  3114. {
  3115. int ret = 1;
  3116. if (s->method != meth) {
  3117. const SSL_METHOD *sm = s->method;
  3118. int (*hf) (SSL *) = s->handshake_func;
  3119. if (sm->version == meth->version)
  3120. s->method = meth;
  3121. else {
  3122. sm->ssl_free(s);
  3123. s->method = meth;
  3124. ret = s->method->ssl_new(s);
  3125. }
  3126. if (hf == sm->ssl_connect)
  3127. s->handshake_func = meth->ssl_connect;
  3128. else if (hf == sm->ssl_accept)
  3129. s->handshake_func = meth->ssl_accept;
  3130. }
  3131. return ret;
  3132. }
  3133. int SSL_get_error(const SSL *s, int i)
  3134. {
  3135. int reason;
  3136. unsigned long l;
  3137. BIO *bio;
  3138. if (i > 0)
  3139. return SSL_ERROR_NONE;
  3140. /*
  3141. * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  3142. * where we do encode the error
  3143. */
  3144. if ((l = ERR_peek_error()) != 0) {
  3145. if (ERR_GET_LIB(l) == ERR_LIB_SYS)
  3146. return SSL_ERROR_SYSCALL;
  3147. else
  3148. return SSL_ERROR_SSL;
  3149. }
  3150. if (SSL_want_read(s)) {
  3151. bio = SSL_get_rbio(s);
  3152. if (BIO_should_read(bio))
  3153. return SSL_ERROR_WANT_READ;
  3154. else if (BIO_should_write(bio))
  3155. /*
  3156. * This one doesn't make too much sense ... We never try to write
  3157. * to the rbio, and an application program where rbio and wbio
  3158. * are separate couldn't even know what it should wait for.
  3159. * However if we ever set s->rwstate incorrectly (so that we have
  3160. * SSL_want_read(s) instead of SSL_want_write(s)) and rbio and
  3161. * wbio *are* the same, this test works around that bug; so it
  3162. * might be safer to keep it.
  3163. */
  3164. return SSL_ERROR_WANT_WRITE;
  3165. else if (BIO_should_io_special(bio)) {
  3166. reason = BIO_get_retry_reason(bio);
  3167. if (reason == BIO_RR_CONNECT)
  3168. return SSL_ERROR_WANT_CONNECT;
  3169. else if (reason == BIO_RR_ACCEPT)
  3170. return SSL_ERROR_WANT_ACCEPT;
  3171. else
  3172. return SSL_ERROR_SYSCALL; /* unknown */
  3173. }
  3174. }
  3175. if (SSL_want_write(s)) {
  3176. /* Access wbio directly - in order to use the buffered bio if present */
  3177. bio = s->wbio;
  3178. if (BIO_should_write(bio))
  3179. return SSL_ERROR_WANT_WRITE;
  3180. else if (BIO_should_read(bio))
  3181. /*
  3182. * See above (SSL_want_read(s) with BIO_should_write(bio))
  3183. */
  3184. return SSL_ERROR_WANT_READ;
  3185. else if (BIO_should_io_special(bio)) {
  3186. reason = BIO_get_retry_reason(bio);
  3187. if (reason == BIO_RR_CONNECT)
  3188. return SSL_ERROR_WANT_CONNECT;
  3189. else if (reason == BIO_RR_ACCEPT)
  3190. return SSL_ERROR_WANT_ACCEPT;
  3191. else
  3192. return SSL_ERROR_SYSCALL;
  3193. }
  3194. }
  3195. if (SSL_want_x509_lookup(s))
  3196. return SSL_ERROR_WANT_X509_LOOKUP;
  3197. if (SSL_want_async(s))
  3198. return SSL_ERROR_WANT_ASYNC;
  3199. if (SSL_want_async_job(s))
  3200. return SSL_ERROR_WANT_ASYNC_JOB;
  3201. if (SSL_want_client_hello_cb(s))
  3202. return SSL_ERROR_WANT_CLIENT_HELLO_CB;
  3203. if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
  3204. (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
  3205. return SSL_ERROR_ZERO_RETURN;
  3206. return SSL_ERROR_SYSCALL;
  3207. }
  3208. static int ssl_do_handshake_intern(void *vargs)
  3209. {
  3210. struct ssl_async_args *args;
  3211. SSL *s;
  3212. args = (struct ssl_async_args *)vargs;
  3213. s = args->s;
  3214. return s->handshake_func(s);
  3215. }
  3216. int SSL_do_handshake(SSL *s)
  3217. {
  3218. int ret = 1;
  3219. if (s->handshake_func == NULL) {
  3220. SSLerr(SSL_F_SSL_DO_HANDSHAKE, SSL_R_CONNECTION_TYPE_NOT_SET);
  3221. return -1;
  3222. }
  3223. ossl_statem_check_finish_init(s, -1);
  3224. s->method->ssl_renegotiate_check(s, 0);
  3225. if (SSL_in_init(s) || SSL_in_before(s)) {
  3226. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  3227. struct ssl_async_args args;
  3228. memset(&args, 0, sizeof(args));
  3229. args.s = s;
  3230. ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern);
  3231. } else {
  3232. ret = s->handshake_func(s);
  3233. }
  3234. }
  3235. return ret;
  3236. }
  3237. void SSL_set_accept_state(SSL *s)
  3238. {
  3239. s->server = 1;
  3240. s->shutdown = 0;
  3241. ossl_statem_clear(s);
  3242. s->handshake_func = s->method->ssl_accept;
  3243. clear_ciphers(s);
  3244. }
  3245. void SSL_set_connect_state(SSL *s)
  3246. {
  3247. s->server = 0;
  3248. s->shutdown = 0;
  3249. ossl_statem_clear(s);
  3250. s->handshake_func = s->method->ssl_connect;
  3251. clear_ciphers(s);
  3252. }
  3253. int ssl_undefined_function(SSL *s)
  3254. {
  3255. SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3256. return 0;
  3257. }
  3258. int ssl_undefined_void_function(void)
  3259. {
  3260. SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,
  3261. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3262. return 0;
  3263. }
  3264. int ssl_undefined_const_function(const SSL *s)
  3265. {
  3266. return 0;
  3267. }
  3268. const SSL_METHOD *ssl_bad_method(int ver)
  3269. {
  3270. SSLerr(SSL_F_SSL_BAD_METHOD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3271. return NULL;
  3272. }
  3273. const char *ssl_protocol_to_string(int version)
  3274. {
  3275. switch(version)
  3276. {
  3277. case TLS1_3_VERSION:
  3278. return "TLSv1.3";
  3279. case TLS1_2_VERSION:
  3280. return "TLSv1.2";
  3281. case TLS1_1_VERSION:
  3282. return "TLSv1.1";
  3283. case TLS1_VERSION:
  3284. return "TLSv1";
  3285. case SSL3_VERSION:
  3286. return "SSLv3";
  3287. case DTLS1_BAD_VER:
  3288. return "DTLSv0.9";
  3289. case DTLS1_VERSION:
  3290. return "DTLSv1";
  3291. case DTLS1_2_VERSION:
  3292. return "DTLSv1.2";
  3293. default:
  3294. return "unknown";
  3295. }
  3296. }
  3297. const char *SSL_get_version(const SSL *s)
  3298. {
  3299. return ssl_protocol_to_string(s->version);
  3300. }
  3301. static int dup_ca_names(STACK_OF(X509_NAME) **dst, STACK_OF(X509_NAME) *src)
  3302. {
  3303. STACK_OF(X509_NAME) *sk;
  3304. X509_NAME *xn;
  3305. int i;
  3306. if (src == NULL) {
  3307. *dst = NULL;
  3308. return 1;
  3309. }
  3310. if ((sk = sk_X509_NAME_new_null()) == NULL)
  3311. return 0;
  3312. for (i = 0; i < sk_X509_NAME_num(src); i++) {
  3313. xn = X509_NAME_dup(sk_X509_NAME_value(src, i));
  3314. if (xn == NULL) {
  3315. sk_X509_NAME_pop_free(sk, X509_NAME_free);
  3316. return 0;
  3317. }
  3318. if (sk_X509_NAME_insert(sk, xn, i) == 0) {
  3319. X509_NAME_free(xn);
  3320. sk_X509_NAME_pop_free(sk, X509_NAME_free);
  3321. return 0;
  3322. }
  3323. }
  3324. *dst = sk;
  3325. return 1;
  3326. }
  3327. SSL *SSL_dup(SSL *s)
  3328. {
  3329. SSL *ret;
  3330. int i;
  3331. /* If we're not quiescent, just up_ref! */
  3332. if (!SSL_in_init(s) || !SSL_in_before(s)) {
  3333. CRYPTO_UP_REF(&s->references, &i, s->lock);
  3334. return s;
  3335. }
  3336. /*
  3337. * Otherwise, copy configuration state, and session if set.
  3338. */
  3339. if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
  3340. return NULL;
  3341. if (s->session != NULL) {
  3342. /*
  3343. * Arranges to share the same session via up_ref. This "copies"
  3344. * session-id, SSL_METHOD, sid_ctx, and 'cert'
  3345. */
  3346. if (!SSL_copy_session_id(ret, s))
  3347. goto err;
  3348. } else {
  3349. /*
  3350. * No session has been established yet, so we have to expect that
  3351. * s->cert or ret->cert will be changed later -- they should not both
  3352. * point to the same object, and thus we can't use
  3353. * SSL_copy_session_id.
  3354. */
  3355. if (!SSL_set_ssl_method(ret, s->method))
  3356. goto err;
  3357. if (s->cert != NULL) {
  3358. ssl_cert_free(ret->cert);
  3359. ret->cert = ssl_cert_dup(s->cert);
  3360. if (ret->cert == NULL)
  3361. goto err;
  3362. }
  3363. if (!SSL_set_session_id_context(ret, s->sid_ctx,
  3364. (int)s->sid_ctx_length))
  3365. goto err;
  3366. }
  3367. if (!ssl_dane_dup(ret, s))
  3368. goto err;
  3369. ret->version = s->version;
  3370. ret->options = s->options;
  3371. ret->min_proto_version = s->min_proto_version;
  3372. ret->max_proto_version = s->max_proto_version;
  3373. ret->mode = s->mode;
  3374. SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
  3375. SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
  3376. ret->msg_callback = s->msg_callback;
  3377. ret->msg_callback_arg = s->msg_callback_arg;
  3378. SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));
  3379. SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
  3380. ret->generate_session_id = s->generate_session_id;
  3381. SSL_set_info_callback(ret, SSL_get_info_callback(s));
  3382. /* copy app data, a little dangerous perhaps */
  3383. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
  3384. goto err;
  3385. ret->server = s->server;
  3386. if (s->handshake_func) {
  3387. if (s->server)
  3388. SSL_set_accept_state(ret);
  3389. else
  3390. SSL_set_connect_state(ret);
  3391. }
  3392. ret->shutdown = s->shutdown;
  3393. ret->hit = s->hit;
  3394. ret->default_passwd_callback = s->default_passwd_callback;
  3395. ret->default_passwd_callback_userdata = s->default_passwd_callback_userdata;
  3396. X509_VERIFY_PARAM_inherit(ret->param, s->param);
  3397. /* dup the cipher_list and cipher_list_by_id stacks */
  3398. if (s->cipher_list != NULL) {
  3399. if ((ret->cipher_list = sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
  3400. goto err;
  3401. }
  3402. if (s->cipher_list_by_id != NULL)
  3403. if ((ret->cipher_list_by_id = sk_SSL_CIPHER_dup(s->cipher_list_by_id))
  3404. == NULL)
  3405. goto err;
  3406. /* Dup the client_CA list */
  3407. if (!dup_ca_names(&ret->ca_names, s->ca_names)
  3408. || !dup_ca_names(&ret->client_ca_names, s->client_ca_names))
  3409. goto err;
  3410. return ret;
  3411. err:
  3412. SSL_free(ret);
  3413. return NULL;
  3414. }
  3415. void ssl_clear_cipher_ctx(SSL *s)
  3416. {
  3417. if (s->enc_read_ctx != NULL) {
  3418. EVP_CIPHER_CTX_free(s->enc_read_ctx);
  3419. s->enc_read_ctx = NULL;
  3420. }
  3421. if (s->enc_write_ctx != NULL) {
  3422. EVP_CIPHER_CTX_free(s->enc_write_ctx);
  3423. s->enc_write_ctx = NULL;
  3424. }
  3425. #ifndef OPENSSL_NO_COMP
  3426. COMP_CTX_free(s->expand);
  3427. s->expand = NULL;
  3428. COMP_CTX_free(s->compress);
  3429. s->compress = NULL;
  3430. #endif
  3431. }
  3432. X509 *SSL_get_certificate(const SSL *s)
  3433. {
  3434. if (s->cert != NULL)
  3435. return s->cert->key->x509;
  3436. else
  3437. return NULL;
  3438. }
  3439. EVP_PKEY *SSL_get_privatekey(const SSL *s)
  3440. {
  3441. if (s->cert != NULL)
  3442. return s->cert->key->privatekey;
  3443. else
  3444. return NULL;
  3445. }
  3446. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
  3447. {
  3448. if (ctx->cert != NULL)
  3449. return ctx->cert->key->x509;
  3450. else
  3451. return NULL;
  3452. }
  3453. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
  3454. {
  3455. if (ctx->cert != NULL)
  3456. return ctx->cert->key->privatekey;
  3457. else
  3458. return NULL;
  3459. }
  3460. const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
  3461. {
  3462. if ((s->session != NULL) && (s->session->cipher != NULL))
  3463. return s->session->cipher;
  3464. return NULL;
  3465. }
  3466. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *s)
  3467. {
  3468. return s->s3->tmp.new_cipher;
  3469. }
  3470. const COMP_METHOD *SSL_get_current_compression(const SSL *s)
  3471. {
  3472. #ifndef OPENSSL_NO_COMP
  3473. return s->compress ? COMP_CTX_get_method(s->compress) : NULL;
  3474. #else
  3475. return NULL;
  3476. #endif
  3477. }
  3478. const COMP_METHOD *SSL_get_current_expansion(const SSL *s)
  3479. {
  3480. #ifndef OPENSSL_NO_COMP
  3481. return s->expand ? COMP_CTX_get_method(s->expand) : NULL;
  3482. #else
  3483. return NULL;
  3484. #endif
  3485. }
  3486. int ssl_init_wbio_buffer(SSL *s)
  3487. {
  3488. BIO *bbio;
  3489. if (s->bbio != NULL) {
  3490. /* Already buffered. */
  3491. return 1;
  3492. }
  3493. bbio = BIO_new(BIO_f_buffer());
  3494. if (bbio == NULL || !BIO_set_read_buffer_size(bbio, 1)) {
  3495. BIO_free(bbio);
  3496. SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER, ERR_R_BUF_LIB);
  3497. return 0;
  3498. }
  3499. s->bbio = bbio;
  3500. s->wbio = BIO_push(bbio, s->wbio);
  3501. return 1;
  3502. }
  3503. int ssl_free_wbio_buffer(SSL *s)
  3504. {
  3505. /* callers ensure s is never null */
  3506. if (s->bbio == NULL)
  3507. return 1;
  3508. s->wbio = BIO_pop(s->wbio);
  3509. BIO_free(s->bbio);
  3510. s->bbio = NULL;
  3511. return 1;
  3512. }
  3513. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
  3514. {
  3515. ctx->quiet_shutdown = mode;
  3516. }
  3517. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
  3518. {
  3519. return ctx->quiet_shutdown;
  3520. }
  3521. void SSL_set_quiet_shutdown(SSL *s, int mode)
  3522. {
  3523. s->quiet_shutdown = mode;
  3524. }
  3525. int SSL_get_quiet_shutdown(const SSL *s)
  3526. {
  3527. return s->quiet_shutdown;
  3528. }
  3529. void SSL_set_shutdown(SSL *s, int mode)
  3530. {
  3531. s->shutdown = mode;
  3532. }
  3533. int SSL_get_shutdown(const SSL *s)
  3534. {
  3535. return s->shutdown;
  3536. }
  3537. int SSL_version(const SSL *s)
  3538. {
  3539. return s->version;
  3540. }
  3541. int SSL_client_version(const SSL *s)
  3542. {
  3543. return s->client_version;
  3544. }
  3545. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
  3546. {
  3547. return ssl->ctx;
  3548. }
  3549. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
  3550. {
  3551. CERT *new_cert;
  3552. if (ssl->ctx == ctx)
  3553. return ssl->ctx;
  3554. if (ctx == NULL)
  3555. ctx = ssl->session_ctx;
  3556. new_cert = ssl_cert_dup(ctx->cert);
  3557. if (new_cert == NULL) {
  3558. return NULL;
  3559. }
  3560. if (!custom_exts_copy_flags(&new_cert->custext, &ssl->cert->custext)) {
  3561. ssl_cert_free(new_cert);
  3562. return NULL;
  3563. }
  3564. ssl_cert_free(ssl->cert);
  3565. ssl->cert = new_cert;
  3566. /*
  3567. * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
  3568. * so setter APIs must prevent invalid lengths from entering the system.
  3569. */
  3570. if (!ossl_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx)))
  3571. return NULL;
  3572. /*
  3573. * If the session ID context matches that of the parent SSL_CTX,
  3574. * inherit it from the new SSL_CTX as well. If however the context does
  3575. * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
  3576. * leave it unchanged.
  3577. */
  3578. if ((ssl->ctx != NULL) &&
  3579. (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) &&
  3580. (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) {
  3581. ssl->sid_ctx_length = ctx->sid_ctx_length;
  3582. memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
  3583. }
  3584. SSL_CTX_up_ref(ctx);
  3585. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  3586. ssl->ctx = ctx;
  3587. return ssl->ctx;
  3588. }
  3589. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
  3590. {
  3591. return X509_STORE_set_default_paths(ctx->cert_store);
  3592. }
  3593. int SSL_CTX_set_default_verify_dir(SSL_CTX *ctx)
  3594. {
  3595. X509_LOOKUP *lookup;
  3596. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_hash_dir());
  3597. if (lookup == NULL)
  3598. return 0;
  3599. X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
  3600. /* Clear any errors if the default directory does not exist */
  3601. ERR_clear_error();
  3602. return 1;
  3603. }
  3604. int SSL_CTX_set_default_verify_file(SSL_CTX *ctx)
  3605. {
  3606. X509_LOOKUP *lookup;
  3607. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_file());
  3608. if (lookup == NULL)
  3609. return 0;
  3610. X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT);
  3611. /* Clear any errors if the default file does not exist */
  3612. ERR_clear_error();
  3613. return 1;
  3614. }
  3615. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
  3616. const char *CApath)
  3617. {
  3618. return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
  3619. }
  3620. void SSL_set_info_callback(SSL *ssl,
  3621. void (*cb) (const SSL *ssl, int type, int val))
  3622. {
  3623. ssl->info_callback = cb;
  3624. }
  3625. /*
  3626. * One compiler (Diab DCC) doesn't like argument names in returned function
  3627. * pointer.
  3628. */
  3629. void (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ ,
  3630. int /* type */ ,
  3631. int /* val */ ) {
  3632. return ssl->info_callback;
  3633. }
  3634. void SSL_set_verify_result(SSL *ssl, long arg)
  3635. {
  3636. ssl->verify_result = arg;
  3637. }
  3638. long SSL_get_verify_result(const SSL *ssl)
  3639. {
  3640. return ssl->verify_result;
  3641. }
  3642. size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, size_t outlen)
  3643. {
  3644. if (outlen == 0)
  3645. return sizeof(ssl->s3->client_random);
  3646. if (outlen > sizeof(ssl->s3->client_random))
  3647. outlen = sizeof(ssl->s3->client_random);
  3648. memcpy(out, ssl->s3->client_random, outlen);
  3649. return outlen;
  3650. }
  3651. size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, size_t outlen)
  3652. {
  3653. if (outlen == 0)
  3654. return sizeof(ssl->s3->server_random);
  3655. if (outlen > sizeof(ssl->s3->server_random))
  3656. outlen = sizeof(ssl->s3->server_random);
  3657. memcpy(out, ssl->s3->server_random, outlen);
  3658. return outlen;
  3659. }
  3660. size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
  3661. unsigned char *out, size_t outlen)
  3662. {
  3663. if (outlen == 0)
  3664. return session->master_key_length;
  3665. if (outlen > session->master_key_length)
  3666. outlen = session->master_key_length;
  3667. memcpy(out, session->master_key, outlen);
  3668. return outlen;
  3669. }
  3670. int SSL_SESSION_set1_master_key(SSL_SESSION *sess, const unsigned char *in,
  3671. size_t len)
  3672. {
  3673. if (len > sizeof(sess->master_key))
  3674. return 0;
  3675. memcpy(sess->master_key, in, len);
  3676. sess->master_key_length = len;
  3677. return 1;
  3678. }
  3679. int SSL_set_ex_data(SSL *s, int idx, void *arg)
  3680. {
  3681. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  3682. }
  3683. void *SSL_get_ex_data(const SSL *s, int idx)
  3684. {
  3685. return CRYPTO_get_ex_data(&s->ex_data, idx);
  3686. }
  3687. int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
  3688. {
  3689. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  3690. }
  3691. void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
  3692. {
  3693. return CRYPTO_get_ex_data(&s->ex_data, idx);
  3694. }
  3695. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
  3696. {
  3697. return ctx->cert_store;
  3698. }
  3699. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
  3700. {
  3701. X509_STORE_free(ctx->cert_store);
  3702. ctx->cert_store = store;
  3703. }
  3704. void SSL_CTX_set1_cert_store(SSL_CTX *ctx, X509_STORE *store)
  3705. {
  3706. if (store != NULL)
  3707. X509_STORE_up_ref(store);
  3708. SSL_CTX_set_cert_store(ctx, store);
  3709. }
  3710. int SSL_want(const SSL *s)
  3711. {
  3712. return s->rwstate;
  3713. }
  3714. /**
  3715. * \brief Set the callback for generating temporary DH keys.
  3716. * \param ctx the SSL context.
  3717. * \param dh the callback
  3718. */
  3719. #ifndef OPENSSL_NO_DH
  3720. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  3721. DH *(*dh) (SSL *ssl, int is_export,
  3722. int keylength))
  3723. {
  3724. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
  3725. }
  3726. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh) (SSL *ssl, int is_export,
  3727. int keylength))
  3728. {
  3729. SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
  3730. }
  3731. #endif
  3732. #ifndef OPENSSL_NO_PSK
  3733. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
  3734. {
  3735. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  3736. SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
  3737. return 0;
  3738. }
  3739. OPENSSL_free(ctx->cert->psk_identity_hint);
  3740. if (identity_hint != NULL) {
  3741. ctx->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
  3742. if (ctx->cert->psk_identity_hint == NULL)
  3743. return 0;
  3744. } else
  3745. ctx->cert->psk_identity_hint = NULL;
  3746. return 1;
  3747. }
  3748. int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
  3749. {
  3750. if (s == NULL)
  3751. return 0;
  3752. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  3753. SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
  3754. return 0;
  3755. }
  3756. OPENSSL_free(s->cert->psk_identity_hint);
  3757. if (identity_hint != NULL) {
  3758. s->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
  3759. if (s->cert->psk_identity_hint == NULL)
  3760. return 0;
  3761. } else
  3762. s->cert->psk_identity_hint = NULL;
  3763. return 1;
  3764. }
  3765. const char *SSL_get_psk_identity_hint(const SSL *s)
  3766. {
  3767. if (s == NULL || s->session == NULL)
  3768. return NULL;
  3769. return s->session->psk_identity_hint;
  3770. }
  3771. const char *SSL_get_psk_identity(const SSL *s)
  3772. {
  3773. if (s == NULL || s->session == NULL)
  3774. return NULL;
  3775. return s->session->psk_identity;
  3776. }
  3777. void SSL_set_psk_client_callback(SSL *s, SSL_psk_client_cb_func cb)
  3778. {
  3779. s->psk_client_callback = cb;
  3780. }
  3781. void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, SSL_psk_client_cb_func cb)
  3782. {
  3783. ctx->psk_client_callback = cb;
  3784. }
  3785. void SSL_set_psk_server_callback(SSL *s, SSL_psk_server_cb_func cb)
  3786. {
  3787. s->psk_server_callback = cb;
  3788. }
  3789. void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, SSL_psk_server_cb_func cb)
  3790. {
  3791. ctx->psk_server_callback = cb;
  3792. }
  3793. #endif
  3794. void SSL_set_psk_find_session_callback(SSL *s, SSL_psk_find_session_cb_func cb)
  3795. {
  3796. s->psk_find_session_cb = cb;
  3797. }
  3798. void SSL_CTX_set_psk_find_session_callback(SSL_CTX *ctx,
  3799. SSL_psk_find_session_cb_func cb)
  3800. {
  3801. ctx->psk_find_session_cb = cb;
  3802. }
  3803. void SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb)
  3804. {
  3805. s->psk_use_session_cb = cb;
  3806. }
  3807. void SSL_CTX_set_psk_use_session_callback(SSL_CTX *ctx,
  3808. SSL_psk_use_session_cb_func cb)
  3809. {
  3810. ctx->psk_use_session_cb = cb;
  3811. }
  3812. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  3813. void (*cb) (int write_p, int version,
  3814. int content_type, const void *buf,
  3815. size_t len, SSL *ssl, void *arg))
  3816. {
  3817. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  3818. }
  3819. void SSL_set_msg_callback(SSL *ssl,
  3820. void (*cb) (int write_p, int version,
  3821. int content_type, const void *buf,
  3822. size_t len, SSL *ssl, void *arg))
  3823. {
  3824. SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  3825. }
  3826. void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx,
  3827. int (*cb) (SSL *ssl,
  3828. int
  3829. is_forward_secure))
  3830. {
  3831. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
  3832. (void (*)(void))cb);
  3833. }
  3834. void SSL_set_not_resumable_session_callback(SSL *ssl,
  3835. int (*cb) (SSL *ssl,
  3836. int is_forward_secure))
  3837. {
  3838. SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
  3839. (void (*)(void))cb);
  3840. }
  3841. void SSL_CTX_set_record_padding_callback(SSL_CTX *ctx,
  3842. size_t (*cb) (SSL *ssl, int type,
  3843. size_t len, void *arg))
  3844. {
  3845. ctx->record_padding_cb = cb;
  3846. }
  3847. void SSL_CTX_set_record_padding_callback_arg(SSL_CTX *ctx, void *arg)
  3848. {
  3849. ctx->record_padding_arg = arg;
  3850. }
  3851. void *SSL_CTX_get_record_padding_callback_arg(const SSL_CTX *ctx)
  3852. {
  3853. return ctx->record_padding_arg;
  3854. }
  3855. int SSL_CTX_set_block_padding(SSL_CTX *ctx, size_t block_size)
  3856. {
  3857. /* block size of 0 or 1 is basically no padding */
  3858. if (block_size == 1)
  3859. ctx->block_padding = 0;
  3860. else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
  3861. ctx->block_padding = block_size;
  3862. else
  3863. return 0;
  3864. return 1;
  3865. }
  3866. void SSL_set_record_padding_callback(SSL *ssl,
  3867. size_t (*cb) (SSL *ssl, int type,
  3868. size_t len, void *arg))
  3869. {
  3870. ssl->record_padding_cb = cb;
  3871. }
  3872. void SSL_set_record_padding_callback_arg(SSL *ssl, void *arg)
  3873. {
  3874. ssl->record_padding_arg = arg;
  3875. }
  3876. void *SSL_get_record_padding_callback_arg(const SSL *ssl)
  3877. {
  3878. return ssl->record_padding_arg;
  3879. }
  3880. int SSL_set_block_padding(SSL *ssl, size_t block_size)
  3881. {
  3882. /* block size of 0 or 1 is basically no padding */
  3883. if (block_size == 1)
  3884. ssl->block_padding = 0;
  3885. else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
  3886. ssl->block_padding = block_size;
  3887. else
  3888. return 0;
  3889. return 1;
  3890. }
  3891. int SSL_set_num_tickets(SSL *s, size_t num_tickets)
  3892. {
  3893. s->num_tickets = num_tickets;
  3894. return 1;
  3895. }
  3896. size_t SSL_get_num_tickets(const SSL *s)
  3897. {
  3898. return s->num_tickets;
  3899. }
  3900. int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets)
  3901. {
  3902. ctx->num_tickets = num_tickets;
  3903. return 1;
  3904. }
  3905. size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx)
  3906. {
  3907. return ctx->num_tickets;
  3908. }
  3909. /*
  3910. * Allocates new EVP_MD_CTX and sets pointer to it into given pointer
  3911. * variable, freeing EVP_MD_CTX previously stored in that variable, if any.
  3912. * If EVP_MD pointer is passed, initializes ctx with this |md|.
  3913. * Returns the newly allocated ctx;
  3914. */
  3915. EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)
  3916. {
  3917. ssl_clear_hash_ctx(hash);
  3918. *hash = EVP_MD_CTX_new();
  3919. if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) {
  3920. EVP_MD_CTX_free(*hash);
  3921. *hash = NULL;
  3922. return NULL;
  3923. }
  3924. return *hash;
  3925. }
  3926. void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
  3927. {
  3928. EVP_MD_CTX_free(*hash);
  3929. *hash = NULL;
  3930. }
  3931. /* Retrieve handshake hashes */
  3932. int ssl_handshake_hash(SSL *s, unsigned char *out, size_t outlen,
  3933. size_t *hashlen)
  3934. {
  3935. EVP_MD_CTX *ctx = NULL;
  3936. EVP_MD_CTX *hdgst = s->s3->handshake_dgst;
  3937. int hashleni = EVP_MD_CTX_size(hdgst);
  3938. int ret = 0;
  3939. if (hashleni < 0 || (size_t)hashleni > outlen) {
  3940. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_HANDSHAKE_HASH,
  3941. ERR_R_INTERNAL_ERROR);
  3942. goto err;
  3943. }
  3944. ctx = EVP_MD_CTX_new();
  3945. if (ctx == NULL) {
  3946. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_HANDSHAKE_HASH,
  3947. ERR_R_INTERNAL_ERROR);
  3948. goto err;
  3949. }
  3950. if (!EVP_MD_CTX_copy_ex(ctx, hdgst)
  3951. || EVP_DigestFinal_ex(ctx, out, NULL) <= 0) {
  3952. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_HANDSHAKE_HASH,
  3953. ERR_R_INTERNAL_ERROR);
  3954. goto err;
  3955. }
  3956. *hashlen = hashleni;
  3957. ret = 1;
  3958. err:
  3959. EVP_MD_CTX_free(ctx);
  3960. return ret;
  3961. }
  3962. int SSL_session_reused(const SSL *s)
  3963. {
  3964. return s->hit;
  3965. }
  3966. int SSL_is_server(const SSL *s)
  3967. {
  3968. return s->server;
  3969. }
  3970. #if OPENSSL_API_COMPAT < 0x10100000L
  3971. void SSL_set_debug(SSL *s, int debug)
  3972. {
  3973. /* Old function was do-nothing anyway... */
  3974. (void)s;
  3975. (void)debug;
  3976. }
  3977. #endif
  3978. void SSL_set_security_level(SSL *s, int level)
  3979. {
  3980. s->cert->sec_level = level;
  3981. }
  3982. int SSL_get_security_level(const SSL *s)
  3983. {
  3984. return s->cert->sec_level;
  3985. }
  3986. void SSL_set_security_callback(SSL *s,
  3987. int (*cb) (const SSL *s, const SSL_CTX *ctx,
  3988. int op, int bits, int nid,
  3989. void *other, void *ex))
  3990. {
  3991. s->cert->sec_cb = cb;
  3992. }
  3993. int (*SSL_get_security_callback(const SSL *s)) (const SSL *s,
  3994. const SSL_CTX *ctx, int op,
  3995. int bits, int nid, void *other,
  3996. void *ex) {
  3997. return s->cert->sec_cb;
  3998. }
  3999. void SSL_set0_security_ex_data(SSL *s, void *ex)
  4000. {
  4001. s->cert->sec_ex = ex;
  4002. }
  4003. void *SSL_get0_security_ex_data(const SSL *s)
  4004. {
  4005. return s->cert->sec_ex;
  4006. }
  4007. void SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
  4008. {
  4009. ctx->cert->sec_level = level;
  4010. }
  4011. int SSL_CTX_get_security_level(const SSL_CTX *ctx)
  4012. {
  4013. return ctx->cert->sec_level;
  4014. }
  4015. void SSL_CTX_set_security_callback(SSL_CTX *ctx,
  4016. int (*cb) (const SSL *s, const SSL_CTX *ctx,
  4017. int op, int bits, int nid,
  4018. void *other, void *ex))
  4019. {
  4020. ctx->cert->sec_cb = cb;
  4021. }
  4022. int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx)) (const SSL *s,
  4023. const SSL_CTX *ctx,
  4024. int op, int bits,
  4025. int nid,
  4026. void *other,
  4027. void *ex) {
  4028. return ctx->cert->sec_cb;
  4029. }
  4030. void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex)
  4031. {
  4032. ctx->cert->sec_ex = ex;
  4033. }
  4034. void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx)
  4035. {
  4036. return ctx->cert->sec_ex;
  4037. }
  4038. /*
  4039. * Get/Set/Clear options in SSL_CTX or SSL, formerly macros, now functions that
  4040. * can return unsigned long, instead of the generic long return value from the
  4041. * control interface.
  4042. */
  4043. unsigned long SSL_CTX_get_options(const SSL_CTX *ctx)
  4044. {
  4045. return ctx->options;
  4046. }
  4047. unsigned long SSL_get_options(const SSL *s)
  4048. {
  4049. return s->options;
  4050. }
  4051. unsigned long SSL_CTX_set_options(SSL_CTX *ctx, unsigned long op)
  4052. {
  4053. return ctx->options |= op;
  4054. }
  4055. unsigned long SSL_set_options(SSL *s, unsigned long op)
  4056. {
  4057. return s->options |= op;
  4058. }
  4059. unsigned long SSL_CTX_clear_options(SSL_CTX *ctx, unsigned long op)
  4060. {
  4061. return ctx->options &= ~op;
  4062. }
  4063. unsigned long SSL_clear_options(SSL *s, unsigned long op)
  4064. {
  4065. return s->options &= ~op;
  4066. }
  4067. STACK_OF(X509) *SSL_get0_verified_chain(const SSL *s)
  4068. {
  4069. return s->verified_chain;
  4070. }
  4071. IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);
  4072. #ifndef OPENSSL_NO_CT
  4073. /*
  4074. * Moves SCTs from the |src| stack to the |dst| stack.
  4075. * The source of each SCT will be set to |origin|.
  4076. * If |dst| points to a NULL pointer, a new stack will be created and owned by
  4077. * the caller.
  4078. * Returns the number of SCTs moved, or a negative integer if an error occurs.
  4079. */
  4080. static int ct_move_scts(STACK_OF(SCT) **dst, STACK_OF(SCT) *src,
  4081. sct_source_t origin)
  4082. {
  4083. int scts_moved = 0;
  4084. SCT *sct = NULL;
  4085. if (*dst == NULL) {
  4086. *dst = sk_SCT_new_null();
  4087. if (*dst == NULL) {
  4088. SSLerr(SSL_F_CT_MOVE_SCTS, ERR_R_MALLOC_FAILURE);
  4089. goto err;
  4090. }
  4091. }
  4092. while ((sct = sk_SCT_pop(src)) != NULL) {
  4093. if (SCT_set_source(sct, origin) != 1)
  4094. goto err;
  4095. if (sk_SCT_push(*dst, sct) <= 0)
  4096. goto err;
  4097. scts_moved += 1;
  4098. }
  4099. return scts_moved;
  4100. err:
  4101. if (sct != NULL)
  4102. sk_SCT_push(src, sct); /* Put the SCT back */
  4103. return -1;
  4104. }
  4105. /*
  4106. * Look for data collected during ServerHello and parse if found.
  4107. * Returns the number of SCTs extracted.
  4108. */
  4109. static int ct_extract_tls_extension_scts(SSL *s)
  4110. {
  4111. int scts_extracted = 0;
  4112. if (s->ext.scts != NULL) {
  4113. const unsigned char *p = s->ext.scts;
  4114. STACK_OF(SCT) *scts = o2i_SCT_LIST(NULL, &p, s->ext.scts_len);
  4115. scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_TLS_EXTENSION);
  4116. SCT_LIST_free(scts);
  4117. }
  4118. return scts_extracted;
  4119. }
  4120. /*
  4121. * Checks for an OCSP response and then attempts to extract any SCTs found if it
  4122. * contains an SCT X509 extension. They will be stored in |s->scts|.
  4123. * Returns:
  4124. * - The number of SCTs extracted, assuming an OCSP response exists.
  4125. * - 0 if no OCSP response exists or it contains no SCTs.
  4126. * - A negative integer if an error occurs.
  4127. */
  4128. static int ct_extract_ocsp_response_scts(SSL *s)
  4129. {
  4130. # ifndef OPENSSL_NO_OCSP
  4131. int scts_extracted = 0;
  4132. const unsigned char *p;
  4133. OCSP_BASICRESP *br = NULL;
  4134. OCSP_RESPONSE *rsp = NULL;
  4135. STACK_OF(SCT) *scts = NULL;
  4136. int i;
  4137. if (s->ext.ocsp.resp == NULL || s->ext.ocsp.resp_len == 0)
  4138. goto err;
  4139. p = s->ext.ocsp.resp;
  4140. rsp = d2i_OCSP_RESPONSE(NULL, &p, (int)s->ext.ocsp.resp_len);
  4141. if (rsp == NULL)
  4142. goto err;
  4143. br = OCSP_response_get1_basic(rsp);
  4144. if (br == NULL)
  4145. goto err;
  4146. for (i = 0; i < OCSP_resp_count(br); ++i) {
  4147. OCSP_SINGLERESP *single = OCSP_resp_get0(br, i);
  4148. if (single == NULL)
  4149. continue;
  4150. scts =
  4151. OCSP_SINGLERESP_get1_ext_d2i(single, NID_ct_cert_scts, NULL, NULL);
  4152. scts_extracted =
  4153. ct_move_scts(&s->scts, scts, SCT_SOURCE_OCSP_STAPLED_RESPONSE);
  4154. if (scts_extracted < 0)
  4155. goto err;
  4156. }
  4157. err:
  4158. SCT_LIST_free(scts);
  4159. OCSP_BASICRESP_free(br);
  4160. OCSP_RESPONSE_free(rsp);
  4161. return scts_extracted;
  4162. # else
  4163. /* Behave as if no OCSP response exists */
  4164. return 0;
  4165. # endif
  4166. }
  4167. /*
  4168. * Attempts to extract SCTs from the peer certificate.
  4169. * Return the number of SCTs extracted, or a negative integer if an error
  4170. * occurs.
  4171. */
  4172. static int ct_extract_x509v3_extension_scts(SSL *s)
  4173. {
  4174. int scts_extracted = 0;
  4175. X509 *cert = s->session != NULL ? s->session->peer : NULL;
  4176. if (cert != NULL) {
  4177. STACK_OF(SCT) *scts =
  4178. X509_get_ext_d2i(cert, NID_ct_precert_scts, NULL, NULL);
  4179. scts_extracted =
  4180. ct_move_scts(&s->scts, scts, SCT_SOURCE_X509V3_EXTENSION);
  4181. SCT_LIST_free(scts);
  4182. }
  4183. return scts_extracted;
  4184. }
  4185. /*
  4186. * Attempts to find all received SCTs by checking TLS extensions, the OCSP
  4187. * response (if it exists) and X509v3 extensions in the certificate.
  4188. * Returns NULL if an error occurs.
  4189. */
  4190. const STACK_OF(SCT) *SSL_get0_peer_scts(SSL *s)
  4191. {
  4192. if (!s->scts_parsed) {
  4193. if (ct_extract_tls_extension_scts(s) < 0 ||
  4194. ct_extract_ocsp_response_scts(s) < 0 ||
  4195. ct_extract_x509v3_extension_scts(s) < 0)
  4196. goto err;
  4197. s->scts_parsed = 1;
  4198. }
  4199. return s->scts;
  4200. err:
  4201. return NULL;
  4202. }
  4203. static int ct_permissive(const CT_POLICY_EVAL_CTX * ctx,
  4204. const STACK_OF(SCT) *scts, void *unused_arg)
  4205. {
  4206. return 1;
  4207. }
  4208. static int ct_strict(const CT_POLICY_EVAL_CTX * ctx,
  4209. const STACK_OF(SCT) *scts, void *unused_arg)
  4210. {
  4211. int count = scts != NULL ? sk_SCT_num(scts) : 0;
  4212. int i;
  4213. for (i = 0; i < count; ++i) {
  4214. SCT *sct = sk_SCT_value(scts, i);
  4215. int status = SCT_get_validation_status(sct);
  4216. if (status == SCT_VALIDATION_STATUS_VALID)
  4217. return 1;
  4218. }
  4219. SSLerr(SSL_F_CT_STRICT, SSL_R_NO_VALID_SCTS);
  4220. return 0;
  4221. }
  4222. int SSL_set_ct_validation_callback(SSL *s, ssl_ct_validation_cb callback,
  4223. void *arg)
  4224. {
  4225. /*
  4226. * Since code exists that uses the custom extension handler for CT, look
  4227. * for this and throw an error if they have already registered to use CT.
  4228. */
  4229. if (callback != NULL && SSL_CTX_has_client_custom_ext(s->ctx,
  4230. TLSEXT_TYPE_signed_certificate_timestamp))
  4231. {
  4232. SSLerr(SSL_F_SSL_SET_CT_VALIDATION_CALLBACK,
  4233. SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
  4234. return 0;
  4235. }
  4236. if (callback != NULL) {
  4237. /*
  4238. * If we are validating CT, then we MUST accept SCTs served via OCSP
  4239. */
  4240. if (!SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp))
  4241. return 0;
  4242. }
  4243. s->ct_validation_callback = callback;
  4244. s->ct_validation_callback_arg = arg;
  4245. return 1;
  4246. }
  4247. int SSL_CTX_set_ct_validation_callback(SSL_CTX *ctx,
  4248. ssl_ct_validation_cb callback, void *arg)
  4249. {
  4250. /*
  4251. * Since code exists that uses the custom extension handler for CT, look for
  4252. * this and throw an error if they have already registered to use CT.
  4253. */
  4254. if (callback != NULL && SSL_CTX_has_client_custom_ext(ctx,
  4255. TLSEXT_TYPE_signed_certificate_timestamp))
  4256. {
  4257. SSLerr(SSL_F_SSL_CTX_SET_CT_VALIDATION_CALLBACK,
  4258. SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
  4259. return 0;
  4260. }
  4261. ctx->ct_validation_callback = callback;
  4262. ctx->ct_validation_callback_arg = arg;
  4263. return 1;
  4264. }
  4265. int SSL_ct_is_enabled(const SSL *s)
  4266. {
  4267. return s->ct_validation_callback != NULL;
  4268. }
  4269. int SSL_CTX_ct_is_enabled(const SSL_CTX *ctx)
  4270. {
  4271. return ctx->ct_validation_callback != NULL;
  4272. }
  4273. int ssl_validate_ct(SSL *s)
  4274. {
  4275. int ret = 0;
  4276. X509 *cert = s->session != NULL ? s->session->peer : NULL;
  4277. X509 *issuer;
  4278. SSL_DANE *dane = &s->dane;
  4279. CT_POLICY_EVAL_CTX *ctx = NULL;
  4280. const STACK_OF(SCT) *scts;
  4281. /*
  4282. * If no callback is set, the peer is anonymous, or its chain is invalid,
  4283. * skip SCT validation - just return success. Applications that continue
  4284. * handshakes without certificates, with unverified chains, or pinned leaf
  4285. * certificates are outside the scope of the WebPKI and CT.
  4286. *
  4287. * The above exclusions notwithstanding the vast majority of peers will
  4288. * have rather ordinary certificate chains validated by typical
  4289. * applications that perform certificate verification and therefore will
  4290. * process SCTs when enabled.
  4291. */
  4292. if (s->ct_validation_callback == NULL || cert == NULL ||
  4293. s->verify_result != X509_V_OK ||
  4294. s->verified_chain == NULL || sk_X509_num(s->verified_chain) <= 1)
  4295. return 1;
  4296. /*
  4297. * CT not applicable for chains validated via DANE-TA(2) or DANE-EE(3)
  4298. * trust-anchors. See https://tools.ietf.org/html/rfc7671#section-4.2
  4299. */
  4300. if (DANETLS_ENABLED(dane) && dane->mtlsa != NULL) {
  4301. switch (dane->mtlsa->usage) {
  4302. case DANETLS_USAGE_DANE_TA:
  4303. case DANETLS_USAGE_DANE_EE:
  4304. return 1;
  4305. }
  4306. }
  4307. ctx = CT_POLICY_EVAL_CTX_new();
  4308. if (ctx == NULL) {
  4309. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_VALIDATE_CT,
  4310. ERR_R_MALLOC_FAILURE);
  4311. goto end;
  4312. }
  4313. issuer = sk_X509_value(s->verified_chain, 1);
  4314. CT_POLICY_EVAL_CTX_set1_cert(ctx, cert);
  4315. CT_POLICY_EVAL_CTX_set1_issuer(ctx, issuer);
  4316. CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(ctx, s->ctx->ctlog_store);
  4317. CT_POLICY_EVAL_CTX_set_time(
  4318. ctx, (uint64_t)SSL_SESSION_get_time(SSL_get0_session(s)) * 1000);
  4319. scts = SSL_get0_peer_scts(s);
  4320. /*
  4321. * This function returns success (> 0) only when all the SCTs are valid, 0
  4322. * when some are invalid, and < 0 on various internal errors (out of
  4323. * memory, etc.). Having some, or even all, invalid SCTs is not sufficient
  4324. * reason to abort the handshake, that decision is up to the callback.
  4325. * Therefore, we error out only in the unexpected case that the return
  4326. * value is negative.
  4327. *
  4328. * XXX: One might well argue that the return value of this function is an
  4329. * unfortunate design choice. Its job is only to determine the validation
  4330. * status of each of the provided SCTs. So long as it correctly separates
  4331. * the wheat from the chaff it should return success. Failure in this case
  4332. * ought to correspond to an inability to carry out its duties.
  4333. */
  4334. if (SCT_LIST_validate(scts, ctx) < 0) {
  4335. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_SSL_VALIDATE_CT,
  4336. SSL_R_SCT_VERIFICATION_FAILED);
  4337. goto end;
  4338. }
  4339. ret = s->ct_validation_callback(ctx, scts, s->ct_validation_callback_arg);
  4340. if (ret < 0)
  4341. ret = 0; /* This function returns 0 on failure */
  4342. if (!ret)
  4343. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_SSL_VALIDATE_CT,
  4344. SSL_R_CALLBACK_FAILED);
  4345. end:
  4346. CT_POLICY_EVAL_CTX_free(ctx);
  4347. /*
  4348. * With SSL_VERIFY_NONE the session may be cached and re-used despite a
  4349. * failure return code here. Also the application may wish the complete
  4350. * the handshake, and then disconnect cleanly at a higher layer, after
  4351. * checking the verification status of the completed connection.
  4352. *
  4353. * We therefore force a certificate verification failure which will be
  4354. * visible via SSL_get_verify_result() and cached as part of any resumed
  4355. * session.
  4356. *
  4357. * Note: the permissive callback is for information gathering only, always
  4358. * returns success, and does not affect verification status. Only the
  4359. * strict callback or a custom application-specified callback can trigger
  4360. * connection failure or record a verification error.
  4361. */
  4362. if (ret <= 0)
  4363. s->verify_result = X509_V_ERR_NO_VALID_SCTS;
  4364. return ret;
  4365. }
  4366. int SSL_CTX_enable_ct(SSL_CTX *ctx, int validation_mode)
  4367. {
  4368. switch (validation_mode) {
  4369. default:
  4370. SSLerr(SSL_F_SSL_CTX_ENABLE_CT, SSL_R_INVALID_CT_VALIDATION_TYPE);
  4371. return 0;
  4372. case SSL_CT_VALIDATION_PERMISSIVE:
  4373. return SSL_CTX_set_ct_validation_callback(ctx, ct_permissive, NULL);
  4374. case SSL_CT_VALIDATION_STRICT:
  4375. return SSL_CTX_set_ct_validation_callback(ctx, ct_strict, NULL);
  4376. }
  4377. }
  4378. int SSL_enable_ct(SSL *s, int validation_mode)
  4379. {
  4380. switch (validation_mode) {
  4381. default:
  4382. SSLerr(SSL_F_SSL_ENABLE_CT, SSL_R_INVALID_CT_VALIDATION_TYPE);
  4383. return 0;
  4384. case SSL_CT_VALIDATION_PERMISSIVE:
  4385. return SSL_set_ct_validation_callback(s, ct_permissive, NULL);
  4386. case SSL_CT_VALIDATION_STRICT:
  4387. return SSL_set_ct_validation_callback(s, ct_strict, NULL);
  4388. }
  4389. }
  4390. int SSL_CTX_set_default_ctlog_list_file(SSL_CTX *ctx)
  4391. {
  4392. return CTLOG_STORE_load_default_file(ctx->ctlog_store);
  4393. }
  4394. int SSL_CTX_set_ctlog_list_file(SSL_CTX *ctx, const char *path)
  4395. {
  4396. return CTLOG_STORE_load_file(ctx->ctlog_store, path);
  4397. }
  4398. void SSL_CTX_set0_ctlog_store(SSL_CTX *ctx, CTLOG_STORE * logs)
  4399. {
  4400. CTLOG_STORE_free(ctx->ctlog_store);
  4401. ctx->ctlog_store = logs;
  4402. }
  4403. const CTLOG_STORE *SSL_CTX_get0_ctlog_store(const SSL_CTX *ctx)
  4404. {
  4405. return ctx->ctlog_store;
  4406. }
  4407. #endif /* OPENSSL_NO_CT */
  4408. void SSL_CTX_set_client_hello_cb(SSL_CTX *c, SSL_client_hello_cb_fn cb,
  4409. void *arg)
  4410. {
  4411. c->client_hello_cb = cb;
  4412. c->client_hello_cb_arg = arg;
  4413. }
  4414. int SSL_client_hello_isv2(SSL *s)
  4415. {
  4416. if (s->clienthello == NULL)
  4417. return 0;
  4418. return s->clienthello->isv2;
  4419. }
  4420. unsigned int SSL_client_hello_get0_legacy_version(SSL *s)
  4421. {
  4422. if (s->clienthello == NULL)
  4423. return 0;
  4424. return s->clienthello->legacy_version;
  4425. }
  4426. size_t SSL_client_hello_get0_random(SSL *s, const unsigned char **out)
  4427. {
  4428. if (s->clienthello == NULL)
  4429. return 0;
  4430. if (out != NULL)
  4431. *out = s->clienthello->random;
  4432. return SSL3_RANDOM_SIZE;
  4433. }
  4434. size_t SSL_client_hello_get0_session_id(SSL *s, const unsigned char **out)
  4435. {
  4436. if (s->clienthello == NULL)
  4437. return 0;
  4438. if (out != NULL)
  4439. *out = s->clienthello->session_id;
  4440. return s->clienthello->session_id_len;
  4441. }
  4442. size_t SSL_client_hello_get0_ciphers(SSL *s, const unsigned char **out)
  4443. {
  4444. if (s->clienthello == NULL)
  4445. return 0;
  4446. if (out != NULL)
  4447. *out = PACKET_data(&s->clienthello->ciphersuites);
  4448. return PACKET_remaining(&s->clienthello->ciphersuites);
  4449. }
  4450. size_t SSL_client_hello_get0_compression_methods(SSL *s, const unsigned char **out)
  4451. {
  4452. if (s->clienthello == NULL)
  4453. return 0;
  4454. if (out != NULL)
  4455. *out = s->clienthello->compressions;
  4456. return s->clienthello->compressions_len;
  4457. }
  4458. int SSL_client_hello_get1_extensions_present(SSL *s, int **out, size_t *outlen)
  4459. {
  4460. RAW_EXTENSION *ext;
  4461. int *present;
  4462. size_t num = 0, i;
  4463. if (s->clienthello == NULL || out == NULL || outlen == NULL)
  4464. return 0;
  4465. for (i = 0; i < s->clienthello->pre_proc_exts_len; i++) {
  4466. ext = s->clienthello->pre_proc_exts + i;
  4467. if (ext->present)
  4468. num++;
  4469. }
  4470. if (num == 0) {
  4471. *out = NULL;
  4472. *outlen = 0;
  4473. return 1;
  4474. }
  4475. if ((present = OPENSSL_malloc(sizeof(*present) * num)) == NULL) {
  4476. SSLerr(SSL_F_SSL_CLIENT_HELLO_GET1_EXTENSIONS_PRESENT,
  4477. ERR_R_MALLOC_FAILURE);
  4478. return 0;
  4479. }
  4480. for (i = 0; i < s->clienthello->pre_proc_exts_len; i++) {
  4481. ext = s->clienthello->pre_proc_exts + i;
  4482. if (ext->present) {
  4483. if (ext->received_order >= num)
  4484. goto err;
  4485. present[ext->received_order] = ext->type;
  4486. }
  4487. }
  4488. *out = present;
  4489. *outlen = num;
  4490. return 1;
  4491. err:
  4492. OPENSSL_free(present);
  4493. return 0;
  4494. }
  4495. int SSL_client_hello_get0_ext(SSL *s, unsigned int type, const unsigned char **out,
  4496. size_t *outlen)
  4497. {
  4498. size_t i;
  4499. RAW_EXTENSION *r;
  4500. if (s->clienthello == NULL)
  4501. return 0;
  4502. for (i = 0; i < s->clienthello->pre_proc_exts_len; ++i) {
  4503. r = s->clienthello->pre_proc_exts + i;
  4504. if (r->present && r->type == type) {
  4505. if (out != NULL)
  4506. *out = PACKET_data(&r->data);
  4507. if (outlen != NULL)
  4508. *outlen = PACKET_remaining(&r->data);
  4509. return 1;
  4510. }
  4511. }
  4512. return 0;
  4513. }
  4514. int SSL_free_buffers(SSL *ssl)
  4515. {
  4516. RECORD_LAYER *rl = &ssl->rlayer;
  4517. if (RECORD_LAYER_read_pending(rl) || RECORD_LAYER_write_pending(rl))
  4518. return 0;
  4519. RECORD_LAYER_release(rl);
  4520. return 1;
  4521. }
  4522. int SSL_alloc_buffers(SSL *ssl)
  4523. {
  4524. return ssl3_setup_buffers(ssl);
  4525. }
  4526. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb)
  4527. {
  4528. ctx->keylog_callback = cb;
  4529. }
  4530. SSL_CTX_keylog_cb_func SSL_CTX_get_keylog_callback(const SSL_CTX *ctx)
  4531. {
  4532. return ctx->keylog_callback;
  4533. }
  4534. static int nss_keylog_int(const char *prefix,
  4535. SSL *ssl,
  4536. const uint8_t *parameter_1,
  4537. size_t parameter_1_len,
  4538. const uint8_t *parameter_2,
  4539. size_t parameter_2_len)
  4540. {
  4541. char *out = NULL;
  4542. char *cursor = NULL;
  4543. size_t out_len = 0;
  4544. size_t i;
  4545. size_t prefix_len;
  4546. if (ssl->ctx->keylog_callback == NULL)
  4547. return 1;
  4548. /*
  4549. * Our output buffer will contain the following strings, rendered with
  4550. * space characters in between, terminated by a NULL character: first the
  4551. * prefix, then the first parameter, then the second parameter. The
  4552. * meaning of each parameter depends on the specific key material being
  4553. * logged. Note that the first and second parameters are encoded in
  4554. * hexadecimal, so we need a buffer that is twice their lengths.
  4555. */
  4556. prefix_len = strlen(prefix);
  4557. out_len = prefix_len + (2 * parameter_1_len) + (2 * parameter_2_len) + 3;
  4558. if ((out = cursor = OPENSSL_malloc(out_len)) == NULL) {
  4559. SSLfatal(ssl, SSL_AD_INTERNAL_ERROR, SSL_F_NSS_KEYLOG_INT,
  4560. ERR_R_MALLOC_FAILURE);
  4561. return 0;
  4562. }
  4563. strcpy(cursor, prefix);
  4564. cursor += prefix_len;
  4565. *cursor++ = ' ';
  4566. for (i = 0; i < parameter_1_len; i++) {
  4567. sprintf(cursor, "%02x", parameter_1[i]);
  4568. cursor += 2;
  4569. }
  4570. *cursor++ = ' ';
  4571. for (i = 0; i < parameter_2_len; i++) {
  4572. sprintf(cursor, "%02x", parameter_2[i]);
  4573. cursor += 2;
  4574. }
  4575. *cursor = '\0';
  4576. ssl->ctx->keylog_callback(ssl, (const char *)out);
  4577. OPENSSL_clear_free(out, out_len);
  4578. return 1;
  4579. }
  4580. int ssl_log_rsa_client_key_exchange(SSL *ssl,
  4581. const uint8_t *encrypted_premaster,
  4582. size_t encrypted_premaster_len,
  4583. const uint8_t *premaster,
  4584. size_t premaster_len)
  4585. {
  4586. if (encrypted_premaster_len < 8) {
  4587. SSLfatal(ssl, SSL_AD_INTERNAL_ERROR,
  4588. SSL_F_SSL_LOG_RSA_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  4589. return 0;
  4590. }
  4591. /* We only want the first 8 bytes of the encrypted premaster as a tag. */
  4592. return nss_keylog_int("RSA",
  4593. ssl,
  4594. encrypted_premaster,
  4595. 8,
  4596. premaster,
  4597. premaster_len);
  4598. }
  4599. int ssl_log_secret(SSL *ssl,
  4600. const char *label,
  4601. const uint8_t *secret,
  4602. size_t secret_len)
  4603. {
  4604. return nss_keylog_int(label,
  4605. ssl,
  4606. ssl->s3->client_random,
  4607. SSL3_RANDOM_SIZE,
  4608. secret,
  4609. secret_len);
  4610. }
  4611. #define SSLV2_CIPHER_LEN 3
  4612. int ssl_cache_cipherlist(SSL *s, PACKET *cipher_suites, int sslv2format)
  4613. {
  4614. int n;
  4615. n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
  4616. if (PACKET_remaining(cipher_suites) == 0) {
  4617. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SSL_CACHE_CIPHERLIST,
  4618. SSL_R_NO_CIPHERS_SPECIFIED);
  4619. return 0;
  4620. }
  4621. if (PACKET_remaining(cipher_suites) % n != 0) {
  4622. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL_CACHE_CIPHERLIST,
  4623. SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  4624. return 0;
  4625. }
  4626. OPENSSL_free(s->s3->tmp.ciphers_raw);
  4627. s->s3->tmp.ciphers_raw = NULL;
  4628. s->s3->tmp.ciphers_rawlen = 0;
  4629. if (sslv2format) {
  4630. size_t numciphers = PACKET_remaining(cipher_suites) / n;
  4631. PACKET sslv2ciphers = *cipher_suites;
  4632. unsigned int leadbyte;
  4633. unsigned char *raw;
  4634. /*
  4635. * We store the raw ciphers list in SSLv3+ format so we need to do some
  4636. * preprocessing to convert the list first. If there are any SSLv2 only
  4637. * ciphersuites with a non-zero leading byte then we are going to
  4638. * slightly over allocate because we won't store those. But that isn't a
  4639. * problem.
  4640. */
  4641. raw = OPENSSL_malloc(numciphers * TLS_CIPHER_LEN);
  4642. s->s3->tmp.ciphers_raw = raw;
  4643. if (raw == NULL) {
  4644. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CACHE_CIPHERLIST,
  4645. ERR_R_MALLOC_FAILURE);
  4646. return 0;
  4647. }
  4648. for (s->s3->tmp.ciphers_rawlen = 0;
  4649. PACKET_remaining(&sslv2ciphers) > 0;
  4650. raw += TLS_CIPHER_LEN) {
  4651. if (!PACKET_get_1(&sslv2ciphers, &leadbyte)
  4652. || (leadbyte == 0
  4653. && !PACKET_copy_bytes(&sslv2ciphers, raw,
  4654. TLS_CIPHER_LEN))
  4655. || (leadbyte != 0
  4656. && !PACKET_forward(&sslv2ciphers, TLS_CIPHER_LEN))) {
  4657. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL_CACHE_CIPHERLIST,
  4658. SSL_R_BAD_PACKET);
  4659. OPENSSL_free(s->s3->tmp.ciphers_raw);
  4660. s->s3->tmp.ciphers_raw = NULL;
  4661. s->s3->tmp.ciphers_rawlen = 0;
  4662. return 0;
  4663. }
  4664. if (leadbyte == 0)
  4665. s->s3->tmp.ciphers_rawlen += TLS_CIPHER_LEN;
  4666. }
  4667. } else if (!PACKET_memdup(cipher_suites, &s->s3->tmp.ciphers_raw,
  4668. &s->s3->tmp.ciphers_rawlen)) {
  4669. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CACHE_CIPHERLIST,
  4670. ERR_R_INTERNAL_ERROR);
  4671. return 0;
  4672. }
  4673. return 1;
  4674. }
  4675. int SSL_bytes_to_cipher_list(SSL *s, const unsigned char *bytes, size_t len,
  4676. int isv2format, STACK_OF(SSL_CIPHER) **sk,
  4677. STACK_OF(SSL_CIPHER) **scsvs)
  4678. {
  4679. PACKET pkt;
  4680. if (!PACKET_buf_init(&pkt, bytes, len))
  4681. return 0;
  4682. return bytes_to_cipher_list(s, &pkt, sk, scsvs, isv2format, 0);
  4683. }
  4684. int bytes_to_cipher_list(SSL *s, PACKET *cipher_suites,
  4685. STACK_OF(SSL_CIPHER) **skp,
  4686. STACK_OF(SSL_CIPHER) **scsvs_out,
  4687. int sslv2format, int fatal)
  4688. {
  4689. const SSL_CIPHER *c;
  4690. STACK_OF(SSL_CIPHER) *sk = NULL;
  4691. STACK_OF(SSL_CIPHER) *scsvs = NULL;
  4692. int n;
  4693. /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
  4694. unsigned char cipher[SSLV2_CIPHER_LEN];
  4695. n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
  4696. if (PACKET_remaining(cipher_suites) == 0) {
  4697. if (fatal)
  4698. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_BYTES_TO_CIPHER_LIST,
  4699. SSL_R_NO_CIPHERS_SPECIFIED);
  4700. else
  4701. SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, SSL_R_NO_CIPHERS_SPECIFIED);
  4702. return 0;
  4703. }
  4704. if (PACKET_remaining(cipher_suites) % n != 0) {
  4705. if (fatal)
  4706. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_BYTES_TO_CIPHER_LIST,
  4707. SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  4708. else
  4709. SSLerr(SSL_F_BYTES_TO_CIPHER_LIST,
  4710. SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  4711. return 0;
  4712. }
  4713. sk = sk_SSL_CIPHER_new_null();
  4714. scsvs = sk_SSL_CIPHER_new_null();
  4715. if (sk == NULL || scsvs == NULL) {
  4716. if (fatal)
  4717. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_BYTES_TO_CIPHER_LIST,
  4718. ERR_R_MALLOC_FAILURE);
  4719. else
  4720. SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
  4721. goto err;
  4722. }
  4723. while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
  4724. /*
  4725. * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
  4726. * first byte set to zero, while true SSLv2 ciphers have a non-zero
  4727. * first byte. We don't support any true SSLv2 ciphers, so skip them.
  4728. */
  4729. if (sslv2format && cipher[0] != '\0')
  4730. continue;
  4731. /* For SSLv2-compat, ignore leading 0-byte. */
  4732. c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher, 1);
  4733. if (c != NULL) {
  4734. if ((c->valid && !sk_SSL_CIPHER_push(sk, c)) ||
  4735. (!c->valid && !sk_SSL_CIPHER_push(scsvs, c))) {
  4736. if (fatal)
  4737. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  4738. SSL_F_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
  4739. else
  4740. SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
  4741. goto err;
  4742. }
  4743. }
  4744. }
  4745. if (PACKET_remaining(cipher_suites) > 0) {
  4746. if (fatal)
  4747. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_BYTES_TO_CIPHER_LIST,
  4748. SSL_R_BAD_LENGTH);
  4749. else
  4750. SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, SSL_R_BAD_LENGTH);
  4751. goto err;
  4752. }
  4753. if (skp != NULL)
  4754. *skp = sk;
  4755. else
  4756. sk_SSL_CIPHER_free(sk);
  4757. if (scsvs_out != NULL)
  4758. *scsvs_out = scsvs;
  4759. else
  4760. sk_SSL_CIPHER_free(scsvs);
  4761. return 1;
  4762. err:
  4763. sk_SSL_CIPHER_free(sk);
  4764. sk_SSL_CIPHER_free(scsvs);
  4765. return 0;
  4766. }
  4767. int SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data)
  4768. {
  4769. ctx->max_early_data = max_early_data;
  4770. return 1;
  4771. }
  4772. uint32_t SSL_CTX_get_max_early_data(const SSL_CTX *ctx)
  4773. {
  4774. return ctx->max_early_data;
  4775. }
  4776. int SSL_set_max_early_data(SSL *s, uint32_t max_early_data)
  4777. {
  4778. s->max_early_data = max_early_data;
  4779. return 1;
  4780. }
  4781. uint32_t SSL_get_max_early_data(const SSL *s)
  4782. {
  4783. return s->max_early_data;
  4784. }
  4785. int SSL_CTX_set_recv_max_early_data(SSL_CTX *ctx, uint32_t recv_max_early_data)
  4786. {
  4787. ctx->recv_max_early_data = recv_max_early_data;
  4788. return 1;
  4789. }
  4790. uint32_t SSL_CTX_get_recv_max_early_data(const SSL_CTX *ctx)
  4791. {
  4792. return ctx->recv_max_early_data;
  4793. }
  4794. int SSL_set_recv_max_early_data(SSL *s, uint32_t recv_max_early_data)
  4795. {
  4796. s->recv_max_early_data = recv_max_early_data;
  4797. return 1;
  4798. }
  4799. uint32_t SSL_get_recv_max_early_data(const SSL *s)
  4800. {
  4801. return s->recv_max_early_data;
  4802. }
  4803. __owur unsigned int ssl_get_max_send_fragment(const SSL *ssl)
  4804. {
  4805. /* Return any active Max Fragment Len extension */
  4806. if (ssl->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(ssl->session))
  4807. return GET_MAX_FRAGMENT_LENGTH(ssl->session);
  4808. /* return current SSL connection setting */
  4809. return ssl->max_send_fragment;
  4810. }
  4811. __owur unsigned int ssl_get_split_send_fragment(const SSL *ssl)
  4812. {
  4813. /* Return a value regarding an active Max Fragment Len extension */
  4814. if (ssl->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(ssl->session)
  4815. && ssl->split_send_fragment > GET_MAX_FRAGMENT_LENGTH(ssl->session))
  4816. return GET_MAX_FRAGMENT_LENGTH(ssl->session);
  4817. /* else limit |split_send_fragment| to current |max_send_fragment| */
  4818. if (ssl->split_send_fragment > ssl->max_send_fragment)
  4819. return ssl->max_send_fragment;
  4820. /* return current SSL connection setting */
  4821. return ssl->split_send_fragment;
  4822. }
  4823. int SSL_stateless(SSL *s)
  4824. {
  4825. int ret;
  4826. /* Ensure there is no state left over from a previous invocation */
  4827. if (!SSL_clear(s))
  4828. return 0;
  4829. ERR_clear_error();
  4830. s->s3->flags |= TLS1_FLAGS_STATELESS;
  4831. ret = SSL_accept(s);
  4832. s->s3->flags &= ~TLS1_FLAGS_STATELESS;
  4833. if (ret > 0 && s->ext.cookieok)
  4834. return 1;
  4835. if (s->hello_retry_request == SSL_HRR_PENDING && !ossl_statem_in_error(s))
  4836. return 0;
  4837. return -1;
  4838. }
  4839. void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
  4840. {
  4841. ctx->pha_enabled = val;
  4842. }
  4843. void SSL_set_post_handshake_auth(SSL *ssl, int val)
  4844. {
  4845. ssl->pha_enabled = val;
  4846. }
  4847. int SSL_verify_client_post_handshake(SSL *ssl)
  4848. {
  4849. if (!SSL_IS_TLS13(ssl)) {
  4850. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_WRONG_SSL_VERSION);
  4851. return 0;
  4852. }
  4853. if (!ssl->server) {
  4854. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_NOT_SERVER);
  4855. return 0;
  4856. }
  4857. if (!SSL_is_init_finished(ssl)) {
  4858. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_STILL_IN_INIT);
  4859. return 0;
  4860. }
  4861. switch (ssl->post_handshake_auth) {
  4862. case SSL_PHA_NONE:
  4863. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_EXTENSION_NOT_RECEIVED);
  4864. return 0;
  4865. default:
  4866. case SSL_PHA_EXT_SENT:
  4867. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, ERR_R_INTERNAL_ERROR);
  4868. return 0;
  4869. case SSL_PHA_EXT_RECEIVED:
  4870. break;
  4871. case SSL_PHA_REQUEST_PENDING:
  4872. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_REQUEST_PENDING);
  4873. return 0;
  4874. case SSL_PHA_REQUESTED:
  4875. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_REQUEST_SENT);
  4876. return 0;
  4877. }
  4878. ssl->post_handshake_auth = SSL_PHA_REQUEST_PENDING;
  4879. /* checks verify_mode and algorithm_auth */
  4880. if (!send_certificate_request(ssl)) {
  4881. ssl->post_handshake_auth = SSL_PHA_EXT_RECEIVED; /* restore on error */
  4882. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_INVALID_CONFIG);
  4883. return 0;
  4884. }
  4885. ossl_statem_set_in_init(ssl, 1);
  4886. return 1;
  4887. }
  4888. int SSL_CTX_set_session_ticket_cb(SSL_CTX *ctx,
  4889. SSL_CTX_generate_session_ticket_fn gen_cb,
  4890. SSL_CTX_decrypt_session_ticket_fn dec_cb,
  4891. void *arg)
  4892. {
  4893. ctx->generate_ticket_cb = gen_cb;
  4894. ctx->decrypt_ticket_cb = dec_cb;
  4895. ctx->ticket_cb_data = arg;
  4896. return 1;
  4897. }
  4898. void SSL_CTX_set_allow_early_data_cb(SSL_CTX *ctx,
  4899. SSL_allow_early_data_cb_fn cb,
  4900. void *arg)
  4901. {
  4902. ctx->allow_early_data_cb = cb;
  4903. ctx->allow_early_data_cb_data = arg;
  4904. }
  4905. void SSL_set_allow_early_data_cb(SSL *s,
  4906. SSL_allow_early_data_cb_fn cb,
  4907. void *arg)
  4908. {
  4909. s->allow_early_data_cb = cb;
  4910. s->allow_early_data_cb_data = arg;
  4911. }