tls.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141
  1. /*
  2. * Embedded Linux library
  3. *
  4. * Copyright (C) 2015 Intel Corporation. All rights reserved.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #define _GNU_SOURCE
  24. #include <time.h>
  25. #include <stdlib.h>
  26. #include <errno.h>
  27. #include <stdio.h>
  28. #include <strings.h>
  29. #include "useful.h"
  30. #include "private.h"
  31. #include "tls.h"
  32. #include "checksum.h"
  33. #include "cipher.h"
  34. #include "random.h"
  35. #include "queue.h"
  36. #include "pem.h"
  37. #include "pem-private.h"
  38. #include "asn1-private.h"
  39. #include "cert.h"
  40. #include "cert-private.h"
  41. #include "tls-private.h"
  42. #include "key.h"
  43. #include "strv.h"
  44. #include "missing.h"
  45. #include "string.h"
  46. bool tls10_prf(const void *secret, size_t secret_len,
  47. const char *label,
  48. const void *seed, size_t seed_len,
  49. uint8_t *out, size_t out_len)
  50. {
  51. uint8_t p_hash2[out_len];
  52. uint8_t l_s1 = (secret_len + 1) / 2;
  53. unsigned int i;
  54. /*
  55. * RFC2246 section 5:
  56. * S1 and S2 are the two halves of the secret, and each is the same
  57. * length. S1 is taken from the first half of the secret, S2 from the
  58. * second half. Their length is created by rounding up the length of
  59. * the overall secret, divided by two; thus, if the original secret is
  60. * an odd number of bytes long, the last byte of S1 will be the same as
  61. * the first byte of S2.
  62. */
  63. if (!tls12_prf(L_CHECKSUM_MD5, secret, l_s1,
  64. label, seed, seed_len,
  65. out, out_len))
  66. return false;
  67. if (secret_len > 0)
  68. secret += secret_len - l_s1;
  69. if (!tls12_prf(L_CHECKSUM_SHA1, secret, l_s1,
  70. label, seed, seed_len,
  71. p_hash2, out_len))
  72. return false;
  73. for (i = 0; i < out_len; i++)
  74. out[i] ^= p_hash2[i];
  75. return true;
  76. }
  77. bool tls12_prf(enum l_checksum_type type,
  78. const void *secret, size_t secret_len,
  79. const char *label,
  80. const void *seed, size_t seed_len,
  81. uint8_t *out, size_t out_len)
  82. {
  83. struct l_checksum *hmac = l_checksum_new_hmac(type, secret, secret_len);
  84. size_t a_len, chunk_len, prfseed_len = strlen(label) + seed_len;
  85. uint8_t a[64 + prfseed_len], prfseed[prfseed_len];
  86. if (!hmac)
  87. return false;
  88. /* Generate the hash seed or A(0) as label + seed */
  89. memcpy(prfseed, label, strlen(label));
  90. memcpy(prfseed + strlen(label), seed, seed_len);
  91. memcpy(a, prfseed, prfseed_len);
  92. a_len = prfseed_len;
  93. while (out_len) {
  94. /* Generate A(i) */
  95. l_checksum_reset(hmac);
  96. l_checksum_update(hmac, a, a_len);
  97. a_len = l_checksum_get_digest(hmac, a, sizeof(a));
  98. /* Append seed & generate output */
  99. memcpy(a + a_len, prfseed, prfseed_len);
  100. l_checksum_reset(hmac);
  101. l_checksum_update(hmac, a, a_len + prfseed_len);
  102. chunk_len = l_checksum_get_digest(hmac, out, out_len);
  103. out += chunk_len;
  104. out_len -= chunk_len;
  105. }
  106. l_checksum_free(hmac);
  107. return true;
  108. }
  109. static bool tls_prf_get_bytes(struct l_tls *tls,
  110. const void *secret, size_t secret_len,
  111. const char *label,
  112. const void *seed, size_t seed_len,
  113. uint8_t *buf, size_t len)
  114. {
  115. if (tls->negotiated_version >= L_TLS_V12)
  116. return tls12_prf(tls->prf_hmac->l_id,
  117. secret, secret_len, label,
  118. seed, seed_len, buf, len);
  119. return tls10_prf(secret, secret_len, label, seed, seed_len,
  120. buf, len);
  121. }
  122. LIB_EXPORT bool l_tls_prf_get_bytes(struct l_tls *tls, bool use_master_secret,
  123. const char *label, uint8_t *buf, size_t len)
  124. {
  125. uint8_t seed[64];
  126. bool r;
  127. if (unlikely(!tls || !tls->prf_hmac))
  128. return false;
  129. memcpy(seed + 0, tls->pending.client_random, 32);
  130. memcpy(seed + 32, tls->pending.server_random, 32);
  131. if (use_master_secret)
  132. r = tls_prf_get_bytes(tls, tls->pending.master_secret, 48,
  133. label, seed, 64, buf, len);
  134. else
  135. r = tls_prf_get_bytes(tls, "", 0, label, seed, 64, buf, len);
  136. explicit_bzero(seed, 64);
  137. return r;
  138. }
  139. static void tls_write_random(uint8_t *buf)
  140. {
  141. l_put_be32(time(NULL), buf);
  142. l_getrandom(buf + 4, 28);
  143. }
  144. static void tls_drop_handshake_hash(struct l_tls *tls,
  145. enum handshake_hash_type hash)
  146. {
  147. if (tls->handshake_hash[hash]) {
  148. l_checksum_free(tls->handshake_hash[hash]);
  149. tls->handshake_hash[hash] = NULL;
  150. }
  151. }
  152. static void tls_reset_handshake(struct l_tls *tls)
  153. {
  154. enum handshake_hash_type hash;
  155. explicit_bzero(tls->pending.key_block, sizeof(tls->pending.key_block));
  156. if (tls->pending.cipher_suite &&
  157. tls->pending.cipher_suite->key_xchg->free_params)
  158. tls->pending.cipher_suite->key_xchg->free_params(tls);
  159. l_cert_free(tls->peer_cert);
  160. l_key_free(tls->peer_pubkey);
  161. tls->peer_cert = NULL;
  162. tls->peer_pubkey = NULL;
  163. tls->peer_pubkey_size = 0;
  164. tls->negotiated_curve = NULL;
  165. tls->negotiated_ff_group = NULL;
  166. for (hash = 0; hash < __HANDSHAKE_HASH_COUNT; hash++)
  167. tls_drop_handshake_hash(tls, hash);
  168. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_START);
  169. tls->cert_requested = 0;
  170. tls->cert_sent = 0;
  171. }
  172. static void tls_cleanup_handshake(struct l_tls *tls)
  173. {
  174. explicit_bzero(tls->pending.client_random, 32);
  175. explicit_bzero(tls->pending.server_random, 32);
  176. explicit_bzero(tls->pending.master_secret, 48);
  177. }
  178. static bool tls_change_cipher_spec(struct l_tls *tls, bool txrx,
  179. const char **error)
  180. {
  181. struct tls_bulk_encryption_algorithm *enc;
  182. struct tls_mac_algorithm *mac;
  183. int key_offset;
  184. static char error_buf[200];
  185. if (tls->cipher_type[txrx] == TLS_CIPHER_AEAD) {
  186. if (tls->aead_cipher[txrx]) {
  187. l_aead_cipher_free(tls->aead_cipher[txrx]);
  188. tls->aead_cipher[txrx] = NULL;
  189. }
  190. } else {
  191. if (tls->cipher[txrx]) {
  192. l_cipher_free(tls->cipher[txrx]);
  193. tls->cipher[txrx] = NULL;
  194. }
  195. }
  196. tls->cipher_type[txrx] = TLS_CIPHER_STREAM;
  197. if (tls->mac[txrx]) {
  198. l_checksum_free(tls->mac[txrx]);
  199. tls->mac[txrx] = NULL;
  200. }
  201. tls->mac_length[txrx] = 0;
  202. tls->block_length[txrx] = 0;
  203. tls->record_iv_length[txrx] = 0;
  204. if (tls->fixed_iv_length[txrx]) {
  205. explicit_bzero(tls->fixed_iv[txrx], tls->fixed_iv_length[txrx]);
  206. tls->fixed_iv_length[txrx] = 0;
  207. }
  208. tls->auth_tag_length[txrx] = 0;
  209. tls->seq_num[txrx] = 0;
  210. tls->cipher_suite[txrx] = tls->pending.cipher_suite;
  211. if (!tls->cipher_suite[txrx])
  212. return true;
  213. key_offset = 0;
  214. if (tls->cipher_suite[txrx]->mac) {
  215. mac = tls->cipher_suite[txrx]->mac;
  216. /* Server write / client read is 2nd in the key block */
  217. if ((tls->server && txrx) || (!tls->server && !txrx))
  218. key_offset += mac->mac_length;
  219. tls->mac[txrx] = l_checksum_new_hmac(mac->hmac_type,
  220. tls->pending.key_block +
  221. key_offset, mac->mac_length);
  222. /* Wipe out the now unneeded part of the key block */
  223. explicit_bzero(tls->pending.key_block + key_offset,
  224. mac->mac_length);
  225. if (!tls->mac[txrx]) {
  226. if (error) {
  227. *error = error_buf;
  228. snprintf(error_buf, sizeof(error_buf),
  229. "Can't create %s's %s HMAC",
  230. tls->cipher_suite[txrx]->name,
  231. txrx ? "Tx" : "Rx");
  232. }
  233. return false;
  234. }
  235. tls->mac_length[txrx] = mac->mac_length;
  236. key_offset = 2 * mac->mac_length;
  237. }
  238. if (tls->cipher_suite[txrx]->encryption) {
  239. void *cipher;
  240. enc = tls->cipher_suite[txrx]->encryption;
  241. /* Server write / client read is 4th in the key block */
  242. if ((tls->server && txrx) || (!tls->server && !txrx))
  243. key_offset += enc->key_length;
  244. if (enc->cipher_type == TLS_CIPHER_AEAD) {
  245. cipher = l_aead_cipher_new(enc->l_aead_id,
  246. tls->pending.key_block +
  247. key_offset, enc->key_length,
  248. enc->auth_tag_length);
  249. tls->aead_cipher[txrx] = cipher;
  250. } else {
  251. cipher = l_cipher_new(enc->l_id,
  252. tls->pending.key_block +
  253. key_offset, enc->key_length);
  254. tls->cipher[txrx] = cipher;
  255. }
  256. /* Wipe out the now unneeded part of the key block */
  257. explicit_bzero(tls->pending.key_block + key_offset,
  258. enc->key_length);
  259. if (!cipher) {
  260. if (error) {
  261. *error = error_buf;
  262. snprintf(error_buf, sizeof(error_buf),
  263. "Can't create %s's %s cipher",
  264. tls->cipher_suite[txrx]->name,
  265. txrx ? "Tx" : "Rx");
  266. }
  267. return false;
  268. }
  269. tls->cipher_type[txrx] = enc->cipher_type;
  270. tls->record_iv_length[txrx] = enc->iv_length -
  271. enc->fixed_iv_length;
  272. tls->block_length[txrx] = enc->block_length;
  273. tls->auth_tag_length[txrx] = enc->auth_tag_length;
  274. if ((tls->server && txrx) || (!tls->server && !txrx))
  275. key_offset += enc->key_length;
  276. else
  277. key_offset += 2 * enc->key_length;
  278. }
  279. if (tls->negotiated_version <= L_TLS_V10 &&
  280. tls->cipher_suite[txrx]->encryption &&
  281. tls->cipher_suite[txrx]->encryption->cipher_type ==
  282. TLS_CIPHER_BLOCK) {
  283. enc = tls->cipher_suite[txrx]->encryption;
  284. /* Server write / client read is 6th in the key block */
  285. if ((tls->server && txrx) || (!tls->server && !txrx))
  286. key_offset += enc->iv_length;
  287. l_cipher_set_iv(tls->cipher[txrx], tls->pending.key_block +
  288. key_offset, enc->iv_length);
  289. /* Wipe out the now unneeded part of the key block */
  290. explicit_bzero(tls->pending.key_block + key_offset,
  291. enc->iv_length);
  292. } else if (tls->cipher_suite[txrx]->encryption &&
  293. tls->cipher_suite[txrx]->encryption->fixed_iv_length) {
  294. enc = tls->cipher_suite[txrx]->encryption;
  295. /* Server write / client read is 6th in the key block */
  296. if ((tls->server && txrx) || (!tls->server && !txrx))
  297. key_offset += enc->fixed_iv_length;
  298. tls->fixed_iv_length[txrx] = enc->fixed_iv_length;
  299. memcpy(tls->fixed_iv[txrx], tls->pending.key_block + key_offset,
  300. enc->fixed_iv_length);
  301. /* Wipe out the now unneeded part of the key block */
  302. explicit_bzero(tls->pending.key_block + key_offset,
  303. enc->fixed_iv_length);
  304. }
  305. return true;
  306. }
  307. static void tls_reset_cipher_spec(struct l_tls *tls, bool txrx)
  308. {
  309. /* Reset everything to the TLS_NULL_WITH_NULL_NULL state */
  310. tls->pending.cipher_suite = NULL;
  311. tls_change_cipher_spec(tls, txrx, NULL);
  312. }
  313. bool tls_cipher_suite_is_compatible(struct l_tls *tls,
  314. const struct tls_cipher_suite *suite,
  315. const char **error)
  316. {
  317. static char error_buf[200];
  318. struct l_cert *leaf;
  319. enum l_tls_version min_version =
  320. tls->negotiated_version ?: tls->min_version;
  321. enum l_tls_version max_version =
  322. tls->negotiated_version ?: tls->max_version;
  323. if (suite->encryption &&
  324. suite->encryption->cipher_type == TLS_CIPHER_AEAD) {
  325. if (max_version < L_TLS_V12) {
  326. if (error) {
  327. *error = error_buf;
  328. snprintf(error_buf, sizeof(error_buf),
  329. "Cipher suite %s uses an AEAD "
  330. "cipher (TLS 1.2+) but "
  331. TLS_VER_FMT
  332. " was negotiated or is the max "
  333. "version allowed", suite->name,
  334. TLS_VER_ARGS(tls->max_version));
  335. }
  336. return false;
  337. }
  338. if (!l_aead_cipher_is_supported(suite->encryption->l_aead_id)) {
  339. if (error) {
  340. *error = error_buf;
  341. snprintf(error_buf, sizeof(error_buf),
  342. "Cipher suite %s's AEAD cipher "
  343. "algorithm not supported by "
  344. "the kernel", suite->name);
  345. }
  346. return false;
  347. }
  348. } else if (suite->encryption) { /* Block or stream cipher */
  349. if (!l_cipher_is_supported(suite->encryption->l_id)) {
  350. if (error) {
  351. *error = error_buf;
  352. snprintf(error_buf, sizeof(error_buf),
  353. "Cipher suite %s's block/stream"
  354. " cipher algorithm not "
  355. "supported by the kernel",
  356. suite->name);
  357. }
  358. return false;
  359. }
  360. }
  361. if (suite->mac &&
  362. !l_checksum_is_supported(suite->mac->hmac_type, true)) {
  363. if (error) {
  364. *error = error_buf;
  365. snprintf(error_buf, sizeof(error_buf),
  366. "Cipher suite %s's HMAC algorithm not "
  367. "supported by the kernel", suite->name);
  368. }
  369. return false;
  370. }
  371. if (
  372. (max_version < L_TLS_V12 &&
  373. (!l_checksum_is_supported(L_CHECKSUM_MD5, true) ||
  374. !l_checksum_is_supported(L_CHECKSUM_SHA1, true))) ||
  375. (min_version >= L_TLS_V12 &&
  376. !l_checksum_is_supported(
  377. suite->prf_hmac != L_CHECKSUM_NONE ?
  378. suite->prf_hmac : L_CHECKSUM_SHA256,
  379. true))) {
  380. if (error) {
  381. *error = error_buf;
  382. snprintf(error_buf, sizeof(error_buf),
  383. "Cipher suite %s's PRF algorithm not "
  384. "supported by the kernel", suite->name);
  385. }
  386. return false;
  387. }
  388. if (suite->key_xchg->need_ffdh &&
  389. !l_key_is_supported(L_KEY_FEATURE_DH)) {
  390. if (error) {
  391. *error = error_buf;
  392. snprintf(error_buf, sizeof(error_buf),
  393. "Cipher suite %s's key exchange "
  394. "mechanism needs kernel DH support",
  395. suite->name);
  396. }
  397. return false;
  398. }
  399. /*
  400. * If the certificate is compatible with the signature algorithm it
  401. * also must be compatible with the key exchange mechanism because
  402. * the cipher suites are defined so that the same certificates can
  403. * be used by both.
  404. */
  405. leaf = l_certchain_get_leaf(tls->cert);
  406. if (leaf && suite->signature &&
  407. !suite->signature->validate_cert_key_type(leaf)) {
  408. if (error) {
  409. *error = error_buf;
  410. snprintf(error_buf, sizeof(error_buf),
  411. "Local certificate has key type "
  412. "incompatible with cipher suite %s's "
  413. "signature algorithm", suite->name);
  414. }
  415. return false;
  416. }
  417. /*
  418. * On the server we know what elliptic curve we'll be using as soon
  419. * as we've processed the ClientHello so for EC-based key exchange
  420. * methods require that a curve has been selected.
  421. */
  422. if (suite->key_xchg->need_ecc && tls->server &&
  423. !tls->negotiated_curve) {
  424. if (error) {
  425. *error = error_buf;
  426. snprintf(error_buf, sizeof(error_buf),
  427. "No common supported elliptic curves "
  428. "with the client, can't use %s",
  429. suite->name);
  430. }
  431. return false;
  432. }
  433. /* Similarly for FF DH groups */
  434. if (suite->key_xchg->need_ffdh && tls->server &&
  435. !tls->negotiated_ff_group) {
  436. if (error) {
  437. *error = error_buf;
  438. snprintf(error_buf, sizeof(error_buf),
  439. "No common supported finite-field "
  440. "groups with the client, can't use %s",
  441. suite->name);
  442. }
  443. return false;
  444. }
  445. return true;
  446. }
  447. static struct tls_cipher_suite *tls_find_cipher_suite(const uint8_t *id)
  448. {
  449. struct tls_cipher_suite **suite;
  450. for (suite = tls_cipher_suite_pref; *suite; suite++)
  451. if ((*suite)->id[0] == id[0] && (*suite)->id[1] == id[1])
  452. return *suite;
  453. return NULL;
  454. }
  455. static struct tls_compression_method tls_compression_pref[] = {
  456. {
  457. 0,
  458. "CompressionMethod.null",
  459. },
  460. };
  461. static struct tls_compression_method *tls_find_compression_method(
  462. const uint8_t id)
  463. {
  464. int i;
  465. for (i = 0; i < (int) L_ARRAY_SIZE(tls_compression_pref); i++)
  466. if (tls_compression_pref[i].id == id)
  467. return &tls_compression_pref[i];
  468. return NULL;
  469. }
  470. const struct tls_hash_algorithm tls_handshake_hash_data[] = {
  471. [HANDSHAKE_HASH_SHA384] = { 5, HANDSHAKE_HASH_SHA384,
  472. L_CHECKSUM_SHA384, "SHA384" },
  473. [HANDSHAKE_HASH_SHA256] = { 4, HANDSHAKE_HASH_SHA256,
  474. L_CHECKSUM_SHA256, "SHA256" },
  475. [HANDSHAKE_HASH_MD5] = { 1, HANDSHAKE_HASH_MD5,
  476. L_CHECKSUM_MD5, "MD5" },
  477. [HANDSHAKE_HASH_SHA1] = { 2, HANDSHAKE_HASH_SHA1,
  478. L_CHECKSUM_SHA1, "SHA1" },
  479. };
  480. static bool tls_init_handshake_hash(struct l_tls *tls)
  481. {
  482. enum handshake_hash_type hash;
  483. bool tls10 = tls->max_version < L_TLS_V12;
  484. for (hash = 0; hash < __HANDSHAKE_HASH_COUNT; hash++) {
  485. /* Skip hash types we already know we won't need */
  486. if (tls10 && hash != HANDSHAKE_HASH_SHA1 &&
  487. hash != HANDSHAKE_HASH_MD5)
  488. continue;
  489. if (tls->handshake_hash[hash]) {
  490. TLS_DEBUG("Handshake hash %s already exists",
  491. tls_handshake_hash_data[hash].name);
  492. goto err;
  493. }
  494. tls->handshake_hash[hash] = l_checksum_new(
  495. tls_handshake_hash_data[hash].l_id);
  496. if (!tls->handshake_hash[hash]) {
  497. TLS_DEBUG("Can't create %s hash",
  498. tls_handshake_hash_data[hash].name);
  499. goto err;
  500. }
  501. }
  502. return true;
  503. err:
  504. for (hash = 0; hash < __HANDSHAKE_HASH_COUNT; hash++)
  505. tls_drop_handshake_hash(tls, hash);
  506. return false;
  507. }
  508. static const struct tls_hash_algorithm *tls_set_prf_hmac(struct l_tls *tls)
  509. {
  510. enum handshake_hash_type hash;
  511. if (tls->pending.cipher_suite->prf_hmac == L_CHECKSUM_NONE) {
  512. tls->prf_hmac = &tls_handshake_hash_data[HANDSHAKE_HASH_SHA256];
  513. return tls->prf_hmac;
  514. }
  515. for (hash = 0; hash < __HANDSHAKE_HASH_COUNT; hash++)
  516. if (tls_handshake_hash_data[hash].l_id ==
  517. tls->pending.cipher_suite->prf_hmac) {
  518. tls->prf_hmac = &tls_handshake_hash_data[hash];
  519. return tls->prf_hmac;
  520. }
  521. return NULL;
  522. }
  523. static bool tls_domain_match_mask(const char *name, size_t name_len,
  524. const char *mask, size_t mask_len)
  525. {
  526. bool at_start = true;
  527. while (1) {
  528. const char *name_seg_end = memchr(name, '.', name_len);
  529. const char *mask_seg_end = memchr(mask, '.', mask_len);
  530. size_t name_seg_len = name_seg_end ?
  531. (size_t) (name_seg_end - name) : name_len;
  532. size_t mask_seg_len = mask_seg_end ?
  533. (size_t) (mask_seg_end - mask) : mask_len;
  534. if (mask_seg_len == 1 && mask[0] == '*') {
  535. /*
  536. * A * at the beginning of the mask matches any
  537. * number of labels.
  538. */
  539. if (at_start && name_seg_end &&
  540. tls_domain_match_mask(name_seg_end + 1,
  541. name_len - name_seg_len - 1,
  542. mask, mask_len))
  543. return true;
  544. goto ok_next;
  545. }
  546. if (name_seg_len != mask_seg_len ||
  547. memcmp(name, mask, name_seg_len))
  548. return false;
  549. ok_next:
  550. /* If either string ends here both must end here */
  551. if (!name_seg_end || !mask_seg_end)
  552. return !name_seg_end && !mask_seg_end;
  553. at_start = false;
  554. name = name_seg_end + 1;
  555. name_len -= name_seg_len + 1;
  556. mask = mask_seg_end + 1;
  557. mask_len -= mask_seg_len + 1;
  558. }
  559. }
  560. static const struct asn1_oid subject_alt_name_oid =
  561. { 3, { 0x55, 0x1d, 0x11 } };
  562. static const struct asn1_oid dn_common_name_oid =
  563. { 3, { 0x55, 0x04, 0x03 } };
  564. #define SAN_DNS_NAME_ID ASN1_CONTEXT_IMPLICIT(2)
  565. static bool tls_cert_domains_match_mask(struct l_cert *cert, char **mask,
  566. char **error_msg)
  567. {
  568. const uint8_t *san, *dn, *end;
  569. size_t san_len, dn_len;
  570. uint8_t san_tag;
  571. const char *cn = NULL;
  572. size_t cn_len;
  573. char **i;
  574. struct l_string *dns_names = NULL;
  575. int dns_name_count = 0;
  576. /*
  577. * Locate SubjectAltName (RFC5280 Section 4.2.1.6) and descend into
  578. * the sole SEQUENCE element, check if any DNSName matches.
  579. */
  580. san = cert_get_extension(cert, &subject_alt_name_oid, NULL, &san_len);
  581. if (san) {
  582. san = asn1_der_find_elem(san, san_len, 0, &san_tag, &san_len);
  583. if (unlikely(!san || san_tag != ASN1_ID_SEQUENCE))
  584. goto parse_error;
  585. end = san + san_len;
  586. while (san < end) {
  587. const uint8_t *value;
  588. uint8_t tag;
  589. size_t len;
  590. value = asn1_der_find_elem(san, end - san,
  591. SAN_DNS_NAME_ID,
  592. &tag, &len);
  593. if (!value)
  594. break;
  595. /* Type is implicitly IA5STRING */
  596. for (i = mask; *i; i++)
  597. if (tls_domain_match_mask((const char *) value,
  598. len, *i, strlen(*i))) {
  599. l_string_free(dns_names);
  600. return true;
  601. }
  602. if (!dns_names) {
  603. dns_names = l_string_new(128);
  604. l_string_append(dns_names, "tried DNSName(s) ");
  605. l_string_append_fixed(dns_names,
  606. (char *) value, len);
  607. } else if (dns_name_count < 20) {
  608. l_string_append(dns_names, ", ");
  609. l_string_append_fixed(dns_names,
  610. (char *) value, len);
  611. }
  612. san = value + len;
  613. dns_name_count++;
  614. }
  615. }
  616. /*
  617. * Retrieve the Common Name from the Subject DN and check if it
  618. * matches.
  619. *
  620. * We look at the Common Name only if no DNSNames were present in
  621. * the certificate, following Wi-Fi Alliance's Hotspot 2.0
  622. * Specification v3.1 section 7.3.3.2 step 2:
  623. * "Verify in the AAA server certificate that the domain name from
  624. * the FQDN [...] is a suffix match of the domain name in at least
  625. * one of the DNSName SubjectAltName extensions. If a SubjectAltName
  626. * of type DNSName is not present, then the domain name from the
  627. * FQDN shall be a suffix match to the CommonName portion of the
  628. * SubjectName. If neither of these conditions holds, then
  629. * verification fails."
  630. */
  631. if (dns_name_count) {
  632. if (dns_name_count > 20)
  633. l_string_append_printf(dns_names, " and %i other",
  634. dns_name_count - 20);
  635. *error_msg = l_string_unwrap(dns_names);
  636. return false;
  637. }
  638. dn = l_cert_get_dn(cert, &dn_len);
  639. if (unlikely(!dn))
  640. goto parse_error;
  641. end = dn + dn_len;
  642. while (dn < end) {
  643. const uint8_t *set, *seq, *oid, *name;
  644. uint8_t tag;
  645. size_t len, oid_len, name_len;
  646. set = asn1_der_find_elem(dn, end - dn, 0, &tag, &len);
  647. if (unlikely(!set || tag != ASN1_ID_SET))
  648. goto parse_error;
  649. dn = set + len;
  650. seq = asn1_der_find_elem(set, len, 0, &tag, &len);
  651. if (unlikely(!seq || tag != ASN1_ID_SEQUENCE))
  652. goto parse_error;
  653. oid = asn1_der_find_elem(seq, len, 0, &tag, &oid_len);
  654. if (unlikely(!oid || tag != ASN1_ID_OID))
  655. goto parse_error;
  656. name = asn1_der_find_elem(seq, len, 1, &tag, &name_len);
  657. if (unlikely(!name || (tag != ASN1_ID_PRINTABLESTRING &&
  658. tag != ASN1_ID_UTF8STRING &&
  659. tag != ASN1_ID_IA5STRING)))
  660. continue;
  661. if (asn1_oid_eq(&dn_common_name_oid, oid_len, oid)) {
  662. cn = (const char *) name;
  663. cn_len = name_len;
  664. break;
  665. }
  666. }
  667. if (unlikely(!cn))
  668. goto parse_error;
  669. for (i = mask; *i; i++)
  670. if (tls_domain_match_mask(cn, cn_len, *i, strlen(*i)))
  671. return true;
  672. *error_msg = l_strdup_printf("tried CommonName %.*s", (int) cn_len, cn);
  673. return false;
  674. parse_error:
  675. *error_msg = l_strdup("couldn't locate DNSName or CommonName");
  676. return false;
  677. }
  678. #define SWITCH_ENUM_TO_STR(val) \
  679. case (val): \
  680. return L_STRINGIFY(val);
  681. static const char *tls_handshake_type_to_str(enum tls_handshake_type type)
  682. {
  683. static char buf[100];
  684. switch (type) {
  685. SWITCH_ENUM_TO_STR(TLS_HELLO_REQUEST)
  686. SWITCH_ENUM_TO_STR(TLS_CLIENT_HELLO)
  687. SWITCH_ENUM_TO_STR(TLS_SERVER_HELLO)
  688. SWITCH_ENUM_TO_STR(TLS_CERTIFICATE)
  689. SWITCH_ENUM_TO_STR(TLS_SERVER_KEY_EXCHANGE)
  690. SWITCH_ENUM_TO_STR(TLS_CERTIFICATE_REQUEST)
  691. SWITCH_ENUM_TO_STR(TLS_SERVER_HELLO_DONE)
  692. SWITCH_ENUM_TO_STR(TLS_CERTIFICATE_VERIFY)
  693. SWITCH_ENUM_TO_STR(TLS_CLIENT_KEY_EXCHANGE)
  694. SWITCH_ENUM_TO_STR(TLS_FINISHED)
  695. }
  696. snprintf(buf, sizeof(buf), "tls_handshake_type(%i)", type);
  697. return buf;
  698. }
  699. static void tls_send_alert(struct l_tls *tls, bool fatal,
  700. enum l_tls_alert_desc alert_desc)
  701. {
  702. uint8_t buf[2];
  703. TLS_DEBUG("Sending a %s Alert: %s", fatal ? "Fatal" : "Warning",
  704. l_tls_alert_to_str(alert_desc));
  705. buf[0] = fatal ? 2 : 1;
  706. buf[1] = alert_desc;
  707. tls_tx_record(tls, TLS_CT_ALERT, buf, 2);
  708. }
  709. /*
  710. * Callers make sure this is about the last function before returning
  711. * from the stack frames up to the exported library call so that the
  712. * user-supplied disconnected callback here is free to use l_tls_free
  713. * for example.
  714. */
  715. void tls_disconnect(struct l_tls *tls, enum l_tls_alert_desc desc,
  716. enum l_tls_alert_desc local_desc)
  717. {
  718. tls_send_alert(tls, true, desc);
  719. tls_reset_handshake(tls);
  720. tls_cleanup_handshake(tls);
  721. tls_reset_cipher_spec(tls, 0);
  722. tls_reset_cipher_spec(tls, 1);
  723. tls->negotiated_version = 0;
  724. tls->ready = false;
  725. tls->disconnected(local_desc ?: desc, local_desc && !desc,
  726. tls->user_data);
  727. }
  728. void tls_tx_handshake(struct l_tls *tls, int type, uint8_t *buf, size_t length)
  729. {
  730. int i;
  731. TLS_DEBUG("Sending a %s of %zi bytes",
  732. tls_handshake_type_to_str(type),
  733. length - TLS_HANDSHAKE_HEADER_SIZE);
  734. /* Fill in the handshake header */
  735. buf[0] = type;
  736. buf[1] = (length - TLS_HANDSHAKE_HEADER_SIZE) >> 16;
  737. buf[2] = (length - TLS_HANDSHAKE_HEADER_SIZE) >> 8;
  738. buf[3] = (length - TLS_HANDSHAKE_HEADER_SIZE) >> 0;
  739. for (i = 0; i < __HANDSHAKE_HASH_COUNT; i++)
  740. if (tls->handshake_hash[i])
  741. l_checksum_update(tls->handshake_hash[i], buf, length);
  742. tls_tx_record(tls, TLS_CT_HANDSHAKE, buf, length);
  743. }
  744. static ssize_t tls_append_hello_extensions(struct l_tls *tls,
  745. struct l_queue *extensions,
  746. uint8_t *buf, size_t len)
  747. {
  748. uint8_t *ptr = buf;
  749. uint8_t *extensions_len_ptr = ptr;
  750. bool client_hello = !tls->server;
  751. unsigned int i = 0;
  752. const struct l_queue_entry *entry = l_queue_get_entries(extensions);
  753. if (len < 2)
  754. return -ENOSPC;
  755. ptr += 2;
  756. len -= 2;
  757. while (1) {
  758. const struct tls_hello_extension *extension;
  759. ssize_t ext_len;
  760. ssize_t (*ext_write)(struct l_tls *tls,
  761. uint8_t *buf, size_t len);
  762. if (client_hello) {
  763. extension = &tls_extensions[i++];
  764. if (!extension->name)
  765. break;
  766. ext_write = extension->client_write;
  767. } else {
  768. uint16_t ext_id;
  769. if (!entry)
  770. break;
  771. ext_id = L_PTR_TO_UINT(entry->data);
  772. entry = entry->next;
  773. for (i = 0; tls_extensions[i].name; i++)
  774. if (tls_extensions[i].id == ext_id)
  775. break;
  776. extension = &tls_extensions[i];
  777. if (!extension->name)
  778. continue;
  779. ext_write = extension->server_write;
  780. }
  781. /*
  782. * Note: could handle NULL client_write with non-NULL
  783. * server_handle or server_handle_absent as "server-oriented"
  784. * extension (7.4.1.4) and write empty extension_data and
  785. * simliarly require empty extension_data in
  786. * tls_handle_client_hello if client_handle NULL.
  787. */
  788. if (!ext_write)
  789. continue;
  790. if (len < 4)
  791. return -ENOSPC;
  792. ext_len = ext_write(tls, ptr + 4, len - 4);
  793. if (ext_len == -ENOMSG)
  794. continue;
  795. if (ext_len < 0) {
  796. TLS_DEBUG("%s extension's %s_write: %s",
  797. extension->name,
  798. client_hello ? "client" : "server",
  799. strerror(-ext_len));
  800. return ext_len;
  801. }
  802. l_put_be16(extension->id, ptr + 0);
  803. l_put_be16(ext_len, ptr + 2);
  804. ptr += 4 + ext_len;
  805. len -= 4 + ext_len;
  806. }
  807. if (ptr > extensions_len_ptr + 2)
  808. l_put_be16(ptr - (extensions_len_ptr + 2), extensions_len_ptr);
  809. else /* Skip the length if no extensions */
  810. ptr = extensions_len_ptr;
  811. return ptr - buf;
  812. }
  813. static bool tls_send_client_hello(struct l_tls *tls)
  814. {
  815. uint8_t buf[1024 + L_ARRAY_SIZE(tls_compression_pref)];
  816. uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
  817. uint8_t *len_ptr;
  818. unsigned int i;
  819. ssize_t r;
  820. struct tls_cipher_suite **suite;
  821. /* Fill in the Client Hello body */
  822. *ptr++ = (uint8_t) (tls->max_version >> 8);
  823. *ptr++ = (uint8_t) (tls->max_version >> 0);
  824. tls_write_random(tls->pending.client_random);
  825. memcpy(ptr, tls->pending.client_random, 32);
  826. ptr += 32;
  827. *ptr++ = 0; /* No SessionID */
  828. len_ptr = ptr;
  829. ptr += 2;
  830. for (suite = tls->cipher_suite_pref_list; *suite; suite++) {
  831. const char *error;
  832. if (!tls_cipher_suite_is_compatible(tls, *suite, &error)) {
  833. TLS_DEBUG("non-fatal: %s", error);
  834. continue;
  835. }
  836. *ptr++ = (*suite)->id[0];
  837. *ptr++ = (*suite)->id[1];
  838. }
  839. if (ptr == len_ptr + 2) {
  840. TLS_DEBUG("No compatible cipher suites, check kernel config, "
  841. "certificate's key type and TLS version range");
  842. return false;
  843. }
  844. l_put_be16((ptr - len_ptr - 2), len_ptr);
  845. *ptr++ = L_ARRAY_SIZE(tls_compression_pref);
  846. for (i = 0; i < L_ARRAY_SIZE(tls_compression_pref); i++)
  847. *ptr++ = tls_compression_pref[i].id;
  848. r = tls_append_hello_extensions(tls, NULL,
  849. ptr, buf + sizeof(buf) - ptr);
  850. if (r < 0)
  851. return false;
  852. ptr += r;
  853. tls_tx_handshake(tls, TLS_CLIENT_HELLO, buf, ptr - buf);
  854. return true;
  855. }
  856. static bool tls_send_server_hello(struct l_tls *tls, struct l_queue *extensions)
  857. {
  858. uint8_t buf[1024];
  859. uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
  860. ssize_t r;
  861. /* Fill in the Server Hello body */
  862. *ptr++ = tls->negotiated_version >> 8;
  863. *ptr++ = tls->negotiated_version >> 0;
  864. tls_write_random(tls->pending.server_random);
  865. memcpy(ptr, tls->pending.server_random, 32);
  866. ptr += 32;
  867. *ptr++ = 0; /* Sessions are not cached */
  868. *ptr++ = tls->pending.cipher_suite->id[0];
  869. *ptr++ = tls->pending.cipher_suite->id[1];
  870. *ptr++ = tls->pending.compression_method->id;
  871. r = tls_append_hello_extensions(tls, extensions,
  872. ptr, buf + sizeof(buf) - ptr);
  873. if (r < 0) {
  874. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  875. "Error appending extensions: %s",
  876. strerror(-r));
  877. return false;
  878. }
  879. ptr += r;
  880. tls_tx_handshake(tls, TLS_SERVER_HELLO, buf, ptr - buf);
  881. return true;
  882. }
  883. static bool tls_cert_list_add_size(struct l_cert *cert, void *user_data)
  884. {
  885. size_t *total = user_data;
  886. size_t der_len;
  887. l_cert_get_der_data(cert, &der_len);
  888. *total += 3 + der_len;
  889. return false;
  890. }
  891. static bool tls_cert_list_append(struct l_cert *cert, void *user_data)
  892. {
  893. uint8_t **ptr = user_data;
  894. const uint8_t *der;
  895. size_t der_len;
  896. der = l_cert_get_der_data(cert, &der_len);
  897. *(*ptr)++ = der_len >> 16;
  898. *(*ptr)++ = der_len >> 8;
  899. *(*ptr)++ = der_len >> 0;
  900. memcpy(*ptr, der, der_len);
  901. *ptr += der_len;
  902. return false;
  903. }
  904. static bool tls_send_certificate(struct l_tls *tls)
  905. {
  906. uint8_t *buf, *ptr;
  907. size_t total;
  908. if (tls->server && !tls->cert) {
  909. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, TLS_ALERT_BAD_CERT,
  910. "Certificate needed in server mode");
  911. return false;
  912. }
  913. /*
  914. * TODO: check that the certificate is compatible with hash and
  915. * signature algorithms lists supplied to us in the Client Hello
  916. * extensions (if we're a 1.2+ server) or in the Certificate Request
  917. * (if we act as a 1.2+ client).
  918. *
  919. * - for the hash and signature_algorithms list, check all
  920. * certs in the cert chain.
  921. *
  922. * - also if !cipher_suite->key_xchg->key_exchange_msg, check that the
  923. * end entity certificate's key type matches and is usable with some
  924. * hash/signature pair.
  925. *
  926. * - on client check if any of the supplied DNs (if any) match
  927. * anything in our cert chain.
  928. */
  929. total = 0;
  930. l_certchain_walk_from_leaf(tls->cert, tls_cert_list_add_size, &total);
  931. buf = l_malloc(128 + total);
  932. ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
  933. /* Fill in the Certificate body */
  934. *ptr++ = total >> 16;
  935. *ptr++ = total >> 8;
  936. *ptr++ = total >> 0;
  937. l_certchain_walk_from_leaf(tls->cert, tls_cert_list_append, &ptr);
  938. tls_tx_handshake(tls, TLS_CERTIFICATE, buf, ptr - buf);
  939. l_free(buf);
  940. if (tls->cert)
  941. tls->cert_sent = true;
  942. return true;
  943. }
  944. /*
  945. * Note: ClientCertificateType.rsa_sign value coincides with the
  946. * SignatureAlgorithm.rsa value but other values in those enum are
  947. * different so we don't mix them, can't extract them from
  948. * tls->pending.cipher_suite->signature.
  949. */
  950. static uint8_t tls_cert_type_pref[] = {
  951. 1, /* RSA_sign */
  952. };
  953. static bool tls_send_certificate_request(struct l_tls *tls)
  954. {
  955. uint8_t *buf, *ptr, *dn_ptr;
  956. size_t len;
  957. const struct l_queue_entry *entry;
  958. unsigned int i;
  959. size_t dn_total = 0;
  960. for (entry = l_queue_get_entries(tls->ca_certs); entry;
  961. entry = entry->next) {
  962. struct l_cert *ca_cert = entry->data;
  963. size_t dn_size;
  964. if (l_cert_get_dn(ca_cert, &dn_size))
  965. dn_total += 10 + dn_size;
  966. }
  967. len = 256 + L_ARRAY_SIZE(tls_cert_type_pref) + dn_total;
  968. buf = l_malloc(len);
  969. ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
  970. /* Fill in the Certificate Request body */
  971. *ptr++ = L_ARRAY_SIZE(tls_cert_type_pref);
  972. for (i = 0; i < L_ARRAY_SIZE(tls_cert_type_pref); i++)
  973. *ptr++ = tls_cert_type_pref[i];
  974. if (tls->negotiated_version >= L_TLS_V12) {
  975. ssize_t ret = tls_write_signature_algorithms(tls, ptr,
  976. buf + len - ptr);
  977. if (ret < 0) {
  978. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  979. "tls_write_signature_algorithms: %s",
  980. strerror(-ret));
  981. l_free(buf);
  982. return false;
  983. }
  984. ptr += ret;
  985. }
  986. dn_ptr = ptr;
  987. ptr += 2; /* Leave space for the total DN size */
  988. for (entry = l_queue_get_entries(tls->ca_certs); entry;
  989. entry = entry->next) {
  990. struct l_cert *ca_cert = entry->data;
  991. size_t dn_size;
  992. const uint8_t *dn = l_cert_get_dn(ca_cert, &dn_size);
  993. uint8_t *cur_dn_ptr = ptr;
  994. if (!dn)
  995. continue;
  996. ptr += 2; /* Leave space for current DN size */
  997. *ptr++ = ASN1_ID_SEQUENCE; /* DER outer SEQUENCE tag */
  998. asn1_write_definite_length(&ptr, dn_size); /* length */
  999. memcpy(ptr, dn, dn_size); /* value */
  1000. ptr += dn_size;
  1001. l_put_be16(ptr - cur_dn_ptr - 2, cur_dn_ptr);
  1002. }
  1003. l_put_be16(ptr - dn_ptr - 2, dn_ptr); /* DistinguishedNames size */
  1004. tls_tx_handshake(tls, TLS_CERTIFICATE_REQUEST, buf, ptr - buf);
  1005. l_free(buf);
  1006. return true;
  1007. }
  1008. static void tls_send_server_hello_done(struct l_tls *tls)
  1009. {
  1010. uint8_t buf[32];
  1011. /* No body */
  1012. tls_tx_handshake(tls, TLS_SERVER_HELLO_DONE, buf,
  1013. TLS_HANDSHAKE_HEADER_SIZE);
  1014. }
  1015. void tls_generate_master_secret(struct l_tls *tls,
  1016. const uint8_t *pre_master_secret,
  1017. int pre_master_secret_len)
  1018. {
  1019. uint8_t seed[64];
  1020. int key_block_size;
  1021. memcpy(seed + 0, tls->pending.client_random, 32);
  1022. memcpy(seed + 32, tls->pending.server_random, 32);
  1023. tls_prf_get_bytes(tls, pre_master_secret, pre_master_secret_len,
  1024. "master secret", seed, 64,
  1025. tls->pending.master_secret, 48);
  1026. /* Directly generate the key block while we're at it */
  1027. key_block_size = 0;
  1028. if (tls->pending.cipher_suite->encryption)
  1029. key_block_size += 2 *
  1030. tls->pending.cipher_suite->encryption->key_length;
  1031. if (tls->pending.cipher_suite->mac)
  1032. key_block_size += 2 *
  1033. tls->pending.cipher_suite->mac->mac_length;
  1034. if (tls->pending.cipher_suite->encryption &&
  1035. tls->negotiated_version <= L_TLS_V10 &&
  1036. tls->pending.cipher_suite->encryption->cipher_type ==
  1037. TLS_CIPHER_BLOCK)
  1038. key_block_size += 2 *
  1039. tls->pending.cipher_suite->encryption->iv_length;
  1040. if (tls->pending.cipher_suite->encryption)
  1041. key_block_size += 2 * tls->pending.cipher_suite->encryption->
  1042. fixed_iv_length;
  1043. /* Reverse order from the master secret seed */
  1044. memcpy(seed + 0, tls->pending.server_random, 32);
  1045. memcpy(seed + 32, tls->pending.client_random, 32);
  1046. tls_prf_get_bytes(tls, tls->pending.master_secret, 48,
  1047. "key expansion", seed, 64,
  1048. tls->pending.key_block, key_block_size);
  1049. explicit_bzero(seed, 64);
  1050. }
  1051. static void tls_get_handshake_hash(struct l_tls *tls,
  1052. enum handshake_hash_type type,
  1053. uint8_t *out)
  1054. {
  1055. struct l_checksum *hash = l_checksum_clone(tls->handshake_hash[type]);
  1056. if (!hash)
  1057. return;
  1058. l_checksum_get_digest(hash, out, l_checksum_digest_length(
  1059. tls_handshake_hash_data[type].l_id));
  1060. l_checksum_free(hash);
  1061. }
  1062. static bool tls_get_handshake_hash_by_type(struct l_tls *tls,
  1063. enum handshake_hash_type type,
  1064. const uint8_t *data, size_t data_len,
  1065. uint8_t *out, size_t *out_len)
  1066. {
  1067. if (!tls->handshake_hash[type])
  1068. return false;
  1069. if (out_len)
  1070. *out_len = l_checksum_digest_length(
  1071. tls_handshake_hash_data[type].l_id);
  1072. tls_get_handshake_hash(tls, type, out);
  1073. return true;
  1074. }
  1075. static bool tls_send_certificate_verify(struct l_tls *tls)
  1076. {
  1077. uint8_t buf[2048];
  1078. int i;
  1079. ssize_t sign_len;
  1080. /* Fill in the Certificate Verify body */
  1081. sign_len = tls->pending.cipher_suite->signature->sign(tls,
  1082. buf + TLS_HANDSHAKE_HEADER_SIZE,
  1083. 2048 - TLS_HANDSHAKE_HEADER_SIZE,
  1084. tls_get_handshake_hash_by_type,
  1085. NULL, 0);
  1086. if (sign_len < 0)
  1087. return false;
  1088. /* Stop maintaining handshake message hashes other than the PRF hash */
  1089. if (tls->negotiated_version >= L_TLS_V12)
  1090. for (i = 0; i < __HANDSHAKE_HASH_COUNT; i++)
  1091. if (&tls_handshake_hash_data[i] != tls->prf_hmac)
  1092. tls_drop_handshake_hash(tls, i);
  1093. tls_tx_handshake(tls, TLS_CERTIFICATE_VERIFY, buf,
  1094. sign_len + TLS_HANDSHAKE_HEADER_SIZE);
  1095. return true;
  1096. }
  1097. static void tls_send_change_cipher_spec(struct l_tls *tls)
  1098. {
  1099. uint8_t buf = 1;
  1100. tls_tx_record(tls, TLS_CT_CHANGE_CIPHER_SPEC, &buf, 1);
  1101. }
  1102. static void tls_send_finished(struct l_tls *tls)
  1103. {
  1104. uint8_t buf[512];
  1105. uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
  1106. uint8_t seed[HANDSHAKE_HASH_MAX_SIZE * 2];
  1107. size_t seed_len;
  1108. if (tls->negotiated_version >= L_TLS_V12) {
  1109. /* Same hash type as that used for the PRF (usually SHA256) */
  1110. tls_get_handshake_hash(tls, tls->prf_hmac->type, seed);
  1111. seed_len = l_checksum_digest_length(tls->prf_hmac->l_id);
  1112. } else {
  1113. tls_get_handshake_hash(tls, HANDSHAKE_HASH_MD5, seed + 0);
  1114. tls_get_handshake_hash(tls, HANDSHAKE_HASH_SHA1, seed + 16);
  1115. seed_len = 36;
  1116. }
  1117. tls_prf_get_bytes(tls, tls->pending.master_secret, 48,
  1118. tls->server ? "server finished" :
  1119. "client finished",
  1120. seed, seed_len,
  1121. ptr, tls->cipher_suite[1]->verify_data_length);
  1122. ptr += tls->cipher_suite[1]->verify_data_length;
  1123. tls_tx_handshake(tls, TLS_FINISHED, buf, ptr - buf);
  1124. }
  1125. static bool tls_verify_finished(struct l_tls *tls, const uint8_t *received,
  1126. size_t len)
  1127. {
  1128. uint8_t expected[tls->cipher_suite[0]->verify_data_length];
  1129. uint8_t *seed;
  1130. size_t seed_len;
  1131. if (len != (size_t) tls->cipher_suite[0]->verify_data_length) {
  1132. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  1133. "TLS_FINISHED length not %i",
  1134. tls->cipher_suite[0]->verify_data_length);
  1135. return false;
  1136. }
  1137. if (tls->negotiated_version >= L_TLS_V12) {
  1138. enum handshake_hash_type hash = tls->prf_hmac->type;
  1139. seed = tls->prev_digest[hash];
  1140. seed_len = l_checksum_digest_length(tls->prf_hmac->l_id);
  1141. } else {
  1142. seed = alloca(36);
  1143. memcpy(seed + 0, tls->prev_digest[HANDSHAKE_HASH_MD5], 16);
  1144. memcpy(seed + 16, tls->prev_digest[HANDSHAKE_HASH_SHA1], 20);
  1145. seed_len = 36;
  1146. }
  1147. tls_prf_get_bytes(tls, tls->pending.master_secret, 48,
  1148. tls->server ? "client finished" :
  1149. "server finished",
  1150. seed, seed_len,
  1151. expected,
  1152. tls->cipher_suite[0]->verify_data_length);
  1153. if (memcmp(received, expected, len)) {
  1154. TLS_DISCONNECT(TLS_ALERT_DECRYPT_ERROR, 0,
  1155. "TLS_FINISHED contents don't match");
  1156. return false;
  1157. }
  1158. return true;
  1159. }
  1160. static bool tls_ptr_match(const void *a, const void *b)
  1161. {
  1162. return a == b;
  1163. }
  1164. static bool tls_handle_hello_extensions(struct l_tls *tls,
  1165. const uint8_t *buf, size_t len,
  1166. struct l_queue *seen)
  1167. {
  1168. unsigned int i;
  1169. const struct tls_hello_extension *extension;
  1170. bool client_hello = tls->server;
  1171. uint16_t extensions_size;
  1172. if (!len)
  1173. return true;
  1174. if (len < 2 || len > 2 + 65535)
  1175. goto decode_error;
  1176. extensions_size = l_get_be16(buf);
  1177. len -= 2;
  1178. buf += 2;
  1179. if (len != extensions_size)
  1180. goto decode_error;
  1181. while (len) {
  1182. uint16_t ext_id;
  1183. size_t ext_len;
  1184. bool (*handler)(struct l_tls *tls,
  1185. const uint8_t *buf, size_t len);
  1186. if (len < 4)
  1187. goto decode_error;
  1188. ext_id = l_get_be16(buf + 0);
  1189. ext_len = l_get_be16(buf + 2);
  1190. buf += 4;
  1191. len -= 4;
  1192. if (ext_len > len)
  1193. goto decode_error;
  1194. /*
  1195. * RFC 5246, Section 7.4.1.4: "There MUST NOT be more than
  1196. * one extension of the same type."
  1197. */
  1198. if (l_queue_find(seen, tls_ptr_match, L_UINT_TO_PTR(ext_id))) {
  1199. TLS_DEBUG("Duplicate extension %u", ext_id);
  1200. goto decode_error;
  1201. }
  1202. l_queue_push_tail(seen, L_UINT_TO_PTR(ext_id));
  1203. extension = NULL;
  1204. for (i = 0; tls_extensions[i].name; i++)
  1205. if (tls_extensions[i].id == ext_id) {
  1206. extension = &tls_extensions[i];
  1207. break;
  1208. }
  1209. if (!extension)
  1210. goto next;
  1211. handler = client_hello ?
  1212. extension->client_handle : extension->server_handle;
  1213. /*
  1214. * RFC 5246, Section 7.4.1.4: "If a client receives an
  1215. * extension type in ServerHello that it did not request in
  1216. * the associated ClientHello, it MUST abort the handshake
  1217. * with an unsupported_extension fatal alert."
  1218. * There are however servers that include an unsolicited
  1219. * Supported Point Format extension where the handshake
  1220. * still completes fine if the extension is ignored so we
  1221. * do this instead.
  1222. */
  1223. if (!client_hello && !handler) {
  1224. TLS_DEBUG("non-fatal: %s extension not expected in "
  1225. "a ServerHello", extension->name);
  1226. goto next;
  1227. }
  1228. if (!handler(tls, buf, ext_len)) {
  1229. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  1230. "Hello %s extension parse error",
  1231. extension->name);
  1232. return false;
  1233. }
  1234. next:
  1235. buf += ext_len;
  1236. len -= ext_len;
  1237. }
  1238. /*
  1239. * Trigger any actions needed when an extension is missing and its
  1240. * handler has not been called yet.
  1241. */
  1242. for (i = 0; tls_extensions[i].name; i++) {
  1243. bool (*handler)(struct l_tls *tls);
  1244. extension = &tls_extensions[i];
  1245. handler = client_hello ?
  1246. extension->client_handle_absent :
  1247. extension->server_handle_absent;
  1248. if (!handler)
  1249. continue;
  1250. if (l_queue_find(seen, tls_ptr_match,
  1251. L_UINT_TO_PTR(extension->id)))
  1252. continue;
  1253. if (!handler(tls)) {
  1254. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  1255. "Hello %s extension missing",
  1256. extension->name);
  1257. return false;
  1258. }
  1259. }
  1260. return true;
  1261. decode_error:
  1262. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  1263. "Hello extensions decode error");
  1264. return false;
  1265. }
  1266. static void tls_handle_client_hello(struct l_tls *tls,
  1267. const uint8_t *buf, size_t len)
  1268. {
  1269. uint16_t cipher_suites_size;
  1270. uint8_t session_id_size, compression_methods_size;
  1271. const uint8_t *cipher_suites;
  1272. const uint8_t *compression_methods;
  1273. int i;
  1274. struct l_queue *extensions_offered = NULL;
  1275. enum l_tls_alert_desc alert_desc = TLS_ALERT_HANDSHAKE_FAIL;
  1276. /* Do we have enough for ProtocolVersion + Random + SessionID size? */
  1277. if (len < 2 + 32 + 1)
  1278. goto decode_error;
  1279. memcpy(tls->pending.client_random, buf + 2, 32);
  1280. session_id_size = buf[34];
  1281. len -= 35;
  1282. /*
  1283. * Do we have enough to hold the actual session ID + 2 byte field for
  1284. * cipher_suite len + minimum of a single cipher suite identifier
  1285. */
  1286. if (len < (size_t) session_id_size + 4)
  1287. goto decode_error;
  1288. len -= session_id_size + 2;
  1289. cipher_suites_size = l_get_be16(buf + 35 + session_id_size);
  1290. cipher_suites = buf + 37 + session_id_size;
  1291. /*
  1292. * Check that size is not odd, more than 0 and we have enough
  1293. * data in the packet for cipher_suites_size + 2 bytes for
  1294. * compression_methods_size + a single compression method
  1295. */
  1296. if (len < (size_t) cipher_suites_size + 2 ||
  1297. (cipher_suites_size & 1) || cipher_suites_size == 0)
  1298. goto decode_error;
  1299. len -= cipher_suites_size + 1;
  1300. compression_methods_size = cipher_suites[cipher_suites_size];
  1301. compression_methods = cipher_suites + cipher_suites_size + 1;
  1302. if (len < (size_t) compression_methods_size ||
  1303. compression_methods_size == 0)
  1304. goto decode_error;
  1305. len -= compression_methods_size;
  1306. extensions_offered = l_queue_new();
  1307. if (!tls_handle_hello_extensions(tls, compression_methods +
  1308. compression_methods_size,
  1309. len, extensions_offered))
  1310. goto cleanup;
  1311. /*
  1312. * Note: if the client is supplying a SessionID we know it is false
  1313. * because our server implementation never generates any SessionIDs
  1314. * yet so either the client is attempting something strange or was
  1315. * trying to connect somewhere else. We might want to throw an error.
  1316. */
  1317. /* Save client_version for Premaster Secret verification */
  1318. tls->client_version = l_get_be16(buf);
  1319. if (tls->client_version < tls->min_version) {
  1320. TLS_DISCONNECT(TLS_ALERT_PROTOCOL_VERSION, 0,
  1321. "Client version too low: %02x",
  1322. tls->client_version);
  1323. goto cleanup;
  1324. }
  1325. tls->negotiated_version = tls->client_version > tls->max_version ?
  1326. tls->max_version : tls->client_version;
  1327. /* Stop maintaining handshake message hashes other than MD1 and SHA. */
  1328. if (tls->negotiated_version < L_TLS_V12)
  1329. for (i = 0; i < __HANDSHAKE_HASH_COUNT; i++)
  1330. if (i != HANDSHAKE_HASH_SHA1 && i != HANDSHAKE_HASH_MD5)
  1331. tls_drop_handshake_hash(tls, i);
  1332. TLS_DEBUG("Negotiated TLS " TLS_VER_FMT,
  1333. TLS_VER_ARGS(tls->negotiated_version));
  1334. if (!tls->cipher_suite_pref_list) {
  1335. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  1336. "No usable cipher suites");
  1337. goto cleanup;
  1338. }
  1339. /* Select a cipher suite according to client's preference list */
  1340. while (cipher_suites_size) {
  1341. struct tls_cipher_suite *suite =
  1342. tls_find_cipher_suite(cipher_suites);
  1343. struct tls_cipher_suite **iter;
  1344. const char *error;
  1345. for (iter = tls->cipher_suite_pref_list; *iter; iter++)
  1346. if (*iter == suite)
  1347. break;
  1348. if (!suite)
  1349. TLS_DEBUG("non-fatal: Cipher suite %04x unknown",
  1350. l_get_be16(cipher_suites));
  1351. else if (!tls_cipher_suite_is_compatible(tls, suite, &error))
  1352. TLS_DEBUG("non-fatal: %s", error);
  1353. else if (!*iter) {
  1354. /*
  1355. * We have at least one matching compatible suite but
  1356. * it is not allowed in this security profile. If the
  1357. * handshake ends up failing then we blame the security
  1358. * profile.
  1359. */
  1360. alert_desc = TLS_ALERT_INSUFFICIENT_SECURITY;
  1361. TLS_DEBUG("non-fatal: Cipher suite %s disallowed "
  1362. "by config", suite->name);
  1363. } else {
  1364. tls->pending.cipher_suite = suite;
  1365. break;
  1366. }
  1367. cipher_suites += 2;
  1368. cipher_suites_size -= 2;
  1369. }
  1370. if (!cipher_suites_size) {
  1371. TLS_DISCONNECT(alert_desc, 0,
  1372. "No common cipher suites matching negotiated "
  1373. "TLS version and our certificate's key type");
  1374. goto cleanup;
  1375. }
  1376. if (!tls_set_prf_hmac(tls)) {
  1377. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  1378. "Error selecting the PRF HMAC");
  1379. goto cleanup;
  1380. }
  1381. TLS_DEBUG("Negotiated %s", tls->pending.cipher_suite->name);
  1382. /* Select a compression method */
  1383. /* CompressionMethod.null must be present in the vector */
  1384. while (compression_methods_size) {
  1385. tls->pending.compression_method =
  1386. tls_find_compression_method(*compression_methods);
  1387. if (tls->pending.compression_method)
  1388. break;
  1389. compression_methods++;
  1390. compression_methods_size--;
  1391. }
  1392. if (!compression_methods_size) {
  1393. TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
  1394. "No common compression methods");
  1395. goto cleanup;
  1396. }
  1397. TLS_DEBUG("Negotiated %s", tls->pending.compression_method->name);
  1398. if (!tls_send_server_hello(tls, extensions_offered))
  1399. goto cleanup;
  1400. l_queue_destroy(extensions_offered, NULL);
  1401. if (tls->pending.cipher_suite->signature && tls->cert)
  1402. if (!tls_send_certificate(tls))
  1403. return;
  1404. if (tls->pending.cipher_suite->key_xchg->send_server_key_exchange)
  1405. if (!tls->pending.cipher_suite->key_xchg->
  1406. send_server_key_exchange(tls))
  1407. return;
  1408. /* TODO: don't bother if configured to not authenticate client */
  1409. if (tls->pending.cipher_suite->signature && tls->ca_certs)
  1410. if (!tls_send_certificate_request(tls))
  1411. return;
  1412. tls_send_server_hello_done(tls);
  1413. if (tls->pending.cipher_suite->signature && tls->ca_certs)
  1414. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_CERTIFICATE);
  1415. else
  1416. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_KEY_EXCHANGE);
  1417. return;
  1418. decode_error:
  1419. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  1420. "ClientHello decode error");
  1421. cleanup:
  1422. l_queue_destroy(extensions_offered, NULL);
  1423. }
  1424. static void tls_handle_server_hello(struct l_tls *tls,
  1425. const uint8_t *buf, size_t len)
  1426. {
  1427. uint8_t session_id_size, cipher_suite_id[2], compression_method_id;
  1428. const char *error;
  1429. struct tls_cipher_suite **iter;
  1430. int i;
  1431. struct l_queue *extensions_seen;
  1432. bool result;
  1433. /* Do we have enough for ProtocolVersion + Random + SessionID len ? */
  1434. if (len < 2 + 32 + 1)
  1435. goto decode_error;
  1436. memcpy(tls->pending.server_random, buf + 2, 32);
  1437. session_id_size = buf[34];
  1438. len -= 35;
  1439. /* Do we have enough for SessionID + CipherSuite ID + Compression ID */
  1440. if (len < (size_t) session_id_size + 2 + 1)
  1441. goto decode_error;
  1442. cipher_suite_id[0] = buf[35 + session_id_size + 0];
  1443. cipher_suite_id[1] = buf[35 + session_id_size + 1];
  1444. compression_method_id = buf[35 + session_id_size + 2];
  1445. len -= session_id_size + 2 + 1;
  1446. extensions_seen = l_queue_new();
  1447. result = tls_handle_hello_extensions(tls, buf + 38 + session_id_size,
  1448. len, extensions_seen);
  1449. l_queue_destroy(extensions_seen, NULL);
  1450. if (!result)
  1451. return;
  1452. tls->negotiated_version = l_get_be16(buf);
  1453. if (tls->negotiated_version < tls->min_version ||
  1454. tls->negotiated_version > tls->max_version) {
  1455. TLS_DISCONNECT(tls->negotiated_version < tls->min_version ?
  1456. TLS_ALERT_PROTOCOL_VERSION :
  1457. TLS_ALERT_ILLEGAL_PARAM, 0,
  1458. "Unsupported version %02x",
  1459. tls->negotiated_version);
  1460. return;
  1461. }
  1462. /* Stop maintaining handshake message hashes other than MD1 and SHA. */
  1463. if (tls->negotiated_version < L_TLS_V12)
  1464. for (i = 0; i < __HANDSHAKE_HASH_COUNT; i++)
  1465. if (i != HANDSHAKE_HASH_SHA1 && i != HANDSHAKE_HASH_MD5)
  1466. tls_drop_handshake_hash(tls, i);
  1467. TLS_DEBUG("Negotiated TLS " TLS_VER_FMT,
  1468. TLS_VER_ARGS(tls->negotiated_version));
  1469. /* Set the new cipher suite and compression method structs */
  1470. tls->pending.cipher_suite = tls_find_cipher_suite(cipher_suite_id);
  1471. if (!tls->pending.cipher_suite) {
  1472. TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
  1473. "Unknown cipher suite %04x",
  1474. l_get_be16(cipher_suite_id));
  1475. return;
  1476. }
  1477. for (iter = tls->cipher_suite_pref_list; *iter; iter++)
  1478. if (*iter == tls->pending.cipher_suite)
  1479. break;
  1480. if (!*iter) {
  1481. TLS_DISCONNECT(TLS_ALERT_INSUFFICIENT_SECURITY, 0,
  1482. "Selected cipher suite %s disallowed by config",
  1483. tls->pending.cipher_suite->name);
  1484. return;
  1485. }
  1486. if (!tls_cipher_suite_is_compatible(tls, tls->pending.cipher_suite,
  1487. &error)) {
  1488. TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
  1489. "Selected cipher suite not compatible: %s",
  1490. error);
  1491. return;
  1492. }
  1493. if (!tls_set_prf_hmac(tls)) {
  1494. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  1495. "Error selecting the PRF HMAC");
  1496. return;
  1497. }
  1498. TLS_DEBUG("Negotiated %s", tls->pending.cipher_suite->name);
  1499. tls->pending.compression_method =
  1500. tls_find_compression_method(compression_method_id);
  1501. if (!tls->pending.compression_method) {
  1502. TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
  1503. "Unknown compression method %i",
  1504. compression_method_id);
  1505. return;
  1506. }
  1507. TLS_DEBUG("Negotiated %s", tls->pending.compression_method->name);
  1508. if (tls->pending.cipher_suite->signature)
  1509. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_CERTIFICATE);
  1510. else
  1511. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_KEY_EXCHANGE);
  1512. return;
  1513. decode_error:
  1514. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  1515. "ServerHello decode error");
  1516. }
  1517. static void tls_handle_certificate(struct l_tls *tls,
  1518. const uint8_t *buf, size_t len)
  1519. {
  1520. size_t total;
  1521. _auto_(l_certchain_free) struct l_certchain *certchain = NULL;
  1522. struct l_cert *leaf;
  1523. size_t der_len;
  1524. const uint8_t *der;
  1525. bool dummy;
  1526. const char *error_str;
  1527. char *subject_str;
  1528. if (len < 3)
  1529. goto decode_error;
  1530. /* Length checks */
  1531. total = *buf++ << 16;
  1532. total |= *buf++ << 8;
  1533. total |= *buf++ << 0;
  1534. if (total + 3 != len)
  1535. goto decode_error;
  1536. if (tls_parse_certificate_list(buf, total, &certchain) < 0) {
  1537. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  1538. "Error decoding peer certificate chain");
  1539. return;
  1540. }
  1541. /*
  1542. * "Note that a client MAY send no certificates if it does not have any
  1543. * appropriate certificate to send in response to the server's
  1544. * authentication request." -- for now we unconditionally accept
  1545. * an empty certificate chain from the client. Later on we need to
  1546. * make this configurable, if we don't want to authenticate the
  1547. * client then also don't bother sending a Certificate Request.
  1548. */
  1549. if (!certchain) {
  1550. if (!tls->server) {
  1551. TLS_DISCONNECT(TLS_ALERT_HANDSHAKE_FAIL, 0,
  1552. "Server sent no certificate chain");
  1553. return;
  1554. }
  1555. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_KEY_EXCHANGE);
  1556. return;
  1557. }
  1558. if (tls->cert_dump_path) {
  1559. int r = pem_write_certificate_chain(certchain,
  1560. tls->cert_dump_path);
  1561. if (r < 0)
  1562. TLS_DEBUG("Error %i (%s) writing the peer certchain "
  1563. "to %s",
  1564. -r, strerror(-r), tls->cert_dump_path);
  1565. else
  1566. TLS_DEBUG("Peer certchain written to %s",
  1567. tls->cert_dump_path);
  1568. }
  1569. /*
  1570. * Validate the certificate chain's consistency and validate it
  1571. * against our CAs if we have any.
  1572. */
  1573. if (!l_certchain_verify(certchain, tls->ca_certs, &error_str)) {
  1574. if (tls->ca_certs) {
  1575. TLS_DISCONNECT(TLS_ALERT_BAD_CERT, 0,
  1576. "Peer certchain verification failed "
  1577. "consistency check%s: %s",
  1578. tls->ca_certs ?
  1579. " or against local CA certs" : "",
  1580. error_str);
  1581. return;
  1582. }
  1583. /*
  1584. * Until the mainstream kernel can handle the occasionally
  1585. * used certificates without the AKID extension (both root,
  1586. * which is legal, and non-root, which is iffy but still
  1587. * happens) don't fail on peer certificate chain verification
  1588. * failure when CA certificates were not provided. Knowing
  1589. * that the chain is self-consistent alone doesn't
  1590. * authenticate the peer in any way. Only warn when it looks
  1591. * like the chain is bad but parses and we can get the peer
  1592. * public key from it below.
  1593. */
  1594. TLS_DEBUG("Peer certchain verification failed (%s.) No local "
  1595. "CA certs provided so proceeding anyway. This "
  1596. "failure can signal a security issue or a "
  1597. "known kernel problem with some certificates.",
  1598. error_str);
  1599. }
  1600. /*
  1601. * RFC5246 7.4.2:
  1602. * "The end entity certificate's public key (and associated
  1603. * restrictions) MUST be compatible with the selected key exchange
  1604. * algorithm."
  1605. */
  1606. leaf = l_certchain_get_leaf(certchain);
  1607. if (!tls->pending.cipher_suite->signature->
  1608. validate_cert_key_type(leaf)) {
  1609. TLS_DISCONNECT(TLS_ALERT_UNSUPPORTED_CERT, 0,
  1610. "Peer certificate key type incompatible with "
  1611. "pending cipher suite %s",
  1612. tls->pending.cipher_suite->name);
  1613. return;
  1614. }
  1615. if (tls->subject_mask && !tls_cert_domains_match_mask(leaf,
  1616. tls->subject_mask,
  1617. &subject_str)) {
  1618. char *mask = l_strjoinv(tls->subject_mask, '|');
  1619. TLS_DISCONNECT(TLS_ALERT_BAD_CERT, 0,
  1620. "Peer certificate's subject domain "
  1621. "doesn't match mask %s: %s", mask, subject_str);
  1622. l_free(mask);
  1623. l_free(subject_str);
  1624. return;
  1625. }
  1626. /* Save the end-entity certificate and free the chain */
  1627. der = l_cert_get_der_data(leaf, &der_len);
  1628. tls->peer_cert = l_cert_new_from_der(der, der_len);
  1629. tls->peer_pubkey = l_cert_get_pubkey(tls->peer_cert);
  1630. if (!tls->peer_pubkey) {
  1631. TLS_DISCONNECT(TLS_ALERT_UNSUPPORTED_CERT, 0,
  1632. "Error loading peer public key to kernel");
  1633. return;
  1634. }
  1635. if (!l_key_get_info(tls->peer_pubkey, L_KEY_RSA_PKCS1_V1_5,
  1636. L_CHECKSUM_NONE, &tls->peer_pubkey_size,
  1637. &dummy)) {
  1638. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  1639. "Can't l_key_get_info for peer public key");
  1640. return;
  1641. }
  1642. tls->peer_pubkey_size /= 8;
  1643. if (tls->server || tls->pending.cipher_suite->key_xchg->
  1644. handle_server_key_exchange)
  1645. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_KEY_EXCHANGE);
  1646. else
  1647. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_HELLO_DONE);
  1648. return;
  1649. decode_error:
  1650. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  1651. "TLS_CERTIFICATE decode error");
  1652. }
  1653. static void tls_handle_certificate_request(struct l_tls *tls,
  1654. const uint8_t *buf, size_t len)
  1655. {
  1656. unsigned int cert_type_len, dn_len, i;
  1657. tls->cert_requested = 1;
  1658. cert_type_len = *buf++;
  1659. if (len < 1 + cert_type_len + 2)
  1660. goto decode_error;
  1661. for (i = 0; i < sizeof(tls_cert_type_pref); i++)
  1662. if (memchr(buf, tls_cert_type_pref[i], cert_type_len))
  1663. break;
  1664. if (i == sizeof(tls_cert_type_pref)) {
  1665. TLS_DISCONNECT(TLS_ALERT_UNSUPPORTED_CERT, 0,
  1666. "Requested certificate types not supported");
  1667. return;
  1668. }
  1669. buf += cert_type_len;
  1670. len -= 1 + cert_type_len;
  1671. /*
  1672. * TODO: parse and save certificate_types,
  1673. * supported_signature_algorithms and certificate_authorities
  1674. * lists for use in tls_send_certificate.
  1675. */
  1676. if (tls->negotiated_version >= L_TLS_V12) {
  1677. enum handshake_hash_type hash;
  1678. ssize_t ret = tls_parse_signature_algorithms(tls, buf, len);
  1679. if (ret == -ENOTSUP) {
  1680. TLS_DISCONNECT(TLS_ALERT_UNSUPPORTED_CERT, 0,
  1681. "No supported signature hash type");
  1682. return;
  1683. }
  1684. if (ret < 0)
  1685. goto decode_error;
  1686. len -= ret;
  1687. buf += ret;
  1688. /*
  1689. * We can now safely stop maintaining handshake message
  1690. * hashes other than the PRF hash and the one selected for
  1691. * signing.
  1692. */
  1693. for (hash = 0; hash < __HANDSHAKE_HASH_COUNT; hash++)
  1694. if (&tls_handshake_hash_data[hash] != tls->prf_hmac &&
  1695. hash != tls->signature_hash)
  1696. tls_drop_handshake_hash(tls, hash);
  1697. }
  1698. dn_len = l_get_be16(buf);
  1699. if (2 + dn_len != len)
  1700. goto decode_error;
  1701. return;
  1702. decode_error:
  1703. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  1704. "CertificateRequest decode error");
  1705. }
  1706. static void tls_handle_server_hello_done(struct l_tls *tls,
  1707. const uint8_t *buf, size_t len)
  1708. {
  1709. const char *error;
  1710. if (len) {
  1711. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  1712. "ServerHello not empty");
  1713. return;
  1714. }
  1715. if (tls->cert_requested)
  1716. if (!tls_send_certificate(tls))
  1717. return;
  1718. if (!tls->pending.cipher_suite->key_xchg->send_client_key_exchange(tls))
  1719. return;
  1720. if (tls->cert_sent)
  1721. if (!tls_send_certificate_verify(tls))
  1722. return;
  1723. tls_send_change_cipher_spec(tls);
  1724. if (!tls_change_cipher_spec(tls, 1, &error)) {
  1725. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  1726. "change_cipher_spec: %s", error);
  1727. return;
  1728. }
  1729. tls_send_finished(tls);
  1730. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_CHANGE_CIPHER_SPEC);
  1731. }
  1732. static bool tls_get_prev_digest_by_type(struct l_tls *tls,
  1733. enum handshake_hash_type type,
  1734. const uint8_t *data, size_t data_len,
  1735. uint8_t *out, size_t *out_len)
  1736. {
  1737. size_t len;
  1738. if (!tls->handshake_hash[type])
  1739. return false;
  1740. len = l_checksum_digest_length(tls_handshake_hash_data[type].l_id);
  1741. memcpy(out, tls->prev_digest[type], len);
  1742. if (out_len)
  1743. *out_len = len;
  1744. return 0;
  1745. }
  1746. static void tls_handle_certificate_verify(struct l_tls *tls,
  1747. const uint8_t *buf, size_t len)
  1748. {
  1749. int i;
  1750. if (!tls->pending.cipher_suite->signature->verify(tls, buf, len,
  1751. tls_get_prev_digest_by_type,
  1752. NULL, 0))
  1753. return;
  1754. /* Stop maintaining handshake message hashes other than the PRF hash */
  1755. if (tls->negotiated_version >= L_TLS_V12)
  1756. for (i = 0; i < __HANDSHAKE_HASH_COUNT; i++)
  1757. if (&tls_handshake_hash_data[i] != tls->prf_hmac)
  1758. tls_drop_handshake_hash(tls, i);
  1759. /*
  1760. * The client's certificate is now verified based on the following
  1761. * logic:
  1762. * - If we received an (expected) Certificate Verify, we must have
  1763. * sent a Certificate Request.
  1764. * - If we sent a Certificate Request that's because
  1765. * tls->ca_certs is non-NULL.
  1766. * - If tls->ca_certs is non-NULL then tls_handle_certificate
  1767. * will have checked the whole certificate chain to be valid and
  1768. * additionally trusted by our CAs if known.
  1769. * - Additionally cipher_suite->signature->verify has just confirmed
  1770. * that the peer owns the end-entity certificate because it was
  1771. * able to sign the contents of the handshake messages and that
  1772. * signature could be verified with the public key from that
  1773. * certificate.
  1774. */
  1775. tls->peer_authenticated = true;
  1776. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_CHANGE_CIPHER_SPEC);
  1777. }
  1778. struct dn_element_info {
  1779. const char *str;
  1780. const struct asn1_oid oid;
  1781. };
  1782. static const struct dn_element_info dn_elements[] = {
  1783. { "CN", { 3, { 0x55, 0x04, 0x03 } } },
  1784. { "SN", { 3, { 0x55, 0x04, 0x04 } } },
  1785. { "serialNumber", { 3, { 0x55, 0x04, 0x05 } } },
  1786. { "C", { 3, { 0x55, 0x04, 0x06 } } },
  1787. { "ST", { 3, { 0x55, 0x04, 0x07 } } },
  1788. { "L", { 3, { 0x55, 0x04, 0x08 } } },
  1789. { "street", { 3, { 0x55, 0x04, 0x09 } } },
  1790. { "O", { 3, { 0x55, 0x04, 0x0a } } },
  1791. { "OU", { 3, { 0x55, 0x04, 0x0b } } },
  1792. { "title", { 3, { 0x55, 0x04, 0x0c } } },
  1793. { "telephoneNumber", { 3, { 0x55, 0x04, 0x14 } } },
  1794. { "givenName", { 3, { 0x55, 0x04, 0x2a } } },
  1795. { "initials", { 3, { 0x55, 0x04, 0x2b } } },
  1796. { "emailAddress", {
  1797. 9,
  1798. { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01 }
  1799. } },
  1800. { "domainComponent", {
  1801. 10,
  1802. { 0x09, 0x92, 0x26, 0x89, 0x93, 0xf2, 0x2c, 0x64, 0x01, 0x19 }
  1803. } },
  1804. {}
  1805. };
  1806. static void tls_str_escape_append(struct l_string *out, char *str, size_t len)
  1807. {
  1808. while (len--) {
  1809. switch (*str) {
  1810. case '\\':
  1811. case '/':
  1812. case '=':
  1813. l_string_append_c(out, '\\');
  1814. l_string_append_c(out, *str);
  1815. break;
  1816. default:
  1817. l_string_append_c(out, *str);
  1818. break;
  1819. }
  1820. str++;
  1821. }
  1822. }
  1823. static char *tls_get_peer_identity_str(struct l_cert *cert)
  1824. {
  1825. const uint8_t *dn, *end;
  1826. size_t dn_size;
  1827. struct l_string *id_str;
  1828. if (!cert)
  1829. return NULL;
  1830. dn = l_cert_get_dn(cert, &dn_size);
  1831. if (!dn)
  1832. return NULL;
  1833. id_str = l_string_new(200);
  1834. end = dn + dn_size;
  1835. while (dn < end) {
  1836. const uint8_t *set, *seq, *oid, *name;
  1837. uint8_t tag;
  1838. size_t len, oid_len, name_len;
  1839. const struct dn_element_info *info;
  1840. set = asn1_der_find_elem(dn, end - dn, 0, &tag, &len);
  1841. if (!set || tag != ASN1_ID_SET)
  1842. goto error;
  1843. dn = set + len;
  1844. seq = asn1_der_find_elem(set, len, 0, &tag, &len);
  1845. if (!seq || tag != ASN1_ID_SEQUENCE)
  1846. goto error;
  1847. oid = asn1_der_find_elem(seq, len, 0, &tag, &oid_len);
  1848. if (!oid || tag != ASN1_ID_OID)
  1849. goto error;
  1850. name = asn1_der_find_elem(seq, len, 1, &tag, &name_len);
  1851. if (!name || (tag != ASN1_ID_PRINTABLESTRING &&
  1852. tag != ASN1_ID_UTF8STRING &&
  1853. tag != ASN1_ID_IA5STRING))
  1854. continue;
  1855. for (info = dn_elements; info->str; info++)
  1856. if (asn1_oid_eq(&info->oid, oid_len, oid))
  1857. break;
  1858. if (!info->str)
  1859. continue;
  1860. l_string_append_c(id_str, '/');
  1861. l_string_append(id_str, info->str);
  1862. l_string_append_c(id_str, '=');
  1863. tls_str_escape_append(id_str, (char *) name, name_len);
  1864. }
  1865. return l_string_unwrap(id_str);
  1866. error:
  1867. l_string_free(id_str);
  1868. return NULL;
  1869. }
  1870. static void tls_finished(struct l_tls *tls)
  1871. {
  1872. char *peer_identity = NULL;
  1873. if (tls->peer_authenticated) {
  1874. peer_identity = tls_get_peer_identity_str(tls->peer_cert);
  1875. if (!peer_identity) {
  1876. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  1877. "tls_get_peer_identity_str failed");
  1878. return;
  1879. }
  1880. }
  1881. /* Free up the resources used in the handshake */
  1882. tls_reset_handshake(tls);
  1883. TLS_SET_STATE(TLS_HANDSHAKE_DONE);
  1884. tls->ready = true;
  1885. tls->in_callback = true;
  1886. tls->ready_handle(peer_identity, tls->user_data);
  1887. tls->in_callback = false;
  1888. l_free(peer_identity);
  1889. tls_cleanup_handshake(tls);
  1890. }
  1891. static void tls_handle_handshake(struct l_tls *tls, int type,
  1892. const uint8_t *buf, size_t len)
  1893. {
  1894. TLS_DEBUG("Handling a %s of %zi bytes",
  1895. tls_handshake_type_to_str(type), len);
  1896. switch (type) {
  1897. case TLS_HELLO_REQUEST:
  1898. if (tls->server) {
  1899. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  1900. "Message invalid in server mode");
  1901. break;
  1902. }
  1903. if (len != 0) {
  1904. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  1905. "HelloRequest not empty");
  1906. break;
  1907. }
  1908. /*
  1909. * May be sent by the server at any time but "SHOULD be ignored
  1910. * by the client if it arrives in the middle of a handshake"
  1911. * and "MAY be ignored by the client if it does not wish to
  1912. * renegotiate a session".
  1913. */
  1914. break;
  1915. case TLS_CLIENT_HELLO:
  1916. if (!tls->server) {
  1917. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  1918. "Message invalid in client mode");
  1919. break;
  1920. }
  1921. if (tls->state != TLS_HANDSHAKE_WAIT_HELLO &&
  1922. tls->state != TLS_HANDSHAKE_DONE) {
  1923. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  1924. "Message invalid in state %s",
  1925. tls_handshake_state_to_str(tls->state));
  1926. break;
  1927. }
  1928. tls_handle_client_hello(tls, buf, len);
  1929. break;
  1930. case TLS_SERVER_HELLO:
  1931. if (tls->server) {
  1932. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  1933. "Message invalid in server mode");
  1934. break;
  1935. }
  1936. if (tls->state != TLS_HANDSHAKE_WAIT_HELLO) {
  1937. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  1938. "Message invalid in state %s",
  1939. tls_handshake_state_to_str(tls->state));
  1940. break;
  1941. }
  1942. tls_handle_server_hello(tls, buf, len);
  1943. break;
  1944. case TLS_CERTIFICATE:
  1945. if (tls->state != TLS_HANDSHAKE_WAIT_CERTIFICATE) {
  1946. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  1947. "Message invalid in state %s",
  1948. tls_handshake_state_to_str(tls->state));
  1949. break;
  1950. }
  1951. tls_handle_certificate(tls, buf, len);
  1952. break;
  1953. case TLS_SERVER_KEY_EXCHANGE:
  1954. if (tls->server) {
  1955. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  1956. "Message invalid in server mode");
  1957. break;
  1958. }
  1959. if (tls->state != TLS_HANDSHAKE_WAIT_KEY_EXCHANGE) {
  1960. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  1961. "Message invalid in state %s",
  1962. tls_handshake_state_to_str(tls->state));
  1963. break;
  1964. }
  1965. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_HELLO_DONE);
  1966. tls->pending.cipher_suite->key_xchg->handle_server_key_exchange(
  1967. tls, buf, len);
  1968. break;
  1969. case TLS_CERTIFICATE_REQUEST:
  1970. if (tls->server) {
  1971. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  1972. "Message invalid in server mode");
  1973. break;
  1974. }
  1975. /*
  1976. * Server sends this optionally so in the WAIT_HELLO_DONE
  1977. * state we accept either this or a Server Hello Done (below).
  1978. */
  1979. if (tls->state != TLS_HANDSHAKE_WAIT_HELLO_DONE ||
  1980. tls->cert_requested ||
  1981. !tls->pending.cipher_suite->signature) {
  1982. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  1983. "Message invalid in current state "
  1984. "or certificate check not supported "
  1985. "in pending cipher suite");
  1986. break;
  1987. }
  1988. tls_handle_certificate_request(tls, buf, len);
  1989. break;
  1990. case TLS_SERVER_HELLO_DONE:
  1991. if (tls->state != TLS_HANDSHAKE_WAIT_HELLO_DONE) {
  1992. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  1993. "Message invalid in state %s",
  1994. tls_handshake_state_to_str(tls->state));
  1995. break;
  1996. }
  1997. tls_handle_server_hello_done(tls, buf, len);
  1998. break;
  1999. case TLS_CERTIFICATE_VERIFY:
  2000. if (tls->state != TLS_HANDSHAKE_WAIT_CERTIFICATE_VERIFY) {
  2001. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  2002. "Message invalid in state %s",
  2003. tls_handshake_state_to_str(tls->state));
  2004. break;
  2005. }
  2006. tls_handle_certificate_verify(tls, buf, len);
  2007. break;
  2008. case TLS_CLIENT_KEY_EXCHANGE:
  2009. if (!tls->server) {
  2010. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  2011. "Message invalid in client mode");
  2012. break;
  2013. }
  2014. if (tls->state != TLS_HANDSHAKE_WAIT_KEY_EXCHANGE) {
  2015. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  2016. "Message invalid in state %s",
  2017. tls_handshake_state_to_str(tls->state));
  2018. break;
  2019. }
  2020. /*
  2021. * If we accepted a client Certificate message with a
  2022. * certificate that has signing capability (TODO: check
  2023. * usage bitmask), Certificate Verify is received next. It
  2024. * sounds as if this is mandatory for the client although
  2025. * this isn't 100% clear.
  2026. */
  2027. if (tls->peer_pubkey)
  2028. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_CERTIFICATE_VERIFY);
  2029. else
  2030. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_CHANGE_CIPHER_SPEC);
  2031. tls->pending.cipher_suite->key_xchg->handle_client_key_exchange(
  2032. tls, buf, len);
  2033. break;
  2034. case TLS_FINISHED:
  2035. if (tls->state != TLS_HANDSHAKE_WAIT_FINISHED) {
  2036. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  2037. "Message invalid in state %s",
  2038. tls_handshake_state_to_str(tls->state));
  2039. break;
  2040. }
  2041. if (!tls_verify_finished(tls, buf, len))
  2042. break;
  2043. if (tls->server) {
  2044. const char *error;
  2045. tls_send_change_cipher_spec(tls);
  2046. if (!tls_change_cipher_spec(tls, 1, &error)) {
  2047. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  2048. "change_cipher_spec: %s",
  2049. error);
  2050. break;
  2051. }
  2052. tls_send_finished(tls);
  2053. }
  2054. /*
  2055. * On the client, the server's certificate is now verified
  2056. * regardless of the key exchange method, based on the
  2057. * following logic:
  2058. *
  2059. * - tls->ca_certs is non-NULL so tls_handle_certificate
  2060. * (always called on the client) must have veritifed the
  2061. * server's certificate chain to be valid and additionally
  2062. * trusted by our CA.
  2063. *
  2064. * - the peer owns the end-entity certificate because:
  2065. * either:
  2066. *
  2067. * * (RSA key exchange algorithm case) the correct
  2068. * receival of this Finished message confirms the
  2069. * posession of the master secret, it is verified by
  2070. * both the successful decryption and the MAC of this
  2071. * message (either should be enough) because we entered
  2072. * the TLS_HANDSHAKE_WAIT_FINISHED state only after
  2073. * encryption and MAC were enabled in ChangeCipherSpec.
  2074. * To obtain the master secret the server must have been
  2075. * able to decrypt the pre_master_secret which we had
  2076. * encrypted with the public key from that certificate.
  2077. *
  2078. * * (ECDHE and DHE key exchange algorithms) server was
  2079. * able to sign the client random together with the
  2080. * ServerKeyExchange parameters using its certified key
  2081. * pair.
  2082. */
  2083. if (!tls->server && tls->cipher_suite[0]->signature &&
  2084. tls->ca_certs)
  2085. tls->peer_authenticated = true;
  2086. tls_finished(tls);
  2087. break;
  2088. default:
  2089. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  2090. "Invalid message");
  2091. }
  2092. }
  2093. LIB_EXPORT struct l_tls *l_tls_new(bool server,
  2094. l_tls_write_cb_t app_data_handler,
  2095. l_tls_write_cb_t tx_handler,
  2096. l_tls_ready_cb_t ready_handler,
  2097. l_tls_disconnect_cb_t disconnect_handler,
  2098. void *user_data)
  2099. {
  2100. struct l_tls *tls;
  2101. if (!l_key_is_supported(L_KEY_FEATURE_CRYPTO))
  2102. return NULL;
  2103. tls = l_new(struct l_tls, 1);
  2104. tls->server = server;
  2105. tls->rx = app_data_handler;
  2106. tls->tx = tx_handler;
  2107. tls->ready_handle = ready_handler;
  2108. tls->disconnected = disconnect_handler;
  2109. tls->user_data = user_data;
  2110. tls->cipher_suite_pref_list = tls_cipher_suite_pref;
  2111. tls->min_version = TLS_MIN_VERSION;
  2112. tls->max_version = TLS_MAX_VERSION;
  2113. /* If we're the server wait for the Client Hello already */
  2114. if (tls->server)
  2115. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_HELLO);
  2116. else
  2117. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_START);
  2118. return tls;
  2119. }
  2120. LIB_EXPORT void l_tls_free(struct l_tls *tls)
  2121. {
  2122. enum handshake_hash_type hash;
  2123. if (unlikely(!tls))
  2124. return;
  2125. if (tls->in_callback) {
  2126. tls->pending_destroy = true;
  2127. return;
  2128. }
  2129. l_tls_set_cacert(tls, NULL);
  2130. l_tls_set_auth_data(tls, NULL, NULL);
  2131. l_tls_set_domain_mask(tls, NULL);
  2132. l_tls_set_cert_dump_path(tls, NULL);
  2133. tls_reset_handshake(tls);
  2134. tls_cleanup_handshake(tls);
  2135. tls_reset_cipher_spec(tls, 0);
  2136. tls_reset_cipher_spec(tls, 1);
  2137. if (tls->record_buf)
  2138. l_free(tls->record_buf);
  2139. if (tls->message_buf)
  2140. l_free(tls->message_buf);
  2141. for (hash = 0; hash < __HANDSHAKE_HASH_COUNT; hash++)
  2142. tls_drop_handshake_hash(tls, hash);
  2143. if (tls->debug_destroy)
  2144. tls->debug_destroy(tls->debug_data);
  2145. if (tls->cipher_suite_pref_list != tls_cipher_suite_pref)
  2146. l_free(tls->cipher_suite_pref_list);
  2147. l_free(tls);
  2148. }
  2149. LIB_EXPORT void l_tls_write(struct l_tls *tls, const uint8_t *data, size_t len)
  2150. {
  2151. if (unlikely(!tls->ready)) {
  2152. return;
  2153. }
  2154. tls_tx_record(tls, TLS_CT_APPLICATION_DATA, data, len);
  2155. }
  2156. bool tls_handle_message(struct l_tls *tls, const uint8_t *message,
  2157. int len, enum tls_content_type type, uint16_t version)
  2158. {
  2159. enum handshake_hash_type hash;
  2160. const char *error;
  2161. switch (type) {
  2162. case TLS_CT_CHANGE_CIPHER_SPEC:
  2163. if (len != 1 || message[0] != 0x01) {
  2164. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  2165. "ChangeCipherSpec msg decode error");
  2166. return false;
  2167. }
  2168. if (tls->state != TLS_HANDSHAKE_WAIT_CHANGE_CIPHER_SPEC) {
  2169. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  2170. "ChangeCipherSpec invalid in state %s",
  2171. tls_handshake_state_to_str(tls->state));
  2172. return false;
  2173. }
  2174. if (!tls_change_cipher_spec(tls, 0, &error)) {
  2175. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  2176. "change_cipher_spec: %s", error);
  2177. return false;
  2178. }
  2179. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_FINISHED);
  2180. return true;
  2181. case TLS_CT_ALERT:
  2182. /* Verify AlertLevel */
  2183. if (message[0] != 0x01 && message[0] != 0x02) {
  2184. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  2185. "Received bad AlertLevel %i",
  2186. message[0]);
  2187. return false;
  2188. }
  2189. /*
  2190. * On a fatal alert we are obligated to respond with a
  2191. * fatal alert and disconnect but also not complain if
  2192. * the connection has been torn down by the peer before
  2193. * we were able to send our alert. However on a non-fatal
  2194. * alert (warning) we're also allowed to panic and send
  2195. * a fatal alert, then disconnect, so we do that
  2196. * regardless of the alert level.
  2197. */
  2198. TLS_DISCONNECT(TLS_ALERT_CLOSE_NOTIFY, message[1],
  2199. "Peer sent a %s Alert: %s",
  2200. message[0] == 0x02 ? "Fatal" : "Warning",
  2201. l_tls_alert_to_str(message[1]));
  2202. return false;
  2203. case TLS_CT_HANDSHAKE:
  2204. /* Start hashing the handshake contents on first message */
  2205. if (tls->server && message[0] == TLS_CLIENT_HELLO &&
  2206. (tls->state == TLS_HANDSHAKE_WAIT_HELLO ||
  2207. tls->state != TLS_HANDSHAKE_DONE))
  2208. if (!tls_init_handshake_hash(tls))
  2209. return false;
  2210. /*
  2211. * Corner case: When handling a Certificate Verify or a
  2212. * Finished message we need access to the messages hash from
  2213. * before this message was transmitted on the Tx side so we
  2214. * can verify it matches the hash the sender included in the
  2215. * message. We save it here for that purpose. Everywhere
  2216. * else we need to update the hash before handling the new
  2217. * message because 1. we may need the new hash to build our
  2218. * own Certificate Verify or Finished messages, and 2. we
  2219. * update the message hash with newly transmitted messages
  2220. * inside tls_tx_handshake which may be called as part of
  2221. * handling incoming message, and if we didn't call
  2222. * l_checksum_update before, the calls would end up being
  2223. * out of order.
  2224. */
  2225. if (message[0] == TLS_CERTIFICATE_VERIFY ||
  2226. message[0] == TLS_FINISHED)
  2227. for (hash = 0; hash < __HANDSHAKE_HASH_COUNT; hash++) {
  2228. if (!tls->handshake_hash[hash])
  2229. continue;
  2230. tls_get_handshake_hash(tls, hash,
  2231. tls->prev_digest[hash]);
  2232. }
  2233. /*
  2234. * RFC 5246, Section 7.4.1.1:
  2235. * This message MUST NOT be included in the message hashes
  2236. * that are maintained throughout the handshake and used in
  2237. * the Finished messages and the certificate verify message.
  2238. */
  2239. if (message[0] != TLS_HELLO_REQUEST)
  2240. for (hash = 0; hash < __HANDSHAKE_HASH_COUNT; hash++) {
  2241. if (!tls->handshake_hash[hash])
  2242. continue;
  2243. l_checksum_update(tls->handshake_hash[hash],
  2244. message, len);
  2245. }
  2246. tls_handle_handshake(tls, message[0],
  2247. message + TLS_HANDSHAKE_HEADER_SIZE,
  2248. len - TLS_HANDSHAKE_HEADER_SIZE);
  2249. if (tls->pending_destroy) {
  2250. l_tls_free(tls);
  2251. return false;
  2252. }
  2253. return true;
  2254. case TLS_CT_APPLICATION_DATA:
  2255. if (!tls->ready) {
  2256. TLS_DISCONNECT(TLS_ALERT_UNEXPECTED_MESSAGE, 0,
  2257. "Application data message before "
  2258. "handshake finished");
  2259. return false;
  2260. }
  2261. if (!len)
  2262. return true;
  2263. tls->in_callback = true;
  2264. tls->rx(message, len, tls->user_data);
  2265. tls->in_callback = false;
  2266. if (tls->pending_destroy) {
  2267. l_tls_free(tls);
  2268. return false;
  2269. }
  2270. return true;
  2271. }
  2272. return false;
  2273. }
  2274. LIB_EXPORT bool l_tls_start(struct l_tls *tls)
  2275. {
  2276. if (tls->max_version < tls->min_version)
  2277. return false;
  2278. if (!tls->cipher_suite_pref_list)
  2279. return false;
  2280. /* This is a nop in server mode */
  2281. if (tls->server)
  2282. return true;
  2283. if (tls->state != TLS_HANDSHAKE_WAIT_START) {
  2284. TLS_DEBUG("Call invalid in state %s",
  2285. tls_handshake_state_to_str(tls->state));
  2286. return false;
  2287. }
  2288. if (!tls_init_handshake_hash(tls))
  2289. return false;
  2290. if (!tls_send_client_hello(tls))
  2291. return false;
  2292. TLS_SET_STATE(TLS_HANDSHAKE_WAIT_HELLO);
  2293. return true;
  2294. }
  2295. LIB_EXPORT void l_tls_close(struct l_tls *tls)
  2296. {
  2297. TLS_DISCONNECT(TLS_ALERT_CLOSE_NOTIFY, 0, "Closing session");
  2298. }
  2299. LIB_EXPORT bool l_tls_set_cacert(struct l_tls *tls, struct l_queue *ca_certs)
  2300. {
  2301. if (tls->ca_certs) {
  2302. l_queue_destroy(tls->ca_certs,
  2303. (l_queue_destroy_func_t) l_cert_free);
  2304. tls->ca_certs = NULL;
  2305. }
  2306. if (ca_certs) {
  2307. if (!l_key_is_supported(L_KEY_FEATURE_RESTRICT)) {
  2308. TLS_DEBUG("keyctl restrict support missing, "
  2309. "check kernel configuration");
  2310. return false;
  2311. }
  2312. tls->ca_certs = ca_certs;
  2313. }
  2314. return true;
  2315. }
  2316. LIB_EXPORT bool l_tls_set_auth_data(struct l_tls *tls,
  2317. struct l_certchain *certchain,
  2318. struct l_key *priv_key)
  2319. {
  2320. if (tls->cert) {
  2321. l_certchain_free(tls->cert);
  2322. tls->cert = NULL;
  2323. }
  2324. if (tls->priv_key) {
  2325. l_key_free(tls->priv_key);
  2326. tls->priv_key = NULL;
  2327. tls->priv_key_size = 0;
  2328. }
  2329. if (certchain)
  2330. tls->cert = certchain;
  2331. if (priv_key) {
  2332. bool is_public = true;
  2333. tls->priv_key = priv_key;
  2334. if (!l_key_get_info(tls->priv_key, L_KEY_RSA_PKCS1_V1_5,
  2335. L_CHECKSUM_NONE, &tls->priv_key_size,
  2336. &is_public) || is_public) {
  2337. TLS_DEBUG("Not a private key or l_key_get_info failed");
  2338. tls->cert = NULL;
  2339. tls->priv_key = NULL;
  2340. tls->priv_key_size = 0;
  2341. return false;
  2342. }
  2343. tls->priv_key_size /= 8;
  2344. }
  2345. return true;
  2346. }
  2347. bool tls_set_cipher_suites(struct l_tls *tls, const char **suite_list)
  2348. {
  2349. struct tls_cipher_suite **suite;
  2350. if (tls->cipher_suite_pref_list != tls_cipher_suite_pref)
  2351. l_free(tls->cipher_suite_pref_list);
  2352. if (!suite_list) {
  2353. /* Use our default cipher suite preference list */
  2354. tls->cipher_suite_pref_list = tls_cipher_suite_pref;
  2355. return true;
  2356. }
  2357. tls->cipher_suite_pref_list = l_new(struct tls_cipher_suite *,
  2358. l_strv_length((char **) suite_list) + 1);
  2359. suite = tls->cipher_suite_pref_list;
  2360. for (; *suite_list; suite_list++) {
  2361. unsigned int i;
  2362. for (i = 0; tls_cipher_suite_pref[i]; i++)
  2363. if (!strcmp(tls_cipher_suite_pref[i]->name,
  2364. *suite_list))
  2365. break;
  2366. if (tls_cipher_suite_pref[i])
  2367. *suite++ = tls_cipher_suite_pref[i];
  2368. else
  2369. TLS_DEBUG("Cipher suite %s is not supported",
  2370. *suite_list);
  2371. }
  2372. if (suite > tls->cipher_suite_pref_list)
  2373. return true;
  2374. TLS_DEBUG("None of the supplied suite names is supported");
  2375. l_free(suite);
  2376. tls->cipher_suite_pref_list = NULL;
  2377. return false;
  2378. }
  2379. LIB_EXPORT void l_tls_set_version_range(struct l_tls *tls,
  2380. enum l_tls_version min_version,
  2381. enum l_tls_version max_version)
  2382. {
  2383. tls->min_version =
  2384. (min_version && min_version > TLS_MIN_VERSION) ?
  2385. min_version : TLS_MIN_VERSION;
  2386. tls->max_version =
  2387. (max_version && max_version < TLS_MAX_VERSION) ?
  2388. max_version : TLS_MAX_VERSION;
  2389. }
  2390. /**
  2391. * l_tls_set_domain_mask:
  2392. * @tls: TLS object being configured
  2393. * @mask: NULL-terminated array of domain masks
  2394. *
  2395. * Sets a mask for domain names contained in the peer certificate
  2396. * (eg. the subject Common Name) to be matched against. If none of the
  2397. * domains match the any mask, authentication will fail. At least one
  2398. * domain has to match at least one mask from the list.
  2399. *
  2400. * The masks are each split into segments at the dot characters and each
  2401. * segment must match the corresponding label of the domain name --
  2402. * a domain name is a sequence of labels joined by dots. An asterisk
  2403. * segment in the mask matches any label. An asterisk segment at the
  2404. * beginning of the mask matches one or more consecutive labels from
  2405. * the beginning of the domain string.
  2406. */
  2407. LIB_EXPORT void l_tls_set_domain_mask(struct l_tls *tls, char **mask)
  2408. {
  2409. l_strv_free(tls->subject_mask);
  2410. tls->subject_mask = l_strv_copy(mask);
  2411. }
  2412. LIB_EXPORT const char *l_tls_alert_to_str(enum l_tls_alert_desc desc)
  2413. {
  2414. switch (desc) {
  2415. case TLS_ALERT_CLOSE_NOTIFY:
  2416. return "close_notify";
  2417. case TLS_ALERT_UNEXPECTED_MESSAGE:
  2418. return "unexpected_message";
  2419. case TLS_ALERT_BAD_RECORD_MAC:
  2420. return "bad_record_mac";
  2421. case TLS_ALERT_DECRYPT_FAIL_RESERVED:
  2422. return "decryption_failure_RESERVED";
  2423. case TLS_ALERT_RECORD_OVERFLOW:
  2424. return "record_overflow";
  2425. case TLS_ALERT_DECOMPRESS_FAIL:
  2426. return "decompression_failure";
  2427. case TLS_ALERT_HANDSHAKE_FAIL:
  2428. return "handshake_failure";
  2429. case TLS_ALERT_NO_CERT_RESERVED:
  2430. return "no_certificate_RESERVED";
  2431. case TLS_ALERT_BAD_CERT:
  2432. return "bad_certificate";
  2433. case TLS_ALERT_UNSUPPORTED_CERT:
  2434. return "unsupported_certificate";
  2435. case TLS_ALERT_CERT_REVOKED:
  2436. return "certificate_revoked";
  2437. case TLS_ALERT_CERT_EXPIRED:
  2438. return "certificate_expired";
  2439. case TLS_ALERT_CERT_UNKNOWN:
  2440. return "certificate_unknown";
  2441. case TLS_ALERT_ILLEGAL_PARAM:
  2442. return "illegal_parameter";
  2443. case TLS_ALERT_UNKNOWN_CA:
  2444. return "unknown_ca";
  2445. case TLS_ALERT_ACCESS_DENIED:
  2446. return "access_denied";
  2447. case TLS_ALERT_DECODE_ERROR:
  2448. return "decode_error";
  2449. case TLS_ALERT_DECRYPT_ERROR:
  2450. return "decrypt_error";
  2451. case TLS_ALERT_EXPORT_RES_RESERVED:
  2452. return "export_restriction_RESERVED";
  2453. case TLS_ALERT_PROTOCOL_VERSION:
  2454. return "protocol_version";
  2455. case TLS_ALERT_INSUFFICIENT_SECURITY:
  2456. return "insufficient_security";
  2457. case TLS_ALERT_INTERNAL_ERROR:
  2458. return "internal_error";
  2459. case TLS_ALERT_USER_CANCELED:
  2460. return "user_canceled";
  2461. case TLS_ALERT_NO_RENEGOTIATION:
  2462. return "no_renegotiation";
  2463. case TLS_ALERT_UNSUPPORTED_EXTENSION:
  2464. return "unsupported_extension";
  2465. }
  2466. return NULL;
  2467. }
  2468. const char *tls_handshake_state_to_str(enum tls_handshake_state state)
  2469. {
  2470. static char buf[100];
  2471. switch (state) {
  2472. SWITCH_ENUM_TO_STR(TLS_HANDSHAKE_WAIT_START)
  2473. SWITCH_ENUM_TO_STR(TLS_HANDSHAKE_WAIT_HELLO)
  2474. SWITCH_ENUM_TO_STR(TLS_HANDSHAKE_WAIT_CERTIFICATE)
  2475. SWITCH_ENUM_TO_STR(TLS_HANDSHAKE_WAIT_KEY_EXCHANGE)
  2476. SWITCH_ENUM_TO_STR(TLS_HANDSHAKE_WAIT_HELLO_DONE)
  2477. SWITCH_ENUM_TO_STR(TLS_HANDSHAKE_WAIT_CERTIFICATE_VERIFY)
  2478. SWITCH_ENUM_TO_STR(TLS_HANDSHAKE_WAIT_CHANGE_CIPHER_SPEC)
  2479. SWITCH_ENUM_TO_STR(TLS_HANDSHAKE_WAIT_FINISHED)
  2480. SWITCH_ENUM_TO_STR(TLS_HANDSHAKE_DONE)
  2481. }
  2482. snprintf(buf, sizeof(buf), "tls_handshake_state(%i)", state);
  2483. return buf;
  2484. }
  2485. int tls_parse_certificate_list(const void *data, size_t len,
  2486. struct l_certchain **out_certchain)
  2487. {
  2488. const uint8_t *buf = data;
  2489. struct l_certchain *chain = NULL;
  2490. while (len) {
  2491. struct l_cert *cert;
  2492. size_t cert_len;
  2493. if (len < 3)
  2494. goto decode_error;
  2495. cert_len = *buf++ << 16;
  2496. cert_len |= *buf++ << 8;
  2497. cert_len |= *buf++ << 0;
  2498. if (cert_len + 3 > len)
  2499. goto decode_error;
  2500. cert = l_cert_new_from_der(buf, cert_len);
  2501. if (!cert)
  2502. goto decode_error;
  2503. if (!chain) {
  2504. chain = certchain_new_from_leaf(cert);
  2505. if (!chain)
  2506. goto decode_error;
  2507. } else
  2508. certchain_link_issuer(chain, cert);
  2509. buf += cert_len;
  2510. len -= cert_len + 3;
  2511. }
  2512. if (out_certchain)
  2513. *out_certchain = chain;
  2514. else
  2515. l_certchain_free(chain);
  2516. return 0;
  2517. decode_error:
  2518. l_certchain_free(chain);
  2519. return -EBADMSG;
  2520. }
  2521. LIB_EXPORT bool l_tls_set_debug(struct l_tls *tls, l_tls_debug_cb_t function,
  2522. void *user_data, l_tls_destroy_cb_t destroy)
  2523. {
  2524. if (unlikely(!tls))
  2525. return false;
  2526. if (tls->debug_destroy)
  2527. tls->debug_destroy(tls->debug_data);
  2528. tls->debug_handler = function;
  2529. tls->debug_destroy = destroy;
  2530. tls->debug_data = user_data;
  2531. return true;
  2532. }
  2533. LIB_EXPORT bool l_tls_set_cert_dump_path(struct l_tls *tls, const char *path)
  2534. {
  2535. l_free(tls->cert_dump_path);
  2536. tls->cert_dump_path = path ? l_strdup(path) : NULL;
  2537. return true;
  2538. }