ssl_lib.c 158 KB

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