tls_openssl.c 84 KB

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