ssl_lib.c 154 KB

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