tls_openssl.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003
  1. /*
  2. * SSL/TLS interface functions for OpenSSL
  3. * Copyright (c) 2004-2011, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #ifndef CONFIG_SMARTCARD
  16. #ifndef OPENSSL_NO_ENGINE
  17. #define OPENSSL_NO_ENGINE
  18. #endif
  19. #endif
  20. #include <openssl/ssl.h>
  21. #include <openssl/err.h>
  22. #include <openssl/pkcs12.h>
  23. #include <openssl/x509v3.h>
  24. #ifndef OPENSSL_NO_ENGINE
  25. #include <openssl/engine.h>
  26. #endif /* OPENSSL_NO_ENGINE */
  27. #ifdef ANDROID
  28. #include <openssl/pem.h>
  29. #include "keystore_get.h"
  30. #endif /* ANDROID */
  31. #include "common.h"
  32. #include "crypto.h"
  33. #include "tls.h"
  34. #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
  35. #define OPENSSL_d2i_TYPE const unsigned char **
  36. #else
  37. #define OPENSSL_d2i_TYPE unsigned char **
  38. #endif
  39. #ifdef SSL_F_SSL_SET_SESSION_TICKET_EXT
  40. #ifdef SSL_OP_NO_TICKET
  41. /*
  42. * Session ticket override patch was merged into OpenSSL 0.9.9 tree on
  43. * 2008-11-15. This version uses a bit different API compared to the old patch.
  44. */
  45. #define CONFIG_OPENSSL_TICKET_OVERRIDE
  46. #endif
  47. #endif
  48. static int tls_openssl_ref_count = 0;
  49. struct tls_global {
  50. void (*event_cb)(void *ctx, enum tls_event ev,
  51. union tls_event_data *data);
  52. void *cb_ctx;
  53. };
  54. static struct tls_global *tls_global = NULL;
  55. struct tls_connection {
  56. SSL *ssl;
  57. BIO *ssl_in, *ssl_out;
  58. #ifndef OPENSSL_NO_ENGINE
  59. ENGINE *engine; /* functional reference to the engine */
  60. EVP_PKEY *private_key; /* the private key if using engine */
  61. #endif /* OPENSSL_NO_ENGINE */
  62. char *subject_match, *altsubject_match;
  63. int read_alerts, write_alerts, failed;
  64. tls_session_ticket_cb session_ticket_cb;
  65. void *session_ticket_cb_ctx;
  66. /* SessionTicket received from OpenSSL hello_extension_cb (server) */
  67. u8 *session_ticket;
  68. size_t session_ticket_len;
  69. unsigned int ca_cert_verify:1;
  70. unsigned int cert_probe:1;
  71. unsigned int server_cert_only:1;
  72. u8 srv_cert_hash[32];
  73. unsigned int flags;
  74. };
  75. #ifdef CONFIG_NO_STDOUT_DEBUG
  76. static void _tls_show_errors(void)
  77. {
  78. unsigned long err;
  79. while ((err = ERR_get_error())) {
  80. /* Just ignore the errors, since stdout is disabled */
  81. }
  82. }
  83. #define tls_show_errors(l, f, t) _tls_show_errors()
  84. #else /* CONFIG_NO_STDOUT_DEBUG */
  85. static void tls_show_errors(int level, const char *func, const char *txt)
  86. {
  87. unsigned long err;
  88. wpa_printf(level, "OpenSSL: %s - %s %s",
  89. func, txt, ERR_error_string(ERR_get_error(), NULL));
  90. while ((err = ERR_get_error())) {
  91. wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
  92. ERR_error_string(err, NULL));
  93. }
  94. }
  95. #endif /* CONFIG_NO_STDOUT_DEBUG */
  96. #ifdef CONFIG_NATIVE_WINDOWS
  97. /* Windows CryptoAPI and access to certificate stores */
  98. #include <wincrypt.h>
  99. #ifdef __MINGW32_VERSION
  100. /*
  101. * MinGW does not yet include all the needed definitions for CryptoAPI, so
  102. * define here whatever extra is needed.
  103. */
  104. #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
  105. #define CERT_STORE_READONLY_FLAG 0x00008000
  106. #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
  107. #endif /* __MINGW32_VERSION */
  108. struct cryptoapi_rsa_data {
  109. const CERT_CONTEXT *cert;
  110. HCRYPTPROV crypt_prov;
  111. DWORD key_spec;
  112. BOOL free_crypt_prov;
  113. };
  114. static void cryptoapi_error(const char *msg)
  115. {
  116. wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
  117. msg, (unsigned int) GetLastError());
  118. }
  119. static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from,
  120. unsigned char *to, RSA *rsa, int padding)
  121. {
  122. wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
  123. return 0;
  124. }
  125. static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from,
  126. unsigned char *to, RSA *rsa, int padding)
  127. {
  128. wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
  129. return 0;
  130. }
  131. static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
  132. unsigned char *to, RSA *rsa, int padding)
  133. {
  134. struct cryptoapi_rsa_data *priv =
  135. (struct cryptoapi_rsa_data *) rsa->meth->app_data;
  136. HCRYPTHASH hash;
  137. DWORD hash_size, len, i;
  138. unsigned char *buf = NULL;
  139. int ret = 0;
  140. if (priv == NULL) {
  141. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
  142. ERR_R_PASSED_NULL_PARAMETER);
  143. return 0;
  144. }
  145. if (padding != RSA_PKCS1_PADDING) {
  146. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
  147. RSA_R_UNKNOWN_PADDING_TYPE);
  148. return 0;
  149. }
  150. if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
  151. wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
  152. __func__);
  153. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
  154. RSA_R_INVALID_MESSAGE_LENGTH);
  155. return 0;
  156. }
  157. if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
  158. {
  159. cryptoapi_error("CryptCreateHash failed");
  160. return 0;
  161. }
  162. len = sizeof(hash_size);
  163. if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
  164. 0)) {
  165. cryptoapi_error("CryptGetHashParam failed");
  166. goto err;
  167. }
  168. if ((int) hash_size != flen) {
  169. wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
  170. (unsigned) hash_size, flen);
  171. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
  172. RSA_R_INVALID_MESSAGE_LENGTH);
  173. goto err;
  174. }
  175. if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
  176. cryptoapi_error("CryptSetHashParam failed");
  177. goto err;
  178. }
  179. len = RSA_size(rsa);
  180. buf = os_malloc(len);
  181. if (buf == NULL) {
  182. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
  183. goto err;
  184. }
  185. if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
  186. cryptoapi_error("CryptSignHash failed");
  187. goto err;
  188. }
  189. for (i = 0; i < len; i++)
  190. to[i] = buf[len - i - 1];
  191. ret = len;
  192. err:
  193. os_free(buf);
  194. CryptDestroyHash(hash);
  195. return ret;
  196. }
  197. static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
  198. unsigned char *to, RSA *rsa, int padding)
  199. {
  200. wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
  201. return 0;
  202. }
  203. static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
  204. {
  205. if (priv == NULL)
  206. return;
  207. if (priv->crypt_prov && priv->free_crypt_prov)
  208. CryptReleaseContext(priv->crypt_prov, 0);
  209. if (priv->cert)
  210. CertFreeCertificateContext(priv->cert);
  211. os_free(priv);
  212. }
  213. static int cryptoapi_finish(RSA *rsa)
  214. {
  215. cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
  216. os_free((void *) rsa->meth);
  217. rsa->meth = NULL;
  218. return 1;
  219. }
  220. static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
  221. {
  222. HCERTSTORE cs;
  223. const CERT_CONTEXT *ret = NULL;
  224. cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
  225. store | CERT_STORE_OPEN_EXISTING_FLAG |
  226. CERT_STORE_READONLY_FLAG, L"MY");
  227. if (cs == NULL) {
  228. cryptoapi_error("Failed to open 'My system store'");
  229. return NULL;
  230. }
  231. if (strncmp(name, "cert://", 7) == 0) {
  232. unsigned short wbuf[255];
  233. MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
  234. ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
  235. PKCS_7_ASN_ENCODING,
  236. 0, CERT_FIND_SUBJECT_STR,
  237. wbuf, NULL);
  238. } else if (strncmp(name, "hash://", 7) == 0) {
  239. CRYPT_HASH_BLOB blob;
  240. int len;
  241. const char *hash = name + 7;
  242. unsigned char *buf;
  243. len = os_strlen(hash) / 2;
  244. buf = os_malloc(len);
  245. if (buf && hexstr2bin(hash, buf, len) == 0) {
  246. blob.cbData = len;
  247. blob.pbData = buf;
  248. ret = CertFindCertificateInStore(cs,
  249. X509_ASN_ENCODING |
  250. PKCS_7_ASN_ENCODING,
  251. 0, CERT_FIND_HASH,
  252. &blob, NULL);
  253. }
  254. os_free(buf);
  255. }
  256. CertCloseStore(cs, 0);
  257. return ret;
  258. }
  259. static int tls_cryptoapi_cert(SSL *ssl, const char *name)
  260. {
  261. X509 *cert = NULL;
  262. RSA *rsa = NULL, *pub_rsa;
  263. struct cryptoapi_rsa_data *priv;
  264. RSA_METHOD *rsa_meth;
  265. if (name == NULL ||
  266. (strncmp(name, "cert://", 7) != 0 &&
  267. strncmp(name, "hash://", 7) != 0))
  268. return -1;
  269. priv = os_zalloc(sizeof(*priv));
  270. rsa_meth = os_zalloc(sizeof(*rsa_meth));
  271. if (priv == NULL || rsa_meth == NULL) {
  272. wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
  273. "for CryptoAPI RSA method");
  274. os_free(priv);
  275. os_free(rsa_meth);
  276. return -1;
  277. }
  278. priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
  279. if (priv->cert == NULL) {
  280. priv->cert = cryptoapi_find_cert(
  281. name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
  282. }
  283. if (priv->cert == NULL) {
  284. wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
  285. "'%s'", name);
  286. goto err;
  287. }
  288. cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &priv->cert->pbCertEncoded,
  289. priv->cert->cbCertEncoded);
  290. if (cert == NULL) {
  291. wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
  292. "encoding");
  293. goto err;
  294. }
  295. if (!CryptAcquireCertificatePrivateKey(priv->cert,
  296. CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
  297. NULL, &priv->crypt_prov,
  298. &priv->key_spec,
  299. &priv->free_crypt_prov)) {
  300. cryptoapi_error("Failed to acquire a private key for the "
  301. "certificate");
  302. goto err;
  303. }
  304. rsa_meth->name = "Microsoft CryptoAPI RSA Method";
  305. rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
  306. rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
  307. rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
  308. rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
  309. rsa_meth->finish = cryptoapi_finish;
  310. rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
  311. rsa_meth->app_data = (char *) priv;
  312. rsa = RSA_new();
  313. if (rsa == NULL) {
  314. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
  315. ERR_R_MALLOC_FAILURE);
  316. goto err;
  317. }
  318. if (!SSL_use_certificate(ssl, cert)) {
  319. RSA_free(rsa);
  320. rsa = NULL;
  321. goto err;
  322. }
  323. pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
  324. X509_free(cert);
  325. cert = NULL;
  326. rsa->n = BN_dup(pub_rsa->n);
  327. rsa->e = BN_dup(pub_rsa->e);
  328. if (!RSA_set_method(rsa, rsa_meth))
  329. goto err;
  330. if (!SSL_use_RSAPrivateKey(ssl, rsa))
  331. goto err;
  332. RSA_free(rsa);
  333. return 0;
  334. err:
  335. if (cert)
  336. X509_free(cert);
  337. if (rsa)
  338. RSA_free(rsa);
  339. else {
  340. os_free(rsa_meth);
  341. cryptoapi_free_data(priv);
  342. }
  343. return -1;
  344. }
  345. static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
  346. {
  347. HCERTSTORE cs;
  348. PCCERT_CONTEXT ctx = NULL;
  349. X509 *cert;
  350. char buf[128];
  351. const char *store;
  352. #ifdef UNICODE
  353. WCHAR *wstore;
  354. #endif /* UNICODE */
  355. if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
  356. return -1;
  357. store = name + 13;
  358. #ifdef UNICODE
  359. wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
  360. if (wstore == NULL)
  361. return -1;
  362. wsprintf(wstore, L"%S", store);
  363. cs = CertOpenSystemStore(0, wstore);
  364. os_free(wstore);
  365. #else /* UNICODE */
  366. cs = CertOpenSystemStore(0, store);
  367. #endif /* UNICODE */
  368. if (cs == NULL) {
  369. wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
  370. "'%s': error=%d", __func__, store,
  371. (int) GetLastError());
  372. return -1;
  373. }
  374. while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
  375. cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &ctx->pbCertEncoded,
  376. ctx->cbCertEncoded);
  377. if (cert == NULL) {
  378. wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
  379. "X509 DER encoding for CA cert");
  380. continue;
  381. }
  382. X509_NAME_oneline(X509_get_subject_name(cert), buf,
  383. sizeof(buf));
  384. wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
  385. "system certificate store: subject='%s'", buf);
  386. if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
  387. tls_show_errors(MSG_WARNING, __func__,
  388. "Failed to add ca_cert to OpenSSL "
  389. "certificate store");
  390. }
  391. X509_free(cert);
  392. }
  393. if (!CertCloseStore(cs, 0)) {
  394. wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
  395. "'%s': error=%d", __func__, name + 13,
  396. (int) GetLastError());
  397. }
  398. return 0;
  399. }
  400. #else /* CONFIG_NATIVE_WINDOWS */
  401. static int tls_cryptoapi_cert(SSL *ssl, const char *name)
  402. {
  403. return -1;
  404. }
  405. #endif /* CONFIG_NATIVE_WINDOWS */
  406. static void ssl_info_cb(const SSL *ssl, int where, int ret)
  407. {
  408. const char *str;
  409. int w;
  410. wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
  411. w = where & ~SSL_ST_MASK;
  412. if (w & SSL_ST_CONNECT)
  413. str = "SSL_connect";
  414. else if (w & SSL_ST_ACCEPT)
  415. str = "SSL_accept";
  416. else
  417. str = "undefined";
  418. if (where & SSL_CB_LOOP) {
  419. wpa_printf(MSG_DEBUG, "SSL: %s:%s",
  420. str, SSL_state_string_long(ssl));
  421. } else if (where & SSL_CB_ALERT) {
  422. wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
  423. where & SSL_CB_READ ?
  424. "read (remote end reported an error)" :
  425. "write (local SSL3 detected an error)",
  426. SSL_alert_type_string_long(ret),
  427. SSL_alert_desc_string_long(ret));
  428. if ((ret >> 8) == SSL3_AL_FATAL) {
  429. struct tls_connection *conn =
  430. SSL_get_app_data((SSL *) ssl);
  431. if (where & SSL_CB_READ)
  432. conn->read_alerts++;
  433. else
  434. conn->write_alerts++;
  435. }
  436. } else if (where & SSL_CB_EXIT && ret <= 0) {
  437. wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
  438. str, ret == 0 ? "failed" : "error",
  439. SSL_state_string_long(ssl));
  440. }
  441. }
  442. #ifndef OPENSSL_NO_ENGINE
  443. /**
  444. * tls_engine_load_dynamic_generic - load any openssl engine
  445. * @pre: an array of commands and values that load an engine initialized
  446. * in the engine specific function
  447. * @post: an array of commands and values that initialize an already loaded
  448. * engine (or %NULL if not required)
  449. * @id: the engine id of the engine to load (only required if post is not %NULL
  450. *
  451. * This function is a generic function that loads any openssl engine.
  452. *
  453. * Returns: 0 on success, -1 on failure
  454. */
  455. static int tls_engine_load_dynamic_generic(const char *pre[],
  456. const char *post[], const char *id)
  457. {
  458. ENGINE *engine;
  459. const char *dynamic_id = "dynamic";
  460. engine = ENGINE_by_id(id);
  461. if (engine) {
  462. ENGINE_free(engine);
  463. wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
  464. "available", id);
  465. return 0;
  466. }
  467. ERR_clear_error();
  468. engine = ENGINE_by_id(dynamic_id);
  469. if (engine == NULL) {
  470. wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
  471. dynamic_id,
  472. ERR_error_string(ERR_get_error(), NULL));
  473. return -1;
  474. }
  475. /* Perform the pre commands. This will load the engine. */
  476. while (pre && pre[0]) {
  477. wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
  478. if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
  479. wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
  480. "%s %s [%s]", pre[0], pre[1],
  481. ERR_error_string(ERR_get_error(), NULL));
  482. ENGINE_free(engine);
  483. return -1;
  484. }
  485. pre += 2;
  486. }
  487. /*
  488. * Free the reference to the "dynamic" engine. The loaded engine can
  489. * now be looked up using ENGINE_by_id().
  490. */
  491. ENGINE_free(engine);
  492. engine = ENGINE_by_id(id);
  493. if (engine == NULL) {
  494. wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
  495. id, ERR_error_string(ERR_get_error(), NULL));
  496. return -1;
  497. }
  498. while (post && post[0]) {
  499. wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
  500. if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
  501. wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
  502. " %s %s [%s]", post[0], post[1],
  503. ERR_error_string(ERR_get_error(), NULL));
  504. ENGINE_remove(engine);
  505. ENGINE_free(engine);
  506. return -1;
  507. }
  508. post += 2;
  509. }
  510. ENGINE_free(engine);
  511. return 0;
  512. }
  513. /**
  514. * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
  515. * @pkcs11_so_path: pksc11_so_path from the configuration
  516. * @pcks11_module_path: pkcs11_module_path from the configuration
  517. */
  518. static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
  519. const char *pkcs11_module_path)
  520. {
  521. char *engine_id = "pkcs11";
  522. const char *pre_cmd[] = {
  523. "SO_PATH", NULL /* pkcs11_so_path */,
  524. "ID", NULL /* engine_id */,
  525. "LIST_ADD", "1",
  526. /* "NO_VCHECK", "1", */
  527. "LOAD", NULL,
  528. NULL, NULL
  529. };
  530. const char *post_cmd[] = {
  531. "MODULE_PATH", NULL /* pkcs11_module_path */,
  532. NULL, NULL
  533. };
  534. if (!pkcs11_so_path || !pkcs11_module_path)
  535. return 0;
  536. pre_cmd[1] = pkcs11_so_path;
  537. pre_cmd[3] = engine_id;
  538. post_cmd[1] = pkcs11_module_path;
  539. wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
  540. pkcs11_so_path);
  541. return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
  542. }
  543. /**
  544. * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
  545. * @opensc_so_path: opensc_so_path from the configuration
  546. */
  547. static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
  548. {
  549. char *engine_id = "opensc";
  550. const char *pre_cmd[] = {
  551. "SO_PATH", NULL /* opensc_so_path */,
  552. "ID", NULL /* engine_id */,
  553. "LIST_ADD", "1",
  554. "LOAD", NULL,
  555. NULL, NULL
  556. };
  557. if (!opensc_so_path)
  558. return 0;
  559. pre_cmd[1] = opensc_so_path;
  560. pre_cmd[3] = engine_id;
  561. wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
  562. opensc_so_path);
  563. return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
  564. }
  565. #endif /* OPENSSL_NO_ENGINE */
  566. void * tls_init(const struct tls_config *conf)
  567. {
  568. SSL_CTX *ssl;
  569. if (tls_openssl_ref_count == 0) {
  570. tls_global = os_zalloc(sizeof(*tls_global));
  571. if (tls_global == NULL)
  572. return NULL;
  573. if (conf) {
  574. tls_global->event_cb = conf->event_cb;
  575. tls_global->cb_ctx = conf->cb_ctx;
  576. }
  577. #ifdef CONFIG_FIPS
  578. #ifdef OPENSSL_FIPS
  579. if (conf && conf->fips_mode) {
  580. if (!FIPS_mode_set(1)) {
  581. wpa_printf(MSG_ERROR, "Failed to enable FIPS "
  582. "mode");
  583. ERR_load_crypto_strings();
  584. ERR_print_errors_fp(stderr);
  585. return NULL;
  586. } else
  587. wpa_printf(MSG_INFO, "Running in FIPS mode");
  588. }
  589. #else /* OPENSSL_FIPS */
  590. if (conf && conf->fips_mode) {
  591. wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
  592. "supported");
  593. return NULL;
  594. }
  595. #endif /* OPENSSL_FIPS */
  596. #endif /* CONFIG_FIPS */
  597. SSL_load_error_strings();
  598. SSL_library_init();
  599. #if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256)
  600. EVP_add_digest(EVP_sha256());
  601. #endif /* OPENSSL_NO_SHA256 */
  602. /* TODO: if /dev/urandom is available, PRNG is seeded
  603. * automatically. If this is not the case, random data should
  604. * be added here. */
  605. #ifdef PKCS12_FUNCS
  606. #ifndef OPENSSL_NO_RC2
  607. /*
  608. * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
  609. * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
  610. * versions, but it looks like OpenSSL 1.0.0 does not do that
  611. * anymore.
  612. */
  613. EVP_add_cipher(EVP_rc2_40_cbc());
  614. #endif /* OPENSSL_NO_RC2 */
  615. PKCS12_PBE_add();
  616. #endif /* PKCS12_FUNCS */
  617. }
  618. tls_openssl_ref_count++;
  619. ssl = SSL_CTX_new(TLSv1_method());
  620. if (ssl == NULL)
  621. return NULL;
  622. SSL_CTX_set_info_callback(ssl, ssl_info_cb);
  623. #ifndef OPENSSL_NO_ENGINE
  624. if (conf &&
  625. (conf->opensc_engine_path || conf->pkcs11_engine_path ||
  626. conf->pkcs11_module_path)) {
  627. wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine");
  628. ERR_load_ENGINE_strings();
  629. ENGINE_load_dynamic();
  630. if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
  631. tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
  632. conf->pkcs11_module_path)) {
  633. tls_deinit(ssl);
  634. return NULL;
  635. }
  636. }
  637. #endif /* OPENSSL_NO_ENGINE */
  638. return ssl;
  639. }
  640. void tls_deinit(void *ssl_ctx)
  641. {
  642. SSL_CTX *ssl = ssl_ctx;
  643. SSL_CTX_free(ssl);
  644. tls_openssl_ref_count--;
  645. if (tls_openssl_ref_count == 0) {
  646. #ifndef OPENSSL_NO_ENGINE
  647. ENGINE_cleanup();
  648. #endif /* OPENSSL_NO_ENGINE */
  649. CRYPTO_cleanup_all_ex_data();
  650. ERR_remove_state(0);
  651. ERR_free_strings();
  652. EVP_cleanup();
  653. os_free(tls_global);
  654. tls_global = NULL;
  655. }
  656. }
  657. static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
  658. const char *pin, const char *key_id,
  659. const char *cert_id, const char *ca_cert_id)
  660. {
  661. #ifndef OPENSSL_NO_ENGINE
  662. int ret = -1;
  663. if (engine_id == NULL) {
  664. wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
  665. return -1;
  666. }
  667. if (pin == NULL) {
  668. wpa_printf(MSG_ERROR, "ENGINE: Smartcard PIN not set");
  669. return -1;
  670. }
  671. if (key_id == NULL) {
  672. wpa_printf(MSG_ERROR, "ENGINE: Key Id not set");
  673. return -1;
  674. }
  675. ERR_clear_error();
  676. conn->engine = ENGINE_by_id(engine_id);
  677. if (!conn->engine) {
  678. wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
  679. engine_id, ERR_error_string(ERR_get_error(), NULL));
  680. goto err;
  681. }
  682. if (ENGINE_init(conn->engine) != 1) {
  683. wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
  684. "(engine: %s) [%s]", engine_id,
  685. ERR_error_string(ERR_get_error(), NULL));
  686. goto err;
  687. }
  688. wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
  689. if (ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
  690. wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
  691. ERR_error_string(ERR_get_error(), NULL));
  692. goto err;
  693. }
  694. /* load private key first in-case PIN is required for cert */
  695. conn->private_key = ENGINE_load_private_key(conn->engine,
  696. key_id, NULL, NULL);
  697. if (!conn->private_key) {
  698. wpa_printf(MSG_ERROR, "ENGINE: cannot load private key with id"
  699. " '%s' [%s]", key_id,
  700. ERR_error_string(ERR_get_error(), NULL));
  701. ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
  702. goto err;
  703. }
  704. /* handle a certificate and/or CA certificate */
  705. if (cert_id || ca_cert_id) {
  706. const char *cmd_name = "LOAD_CERT_CTRL";
  707. /* test if the engine supports a LOAD_CERT_CTRL */
  708. if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
  709. 0, (void *)cmd_name, NULL)) {
  710. wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
  711. " loading certificates");
  712. ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
  713. goto err;
  714. }
  715. }
  716. return 0;
  717. err:
  718. if (conn->engine) {
  719. ENGINE_free(conn->engine);
  720. conn->engine = NULL;
  721. }
  722. if (conn->private_key) {
  723. EVP_PKEY_free(conn->private_key);
  724. conn->private_key = NULL;
  725. }
  726. return ret;
  727. #else /* OPENSSL_NO_ENGINE */
  728. return 0;
  729. #endif /* OPENSSL_NO_ENGINE */
  730. }
  731. static void tls_engine_deinit(struct tls_connection *conn)
  732. {
  733. #ifndef OPENSSL_NO_ENGINE
  734. wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
  735. if (conn->private_key) {
  736. EVP_PKEY_free(conn->private_key);
  737. conn->private_key = NULL;
  738. }
  739. if (conn->engine) {
  740. ENGINE_finish(conn->engine);
  741. conn->engine = NULL;
  742. }
  743. #endif /* OPENSSL_NO_ENGINE */
  744. }
  745. int tls_get_errors(void *ssl_ctx)
  746. {
  747. int count = 0;
  748. unsigned long err;
  749. while ((err = ERR_get_error())) {
  750. wpa_printf(MSG_INFO, "TLS - SSL error: %s",
  751. ERR_error_string(err, NULL));
  752. count++;
  753. }
  754. return count;
  755. }
  756. struct tls_connection * tls_connection_init(void *ssl_ctx)
  757. {
  758. SSL_CTX *ssl = ssl_ctx;
  759. struct tls_connection *conn;
  760. long options;
  761. conn = os_zalloc(sizeof(*conn));
  762. if (conn == NULL)
  763. return NULL;
  764. conn->ssl = SSL_new(ssl);
  765. if (conn->ssl == NULL) {
  766. tls_show_errors(MSG_INFO, __func__,
  767. "Failed to initialize new SSL connection");
  768. os_free(conn);
  769. return NULL;
  770. }
  771. SSL_set_app_data(conn->ssl, conn);
  772. options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
  773. SSL_OP_SINGLE_DH_USE;
  774. #ifdef SSL_OP_NO_COMPRESSION
  775. options |= SSL_OP_NO_COMPRESSION;
  776. #endif /* SSL_OP_NO_COMPRESSION */
  777. SSL_set_options(conn->ssl, options);
  778. conn->ssl_in = BIO_new(BIO_s_mem());
  779. if (!conn->ssl_in) {
  780. tls_show_errors(MSG_INFO, __func__,
  781. "Failed to create a new BIO for ssl_in");
  782. SSL_free(conn->ssl);
  783. os_free(conn);
  784. return NULL;
  785. }
  786. conn->ssl_out = BIO_new(BIO_s_mem());
  787. if (!conn->ssl_out) {
  788. tls_show_errors(MSG_INFO, __func__,
  789. "Failed to create a new BIO for ssl_out");
  790. SSL_free(conn->ssl);
  791. BIO_free(conn->ssl_in);
  792. os_free(conn);
  793. return NULL;
  794. }
  795. SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
  796. return conn;
  797. }
  798. void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
  799. {
  800. if (conn == NULL)
  801. return;
  802. SSL_free(conn->ssl);
  803. tls_engine_deinit(conn);
  804. os_free(conn->subject_match);
  805. os_free(conn->altsubject_match);
  806. os_free(conn->session_ticket);
  807. os_free(conn);
  808. }
  809. int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
  810. {
  811. return conn ? SSL_is_init_finished(conn->ssl) : 0;
  812. }
  813. int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
  814. {
  815. if (conn == NULL)
  816. return -1;
  817. /* Shutdown previous TLS connection without notifying the peer
  818. * because the connection was already terminated in practice
  819. * and "close notify" shutdown alert would confuse AS. */
  820. SSL_set_quiet_shutdown(conn->ssl, 1);
  821. SSL_shutdown(conn->ssl);
  822. return 0;
  823. }
  824. static int tls_match_altsubject_component(X509 *cert, int type,
  825. const char *value, size_t len)
  826. {
  827. GENERAL_NAME *gen;
  828. void *ext;
  829. int i, found = 0;
  830. ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
  831. for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
  832. gen = sk_GENERAL_NAME_value(ext, i);
  833. if (gen->type != type)
  834. continue;
  835. if (os_strlen((char *) gen->d.ia5->data) == len &&
  836. os_memcmp(value, gen->d.ia5->data, len) == 0)
  837. found++;
  838. }
  839. return found;
  840. }
  841. static int tls_match_altsubject(X509 *cert, const char *match)
  842. {
  843. int type;
  844. const char *pos, *end;
  845. size_t len;
  846. pos = match;
  847. do {
  848. if (os_strncmp(pos, "EMAIL:", 6) == 0) {
  849. type = GEN_EMAIL;
  850. pos += 6;
  851. } else if (os_strncmp(pos, "DNS:", 4) == 0) {
  852. type = GEN_DNS;
  853. pos += 4;
  854. } else if (os_strncmp(pos, "URI:", 4) == 0) {
  855. type = GEN_URI;
  856. pos += 4;
  857. } else {
  858. wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
  859. "match '%s'", pos);
  860. return 0;
  861. }
  862. end = os_strchr(pos, ';');
  863. while (end) {
  864. if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
  865. os_strncmp(end + 1, "DNS:", 4) == 0 ||
  866. os_strncmp(end + 1, "URI:", 4) == 0)
  867. break;
  868. end = os_strchr(end + 1, ';');
  869. }
  870. if (end)
  871. len = end - pos;
  872. else
  873. len = os_strlen(pos);
  874. if (tls_match_altsubject_component(cert, type, pos, len) > 0)
  875. return 1;
  876. pos = end + 1;
  877. } while (end);
  878. return 0;
  879. }
  880. static enum tls_fail_reason openssl_tls_fail_reason(int err)
  881. {
  882. switch (err) {
  883. case X509_V_ERR_CERT_REVOKED:
  884. return TLS_FAIL_REVOKED;
  885. case X509_V_ERR_CERT_NOT_YET_VALID:
  886. case X509_V_ERR_CRL_NOT_YET_VALID:
  887. return TLS_FAIL_NOT_YET_VALID;
  888. case X509_V_ERR_CERT_HAS_EXPIRED:
  889. case X509_V_ERR_CRL_HAS_EXPIRED:
  890. return TLS_FAIL_EXPIRED;
  891. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  892. case X509_V_ERR_UNABLE_TO_GET_CRL:
  893. case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
  894. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  895. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  896. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  897. case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  898. case X509_V_ERR_CERT_CHAIN_TOO_LONG:
  899. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  900. case X509_V_ERR_INVALID_CA:
  901. return TLS_FAIL_UNTRUSTED;
  902. case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
  903. case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
  904. case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
  905. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  906. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  907. case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
  908. case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
  909. case X509_V_ERR_CERT_UNTRUSTED:
  910. case X509_V_ERR_CERT_REJECTED:
  911. return TLS_FAIL_BAD_CERTIFICATE;
  912. default:
  913. return TLS_FAIL_UNSPECIFIED;
  914. }
  915. }
  916. static struct wpabuf * get_x509_cert(X509 *cert)
  917. {
  918. struct wpabuf *buf;
  919. u8 *tmp;
  920. int cert_len = i2d_X509(cert, NULL);
  921. if (cert_len <= 0)
  922. return NULL;
  923. buf = wpabuf_alloc(cert_len);
  924. if (buf == NULL)
  925. return NULL;
  926. tmp = wpabuf_put(buf, cert_len);
  927. i2d_X509(cert, &tmp);
  928. return buf;
  929. }
  930. static void openssl_tls_fail_event(struct tls_connection *conn,
  931. X509 *err_cert, int err, int depth,
  932. const char *subject, const char *err_str,
  933. enum tls_fail_reason reason)
  934. {
  935. union tls_event_data ev;
  936. struct wpabuf *cert = NULL;
  937. if (tls_global->event_cb == NULL)
  938. return;
  939. cert = get_x509_cert(err_cert);
  940. os_memset(&ev, 0, sizeof(ev));
  941. ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
  942. reason : openssl_tls_fail_reason(err);
  943. ev.cert_fail.depth = depth;
  944. ev.cert_fail.subject = subject;
  945. ev.cert_fail.reason_txt = err_str;
  946. ev.cert_fail.cert = cert;
  947. tls_global->event_cb(tls_global->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
  948. wpabuf_free(cert);
  949. }
  950. static void openssl_tls_cert_event(struct tls_connection *conn,
  951. X509 *err_cert, int depth,
  952. const char *subject)
  953. {
  954. struct wpabuf *cert = NULL;
  955. union tls_event_data ev;
  956. #ifdef CONFIG_SHA256
  957. u8 hash[32];
  958. #endif /* CONFIG_SHA256 */
  959. if (tls_global->event_cb == NULL)
  960. return;
  961. os_memset(&ev, 0, sizeof(ev));
  962. if (conn->cert_probe) {
  963. cert = get_x509_cert(err_cert);
  964. ev.peer_cert.cert = cert;
  965. }
  966. #ifdef CONFIG_SHA256
  967. if (cert) {
  968. const u8 *addr[1];
  969. size_t len[1];
  970. addr[0] = wpabuf_head(cert);
  971. len[0] = wpabuf_len(cert);
  972. if (sha256_vector(1, addr, len, hash) == 0) {
  973. ev.peer_cert.hash = hash;
  974. ev.peer_cert.hash_len = sizeof(hash);
  975. }
  976. }
  977. #endif /* CONFIG_SHA256 */
  978. ev.peer_cert.depth = depth;
  979. ev.peer_cert.subject = subject;
  980. tls_global->event_cb(tls_global->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
  981. wpabuf_free(cert);
  982. }
  983. static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
  984. {
  985. char buf[256];
  986. X509 *err_cert;
  987. int err, depth;
  988. SSL *ssl;
  989. struct tls_connection *conn;
  990. char *match, *altmatch;
  991. const char *err_str;
  992. err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
  993. err = X509_STORE_CTX_get_error(x509_ctx);
  994. depth = X509_STORE_CTX_get_error_depth(x509_ctx);
  995. ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
  996. SSL_get_ex_data_X509_STORE_CTX_idx());
  997. X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
  998. conn = SSL_get_app_data(ssl);
  999. if (conn == NULL)
  1000. return 0;
  1001. match = conn->subject_match;
  1002. altmatch = conn->altsubject_match;
  1003. if (!preverify_ok && !conn->ca_cert_verify)
  1004. preverify_ok = 1;
  1005. if (!preverify_ok && depth > 0 && conn->server_cert_only)
  1006. preverify_ok = 1;
  1007. if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
  1008. (err == X509_V_ERR_CERT_HAS_EXPIRED ||
  1009. err == X509_V_ERR_CERT_NOT_YET_VALID)) {
  1010. wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity "
  1011. "time mismatch");
  1012. preverify_ok = 1;
  1013. }
  1014. err_str = X509_verify_cert_error_string(err);
  1015. #ifdef CONFIG_SHA256
  1016. if (preverify_ok && depth == 0 && conn->server_cert_only) {
  1017. struct wpabuf *cert;
  1018. cert = get_x509_cert(err_cert);
  1019. if (!cert) {
  1020. wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch "
  1021. "server certificate data");
  1022. preverify_ok = 0;
  1023. } else {
  1024. u8 hash[32];
  1025. const u8 *addr[1];
  1026. size_t len[1];
  1027. addr[0] = wpabuf_head(cert);
  1028. len[0] = wpabuf_len(cert);
  1029. if (sha256_vector(1, addr, len, hash) < 0 ||
  1030. os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
  1031. err_str = "Server certificate mismatch";
  1032. err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
  1033. preverify_ok = 0;
  1034. }
  1035. wpabuf_free(cert);
  1036. }
  1037. }
  1038. #endif /* CONFIG_SHA256 */
  1039. if (!preverify_ok) {
  1040. wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
  1041. " error %d (%s) depth %d for '%s'", err, err_str,
  1042. depth, buf);
  1043. openssl_tls_fail_event(conn, err_cert, err, depth, buf,
  1044. err_str, TLS_FAIL_UNSPECIFIED);
  1045. return preverify_ok;
  1046. }
  1047. wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
  1048. "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
  1049. preverify_ok, err, err_str,
  1050. conn->ca_cert_verify, depth, buf);
  1051. if (depth == 0 && match && os_strstr(buf, match) == NULL) {
  1052. wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
  1053. "match with '%s'", buf, match);
  1054. preverify_ok = 0;
  1055. openssl_tls_fail_event(conn, err_cert, err, depth, buf,
  1056. "Subject mismatch",
  1057. TLS_FAIL_SUBJECT_MISMATCH);
  1058. } else if (depth == 0 && altmatch &&
  1059. !tls_match_altsubject(err_cert, altmatch)) {
  1060. wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
  1061. "'%s' not found", altmatch);
  1062. preverify_ok = 0;
  1063. openssl_tls_fail_event(conn, err_cert, err, depth, buf,
  1064. "AltSubject mismatch",
  1065. TLS_FAIL_ALTSUBJECT_MISMATCH);
  1066. } else
  1067. openssl_tls_cert_event(conn, err_cert, depth, buf);
  1068. if (conn->cert_probe && preverify_ok && depth == 0) {
  1069. wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
  1070. "on probe-only run");
  1071. preverify_ok = 0;
  1072. openssl_tls_fail_event(conn, err_cert, err, depth, buf,
  1073. "Server certificate chain probe",
  1074. TLS_FAIL_SERVER_CHAIN_PROBE);
  1075. }
  1076. return preverify_ok;
  1077. }
  1078. #ifndef OPENSSL_NO_STDIO
  1079. static int tls_load_ca_der(void *_ssl_ctx, const char *ca_cert)
  1080. {
  1081. SSL_CTX *ssl_ctx = _ssl_ctx;
  1082. X509_LOOKUP *lookup;
  1083. int ret = 0;
  1084. lookup = X509_STORE_add_lookup(ssl_ctx->cert_store,
  1085. X509_LOOKUP_file());
  1086. if (lookup == NULL) {
  1087. tls_show_errors(MSG_WARNING, __func__,
  1088. "Failed add lookup for X509 store");
  1089. return -1;
  1090. }
  1091. if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
  1092. unsigned long err = ERR_peek_error();
  1093. tls_show_errors(MSG_WARNING, __func__,
  1094. "Failed load CA in DER format");
  1095. if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
  1096. ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
  1097. wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
  1098. "cert already in hash table error",
  1099. __func__);
  1100. } else
  1101. ret = -1;
  1102. }
  1103. return ret;
  1104. }
  1105. #endif /* OPENSSL_NO_STDIO */
  1106. #ifdef ANDROID
  1107. static BIO * BIO_from_keystore(const char *key)
  1108. {
  1109. BIO *bio = NULL;
  1110. char value[KEYSTORE_MESSAGE_SIZE];
  1111. int length = keystore_get(key, strlen(key), value);
  1112. if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL)
  1113. BIO_write(bio, value, length);
  1114. return bio;
  1115. }
  1116. #endif /* ANDROID */
  1117. static int tls_connection_ca_cert(void *_ssl_ctx, struct tls_connection *conn,
  1118. const char *ca_cert, const u8 *ca_cert_blob,
  1119. size_t ca_cert_blob_len, const char *ca_path)
  1120. {
  1121. SSL_CTX *ssl_ctx = _ssl_ctx;
  1122. /*
  1123. * Remove previously configured trusted CA certificates before adding
  1124. * new ones.
  1125. */
  1126. X509_STORE_free(ssl_ctx->cert_store);
  1127. ssl_ctx->cert_store = X509_STORE_new();
  1128. if (ssl_ctx->cert_store == NULL) {
  1129. wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
  1130. "certificate store", __func__);
  1131. return -1;
  1132. }
  1133. SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
  1134. conn->ca_cert_verify = 1;
  1135. if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
  1136. wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
  1137. "chain");
  1138. conn->cert_probe = 1;
  1139. conn->ca_cert_verify = 0;
  1140. return 0;
  1141. }
  1142. if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
  1143. #ifdef CONFIG_SHA256
  1144. const char *pos = ca_cert + 7;
  1145. if (os_strncmp(pos, "server/sha256/", 14) != 0) {
  1146. wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
  1147. "hash value '%s'", ca_cert);
  1148. return -1;
  1149. }
  1150. pos += 14;
  1151. if (os_strlen(pos) != 32 * 2) {
  1152. wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
  1153. "hash length in ca_cert '%s'", ca_cert);
  1154. return -1;
  1155. }
  1156. if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
  1157. wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
  1158. "value in ca_cert '%s'", ca_cert);
  1159. return -1;
  1160. }
  1161. conn->server_cert_only = 1;
  1162. wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
  1163. "certificate match");
  1164. return 0;
  1165. #else /* CONFIG_SHA256 */
  1166. wpa_printf(MSG_INFO, "No SHA256 included in the build - "
  1167. "cannot validate server certificate hash");
  1168. return -1;
  1169. #endif /* CONFIG_SHA256 */
  1170. }
  1171. if (ca_cert_blob) {
  1172. X509 *cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &ca_cert_blob,
  1173. ca_cert_blob_len);
  1174. if (cert == NULL) {
  1175. tls_show_errors(MSG_WARNING, __func__,
  1176. "Failed to parse ca_cert_blob");
  1177. return -1;
  1178. }
  1179. if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
  1180. unsigned long err = ERR_peek_error();
  1181. tls_show_errors(MSG_WARNING, __func__,
  1182. "Failed to add ca_cert_blob to "
  1183. "certificate store");
  1184. if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
  1185. ERR_GET_REASON(err) ==
  1186. X509_R_CERT_ALREADY_IN_HASH_TABLE) {
  1187. wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
  1188. "cert already in hash table error",
  1189. __func__);
  1190. } else {
  1191. X509_free(cert);
  1192. return -1;
  1193. }
  1194. }
  1195. X509_free(cert);
  1196. wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
  1197. "to certificate store", __func__);
  1198. return 0;
  1199. }
  1200. #ifdef ANDROID
  1201. if (ca_cert && os_strncmp("keystore://", ca_cert, 11) == 0) {
  1202. BIO *bio = BIO_from_keystore(&ca_cert[11]);
  1203. STACK_OF(X509_INFO) *stack = NULL;
  1204. int i;
  1205. if (bio) {
  1206. stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
  1207. BIO_free(bio);
  1208. }
  1209. if (!stack)
  1210. return -1;
  1211. for (i = 0; i < sk_X509_INFO_num(stack); ++i) {
  1212. X509_INFO *info = sk_X509_INFO_value(stack, i);
  1213. if (info->x509) {
  1214. X509_STORE_add_cert(ssl_ctx->cert_store,
  1215. info->x509);
  1216. }
  1217. if (info->crl) {
  1218. X509_STORE_add_crl(ssl_ctx->cert_store,
  1219. info->crl);
  1220. }
  1221. }
  1222. sk_X509_INFO_pop_free(stack, X509_INFO_free);
  1223. SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
  1224. return 0;
  1225. }
  1226. #endif /* ANDROID */
  1227. #ifdef CONFIG_NATIVE_WINDOWS
  1228. if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
  1229. 0) {
  1230. wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
  1231. "system certificate store");
  1232. return 0;
  1233. }
  1234. #endif /* CONFIG_NATIVE_WINDOWS */
  1235. if (ca_cert || ca_path) {
  1236. #ifndef OPENSSL_NO_STDIO
  1237. if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
  1238. 1) {
  1239. tls_show_errors(MSG_WARNING, __func__,
  1240. "Failed to load root certificates");
  1241. if (ca_cert &&
  1242. tls_load_ca_der(ssl_ctx, ca_cert) == 0) {
  1243. wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
  1244. "DER format CA certificate",
  1245. __func__);
  1246. } else
  1247. return -1;
  1248. } else {
  1249. wpa_printf(MSG_DEBUG, "TLS: Trusted root "
  1250. "certificate(s) loaded");
  1251. tls_get_errors(ssl_ctx);
  1252. }
  1253. #else /* OPENSSL_NO_STDIO */
  1254. wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
  1255. __func__);
  1256. return -1;
  1257. #endif /* OPENSSL_NO_STDIO */
  1258. } else {
  1259. /* No ca_cert configured - do not try to verify server
  1260. * certificate */
  1261. conn->ca_cert_verify = 0;
  1262. }
  1263. return 0;
  1264. }
  1265. static int tls_global_ca_cert(SSL_CTX *ssl_ctx, const char *ca_cert)
  1266. {
  1267. if (ca_cert) {
  1268. if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
  1269. {
  1270. tls_show_errors(MSG_WARNING, __func__,
  1271. "Failed to load root certificates");
  1272. return -1;
  1273. }
  1274. wpa_printf(MSG_DEBUG, "TLS: Trusted root "
  1275. "certificate(s) loaded");
  1276. #ifndef OPENSSL_NO_STDIO
  1277. /* Add the same CAs to the client certificate requests */
  1278. SSL_CTX_set_client_CA_list(ssl_ctx,
  1279. SSL_load_client_CA_file(ca_cert));
  1280. #endif /* OPENSSL_NO_STDIO */
  1281. }
  1282. return 0;
  1283. }
  1284. int tls_global_set_verify(void *ssl_ctx, int check_crl)
  1285. {
  1286. int flags;
  1287. if (check_crl) {
  1288. X509_STORE *cs = SSL_CTX_get_cert_store(ssl_ctx);
  1289. if (cs == NULL) {
  1290. tls_show_errors(MSG_INFO, __func__, "Failed to get "
  1291. "certificate store when enabling "
  1292. "check_crl");
  1293. return -1;
  1294. }
  1295. flags = X509_V_FLAG_CRL_CHECK;
  1296. if (check_crl == 2)
  1297. flags |= X509_V_FLAG_CRL_CHECK_ALL;
  1298. X509_STORE_set_flags(cs, flags);
  1299. }
  1300. return 0;
  1301. }
  1302. static int tls_connection_set_subject_match(struct tls_connection *conn,
  1303. const char *subject_match,
  1304. const char *altsubject_match)
  1305. {
  1306. os_free(conn->subject_match);
  1307. conn->subject_match = NULL;
  1308. if (subject_match) {
  1309. conn->subject_match = os_strdup(subject_match);
  1310. if (conn->subject_match == NULL)
  1311. return -1;
  1312. }
  1313. os_free(conn->altsubject_match);
  1314. conn->altsubject_match = NULL;
  1315. if (altsubject_match) {
  1316. conn->altsubject_match = os_strdup(altsubject_match);
  1317. if (conn->altsubject_match == NULL)
  1318. return -1;
  1319. }
  1320. return 0;
  1321. }
  1322. int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
  1323. int verify_peer)
  1324. {
  1325. static int counter = 0;
  1326. if (conn == NULL)
  1327. return -1;
  1328. if (verify_peer) {
  1329. conn->ca_cert_verify = 1;
  1330. SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
  1331. SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
  1332. SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
  1333. } else {
  1334. conn->ca_cert_verify = 0;
  1335. SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
  1336. }
  1337. SSL_set_accept_state(conn->ssl);
  1338. /*
  1339. * Set session id context in order to avoid fatal errors when client
  1340. * tries to resume a session. However, set the context to a unique
  1341. * value in order to effectively disable session resumption for now
  1342. * since not all areas of the server code are ready for it (e.g.,
  1343. * EAP-TTLS needs special handling for Phase 2 after abbreviated TLS
  1344. * handshake).
  1345. */
  1346. counter++;
  1347. SSL_set_session_id_context(conn->ssl,
  1348. (const unsigned char *) &counter,
  1349. sizeof(counter));
  1350. return 0;
  1351. }
  1352. static int tls_connection_client_cert(struct tls_connection *conn,
  1353. const char *client_cert,
  1354. const u8 *client_cert_blob,
  1355. size_t client_cert_blob_len)
  1356. {
  1357. if (client_cert == NULL && client_cert_blob == NULL)
  1358. return 0;
  1359. if (client_cert_blob &&
  1360. SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
  1361. client_cert_blob_len) == 1) {
  1362. wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
  1363. "OK");
  1364. return 0;
  1365. } else if (client_cert_blob) {
  1366. tls_show_errors(MSG_DEBUG, __func__,
  1367. "SSL_use_certificate_ASN1 failed");
  1368. }
  1369. if (client_cert == NULL)
  1370. return -1;
  1371. #ifdef ANDROID
  1372. if (os_strncmp("keystore://", client_cert, 11) == 0) {
  1373. BIO *bio = BIO_from_keystore(&client_cert[11]);
  1374. X509 *x509 = NULL;
  1375. int ret = -1;
  1376. if (bio) {
  1377. x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
  1378. BIO_free(bio);
  1379. }
  1380. if (x509) {
  1381. if (SSL_use_certificate(conn->ssl, x509) == 1)
  1382. ret = 0;
  1383. X509_free(x509);
  1384. }
  1385. return ret;
  1386. }
  1387. #endif /* ANDROID */
  1388. #ifndef OPENSSL_NO_STDIO
  1389. if (SSL_use_certificate_file(conn->ssl, client_cert,
  1390. SSL_FILETYPE_ASN1) == 1) {
  1391. wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
  1392. " --> OK");
  1393. return 0;
  1394. }
  1395. if (SSL_use_certificate_file(conn->ssl, client_cert,
  1396. SSL_FILETYPE_PEM) == 1) {
  1397. ERR_clear_error();
  1398. wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
  1399. " --> OK");
  1400. return 0;
  1401. }
  1402. tls_show_errors(MSG_DEBUG, __func__,
  1403. "SSL_use_certificate_file failed");
  1404. #else /* OPENSSL_NO_STDIO */
  1405. wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
  1406. #endif /* OPENSSL_NO_STDIO */
  1407. return -1;
  1408. }
  1409. static int tls_global_client_cert(SSL_CTX *ssl_ctx, const char *client_cert)
  1410. {
  1411. #ifndef OPENSSL_NO_STDIO
  1412. if (client_cert == NULL)
  1413. return 0;
  1414. if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
  1415. SSL_FILETYPE_ASN1) != 1 &&
  1416. SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
  1417. SSL_FILETYPE_PEM) != 1) {
  1418. tls_show_errors(MSG_INFO, __func__,
  1419. "Failed to load client certificate");
  1420. return -1;
  1421. }
  1422. return 0;
  1423. #else /* OPENSSL_NO_STDIO */
  1424. if (client_cert == NULL)
  1425. return 0;
  1426. wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
  1427. return -1;
  1428. #endif /* OPENSSL_NO_STDIO */
  1429. }
  1430. static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
  1431. {
  1432. if (password == NULL) {
  1433. return 0;
  1434. }
  1435. os_strlcpy(buf, (char *) password, size);
  1436. return os_strlen(buf);
  1437. }
  1438. #ifdef PKCS12_FUNCS
  1439. static int tls_parse_pkcs12(SSL_CTX *ssl_ctx, SSL *ssl, PKCS12 *p12,
  1440. const char *passwd)
  1441. {
  1442. EVP_PKEY *pkey;
  1443. X509 *cert;
  1444. STACK_OF(X509) *certs;
  1445. int res = 0;
  1446. char buf[256];
  1447. pkey = NULL;
  1448. cert = NULL;
  1449. certs = NULL;
  1450. if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
  1451. tls_show_errors(MSG_DEBUG, __func__,
  1452. "Failed to parse PKCS12 file");
  1453. PKCS12_free(p12);
  1454. return -1;
  1455. }
  1456. wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
  1457. if (cert) {
  1458. X509_NAME_oneline(X509_get_subject_name(cert), buf,
  1459. sizeof(buf));
  1460. wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
  1461. "subject='%s'", buf);
  1462. if (ssl) {
  1463. if (SSL_use_certificate(ssl, cert) != 1)
  1464. res = -1;
  1465. } else {
  1466. if (SSL_CTX_use_certificate(ssl_ctx, cert) != 1)
  1467. res = -1;
  1468. }
  1469. X509_free(cert);
  1470. }
  1471. if (pkey) {
  1472. wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
  1473. if (ssl) {
  1474. if (SSL_use_PrivateKey(ssl, pkey) != 1)
  1475. res = -1;
  1476. } else {
  1477. if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) != 1)
  1478. res = -1;
  1479. }
  1480. EVP_PKEY_free(pkey);
  1481. }
  1482. if (certs) {
  1483. while ((cert = sk_X509_pop(certs)) != NULL) {
  1484. X509_NAME_oneline(X509_get_subject_name(cert), buf,
  1485. sizeof(buf));
  1486. wpa_printf(MSG_DEBUG, "TLS: additional certificate"
  1487. " from PKCS12: subject='%s'", buf);
  1488. /*
  1489. * There is no SSL equivalent for the chain cert - so
  1490. * always add it to the context...
  1491. */
  1492. if (SSL_CTX_add_extra_chain_cert(ssl_ctx, cert) != 1) {
  1493. res = -1;
  1494. break;
  1495. }
  1496. }
  1497. sk_X509_free(certs);
  1498. }
  1499. PKCS12_free(p12);
  1500. if (res < 0)
  1501. tls_get_errors(ssl_ctx);
  1502. return res;
  1503. }
  1504. #endif /* PKCS12_FUNCS */
  1505. static int tls_read_pkcs12(SSL_CTX *ssl_ctx, SSL *ssl, const char *private_key,
  1506. const char *passwd)
  1507. {
  1508. #ifdef PKCS12_FUNCS
  1509. FILE *f;
  1510. PKCS12 *p12;
  1511. f = fopen(private_key, "rb");
  1512. if (f == NULL)
  1513. return -1;
  1514. p12 = d2i_PKCS12_fp(f, NULL);
  1515. fclose(f);
  1516. if (p12 == NULL) {
  1517. tls_show_errors(MSG_INFO, __func__,
  1518. "Failed to use PKCS#12 file");
  1519. return -1;
  1520. }
  1521. return tls_parse_pkcs12(ssl_ctx, ssl, p12, passwd);
  1522. #else /* PKCS12_FUNCS */
  1523. wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
  1524. "p12/pfx files");
  1525. return -1;
  1526. #endif /* PKCS12_FUNCS */
  1527. }
  1528. static int tls_read_pkcs12_blob(SSL_CTX *ssl_ctx, SSL *ssl,
  1529. const u8 *blob, size_t len, const char *passwd)
  1530. {
  1531. #ifdef PKCS12_FUNCS
  1532. PKCS12 *p12;
  1533. p12 = d2i_PKCS12(NULL, (OPENSSL_d2i_TYPE) &blob, len);
  1534. if (p12 == NULL) {
  1535. tls_show_errors(MSG_INFO, __func__,
  1536. "Failed to use PKCS#12 blob");
  1537. return -1;
  1538. }
  1539. return tls_parse_pkcs12(ssl_ctx, ssl, p12, passwd);
  1540. #else /* PKCS12_FUNCS */
  1541. wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
  1542. "p12/pfx blobs");
  1543. return -1;
  1544. #endif /* PKCS12_FUNCS */
  1545. }
  1546. #ifndef OPENSSL_NO_ENGINE
  1547. static int tls_engine_get_cert(struct tls_connection *conn,
  1548. const char *cert_id,
  1549. X509 **cert)
  1550. {
  1551. /* this runs after the private key is loaded so no PIN is required */
  1552. struct {
  1553. const char *cert_id;
  1554. X509 *cert;
  1555. } params;
  1556. params.cert_id = cert_id;
  1557. params.cert = NULL;
  1558. if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
  1559. 0, &params, NULL, 1)) {
  1560. wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
  1561. " '%s' [%s]", cert_id,
  1562. ERR_error_string(ERR_get_error(), NULL));
  1563. return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
  1564. }
  1565. if (!params.cert) {
  1566. wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
  1567. " '%s'", cert_id);
  1568. return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
  1569. }
  1570. *cert = params.cert;
  1571. return 0;
  1572. }
  1573. #endif /* OPENSSL_NO_ENGINE */
  1574. static int tls_connection_engine_client_cert(struct tls_connection *conn,
  1575. const char *cert_id)
  1576. {
  1577. #ifndef OPENSSL_NO_ENGINE
  1578. X509 *cert;
  1579. if (tls_engine_get_cert(conn, cert_id, &cert))
  1580. return -1;
  1581. if (!SSL_use_certificate(conn->ssl, cert)) {
  1582. tls_show_errors(MSG_ERROR, __func__,
  1583. "SSL_use_certificate failed");
  1584. X509_free(cert);
  1585. return -1;
  1586. }
  1587. X509_free(cert);
  1588. wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
  1589. "OK");
  1590. return 0;
  1591. #else /* OPENSSL_NO_ENGINE */
  1592. return -1;
  1593. #endif /* OPENSSL_NO_ENGINE */
  1594. }
  1595. static int tls_connection_engine_ca_cert(void *_ssl_ctx,
  1596. struct tls_connection *conn,
  1597. const char *ca_cert_id)
  1598. {
  1599. #ifndef OPENSSL_NO_ENGINE
  1600. X509 *cert;
  1601. SSL_CTX *ssl_ctx = _ssl_ctx;
  1602. if (tls_engine_get_cert(conn, ca_cert_id, &cert))
  1603. return -1;
  1604. /* start off the same as tls_connection_ca_cert */
  1605. X509_STORE_free(ssl_ctx->cert_store);
  1606. ssl_ctx->cert_store = X509_STORE_new();
  1607. if (ssl_ctx->cert_store == NULL) {
  1608. wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
  1609. "certificate store", __func__);
  1610. X509_free(cert);
  1611. return -1;
  1612. }
  1613. if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
  1614. unsigned long err = ERR_peek_error();
  1615. tls_show_errors(MSG_WARNING, __func__,
  1616. "Failed to add CA certificate from engine "
  1617. "to certificate store");
  1618. if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
  1619. ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
  1620. wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
  1621. " already in hash table error",
  1622. __func__);
  1623. } else {
  1624. X509_free(cert);
  1625. return -1;
  1626. }
  1627. }
  1628. X509_free(cert);
  1629. wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
  1630. "to certificate store", __func__);
  1631. SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
  1632. return 0;
  1633. #else /* OPENSSL_NO_ENGINE */
  1634. return -1;
  1635. #endif /* OPENSSL_NO_ENGINE */
  1636. }
  1637. static int tls_connection_engine_private_key(struct tls_connection *conn)
  1638. {
  1639. #ifndef OPENSSL_NO_ENGINE
  1640. if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
  1641. tls_show_errors(MSG_ERROR, __func__,
  1642. "ENGINE: cannot use private key for TLS");
  1643. return -1;
  1644. }
  1645. if (!SSL_check_private_key(conn->ssl)) {
  1646. tls_show_errors(MSG_INFO, __func__,
  1647. "Private key failed verification");
  1648. return -1;
  1649. }
  1650. return 0;
  1651. #else /* OPENSSL_NO_ENGINE */
  1652. wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
  1653. "engine support was not compiled in");
  1654. return -1;
  1655. #endif /* OPENSSL_NO_ENGINE */
  1656. }
  1657. static int tls_connection_private_key(void *_ssl_ctx,
  1658. struct tls_connection *conn,
  1659. const char *private_key,
  1660. const char *private_key_passwd,
  1661. const u8 *private_key_blob,
  1662. size_t private_key_blob_len)
  1663. {
  1664. SSL_CTX *ssl_ctx = _ssl_ctx;
  1665. char *passwd;
  1666. int ok;
  1667. if (private_key == NULL && private_key_blob == NULL)
  1668. return 0;
  1669. if (private_key_passwd) {
  1670. passwd = os_strdup(private_key_passwd);
  1671. if (passwd == NULL)
  1672. return -1;
  1673. } else
  1674. passwd = NULL;
  1675. SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
  1676. SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
  1677. ok = 0;
  1678. while (private_key_blob) {
  1679. if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
  1680. (u8 *) private_key_blob,
  1681. private_key_blob_len) == 1) {
  1682. wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
  1683. "ASN1(EVP_PKEY_RSA) --> OK");
  1684. ok = 1;
  1685. break;
  1686. }
  1687. if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
  1688. (u8 *) private_key_blob,
  1689. private_key_blob_len) == 1) {
  1690. wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
  1691. "ASN1(EVP_PKEY_DSA) --> OK");
  1692. ok = 1;
  1693. break;
  1694. }
  1695. if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
  1696. (u8 *) private_key_blob,
  1697. private_key_blob_len) == 1) {
  1698. wpa_printf(MSG_DEBUG, "OpenSSL: "
  1699. "SSL_use_RSAPrivateKey_ASN1 --> OK");
  1700. ok = 1;
  1701. break;
  1702. }
  1703. if (tls_read_pkcs12_blob(ssl_ctx, conn->ssl, private_key_blob,
  1704. private_key_blob_len, passwd) == 0) {
  1705. wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
  1706. "OK");
  1707. ok = 1;
  1708. break;
  1709. }
  1710. break;
  1711. }
  1712. #ifdef ANDROID
  1713. if (!ok && private_key &&
  1714. os_strncmp("keystore://", private_key, 11) == 0) {
  1715. BIO *bio = BIO_from_keystore(&private_key[11]);
  1716. EVP_PKEY *pkey = NULL;
  1717. if (bio) {
  1718. pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
  1719. BIO_free(bio);
  1720. }
  1721. if (pkey) {
  1722. if (SSL_use_PrivateKey(conn->ssl, pkey) == 1) {
  1723. wpa_printf(MSG_DEBUG, "OpenSSL: Private key "
  1724. "from keystore");
  1725. ok = 1;
  1726. }
  1727. EVP_PKEY_free(pkey);
  1728. }
  1729. }
  1730. #endif /* ANDROID */
  1731. while (!ok && private_key) {
  1732. #ifndef OPENSSL_NO_STDIO
  1733. if (SSL_use_PrivateKey_file(conn->ssl, private_key,
  1734. SSL_FILETYPE_ASN1) == 1) {
  1735. wpa_printf(MSG_DEBUG, "OpenSSL: "
  1736. "SSL_use_PrivateKey_File (DER) --> OK");
  1737. ok = 1;
  1738. break;
  1739. }
  1740. if (SSL_use_PrivateKey_file(conn->ssl, private_key,
  1741. SSL_FILETYPE_PEM) == 1) {
  1742. wpa_printf(MSG_DEBUG, "OpenSSL: "
  1743. "SSL_use_PrivateKey_File (PEM) --> OK");
  1744. ok = 1;
  1745. break;
  1746. }
  1747. #else /* OPENSSL_NO_STDIO */
  1748. wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
  1749. __func__);
  1750. #endif /* OPENSSL_NO_STDIO */
  1751. if (tls_read_pkcs12(ssl_ctx, conn->ssl, private_key, passwd)
  1752. == 0) {
  1753. wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
  1754. "--> OK");
  1755. ok = 1;
  1756. break;
  1757. }
  1758. if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
  1759. wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
  1760. "access certificate store --> OK");
  1761. ok = 1;
  1762. break;
  1763. }
  1764. break;
  1765. }
  1766. if (!ok) {
  1767. tls_show_errors(MSG_INFO, __func__,
  1768. "Failed to load private key");
  1769. os_free(passwd);
  1770. return -1;
  1771. }
  1772. ERR_clear_error();
  1773. SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
  1774. os_free(passwd);
  1775. if (!SSL_check_private_key(conn->ssl)) {
  1776. tls_show_errors(MSG_INFO, __func__, "Private key failed "
  1777. "verification");
  1778. return -1;
  1779. }
  1780. wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
  1781. return 0;
  1782. }
  1783. static int tls_global_private_key(SSL_CTX *ssl_ctx, const char *private_key,
  1784. const char *private_key_passwd)
  1785. {
  1786. char *passwd;
  1787. if (private_key == NULL)
  1788. return 0;
  1789. if (private_key_passwd) {
  1790. passwd = os_strdup(private_key_passwd);
  1791. if (passwd == NULL)
  1792. return -1;
  1793. } else
  1794. passwd = NULL;
  1795. SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
  1796. SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
  1797. if (
  1798. #ifndef OPENSSL_NO_STDIO
  1799. SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
  1800. SSL_FILETYPE_ASN1) != 1 &&
  1801. SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
  1802. SSL_FILETYPE_PEM) != 1 &&
  1803. #endif /* OPENSSL_NO_STDIO */
  1804. tls_read_pkcs12(ssl_ctx, NULL, private_key, passwd)) {
  1805. tls_show_errors(MSG_INFO, __func__,
  1806. "Failed to load private key");
  1807. os_free(passwd);
  1808. ERR_clear_error();
  1809. return -1;
  1810. }
  1811. os_free(passwd);
  1812. ERR_clear_error();
  1813. SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
  1814. if (!SSL_CTX_check_private_key(ssl_ctx)) {
  1815. tls_show_errors(MSG_INFO, __func__,
  1816. "Private key failed verification");
  1817. return -1;
  1818. }
  1819. return 0;
  1820. }
  1821. static int tls_connection_dh(struct tls_connection *conn, const char *dh_file)
  1822. {
  1823. #ifdef OPENSSL_NO_DH
  1824. if (dh_file == NULL)
  1825. return 0;
  1826. wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
  1827. "dh_file specified");
  1828. return -1;
  1829. #else /* OPENSSL_NO_DH */
  1830. DH *dh;
  1831. BIO *bio;
  1832. /* TODO: add support for dh_blob */
  1833. if (dh_file == NULL)
  1834. return 0;
  1835. if (conn == NULL)
  1836. return -1;
  1837. bio = BIO_new_file(dh_file, "r");
  1838. if (bio == NULL) {
  1839. wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
  1840. dh_file, ERR_error_string(ERR_get_error(), NULL));
  1841. return -1;
  1842. }
  1843. dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
  1844. BIO_free(bio);
  1845. #ifndef OPENSSL_NO_DSA
  1846. while (dh == NULL) {
  1847. DSA *dsa;
  1848. wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
  1849. " trying to parse as DSA params", dh_file,
  1850. ERR_error_string(ERR_get_error(), NULL));
  1851. bio = BIO_new_file(dh_file, "r");
  1852. if (bio == NULL)
  1853. break;
  1854. dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  1855. BIO_free(bio);
  1856. if (!dsa) {
  1857. wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
  1858. "'%s': %s", dh_file,
  1859. ERR_error_string(ERR_get_error(), NULL));
  1860. break;
  1861. }
  1862. wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
  1863. dh = DSA_dup_DH(dsa);
  1864. DSA_free(dsa);
  1865. if (dh == NULL) {
  1866. wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
  1867. "params into DH params");
  1868. break;
  1869. }
  1870. break;
  1871. }
  1872. #endif /* !OPENSSL_NO_DSA */
  1873. if (dh == NULL) {
  1874. wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
  1875. "'%s'", dh_file);
  1876. return -1;
  1877. }
  1878. if (SSL_set_tmp_dh(conn->ssl, dh) != 1) {
  1879. wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
  1880. "%s", dh_file,
  1881. ERR_error_string(ERR_get_error(), NULL));
  1882. DH_free(dh);
  1883. return -1;
  1884. }
  1885. DH_free(dh);
  1886. return 0;
  1887. #endif /* OPENSSL_NO_DH */
  1888. }
  1889. static int tls_global_dh(SSL_CTX *ssl_ctx, const char *dh_file)
  1890. {
  1891. #ifdef OPENSSL_NO_DH
  1892. if (dh_file == NULL)
  1893. return 0;
  1894. wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
  1895. "dh_file specified");
  1896. return -1;
  1897. #else /* OPENSSL_NO_DH */
  1898. DH *dh;
  1899. BIO *bio;
  1900. /* TODO: add support for dh_blob */
  1901. if (dh_file == NULL)
  1902. return 0;
  1903. if (ssl_ctx == NULL)
  1904. return -1;
  1905. bio = BIO_new_file(dh_file, "r");
  1906. if (bio == NULL) {
  1907. wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
  1908. dh_file, ERR_error_string(ERR_get_error(), NULL));
  1909. return -1;
  1910. }
  1911. dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
  1912. BIO_free(bio);
  1913. #ifndef OPENSSL_NO_DSA
  1914. while (dh == NULL) {
  1915. DSA *dsa;
  1916. wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
  1917. " trying to parse as DSA params", dh_file,
  1918. ERR_error_string(ERR_get_error(), NULL));
  1919. bio = BIO_new_file(dh_file, "r");
  1920. if (bio == NULL)
  1921. break;
  1922. dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  1923. BIO_free(bio);
  1924. if (!dsa) {
  1925. wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
  1926. "'%s': %s", dh_file,
  1927. ERR_error_string(ERR_get_error(), NULL));
  1928. break;
  1929. }
  1930. wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
  1931. dh = DSA_dup_DH(dsa);
  1932. DSA_free(dsa);
  1933. if (dh == NULL) {
  1934. wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
  1935. "params into DH params");
  1936. break;
  1937. }
  1938. break;
  1939. }
  1940. #endif /* !OPENSSL_NO_DSA */
  1941. if (dh == NULL) {
  1942. wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
  1943. "'%s'", dh_file);
  1944. return -1;
  1945. }
  1946. if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
  1947. wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
  1948. "%s", dh_file,
  1949. ERR_error_string(ERR_get_error(), NULL));
  1950. DH_free(dh);
  1951. return -1;
  1952. }
  1953. DH_free(dh);
  1954. return 0;
  1955. #endif /* OPENSSL_NO_DH */
  1956. }
  1957. int tls_connection_get_keys(void *ssl_ctx, struct tls_connection *conn,
  1958. struct tls_keys *keys)
  1959. {
  1960. SSL *ssl;
  1961. if (conn == NULL || keys == NULL)
  1962. return -1;
  1963. ssl = conn->ssl;
  1964. if (ssl == NULL || ssl->s3 == NULL || ssl->session == NULL)
  1965. return -1;
  1966. os_memset(keys, 0, sizeof(*keys));
  1967. keys->master_key = ssl->session->master_key;
  1968. keys->master_key_len = ssl->session->master_key_length;
  1969. keys->client_random = ssl->s3->client_random;
  1970. keys->client_random_len = SSL3_RANDOM_SIZE;
  1971. keys->server_random = ssl->s3->server_random;
  1972. keys->server_random_len = SSL3_RANDOM_SIZE;
  1973. return 0;
  1974. }
  1975. int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
  1976. const char *label, int server_random_first,
  1977. u8 *out, size_t out_len)
  1978. {
  1979. return -1;
  1980. }
  1981. static struct wpabuf *
  1982. openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data,
  1983. int server)
  1984. {
  1985. int res;
  1986. struct wpabuf *out_data;
  1987. /*
  1988. * Give TLS handshake data from the server (if available) to OpenSSL
  1989. * for processing.
  1990. */
  1991. if (in_data &&
  1992. BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
  1993. < 0) {
  1994. tls_show_errors(MSG_INFO, __func__,
  1995. "Handshake failed - BIO_write");
  1996. return NULL;
  1997. }
  1998. /* Initiate TLS handshake or continue the existing handshake */
  1999. if (server)
  2000. res = SSL_accept(conn->ssl);
  2001. else
  2002. res = SSL_connect(conn->ssl);
  2003. if (res != 1) {
  2004. int err = SSL_get_error(conn->ssl, res);
  2005. if (err == SSL_ERROR_WANT_READ)
  2006. wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
  2007. "more data");
  2008. else if (err == SSL_ERROR_WANT_WRITE)
  2009. wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
  2010. "write");
  2011. else {
  2012. tls_show_errors(MSG_INFO, __func__, "SSL_connect");
  2013. conn->failed++;
  2014. }
  2015. }
  2016. /* Get the TLS handshake data to be sent to the server */
  2017. res = BIO_ctrl_pending(conn->ssl_out);
  2018. wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
  2019. out_data = wpabuf_alloc(res);
  2020. if (out_data == NULL) {
  2021. wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
  2022. "handshake output (%d bytes)", res);
  2023. if (BIO_reset(conn->ssl_out) < 0) {
  2024. tls_show_errors(MSG_INFO, __func__,
  2025. "BIO_reset failed");
  2026. }
  2027. return NULL;
  2028. }
  2029. res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
  2030. res);
  2031. if (res < 0) {
  2032. tls_show_errors(MSG_INFO, __func__,
  2033. "Handshake failed - BIO_read");
  2034. if (BIO_reset(conn->ssl_out) < 0) {
  2035. tls_show_errors(MSG_INFO, __func__,
  2036. "BIO_reset failed");
  2037. }
  2038. wpabuf_free(out_data);
  2039. return NULL;
  2040. }
  2041. wpabuf_put(out_data, res);
  2042. return out_data;
  2043. }
  2044. static struct wpabuf *
  2045. openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
  2046. {
  2047. struct wpabuf *appl_data;
  2048. int res;
  2049. appl_data = wpabuf_alloc(max_len + 100);
  2050. if (appl_data == NULL)
  2051. return NULL;
  2052. res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
  2053. wpabuf_size(appl_data));
  2054. if (res < 0) {
  2055. int err = SSL_get_error(conn->ssl, res);
  2056. if (err == SSL_ERROR_WANT_READ ||
  2057. err == SSL_ERROR_WANT_WRITE) {
  2058. wpa_printf(MSG_DEBUG, "SSL: No Application Data "
  2059. "included");
  2060. } else {
  2061. tls_show_errors(MSG_INFO, __func__,
  2062. "Failed to read possible "
  2063. "Application Data");
  2064. }
  2065. wpabuf_free(appl_data);
  2066. return NULL;
  2067. }
  2068. wpabuf_put(appl_data, res);
  2069. wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
  2070. "message", appl_data);
  2071. return appl_data;
  2072. }
  2073. static struct wpabuf *
  2074. openssl_connection_handshake(struct tls_connection *conn,
  2075. const struct wpabuf *in_data,
  2076. struct wpabuf **appl_data, int server)
  2077. {
  2078. struct wpabuf *out_data;
  2079. if (appl_data)
  2080. *appl_data = NULL;
  2081. out_data = openssl_handshake(conn, in_data, server);
  2082. if (out_data == NULL)
  2083. return NULL;
  2084. if (SSL_is_init_finished(conn->ssl) && appl_data && in_data)
  2085. *appl_data = openssl_get_appl_data(conn, wpabuf_len(in_data));
  2086. return out_data;
  2087. }
  2088. struct wpabuf *
  2089. tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
  2090. const struct wpabuf *in_data,
  2091. struct wpabuf **appl_data)
  2092. {
  2093. return openssl_connection_handshake(conn, in_data, appl_data, 0);
  2094. }
  2095. struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
  2096. struct tls_connection *conn,
  2097. const struct wpabuf *in_data,
  2098. struct wpabuf **appl_data)
  2099. {
  2100. return openssl_connection_handshake(conn, in_data, appl_data, 1);
  2101. }
  2102. struct wpabuf * tls_connection_encrypt(void *tls_ctx,
  2103. struct tls_connection *conn,
  2104. const struct wpabuf *in_data)
  2105. {
  2106. int res;
  2107. struct wpabuf *buf;
  2108. if (conn == NULL)
  2109. return NULL;
  2110. /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
  2111. if ((res = BIO_reset(conn->ssl_in)) < 0 ||
  2112. (res = BIO_reset(conn->ssl_out)) < 0) {
  2113. tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
  2114. return NULL;
  2115. }
  2116. res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
  2117. if (res < 0) {
  2118. tls_show_errors(MSG_INFO, __func__,
  2119. "Encryption failed - SSL_write");
  2120. return NULL;
  2121. }
  2122. /* Read encrypted data to be sent to the server */
  2123. buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
  2124. if (buf == NULL)
  2125. return NULL;
  2126. res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
  2127. if (res < 0) {
  2128. tls_show_errors(MSG_INFO, __func__,
  2129. "Encryption failed - BIO_read");
  2130. wpabuf_free(buf);
  2131. return NULL;
  2132. }
  2133. wpabuf_put(buf, res);
  2134. return buf;
  2135. }
  2136. struct wpabuf * tls_connection_decrypt(void *tls_ctx,
  2137. struct tls_connection *conn,
  2138. const struct wpabuf *in_data)
  2139. {
  2140. int res;
  2141. struct wpabuf *buf;
  2142. /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
  2143. res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
  2144. wpabuf_len(in_data));
  2145. if (res < 0) {
  2146. tls_show_errors(MSG_INFO, __func__,
  2147. "Decryption failed - BIO_write");
  2148. return NULL;
  2149. }
  2150. if (BIO_reset(conn->ssl_out) < 0) {
  2151. tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
  2152. return NULL;
  2153. }
  2154. /* Read decrypted data for further processing */
  2155. /*
  2156. * Even though we try to disable TLS compression, it is possible that
  2157. * this cannot be done with all TLS libraries. Add extra buffer space
  2158. * to handle the possibility of the decrypted data being longer than
  2159. * input data.
  2160. */
  2161. buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
  2162. if (buf == NULL)
  2163. return NULL;
  2164. res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
  2165. if (res < 0) {
  2166. tls_show_errors(MSG_INFO, __func__,
  2167. "Decryption failed - SSL_read");
  2168. wpabuf_free(buf);
  2169. return NULL;
  2170. }
  2171. wpabuf_put(buf, res);
  2172. return buf;
  2173. }
  2174. int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
  2175. {
  2176. return conn ? conn->ssl->hit : 0;
  2177. }
  2178. int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
  2179. u8 *ciphers)
  2180. {
  2181. char buf[100], *pos, *end;
  2182. u8 *c;
  2183. int ret;
  2184. if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
  2185. return -1;
  2186. buf[0] = '\0';
  2187. pos = buf;
  2188. end = pos + sizeof(buf);
  2189. c = ciphers;
  2190. while (*c != TLS_CIPHER_NONE) {
  2191. const char *suite;
  2192. switch (*c) {
  2193. case TLS_CIPHER_RC4_SHA:
  2194. suite = "RC4-SHA";
  2195. break;
  2196. case TLS_CIPHER_AES128_SHA:
  2197. suite = "AES128-SHA";
  2198. break;
  2199. case TLS_CIPHER_RSA_DHE_AES128_SHA:
  2200. suite = "DHE-RSA-AES128-SHA";
  2201. break;
  2202. case TLS_CIPHER_ANON_DH_AES128_SHA:
  2203. suite = "ADH-AES128-SHA";
  2204. break;
  2205. default:
  2206. wpa_printf(MSG_DEBUG, "TLS: Unsupported "
  2207. "cipher selection: %d", *c);
  2208. return -1;
  2209. }
  2210. ret = os_snprintf(pos, end - pos, ":%s", suite);
  2211. if (ret < 0 || ret >= end - pos)
  2212. break;
  2213. pos += ret;
  2214. c++;
  2215. }
  2216. wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
  2217. if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
  2218. tls_show_errors(MSG_INFO, __func__,
  2219. "Cipher suite configuration failed");
  2220. return -1;
  2221. }
  2222. return 0;
  2223. }
  2224. int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
  2225. char *buf, size_t buflen)
  2226. {
  2227. const char *name;
  2228. if (conn == NULL || conn->ssl == NULL)
  2229. return -1;
  2230. name = SSL_get_cipher(conn->ssl);
  2231. if (name == NULL)
  2232. return -1;
  2233. os_strlcpy(buf, name, buflen);
  2234. return 0;
  2235. }
  2236. int tls_connection_enable_workaround(void *ssl_ctx,
  2237. struct tls_connection *conn)
  2238. {
  2239. SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
  2240. return 0;
  2241. }
  2242. #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
  2243. /* ClientHello TLS extensions require a patch to openssl, so this function is
  2244. * commented out unless explicitly needed for EAP-FAST in order to be able to
  2245. * build this file with unmodified openssl. */
  2246. int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
  2247. int ext_type, const u8 *data,
  2248. size_t data_len)
  2249. {
  2250. if (conn == NULL || conn->ssl == NULL || ext_type != 35)
  2251. return -1;
  2252. #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
  2253. if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
  2254. data_len) != 1)
  2255. return -1;
  2256. #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
  2257. if (SSL_set_hello_extension(conn->ssl, ext_type, (void *) data,
  2258. data_len) != 1)
  2259. return -1;
  2260. #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
  2261. return 0;
  2262. }
  2263. #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
  2264. int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
  2265. {
  2266. if (conn == NULL)
  2267. return -1;
  2268. return conn->failed;
  2269. }
  2270. int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
  2271. {
  2272. if (conn == NULL)
  2273. return -1;
  2274. return conn->read_alerts;
  2275. }
  2276. int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
  2277. {
  2278. if (conn == NULL)
  2279. return -1;
  2280. return conn->write_alerts;
  2281. }
  2282. int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
  2283. const struct tls_connection_params *params)
  2284. {
  2285. int ret;
  2286. unsigned long err;
  2287. if (conn == NULL)
  2288. return -1;
  2289. while ((err = ERR_get_error())) {
  2290. wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
  2291. __func__, ERR_error_string(err, NULL));
  2292. }
  2293. if (params->engine) {
  2294. wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine");
  2295. ret = tls_engine_init(conn, params->engine_id, params->pin,
  2296. params->key_id, params->cert_id,
  2297. params->ca_cert_id);
  2298. if (ret)
  2299. return ret;
  2300. }
  2301. if (tls_connection_set_subject_match(conn,
  2302. params->subject_match,
  2303. params->altsubject_match))
  2304. return -1;
  2305. if (params->engine && params->ca_cert_id) {
  2306. if (tls_connection_engine_ca_cert(tls_ctx, conn,
  2307. params->ca_cert_id))
  2308. return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
  2309. } else if (tls_connection_ca_cert(tls_ctx, conn, params->ca_cert,
  2310. params->ca_cert_blob,
  2311. params->ca_cert_blob_len,
  2312. params->ca_path))
  2313. return -1;
  2314. if (params->engine && params->cert_id) {
  2315. if (tls_connection_engine_client_cert(conn, params->cert_id))
  2316. return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
  2317. } else if (tls_connection_client_cert(conn, params->client_cert,
  2318. params->client_cert_blob,
  2319. params->client_cert_blob_len))
  2320. return -1;
  2321. if (params->engine && params->key_id) {
  2322. wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
  2323. if (tls_connection_engine_private_key(conn))
  2324. return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
  2325. } else if (tls_connection_private_key(tls_ctx, conn,
  2326. params->private_key,
  2327. params->private_key_passwd,
  2328. params->private_key_blob,
  2329. params->private_key_blob_len)) {
  2330. wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
  2331. params->private_key);
  2332. return -1;
  2333. }
  2334. if (tls_connection_dh(conn, params->dh_file)) {
  2335. wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
  2336. params->dh_file);
  2337. return -1;
  2338. }
  2339. conn->flags = params->flags;
  2340. tls_get_errors(tls_ctx);
  2341. return 0;
  2342. }
  2343. int tls_global_set_params(void *tls_ctx,
  2344. const struct tls_connection_params *params)
  2345. {
  2346. SSL_CTX *ssl_ctx = tls_ctx;
  2347. unsigned long err;
  2348. while ((err = ERR_get_error())) {
  2349. wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
  2350. __func__, ERR_error_string(err, NULL));
  2351. }
  2352. if (tls_global_ca_cert(ssl_ctx, params->ca_cert))
  2353. return -1;
  2354. if (tls_global_client_cert(ssl_ctx, params->client_cert))
  2355. return -1;
  2356. if (tls_global_private_key(ssl_ctx, params->private_key,
  2357. params->private_key_passwd))
  2358. return -1;
  2359. if (tls_global_dh(ssl_ctx, params->dh_file)) {
  2360. wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
  2361. params->dh_file);
  2362. return -1;
  2363. }
  2364. return 0;
  2365. }
  2366. int tls_connection_get_keyblock_size(void *tls_ctx,
  2367. struct tls_connection *conn)
  2368. {
  2369. const EVP_CIPHER *c;
  2370. const EVP_MD *h;
  2371. if (conn == NULL || conn->ssl == NULL ||
  2372. conn->ssl->enc_read_ctx == NULL ||
  2373. conn->ssl->enc_read_ctx->cipher == NULL ||
  2374. conn->ssl->read_hash == NULL)
  2375. return -1;
  2376. c = conn->ssl->enc_read_ctx->cipher;
  2377. #if OPENSSL_VERSION_NUMBER >= 0x00909000L
  2378. h = EVP_MD_CTX_md(conn->ssl->read_hash);
  2379. #else
  2380. h = conn->ssl->read_hash;
  2381. #endif
  2382. return 2 * (EVP_CIPHER_key_length(c) +
  2383. EVP_MD_size(h) +
  2384. EVP_CIPHER_iv_length(c));
  2385. }
  2386. unsigned int tls_capabilities(void *tls_ctx)
  2387. {
  2388. return 0;
  2389. }
  2390. int tls_connection_set_ia(void *tls_ctx, struct tls_connection *conn,
  2391. int tls_ia)
  2392. {
  2393. return -1;
  2394. }
  2395. struct wpabuf * tls_connection_ia_send_phase_finished(
  2396. void *tls_ctx, struct tls_connection *conn, int final)
  2397. {
  2398. return NULL;
  2399. }
  2400. int tls_connection_ia_final_phase_finished(void *tls_ctx,
  2401. struct tls_connection *conn)
  2402. {
  2403. return -1;
  2404. }
  2405. int tls_connection_ia_permute_inner_secret(void *tls_ctx,
  2406. struct tls_connection *conn,
  2407. const u8 *key, size_t key_len)
  2408. {
  2409. return -1;
  2410. }
  2411. #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
  2412. /* Pre-shared secred requires a patch to openssl, so this function is
  2413. * commented out unless explicitly needed for EAP-FAST in order to be able to
  2414. * build this file with unmodified openssl. */
  2415. static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
  2416. STACK_OF(SSL_CIPHER) *peer_ciphers,
  2417. SSL_CIPHER **cipher, void *arg)
  2418. {
  2419. struct tls_connection *conn = arg;
  2420. int ret;
  2421. if (conn == NULL || conn->session_ticket_cb == NULL)
  2422. return 0;
  2423. ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
  2424. conn->session_ticket,
  2425. conn->session_ticket_len,
  2426. s->s3->client_random,
  2427. s->s3->server_random, secret);
  2428. os_free(conn->session_ticket);
  2429. conn->session_ticket = NULL;
  2430. if (ret <= 0)
  2431. return 0;
  2432. *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
  2433. return 1;
  2434. }
  2435. #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
  2436. static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
  2437. int len, void *arg)
  2438. {
  2439. struct tls_connection *conn = arg;
  2440. if (conn == NULL || conn->session_ticket_cb == NULL)
  2441. return 0;
  2442. wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
  2443. os_free(conn->session_ticket);
  2444. conn->session_ticket = NULL;
  2445. wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
  2446. "extension", data, len);
  2447. conn->session_ticket = os_malloc(len);
  2448. if (conn->session_ticket == NULL)
  2449. return 0;
  2450. os_memcpy(conn->session_ticket, data, len);
  2451. conn->session_ticket_len = len;
  2452. return 1;
  2453. }
  2454. #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
  2455. #ifdef SSL_OP_NO_TICKET
  2456. static void tls_hello_ext_cb(SSL *s, int client_server, int type,
  2457. unsigned char *data, int len, void *arg)
  2458. {
  2459. struct tls_connection *conn = arg;
  2460. if (conn == NULL || conn->session_ticket_cb == NULL)
  2461. return;
  2462. wpa_printf(MSG_DEBUG, "OpenSSL: %s: type=%d length=%d", __func__,
  2463. type, len);
  2464. if (type == TLSEXT_TYPE_session_ticket && !client_server) {
  2465. os_free(conn->session_ticket);
  2466. conn->session_ticket = NULL;
  2467. wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
  2468. "extension", data, len);
  2469. conn->session_ticket = os_malloc(len);
  2470. if (conn->session_ticket == NULL)
  2471. return;
  2472. os_memcpy(conn->session_ticket, data, len);
  2473. conn->session_ticket_len = len;
  2474. }
  2475. }
  2476. #else /* SSL_OP_NO_TICKET */
  2477. static int tls_hello_ext_cb(SSL *s, TLS_EXTENSION *ext, void *arg)
  2478. {
  2479. struct tls_connection *conn = arg;
  2480. if (conn == NULL || conn->session_ticket_cb == NULL)
  2481. return 0;
  2482. wpa_printf(MSG_DEBUG, "OpenSSL: %s: type=%d length=%d", __func__,
  2483. ext->type, ext->length);
  2484. os_free(conn->session_ticket);
  2485. conn->session_ticket = NULL;
  2486. if (ext->type == 35) {
  2487. wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
  2488. "extension", ext->data, ext->length);
  2489. conn->session_ticket = os_malloc(ext->length);
  2490. if (conn->session_ticket == NULL)
  2491. return SSL_AD_INTERNAL_ERROR;
  2492. os_memcpy(conn->session_ticket, ext->data, ext->length);
  2493. conn->session_ticket_len = ext->length;
  2494. }
  2495. return 0;
  2496. }
  2497. #endif /* SSL_OP_NO_TICKET */
  2498. #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
  2499. #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
  2500. int tls_connection_set_session_ticket_cb(void *tls_ctx,
  2501. struct tls_connection *conn,
  2502. tls_session_ticket_cb cb,
  2503. void *ctx)
  2504. {
  2505. #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
  2506. conn->session_ticket_cb = cb;
  2507. conn->session_ticket_cb_ctx = ctx;
  2508. if (cb) {
  2509. if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
  2510. conn) != 1)
  2511. return -1;
  2512. #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
  2513. SSL_set_session_ticket_ext_cb(conn->ssl,
  2514. tls_session_ticket_ext_cb, conn);
  2515. #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
  2516. #ifdef SSL_OP_NO_TICKET
  2517. SSL_set_tlsext_debug_callback(conn->ssl, tls_hello_ext_cb);
  2518. SSL_set_tlsext_debug_arg(conn->ssl, conn);
  2519. #else /* SSL_OP_NO_TICKET */
  2520. if (SSL_set_hello_extension_cb(conn->ssl, tls_hello_ext_cb,
  2521. conn) != 1)
  2522. return -1;
  2523. #endif /* SSL_OP_NO_TICKET */
  2524. #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
  2525. } else {
  2526. if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
  2527. return -1;
  2528. #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
  2529. SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
  2530. #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
  2531. #ifdef SSL_OP_NO_TICKET
  2532. SSL_set_tlsext_debug_callback(conn->ssl, NULL);
  2533. SSL_set_tlsext_debug_arg(conn->ssl, conn);
  2534. #else /* SSL_OP_NO_TICKET */
  2535. if (SSL_set_hello_extension_cb(conn->ssl, NULL, NULL) != 1)
  2536. return -1;
  2537. #endif /* SSL_OP_NO_TICKET */
  2538. #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
  2539. }
  2540. return 0;
  2541. #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
  2542. return -1;
  2543. #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
  2544. }