tls_openssl.c 73 KB

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