1
0

ssl_lib.c 154 KB

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