1
0

ssl_lib.c 157 KB

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