tls_openssl.c 106 KB

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