tls_openssl.c 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284
  1. /*
  2. * SSL/TLS interface functions for OpenSSL
  3. * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #ifndef CONFIG_SMARTCARD
  10. #ifndef OPENSSL_NO_ENGINE
  11. #ifndef ANDROID
  12. #define OPENSSL_NO_ENGINE
  13. #endif
  14. #endif
  15. #endif
  16. #include <openssl/ssl.h>
  17. #include <openssl/err.h>
  18. #include <openssl/pkcs12.h>
  19. #include <openssl/x509v3.h>
  20. #ifndef OPENSSL_NO_ENGINE
  21. #include <openssl/engine.h>
  22. #endif /* OPENSSL_NO_ENGINE */
  23. #ifndef OPENSSL_NO_DSA
  24. #include <openssl/dsa.h>
  25. #endif
  26. #ifndef OPENSSL_NO_DH
  27. #include <openssl/dh.h>
  28. #endif
  29. #include "common.h"
  30. #include "crypto.h"
  31. #include "sha1.h"
  32. #include "sha256.h"
  33. #include "tls.h"
  34. #include "tls_openssl.h"
  35. #if OPENSSL_VERSION_NUMBER < 0x10000000L
  36. /* ERR_remove_thread_state replaces ERR_remove_state and the latter is
  37. * deprecated. However, OpenSSL 0.9.8 doesn't include
  38. * ERR_remove_thread_state. */
  39. #define ERR_remove_thread_state(tid) ERR_remove_state(0)
  40. #endif
  41. #if defined(OPENSSL_IS_BORINGSSL)
  42. /* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */
  43. typedef size_t stack_index_t;
  44. #else
  45. typedef int stack_index_t;
  46. #endif
  47. #ifdef SSL_set_tlsext_status_type
  48. #ifndef OPENSSL_NO_TLSEXT
  49. #define HAVE_OCSP
  50. #include <openssl/ocsp.h>
  51. #endif /* OPENSSL_NO_TLSEXT */
  52. #endif /* SSL_set_tlsext_status_type */
  53. #ifdef ANDROID
  54. #include <openssl/pem.h>
  55. #include <keystore/keystore_get.h>
  56. static BIO * BIO_from_keystore(const char *key)
  57. {
  58. BIO *bio = NULL;
  59. uint8_t *value = NULL;
  60. int length = keystore_get(key, strlen(key), &value);
  61. if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL)
  62. BIO_write(bio, value, length);
  63. free(value);
  64. return bio;
  65. }
  66. #endif /* ANDROID */
  67. static int tls_openssl_ref_count = 0;
  68. static int tls_ex_idx_session = -1;
  69. struct tls_context {
  70. void (*event_cb)(void *ctx, enum tls_event ev,
  71. union tls_event_data *data);
  72. void *cb_ctx;
  73. int cert_in_cb;
  74. char *ocsp_stapling_response;
  75. };
  76. static struct tls_context *tls_global = NULL;
  77. struct tls_data {
  78. SSL_CTX *ssl;
  79. unsigned int tls_session_lifetime;
  80. };
  81. struct tls_connection {
  82. struct tls_context *context;
  83. SSL_CTX *ssl_ctx;
  84. SSL *ssl;
  85. BIO *ssl_in, *ssl_out;
  86. #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
  87. ENGINE *engine; /* functional reference to the engine */
  88. EVP_PKEY *private_key; /* the private key if using engine */
  89. #endif /* OPENSSL_NO_ENGINE */
  90. char *subject_match, *altsubject_match, *suffix_match, *domain_match;
  91. int read_alerts, write_alerts, failed;
  92. tls_session_ticket_cb session_ticket_cb;
  93. void *session_ticket_cb_ctx;
  94. /* SessionTicket received from OpenSSL hello_extension_cb (server) */
  95. u8 *session_ticket;
  96. size_t session_ticket_len;
  97. unsigned int ca_cert_verify:1;
  98. unsigned int cert_probe:1;
  99. unsigned int server_cert_only:1;
  100. unsigned int invalid_hb_used:1;
  101. unsigned int success_data:1;
  102. u8 srv_cert_hash[32];
  103. unsigned int flags;
  104. X509 *peer_cert;
  105. X509 *peer_issuer;
  106. X509 *peer_issuer_issuer;
  107. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  108. unsigned char client_random[SSL3_RANDOM_SIZE];
  109. unsigned char server_random[SSL3_RANDOM_SIZE];
  110. #endif
  111. };
  112. static struct tls_context * tls_context_new(const struct tls_config *conf)
  113. {
  114. struct tls_context *context = os_zalloc(sizeof(*context));
  115. if (context == NULL)
  116. return NULL;
  117. if (conf) {
  118. context->event_cb = conf->event_cb;
  119. context->cb_ctx = conf->cb_ctx;
  120. context->cert_in_cb = conf->cert_in_cb;
  121. }
  122. return context;
  123. }
  124. #ifdef CONFIG_NO_STDOUT_DEBUG
  125. static void _tls_show_errors(void)
  126. {
  127. unsigned long err;
  128. while ((err = ERR_get_error())) {
  129. /* Just ignore the errors, since stdout is disabled */
  130. }
  131. }
  132. #define tls_show_errors(l, f, t) _tls_show_errors()
  133. #else /* CONFIG_NO_STDOUT_DEBUG */
  134. static void tls_show_errors(int level, const char *func, const char *txt)
  135. {
  136. unsigned long err;
  137. wpa_printf(level, "OpenSSL: %s - %s %s",
  138. func, txt, ERR_error_string(ERR_get_error(), NULL));
  139. while ((err = ERR_get_error())) {
  140. wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
  141. ERR_error_string(err, NULL));
  142. }
  143. }
  144. #endif /* CONFIG_NO_STDOUT_DEBUG */
  145. #ifdef CONFIG_NATIVE_WINDOWS
  146. /* Windows CryptoAPI and access to certificate stores */
  147. #include <wincrypt.h>
  148. #ifdef __MINGW32_VERSION
  149. /*
  150. * MinGW does not yet include all the needed definitions for CryptoAPI, so
  151. * define here whatever extra is needed.
  152. */
  153. #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
  154. #define CERT_STORE_READONLY_FLAG 0x00008000
  155. #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
  156. #endif /* __MINGW32_VERSION */
  157. struct cryptoapi_rsa_data {
  158. const CERT_CONTEXT *cert;
  159. HCRYPTPROV crypt_prov;
  160. DWORD key_spec;
  161. BOOL free_crypt_prov;
  162. };
  163. static void cryptoapi_error(const char *msg)
  164. {
  165. wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
  166. msg, (unsigned int) GetLastError());
  167. }
  168. static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from,
  169. unsigned char *to, RSA *rsa, int padding)
  170. {
  171. wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
  172. return 0;
  173. }
  174. static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from,
  175. unsigned char *to, RSA *rsa, int padding)
  176. {
  177. wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
  178. return 0;
  179. }
  180. static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
  181. unsigned char *to, RSA *rsa, int padding)
  182. {
  183. struct cryptoapi_rsa_data *priv =
  184. (struct cryptoapi_rsa_data *) rsa->meth->app_data;
  185. HCRYPTHASH hash;
  186. DWORD hash_size, len, i;
  187. unsigned char *buf = NULL;
  188. int ret = 0;
  189. if (priv == NULL) {
  190. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
  191. ERR_R_PASSED_NULL_PARAMETER);
  192. return 0;
  193. }
  194. if (padding != RSA_PKCS1_PADDING) {
  195. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
  196. RSA_R_UNKNOWN_PADDING_TYPE);
  197. return 0;
  198. }
  199. if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
  200. wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
  201. __func__);
  202. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
  203. RSA_R_INVALID_MESSAGE_LENGTH);
  204. return 0;
  205. }
  206. if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
  207. {
  208. cryptoapi_error("CryptCreateHash failed");
  209. return 0;
  210. }
  211. len = sizeof(hash_size);
  212. if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
  213. 0)) {
  214. cryptoapi_error("CryptGetHashParam failed");
  215. goto err;
  216. }
  217. if ((int) hash_size != flen) {
  218. wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
  219. (unsigned) hash_size, flen);
  220. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
  221. RSA_R_INVALID_MESSAGE_LENGTH);
  222. goto err;
  223. }
  224. if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
  225. cryptoapi_error("CryptSetHashParam failed");
  226. goto err;
  227. }
  228. len = RSA_size(rsa);
  229. buf = os_malloc(len);
  230. if (buf == NULL) {
  231. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
  232. goto err;
  233. }
  234. if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
  235. cryptoapi_error("CryptSignHash failed");
  236. goto err;
  237. }
  238. for (i = 0; i < len; i++)
  239. to[i] = buf[len - i - 1];
  240. ret = len;
  241. err:
  242. os_free(buf);
  243. CryptDestroyHash(hash);
  244. return ret;
  245. }
  246. static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
  247. unsigned char *to, RSA *rsa, int padding)
  248. {
  249. wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
  250. return 0;
  251. }
  252. static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
  253. {
  254. if (priv == NULL)
  255. return;
  256. if (priv->crypt_prov && priv->free_crypt_prov)
  257. CryptReleaseContext(priv->crypt_prov, 0);
  258. if (priv->cert)
  259. CertFreeCertificateContext(priv->cert);
  260. os_free(priv);
  261. }
  262. static int cryptoapi_finish(RSA *rsa)
  263. {
  264. cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
  265. os_free((void *) rsa->meth);
  266. rsa->meth = NULL;
  267. return 1;
  268. }
  269. static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
  270. {
  271. HCERTSTORE cs;
  272. const CERT_CONTEXT *ret = NULL;
  273. cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
  274. store | CERT_STORE_OPEN_EXISTING_FLAG |
  275. CERT_STORE_READONLY_FLAG, L"MY");
  276. if (cs == NULL) {
  277. cryptoapi_error("Failed to open 'My system store'");
  278. return NULL;
  279. }
  280. if (strncmp(name, "cert://", 7) == 0) {
  281. unsigned short wbuf[255];
  282. MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
  283. ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
  284. PKCS_7_ASN_ENCODING,
  285. 0, CERT_FIND_SUBJECT_STR,
  286. wbuf, NULL);
  287. } else if (strncmp(name, "hash://", 7) == 0) {
  288. CRYPT_HASH_BLOB blob;
  289. int len;
  290. const char *hash = name + 7;
  291. unsigned char *buf;
  292. len = os_strlen(hash) / 2;
  293. buf = os_malloc(len);
  294. if (buf && hexstr2bin(hash, buf, len) == 0) {
  295. blob.cbData = len;
  296. blob.pbData = buf;
  297. ret = CertFindCertificateInStore(cs,
  298. X509_ASN_ENCODING |
  299. PKCS_7_ASN_ENCODING,
  300. 0, CERT_FIND_HASH,
  301. &blob, NULL);
  302. }
  303. os_free(buf);
  304. }
  305. CertCloseStore(cs, 0);
  306. return ret;
  307. }
  308. static int tls_cryptoapi_cert(SSL *ssl, const char *name)
  309. {
  310. X509 *cert = NULL;
  311. RSA *rsa = NULL, *pub_rsa;
  312. struct cryptoapi_rsa_data *priv;
  313. RSA_METHOD *rsa_meth;
  314. if (name == NULL ||
  315. (strncmp(name, "cert://", 7) != 0 &&
  316. strncmp(name, "hash://", 7) != 0))
  317. return -1;
  318. priv = os_zalloc(sizeof(*priv));
  319. rsa_meth = os_zalloc(sizeof(*rsa_meth));
  320. if (priv == NULL || rsa_meth == NULL) {
  321. wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
  322. "for CryptoAPI RSA method");
  323. os_free(priv);
  324. os_free(rsa_meth);
  325. return -1;
  326. }
  327. priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
  328. if (priv->cert == NULL) {
  329. priv->cert = cryptoapi_find_cert(
  330. name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
  331. }
  332. if (priv->cert == NULL) {
  333. wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
  334. "'%s'", name);
  335. goto err;
  336. }
  337. cert = d2i_X509(NULL,
  338. (const unsigned char **) &priv->cert->pbCertEncoded,
  339. priv->cert->cbCertEncoded);
  340. if (cert == NULL) {
  341. wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
  342. "encoding");
  343. goto err;
  344. }
  345. if (!CryptAcquireCertificatePrivateKey(priv->cert,
  346. CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
  347. NULL, &priv->crypt_prov,
  348. &priv->key_spec,
  349. &priv->free_crypt_prov)) {
  350. cryptoapi_error("Failed to acquire a private key for the "
  351. "certificate");
  352. goto err;
  353. }
  354. rsa_meth->name = "Microsoft CryptoAPI RSA Method";
  355. rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
  356. rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
  357. rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
  358. rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
  359. rsa_meth->finish = cryptoapi_finish;
  360. rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
  361. rsa_meth->app_data = (char *) priv;
  362. rsa = RSA_new();
  363. if (rsa == NULL) {
  364. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
  365. ERR_R_MALLOC_FAILURE);
  366. goto err;
  367. }
  368. if (!SSL_use_certificate(ssl, cert)) {
  369. RSA_free(rsa);
  370. rsa = NULL;
  371. goto err;
  372. }
  373. pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
  374. X509_free(cert);
  375. cert = NULL;
  376. rsa->n = BN_dup(pub_rsa->n);
  377. rsa->e = BN_dup(pub_rsa->e);
  378. if (!RSA_set_method(rsa, rsa_meth))
  379. goto err;
  380. if (!SSL_use_RSAPrivateKey(ssl, rsa))
  381. goto err;
  382. RSA_free(rsa);
  383. return 0;
  384. err:
  385. if (cert)
  386. X509_free(cert);
  387. if (rsa)
  388. RSA_free(rsa);
  389. else {
  390. os_free(rsa_meth);
  391. cryptoapi_free_data(priv);
  392. }
  393. return -1;
  394. }
  395. static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
  396. {
  397. HCERTSTORE cs;
  398. PCCERT_CONTEXT ctx = NULL;
  399. X509 *cert;
  400. char buf[128];
  401. const char *store;
  402. #ifdef UNICODE
  403. WCHAR *wstore;
  404. #endif /* UNICODE */
  405. if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
  406. return -1;
  407. store = name + 13;
  408. #ifdef UNICODE
  409. wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
  410. if (wstore == NULL)
  411. return -1;
  412. wsprintf(wstore, L"%S", store);
  413. cs = CertOpenSystemStore(0, wstore);
  414. os_free(wstore);
  415. #else /* UNICODE */
  416. cs = CertOpenSystemStore(0, store);
  417. #endif /* UNICODE */
  418. if (cs == NULL) {
  419. wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
  420. "'%s': error=%d", __func__, store,
  421. (int) GetLastError());
  422. return -1;
  423. }
  424. while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
  425. cert = d2i_X509(NULL,
  426. (const unsigned char **) &ctx->pbCertEncoded,
  427. ctx->cbCertEncoded);
  428. if (cert == NULL) {
  429. wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
  430. "X509 DER encoding for CA cert");
  431. continue;
  432. }
  433. X509_NAME_oneline(X509_get_subject_name(cert), buf,
  434. sizeof(buf));
  435. wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
  436. "system certificate store: subject='%s'", buf);
  437. if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
  438. tls_show_errors(MSG_WARNING, __func__,
  439. "Failed to add ca_cert to OpenSSL "
  440. "certificate store");
  441. }
  442. X509_free(cert);
  443. }
  444. if (!CertCloseStore(cs, 0)) {
  445. wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
  446. "'%s': error=%d", __func__, name + 13,
  447. (int) GetLastError());
  448. }
  449. return 0;
  450. }
  451. #else /* CONFIG_NATIVE_WINDOWS */
  452. static int tls_cryptoapi_cert(SSL *ssl, const char *name)
  453. {
  454. return -1;
  455. }
  456. #endif /* CONFIG_NATIVE_WINDOWS */
  457. static void ssl_info_cb(const SSL *ssl, int where, int ret)
  458. {
  459. const char *str;
  460. int w;
  461. wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
  462. w = where & ~SSL_ST_MASK;
  463. if (w & SSL_ST_CONNECT)
  464. str = "SSL_connect";
  465. else if (w & SSL_ST_ACCEPT)
  466. str = "SSL_accept";
  467. else
  468. str = "undefined";
  469. if (where & SSL_CB_LOOP) {
  470. wpa_printf(MSG_DEBUG, "SSL: %s:%s",
  471. str, SSL_state_string_long(ssl));
  472. } else if (where & SSL_CB_ALERT) {
  473. struct tls_connection *conn = SSL_get_app_data((SSL *) ssl);
  474. wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
  475. where & SSL_CB_READ ?
  476. "read (remote end reported an error)" :
  477. "write (local SSL3 detected an error)",
  478. SSL_alert_type_string_long(ret),
  479. SSL_alert_desc_string_long(ret));
  480. if ((ret >> 8) == SSL3_AL_FATAL) {
  481. if (where & SSL_CB_READ)
  482. conn->read_alerts++;
  483. else
  484. conn->write_alerts++;
  485. }
  486. if (conn->context->event_cb != NULL) {
  487. union tls_event_data ev;
  488. struct tls_context *context = conn->context;
  489. os_memset(&ev, 0, sizeof(ev));
  490. ev.alert.is_local = !(where & SSL_CB_READ);
  491. ev.alert.type = SSL_alert_type_string_long(ret);
  492. ev.alert.description = SSL_alert_desc_string_long(ret);
  493. context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
  494. }
  495. } else if (where & SSL_CB_EXIT && ret <= 0) {
  496. wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
  497. str, ret == 0 ? "failed" : "error",
  498. SSL_state_string_long(ssl));
  499. }
  500. }
  501. #ifndef OPENSSL_NO_ENGINE
  502. /**
  503. * tls_engine_load_dynamic_generic - load any openssl engine
  504. * @pre: an array of commands and values that load an engine initialized
  505. * in the engine specific function
  506. * @post: an array of commands and values that initialize an already loaded
  507. * engine (or %NULL if not required)
  508. * @id: the engine id of the engine to load (only required if post is not %NULL
  509. *
  510. * This function is a generic function that loads any openssl engine.
  511. *
  512. * Returns: 0 on success, -1 on failure
  513. */
  514. static int tls_engine_load_dynamic_generic(const char *pre[],
  515. const char *post[], const char *id)
  516. {
  517. ENGINE *engine;
  518. const char *dynamic_id = "dynamic";
  519. engine = ENGINE_by_id(id);
  520. if (engine) {
  521. ENGINE_free(engine);
  522. wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
  523. "available", id);
  524. return 0;
  525. }
  526. ERR_clear_error();
  527. engine = ENGINE_by_id(dynamic_id);
  528. if (engine == NULL) {
  529. wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
  530. dynamic_id,
  531. ERR_error_string(ERR_get_error(), NULL));
  532. return -1;
  533. }
  534. /* Perform the pre commands. This will load the engine. */
  535. while (pre && pre[0]) {
  536. wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
  537. if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
  538. wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
  539. "%s %s [%s]", pre[0], pre[1],
  540. ERR_error_string(ERR_get_error(), NULL));
  541. ENGINE_free(engine);
  542. return -1;
  543. }
  544. pre += 2;
  545. }
  546. /*
  547. * Free the reference to the "dynamic" engine. The loaded engine can
  548. * now be looked up using ENGINE_by_id().
  549. */
  550. ENGINE_free(engine);
  551. engine = ENGINE_by_id(id);
  552. if (engine == NULL) {
  553. wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
  554. id, ERR_error_string(ERR_get_error(), NULL));
  555. return -1;
  556. }
  557. while (post && post[0]) {
  558. wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
  559. if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
  560. wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
  561. " %s %s [%s]", post[0], post[1],
  562. ERR_error_string(ERR_get_error(), NULL));
  563. ENGINE_remove(engine);
  564. ENGINE_free(engine);
  565. return -1;
  566. }
  567. post += 2;
  568. }
  569. ENGINE_free(engine);
  570. return 0;
  571. }
  572. /**
  573. * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
  574. * @pkcs11_so_path: pksc11_so_path from the configuration
  575. * @pcks11_module_path: pkcs11_module_path from the configuration
  576. */
  577. static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
  578. const char *pkcs11_module_path)
  579. {
  580. char *engine_id = "pkcs11";
  581. const char *pre_cmd[] = {
  582. "SO_PATH", NULL /* pkcs11_so_path */,
  583. "ID", NULL /* engine_id */,
  584. "LIST_ADD", "1",
  585. /* "NO_VCHECK", "1", */
  586. "LOAD", NULL,
  587. NULL, NULL
  588. };
  589. const char *post_cmd[] = {
  590. "MODULE_PATH", NULL /* pkcs11_module_path */,
  591. NULL, NULL
  592. };
  593. if (!pkcs11_so_path)
  594. return 0;
  595. pre_cmd[1] = pkcs11_so_path;
  596. pre_cmd[3] = engine_id;
  597. if (pkcs11_module_path)
  598. post_cmd[1] = pkcs11_module_path;
  599. else
  600. post_cmd[0] = NULL;
  601. wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
  602. pkcs11_so_path);
  603. return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
  604. }
  605. /**
  606. * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
  607. * @opensc_so_path: opensc_so_path from the configuration
  608. */
  609. static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
  610. {
  611. char *engine_id = "opensc";
  612. const char *pre_cmd[] = {
  613. "SO_PATH", NULL /* opensc_so_path */,
  614. "ID", NULL /* engine_id */,
  615. "LIST_ADD", "1",
  616. "LOAD", NULL,
  617. NULL, NULL
  618. };
  619. if (!opensc_so_path)
  620. return 0;
  621. pre_cmd[1] = opensc_so_path;
  622. pre_cmd[3] = engine_id;
  623. wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
  624. opensc_so_path);
  625. return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
  626. }
  627. #endif /* OPENSSL_NO_ENGINE */
  628. static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
  629. {
  630. struct wpabuf *buf;
  631. if (tls_ex_idx_session < 0)
  632. return;
  633. buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
  634. if (!buf)
  635. return;
  636. wpa_printf(MSG_DEBUG,
  637. "OpenSSL: Free application session data %p (sess %p)",
  638. buf, sess);
  639. wpabuf_free(buf);
  640. SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL);
  641. }
  642. void * tls_init(const struct tls_config *conf)
  643. {
  644. struct tls_data *data;
  645. SSL_CTX *ssl;
  646. struct tls_context *context;
  647. const char *ciphers;
  648. if (tls_openssl_ref_count == 0) {
  649. tls_global = context = tls_context_new(conf);
  650. if (context == NULL)
  651. return NULL;
  652. #ifdef CONFIG_FIPS
  653. #ifdef OPENSSL_FIPS
  654. if (conf && conf->fips_mode) {
  655. static int fips_enabled = 0;
  656. if (!fips_enabled && !FIPS_mode_set(1)) {
  657. wpa_printf(MSG_ERROR, "Failed to enable FIPS "
  658. "mode");
  659. ERR_load_crypto_strings();
  660. ERR_print_errors_fp(stderr);
  661. os_free(tls_global);
  662. tls_global = NULL;
  663. return NULL;
  664. } else {
  665. wpa_printf(MSG_INFO, "Running in FIPS mode");
  666. fips_enabled = 1;
  667. }
  668. }
  669. #else /* OPENSSL_FIPS */
  670. if (conf && conf->fips_mode) {
  671. wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
  672. "supported");
  673. os_free(tls_global);
  674. tls_global = NULL;
  675. return NULL;
  676. }
  677. #endif /* OPENSSL_FIPS */
  678. #endif /* CONFIG_FIPS */
  679. SSL_load_error_strings();
  680. SSL_library_init();
  681. #ifndef OPENSSL_NO_SHA256
  682. EVP_add_digest(EVP_sha256());
  683. #endif /* OPENSSL_NO_SHA256 */
  684. /* TODO: if /dev/urandom is available, PRNG is seeded
  685. * automatically. If this is not the case, random data should
  686. * be added here. */
  687. #ifdef PKCS12_FUNCS
  688. #ifndef OPENSSL_NO_RC2
  689. /*
  690. * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
  691. * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
  692. * versions, but it looks like OpenSSL 1.0.0 does not do that
  693. * anymore.
  694. */
  695. EVP_add_cipher(EVP_rc2_40_cbc());
  696. #endif /* OPENSSL_NO_RC2 */
  697. PKCS12_PBE_add();
  698. #endif /* PKCS12_FUNCS */
  699. } else {
  700. context = tls_context_new(conf);
  701. if (context == NULL)
  702. return NULL;
  703. }
  704. tls_openssl_ref_count++;
  705. data = os_zalloc(sizeof(*data));
  706. if (data)
  707. ssl = SSL_CTX_new(SSLv23_method());
  708. else
  709. ssl = NULL;
  710. if (ssl == NULL) {
  711. tls_openssl_ref_count--;
  712. if (context != tls_global)
  713. os_free(context);
  714. if (tls_openssl_ref_count == 0) {
  715. os_free(tls_global);
  716. tls_global = NULL;
  717. }
  718. return NULL;
  719. }
  720. data->ssl = ssl;
  721. if (conf)
  722. data->tls_session_lifetime = conf->tls_session_lifetime;
  723. SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2);
  724. SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3);
  725. SSL_CTX_set_info_callback(ssl, ssl_info_cb);
  726. SSL_CTX_set_app_data(ssl, context);
  727. if (data->tls_session_lifetime > 0) {
  728. SSL_CTX_set_quiet_shutdown(ssl, 1);
  729. /*
  730. * Set default context here. In practice, this will be replaced
  731. * by the per-EAP method context in tls_connection_set_verify().
  732. */
  733. SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7);
  734. SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER);
  735. SSL_CTX_set_timeout(ssl, data->tls_session_lifetime);
  736. SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb);
  737. } else {
  738. SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF);
  739. }
  740. if (tls_ex_idx_session < 0) {
  741. tls_ex_idx_session = SSL_SESSION_get_ex_new_index(
  742. 0, NULL, NULL, NULL, NULL);
  743. if (tls_ex_idx_session < 0) {
  744. tls_deinit(data);
  745. return NULL;
  746. }
  747. }
  748. #ifndef OPENSSL_NO_ENGINE
  749. wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine");
  750. ERR_load_ENGINE_strings();
  751. ENGINE_load_dynamic();
  752. if (conf &&
  753. (conf->opensc_engine_path || conf->pkcs11_engine_path ||
  754. conf->pkcs11_module_path)) {
  755. if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
  756. tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
  757. conf->pkcs11_module_path)) {
  758. tls_deinit(data);
  759. return NULL;
  760. }
  761. }
  762. #endif /* OPENSSL_NO_ENGINE */
  763. if (conf && conf->openssl_ciphers)
  764. ciphers = conf->openssl_ciphers;
  765. else
  766. ciphers = "DEFAULT:!EXP:!LOW";
  767. if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) {
  768. wpa_printf(MSG_ERROR,
  769. "OpenSSL: Failed to set cipher string '%s'",
  770. ciphers);
  771. tls_deinit(data);
  772. return NULL;
  773. }
  774. return data;
  775. }
  776. void tls_deinit(void *ssl_ctx)
  777. {
  778. struct tls_data *data = ssl_ctx;
  779. SSL_CTX *ssl = data->ssl;
  780. struct tls_context *context = SSL_CTX_get_app_data(ssl);
  781. if (context != tls_global)
  782. os_free(context);
  783. if (data->tls_session_lifetime > 0)
  784. SSL_CTX_flush_sessions(ssl, 0);
  785. SSL_CTX_free(ssl);
  786. tls_openssl_ref_count--;
  787. if (tls_openssl_ref_count == 0) {
  788. #ifndef OPENSSL_NO_ENGINE
  789. ENGINE_cleanup();
  790. #endif /* OPENSSL_NO_ENGINE */
  791. CRYPTO_cleanup_all_ex_data();
  792. ERR_remove_thread_state(NULL);
  793. ERR_free_strings();
  794. EVP_cleanup();
  795. os_free(tls_global->ocsp_stapling_response);
  796. tls_global->ocsp_stapling_response = NULL;
  797. os_free(tls_global);
  798. tls_global = NULL;
  799. }
  800. os_free(data);
  801. }
  802. #ifndef OPENSSL_NO_ENGINE
  803. /* Cryptoki return values */
  804. #define CKR_PIN_INCORRECT 0x000000a0
  805. #define CKR_PIN_INVALID 0x000000a1
  806. #define CKR_PIN_LEN_RANGE 0x000000a2
  807. /* libp11 */
  808. #define ERR_LIB_PKCS11 ERR_LIB_USER
  809. static int tls_is_pin_error(unsigned int err)
  810. {
  811. return ERR_GET_LIB(err) == ERR_LIB_PKCS11 &&
  812. (ERR_GET_REASON(err) == CKR_PIN_INCORRECT ||
  813. ERR_GET_REASON(err) == CKR_PIN_INVALID ||
  814. ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE);
  815. }
  816. #endif /* OPENSSL_NO_ENGINE */
  817. #ifdef ANDROID
  818. /* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
  819. EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id);
  820. #endif /* ANDROID */
  821. static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
  822. const char *pin, const char *key_id,
  823. const char *cert_id, const char *ca_cert_id)
  824. {
  825. #if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
  826. #if !defined(OPENSSL_NO_ENGINE)
  827. #error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
  828. #endif
  829. if (!key_id)
  830. return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
  831. conn->engine = NULL;
  832. conn->private_key = EVP_PKEY_from_keystore(key_id);
  833. if (!conn->private_key) {
  834. wpa_printf(MSG_ERROR,
  835. "ENGINE: cannot load private key with id '%s' [%s]",
  836. key_id,
  837. ERR_error_string(ERR_get_error(), NULL));
  838. return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
  839. }
  840. #endif /* ANDROID && OPENSSL_IS_BORINGSSL */
  841. #ifndef OPENSSL_NO_ENGINE
  842. int ret = -1;
  843. if (engine_id == NULL) {
  844. wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
  845. return -1;
  846. }
  847. ERR_clear_error();
  848. #ifdef ANDROID
  849. ENGINE_load_dynamic();
  850. #endif
  851. conn->engine = ENGINE_by_id(engine_id);
  852. if (!conn->engine) {
  853. wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
  854. engine_id, ERR_error_string(ERR_get_error(), NULL));
  855. goto err;
  856. }
  857. if (ENGINE_init(conn->engine) != 1) {
  858. wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
  859. "(engine: %s) [%s]", engine_id,
  860. ERR_error_string(ERR_get_error(), NULL));
  861. goto err;
  862. }
  863. wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
  864. #ifndef ANDROID
  865. if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
  866. wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
  867. ERR_error_string(ERR_get_error(), NULL));
  868. goto err;
  869. }
  870. #endif
  871. if (key_id) {
  872. /*
  873. * Ensure that the ENGINE does not attempt to use the OpenSSL
  874. * UI system to obtain a PIN, if we didn't provide one.
  875. */
  876. struct {
  877. const void *password;
  878. const char *prompt_info;
  879. } key_cb = { "", NULL };
  880. /* load private key first in-case PIN is required for cert */
  881. conn->private_key = ENGINE_load_private_key(conn->engine,
  882. key_id, NULL,
  883. &key_cb);
  884. if (!conn->private_key) {
  885. unsigned long err = ERR_get_error();
  886. wpa_printf(MSG_ERROR,
  887. "ENGINE: cannot load private key with id '%s' [%s]",
  888. key_id,
  889. ERR_error_string(err, NULL));
  890. if (tls_is_pin_error(err))
  891. ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
  892. else
  893. ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
  894. goto err;
  895. }
  896. }
  897. /* handle a certificate and/or CA certificate */
  898. if (cert_id || ca_cert_id) {
  899. const char *cmd_name = "LOAD_CERT_CTRL";
  900. /* test if the engine supports a LOAD_CERT_CTRL */
  901. if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
  902. 0, (void *)cmd_name, NULL)) {
  903. wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
  904. " loading certificates");
  905. ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
  906. goto err;
  907. }
  908. }
  909. return 0;
  910. err:
  911. if (conn->engine) {
  912. ENGINE_free(conn->engine);
  913. conn->engine = NULL;
  914. }
  915. if (conn->private_key) {
  916. EVP_PKEY_free(conn->private_key);
  917. conn->private_key = NULL;
  918. }
  919. return ret;
  920. #else /* OPENSSL_NO_ENGINE */
  921. return 0;
  922. #endif /* OPENSSL_NO_ENGINE */
  923. }
  924. static void tls_engine_deinit(struct tls_connection *conn)
  925. {
  926. #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
  927. wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
  928. if (conn->private_key) {
  929. EVP_PKEY_free(conn->private_key);
  930. conn->private_key = NULL;
  931. }
  932. if (conn->engine) {
  933. #if !defined(OPENSSL_IS_BORINGSSL)
  934. ENGINE_finish(conn->engine);
  935. #endif /* !OPENSSL_IS_BORINGSSL */
  936. conn->engine = NULL;
  937. }
  938. #endif /* ANDROID || !OPENSSL_NO_ENGINE */
  939. }
  940. int tls_get_errors(void *ssl_ctx)
  941. {
  942. int count = 0;
  943. unsigned long err;
  944. while ((err = ERR_get_error())) {
  945. wpa_printf(MSG_INFO, "TLS - SSL error: %s",
  946. ERR_error_string(err, NULL));
  947. count++;
  948. }
  949. return count;
  950. }
  951. static const char * openssl_content_type(int content_type)
  952. {
  953. switch (content_type) {
  954. case 20:
  955. return "change cipher spec";
  956. case 21:
  957. return "alert";
  958. case 22:
  959. return "handshake";
  960. case 23:
  961. return "application data";
  962. case 24:
  963. return "heartbeat";
  964. case 256:
  965. return "TLS header info"; /* pseudo content type */
  966. default:
  967. return "?";
  968. }
  969. }
  970. static const char * openssl_handshake_type(int content_type, const u8 *buf,
  971. size_t len)
  972. {
  973. if (content_type != 22 || !buf || len == 0)
  974. return "";
  975. switch (buf[0]) {
  976. case 0:
  977. return "hello request";
  978. case 1:
  979. return "client hello";
  980. case 2:
  981. return "server hello";
  982. case 4:
  983. return "new session ticket";
  984. case 11:
  985. return "certificate";
  986. case 12:
  987. return "server key exchange";
  988. case 13:
  989. return "certificate request";
  990. case 14:
  991. return "server hello done";
  992. case 15:
  993. return "certificate verify";
  994. case 16:
  995. return "client key exchange";
  996. case 20:
  997. return "finished";
  998. case 21:
  999. return "certificate url";
  1000. case 22:
  1001. return "certificate status";
  1002. default:
  1003. return "?";
  1004. }
  1005. }
  1006. static void tls_msg_cb(int write_p, int version, int content_type,
  1007. const void *buf, size_t len, SSL *ssl, void *arg)
  1008. {
  1009. struct tls_connection *conn = arg;
  1010. const u8 *pos = buf;
  1011. if (write_p == 2) {
  1012. wpa_printf(MSG_DEBUG,
  1013. "OpenSSL: session ver=0x%x content_type=%d",
  1014. version, content_type);
  1015. wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len);
  1016. return;
  1017. }
  1018. wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
  1019. write_p ? "TX" : "RX", version, content_type,
  1020. openssl_content_type(content_type),
  1021. openssl_handshake_type(content_type, buf, len));
  1022. wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len);
  1023. if (content_type == 24 && len >= 3 && pos[0] == 1) {
  1024. size_t payload_len = WPA_GET_BE16(pos + 1);
  1025. if (payload_len + 3 > len) {
  1026. wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected");
  1027. conn->invalid_hb_used = 1;
  1028. }
  1029. }
  1030. }
  1031. struct tls_connection * tls_connection_init(void *ssl_ctx)
  1032. {
  1033. struct tls_data *data = ssl_ctx;
  1034. SSL_CTX *ssl = data->ssl;
  1035. struct tls_connection *conn;
  1036. long options;
  1037. struct tls_context *context = SSL_CTX_get_app_data(ssl);
  1038. conn = os_zalloc(sizeof(*conn));
  1039. if (conn == NULL)
  1040. return NULL;
  1041. conn->ssl_ctx = ssl;
  1042. conn->ssl = SSL_new(ssl);
  1043. if (conn->ssl == NULL) {
  1044. tls_show_errors(MSG_INFO, __func__,
  1045. "Failed to initialize new SSL connection");
  1046. os_free(conn);
  1047. return NULL;
  1048. }
  1049. conn->context = context;
  1050. SSL_set_app_data(conn->ssl, conn);
  1051. SSL_set_msg_callback(conn->ssl, tls_msg_cb);
  1052. SSL_set_msg_callback_arg(conn->ssl, conn);
  1053. options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
  1054. SSL_OP_SINGLE_DH_USE;
  1055. #ifdef SSL_OP_NO_COMPRESSION
  1056. options |= SSL_OP_NO_COMPRESSION;
  1057. #endif /* SSL_OP_NO_COMPRESSION */
  1058. SSL_set_options(conn->ssl, options);
  1059. conn->ssl_in = BIO_new(BIO_s_mem());
  1060. if (!conn->ssl_in) {
  1061. tls_show_errors(MSG_INFO, __func__,
  1062. "Failed to create a new BIO for ssl_in");
  1063. SSL_free(conn->ssl);
  1064. os_free(conn);
  1065. return NULL;
  1066. }
  1067. conn->ssl_out = BIO_new(BIO_s_mem());
  1068. if (!conn->ssl_out) {
  1069. tls_show_errors(MSG_INFO, __func__,
  1070. "Failed to create a new BIO for ssl_out");
  1071. SSL_free(conn->ssl);
  1072. BIO_free(conn->ssl_in);
  1073. os_free(conn);
  1074. return NULL;
  1075. }
  1076. SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
  1077. return conn;
  1078. }
  1079. void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
  1080. {
  1081. if (conn == NULL)
  1082. return;
  1083. if (conn->success_data) {
  1084. /*
  1085. * Make sure ssl_clear_bad_session() does not remove this
  1086. * session.
  1087. */
  1088. SSL_set_quiet_shutdown(conn->ssl, 1);
  1089. SSL_shutdown(conn->ssl);
  1090. }
  1091. SSL_free(conn->ssl);
  1092. tls_engine_deinit(conn);
  1093. os_free(conn->subject_match);
  1094. os_free(conn->altsubject_match);
  1095. os_free(conn->suffix_match);
  1096. os_free(conn->domain_match);
  1097. os_free(conn->session_ticket);
  1098. os_free(conn);
  1099. }
  1100. int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
  1101. {
  1102. return conn ? SSL_is_init_finished(conn->ssl) : 0;
  1103. }
  1104. int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
  1105. {
  1106. if (conn == NULL)
  1107. return -1;
  1108. /* Shutdown previous TLS connection without notifying the peer
  1109. * because the connection was already terminated in practice
  1110. * and "close notify" shutdown alert would confuse AS. */
  1111. SSL_set_quiet_shutdown(conn->ssl, 1);
  1112. SSL_shutdown(conn->ssl);
  1113. return SSL_clear(conn->ssl) == 1 ? 0 : -1;
  1114. }
  1115. static int tls_match_altsubject_component(X509 *cert, int type,
  1116. const char *value, size_t len)
  1117. {
  1118. GENERAL_NAME *gen;
  1119. void *ext;
  1120. int found = 0;
  1121. stack_index_t i;
  1122. ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
  1123. for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
  1124. gen = sk_GENERAL_NAME_value(ext, i);
  1125. if (gen->type != type)
  1126. continue;
  1127. if (os_strlen((char *) gen->d.ia5->data) == len &&
  1128. os_memcmp(value, gen->d.ia5->data, len) == 0)
  1129. found++;
  1130. }
  1131. return found;
  1132. }
  1133. static int tls_match_altsubject(X509 *cert, const char *match)
  1134. {
  1135. int type;
  1136. const char *pos, *end;
  1137. size_t len;
  1138. pos = match;
  1139. do {
  1140. if (os_strncmp(pos, "EMAIL:", 6) == 0) {
  1141. type = GEN_EMAIL;
  1142. pos += 6;
  1143. } else if (os_strncmp(pos, "DNS:", 4) == 0) {
  1144. type = GEN_DNS;
  1145. pos += 4;
  1146. } else if (os_strncmp(pos, "URI:", 4) == 0) {
  1147. type = GEN_URI;
  1148. pos += 4;
  1149. } else {
  1150. wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
  1151. "match '%s'", pos);
  1152. return 0;
  1153. }
  1154. end = os_strchr(pos, ';');
  1155. while (end) {
  1156. if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
  1157. os_strncmp(end + 1, "DNS:", 4) == 0 ||
  1158. os_strncmp(end + 1, "URI:", 4) == 0)
  1159. break;
  1160. end = os_strchr(end + 1, ';');
  1161. }
  1162. if (end)
  1163. len = end - pos;
  1164. else
  1165. len = os_strlen(pos);
  1166. if (tls_match_altsubject_component(cert, type, pos, len) > 0)
  1167. return 1;
  1168. pos = end + 1;
  1169. } while (end);
  1170. return 0;
  1171. }
  1172. #ifndef CONFIG_NATIVE_WINDOWS
  1173. static int domain_suffix_match(const u8 *val, size_t len, const char *match,
  1174. int full)
  1175. {
  1176. size_t i, match_len;
  1177. /* Check for embedded nuls that could mess up suffix matching */
  1178. for (i = 0; i < len; i++) {
  1179. if (val[i] == '\0') {
  1180. wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject");
  1181. return 0;
  1182. }
  1183. }
  1184. match_len = os_strlen(match);
  1185. if (match_len > len || (full && match_len != len))
  1186. return 0;
  1187. if (os_strncasecmp((const char *) val + len - match_len, match,
  1188. match_len) != 0)
  1189. return 0; /* no match */
  1190. if (match_len == len)
  1191. return 1; /* exact match */
  1192. if (val[len - match_len - 1] == '.')
  1193. return 1; /* full label match completes suffix match */
  1194. wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match");
  1195. return 0;
  1196. }
  1197. #endif /* CONFIG_NATIVE_WINDOWS */
  1198. static int tls_match_suffix(X509 *cert, const char *match, int full)
  1199. {
  1200. #ifdef CONFIG_NATIVE_WINDOWS
  1201. /* wincrypt.h has conflicting X509_NAME definition */
  1202. return -1;
  1203. #else /* CONFIG_NATIVE_WINDOWS */
  1204. GENERAL_NAME *gen;
  1205. void *ext;
  1206. int i;
  1207. stack_index_t j;
  1208. int dns_name = 0;
  1209. X509_NAME *name;
  1210. wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s",
  1211. full ? "": "suffix ", match);
  1212. ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
  1213. for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) {
  1214. gen = sk_GENERAL_NAME_value(ext, j);
  1215. if (gen->type != GEN_DNS)
  1216. continue;
  1217. dns_name++;
  1218. wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName",
  1219. gen->d.dNSName->data,
  1220. gen->d.dNSName->length);
  1221. if (domain_suffix_match(gen->d.dNSName->data,
  1222. gen->d.dNSName->length, match, full) ==
  1223. 1) {
  1224. wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found",
  1225. full ? "Match" : "Suffix match");
  1226. return 1;
  1227. }
  1228. }
  1229. if (dns_name) {
  1230. wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched");
  1231. return 0;
  1232. }
  1233. name = X509_get_subject_name(cert);
  1234. i = -1;
  1235. for (;;) {
  1236. X509_NAME_ENTRY *e;
  1237. ASN1_STRING *cn;
  1238. i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
  1239. if (i == -1)
  1240. break;
  1241. e = X509_NAME_get_entry(name, i);
  1242. if (e == NULL)
  1243. continue;
  1244. cn = X509_NAME_ENTRY_get_data(e);
  1245. if (cn == NULL)
  1246. continue;
  1247. wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName",
  1248. cn->data, cn->length);
  1249. if (domain_suffix_match(cn->data, cn->length, match, full) == 1)
  1250. {
  1251. wpa_printf(MSG_DEBUG, "TLS: %s in commonName found",
  1252. full ? "Match" : "Suffix match");
  1253. return 1;
  1254. }
  1255. }
  1256. wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found",
  1257. full ? "": "suffix ");
  1258. return 0;
  1259. #endif /* CONFIG_NATIVE_WINDOWS */
  1260. }
  1261. static enum tls_fail_reason openssl_tls_fail_reason(int err)
  1262. {
  1263. switch (err) {
  1264. case X509_V_ERR_CERT_REVOKED:
  1265. return TLS_FAIL_REVOKED;
  1266. case X509_V_ERR_CERT_NOT_YET_VALID:
  1267. case X509_V_ERR_CRL_NOT_YET_VALID:
  1268. return TLS_FAIL_NOT_YET_VALID;
  1269. case X509_V_ERR_CERT_HAS_EXPIRED:
  1270. case X509_V_ERR_CRL_HAS_EXPIRED:
  1271. return TLS_FAIL_EXPIRED;
  1272. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  1273. case X509_V_ERR_UNABLE_TO_GET_CRL:
  1274. case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
  1275. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  1276. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  1277. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  1278. case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  1279. case X509_V_ERR_CERT_CHAIN_TOO_LONG:
  1280. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  1281. case X509_V_ERR_INVALID_CA:
  1282. return TLS_FAIL_UNTRUSTED;
  1283. case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
  1284. case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
  1285. case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
  1286. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  1287. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  1288. case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
  1289. case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
  1290. case X509_V_ERR_CERT_UNTRUSTED:
  1291. case X509_V_ERR_CERT_REJECTED:
  1292. return TLS_FAIL_BAD_CERTIFICATE;
  1293. default:
  1294. return TLS_FAIL_UNSPECIFIED;
  1295. }
  1296. }
  1297. static struct wpabuf * get_x509_cert(X509 *cert)
  1298. {
  1299. struct wpabuf *buf;
  1300. u8 *tmp;
  1301. int cert_len = i2d_X509(cert, NULL);
  1302. if (cert_len <= 0)
  1303. return NULL;
  1304. buf = wpabuf_alloc(cert_len);
  1305. if (buf == NULL)
  1306. return NULL;
  1307. tmp = wpabuf_put(buf, cert_len);
  1308. i2d_X509(cert, &tmp);
  1309. return buf;
  1310. }
  1311. static void openssl_tls_fail_event(struct tls_connection *conn,
  1312. X509 *err_cert, int err, int depth,
  1313. const char *subject, const char *err_str,
  1314. enum tls_fail_reason reason)
  1315. {
  1316. union tls_event_data ev;
  1317. struct wpabuf *cert = NULL;
  1318. struct tls_context *context = conn->context;
  1319. if (context->event_cb == NULL)
  1320. return;
  1321. cert = get_x509_cert(err_cert);
  1322. os_memset(&ev, 0, sizeof(ev));
  1323. ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
  1324. reason : openssl_tls_fail_reason(err);
  1325. ev.cert_fail.depth = depth;
  1326. ev.cert_fail.subject = subject;
  1327. ev.cert_fail.reason_txt = err_str;
  1328. ev.cert_fail.cert = cert;
  1329. context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
  1330. wpabuf_free(cert);
  1331. }
  1332. static void openssl_tls_cert_event(struct tls_connection *conn,
  1333. X509 *err_cert, int depth,
  1334. const char *subject)
  1335. {
  1336. struct wpabuf *cert = NULL;
  1337. union tls_event_data ev;
  1338. struct tls_context *context = conn->context;
  1339. char *altsubject[TLS_MAX_ALT_SUBJECT];
  1340. int alt, num_altsubject = 0;
  1341. GENERAL_NAME *gen;
  1342. void *ext;
  1343. stack_index_t i;
  1344. #ifdef CONFIG_SHA256
  1345. u8 hash[32];
  1346. #endif /* CONFIG_SHA256 */
  1347. if (context->event_cb == NULL)
  1348. return;
  1349. os_memset(&ev, 0, sizeof(ev));
  1350. if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) ||
  1351. context->cert_in_cb) {
  1352. cert = get_x509_cert(err_cert);
  1353. ev.peer_cert.cert = cert;
  1354. }
  1355. #ifdef CONFIG_SHA256
  1356. if (cert) {
  1357. const u8 *addr[1];
  1358. size_t len[1];
  1359. addr[0] = wpabuf_head(cert);
  1360. len[0] = wpabuf_len(cert);
  1361. if (sha256_vector(1, addr, len, hash) == 0) {
  1362. ev.peer_cert.hash = hash;
  1363. ev.peer_cert.hash_len = sizeof(hash);
  1364. }
  1365. }
  1366. #endif /* CONFIG_SHA256 */
  1367. ev.peer_cert.depth = depth;
  1368. ev.peer_cert.subject = subject;
  1369. ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL);
  1370. for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
  1371. char *pos;
  1372. if (num_altsubject == TLS_MAX_ALT_SUBJECT)
  1373. break;
  1374. gen = sk_GENERAL_NAME_value(ext, i);
  1375. if (gen->type != GEN_EMAIL &&
  1376. gen->type != GEN_DNS &&
  1377. gen->type != GEN_URI)
  1378. continue;
  1379. pos = os_malloc(10 + gen->d.ia5->length + 1);
  1380. if (pos == NULL)
  1381. break;
  1382. altsubject[num_altsubject++] = pos;
  1383. switch (gen->type) {
  1384. case GEN_EMAIL:
  1385. os_memcpy(pos, "EMAIL:", 6);
  1386. pos += 6;
  1387. break;
  1388. case GEN_DNS:
  1389. os_memcpy(pos, "DNS:", 4);
  1390. pos += 4;
  1391. break;
  1392. case GEN_URI:
  1393. os_memcpy(pos, "URI:", 4);
  1394. pos += 4;
  1395. break;
  1396. }
  1397. os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length);
  1398. pos += gen->d.ia5->length;
  1399. *pos = '\0';
  1400. }
  1401. for (alt = 0; alt < num_altsubject; alt++)
  1402. ev.peer_cert.altsubject[alt] = altsubject[alt];
  1403. ev.peer_cert.num_altsubject = num_altsubject;
  1404. context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
  1405. wpabuf_free(cert);
  1406. for (alt = 0; alt < num_altsubject; alt++)
  1407. os_free(altsubject[alt]);
  1408. }
  1409. static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
  1410. {
  1411. char buf[256];
  1412. X509 *err_cert;
  1413. int err, depth;
  1414. SSL *ssl;
  1415. struct tls_connection *conn;
  1416. struct tls_context *context;
  1417. char *match, *altmatch, *suffix_match, *domain_match;
  1418. const char *err_str;
  1419. err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
  1420. if (!err_cert)
  1421. return 0;
  1422. err = X509_STORE_CTX_get_error(x509_ctx);
  1423. depth = X509_STORE_CTX_get_error_depth(x509_ctx);
  1424. ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
  1425. SSL_get_ex_data_X509_STORE_CTX_idx());
  1426. X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
  1427. conn = SSL_get_app_data(ssl);
  1428. if (conn == NULL)
  1429. return 0;
  1430. if (depth == 0)
  1431. conn->peer_cert = err_cert;
  1432. else if (depth == 1)
  1433. conn->peer_issuer = err_cert;
  1434. else if (depth == 2)
  1435. conn->peer_issuer_issuer = err_cert;
  1436. context = conn->context;
  1437. match = conn->subject_match;
  1438. altmatch = conn->altsubject_match;
  1439. suffix_match = conn->suffix_match;
  1440. domain_match = conn->domain_match;
  1441. if (!preverify_ok && !conn->ca_cert_verify)
  1442. preverify_ok = 1;
  1443. if (!preverify_ok && depth > 0 && conn->server_cert_only)
  1444. preverify_ok = 1;
  1445. if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
  1446. (err == X509_V_ERR_CERT_HAS_EXPIRED ||
  1447. err == X509_V_ERR_CERT_NOT_YET_VALID)) {
  1448. wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity "
  1449. "time mismatch");
  1450. preverify_ok = 1;
  1451. }
  1452. err_str = X509_verify_cert_error_string(err);
  1453. #ifdef CONFIG_SHA256
  1454. /*
  1455. * Do not require preverify_ok so we can explicity allow otherwise
  1456. * invalid pinned server certificates.
  1457. */
  1458. if (depth == 0 && conn->server_cert_only) {
  1459. struct wpabuf *cert;
  1460. cert = get_x509_cert(err_cert);
  1461. if (!cert) {
  1462. wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch "
  1463. "server certificate data");
  1464. preverify_ok = 0;
  1465. } else {
  1466. u8 hash[32];
  1467. const u8 *addr[1];
  1468. size_t len[1];
  1469. addr[0] = wpabuf_head(cert);
  1470. len[0] = wpabuf_len(cert);
  1471. if (sha256_vector(1, addr, len, hash) < 0 ||
  1472. os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
  1473. err_str = "Server certificate mismatch";
  1474. err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
  1475. preverify_ok = 0;
  1476. } else if (!preverify_ok) {
  1477. /*
  1478. * Certificate matches pinned certificate, allow
  1479. * regardless of other problems.
  1480. */
  1481. wpa_printf(MSG_DEBUG,
  1482. "OpenSSL: Ignore validation issues for a pinned server certificate");
  1483. preverify_ok = 1;
  1484. }
  1485. wpabuf_free(cert);
  1486. }
  1487. }
  1488. #endif /* CONFIG_SHA256 */
  1489. if (!preverify_ok) {
  1490. wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
  1491. " error %d (%s) depth %d for '%s'", err, err_str,
  1492. depth, buf);
  1493. openssl_tls_fail_event(conn, err_cert, err, depth, buf,
  1494. err_str, TLS_FAIL_UNSPECIFIED);
  1495. return preverify_ok;
  1496. }
  1497. wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
  1498. "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
  1499. preverify_ok, err, err_str,
  1500. conn->ca_cert_verify, depth, buf);
  1501. if (depth == 0 && match && os_strstr(buf, match) == NULL) {
  1502. wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
  1503. "match with '%s'", buf, match);
  1504. preverify_ok = 0;
  1505. openssl_tls_fail_event(conn, err_cert, err, depth, buf,
  1506. "Subject mismatch",
  1507. TLS_FAIL_SUBJECT_MISMATCH);
  1508. } else if (depth == 0 && altmatch &&
  1509. !tls_match_altsubject(err_cert, altmatch)) {
  1510. wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
  1511. "'%s' not found", altmatch);
  1512. preverify_ok = 0;
  1513. openssl_tls_fail_event(conn, err_cert, err, depth, buf,
  1514. "AltSubject mismatch",
  1515. TLS_FAIL_ALTSUBJECT_MISMATCH);
  1516. } else if (depth == 0 && suffix_match &&
  1517. !tls_match_suffix(err_cert, suffix_match, 0)) {
  1518. wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found",
  1519. suffix_match);
  1520. preverify_ok = 0;
  1521. openssl_tls_fail_event(conn, err_cert, err, depth, buf,
  1522. "Domain suffix mismatch",
  1523. TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
  1524. } else if (depth == 0 && domain_match &&
  1525. !tls_match_suffix(err_cert, domain_match, 1)) {
  1526. wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
  1527. domain_match);
  1528. preverify_ok = 0;
  1529. openssl_tls_fail_event(conn, err_cert, err, depth, buf,
  1530. "Domain mismatch",
  1531. TLS_FAIL_DOMAIN_MISMATCH);
  1532. } else
  1533. openssl_tls_cert_event(conn, err_cert, depth, buf);
  1534. if (conn->cert_probe && preverify_ok && depth == 0) {
  1535. wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
  1536. "on probe-only run");
  1537. preverify_ok = 0;
  1538. openssl_tls_fail_event(conn, err_cert, err, depth, buf,
  1539. "Server certificate chain probe",
  1540. TLS_FAIL_SERVER_CHAIN_PROBE);
  1541. }
  1542. #ifdef OPENSSL_IS_BORINGSSL
  1543. if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
  1544. preverify_ok) {
  1545. enum ocsp_result res;
  1546. res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
  1547. conn->peer_issuer,
  1548. conn->peer_issuer_issuer);
  1549. if (res == OCSP_REVOKED) {
  1550. preverify_ok = 0;
  1551. openssl_tls_fail_event(conn, err_cert, err, depth, buf,
  1552. "certificate revoked",
  1553. TLS_FAIL_REVOKED);
  1554. if (err == X509_V_OK)
  1555. X509_STORE_CTX_set_error(
  1556. x509_ctx, X509_V_ERR_CERT_REVOKED);
  1557. } else if (res != OCSP_GOOD &&
  1558. (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
  1559. preverify_ok = 0;
  1560. openssl_tls_fail_event(conn, err_cert, err, depth, buf,
  1561. "bad certificate status response",
  1562. TLS_FAIL_UNSPECIFIED);
  1563. }
  1564. }
  1565. #endif /* OPENSSL_IS_BORINGSSL */
  1566. if (depth == 0 && preverify_ok && context->event_cb != NULL)
  1567. context->event_cb(context->cb_ctx,
  1568. TLS_CERT_CHAIN_SUCCESS, NULL);
  1569. return preverify_ok;
  1570. }
  1571. #ifndef OPENSSL_NO_STDIO
  1572. static int tls_load_ca_der(struct tls_data *data, const char *ca_cert)
  1573. {
  1574. SSL_CTX *ssl_ctx = data->ssl;
  1575. X509_LOOKUP *lookup;
  1576. int ret = 0;
  1577. lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx),
  1578. X509_LOOKUP_file());
  1579. if (lookup == NULL) {
  1580. tls_show_errors(MSG_WARNING, __func__,
  1581. "Failed add lookup for X509 store");
  1582. return -1;
  1583. }
  1584. if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
  1585. unsigned long err = ERR_peek_error();
  1586. tls_show_errors(MSG_WARNING, __func__,
  1587. "Failed load CA in DER format");
  1588. if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
  1589. ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
  1590. wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
  1591. "cert already in hash table error",
  1592. __func__);
  1593. } else
  1594. ret = -1;
  1595. }
  1596. return ret;
  1597. }
  1598. #endif /* OPENSSL_NO_STDIO */
  1599. static int tls_connection_ca_cert(struct tls_data *data,
  1600. struct tls_connection *conn,
  1601. const char *ca_cert, const u8 *ca_cert_blob,
  1602. size_t ca_cert_blob_len, const char *ca_path)
  1603. {
  1604. SSL_CTX *ssl_ctx = data->ssl;
  1605. X509_STORE *store;
  1606. /*
  1607. * Remove previously configured trusted CA certificates before adding
  1608. * new ones.
  1609. */
  1610. store = X509_STORE_new();
  1611. if (store == NULL) {
  1612. wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
  1613. "certificate store", __func__);
  1614. return -1;
  1615. }
  1616. SSL_CTX_set_cert_store(ssl_ctx, store);
  1617. SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
  1618. conn->ca_cert_verify = 1;
  1619. if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
  1620. wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
  1621. "chain");
  1622. conn->cert_probe = 1;
  1623. conn->ca_cert_verify = 0;
  1624. return 0;
  1625. }
  1626. if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
  1627. #ifdef CONFIG_SHA256
  1628. const char *pos = ca_cert + 7;
  1629. if (os_strncmp(pos, "server/sha256/", 14) != 0) {
  1630. wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
  1631. "hash value '%s'", ca_cert);
  1632. return -1;
  1633. }
  1634. pos += 14;
  1635. if (os_strlen(pos) != 32 * 2) {
  1636. wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
  1637. "hash length in ca_cert '%s'", ca_cert);
  1638. return -1;
  1639. }
  1640. if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
  1641. wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
  1642. "value in ca_cert '%s'", ca_cert);
  1643. return -1;
  1644. }
  1645. conn->server_cert_only = 1;
  1646. wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
  1647. "certificate match");
  1648. return 0;
  1649. #else /* CONFIG_SHA256 */
  1650. wpa_printf(MSG_INFO, "No SHA256 included in the build - "
  1651. "cannot validate server certificate hash");
  1652. return -1;
  1653. #endif /* CONFIG_SHA256 */
  1654. }
  1655. if (ca_cert_blob) {
  1656. X509 *cert = d2i_X509(NULL,
  1657. (const unsigned char **) &ca_cert_blob,
  1658. ca_cert_blob_len);
  1659. if (cert == NULL) {
  1660. tls_show_errors(MSG_WARNING, __func__,
  1661. "Failed to parse ca_cert_blob");
  1662. return -1;
  1663. }
  1664. if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
  1665. cert)) {
  1666. unsigned long err = ERR_peek_error();
  1667. tls_show_errors(MSG_WARNING, __func__,
  1668. "Failed to add ca_cert_blob to "
  1669. "certificate store");
  1670. if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
  1671. ERR_GET_REASON(err) ==
  1672. X509_R_CERT_ALREADY_IN_HASH_TABLE) {
  1673. wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
  1674. "cert already in hash table error",
  1675. __func__);
  1676. } else {
  1677. X509_free(cert);
  1678. return -1;
  1679. }
  1680. }
  1681. X509_free(cert);
  1682. wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
  1683. "to certificate store", __func__);
  1684. return 0;
  1685. }
  1686. #ifdef ANDROID
  1687. if (ca_cert && os_strncmp("keystore://", ca_cert, 11) == 0) {
  1688. BIO *bio = BIO_from_keystore(&ca_cert[11]);
  1689. STACK_OF(X509_INFO) *stack = NULL;
  1690. stack_index_t i;
  1691. if (bio) {
  1692. stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
  1693. BIO_free(bio);
  1694. }
  1695. if (!stack)
  1696. return -1;
  1697. for (i = 0; i < sk_X509_INFO_num(stack); ++i) {
  1698. X509_INFO *info = sk_X509_INFO_value(stack, i);
  1699. if (info->x509) {
  1700. X509_STORE_add_cert(ssl_ctx->cert_store,
  1701. info->x509);
  1702. }
  1703. if (info->crl) {
  1704. X509_STORE_add_crl(ssl_ctx->cert_store,
  1705. info->crl);
  1706. }
  1707. }
  1708. sk_X509_INFO_pop_free(stack, X509_INFO_free);
  1709. SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
  1710. return 0;
  1711. }
  1712. #endif /* ANDROID */
  1713. #ifdef CONFIG_NATIVE_WINDOWS
  1714. if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
  1715. 0) {
  1716. wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
  1717. "system certificate store");
  1718. return 0;
  1719. }
  1720. #endif /* CONFIG_NATIVE_WINDOWS */
  1721. if (ca_cert || ca_path) {
  1722. #ifndef OPENSSL_NO_STDIO
  1723. if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
  1724. 1) {
  1725. tls_show_errors(MSG_WARNING, __func__,
  1726. "Failed to load root certificates");
  1727. if (ca_cert &&
  1728. tls_load_ca_der(data, ca_cert) == 0) {
  1729. wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
  1730. "DER format CA certificate",
  1731. __func__);
  1732. } else
  1733. return -1;
  1734. } else {
  1735. wpa_printf(MSG_DEBUG, "TLS: Trusted root "
  1736. "certificate(s) loaded");
  1737. tls_get_errors(data);
  1738. }
  1739. #else /* OPENSSL_NO_STDIO */
  1740. wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
  1741. __func__);
  1742. return -1;
  1743. #endif /* OPENSSL_NO_STDIO */
  1744. } else {
  1745. /* No ca_cert configured - do not try to verify server
  1746. * certificate */
  1747. conn->ca_cert_verify = 0;
  1748. }
  1749. return 0;
  1750. }
  1751. static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert)
  1752. {
  1753. SSL_CTX *ssl_ctx = data->ssl;
  1754. if (ca_cert) {
  1755. if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
  1756. {
  1757. tls_show_errors(MSG_WARNING, __func__,
  1758. "Failed to load root certificates");
  1759. return -1;
  1760. }
  1761. wpa_printf(MSG_DEBUG, "TLS: Trusted root "
  1762. "certificate(s) loaded");
  1763. #ifndef OPENSSL_NO_STDIO
  1764. /* Add the same CAs to the client certificate requests */
  1765. SSL_CTX_set_client_CA_list(ssl_ctx,
  1766. SSL_load_client_CA_file(ca_cert));
  1767. #endif /* OPENSSL_NO_STDIO */
  1768. }
  1769. return 0;
  1770. }
  1771. int tls_global_set_verify(void *ssl_ctx, int check_crl)
  1772. {
  1773. int flags;
  1774. if (check_crl) {
  1775. struct tls_data *data = ssl_ctx;
  1776. X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl);
  1777. if (cs == NULL) {
  1778. tls_show_errors(MSG_INFO, __func__, "Failed to get "
  1779. "certificate store when enabling "
  1780. "check_crl");
  1781. return -1;
  1782. }
  1783. flags = X509_V_FLAG_CRL_CHECK;
  1784. if (check_crl == 2)
  1785. flags |= X509_V_FLAG_CRL_CHECK_ALL;
  1786. X509_STORE_set_flags(cs, flags);
  1787. }
  1788. return 0;
  1789. }
  1790. static int tls_connection_set_subject_match(struct tls_connection *conn,
  1791. const char *subject_match,
  1792. const char *altsubject_match,
  1793. const char *suffix_match,
  1794. const char *domain_match)
  1795. {
  1796. os_free(conn->subject_match);
  1797. conn->subject_match = NULL;
  1798. if (subject_match) {
  1799. conn->subject_match = os_strdup(subject_match);
  1800. if (conn->subject_match == NULL)
  1801. return -1;
  1802. }
  1803. os_free(conn->altsubject_match);
  1804. conn->altsubject_match = NULL;
  1805. if (altsubject_match) {
  1806. conn->altsubject_match = os_strdup(altsubject_match);
  1807. if (conn->altsubject_match == NULL)
  1808. return -1;
  1809. }
  1810. os_free(conn->suffix_match);
  1811. conn->suffix_match = NULL;
  1812. if (suffix_match) {
  1813. conn->suffix_match = os_strdup(suffix_match);
  1814. if (conn->suffix_match == NULL)
  1815. return -1;
  1816. }
  1817. os_free(conn->domain_match);
  1818. conn->domain_match = NULL;
  1819. if (domain_match) {
  1820. conn->domain_match = os_strdup(domain_match);
  1821. if (conn->domain_match == NULL)
  1822. return -1;
  1823. }
  1824. return 0;
  1825. }
  1826. static void tls_set_conn_flags(SSL *ssl, unsigned int flags)
  1827. {
  1828. #ifdef SSL_OP_NO_TICKET
  1829. if (flags & TLS_CONN_DISABLE_SESSION_TICKET)
  1830. SSL_set_options(ssl, SSL_OP_NO_TICKET);
  1831. #ifdef SSL_clear_options
  1832. else
  1833. SSL_clear_options(ssl, SSL_OP_NO_TICKET);
  1834. #endif /* SSL_clear_options */
  1835. #endif /* SSL_OP_NO_TICKET */
  1836. #ifdef SSL_OP_NO_TLSv1
  1837. if (flags & TLS_CONN_DISABLE_TLSv1_0)
  1838. SSL_set_options(ssl, SSL_OP_NO_TLSv1);
  1839. else
  1840. SSL_clear_options(ssl, SSL_OP_NO_TLSv1);
  1841. #endif /* SSL_OP_NO_TLSv1 */
  1842. #ifdef SSL_OP_NO_TLSv1_1
  1843. if (flags & TLS_CONN_DISABLE_TLSv1_1)
  1844. SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
  1845. else
  1846. SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1);
  1847. #endif /* SSL_OP_NO_TLSv1_1 */
  1848. #ifdef SSL_OP_NO_TLSv1_2
  1849. if (flags & TLS_CONN_DISABLE_TLSv1_2)
  1850. SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
  1851. else
  1852. SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2);
  1853. #endif /* SSL_OP_NO_TLSv1_2 */
  1854. }
  1855. int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
  1856. int verify_peer, unsigned int flags,
  1857. const u8 *session_ctx, size_t session_ctx_len)
  1858. {
  1859. static int counter = 0;
  1860. struct tls_data *data = ssl_ctx;
  1861. if (conn == NULL)
  1862. return -1;
  1863. if (verify_peer) {
  1864. conn->ca_cert_verify = 1;
  1865. SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
  1866. SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
  1867. SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
  1868. } else {
  1869. conn->ca_cert_verify = 0;
  1870. SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
  1871. }
  1872. tls_set_conn_flags(conn->ssl, flags);
  1873. conn->flags = flags;
  1874. SSL_set_accept_state(conn->ssl);
  1875. if (data->tls_session_lifetime == 0) {
  1876. /*
  1877. * Set session id context to a unique value to make sure
  1878. * session resumption cannot be used either through session
  1879. * caching or TLS ticket extension.
  1880. */
  1881. counter++;
  1882. SSL_set_session_id_context(conn->ssl,
  1883. (const unsigned char *) &counter,
  1884. sizeof(counter));
  1885. } else if (session_ctx) {
  1886. SSL_set_session_id_context(conn->ssl, session_ctx,
  1887. session_ctx_len);
  1888. }
  1889. return 0;
  1890. }
  1891. static int tls_connection_client_cert(struct tls_connection *conn,
  1892. const char *client_cert,
  1893. const u8 *client_cert_blob,
  1894. size_t client_cert_blob_len)
  1895. {
  1896. if (client_cert == NULL && client_cert_blob == NULL)
  1897. return 0;
  1898. if (client_cert_blob &&
  1899. SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
  1900. client_cert_blob_len) == 1) {
  1901. wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
  1902. "OK");
  1903. return 0;
  1904. } else if (client_cert_blob) {
  1905. tls_show_errors(MSG_DEBUG, __func__,
  1906. "SSL_use_certificate_ASN1 failed");
  1907. }
  1908. if (client_cert == NULL)
  1909. return -1;
  1910. #ifdef ANDROID
  1911. if (os_strncmp("keystore://", client_cert, 11) == 0) {
  1912. BIO *bio = BIO_from_keystore(&client_cert[11]);
  1913. X509 *x509 = NULL;
  1914. int ret = -1;
  1915. if (bio) {
  1916. x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
  1917. BIO_free(bio);
  1918. }
  1919. if (x509) {
  1920. if (SSL_use_certificate(conn->ssl, x509) == 1)
  1921. ret = 0;
  1922. X509_free(x509);
  1923. }
  1924. return ret;
  1925. }
  1926. #endif /* ANDROID */
  1927. #ifndef OPENSSL_NO_STDIO
  1928. if (SSL_use_certificate_file(conn->ssl, client_cert,
  1929. SSL_FILETYPE_ASN1) == 1) {
  1930. wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
  1931. " --> OK");
  1932. return 0;
  1933. }
  1934. if (SSL_use_certificate_file(conn->ssl, client_cert,
  1935. SSL_FILETYPE_PEM) == 1) {
  1936. ERR_clear_error();
  1937. wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
  1938. " --> OK");
  1939. return 0;
  1940. }
  1941. tls_show_errors(MSG_DEBUG, __func__,
  1942. "SSL_use_certificate_file failed");
  1943. #else /* OPENSSL_NO_STDIO */
  1944. wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
  1945. #endif /* OPENSSL_NO_STDIO */
  1946. return -1;
  1947. }
  1948. static int tls_global_client_cert(struct tls_data *data,
  1949. const char *client_cert)
  1950. {
  1951. #ifndef OPENSSL_NO_STDIO
  1952. SSL_CTX *ssl_ctx = data->ssl;
  1953. if (client_cert == NULL)
  1954. return 0;
  1955. if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
  1956. SSL_FILETYPE_ASN1) != 1 &&
  1957. SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 &&
  1958. SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
  1959. SSL_FILETYPE_PEM) != 1) {
  1960. tls_show_errors(MSG_INFO, __func__,
  1961. "Failed to load client certificate");
  1962. return -1;
  1963. }
  1964. return 0;
  1965. #else /* OPENSSL_NO_STDIO */
  1966. if (client_cert == NULL)
  1967. return 0;
  1968. wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
  1969. return -1;
  1970. #endif /* OPENSSL_NO_STDIO */
  1971. }
  1972. static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
  1973. {
  1974. if (password == NULL) {
  1975. return 0;
  1976. }
  1977. os_strlcpy(buf, (char *) password, size);
  1978. return os_strlen(buf);
  1979. }
  1980. #ifdef PKCS12_FUNCS
  1981. static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12,
  1982. const char *passwd)
  1983. {
  1984. EVP_PKEY *pkey;
  1985. X509 *cert;
  1986. STACK_OF(X509) *certs;
  1987. int res = 0;
  1988. char buf[256];
  1989. pkey = NULL;
  1990. cert = NULL;
  1991. certs = NULL;
  1992. if (!passwd)
  1993. passwd = "";
  1994. if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
  1995. tls_show_errors(MSG_DEBUG, __func__,
  1996. "Failed to parse PKCS12 file");
  1997. PKCS12_free(p12);
  1998. return -1;
  1999. }
  2000. wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
  2001. if (cert) {
  2002. X509_NAME_oneline(X509_get_subject_name(cert), buf,
  2003. sizeof(buf));
  2004. wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
  2005. "subject='%s'", buf);
  2006. if (ssl) {
  2007. if (SSL_use_certificate(ssl, cert) != 1)
  2008. res = -1;
  2009. } else {
  2010. if (SSL_CTX_use_certificate(data->ssl, cert) != 1)
  2011. res = -1;
  2012. }
  2013. X509_free(cert);
  2014. }
  2015. if (pkey) {
  2016. wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
  2017. if (ssl) {
  2018. if (SSL_use_PrivateKey(ssl, pkey) != 1)
  2019. res = -1;
  2020. } else {
  2021. if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1)
  2022. res = -1;
  2023. }
  2024. EVP_PKEY_free(pkey);
  2025. }
  2026. if (certs) {
  2027. #if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER)
  2028. SSL_clear_chain_certs(ssl);
  2029. while ((cert = sk_X509_pop(certs)) != NULL) {
  2030. X509_NAME_oneline(X509_get_subject_name(cert), buf,
  2031. sizeof(buf));
  2032. wpa_printf(MSG_DEBUG, "TLS: additional certificate"
  2033. " from PKCS12: subject='%s'", buf);
  2034. if (SSL_add1_chain_cert(ssl, cert) != 1) {
  2035. tls_show_errors(MSG_DEBUG, __func__,
  2036. "Failed to add additional certificate");
  2037. res = -1;
  2038. break;
  2039. }
  2040. }
  2041. if (!res) {
  2042. /* Try to continue anyway */
  2043. }
  2044. sk_X509_free(certs);
  2045. #ifndef OPENSSL_IS_BORINGSSL
  2046. res = SSL_build_cert_chain(ssl,
  2047. SSL_BUILD_CHAIN_FLAG_CHECK |
  2048. SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
  2049. if (!res) {
  2050. tls_show_errors(MSG_DEBUG, __func__,
  2051. "Failed to build certificate chain");
  2052. } else if (res == 2) {
  2053. wpa_printf(MSG_DEBUG,
  2054. "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
  2055. }
  2056. #endif /* OPENSSL_IS_BORINGSSL */
  2057. /*
  2058. * Try to continue regardless of result since it is possible for
  2059. * the extra certificates not to be required.
  2060. */
  2061. res = 0;
  2062. #else /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
  2063. #if OPENSSL_VERSION_NUMBER >= 0x10001000L
  2064. SSL_CTX_clear_extra_chain_certs(data->ssl);
  2065. #endif /* OPENSSL_VERSION_NUMBER >= 0x10001000L */
  2066. while ((cert = sk_X509_pop(certs)) != NULL) {
  2067. X509_NAME_oneline(X509_get_subject_name(cert), buf,
  2068. sizeof(buf));
  2069. wpa_printf(MSG_DEBUG, "TLS: additional certificate"
  2070. " from PKCS12: subject='%s'", buf);
  2071. /*
  2072. * There is no SSL equivalent for the chain cert - so
  2073. * always add it to the context...
  2074. */
  2075. if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1)
  2076. {
  2077. res = -1;
  2078. break;
  2079. }
  2080. }
  2081. sk_X509_free(certs);
  2082. #endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
  2083. }
  2084. PKCS12_free(p12);
  2085. if (res < 0)
  2086. tls_get_errors(data);
  2087. return res;
  2088. }
  2089. #endif /* PKCS12_FUNCS */
  2090. static int tls_read_pkcs12(struct tls_data *data, SSL *ssl,
  2091. const char *private_key, const char *passwd)
  2092. {
  2093. #ifdef PKCS12_FUNCS
  2094. FILE *f;
  2095. PKCS12 *p12;
  2096. f = fopen(private_key, "rb");
  2097. if (f == NULL)
  2098. return -1;
  2099. p12 = d2i_PKCS12_fp(f, NULL);
  2100. fclose(f);
  2101. if (p12 == NULL) {
  2102. tls_show_errors(MSG_INFO, __func__,
  2103. "Failed to use PKCS#12 file");
  2104. return -1;
  2105. }
  2106. return tls_parse_pkcs12(data, ssl, p12, passwd);
  2107. #else /* PKCS12_FUNCS */
  2108. wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
  2109. "p12/pfx files");
  2110. return -1;
  2111. #endif /* PKCS12_FUNCS */
  2112. }
  2113. static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl,
  2114. const u8 *blob, size_t len, const char *passwd)
  2115. {
  2116. #ifdef PKCS12_FUNCS
  2117. PKCS12 *p12;
  2118. p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len);
  2119. if (p12 == NULL) {
  2120. tls_show_errors(MSG_INFO, __func__,
  2121. "Failed to use PKCS#12 blob");
  2122. return -1;
  2123. }
  2124. return tls_parse_pkcs12(data, ssl, p12, passwd);
  2125. #else /* PKCS12_FUNCS */
  2126. wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
  2127. "p12/pfx blobs");
  2128. return -1;
  2129. #endif /* PKCS12_FUNCS */
  2130. }
  2131. #ifndef OPENSSL_NO_ENGINE
  2132. static int tls_engine_get_cert(struct tls_connection *conn,
  2133. const char *cert_id,
  2134. X509 **cert)
  2135. {
  2136. /* this runs after the private key is loaded so no PIN is required */
  2137. struct {
  2138. const char *cert_id;
  2139. X509 *cert;
  2140. } params;
  2141. params.cert_id = cert_id;
  2142. params.cert = NULL;
  2143. if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
  2144. 0, &params, NULL, 1)) {
  2145. unsigned long err = ERR_get_error();
  2146. wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
  2147. " '%s' [%s]", cert_id,
  2148. ERR_error_string(err, NULL));
  2149. if (tls_is_pin_error(err))
  2150. return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
  2151. return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
  2152. }
  2153. if (!params.cert) {
  2154. wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
  2155. " '%s'", cert_id);
  2156. return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
  2157. }
  2158. *cert = params.cert;
  2159. return 0;
  2160. }
  2161. #endif /* OPENSSL_NO_ENGINE */
  2162. static int tls_connection_engine_client_cert(struct tls_connection *conn,
  2163. const char *cert_id)
  2164. {
  2165. #ifndef OPENSSL_NO_ENGINE
  2166. X509 *cert;
  2167. if (tls_engine_get_cert(conn, cert_id, &cert))
  2168. return -1;
  2169. if (!SSL_use_certificate(conn->ssl, cert)) {
  2170. tls_show_errors(MSG_ERROR, __func__,
  2171. "SSL_use_certificate failed");
  2172. X509_free(cert);
  2173. return -1;
  2174. }
  2175. X509_free(cert);
  2176. wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
  2177. "OK");
  2178. return 0;
  2179. #else /* OPENSSL_NO_ENGINE */
  2180. return -1;
  2181. #endif /* OPENSSL_NO_ENGINE */
  2182. }
  2183. static int tls_connection_engine_ca_cert(struct tls_data *data,
  2184. struct tls_connection *conn,
  2185. const char *ca_cert_id)
  2186. {
  2187. #ifndef OPENSSL_NO_ENGINE
  2188. X509 *cert;
  2189. SSL_CTX *ssl_ctx = data->ssl;
  2190. X509_STORE *store;
  2191. if (tls_engine_get_cert(conn, ca_cert_id, &cert))
  2192. return -1;
  2193. /* start off the same as tls_connection_ca_cert */
  2194. store = X509_STORE_new();
  2195. if (store == NULL) {
  2196. wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
  2197. "certificate store", __func__);
  2198. X509_free(cert);
  2199. return -1;
  2200. }
  2201. SSL_CTX_set_cert_store(ssl_ctx, store);
  2202. if (!X509_STORE_add_cert(store, cert)) {
  2203. unsigned long err = ERR_peek_error();
  2204. tls_show_errors(MSG_WARNING, __func__,
  2205. "Failed to add CA certificate from engine "
  2206. "to certificate store");
  2207. if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
  2208. ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
  2209. wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
  2210. " already in hash table error",
  2211. __func__);
  2212. } else {
  2213. X509_free(cert);
  2214. return -1;
  2215. }
  2216. }
  2217. X509_free(cert);
  2218. wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
  2219. "to certificate store", __func__);
  2220. SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
  2221. conn->ca_cert_verify = 1;
  2222. return 0;
  2223. #else /* OPENSSL_NO_ENGINE */
  2224. return -1;
  2225. #endif /* OPENSSL_NO_ENGINE */
  2226. }
  2227. static int tls_connection_engine_private_key(struct tls_connection *conn)
  2228. {
  2229. #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
  2230. if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
  2231. tls_show_errors(MSG_ERROR, __func__,
  2232. "ENGINE: cannot use private key for TLS");
  2233. return -1;
  2234. }
  2235. if (!SSL_check_private_key(conn->ssl)) {
  2236. tls_show_errors(MSG_INFO, __func__,
  2237. "Private key failed verification");
  2238. return -1;
  2239. }
  2240. return 0;
  2241. #else /* OPENSSL_NO_ENGINE */
  2242. wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
  2243. "engine support was not compiled in");
  2244. return -1;
  2245. #endif /* OPENSSL_NO_ENGINE */
  2246. }
  2247. static int tls_connection_private_key(struct tls_data *data,
  2248. struct tls_connection *conn,
  2249. const char *private_key,
  2250. const char *private_key_passwd,
  2251. const u8 *private_key_blob,
  2252. size_t private_key_blob_len)
  2253. {
  2254. SSL_CTX *ssl_ctx = data->ssl;
  2255. char *passwd;
  2256. int ok;
  2257. if (private_key == NULL && private_key_blob == NULL)
  2258. return 0;
  2259. if (private_key_passwd) {
  2260. passwd = os_strdup(private_key_passwd);
  2261. if (passwd == NULL)
  2262. return -1;
  2263. } else
  2264. passwd = NULL;
  2265. SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
  2266. SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
  2267. ok = 0;
  2268. while (private_key_blob) {
  2269. if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
  2270. (u8 *) private_key_blob,
  2271. private_key_blob_len) == 1) {
  2272. wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
  2273. "ASN1(EVP_PKEY_RSA) --> OK");
  2274. ok = 1;
  2275. break;
  2276. }
  2277. if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
  2278. (u8 *) private_key_blob,
  2279. private_key_blob_len) == 1) {
  2280. wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
  2281. "ASN1(EVP_PKEY_DSA) --> OK");
  2282. ok = 1;
  2283. break;
  2284. }
  2285. if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
  2286. (u8 *) private_key_blob,
  2287. private_key_blob_len) == 1) {
  2288. wpa_printf(MSG_DEBUG, "OpenSSL: "
  2289. "SSL_use_RSAPrivateKey_ASN1 --> OK");
  2290. ok = 1;
  2291. break;
  2292. }
  2293. if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob,
  2294. private_key_blob_len, passwd) == 0) {
  2295. wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
  2296. "OK");
  2297. ok = 1;
  2298. break;
  2299. }
  2300. break;
  2301. }
  2302. while (!ok && private_key) {
  2303. #ifndef OPENSSL_NO_STDIO
  2304. if (SSL_use_PrivateKey_file(conn->ssl, private_key,
  2305. SSL_FILETYPE_ASN1) == 1) {
  2306. wpa_printf(MSG_DEBUG, "OpenSSL: "
  2307. "SSL_use_PrivateKey_File (DER) --> OK");
  2308. ok = 1;
  2309. break;
  2310. }
  2311. if (SSL_use_PrivateKey_file(conn->ssl, private_key,
  2312. SSL_FILETYPE_PEM) == 1) {
  2313. wpa_printf(MSG_DEBUG, "OpenSSL: "
  2314. "SSL_use_PrivateKey_File (PEM) --> OK");
  2315. ok = 1;
  2316. break;
  2317. }
  2318. #else /* OPENSSL_NO_STDIO */
  2319. wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
  2320. __func__);
  2321. #endif /* OPENSSL_NO_STDIO */
  2322. if (tls_read_pkcs12(data, conn->ssl, private_key, passwd)
  2323. == 0) {
  2324. wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
  2325. "--> OK");
  2326. ok = 1;
  2327. break;
  2328. }
  2329. if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
  2330. wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
  2331. "access certificate store --> OK");
  2332. ok = 1;
  2333. break;
  2334. }
  2335. break;
  2336. }
  2337. if (!ok) {
  2338. tls_show_errors(MSG_INFO, __func__,
  2339. "Failed to load private key");
  2340. os_free(passwd);
  2341. return -1;
  2342. }
  2343. ERR_clear_error();
  2344. SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
  2345. os_free(passwd);
  2346. if (!SSL_check_private_key(conn->ssl)) {
  2347. tls_show_errors(MSG_INFO, __func__, "Private key failed "
  2348. "verification");
  2349. return -1;
  2350. }
  2351. wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
  2352. return 0;
  2353. }
  2354. static int tls_global_private_key(struct tls_data *data,
  2355. const char *private_key,
  2356. const char *private_key_passwd)
  2357. {
  2358. SSL_CTX *ssl_ctx = data->ssl;
  2359. char *passwd;
  2360. if (private_key == NULL)
  2361. return 0;
  2362. if (private_key_passwd) {
  2363. passwd = os_strdup(private_key_passwd);
  2364. if (passwd == NULL)
  2365. return -1;
  2366. } else
  2367. passwd = NULL;
  2368. SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
  2369. SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
  2370. if (
  2371. #ifndef OPENSSL_NO_STDIO
  2372. SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
  2373. SSL_FILETYPE_ASN1) != 1 &&
  2374. SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
  2375. SSL_FILETYPE_PEM) != 1 &&
  2376. #endif /* OPENSSL_NO_STDIO */
  2377. tls_read_pkcs12(data, NULL, private_key, passwd)) {
  2378. tls_show_errors(MSG_INFO, __func__,
  2379. "Failed to load private key");
  2380. os_free(passwd);
  2381. ERR_clear_error();
  2382. return -1;
  2383. }
  2384. os_free(passwd);
  2385. ERR_clear_error();
  2386. SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
  2387. if (!SSL_CTX_check_private_key(ssl_ctx)) {
  2388. tls_show_errors(MSG_INFO, __func__,
  2389. "Private key failed verification");
  2390. return -1;
  2391. }
  2392. return 0;
  2393. }
  2394. static int tls_connection_dh(struct tls_connection *conn, const char *dh_file)
  2395. {
  2396. #ifdef OPENSSL_NO_DH
  2397. if (dh_file == NULL)
  2398. return 0;
  2399. wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
  2400. "dh_file specified");
  2401. return -1;
  2402. #else /* OPENSSL_NO_DH */
  2403. DH *dh;
  2404. BIO *bio;
  2405. /* TODO: add support for dh_blob */
  2406. if (dh_file == NULL)
  2407. return 0;
  2408. if (conn == NULL)
  2409. return -1;
  2410. bio = BIO_new_file(dh_file, "r");
  2411. if (bio == NULL) {
  2412. wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
  2413. dh_file, ERR_error_string(ERR_get_error(), NULL));
  2414. return -1;
  2415. }
  2416. dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
  2417. BIO_free(bio);
  2418. #ifndef OPENSSL_NO_DSA
  2419. while (dh == NULL) {
  2420. DSA *dsa;
  2421. wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
  2422. " trying to parse as DSA params", dh_file,
  2423. ERR_error_string(ERR_get_error(), NULL));
  2424. bio = BIO_new_file(dh_file, "r");
  2425. if (bio == NULL)
  2426. break;
  2427. dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  2428. BIO_free(bio);
  2429. if (!dsa) {
  2430. wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
  2431. "'%s': %s", dh_file,
  2432. ERR_error_string(ERR_get_error(), NULL));
  2433. break;
  2434. }
  2435. wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
  2436. dh = DSA_dup_DH(dsa);
  2437. DSA_free(dsa);
  2438. if (dh == NULL) {
  2439. wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
  2440. "params into DH params");
  2441. break;
  2442. }
  2443. break;
  2444. }
  2445. #endif /* !OPENSSL_NO_DSA */
  2446. if (dh == NULL) {
  2447. wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
  2448. "'%s'", dh_file);
  2449. return -1;
  2450. }
  2451. if (SSL_set_tmp_dh(conn->ssl, dh) != 1) {
  2452. wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
  2453. "%s", dh_file,
  2454. ERR_error_string(ERR_get_error(), NULL));
  2455. DH_free(dh);
  2456. return -1;
  2457. }
  2458. DH_free(dh);
  2459. return 0;
  2460. #endif /* OPENSSL_NO_DH */
  2461. }
  2462. static int tls_global_dh(struct tls_data *data, const char *dh_file)
  2463. {
  2464. #ifdef OPENSSL_NO_DH
  2465. if (dh_file == NULL)
  2466. return 0;
  2467. wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
  2468. "dh_file specified");
  2469. return -1;
  2470. #else /* OPENSSL_NO_DH */
  2471. SSL_CTX *ssl_ctx = data->ssl;
  2472. DH *dh;
  2473. BIO *bio;
  2474. /* TODO: add support for dh_blob */
  2475. if (dh_file == NULL)
  2476. return 0;
  2477. if (ssl_ctx == NULL)
  2478. return -1;
  2479. bio = BIO_new_file(dh_file, "r");
  2480. if (bio == NULL) {
  2481. wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
  2482. dh_file, ERR_error_string(ERR_get_error(), NULL));
  2483. return -1;
  2484. }
  2485. dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
  2486. BIO_free(bio);
  2487. #ifndef OPENSSL_NO_DSA
  2488. while (dh == NULL) {
  2489. DSA *dsa;
  2490. wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
  2491. " trying to parse as DSA params", dh_file,
  2492. ERR_error_string(ERR_get_error(), NULL));
  2493. bio = BIO_new_file(dh_file, "r");
  2494. if (bio == NULL)
  2495. break;
  2496. dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  2497. BIO_free(bio);
  2498. if (!dsa) {
  2499. wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
  2500. "'%s': %s", dh_file,
  2501. ERR_error_string(ERR_get_error(), NULL));
  2502. break;
  2503. }
  2504. wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
  2505. dh = DSA_dup_DH(dsa);
  2506. DSA_free(dsa);
  2507. if (dh == NULL) {
  2508. wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
  2509. "params into DH params");
  2510. break;
  2511. }
  2512. break;
  2513. }
  2514. #endif /* !OPENSSL_NO_DSA */
  2515. if (dh == NULL) {
  2516. wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
  2517. "'%s'", dh_file);
  2518. return -1;
  2519. }
  2520. if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
  2521. wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
  2522. "%s", dh_file,
  2523. ERR_error_string(ERR_get_error(), NULL));
  2524. DH_free(dh);
  2525. return -1;
  2526. }
  2527. DH_free(dh);
  2528. return 0;
  2529. #endif /* OPENSSL_NO_DH */
  2530. }
  2531. int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
  2532. struct tls_random *keys)
  2533. {
  2534. SSL *ssl;
  2535. if (conn == NULL || keys == NULL)
  2536. return -1;
  2537. ssl = conn->ssl;
  2538. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  2539. if (ssl == NULL || ssl->s3 == NULL || ssl->session == NULL)
  2540. return -1;
  2541. os_memset(keys, 0, sizeof(*keys));
  2542. keys->client_random = ssl->s3->client_random;
  2543. keys->client_random_len = SSL3_RANDOM_SIZE;
  2544. keys->server_random = ssl->s3->server_random;
  2545. keys->server_random_len = SSL3_RANDOM_SIZE;
  2546. #else
  2547. if (ssl == NULL)
  2548. return -1;
  2549. os_memset(keys, 0, sizeof(*keys));
  2550. keys->client_random = conn->client_random;
  2551. keys->client_random_len = SSL_get_client_random(
  2552. ssl, conn->client_random, sizeof(conn->client_random));
  2553. keys->server_random = conn->server_random;
  2554. keys->server_random_len = SSL_get_server_random(
  2555. ssl, conn->server_random, sizeof(conn->server_random));
  2556. #endif
  2557. return 0;
  2558. }
  2559. #ifndef CONFIG_FIPS
  2560. static int openssl_get_keyblock_size(SSL *ssl)
  2561. {
  2562. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  2563. const EVP_CIPHER *c;
  2564. const EVP_MD *h;
  2565. int md_size;
  2566. if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
  2567. ssl->read_hash == NULL)
  2568. return -1;
  2569. c = ssl->enc_read_ctx->cipher;
  2570. #if OPENSSL_VERSION_NUMBER >= 0x00909000L
  2571. h = EVP_MD_CTX_md(ssl->read_hash);
  2572. #else
  2573. h = ssl->read_hash;
  2574. #endif
  2575. if (h)
  2576. md_size = EVP_MD_size(h);
  2577. #if OPENSSL_VERSION_NUMBER >= 0x10000000L
  2578. else if (ssl->s3)
  2579. md_size = ssl->s3->tmp.new_mac_secret_size;
  2580. #endif
  2581. else
  2582. return -1;
  2583. wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
  2584. "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
  2585. EVP_CIPHER_iv_length(c));
  2586. return 2 * (EVP_CIPHER_key_length(c) +
  2587. md_size +
  2588. EVP_CIPHER_iv_length(c));
  2589. #else
  2590. const SSL_CIPHER *ssl_cipher;
  2591. int cipher, digest;
  2592. const EVP_CIPHER *c;
  2593. const EVP_MD *h;
  2594. ssl_cipher = SSL_get_current_cipher(ssl);
  2595. if (!ssl_cipher)
  2596. return -1;
  2597. cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
  2598. digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
  2599. wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d",
  2600. cipher, digest);
  2601. if (cipher < 0 || digest < 0)
  2602. return -1;
  2603. c = EVP_get_cipherbynid(cipher);
  2604. h = EVP_get_digestbynid(digest);
  2605. if (!c || !h)
  2606. return -1;
  2607. wpa_printf(MSG_DEBUG,
  2608. "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
  2609. EVP_CIPHER_key_length(c), EVP_MD_size(h),
  2610. EVP_CIPHER_iv_length(c));
  2611. return 2 * (EVP_CIPHER_key_length(c) + EVP_MD_size(h) +
  2612. EVP_CIPHER_iv_length(c));
  2613. #endif
  2614. }
  2615. #endif /* CONFIG_FIPS */
  2616. static int openssl_tls_prf(struct tls_connection *conn,
  2617. const char *label, int server_random_first,
  2618. int skip_keyblock, u8 *out, size_t out_len)
  2619. {
  2620. #ifdef CONFIG_FIPS
  2621. wpa_printf(MSG_ERROR, "OpenSSL: TLS keys cannot be exported in FIPS "
  2622. "mode");
  2623. return -1;
  2624. #else /* CONFIG_FIPS */
  2625. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  2626. SSL *ssl;
  2627. u8 *rnd;
  2628. int ret = -1;
  2629. int skip = 0;
  2630. u8 *tmp_out = NULL;
  2631. u8 *_out = out;
  2632. const char *ver;
  2633. /*
  2634. * TLS library did not support key generation, so get the needed TLS
  2635. * session parameters and use an internal implementation of TLS PRF to
  2636. * derive the key.
  2637. */
  2638. if (conn == NULL)
  2639. return -1;
  2640. ssl = conn->ssl;
  2641. if (ssl == NULL || ssl->s3 == NULL || ssl->session == NULL ||
  2642. ssl->session->master_key_length <= 0)
  2643. return -1;
  2644. ver = SSL_get_version(ssl);
  2645. if (skip_keyblock) {
  2646. skip = openssl_get_keyblock_size(ssl);
  2647. if (skip < 0)
  2648. return -1;
  2649. tmp_out = os_malloc(skip + out_len);
  2650. if (!tmp_out)
  2651. return -1;
  2652. _out = tmp_out;
  2653. }
  2654. rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
  2655. if (!rnd) {
  2656. os_free(tmp_out);
  2657. return -1;
  2658. }
  2659. if (server_random_first) {
  2660. os_memcpy(rnd, ssl->s3->server_random, SSL3_RANDOM_SIZE);
  2661. os_memcpy(rnd + SSL3_RANDOM_SIZE, ssl->s3->client_random,
  2662. SSL3_RANDOM_SIZE);
  2663. } else {
  2664. os_memcpy(rnd, ssl->s3->client_random, SSL3_RANDOM_SIZE);
  2665. os_memcpy(rnd + SSL3_RANDOM_SIZE, ssl->s3->server_random,
  2666. SSL3_RANDOM_SIZE);
  2667. }
  2668. if (os_strcmp(ver, "TLSv1.2") == 0) {
  2669. tls_prf_sha256(ssl->session->master_key,
  2670. ssl->session->master_key_length,
  2671. label, rnd, 2 * SSL3_RANDOM_SIZE,
  2672. _out, skip + out_len);
  2673. ret = 0;
  2674. } else if (tls_prf_sha1_md5(ssl->session->master_key,
  2675. ssl->session->master_key_length,
  2676. label, rnd, 2 * SSL3_RANDOM_SIZE,
  2677. _out, skip + out_len) == 0) {
  2678. ret = 0;
  2679. }
  2680. os_free(rnd);
  2681. if (ret == 0 && skip_keyblock)
  2682. os_memcpy(out, _out + skip, out_len);
  2683. bin_clear_free(tmp_out, skip);
  2684. return ret;
  2685. #else
  2686. SSL *ssl;
  2687. SSL_SESSION *sess;
  2688. u8 *rnd;
  2689. int ret = -1;
  2690. int skip = 0;
  2691. u8 *tmp_out = NULL;
  2692. u8 *_out = out;
  2693. unsigned char client_random[SSL3_RANDOM_SIZE];
  2694. unsigned char server_random[SSL3_RANDOM_SIZE];
  2695. unsigned char master_key[64];
  2696. size_t master_key_len;
  2697. const char *ver;
  2698. /*
  2699. * TLS library did not support key generation, so get the needed TLS
  2700. * session parameters and use an internal implementation of TLS PRF to
  2701. * derive the key.
  2702. */
  2703. if (conn == NULL)
  2704. return -1;
  2705. ssl = conn->ssl;
  2706. if (ssl == NULL)
  2707. return -1;
  2708. ver = SSL_get_version(ssl);
  2709. sess = SSL_get_session(ssl);
  2710. if (!ver || !sess)
  2711. return -1;
  2712. if (skip_keyblock) {
  2713. skip = openssl_get_keyblock_size(ssl);
  2714. if (skip < 0)
  2715. return -1;
  2716. tmp_out = os_malloc(skip + out_len);
  2717. if (!tmp_out)
  2718. return -1;
  2719. _out = tmp_out;
  2720. }
  2721. rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
  2722. if (!rnd) {
  2723. os_free(tmp_out);
  2724. return -1;
  2725. }
  2726. SSL_get_client_random(ssl, client_random, sizeof(client_random));
  2727. SSL_get_server_random(ssl, server_random, sizeof(server_random));
  2728. master_key_len = SSL_SESSION_get_master_key(sess, master_key,
  2729. sizeof(master_key));
  2730. if (server_random_first) {
  2731. os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE);
  2732. os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random,
  2733. SSL3_RANDOM_SIZE);
  2734. } else {
  2735. os_memcpy(rnd, client_random, SSL3_RANDOM_SIZE);
  2736. os_memcpy(rnd + SSL3_RANDOM_SIZE, server_random,
  2737. SSL3_RANDOM_SIZE);
  2738. }
  2739. if (os_strcmp(ver, "TLSv1.2") == 0) {
  2740. tls_prf_sha256(master_key, master_key_len,
  2741. label, rnd, 2 * SSL3_RANDOM_SIZE,
  2742. _out, skip + out_len);
  2743. ret = 0;
  2744. } else if (tls_prf_sha1_md5(master_key, master_key_len,
  2745. label, rnd, 2 * SSL3_RANDOM_SIZE,
  2746. _out, skip + out_len) == 0) {
  2747. ret = 0;
  2748. }
  2749. os_memset(master_key, 0, sizeof(master_key));
  2750. os_free(rnd);
  2751. if (ret == 0 && skip_keyblock)
  2752. os_memcpy(out, _out + skip, out_len);
  2753. bin_clear_free(tmp_out, skip);
  2754. return ret;
  2755. #endif
  2756. #endif /* CONFIG_FIPS */
  2757. }
  2758. int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
  2759. const char *label, int server_random_first,
  2760. int skip_keyblock, u8 *out, size_t out_len)
  2761. {
  2762. #if OPENSSL_VERSION_NUMBER >= 0x10001000L
  2763. SSL *ssl;
  2764. if (conn == NULL)
  2765. return -1;
  2766. if (server_random_first || skip_keyblock)
  2767. return openssl_tls_prf(conn, label,
  2768. server_random_first, skip_keyblock,
  2769. out, out_len);
  2770. ssl = conn->ssl;
  2771. if (SSL_export_keying_material(ssl, out, out_len, label,
  2772. os_strlen(label), NULL, 0, 0) == 1) {
  2773. wpa_printf(MSG_DEBUG, "OpenSSL: Using internal PRF");
  2774. return 0;
  2775. }
  2776. #endif
  2777. return openssl_tls_prf(conn, label, server_random_first,
  2778. skip_keyblock, out, out_len);
  2779. }
  2780. static struct wpabuf *
  2781. openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data,
  2782. int server)
  2783. {
  2784. int res;
  2785. struct wpabuf *out_data;
  2786. /*
  2787. * Give TLS handshake data from the server (if available) to OpenSSL
  2788. * for processing.
  2789. */
  2790. if (in_data && wpabuf_len(in_data) > 0 &&
  2791. BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
  2792. < 0) {
  2793. tls_show_errors(MSG_INFO, __func__,
  2794. "Handshake failed - BIO_write");
  2795. return NULL;
  2796. }
  2797. /* Initiate TLS handshake or continue the existing handshake */
  2798. if (server)
  2799. res = SSL_accept(conn->ssl);
  2800. else
  2801. res = SSL_connect(conn->ssl);
  2802. if (res != 1) {
  2803. int err = SSL_get_error(conn->ssl, res);
  2804. if (err == SSL_ERROR_WANT_READ)
  2805. wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
  2806. "more data");
  2807. else if (err == SSL_ERROR_WANT_WRITE)
  2808. wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
  2809. "write");
  2810. else {
  2811. tls_show_errors(MSG_INFO, __func__, "SSL_connect");
  2812. conn->failed++;
  2813. }
  2814. }
  2815. /* Get the TLS handshake data to be sent to the server */
  2816. res = BIO_ctrl_pending(conn->ssl_out);
  2817. wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
  2818. out_data = wpabuf_alloc(res);
  2819. if (out_data == NULL) {
  2820. wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
  2821. "handshake output (%d bytes)", res);
  2822. if (BIO_reset(conn->ssl_out) < 0) {
  2823. tls_show_errors(MSG_INFO, __func__,
  2824. "BIO_reset failed");
  2825. }
  2826. return NULL;
  2827. }
  2828. res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
  2829. res);
  2830. if (res < 0) {
  2831. tls_show_errors(MSG_INFO, __func__,
  2832. "Handshake failed - BIO_read");
  2833. if (BIO_reset(conn->ssl_out) < 0) {
  2834. tls_show_errors(MSG_INFO, __func__,
  2835. "BIO_reset failed");
  2836. }
  2837. wpabuf_free(out_data);
  2838. return NULL;
  2839. }
  2840. wpabuf_put(out_data, res);
  2841. return out_data;
  2842. }
  2843. static struct wpabuf *
  2844. openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
  2845. {
  2846. struct wpabuf *appl_data;
  2847. int res;
  2848. appl_data = wpabuf_alloc(max_len + 100);
  2849. if (appl_data == NULL)
  2850. return NULL;
  2851. res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
  2852. wpabuf_size(appl_data));
  2853. if (res < 0) {
  2854. int err = SSL_get_error(conn->ssl, res);
  2855. if (err == SSL_ERROR_WANT_READ ||
  2856. err == SSL_ERROR_WANT_WRITE) {
  2857. wpa_printf(MSG_DEBUG, "SSL: No Application Data "
  2858. "included");
  2859. } else {
  2860. tls_show_errors(MSG_INFO, __func__,
  2861. "Failed to read possible "
  2862. "Application Data");
  2863. }
  2864. wpabuf_free(appl_data);
  2865. return NULL;
  2866. }
  2867. wpabuf_put(appl_data, res);
  2868. wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
  2869. "message", appl_data);
  2870. return appl_data;
  2871. }
  2872. static struct wpabuf *
  2873. openssl_connection_handshake(struct tls_connection *conn,
  2874. const struct wpabuf *in_data,
  2875. struct wpabuf **appl_data, int server)
  2876. {
  2877. struct wpabuf *out_data;
  2878. if (appl_data)
  2879. *appl_data = NULL;
  2880. out_data = openssl_handshake(conn, in_data, server);
  2881. if (out_data == NULL)
  2882. return NULL;
  2883. if (conn->invalid_hb_used) {
  2884. wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
  2885. wpabuf_free(out_data);
  2886. return NULL;
  2887. }
  2888. if (SSL_is_init_finished(conn->ssl)) {
  2889. wpa_printf(MSG_DEBUG,
  2890. "OpenSSL: Handshake finished - resumed=%d",
  2891. tls_connection_resumed(conn->ssl_ctx, conn));
  2892. if (appl_data && in_data)
  2893. *appl_data = openssl_get_appl_data(conn,
  2894. wpabuf_len(in_data));
  2895. }
  2896. if (conn->invalid_hb_used) {
  2897. wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
  2898. if (appl_data) {
  2899. wpabuf_free(*appl_data);
  2900. *appl_data = NULL;
  2901. }
  2902. wpabuf_free(out_data);
  2903. return NULL;
  2904. }
  2905. return out_data;
  2906. }
  2907. struct wpabuf *
  2908. tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
  2909. const struct wpabuf *in_data,
  2910. struct wpabuf **appl_data)
  2911. {
  2912. return openssl_connection_handshake(conn, in_data, appl_data, 0);
  2913. }
  2914. struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
  2915. struct tls_connection *conn,
  2916. const struct wpabuf *in_data,
  2917. struct wpabuf **appl_data)
  2918. {
  2919. return openssl_connection_handshake(conn, in_data, appl_data, 1);
  2920. }
  2921. struct wpabuf * tls_connection_encrypt(void *tls_ctx,
  2922. struct tls_connection *conn,
  2923. const struct wpabuf *in_data)
  2924. {
  2925. int res;
  2926. struct wpabuf *buf;
  2927. if (conn == NULL)
  2928. return NULL;
  2929. /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
  2930. if ((res = BIO_reset(conn->ssl_in)) < 0 ||
  2931. (res = BIO_reset(conn->ssl_out)) < 0) {
  2932. tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
  2933. return NULL;
  2934. }
  2935. res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
  2936. if (res < 0) {
  2937. tls_show_errors(MSG_INFO, __func__,
  2938. "Encryption failed - SSL_write");
  2939. return NULL;
  2940. }
  2941. /* Read encrypted data to be sent to the server */
  2942. buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
  2943. if (buf == NULL)
  2944. return NULL;
  2945. res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
  2946. if (res < 0) {
  2947. tls_show_errors(MSG_INFO, __func__,
  2948. "Encryption failed - BIO_read");
  2949. wpabuf_free(buf);
  2950. return NULL;
  2951. }
  2952. wpabuf_put(buf, res);
  2953. return buf;
  2954. }
  2955. struct wpabuf * tls_connection_decrypt(void *tls_ctx,
  2956. struct tls_connection *conn,
  2957. const struct wpabuf *in_data)
  2958. {
  2959. int res;
  2960. struct wpabuf *buf;
  2961. /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
  2962. res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
  2963. wpabuf_len(in_data));
  2964. if (res < 0) {
  2965. tls_show_errors(MSG_INFO, __func__,
  2966. "Decryption failed - BIO_write");
  2967. return NULL;
  2968. }
  2969. if (BIO_reset(conn->ssl_out) < 0) {
  2970. tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
  2971. return NULL;
  2972. }
  2973. /* Read decrypted data for further processing */
  2974. /*
  2975. * Even though we try to disable TLS compression, it is possible that
  2976. * this cannot be done with all TLS libraries. Add extra buffer space
  2977. * to handle the possibility of the decrypted data being longer than
  2978. * input data.
  2979. */
  2980. buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
  2981. if (buf == NULL)
  2982. return NULL;
  2983. res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
  2984. if (res < 0) {
  2985. tls_show_errors(MSG_INFO, __func__,
  2986. "Decryption failed - SSL_read");
  2987. wpabuf_free(buf);
  2988. return NULL;
  2989. }
  2990. wpabuf_put(buf, res);
  2991. if (conn->invalid_hb_used) {
  2992. wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
  2993. wpabuf_free(buf);
  2994. return NULL;
  2995. }
  2996. return buf;
  2997. }
  2998. int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
  2999. {
  3000. #if OPENSSL_VERSION_NUMBER >= 0x10001000L
  3001. return conn ? SSL_cache_hit(conn->ssl) : 0;
  3002. #else
  3003. return conn ? conn->ssl->hit : 0;
  3004. #endif
  3005. }
  3006. int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
  3007. u8 *ciphers)
  3008. {
  3009. char buf[100], *pos, *end;
  3010. u8 *c;
  3011. int ret;
  3012. if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
  3013. return -1;
  3014. buf[0] = '\0';
  3015. pos = buf;
  3016. end = pos + sizeof(buf);
  3017. c = ciphers;
  3018. while (*c != TLS_CIPHER_NONE) {
  3019. const char *suite;
  3020. switch (*c) {
  3021. case TLS_CIPHER_RC4_SHA:
  3022. suite = "RC4-SHA";
  3023. break;
  3024. case TLS_CIPHER_AES128_SHA:
  3025. suite = "AES128-SHA";
  3026. break;
  3027. case TLS_CIPHER_RSA_DHE_AES128_SHA:
  3028. suite = "DHE-RSA-AES128-SHA";
  3029. break;
  3030. case TLS_CIPHER_ANON_DH_AES128_SHA:
  3031. suite = "ADH-AES128-SHA";
  3032. break;
  3033. default:
  3034. wpa_printf(MSG_DEBUG, "TLS: Unsupported "
  3035. "cipher selection: %d", *c);
  3036. return -1;
  3037. }
  3038. ret = os_snprintf(pos, end - pos, ":%s", suite);
  3039. if (os_snprintf_error(end - pos, ret))
  3040. break;
  3041. pos += ret;
  3042. c++;
  3043. }
  3044. wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
  3045. #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
  3046. #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
  3047. if (os_strstr(buf, ":ADH-")) {
  3048. /*
  3049. * Need to drop to security level 0 to allow anonymous
  3050. * cipher suites for EAP-FAST.
  3051. */
  3052. SSL_set_security_level(conn->ssl, 0);
  3053. } else if (SSL_get_security_level(conn->ssl) == 0) {
  3054. /* Force at least security level 1 */
  3055. SSL_set_security_level(conn->ssl, 1);
  3056. }
  3057. #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
  3058. #endif
  3059. if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
  3060. tls_show_errors(MSG_INFO, __func__,
  3061. "Cipher suite configuration failed");
  3062. return -1;
  3063. }
  3064. return 0;
  3065. }
  3066. int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
  3067. char *buf, size_t buflen)
  3068. {
  3069. const char *name;
  3070. if (conn == NULL || conn->ssl == NULL)
  3071. return -1;
  3072. name = SSL_get_version(conn->ssl);
  3073. if (name == NULL)
  3074. return -1;
  3075. os_strlcpy(buf, name, buflen);
  3076. return 0;
  3077. }
  3078. int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
  3079. char *buf, size_t buflen)
  3080. {
  3081. const char *name;
  3082. if (conn == NULL || conn->ssl == NULL)
  3083. return -1;
  3084. name = SSL_get_cipher(conn->ssl);
  3085. if (name == NULL)
  3086. return -1;
  3087. os_strlcpy(buf, name, buflen);
  3088. return 0;
  3089. }
  3090. int tls_connection_enable_workaround(void *ssl_ctx,
  3091. struct tls_connection *conn)
  3092. {
  3093. SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
  3094. return 0;
  3095. }
  3096. #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
  3097. /* ClientHello TLS extensions require a patch to openssl, so this function is
  3098. * commented out unless explicitly needed for EAP-FAST in order to be able to
  3099. * build this file with unmodified openssl. */
  3100. int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
  3101. int ext_type, const u8 *data,
  3102. size_t data_len)
  3103. {
  3104. if (conn == NULL || conn->ssl == NULL || ext_type != 35)
  3105. return -1;
  3106. if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
  3107. data_len) != 1)
  3108. return -1;
  3109. return 0;
  3110. }
  3111. #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
  3112. int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
  3113. {
  3114. if (conn == NULL)
  3115. return -1;
  3116. return conn->failed;
  3117. }
  3118. int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
  3119. {
  3120. if (conn == NULL)
  3121. return -1;
  3122. return conn->read_alerts;
  3123. }
  3124. int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
  3125. {
  3126. if (conn == NULL)
  3127. return -1;
  3128. return conn->write_alerts;
  3129. }
  3130. #ifdef HAVE_OCSP
  3131. static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
  3132. {
  3133. #ifndef CONFIG_NO_STDOUT_DEBUG
  3134. BIO *out;
  3135. size_t rlen;
  3136. char *txt;
  3137. int res;
  3138. if (wpa_debug_level > MSG_DEBUG)
  3139. return;
  3140. out = BIO_new(BIO_s_mem());
  3141. if (!out)
  3142. return;
  3143. OCSP_RESPONSE_print(out, rsp, 0);
  3144. rlen = BIO_ctrl_pending(out);
  3145. txt = os_malloc(rlen + 1);
  3146. if (!txt) {
  3147. BIO_free(out);
  3148. return;
  3149. }
  3150. res = BIO_read(out, txt, rlen);
  3151. if (res > 0) {
  3152. txt[res] = '\0';
  3153. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt);
  3154. }
  3155. os_free(txt);
  3156. BIO_free(out);
  3157. #endif /* CONFIG_NO_STDOUT_DEBUG */
  3158. }
  3159. static void debug_print_cert(X509 *cert, const char *title)
  3160. {
  3161. #ifndef CONFIG_NO_STDOUT_DEBUG
  3162. BIO *out;
  3163. size_t rlen;
  3164. char *txt;
  3165. int res;
  3166. if (wpa_debug_level > MSG_DEBUG)
  3167. return;
  3168. out = BIO_new(BIO_s_mem());
  3169. if (!out)
  3170. return;
  3171. X509_print(out, cert);
  3172. rlen = BIO_ctrl_pending(out);
  3173. txt = os_malloc(rlen + 1);
  3174. if (!txt) {
  3175. BIO_free(out);
  3176. return;
  3177. }
  3178. res = BIO_read(out, txt, rlen);
  3179. if (res > 0) {
  3180. txt[res] = '\0';
  3181. wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt);
  3182. }
  3183. os_free(txt);
  3184. BIO_free(out);
  3185. #endif /* CONFIG_NO_STDOUT_DEBUG */
  3186. }
  3187. static int ocsp_resp_cb(SSL *s, void *arg)
  3188. {
  3189. struct tls_connection *conn = arg;
  3190. const unsigned char *p;
  3191. int len, status, reason;
  3192. OCSP_RESPONSE *rsp;
  3193. OCSP_BASICRESP *basic;
  3194. OCSP_CERTID *id;
  3195. ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
  3196. X509_STORE *store;
  3197. STACK_OF(X509) *certs = NULL;
  3198. len = SSL_get_tlsext_status_ocsp_resp(s, &p);
  3199. if (!p) {
  3200. wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
  3201. return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
  3202. }
  3203. wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
  3204. rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
  3205. if (!rsp) {
  3206. wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
  3207. return 0;
  3208. }
  3209. ocsp_debug_print_resp(rsp);
  3210. status = OCSP_response_status(rsp);
  3211. if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
  3212. wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
  3213. status, OCSP_response_status_str(status));
  3214. return 0;
  3215. }
  3216. basic = OCSP_response_get1_basic(rsp);
  3217. if (!basic) {
  3218. wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
  3219. return 0;
  3220. }
  3221. store = SSL_CTX_get_cert_store(conn->ssl_ctx);
  3222. if (conn->peer_issuer) {
  3223. debug_print_cert(conn->peer_issuer, "Add OCSP issuer");
  3224. if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) {
  3225. tls_show_errors(MSG_INFO, __func__,
  3226. "OpenSSL: Could not add issuer to certificate store");
  3227. }
  3228. certs = sk_X509_new_null();
  3229. if (certs) {
  3230. X509 *cert;
  3231. cert = X509_dup(conn->peer_issuer);
  3232. if (cert && !sk_X509_push(certs, cert)) {
  3233. tls_show_errors(
  3234. MSG_INFO, __func__,
  3235. "OpenSSL: Could not add issuer to OCSP responder trust store");
  3236. X509_free(cert);
  3237. sk_X509_free(certs);
  3238. certs = NULL;
  3239. }
  3240. if (certs && conn->peer_issuer_issuer) {
  3241. cert = X509_dup(conn->peer_issuer_issuer);
  3242. if (cert && !sk_X509_push(certs, cert)) {
  3243. tls_show_errors(
  3244. MSG_INFO, __func__,
  3245. "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
  3246. X509_free(cert);
  3247. }
  3248. }
  3249. }
  3250. }
  3251. status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
  3252. sk_X509_pop_free(certs, X509_free);
  3253. if (status <= 0) {
  3254. tls_show_errors(MSG_INFO, __func__,
  3255. "OpenSSL: OCSP response failed verification");
  3256. OCSP_BASICRESP_free(basic);
  3257. OCSP_RESPONSE_free(rsp);
  3258. return 0;
  3259. }
  3260. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
  3261. if (!conn->peer_cert) {
  3262. wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
  3263. OCSP_BASICRESP_free(basic);
  3264. OCSP_RESPONSE_free(rsp);
  3265. return 0;
  3266. }
  3267. if (!conn->peer_issuer) {
  3268. wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
  3269. OCSP_BASICRESP_free(basic);
  3270. OCSP_RESPONSE_free(rsp);
  3271. return 0;
  3272. }
  3273. id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer);
  3274. if (!id) {
  3275. wpa_printf(MSG_DEBUG, "OpenSSL: Could not create OCSP certificate identifier");
  3276. OCSP_BASICRESP_free(basic);
  3277. OCSP_RESPONSE_free(rsp);
  3278. return 0;
  3279. }
  3280. if (!OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
  3281. &this_update, &next_update)) {
  3282. wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
  3283. (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" :
  3284. " (OCSP not required)");
  3285. OCSP_BASICRESP_free(basic);
  3286. OCSP_RESPONSE_free(rsp);
  3287. return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
  3288. }
  3289. if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
  3290. tls_show_errors(MSG_INFO, __func__,
  3291. "OpenSSL: OCSP status times invalid");
  3292. OCSP_BASICRESP_free(basic);
  3293. OCSP_RESPONSE_free(rsp);
  3294. return 0;
  3295. }
  3296. OCSP_BASICRESP_free(basic);
  3297. OCSP_RESPONSE_free(rsp);
  3298. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
  3299. OCSP_cert_status_str(status));
  3300. if (status == V_OCSP_CERTSTATUS_GOOD)
  3301. return 1;
  3302. if (status == V_OCSP_CERTSTATUS_REVOKED)
  3303. return 0;
  3304. if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
  3305. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
  3306. return 0;
  3307. }
  3308. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue");
  3309. return 1;
  3310. }
  3311. static int ocsp_status_cb(SSL *s, void *arg)
  3312. {
  3313. char *tmp;
  3314. char *resp;
  3315. size_t len;
  3316. if (tls_global->ocsp_stapling_response == NULL) {
  3317. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured");
  3318. return SSL_TLSEXT_ERR_OK;
  3319. }
  3320. resp = os_readfile(tls_global->ocsp_stapling_response, &len);
  3321. if (resp == NULL) {
  3322. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file");
  3323. /* TODO: Build OCSPResponse with responseStatus = internalError
  3324. */
  3325. return SSL_TLSEXT_ERR_OK;
  3326. }
  3327. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response");
  3328. tmp = OPENSSL_malloc(len);
  3329. if (tmp == NULL) {
  3330. os_free(resp);
  3331. return SSL_TLSEXT_ERR_ALERT_FATAL;
  3332. }
  3333. os_memcpy(tmp, resp, len);
  3334. os_free(resp);
  3335. SSL_set_tlsext_status_ocsp_resp(s, tmp, len);
  3336. return SSL_TLSEXT_ERR_OK;
  3337. }
  3338. #endif /* HAVE_OCSP */
  3339. int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
  3340. const struct tls_connection_params *params)
  3341. {
  3342. struct tls_data *data = tls_ctx;
  3343. int ret;
  3344. unsigned long err;
  3345. int can_pkcs11 = 0;
  3346. const char *key_id = params->key_id;
  3347. const char *cert_id = params->cert_id;
  3348. const char *ca_cert_id = params->ca_cert_id;
  3349. const char *engine_id = params->engine ? params->engine_id : NULL;
  3350. if (conn == NULL)
  3351. return -1;
  3352. if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
  3353. wpa_printf(MSG_INFO,
  3354. "OpenSSL: ocsp=3 not supported");
  3355. return -1;
  3356. }
  3357. /*
  3358. * If the engine isn't explicitly configured, and any of the
  3359. * cert/key fields are actually PKCS#11 URIs, then automatically
  3360. * use the PKCS#11 ENGINE.
  3361. */
  3362. if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0)
  3363. can_pkcs11 = 1;
  3364. if (!key_id && params->private_key && can_pkcs11 &&
  3365. os_strncmp(params->private_key, "pkcs11:", 7) == 0) {
  3366. can_pkcs11 = 2;
  3367. key_id = params->private_key;
  3368. }
  3369. if (!cert_id && params->client_cert && can_pkcs11 &&
  3370. os_strncmp(params->client_cert, "pkcs11:", 7) == 0) {
  3371. can_pkcs11 = 2;
  3372. cert_id = params->client_cert;
  3373. }
  3374. if (!ca_cert_id && params->ca_cert && can_pkcs11 &&
  3375. os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) {
  3376. can_pkcs11 = 2;
  3377. ca_cert_id = params->ca_cert;
  3378. }
  3379. /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
  3380. if (can_pkcs11 == 2 && !engine_id)
  3381. engine_id = "pkcs11";
  3382. #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
  3383. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  3384. if (params->flags & TLS_CONN_EAP_FAST) {
  3385. wpa_printf(MSG_DEBUG,
  3386. "OpenSSL: Use TLSv1_method() for EAP-FAST");
  3387. if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) {
  3388. tls_show_errors(MSG_INFO, __func__,
  3389. "Failed to set TLSv1_method() for EAP-FAST");
  3390. return -1;
  3391. }
  3392. }
  3393. #endif
  3394. #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
  3395. while ((err = ERR_get_error())) {
  3396. wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
  3397. __func__, ERR_error_string(err, NULL));
  3398. }
  3399. if (engine_id) {
  3400. wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine");
  3401. ret = tls_engine_init(conn, engine_id, params->pin,
  3402. key_id, cert_id, ca_cert_id);
  3403. if (ret)
  3404. return ret;
  3405. }
  3406. if (tls_connection_set_subject_match(conn,
  3407. params->subject_match,
  3408. params->altsubject_match,
  3409. params->suffix_match,
  3410. params->domain_match))
  3411. return -1;
  3412. if (engine_id && ca_cert_id) {
  3413. if (tls_connection_engine_ca_cert(data, conn, ca_cert_id))
  3414. return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
  3415. } else if (tls_connection_ca_cert(data, conn, params->ca_cert,
  3416. params->ca_cert_blob,
  3417. params->ca_cert_blob_len,
  3418. params->ca_path))
  3419. return -1;
  3420. if (engine_id && cert_id) {
  3421. if (tls_connection_engine_client_cert(conn, cert_id))
  3422. return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
  3423. } else if (tls_connection_client_cert(conn, params->client_cert,
  3424. params->client_cert_blob,
  3425. params->client_cert_blob_len))
  3426. return -1;
  3427. if (engine_id && key_id) {
  3428. wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
  3429. if (tls_connection_engine_private_key(conn))
  3430. return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
  3431. } else if (tls_connection_private_key(data, conn,
  3432. params->private_key,
  3433. params->private_key_passwd,
  3434. params->private_key_blob,
  3435. params->private_key_blob_len)) {
  3436. wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
  3437. params->private_key);
  3438. return -1;
  3439. }
  3440. if (tls_connection_dh(conn, params->dh_file)) {
  3441. wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
  3442. params->dh_file);
  3443. return -1;
  3444. }
  3445. if (params->openssl_ciphers &&
  3446. SSL_set_cipher_list(conn->ssl, params->openssl_ciphers) != 1) {
  3447. wpa_printf(MSG_INFO,
  3448. "OpenSSL: Failed to set cipher string '%s'",
  3449. params->openssl_ciphers);
  3450. return -1;
  3451. }
  3452. tls_set_conn_flags(conn->ssl, params->flags);
  3453. #ifdef OPENSSL_IS_BORINGSSL
  3454. if (params->flags & TLS_CONN_REQUEST_OCSP) {
  3455. SSL_enable_ocsp_stapling(conn->ssl);
  3456. }
  3457. #else /* OPENSSL_IS_BORINGSSL */
  3458. #ifdef HAVE_OCSP
  3459. if (params->flags & TLS_CONN_REQUEST_OCSP) {
  3460. SSL_CTX *ssl_ctx = data->ssl;
  3461. SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp);
  3462. SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
  3463. SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn);
  3464. }
  3465. #else /* HAVE_OCSP */
  3466. if (params->flags & TLS_CONN_REQUIRE_OCSP) {
  3467. wpa_printf(MSG_INFO,
  3468. "OpenSSL: No OCSP support included - reject configuration");
  3469. return -1;
  3470. }
  3471. if (params->flags & TLS_CONN_REQUEST_OCSP) {
  3472. wpa_printf(MSG_DEBUG,
  3473. "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
  3474. }
  3475. #endif /* HAVE_OCSP */
  3476. #endif /* OPENSSL_IS_BORINGSSL */
  3477. conn->flags = params->flags;
  3478. tls_get_errors(data);
  3479. return 0;
  3480. }
  3481. int tls_global_set_params(void *tls_ctx,
  3482. const struct tls_connection_params *params)
  3483. {
  3484. struct tls_data *data = tls_ctx;
  3485. SSL_CTX *ssl_ctx = data->ssl;
  3486. unsigned long err;
  3487. while ((err = ERR_get_error())) {
  3488. wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
  3489. __func__, ERR_error_string(err, NULL));
  3490. }
  3491. if (tls_global_ca_cert(data, params->ca_cert) ||
  3492. tls_global_client_cert(data, params->client_cert) ||
  3493. tls_global_private_key(data, params->private_key,
  3494. params->private_key_passwd) ||
  3495. tls_global_dh(data, params->dh_file)) {
  3496. wpa_printf(MSG_INFO, "TLS: Failed to set global parameters");
  3497. return -1;
  3498. }
  3499. if (params->openssl_ciphers &&
  3500. SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) {
  3501. wpa_printf(MSG_INFO,
  3502. "OpenSSL: Failed to set cipher string '%s'",
  3503. params->openssl_ciphers);
  3504. return -1;
  3505. }
  3506. #ifdef SSL_OP_NO_TICKET
  3507. if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET)
  3508. SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
  3509. #ifdef SSL_CTX_clear_options
  3510. else
  3511. SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET);
  3512. #endif /* SSL_clear_options */
  3513. #endif /* SSL_OP_NO_TICKET */
  3514. #ifdef HAVE_OCSP
  3515. SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb);
  3516. SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx);
  3517. os_free(tls_global->ocsp_stapling_response);
  3518. if (params->ocsp_stapling_response)
  3519. tls_global->ocsp_stapling_response =
  3520. os_strdup(params->ocsp_stapling_response);
  3521. else
  3522. tls_global->ocsp_stapling_response = NULL;
  3523. #endif /* HAVE_OCSP */
  3524. return 0;
  3525. }
  3526. #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
  3527. /* Pre-shared secred requires a patch to openssl, so this function is
  3528. * commented out unless explicitly needed for EAP-FAST in order to be able to
  3529. * build this file with unmodified openssl. */
  3530. #ifdef OPENSSL_IS_BORINGSSL
  3531. static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
  3532. STACK_OF(SSL_CIPHER) *peer_ciphers,
  3533. const SSL_CIPHER **cipher, void *arg)
  3534. #else /* OPENSSL_IS_BORINGSSL */
  3535. static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
  3536. STACK_OF(SSL_CIPHER) *peer_ciphers,
  3537. SSL_CIPHER **cipher, void *arg)
  3538. #endif /* OPENSSL_IS_BORINGSSL */
  3539. {
  3540. struct tls_connection *conn = arg;
  3541. int ret;
  3542. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  3543. if (conn == NULL || conn->session_ticket_cb == NULL)
  3544. return 0;
  3545. ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
  3546. conn->session_ticket,
  3547. conn->session_ticket_len,
  3548. s->s3->client_random,
  3549. s->s3->server_random, secret);
  3550. #else
  3551. unsigned char client_random[SSL3_RANDOM_SIZE];
  3552. unsigned char server_random[SSL3_RANDOM_SIZE];
  3553. if (conn == NULL || conn->session_ticket_cb == NULL)
  3554. return 0;
  3555. SSL_get_client_random(s, client_random, sizeof(client_random));
  3556. SSL_get_server_random(s, server_random, sizeof(server_random));
  3557. ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
  3558. conn->session_ticket,
  3559. conn->session_ticket_len,
  3560. client_random,
  3561. server_random, secret);
  3562. #endif
  3563. os_free(conn->session_ticket);
  3564. conn->session_ticket = NULL;
  3565. if (ret <= 0)
  3566. return 0;
  3567. *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
  3568. return 1;
  3569. }
  3570. static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
  3571. int len, void *arg)
  3572. {
  3573. struct tls_connection *conn = arg;
  3574. if (conn == NULL || conn->session_ticket_cb == NULL)
  3575. return 0;
  3576. wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
  3577. os_free(conn->session_ticket);
  3578. conn->session_ticket = NULL;
  3579. wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
  3580. "extension", data, len);
  3581. conn->session_ticket = os_malloc(len);
  3582. if (conn->session_ticket == NULL)
  3583. return 0;
  3584. os_memcpy(conn->session_ticket, data, len);
  3585. conn->session_ticket_len = len;
  3586. return 1;
  3587. }
  3588. #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
  3589. int tls_connection_set_session_ticket_cb(void *tls_ctx,
  3590. struct tls_connection *conn,
  3591. tls_session_ticket_cb cb,
  3592. void *ctx)
  3593. {
  3594. #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
  3595. conn->session_ticket_cb = cb;
  3596. conn->session_ticket_cb_ctx = ctx;
  3597. if (cb) {
  3598. if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
  3599. conn) != 1)
  3600. return -1;
  3601. SSL_set_session_ticket_ext_cb(conn->ssl,
  3602. tls_session_ticket_ext_cb, conn);
  3603. } else {
  3604. if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
  3605. return -1;
  3606. SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
  3607. }
  3608. return 0;
  3609. #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
  3610. return -1;
  3611. #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
  3612. }
  3613. int tls_get_library_version(char *buf, size_t buf_len)
  3614. {
  3615. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  3616. return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
  3617. OPENSSL_VERSION_TEXT,
  3618. OpenSSL_version(OPENSSL_VERSION));
  3619. #else
  3620. return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
  3621. OPENSSL_VERSION_TEXT,
  3622. SSLeay_version(SSLEAY_VERSION));
  3623. #endif
  3624. }
  3625. void tls_connection_set_success_data(struct tls_connection *conn,
  3626. struct wpabuf *data)
  3627. {
  3628. SSL_SESSION *sess;
  3629. struct wpabuf *old;
  3630. if (tls_ex_idx_session < 0)
  3631. goto fail;
  3632. sess = SSL_get_session(conn->ssl);
  3633. if (!sess)
  3634. goto fail;
  3635. old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
  3636. if (old) {
  3637. wpa_printf(MSG_DEBUG, "OpenSSL: Replacing old success data %p",
  3638. old);
  3639. wpabuf_free(old);
  3640. }
  3641. if (SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
  3642. goto fail;
  3643. wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p", data);
  3644. conn->success_data = 1;
  3645. return;
  3646. fail:
  3647. wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data");
  3648. wpabuf_free(data);
  3649. }
  3650. void tls_connection_set_success_data_resumed(struct tls_connection *conn)
  3651. {
  3652. wpa_printf(MSG_DEBUG,
  3653. "OpenSSL: Success data accepted for resumed session");
  3654. conn->success_data = 1;
  3655. }
  3656. const struct wpabuf *
  3657. tls_connection_get_success_data(struct tls_connection *conn)
  3658. {
  3659. SSL_SESSION *sess;
  3660. if (tls_ex_idx_session < 0 ||
  3661. !(sess = SSL_get_session(conn->ssl)))
  3662. return NULL;
  3663. return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
  3664. }
  3665. void tls_connection_remove_session(struct tls_connection *conn)
  3666. {
  3667. SSL_SESSION *sess;
  3668. sess = SSL_get_session(conn->ssl);
  3669. if (!sess)
  3670. return;
  3671. if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1)
  3672. wpa_printf(MSG_DEBUG,
  3673. "OpenSSL: Session was not cached");
  3674. else
  3675. wpa_printf(MSG_DEBUG,
  3676. "OpenSSL: Removed cached session to disable session resumption");
  3677. }