2
0

t1_lib.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <openssl/objects.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/hmac.h>
  14. #include <openssl/ocsp.h>
  15. #include <openssl/conf.h>
  16. #include <openssl/x509v3.h>
  17. #include <openssl/dh.h>
  18. #include <openssl/bn.h>
  19. #include "internal/nelem.h"
  20. #include "ssl_locl.h"
  21. #include <openssl/ct.h>
  22. SSL3_ENC_METHOD const TLSv1_enc_data = {
  23. tls1_enc,
  24. tls1_mac,
  25. tls1_setup_key_block,
  26. tls1_generate_master_secret,
  27. tls1_change_cipher_state,
  28. tls1_final_finish_mac,
  29. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  30. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  31. tls1_alert_code,
  32. tls1_export_keying_material,
  33. 0,
  34. ssl3_set_handshake_header,
  35. tls_close_construct_packet,
  36. ssl3_handshake_write
  37. };
  38. SSL3_ENC_METHOD const TLSv1_1_enc_data = {
  39. tls1_enc,
  40. tls1_mac,
  41. tls1_setup_key_block,
  42. tls1_generate_master_secret,
  43. tls1_change_cipher_state,
  44. tls1_final_finish_mac,
  45. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  46. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  47. tls1_alert_code,
  48. tls1_export_keying_material,
  49. SSL_ENC_FLAG_EXPLICIT_IV,
  50. ssl3_set_handshake_header,
  51. tls_close_construct_packet,
  52. ssl3_handshake_write
  53. };
  54. SSL3_ENC_METHOD const TLSv1_2_enc_data = {
  55. tls1_enc,
  56. tls1_mac,
  57. tls1_setup_key_block,
  58. tls1_generate_master_secret,
  59. tls1_change_cipher_state,
  60. tls1_final_finish_mac,
  61. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  62. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  63. tls1_alert_code,
  64. tls1_export_keying_material,
  65. SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
  66. | SSL_ENC_FLAG_TLS1_2_CIPHERS,
  67. ssl3_set_handshake_header,
  68. tls_close_construct_packet,
  69. ssl3_handshake_write
  70. };
  71. SSL3_ENC_METHOD const TLSv1_3_enc_data = {
  72. tls13_enc,
  73. tls1_mac,
  74. tls13_setup_key_block,
  75. tls13_generate_master_secret,
  76. tls13_change_cipher_state,
  77. tls13_final_finish_mac,
  78. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  79. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  80. tls13_alert_code,
  81. tls13_export_keying_material,
  82. SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF,
  83. ssl3_set_handshake_header,
  84. tls_close_construct_packet,
  85. ssl3_handshake_write
  86. };
  87. long tls1_default_timeout(void)
  88. {
  89. /*
  90. * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
  91. * http, the cache would over fill
  92. */
  93. return (60 * 60 * 2);
  94. }
  95. int tls1_new(SSL *s)
  96. {
  97. if (!ssl3_new(s))
  98. return 0;
  99. if (!s->method->ssl_clear(s))
  100. return 0;
  101. return 1;
  102. }
  103. void tls1_free(SSL *s)
  104. {
  105. OPENSSL_free(s->ext.session_ticket);
  106. ssl3_free(s);
  107. }
  108. int tls1_clear(SSL *s)
  109. {
  110. if (!ssl3_clear(s))
  111. return 0;
  112. if (s->method->version == TLS_ANY_VERSION)
  113. s->version = TLS_MAX_VERSION;
  114. else
  115. s->version = s->method->version;
  116. return 1;
  117. }
  118. #ifndef OPENSSL_NO_EC
  119. /*
  120. * Table of curve information.
  121. * Do not delete entries or reorder this array! It is used as a lookup
  122. * table: the index of each entry is one less than the TLS curve id.
  123. */
  124. static const TLS_GROUP_INFO nid_list[] = {
  125. {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
  126. {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */
  127. {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */
  128. {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */
  129. {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */
  130. {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */
  131. {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */
  132. {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */
  133. {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */
  134. {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */
  135. {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */
  136. {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */
  137. {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */
  138. {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */
  139. {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */
  140. {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */
  141. {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */
  142. {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */
  143. {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */
  144. {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */
  145. {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */
  146. {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */
  147. {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */
  148. {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */
  149. {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */
  150. {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
  151. {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
  152. {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */
  153. {EVP_PKEY_X25519, 128, TLS_CURVE_CUSTOM}, /* X25519 (29) */
  154. {EVP_PKEY_X448, 224, TLS_CURVE_CUSTOM}, /* X448 (30) */
  155. };
  156. static const unsigned char ecformats_default[] = {
  157. TLSEXT_ECPOINTFORMAT_uncompressed,
  158. TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
  159. TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
  160. };
  161. /* The default curves */
  162. static const uint16_t eccurves_default[] = {
  163. 29, /* X25519 (29) */
  164. 23, /* secp256r1 (23) */
  165. 30, /* X448 (30) */
  166. 25, /* secp521r1 (25) */
  167. 24, /* secp384r1 (24) */
  168. };
  169. static const uint16_t suiteb_curves[] = {
  170. TLSEXT_curve_P_256,
  171. TLSEXT_curve_P_384
  172. };
  173. const TLS_GROUP_INFO *tls1_group_id_lookup(uint16_t group_id)
  174. {
  175. /* ECC curves from RFC 4492 and RFC 7027 */
  176. if (group_id < 1 || group_id > OSSL_NELEM(nid_list))
  177. return NULL;
  178. return &nid_list[group_id - 1];
  179. }
  180. static uint16_t tls1_nid2group_id(int nid)
  181. {
  182. size_t i;
  183. for (i = 0; i < OSSL_NELEM(nid_list); i++) {
  184. if (nid_list[i].nid == nid)
  185. return (uint16_t)(i + 1);
  186. }
  187. return 0;
  188. }
  189. /*
  190. * Set *pgroups to the supported groups list and *pgroupslen to
  191. * the number of groups supported.
  192. */
  193. void tls1_get_supported_groups(SSL *s, const uint16_t **pgroups,
  194. size_t *pgroupslen)
  195. {
  196. /* For Suite B mode only include P-256, P-384 */
  197. switch (tls1_suiteb(s)) {
  198. case SSL_CERT_FLAG_SUITEB_128_LOS:
  199. *pgroups = suiteb_curves;
  200. *pgroupslen = OSSL_NELEM(suiteb_curves);
  201. break;
  202. case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
  203. *pgroups = suiteb_curves;
  204. *pgroupslen = 1;
  205. break;
  206. case SSL_CERT_FLAG_SUITEB_192_LOS:
  207. *pgroups = suiteb_curves + 1;
  208. *pgroupslen = 1;
  209. break;
  210. default:
  211. if (s->ext.supportedgroups == NULL) {
  212. *pgroups = eccurves_default;
  213. *pgroupslen = OSSL_NELEM(eccurves_default);
  214. } else {
  215. *pgroups = s->ext.supportedgroups;
  216. *pgroupslen = s->ext.supportedgroups_len;
  217. }
  218. break;
  219. }
  220. }
  221. /* See if curve is allowed by security callback */
  222. int tls_curve_allowed(SSL *s, uint16_t curve, int op)
  223. {
  224. const TLS_GROUP_INFO *cinfo = tls1_group_id_lookup(curve);
  225. unsigned char ctmp[2];
  226. if (cinfo == NULL)
  227. return 0;
  228. # ifdef OPENSSL_NO_EC2M
  229. if (cinfo->flags & TLS_CURVE_CHAR2)
  230. return 0;
  231. # endif
  232. ctmp[0] = curve >> 8;
  233. ctmp[1] = curve & 0xff;
  234. return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)ctmp);
  235. }
  236. /* Return 1 if "id" is in "list" */
  237. static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen)
  238. {
  239. size_t i;
  240. for (i = 0; i < listlen; i++)
  241. if (list[i] == id)
  242. return 1;
  243. return 0;
  244. }
  245. /*-
  246. * For nmatch >= 0, return the id of the |nmatch|th shared group or 0
  247. * if there is no match.
  248. * For nmatch == -1, return number of matches
  249. * For nmatch == -2, return the id of the group to use for
  250. * a tmp key, or 0 if there is no match.
  251. */
  252. uint16_t tls1_shared_group(SSL *s, int nmatch)
  253. {
  254. const uint16_t *pref, *supp;
  255. size_t num_pref, num_supp, i;
  256. int k;
  257. /* Can't do anything on client side */
  258. if (s->server == 0)
  259. return 0;
  260. if (nmatch == -2) {
  261. if (tls1_suiteb(s)) {
  262. /*
  263. * For Suite B ciphersuite determines curve: we already know
  264. * these are acceptable due to previous checks.
  265. */
  266. unsigned long cid = s->s3->tmp.new_cipher->id;
  267. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
  268. return TLSEXT_curve_P_256;
  269. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
  270. return TLSEXT_curve_P_384;
  271. /* Should never happen */
  272. return 0;
  273. }
  274. /* If not Suite B just return first preference shared curve */
  275. nmatch = 0;
  276. }
  277. /*
  278. * If server preference set, our groups are the preference order
  279. * otherwise peer decides.
  280. */
  281. if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
  282. tls1_get_supported_groups(s, &pref, &num_pref);
  283. tls1_get_peer_groups(s, &supp, &num_supp);
  284. } else {
  285. tls1_get_peer_groups(s, &pref, &num_pref);
  286. tls1_get_supported_groups(s, &supp, &num_supp);
  287. }
  288. for (k = 0, i = 0; i < num_pref; i++) {
  289. uint16_t id = pref[i];
  290. if (!tls1_in_list(id, supp, num_supp)
  291. || !tls_curve_allowed(s, id, SSL_SECOP_CURVE_SHARED))
  292. continue;
  293. if (nmatch == k)
  294. return id;
  295. k++;
  296. }
  297. if (nmatch == -1)
  298. return k;
  299. /* Out of range (nmatch > k). */
  300. return 0;
  301. }
  302. int tls1_set_groups(uint16_t **pext, size_t *pextlen,
  303. int *groups, size_t ngroups)
  304. {
  305. uint16_t *glist;
  306. size_t i;
  307. /*
  308. * Bitmap of groups included to detect duplicates: only works while group
  309. * ids < 32
  310. */
  311. unsigned long dup_list = 0;
  312. if ((glist = OPENSSL_malloc(ngroups * sizeof(*glist))) == NULL) {
  313. SSLerr(SSL_F_TLS1_SET_GROUPS, ERR_R_MALLOC_FAILURE);
  314. return 0;
  315. }
  316. for (i = 0; i < ngroups; i++) {
  317. unsigned long idmask;
  318. uint16_t id;
  319. /* TODO(TLS1.3): Convert for DH groups */
  320. id = tls1_nid2group_id(groups[i]);
  321. idmask = 1L << id;
  322. if (!id || (dup_list & idmask)) {
  323. OPENSSL_free(glist);
  324. return 0;
  325. }
  326. dup_list |= idmask;
  327. glist[i] = id;
  328. }
  329. OPENSSL_free(*pext);
  330. *pext = glist;
  331. *pextlen = ngroups;
  332. return 1;
  333. }
  334. # define MAX_CURVELIST OSSL_NELEM(nid_list)
  335. typedef struct {
  336. size_t nidcnt;
  337. int nid_arr[MAX_CURVELIST];
  338. } nid_cb_st;
  339. static int nid_cb(const char *elem, int len, void *arg)
  340. {
  341. nid_cb_st *narg = arg;
  342. size_t i;
  343. int nid;
  344. char etmp[20];
  345. if (elem == NULL)
  346. return 0;
  347. if (narg->nidcnt == MAX_CURVELIST)
  348. return 0;
  349. if (len > (int)(sizeof(etmp) - 1))
  350. return 0;
  351. memcpy(etmp, elem, len);
  352. etmp[len] = 0;
  353. nid = EC_curve_nist2nid(etmp);
  354. if (nid == NID_undef)
  355. nid = OBJ_sn2nid(etmp);
  356. if (nid == NID_undef)
  357. nid = OBJ_ln2nid(etmp);
  358. if (nid == NID_undef)
  359. return 0;
  360. for (i = 0; i < narg->nidcnt; i++)
  361. if (narg->nid_arr[i] == nid)
  362. return 0;
  363. narg->nid_arr[narg->nidcnt++] = nid;
  364. return 1;
  365. }
  366. /* Set groups based on a colon separate list */
  367. int tls1_set_groups_list(uint16_t **pext, size_t *pextlen, const char *str)
  368. {
  369. nid_cb_st ncb;
  370. ncb.nidcnt = 0;
  371. if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
  372. return 0;
  373. if (pext == NULL)
  374. return 1;
  375. return tls1_set_groups(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
  376. }
  377. /* Return group id of a key */
  378. static uint16_t tls1_get_group_id(EVP_PKEY *pkey)
  379. {
  380. EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
  381. const EC_GROUP *grp;
  382. if (ec == NULL)
  383. return 0;
  384. grp = EC_KEY_get0_group(ec);
  385. return tls1_nid2group_id(EC_GROUP_get_curve_name(grp));
  386. }
  387. /* Check a key is compatible with compression extension */
  388. static int tls1_check_pkey_comp(SSL *s, EVP_PKEY *pkey)
  389. {
  390. const EC_KEY *ec;
  391. const EC_GROUP *grp;
  392. unsigned char comp_id;
  393. size_t i;
  394. /* If not an EC key nothing to check */
  395. if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
  396. return 1;
  397. ec = EVP_PKEY_get0_EC_KEY(pkey);
  398. grp = EC_KEY_get0_group(ec);
  399. /* Get required compression id */
  400. if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
  401. comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
  402. } else if (SSL_IS_TLS13(s)) {
  403. /*
  404. * ec_point_formats extension is not used in TLSv1.3 so we ignore
  405. * this check.
  406. */
  407. return 1;
  408. } else {
  409. int field_type = EC_METHOD_get_field_type(EC_GROUP_method_of(grp));
  410. if (field_type == NID_X9_62_prime_field)
  411. comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
  412. else if (field_type == NID_X9_62_characteristic_two_field)
  413. comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
  414. else
  415. return 0;
  416. }
  417. /*
  418. * If point formats extension present check it, otherwise everything is
  419. * supported (see RFC4492).
  420. */
  421. if (s->session->ext.ecpointformats == NULL)
  422. return 1;
  423. for (i = 0; i < s->session->ext.ecpointformats_len; i++) {
  424. if (s->session->ext.ecpointformats[i] == comp_id)
  425. return 1;
  426. }
  427. return 0;
  428. }
  429. /* Check a group id matches preferences */
  430. int tls1_check_group_id(SSL *s, uint16_t group_id, int check_own_groups)
  431. {
  432. const uint16_t *groups;
  433. size_t groups_len;
  434. if (group_id == 0)
  435. return 0;
  436. /* Check for Suite B compliance */
  437. if (tls1_suiteb(s) && s->s3->tmp.new_cipher != NULL) {
  438. unsigned long cid = s->s3->tmp.new_cipher->id;
  439. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
  440. if (group_id != TLSEXT_curve_P_256)
  441. return 0;
  442. } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
  443. if (group_id != TLSEXT_curve_P_384)
  444. return 0;
  445. } else {
  446. /* Should never happen */
  447. return 0;
  448. }
  449. }
  450. if (check_own_groups) {
  451. /* Check group is one of our preferences */
  452. tls1_get_supported_groups(s, &groups, &groups_len);
  453. if (!tls1_in_list(group_id, groups, groups_len))
  454. return 0;
  455. }
  456. if (!tls_curve_allowed(s, group_id, SSL_SECOP_CURVE_CHECK))
  457. return 0;
  458. /* For clients, nothing more to check */
  459. if (!s->server)
  460. return 1;
  461. /* Check group is one of peers preferences */
  462. tls1_get_peer_groups(s, &groups, &groups_len);
  463. /*
  464. * RFC 4492 does not require the supported elliptic curves extension
  465. * so if it is not sent we can just choose any curve.
  466. * It is invalid to send an empty list in the supported groups
  467. * extension, so groups_len == 0 always means no extension.
  468. */
  469. if (groups_len == 0)
  470. return 1;
  471. return tls1_in_list(group_id, groups, groups_len);
  472. }
  473. void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
  474. size_t *num_formats)
  475. {
  476. /*
  477. * If we have a custom point format list use it otherwise use default
  478. */
  479. if (s->ext.ecpointformats) {
  480. *pformats = s->ext.ecpointformats;
  481. *num_formats = s->ext.ecpointformats_len;
  482. } else {
  483. *pformats = ecformats_default;
  484. /* For Suite B we don't support char2 fields */
  485. if (tls1_suiteb(s))
  486. *num_formats = sizeof(ecformats_default) - 1;
  487. else
  488. *num_formats = sizeof(ecformats_default);
  489. }
  490. }
  491. /*
  492. * Check cert parameters compatible with extensions: currently just checks EC
  493. * certificates have compatible curves and compression.
  494. */
  495. static int tls1_check_cert_param(SSL *s, X509 *x, int check_ee_md)
  496. {
  497. uint16_t group_id;
  498. EVP_PKEY *pkey;
  499. pkey = X509_get0_pubkey(x);
  500. if (pkey == NULL)
  501. return 0;
  502. /* If not EC nothing to do */
  503. if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
  504. return 1;
  505. /* Check compression */
  506. if (!tls1_check_pkey_comp(s, pkey))
  507. return 0;
  508. group_id = tls1_get_group_id(pkey);
  509. /*
  510. * For a server we allow the certificate to not be in our list of supported
  511. * groups.
  512. */
  513. if (!tls1_check_group_id(s, group_id, !s->server))
  514. return 0;
  515. /*
  516. * Special case for suite B. We *MUST* sign using SHA256+P-256 or
  517. * SHA384+P-384.
  518. */
  519. if (check_ee_md && tls1_suiteb(s)) {
  520. int check_md;
  521. size_t i;
  522. CERT *c = s->cert;
  523. /* Check to see we have necessary signing algorithm */
  524. if (group_id == TLSEXT_curve_P_256)
  525. check_md = NID_ecdsa_with_SHA256;
  526. else if (group_id == TLSEXT_curve_P_384)
  527. check_md = NID_ecdsa_with_SHA384;
  528. else
  529. return 0; /* Should never happen */
  530. for (i = 0; i < c->shared_sigalgslen; i++) {
  531. if (check_md == c->shared_sigalgs[i]->sigandhash)
  532. return 1;;
  533. }
  534. return 0;
  535. }
  536. return 1;
  537. }
  538. /*
  539. * tls1_check_ec_tmp_key - Check EC temporary key compatibility
  540. * @s: SSL connection
  541. * @cid: Cipher ID we're considering using
  542. *
  543. * Checks that the kECDHE cipher suite we're considering using
  544. * is compatible with the client extensions.
  545. *
  546. * Returns 0 when the cipher can't be used or 1 when it can.
  547. */
  548. int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
  549. {
  550. /* If not Suite B just need a shared group */
  551. if (!tls1_suiteb(s))
  552. return tls1_shared_group(s, 0) != 0;
  553. /*
  554. * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
  555. * curves permitted.
  556. */
  557. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
  558. return tls1_check_group_id(s, TLSEXT_curve_P_256, 1);
  559. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
  560. return tls1_check_group_id(s, TLSEXT_curve_P_384, 1);
  561. return 0;
  562. }
  563. #else
  564. static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
  565. {
  566. return 1;
  567. }
  568. #endif /* OPENSSL_NO_EC */
  569. /* Default sigalg schemes */
  570. static const uint16_t tls12_sigalgs[] = {
  571. #ifndef OPENSSL_NO_EC
  572. TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
  573. TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
  574. TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
  575. TLSEXT_SIGALG_ed25519,
  576. TLSEXT_SIGALG_ed448,
  577. #endif
  578. TLSEXT_SIGALG_rsa_pss_pss_sha256,
  579. TLSEXT_SIGALG_rsa_pss_pss_sha384,
  580. TLSEXT_SIGALG_rsa_pss_pss_sha512,
  581. TLSEXT_SIGALG_rsa_pss_rsae_sha256,
  582. TLSEXT_SIGALG_rsa_pss_rsae_sha384,
  583. TLSEXT_SIGALG_rsa_pss_rsae_sha512,
  584. TLSEXT_SIGALG_rsa_pkcs1_sha256,
  585. TLSEXT_SIGALG_rsa_pkcs1_sha384,
  586. TLSEXT_SIGALG_rsa_pkcs1_sha512,
  587. #ifndef OPENSSL_NO_EC
  588. TLSEXT_SIGALG_ecdsa_sha224,
  589. TLSEXT_SIGALG_ecdsa_sha1,
  590. #endif
  591. TLSEXT_SIGALG_rsa_pkcs1_sha224,
  592. TLSEXT_SIGALG_rsa_pkcs1_sha1,
  593. #ifndef OPENSSL_NO_DSA
  594. TLSEXT_SIGALG_dsa_sha224,
  595. TLSEXT_SIGALG_dsa_sha1,
  596. TLSEXT_SIGALG_dsa_sha256,
  597. TLSEXT_SIGALG_dsa_sha384,
  598. TLSEXT_SIGALG_dsa_sha512,
  599. #endif
  600. #ifndef OPENSSL_NO_GOST
  601. TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,
  602. TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,
  603. TLSEXT_SIGALG_gostr34102001_gostr3411,
  604. #endif
  605. };
  606. #ifndef OPENSSL_NO_EC
  607. static const uint16_t suiteb_sigalgs[] = {
  608. TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
  609. TLSEXT_SIGALG_ecdsa_secp384r1_sha384
  610. };
  611. #endif
  612. static const SIGALG_LOOKUP sigalg_lookup_tbl[] = {
  613. #ifndef OPENSSL_NO_EC
  614. {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
  615. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  616. NID_ecdsa_with_SHA256, NID_X9_62_prime256v1},
  617. {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
  618. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  619. NID_ecdsa_with_SHA384, NID_secp384r1},
  620. {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
  621. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  622. NID_ecdsa_with_SHA512, NID_secp521r1},
  623. {"ed25519", TLSEXT_SIGALG_ed25519,
  624. NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519,
  625. NID_undef, NID_undef},
  626. {"ed448", TLSEXT_SIGALG_ed448,
  627. NID_undef, -1, EVP_PKEY_ED448, SSL_PKEY_ED448,
  628. NID_undef, NID_undef},
  629. {NULL, TLSEXT_SIGALG_ecdsa_sha224,
  630. NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  631. NID_ecdsa_with_SHA224, NID_undef},
  632. {NULL, TLSEXT_SIGALG_ecdsa_sha1,
  633. NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  634. NID_ecdsa_with_SHA1, NID_undef},
  635. #endif
  636. {"rsa_pss_rsae_sha256", TLSEXT_SIGALG_rsa_pss_rsae_sha256,
  637. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
  638. NID_undef, NID_undef},
  639. {"rsa_pss_rsae_sha384", TLSEXT_SIGALG_rsa_pss_rsae_sha384,
  640. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
  641. NID_undef, NID_undef},
  642. {"rsa_pss_rsae_sha512", TLSEXT_SIGALG_rsa_pss_rsae_sha512,
  643. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
  644. NID_undef, NID_undef},
  645. {"rsa_pss_pss_sha256", TLSEXT_SIGALG_rsa_pss_pss_sha256,
  646. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
  647. NID_undef, NID_undef},
  648. {"rsa_pss_pss_sha384", TLSEXT_SIGALG_rsa_pss_pss_sha384,
  649. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
  650. NID_undef, NID_undef},
  651. {"rsa_pss_pss_sha512", TLSEXT_SIGALG_rsa_pss_pss_sha512,
  652. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
  653. NID_undef, NID_undef},
  654. {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256,
  655. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  656. NID_sha256WithRSAEncryption, NID_undef},
  657. {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384,
  658. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  659. NID_sha384WithRSAEncryption, NID_undef},
  660. {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512,
  661. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  662. NID_sha512WithRSAEncryption, NID_undef},
  663. {"rsa_pkcs1_sha224", TLSEXT_SIGALG_rsa_pkcs1_sha224,
  664. NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  665. NID_sha224WithRSAEncryption, NID_undef},
  666. {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1,
  667. NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  668. NID_sha1WithRSAEncryption, NID_undef},
  669. #ifndef OPENSSL_NO_DSA
  670. {NULL, TLSEXT_SIGALG_dsa_sha256,
  671. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  672. NID_dsa_with_SHA256, NID_undef},
  673. {NULL, TLSEXT_SIGALG_dsa_sha384,
  674. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  675. NID_undef, NID_undef},
  676. {NULL, TLSEXT_SIGALG_dsa_sha512,
  677. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  678. NID_undef, NID_undef},
  679. {NULL, TLSEXT_SIGALG_dsa_sha224,
  680. NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  681. NID_undef, NID_undef},
  682. {NULL, TLSEXT_SIGALG_dsa_sha1,
  683. NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  684. NID_dsaWithSHA1, NID_undef},
  685. #endif
  686. #ifndef OPENSSL_NO_GOST
  687. {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,
  688. NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX,
  689. NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256,
  690. NID_undef, NID_undef},
  691. {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,
  692. NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX,
  693. NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512,
  694. NID_undef, NID_undef},
  695. {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411,
  696. NID_id_GostR3411_94, SSL_MD_GOST94_IDX,
  697. NID_id_GostR3410_2001, SSL_PKEY_GOST01,
  698. NID_undef, NID_undef}
  699. #endif
  700. };
  701. /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */
  702. static const SIGALG_LOOKUP legacy_rsa_sigalg = {
  703. "rsa_pkcs1_md5_sha1", 0,
  704. NID_md5_sha1, SSL_MD_MD5_SHA1_IDX,
  705. EVP_PKEY_RSA, SSL_PKEY_RSA,
  706. NID_undef, NID_undef
  707. };
  708. /*
  709. * Default signature algorithm values used if signature algorithms not present.
  710. * From RFC5246. Note: order must match certificate index order.
  711. */
  712. static const uint16_t tls_default_sigalg[] = {
  713. TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */
  714. 0, /* SSL_PKEY_RSA_PSS_SIGN */
  715. TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */
  716. TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */
  717. TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */
  718. TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, /* SSL_PKEY_GOST12_256 */
  719. TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, /* SSL_PKEY_GOST12_512 */
  720. 0, /* SSL_PKEY_ED25519 */
  721. 0, /* SSL_PKEY_ED448 */
  722. };
  723. /* Lookup TLS signature algorithm */
  724. static const SIGALG_LOOKUP *tls1_lookup_sigalg(uint16_t sigalg)
  725. {
  726. size_t i;
  727. const SIGALG_LOOKUP *s;
  728. for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
  729. i++, s++) {
  730. if (s->sigalg == sigalg)
  731. return s;
  732. }
  733. return NULL;
  734. }
  735. /* Lookup hash: return 0 if invalid or not enabled */
  736. int tls1_lookup_md(const SIGALG_LOOKUP *lu, const EVP_MD **pmd)
  737. {
  738. const EVP_MD *md;
  739. if (lu == NULL)
  740. return 0;
  741. /* lu->hash == NID_undef means no associated digest */
  742. if (lu->hash == NID_undef) {
  743. md = NULL;
  744. } else {
  745. md = ssl_md(lu->hash_idx);
  746. if (md == NULL)
  747. return 0;
  748. }
  749. if (pmd)
  750. *pmd = md;
  751. return 1;
  752. }
  753. /*
  754. * Check if key is large enough to generate RSA-PSS signature.
  755. *
  756. * The key must greater than or equal to 2 * hash length + 2.
  757. * SHA512 has a hash length of 64 bytes, which is incompatible
  758. * with a 128 byte (1024 bit) key.
  759. */
  760. #define RSA_PSS_MINIMUM_KEY_SIZE(md) (2 * EVP_MD_size(md) + 2)
  761. static int rsa_pss_check_min_key_size(const RSA *rsa, const SIGALG_LOOKUP *lu)
  762. {
  763. const EVP_MD *md;
  764. if (rsa == NULL)
  765. return 0;
  766. if (!tls1_lookup_md(lu, &md) || md == NULL)
  767. return 0;
  768. if (RSA_size(rsa) < RSA_PSS_MINIMUM_KEY_SIZE(md))
  769. return 0;
  770. return 1;
  771. }
  772. /*
  773. * Return a signature algorithm for TLS < 1.2 where the signature type
  774. * is fixed by the certificate type.
  775. */
  776. static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx)
  777. {
  778. if (idx == -1) {
  779. if (s->server) {
  780. size_t i;
  781. /* Work out index corresponding to ciphersuite */
  782. for (i = 0; i < SSL_PKEY_NUM; i++) {
  783. const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(i);
  784. if (clu->amask & s->s3->tmp.new_cipher->algorithm_auth) {
  785. idx = i;
  786. break;
  787. }
  788. }
  789. /*
  790. * Some GOST ciphersuites allow more than one signature algorithms
  791. * */
  792. if (idx == SSL_PKEY_GOST01 && s->s3->tmp.new_cipher->algorithm_auth != SSL_aGOST01) {
  793. int real_idx;
  794. for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST01;
  795. real_idx--) {
  796. if (s->cert->pkeys[real_idx].privatekey != NULL) {
  797. idx = real_idx;
  798. break;
  799. }
  800. }
  801. }
  802. } else {
  803. idx = s->cert->key - s->cert->pkeys;
  804. }
  805. }
  806. if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg))
  807. return NULL;
  808. if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) {
  809. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(tls_default_sigalg[idx]);
  810. if (!tls1_lookup_md(lu, NULL))
  811. return NULL;
  812. return lu;
  813. }
  814. return &legacy_rsa_sigalg;
  815. }
  816. /* Set peer sigalg based key type */
  817. int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey)
  818. {
  819. size_t idx;
  820. const SIGALG_LOOKUP *lu;
  821. if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL)
  822. return 0;
  823. lu = tls1_get_legacy_sigalg(s, idx);
  824. if (lu == NULL)
  825. return 0;
  826. s->s3->tmp.peer_sigalg = lu;
  827. return 1;
  828. }
  829. size_t tls12_get_psigalgs(SSL *s, int sent, const uint16_t **psigs)
  830. {
  831. /*
  832. * If Suite B mode use Suite B sigalgs only, ignore any other
  833. * preferences.
  834. */
  835. #ifndef OPENSSL_NO_EC
  836. switch (tls1_suiteb(s)) {
  837. case SSL_CERT_FLAG_SUITEB_128_LOS:
  838. *psigs = suiteb_sigalgs;
  839. return OSSL_NELEM(suiteb_sigalgs);
  840. case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
  841. *psigs = suiteb_sigalgs;
  842. return 1;
  843. case SSL_CERT_FLAG_SUITEB_192_LOS:
  844. *psigs = suiteb_sigalgs + 1;
  845. return 1;
  846. }
  847. #endif
  848. /*
  849. * We use client_sigalgs (if not NULL) if we're a server
  850. * and sending a certificate request or if we're a client and
  851. * determining which shared algorithm to use.
  852. */
  853. if ((s->server == sent) && s->cert->client_sigalgs != NULL) {
  854. *psigs = s->cert->client_sigalgs;
  855. return s->cert->client_sigalgslen;
  856. } else if (s->cert->conf_sigalgs) {
  857. *psigs = s->cert->conf_sigalgs;
  858. return s->cert->conf_sigalgslen;
  859. } else {
  860. *psigs = tls12_sigalgs;
  861. return OSSL_NELEM(tls12_sigalgs);
  862. }
  863. }
  864. /*
  865. * Check signature algorithm is consistent with sent supported signature
  866. * algorithms and if so set relevant digest and signature scheme in
  867. * s.
  868. */
  869. int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey)
  870. {
  871. const uint16_t *sent_sigs;
  872. const EVP_MD *md = NULL;
  873. char sigalgstr[2];
  874. size_t sent_sigslen, i, cidx;
  875. int pkeyid = EVP_PKEY_id(pkey);
  876. const SIGALG_LOOKUP *lu;
  877. /* Should never happen */
  878. if (pkeyid == -1)
  879. return -1;
  880. if (SSL_IS_TLS13(s)) {
  881. /* Disallow DSA for TLS 1.3 */
  882. if (pkeyid == EVP_PKEY_DSA) {
  883. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG,
  884. SSL_R_WRONG_SIGNATURE_TYPE);
  885. return 0;
  886. }
  887. /* Only allow PSS for TLS 1.3 */
  888. if (pkeyid == EVP_PKEY_RSA)
  889. pkeyid = EVP_PKEY_RSA_PSS;
  890. }
  891. lu = tls1_lookup_sigalg(sig);
  892. /*
  893. * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type
  894. * is consistent with signature: RSA keys can be used for RSA-PSS
  895. */
  896. if (lu == NULL
  897. || (SSL_IS_TLS13(s) && (lu->hash == NID_sha1 || lu->hash == NID_sha224))
  898. || (pkeyid != lu->sig
  899. && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) {
  900. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG,
  901. SSL_R_WRONG_SIGNATURE_TYPE);
  902. return 0;
  903. }
  904. /* Check the sigalg is consistent with the key OID */
  905. if (!ssl_cert_lookup_by_nid(EVP_PKEY_id(pkey), &cidx)
  906. || lu->sig_idx != (int)cidx) {
  907. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG,
  908. SSL_R_WRONG_SIGNATURE_TYPE);
  909. return 0;
  910. }
  911. #ifndef OPENSSL_NO_EC
  912. if (pkeyid == EVP_PKEY_EC) {
  913. /* Check point compression is permitted */
  914. if (!tls1_check_pkey_comp(s, pkey)) {
  915. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  916. SSL_F_TLS12_CHECK_PEER_SIGALG,
  917. SSL_R_ILLEGAL_POINT_COMPRESSION);
  918. return 0;
  919. }
  920. /* For TLS 1.3 or Suite B check curve matches signature algorithm */
  921. if (SSL_IS_TLS13(s) || tls1_suiteb(s)) {
  922. EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
  923. int curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
  924. if (lu->curve != NID_undef && curve != lu->curve) {
  925. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  926. SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
  927. return 0;
  928. }
  929. }
  930. if (!SSL_IS_TLS13(s)) {
  931. /* Check curve matches extensions */
  932. if (!tls1_check_group_id(s, tls1_get_group_id(pkey), 1)) {
  933. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  934. SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
  935. return 0;
  936. }
  937. if (tls1_suiteb(s)) {
  938. /* Check sigalg matches a permissible Suite B value */
  939. if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256
  940. && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) {
  941. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  942. SSL_F_TLS12_CHECK_PEER_SIGALG,
  943. SSL_R_WRONG_SIGNATURE_TYPE);
  944. return 0;
  945. }
  946. }
  947. }
  948. } else if (tls1_suiteb(s)) {
  949. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
  950. SSL_R_WRONG_SIGNATURE_TYPE);
  951. return 0;
  952. }
  953. #endif
  954. /* Check signature matches a type we sent */
  955. sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
  956. for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
  957. if (sig == *sent_sigs)
  958. break;
  959. }
  960. /* Allow fallback to SHA1 if not strict mode */
  961. if (i == sent_sigslen && (lu->hash != NID_sha1
  962. || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
  963. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
  964. SSL_R_WRONG_SIGNATURE_TYPE);
  965. return 0;
  966. }
  967. if (!tls1_lookup_md(lu, &md)) {
  968. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
  969. SSL_R_UNKNOWN_DIGEST);
  970. return 0;
  971. }
  972. if (md != NULL) {
  973. /*
  974. * Make sure security callback allows algorithm. For historical
  975. * reasons we have to pass the sigalg as a two byte char array.
  976. */
  977. sigalgstr[0] = (sig >> 8) & 0xff;
  978. sigalgstr[1] = sig & 0xff;
  979. if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
  980. EVP_MD_size(md) * 4, EVP_MD_type(md),
  981. (void *)sigalgstr)) {
  982. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
  983. SSL_R_WRONG_SIGNATURE_TYPE);
  984. return 0;
  985. }
  986. }
  987. /* Store the sigalg the peer uses */
  988. s->s3->tmp.peer_sigalg = lu;
  989. return 1;
  990. }
  991. int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid)
  992. {
  993. if (s->s3->tmp.peer_sigalg == NULL)
  994. return 0;
  995. *pnid = s->s3->tmp.peer_sigalg->sig;
  996. return 1;
  997. }
  998. /*
  999. * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
  1000. * supported, doesn't appear in supported signature algorithms, isn't supported
  1001. * by the enabled protocol versions or by the security level.
  1002. *
  1003. * This function should only be used for checking which ciphers are supported
  1004. * by the client.
  1005. *
  1006. * Call ssl_cipher_disabled() to check that it's enabled or not.
  1007. */
  1008. int ssl_set_client_disabled(SSL *s)
  1009. {
  1010. s->s3->tmp.mask_a = 0;
  1011. s->s3->tmp.mask_k = 0;
  1012. ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
  1013. if (ssl_get_min_max_version(s, &s->s3->tmp.min_ver,
  1014. &s->s3->tmp.max_ver, NULL) != 0)
  1015. return 0;
  1016. #ifndef OPENSSL_NO_PSK
  1017. /* with PSK there must be client callback set */
  1018. if (!s->psk_client_callback) {
  1019. s->s3->tmp.mask_a |= SSL_aPSK;
  1020. s->s3->tmp.mask_k |= SSL_PSK;
  1021. }
  1022. #endif /* OPENSSL_NO_PSK */
  1023. #ifndef OPENSSL_NO_SRP
  1024. if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
  1025. s->s3->tmp.mask_a |= SSL_aSRP;
  1026. s->s3->tmp.mask_k |= SSL_kSRP;
  1027. }
  1028. #endif
  1029. return 1;
  1030. }
  1031. /*
  1032. * ssl_cipher_disabled - check that a cipher is disabled or not
  1033. * @s: SSL connection that you want to use the cipher on
  1034. * @c: cipher to check
  1035. * @op: Security check that you want to do
  1036. * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3
  1037. *
  1038. * Returns 1 when it's disabled, 0 when enabled.
  1039. */
  1040. int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op, int ecdhe)
  1041. {
  1042. if (c->algorithm_mkey & s->s3->tmp.mask_k
  1043. || c->algorithm_auth & s->s3->tmp.mask_a)
  1044. return 1;
  1045. if (s->s3->tmp.max_ver == 0)
  1046. return 1;
  1047. if (!SSL_IS_DTLS(s)) {
  1048. int min_tls = c->min_tls;
  1049. /*
  1050. * For historical reasons we will allow ECHDE to be selected by a server
  1051. * in SSLv3 if we are a client
  1052. */
  1053. if (min_tls == TLS1_VERSION && ecdhe
  1054. && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0)
  1055. min_tls = SSL3_VERSION;
  1056. if ((min_tls > s->s3->tmp.max_ver) || (c->max_tls < s->s3->tmp.min_ver))
  1057. return 1;
  1058. }
  1059. if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver)
  1060. || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver)))
  1061. return 1;
  1062. return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
  1063. }
  1064. int tls_use_ticket(SSL *s)
  1065. {
  1066. if ((s->options & SSL_OP_NO_TICKET))
  1067. return 0;
  1068. return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
  1069. }
  1070. int tls1_set_server_sigalgs(SSL *s)
  1071. {
  1072. size_t i;
  1073. /* Clear any shared signature algorithms */
  1074. OPENSSL_free(s->cert->shared_sigalgs);
  1075. s->cert->shared_sigalgs = NULL;
  1076. s->cert->shared_sigalgslen = 0;
  1077. /* Clear certificate validity flags */
  1078. for (i = 0; i < SSL_PKEY_NUM; i++)
  1079. s->s3->tmp.valid_flags[i] = 0;
  1080. /*
  1081. * If peer sent no signature algorithms check to see if we support
  1082. * the default algorithm for each certificate type
  1083. */
  1084. if (s->s3->tmp.peer_cert_sigalgs == NULL
  1085. && s->s3->tmp.peer_sigalgs == NULL) {
  1086. const uint16_t *sent_sigs;
  1087. size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
  1088. for (i = 0; i < SSL_PKEY_NUM; i++) {
  1089. const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i);
  1090. size_t j;
  1091. if (lu == NULL)
  1092. continue;
  1093. /* Check default matches a type we sent */
  1094. for (j = 0; j < sent_sigslen; j++) {
  1095. if (lu->sigalg == sent_sigs[j]) {
  1096. s->s3->tmp.valid_flags[i] = CERT_PKEY_SIGN;
  1097. break;
  1098. }
  1099. }
  1100. }
  1101. return 1;
  1102. }
  1103. if (!tls1_process_sigalgs(s)) {
  1104. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1105. SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_INTERNAL_ERROR);
  1106. return 0;
  1107. }
  1108. if (s->cert->shared_sigalgs != NULL)
  1109. return 1;
  1110. /* Fatal error if no shared signature algorithms */
  1111. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS1_SET_SERVER_SIGALGS,
  1112. SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
  1113. return 0;
  1114. }
  1115. /*-
  1116. * Gets the ticket information supplied by the client if any.
  1117. *
  1118. * hello: The parsed ClientHello data
  1119. * ret: (output) on return, if a ticket was decrypted, then this is set to
  1120. * point to the resulting session.
  1121. */
  1122. SSL_TICKET_STATUS tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello,
  1123. SSL_SESSION **ret)
  1124. {
  1125. size_t size;
  1126. RAW_EXTENSION *ticketext;
  1127. *ret = NULL;
  1128. s->ext.ticket_expected = 0;
  1129. /*
  1130. * If tickets disabled or not supported by the protocol version
  1131. * (e.g. TLSv1.3) behave as if no ticket present to permit stateful
  1132. * resumption.
  1133. */
  1134. if (s->version <= SSL3_VERSION || !tls_use_ticket(s))
  1135. return SSL_TICKET_NONE;
  1136. ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket];
  1137. if (!ticketext->present)
  1138. return SSL_TICKET_NONE;
  1139. size = PACKET_remaining(&ticketext->data);
  1140. return tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size,
  1141. hello->session_id, hello->session_id_len, ret);
  1142. }
  1143. /*-
  1144. * tls_decrypt_ticket attempts to decrypt a session ticket.
  1145. *
  1146. * If s->tls_session_secret_cb is set and we're not doing TLSv1.3 then we are
  1147. * expecting a pre-shared key ciphersuite, in which case we have no use for
  1148. * session tickets and one will never be decrypted, nor will
  1149. * s->ext.ticket_expected be set to 1.
  1150. *
  1151. * Side effects:
  1152. * Sets s->ext.ticket_expected to 1 if the server will have to issue
  1153. * a new session ticket to the client because the client indicated support
  1154. * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
  1155. * a session ticket or we couldn't use the one it gave us, or if
  1156. * s->ctx->ext.ticket_key_cb asked to renew the client's ticket.
  1157. * Otherwise, s->ext.ticket_expected is set to 0.
  1158. *
  1159. * etick: points to the body of the session ticket extension.
  1160. * eticklen: the length of the session tickets extension.
  1161. * sess_id: points at the session ID.
  1162. * sesslen: the length of the session ID.
  1163. * psess: (output) on return, if a ticket was decrypted, then this is set to
  1164. * point to the resulting session.
  1165. */
  1166. SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick,
  1167. size_t eticklen, const unsigned char *sess_id,
  1168. size_t sesslen, SSL_SESSION **psess)
  1169. {
  1170. SSL_SESSION *sess = NULL;
  1171. unsigned char *sdec;
  1172. const unsigned char *p;
  1173. int slen, renew_ticket = 0, declen;
  1174. SSL_TICKET_STATUS ret = SSL_TICKET_FATAL_ERR_OTHER;
  1175. size_t mlen;
  1176. unsigned char tick_hmac[EVP_MAX_MD_SIZE];
  1177. HMAC_CTX *hctx = NULL;
  1178. EVP_CIPHER_CTX *ctx = NULL;
  1179. SSL_CTX *tctx = s->session_ctx;
  1180. if (eticklen == 0) {
  1181. /*
  1182. * The client will accept a ticket but doesn't currently have
  1183. * one (TLSv1.2 and below), or treated as a fatal error in TLSv1.3
  1184. */
  1185. ret = SSL_TICKET_EMPTY;
  1186. goto end;
  1187. }
  1188. if (!SSL_IS_TLS13(s) && s->ext.session_secret_cb) {
  1189. /*
  1190. * Indicate that the ticket couldn't be decrypted rather than
  1191. * generating the session from ticket now, trigger
  1192. * abbreviated handshake based on external mechanism to
  1193. * calculate the master secret later.
  1194. */
  1195. ret = SSL_TICKET_NO_DECRYPT;
  1196. goto end;
  1197. }
  1198. /* Need at least keyname + iv */
  1199. if (eticklen < TLSEXT_KEYNAME_LENGTH + EVP_MAX_IV_LENGTH) {
  1200. ret = SSL_TICKET_NO_DECRYPT;
  1201. goto end;
  1202. }
  1203. /* Initialize session ticket encryption and HMAC contexts */
  1204. hctx = HMAC_CTX_new();
  1205. if (hctx == NULL) {
  1206. ret = SSL_TICKET_FATAL_ERR_MALLOC;
  1207. goto end;
  1208. }
  1209. ctx = EVP_CIPHER_CTX_new();
  1210. if (ctx == NULL) {
  1211. ret = SSL_TICKET_FATAL_ERR_MALLOC;
  1212. goto end;
  1213. }
  1214. if (tctx->ext.ticket_key_cb) {
  1215. unsigned char *nctick = (unsigned char *)etick;
  1216. int rv = tctx->ext.ticket_key_cb(s, nctick,
  1217. nctick + TLSEXT_KEYNAME_LENGTH,
  1218. ctx, hctx, 0);
  1219. if (rv < 0) {
  1220. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1221. goto end;
  1222. }
  1223. if (rv == 0) {
  1224. ret = SSL_TICKET_NO_DECRYPT;
  1225. goto end;
  1226. }
  1227. if (rv == 2)
  1228. renew_ticket = 1;
  1229. } else {
  1230. /* Check key name matches */
  1231. if (memcmp(etick, tctx->ext.tick_key_name,
  1232. TLSEXT_KEYNAME_LENGTH) != 0) {
  1233. ret = SSL_TICKET_NO_DECRYPT;
  1234. goto end;
  1235. }
  1236. if (HMAC_Init_ex(hctx, tctx->ext.secure->tick_hmac_key,
  1237. sizeof(tctx->ext.secure->tick_hmac_key),
  1238. EVP_sha256(), NULL) <= 0
  1239. || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
  1240. tctx->ext.secure->tick_aes_key,
  1241. etick + TLSEXT_KEYNAME_LENGTH) <= 0) {
  1242. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1243. goto end;
  1244. }
  1245. if (SSL_IS_TLS13(s))
  1246. renew_ticket = 1;
  1247. }
  1248. /*
  1249. * Attempt to process session ticket, first conduct sanity and integrity
  1250. * checks on ticket.
  1251. */
  1252. mlen = HMAC_size(hctx);
  1253. if (mlen == 0) {
  1254. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1255. goto end;
  1256. }
  1257. /* Sanity check ticket length: must exceed keyname + IV + HMAC */
  1258. if (eticklen <=
  1259. TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) {
  1260. ret = SSL_TICKET_NO_DECRYPT;
  1261. goto end;
  1262. }
  1263. eticklen -= mlen;
  1264. /* Check HMAC of encrypted ticket */
  1265. if (HMAC_Update(hctx, etick, eticklen) <= 0
  1266. || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
  1267. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1268. goto end;
  1269. }
  1270. if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
  1271. ret = SSL_TICKET_NO_DECRYPT;
  1272. goto end;
  1273. }
  1274. /* Attempt to decrypt session data */
  1275. /* Move p after IV to start of encrypted ticket, update length */
  1276. p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
  1277. eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
  1278. sdec = OPENSSL_malloc(eticklen);
  1279. if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p,
  1280. (int)eticklen) <= 0) {
  1281. OPENSSL_free(sdec);
  1282. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1283. goto end;
  1284. }
  1285. if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) {
  1286. OPENSSL_free(sdec);
  1287. ret = SSL_TICKET_NO_DECRYPT;
  1288. goto end;
  1289. }
  1290. slen += declen;
  1291. p = sdec;
  1292. sess = d2i_SSL_SESSION(NULL, &p, slen);
  1293. slen -= p - sdec;
  1294. OPENSSL_free(sdec);
  1295. if (sess) {
  1296. /* Some additional consistency checks */
  1297. if (slen != 0) {
  1298. SSL_SESSION_free(sess);
  1299. sess = NULL;
  1300. ret = SSL_TICKET_NO_DECRYPT;
  1301. goto end;
  1302. }
  1303. /*
  1304. * The session ID, if non-empty, is used by some clients to detect
  1305. * that the ticket has been accepted. So we copy it to the session
  1306. * structure. If it is empty set length to zero as required by
  1307. * standard.
  1308. */
  1309. if (sesslen) {
  1310. memcpy(sess->session_id, sess_id, sesslen);
  1311. sess->session_id_length = sesslen;
  1312. }
  1313. if (renew_ticket)
  1314. ret = SSL_TICKET_SUCCESS_RENEW;
  1315. else
  1316. ret = SSL_TICKET_SUCCESS;
  1317. goto end;
  1318. }
  1319. ERR_clear_error();
  1320. /*
  1321. * For session parse failure, indicate that we need to send a new ticket.
  1322. */
  1323. ret = SSL_TICKET_NO_DECRYPT;
  1324. end:
  1325. EVP_CIPHER_CTX_free(ctx);
  1326. HMAC_CTX_free(hctx);
  1327. /*
  1328. * If set, the decrypt_ticket_cb() is called unless a fatal error was
  1329. * detected above. The callback is responsible for checking |ret| before it
  1330. * performs any action
  1331. */
  1332. if (s->session_ctx->decrypt_ticket_cb != NULL
  1333. && (ret == SSL_TICKET_EMPTY
  1334. || ret == SSL_TICKET_NO_DECRYPT
  1335. || ret == SSL_TICKET_SUCCESS
  1336. || ret == SSL_TICKET_SUCCESS_RENEW)) {
  1337. size_t keyname_len = eticklen;
  1338. int retcb;
  1339. if (keyname_len > TLSEXT_KEYNAME_LENGTH)
  1340. keyname_len = TLSEXT_KEYNAME_LENGTH;
  1341. retcb = s->session_ctx->decrypt_ticket_cb(s, sess, etick, keyname_len,
  1342. ret,
  1343. s->session_ctx->ticket_cb_data);
  1344. switch (retcb) {
  1345. case SSL_TICKET_RETURN_ABORT:
  1346. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1347. break;
  1348. case SSL_TICKET_RETURN_IGNORE:
  1349. ret = SSL_TICKET_NONE;
  1350. SSL_SESSION_free(sess);
  1351. sess = NULL;
  1352. break;
  1353. case SSL_TICKET_RETURN_IGNORE_RENEW:
  1354. if (ret != SSL_TICKET_EMPTY && ret != SSL_TICKET_NO_DECRYPT)
  1355. ret = SSL_TICKET_NO_DECRYPT;
  1356. /* else the value of |ret| will already do the right thing */
  1357. SSL_SESSION_free(sess);
  1358. sess = NULL;
  1359. break;
  1360. case SSL_TICKET_RETURN_USE:
  1361. case SSL_TICKET_RETURN_USE_RENEW:
  1362. if (ret != SSL_TICKET_SUCCESS
  1363. && ret != SSL_TICKET_SUCCESS_RENEW)
  1364. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1365. else if (retcb == SSL_TICKET_RETURN_USE)
  1366. ret = SSL_TICKET_SUCCESS;
  1367. else
  1368. ret = SSL_TICKET_SUCCESS_RENEW;
  1369. break;
  1370. default:
  1371. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1372. }
  1373. }
  1374. if (s->ext.session_secret_cb == NULL || SSL_IS_TLS13(s)) {
  1375. switch (ret) {
  1376. case SSL_TICKET_NO_DECRYPT:
  1377. case SSL_TICKET_SUCCESS_RENEW:
  1378. case SSL_TICKET_EMPTY:
  1379. s->ext.ticket_expected = 1;
  1380. }
  1381. }
  1382. *psess = sess;
  1383. return ret;
  1384. }
  1385. /* Check to see if a signature algorithm is allowed */
  1386. static int tls12_sigalg_allowed(SSL *s, int op, const SIGALG_LOOKUP *lu)
  1387. {
  1388. unsigned char sigalgstr[2];
  1389. int secbits;
  1390. /* See if sigalgs is recognised and if hash is enabled */
  1391. if (!tls1_lookup_md(lu, NULL))
  1392. return 0;
  1393. /* DSA is not allowed in TLS 1.3 */
  1394. if (SSL_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA)
  1395. return 0;
  1396. /* TODO(OpenSSL1.2) fully axe DSA/etc. in ClientHello per TLS 1.3 spec */
  1397. if (!s->server && !SSL_IS_DTLS(s) && s->s3->tmp.min_ver >= TLS1_3_VERSION
  1398. && (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX
  1399. || lu->hash_idx == SSL_MD_MD5_IDX
  1400. || lu->hash_idx == SSL_MD_SHA224_IDX))
  1401. return 0;
  1402. /* See if public key algorithm allowed */
  1403. if (ssl_cert_is_disabled(lu->sig_idx))
  1404. return 0;
  1405. if (lu->sig == NID_id_GostR3410_2012_256
  1406. || lu->sig == NID_id_GostR3410_2012_512
  1407. || lu->sig == NID_id_GostR3410_2001) {
  1408. /* We never allow GOST sig algs on the server with TLSv1.3 */
  1409. if (s->server && SSL_IS_TLS13(s))
  1410. return 0;
  1411. if (!s->server
  1412. && s->method->version == TLS_ANY_VERSION
  1413. && s->s3->tmp.max_ver >= TLS1_3_VERSION) {
  1414. int i, num;
  1415. STACK_OF(SSL_CIPHER) *sk;
  1416. /*
  1417. * We're a client that could negotiate TLSv1.3. We only allow GOST
  1418. * sig algs if we could negotiate TLSv1.2 or below and we have GOST
  1419. * ciphersuites enabled.
  1420. */
  1421. if (s->s3->tmp.min_ver >= TLS1_3_VERSION)
  1422. return 0;
  1423. sk = SSL_get_ciphers(s);
  1424. num = sk != NULL ? sk_SSL_CIPHER_num(sk) : 0;
  1425. for (i = 0; i < num; i++) {
  1426. const SSL_CIPHER *c;
  1427. c = sk_SSL_CIPHER_value(sk, i);
  1428. /* Skip disabled ciphers */
  1429. if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))
  1430. continue;
  1431. if ((c->algorithm_mkey & SSL_kGOST) != 0)
  1432. break;
  1433. }
  1434. if (i == num)
  1435. return 0;
  1436. }
  1437. }
  1438. if (lu->hash == NID_undef)
  1439. return 1;
  1440. /* Security bits: half digest bits */
  1441. secbits = EVP_MD_size(ssl_md(lu->hash_idx)) * 4;
  1442. /* Finally see if security callback allows it */
  1443. sigalgstr[0] = (lu->sigalg >> 8) & 0xff;
  1444. sigalgstr[1] = lu->sigalg & 0xff;
  1445. return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr);
  1446. }
  1447. /*
  1448. * Get a mask of disabled public key algorithms based on supported signature
  1449. * algorithms. For example if no signature algorithm supports RSA then RSA is
  1450. * disabled.
  1451. */
  1452. void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
  1453. {
  1454. const uint16_t *sigalgs;
  1455. size_t i, sigalgslen;
  1456. uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA;
  1457. /*
  1458. * Go through all signature algorithms seeing if we support any
  1459. * in disabled_mask.
  1460. */
  1461. sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs);
  1462. for (i = 0; i < sigalgslen; i++, sigalgs++) {
  1463. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*sigalgs);
  1464. const SSL_CERT_LOOKUP *clu;
  1465. if (lu == NULL)
  1466. continue;
  1467. clu = ssl_cert_lookup_by_idx(lu->sig_idx);
  1468. if (clu == NULL)
  1469. continue;
  1470. /* If algorithm is disabled see if we can enable it */
  1471. if ((clu->amask & disabled_mask) != 0
  1472. && tls12_sigalg_allowed(s, op, lu))
  1473. disabled_mask &= ~clu->amask;
  1474. }
  1475. *pmask_a |= disabled_mask;
  1476. }
  1477. int tls12_copy_sigalgs(SSL *s, WPACKET *pkt,
  1478. const uint16_t *psig, size_t psiglen)
  1479. {
  1480. size_t i;
  1481. int rv = 0;
  1482. for (i = 0; i < psiglen; i++, psig++) {
  1483. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*psig);
  1484. if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu))
  1485. continue;
  1486. if (!WPACKET_put_bytes_u16(pkt, *psig))
  1487. return 0;
  1488. /*
  1489. * If TLS 1.3 must have at least one valid TLS 1.3 message
  1490. * signing algorithm: i.e. neither RSA nor SHA1/SHA224
  1491. */
  1492. if (rv == 0 && (!SSL_IS_TLS13(s)
  1493. || (lu->sig != EVP_PKEY_RSA
  1494. && lu->hash != NID_sha1
  1495. && lu->hash != NID_sha224)))
  1496. rv = 1;
  1497. }
  1498. if (rv == 0)
  1499. SSLerr(SSL_F_TLS12_COPY_SIGALGS, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  1500. return rv;
  1501. }
  1502. /* Given preference and allowed sigalgs set shared sigalgs */
  1503. static size_t tls12_shared_sigalgs(SSL *s, const SIGALG_LOOKUP **shsig,
  1504. const uint16_t *pref, size_t preflen,
  1505. const uint16_t *allow, size_t allowlen)
  1506. {
  1507. const uint16_t *ptmp, *atmp;
  1508. size_t i, j, nmatch = 0;
  1509. for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) {
  1510. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*ptmp);
  1511. /* Skip disabled hashes or signature algorithms */
  1512. if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu))
  1513. continue;
  1514. for (j = 0, atmp = allow; j < allowlen; j++, atmp++) {
  1515. if (*ptmp == *atmp) {
  1516. nmatch++;
  1517. if (shsig)
  1518. *shsig++ = lu;
  1519. break;
  1520. }
  1521. }
  1522. }
  1523. return nmatch;
  1524. }
  1525. /* Set shared signature algorithms for SSL structures */
  1526. static int tls1_set_shared_sigalgs(SSL *s)
  1527. {
  1528. const uint16_t *pref, *allow, *conf;
  1529. size_t preflen, allowlen, conflen;
  1530. size_t nmatch;
  1531. const SIGALG_LOOKUP **salgs = NULL;
  1532. CERT *c = s->cert;
  1533. unsigned int is_suiteb = tls1_suiteb(s);
  1534. OPENSSL_free(c->shared_sigalgs);
  1535. c->shared_sigalgs = NULL;
  1536. c->shared_sigalgslen = 0;
  1537. /* If client use client signature algorithms if not NULL */
  1538. if (!s->server && c->client_sigalgs && !is_suiteb) {
  1539. conf = c->client_sigalgs;
  1540. conflen = c->client_sigalgslen;
  1541. } else if (c->conf_sigalgs && !is_suiteb) {
  1542. conf = c->conf_sigalgs;
  1543. conflen = c->conf_sigalgslen;
  1544. } else
  1545. conflen = tls12_get_psigalgs(s, 0, &conf);
  1546. if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
  1547. pref = conf;
  1548. preflen = conflen;
  1549. allow = s->s3->tmp.peer_sigalgs;
  1550. allowlen = s->s3->tmp.peer_sigalgslen;
  1551. } else {
  1552. allow = conf;
  1553. allowlen = conflen;
  1554. pref = s->s3->tmp.peer_sigalgs;
  1555. preflen = s->s3->tmp.peer_sigalgslen;
  1556. }
  1557. nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
  1558. if (nmatch) {
  1559. if ((salgs = OPENSSL_malloc(nmatch * sizeof(*salgs))) == NULL) {
  1560. SSLerr(SSL_F_TLS1_SET_SHARED_SIGALGS, ERR_R_MALLOC_FAILURE);
  1561. return 0;
  1562. }
  1563. nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
  1564. } else {
  1565. salgs = NULL;
  1566. }
  1567. c->shared_sigalgs = salgs;
  1568. c->shared_sigalgslen = nmatch;
  1569. return 1;
  1570. }
  1571. int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen)
  1572. {
  1573. unsigned int stmp;
  1574. size_t size, i;
  1575. uint16_t *buf;
  1576. size = PACKET_remaining(pkt);
  1577. /* Invalid data length */
  1578. if (size == 0 || (size & 1) != 0)
  1579. return 0;
  1580. size >>= 1;
  1581. if ((buf = OPENSSL_malloc(size * sizeof(*buf))) == NULL) {
  1582. SSLerr(SSL_F_TLS1_SAVE_U16, ERR_R_MALLOC_FAILURE);
  1583. return 0;
  1584. }
  1585. for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++)
  1586. buf[i] = stmp;
  1587. if (i != size) {
  1588. OPENSSL_free(buf);
  1589. return 0;
  1590. }
  1591. OPENSSL_free(*pdest);
  1592. *pdest = buf;
  1593. *pdestlen = size;
  1594. return 1;
  1595. }
  1596. int tls1_save_sigalgs(SSL *s, PACKET *pkt, int cert)
  1597. {
  1598. /* Extension ignored for inappropriate versions */
  1599. if (!SSL_USE_SIGALGS(s))
  1600. return 1;
  1601. /* Should never happen */
  1602. if (s->cert == NULL)
  1603. return 0;
  1604. if (cert)
  1605. return tls1_save_u16(pkt, &s->s3->tmp.peer_cert_sigalgs,
  1606. &s->s3->tmp.peer_cert_sigalgslen);
  1607. else
  1608. return tls1_save_u16(pkt, &s->s3->tmp.peer_sigalgs,
  1609. &s->s3->tmp.peer_sigalgslen);
  1610. }
  1611. /* Set preferred digest for each key type */
  1612. int tls1_process_sigalgs(SSL *s)
  1613. {
  1614. size_t i;
  1615. uint32_t *pvalid = s->s3->tmp.valid_flags;
  1616. CERT *c = s->cert;
  1617. if (!tls1_set_shared_sigalgs(s))
  1618. return 0;
  1619. for (i = 0; i < SSL_PKEY_NUM; i++)
  1620. pvalid[i] = 0;
  1621. for (i = 0; i < c->shared_sigalgslen; i++) {
  1622. const SIGALG_LOOKUP *sigptr = c->shared_sigalgs[i];
  1623. int idx = sigptr->sig_idx;
  1624. /* Ignore PKCS1 based sig algs in TLSv1.3 */
  1625. if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA)
  1626. continue;
  1627. /* If not disabled indicate we can explicitly sign */
  1628. if (pvalid[idx] == 0 && !ssl_cert_is_disabled(idx))
  1629. pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
  1630. }
  1631. return 1;
  1632. }
  1633. int SSL_get_sigalgs(SSL *s, int idx,
  1634. int *psign, int *phash, int *psignhash,
  1635. unsigned char *rsig, unsigned char *rhash)
  1636. {
  1637. uint16_t *psig = s->s3->tmp.peer_sigalgs;
  1638. size_t numsigalgs = s->s3->tmp.peer_sigalgslen;
  1639. if (psig == NULL || numsigalgs > INT_MAX)
  1640. return 0;
  1641. if (idx >= 0) {
  1642. const SIGALG_LOOKUP *lu;
  1643. if (idx >= (int)numsigalgs)
  1644. return 0;
  1645. psig += idx;
  1646. if (rhash != NULL)
  1647. *rhash = (unsigned char)((*psig >> 8) & 0xff);
  1648. if (rsig != NULL)
  1649. *rsig = (unsigned char)(*psig & 0xff);
  1650. lu = tls1_lookup_sigalg(*psig);
  1651. if (psign != NULL)
  1652. *psign = lu != NULL ? lu->sig : NID_undef;
  1653. if (phash != NULL)
  1654. *phash = lu != NULL ? lu->hash : NID_undef;
  1655. if (psignhash != NULL)
  1656. *psignhash = lu != NULL ? lu->sigandhash : NID_undef;
  1657. }
  1658. return (int)numsigalgs;
  1659. }
  1660. int SSL_get_shared_sigalgs(SSL *s, int idx,
  1661. int *psign, int *phash, int *psignhash,
  1662. unsigned char *rsig, unsigned char *rhash)
  1663. {
  1664. const SIGALG_LOOKUP *shsigalgs;
  1665. if (s->cert->shared_sigalgs == NULL
  1666. || idx < 0
  1667. || idx >= (int)s->cert->shared_sigalgslen
  1668. || s->cert->shared_sigalgslen > INT_MAX)
  1669. return 0;
  1670. shsigalgs = s->cert->shared_sigalgs[idx];
  1671. if (phash != NULL)
  1672. *phash = shsigalgs->hash;
  1673. if (psign != NULL)
  1674. *psign = shsigalgs->sig;
  1675. if (psignhash != NULL)
  1676. *psignhash = shsigalgs->sigandhash;
  1677. if (rsig != NULL)
  1678. *rsig = (unsigned char)(shsigalgs->sigalg & 0xff);
  1679. if (rhash != NULL)
  1680. *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff);
  1681. return (int)s->cert->shared_sigalgslen;
  1682. }
  1683. /* Maximum possible number of unique entries in sigalgs array */
  1684. #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2)
  1685. typedef struct {
  1686. size_t sigalgcnt;
  1687. /* TLSEXT_SIGALG_XXX values */
  1688. uint16_t sigalgs[TLS_MAX_SIGALGCNT];
  1689. } sig_cb_st;
  1690. static void get_sigorhash(int *psig, int *phash, const char *str)
  1691. {
  1692. if (strcmp(str, "RSA") == 0) {
  1693. *psig = EVP_PKEY_RSA;
  1694. } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) {
  1695. *psig = EVP_PKEY_RSA_PSS;
  1696. } else if (strcmp(str, "DSA") == 0) {
  1697. *psig = EVP_PKEY_DSA;
  1698. } else if (strcmp(str, "ECDSA") == 0) {
  1699. *psig = EVP_PKEY_EC;
  1700. } else {
  1701. *phash = OBJ_sn2nid(str);
  1702. if (*phash == NID_undef)
  1703. *phash = OBJ_ln2nid(str);
  1704. }
  1705. }
  1706. /* Maximum length of a signature algorithm string component */
  1707. #define TLS_MAX_SIGSTRING_LEN 40
  1708. static int sig_cb(const char *elem, int len, void *arg)
  1709. {
  1710. sig_cb_st *sarg = arg;
  1711. size_t i;
  1712. const SIGALG_LOOKUP *s;
  1713. char etmp[TLS_MAX_SIGSTRING_LEN], *p;
  1714. int sig_alg = NID_undef, hash_alg = NID_undef;
  1715. if (elem == NULL)
  1716. return 0;
  1717. if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT)
  1718. return 0;
  1719. if (len > (int)(sizeof(etmp) - 1))
  1720. return 0;
  1721. memcpy(etmp, elem, len);
  1722. etmp[len] = 0;
  1723. p = strchr(etmp, '+');
  1724. /*
  1725. * We only allow SignatureSchemes listed in the sigalg_lookup_tbl;
  1726. * if there's no '+' in the provided name, look for the new-style combined
  1727. * name. If not, match both sig+hash to find the needed SIGALG_LOOKUP.
  1728. * Just sig+hash is not unique since TLS 1.3 adds rsa_pss_pss_* and
  1729. * rsa_pss_rsae_* that differ only by public key OID; in such cases
  1730. * we will pick the _rsae_ variant, by virtue of them appearing earlier
  1731. * in the table.
  1732. */
  1733. if (p == NULL) {
  1734. for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
  1735. i++, s++) {
  1736. if (s->name != NULL && strcmp(etmp, s->name) == 0) {
  1737. sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;
  1738. break;
  1739. }
  1740. }
  1741. if (i == OSSL_NELEM(sigalg_lookup_tbl))
  1742. return 0;
  1743. } else {
  1744. *p = 0;
  1745. p++;
  1746. if (*p == 0)
  1747. return 0;
  1748. get_sigorhash(&sig_alg, &hash_alg, etmp);
  1749. get_sigorhash(&sig_alg, &hash_alg, p);
  1750. if (sig_alg == NID_undef || hash_alg == NID_undef)
  1751. return 0;
  1752. for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
  1753. i++, s++) {
  1754. if (s->hash == hash_alg && s->sig == sig_alg) {
  1755. sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;
  1756. break;
  1757. }
  1758. }
  1759. if (i == OSSL_NELEM(sigalg_lookup_tbl))
  1760. return 0;
  1761. }
  1762. /* Reject duplicates */
  1763. for (i = 0; i < sarg->sigalgcnt - 1; i++) {
  1764. if (sarg->sigalgs[i] == sarg->sigalgs[sarg->sigalgcnt - 1]) {
  1765. sarg->sigalgcnt--;
  1766. return 0;
  1767. }
  1768. }
  1769. return 1;
  1770. }
  1771. /*
  1772. * Set supported signature algorithms based on a colon separated list of the
  1773. * form sig+hash e.g. RSA+SHA512:DSA+SHA512
  1774. */
  1775. int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
  1776. {
  1777. sig_cb_st sig;
  1778. sig.sigalgcnt = 0;
  1779. if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
  1780. return 0;
  1781. if (c == NULL)
  1782. return 1;
  1783. return tls1_set_raw_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
  1784. }
  1785. int tls1_set_raw_sigalgs(CERT *c, const uint16_t *psigs, size_t salglen,
  1786. int client)
  1787. {
  1788. uint16_t *sigalgs;
  1789. if ((sigalgs = OPENSSL_malloc(salglen * sizeof(*sigalgs))) == NULL) {
  1790. SSLerr(SSL_F_TLS1_SET_RAW_SIGALGS, ERR_R_MALLOC_FAILURE);
  1791. return 0;
  1792. }
  1793. memcpy(sigalgs, psigs, salglen * sizeof(*sigalgs));
  1794. if (client) {
  1795. OPENSSL_free(c->client_sigalgs);
  1796. c->client_sigalgs = sigalgs;
  1797. c->client_sigalgslen = salglen;
  1798. } else {
  1799. OPENSSL_free(c->conf_sigalgs);
  1800. c->conf_sigalgs = sigalgs;
  1801. c->conf_sigalgslen = salglen;
  1802. }
  1803. return 1;
  1804. }
  1805. int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
  1806. {
  1807. uint16_t *sigalgs, *sptr;
  1808. size_t i;
  1809. if (salglen & 1)
  1810. return 0;
  1811. if ((sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs))) == NULL) {
  1812. SSLerr(SSL_F_TLS1_SET_SIGALGS, ERR_R_MALLOC_FAILURE);
  1813. return 0;
  1814. }
  1815. for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
  1816. size_t j;
  1817. const SIGALG_LOOKUP *curr;
  1818. int md_id = *psig_nids++;
  1819. int sig_id = *psig_nids++;
  1820. for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl);
  1821. j++, curr++) {
  1822. if (curr->hash == md_id && curr->sig == sig_id) {
  1823. *sptr++ = curr->sigalg;
  1824. break;
  1825. }
  1826. }
  1827. if (j == OSSL_NELEM(sigalg_lookup_tbl))
  1828. goto err;
  1829. }
  1830. if (client) {
  1831. OPENSSL_free(c->client_sigalgs);
  1832. c->client_sigalgs = sigalgs;
  1833. c->client_sigalgslen = salglen / 2;
  1834. } else {
  1835. OPENSSL_free(c->conf_sigalgs);
  1836. c->conf_sigalgs = sigalgs;
  1837. c->conf_sigalgslen = salglen / 2;
  1838. }
  1839. return 1;
  1840. err:
  1841. OPENSSL_free(sigalgs);
  1842. return 0;
  1843. }
  1844. static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
  1845. {
  1846. int sig_nid;
  1847. size_t i;
  1848. if (default_nid == -1)
  1849. return 1;
  1850. sig_nid = X509_get_signature_nid(x);
  1851. if (default_nid)
  1852. return sig_nid == default_nid ? 1 : 0;
  1853. for (i = 0; i < c->shared_sigalgslen; i++)
  1854. if (sig_nid == c->shared_sigalgs[i]->sigandhash)
  1855. return 1;
  1856. return 0;
  1857. }
  1858. /* Check to see if a certificate issuer name matches list of CA names */
  1859. static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
  1860. {
  1861. X509_NAME *nm;
  1862. int i;
  1863. nm = X509_get_issuer_name(x);
  1864. for (i = 0; i < sk_X509_NAME_num(names); i++) {
  1865. if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
  1866. return 1;
  1867. }
  1868. return 0;
  1869. }
  1870. /*
  1871. * Check certificate chain is consistent with TLS extensions and is usable by
  1872. * server. This servers two purposes: it allows users to check chains before
  1873. * passing them to the server and it allows the server to check chains before
  1874. * attempting to use them.
  1875. */
  1876. /* Flags which need to be set for a certificate when strict mode not set */
  1877. #define CERT_PKEY_VALID_FLAGS \
  1878. (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
  1879. /* Strict mode flags */
  1880. #define CERT_PKEY_STRICT_FLAGS \
  1881. (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
  1882. | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
  1883. int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
  1884. int idx)
  1885. {
  1886. int i;
  1887. int rv = 0;
  1888. int check_flags = 0, strict_mode;
  1889. CERT_PKEY *cpk = NULL;
  1890. CERT *c = s->cert;
  1891. uint32_t *pvalid;
  1892. unsigned int suiteb_flags = tls1_suiteb(s);
  1893. /* idx == -1 means checking server chains */
  1894. if (idx != -1) {
  1895. /* idx == -2 means checking client certificate chains */
  1896. if (idx == -2) {
  1897. cpk = c->key;
  1898. idx = (int)(cpk - c->pkeys);
  1899. } else
  1900. cpk = c->pkeys + idx;
  1901. pvalid = s->s3->tmp.valid_flags + idx;
  1902. x = cpk->x509;
  1903. pk = cpk->privatekey;
  1904. chain = cpk->chain;
  1905. strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
  1906. /* If no cert or key, forget it */
  1907. if (!x || !pk)
  1908. goto end;
  1909. } else {
  1910. size_t certidx;
  1911. if (!x || !pk)
  1912. return 0;
  1913. if (ssl_cert_lookup_by_pkey(pk, &certidx) == NULL)
  1914. return 0;
  1915. idx = certidx;
  1916. pvalid = s->s3->tmp.valid_flags + idx;
  1917. if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
  1918. check_flags = CERT_PKEY_STRICT_FLAGS;
  1919. else
  1920. check_flags = CERT_PKEY_VALID_FLAGS;
  1921. strict_mode = 1;
  1922. }
  1923. if (suiteb_flags) {
  1924. int ok;
  1925. if (check_flags)
  1926. check_flags |= CERT_PKEY_SUITEB;
  1927. ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
  1928. if (ok == X509_V_OK)
  1929. rv |= CERT_PKEY_SUITEB;
  1930. else if (!check_flags)
  1931. goto end;
  1932. }
  1933. /*
  1934. * Check all signature algorithms are consistent with signature
  1935. * algorithms extension if TLS 1.2 or later and strict mode.
  1936. */
  1937. if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
  1938. int default_nid;
  1939. int rsign = 0;
  1940. if (s->s3->tmp.peer_cert_sigalgs != NULL
  1941. || s->s3->tmp.peer_sigalgs != NULL) {
  1942. default_nid = 0;
  1943. /* If no sigalgs extension use defaults from RFC5246 */
  1944. } else {
  1945. switch (idx) {
  1946. case SSL_PKEY_RSA:
  1947. rsign = EVP_PKEY_RSA;
  1948. default_nid = NID_sha1WithRSAEncryption;
  1949. break;
  1950. case SSL_PKEY_DSA_SIGN:
  1951. rsign = EVP_PKEY_DSA;
  1952. default_nid = NID_dsaWithSHA1;
  1953. break;
  1954. case SSL_PKEY_ECC:
  1955. rsign = EVP_PKEY_EC;
  1956. default_nid = NID_ecdsa_with_SHA1;
  1957. break;
  1958. case SSL_PKEY_GOST01:
  1959. rsign = NID_id_GostR3410_2001;
  1960. default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
  1961. break;
  1962. case SSL_PKEY_GOST12_256:
  1963. rsign = NID_id_GostR3410_2012_256;
  1964. default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
  1965. break;
  1966. case SSL_PKEY_GOST12_512:
  1967. rsign = NID_id_GostR3410_2012_512;
  1968. default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
  1969. break;
  1970. default:
  1971. default_nid = -1;
  1972. break;
  1973. }
  1974. }
  1975. /*
  1976. * If peer sent no signature algorithms extension and we have set
  1977. * preferred signature algorithms check we support sha1.
  1978. */
  1979. if (default_nid > 0 && c->conf_sigalgs) {
  1980. size_t j;
  1981. const uint16_t *p = c->conf_sigalgs;
  1982. for (j = 0; j < c->conf_sigalgslen; j++, p++) {
  1983. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*p);
  1984. if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign)
  1985. break;
  1986. }
  1987. if (j == c->conf_sigalgslen) {
  1988. if (check_flags)
  1989. goto skip_sigs;
  1990. else
  1991. goto end;
  1992. }
  1993. }
  1994. /* Check signature algorithm of each cert in chain */
  1995. if (!tls1_check_sig_alg(c, x, default_nid)) {
  1996. if (!check_flags)
  1997. goto end;
  1998. } else
  1999. rv |= CERT_PKEY_EE_SIGNATURE;
  2000. rv |= CERT_PKEY_CA_SIGNATURE;
  2001. for (i = 0; i < sk_X509_num(chain); i++) {
  2002. if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
  2003. if (check_flags) {
  2004. rv &= ~CERT_PKEY_CA_SIGNATURE;
  2005. break;
  2006. } else
  2007. goto end;
  2008. }
  2009. }
  2010. }
  2011. /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
  2012. else if (check_flags)
  2013. rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
  2014. skip_sigs:
  2015. /* Check cert parameters are consistent */
  2016. if (tls1_check_cert_param(s, x, 1))
  2017. rv |= CERT_PKEY_EE_PARAM;
  2018. else if (!check_flags)
  2019. goto end;
  2020. if (!s->server)
  2021. rv |= CERT_PKEY_CA_PARAM;
  2022. /* In strict mode check rest of chain too */
  2023. else if (strict_mode) {
  2024. rv |= CERT_PKEY_CA_PARAM;
  2025. for (i = 0; i < sk_X509_num(chain); i++) {
  2026. X509 *ca = sk_X509_value(chain, i);
  2027. if (!tls1_check_cert_param(s, ca, 0)) {
  2028. if (check_flags) {
  2029. rv &= ~CERT_PKEY_CA_PARAM;
  2030. break;
  2031. } else
  2032. goto end;
  2033. }
  2034. }
  2035. }
  2036. if (!s->server && strict_mode) {
  2037. STACK_OF(X509_NAME) *ca_dn;
  2038. int check_type = 0;
  2039. switch (EVP_PKEY_id(pk)) {
  2040. case EVP_PKEY_RSA:
  2041. check_type = TLS_CT_RSA_SIGN;
  2042. break;
  2043. case EVP_PKEY_DSA:
  2044. check_type = TLS_CT_DSS_SIGN;
  2045. break;
  2046. case EVP_PKEY_EC:
  2047. check_type = TLS_CT_ECDSA_SIGN;
  2048. break;
  2049. }
  2050. if (check_type) {
  2051. const uint8_t *ctypes = s->s3->tmp.ctype;
  2052. size_t j;
  2053. for (j = 0; j < s->s3->tmp.ctype_len; j++, ctypes++) {
  2054. if (*ctypes == check_type) {
  2055. rv |= CERT_PKEY_CERT_TYPE;
  2056. break;
  2057. }
  2058. }
  2059. if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
  2060. goto end;
  2061. } else {
  2062. rv |= CERT_PKEY_CERT_TYPE;
  2063. }
  2064. ca_dn = s->s3->tmp.peer_ca_names;
  2065. if (!sk_X509_NAME_num(ca_dn))
  2066. rv |= CERT_PKEY_ISSUER_NAME;
  2067. if (!(rv & CERT_PKEY_ISSUER_NAME)) {
  2068. if (ssl_check_ca_name(ca_dn, x))
  2069. rv |= CERT_PKEY_ISSUER_NAME;
  2070. }
  2071. if (!(rv & CERT_PKEY_ISSUER_NAME)) {
  2072. for (i = 0; i < sk_X509_num(chain); i++) {
  2073. X509 *xtmp = sk_X509_value(chain, i);
  2074. if (ssl_check_ca_name(ca_dn, xtmp)) {
  2075. rv |= CERT_PKEY_ISSUER_NAME;
  2076. break;
  2077. }
  2078. }
  2079. }
  2080. if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
  2081. goto end;
  2082. } else
  2083. rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
  2084. if (!check_flags || (rv & check_flags) == check_flags)
  2085. rv |= CERT_PKEY_VALID;
  2086. end:
  2087. if (TLS1_get_version(s) >= TLS1_2_VERSION)
  2088. rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN);
  2089. else
  2090. rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
  2091. /*
  2092. * When checking a CERT_PKEY structure all flags are irrelevant if the
  2093. * chain is invalid.
  2094. */
  2095. if (!check_flags) {
  2096. if (rv & CERT_PKEY_VALID) {
  2097. *pvalid = rv;
  2098. } else {
  2099. /* Preserve sign and explicit sign flag, clear rest */
  2100. *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
  2101. return 0;
  2102. }
  2103. }
  2104. return rv;
  2105. }
  2106. /* Set validity of certificates in an SSL structure */
  2107. void tls1_set_cert_validity(SSL *s)
  2108. {
  2109. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA);
  2110. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN);
  2111. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
  2112. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
  2113. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
  2114. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
  2115. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
  2116. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519);
  2117. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED448);
  2118. }
  2119. /* User level utility function to check a chain is suitable */
  2120. int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
  2121. {
  2122. return tls1_check_chain(s, x, pk, chain, -1);
  2123. }
  2124. #ifndef OPENSSL_NO_DH
  2125. DH *ssl_get_auto_dh(SSL *s)
  2126. {
  2127. int dh_secbits = 80;
  2128. if (s->cert->dh_tmp_auto == 2)
  2129. return DH_get_1024_160();
  2130. if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
  2131. if (s->s3->tmp.new_cipher->strength_bits == 256)
  2132. dh_secbits = 128;
  2133. else
  2134. dh_secbits = 80;
  2135. } else {
  2136. if (s->s3->tmp.cert == NULL)
  2137. return NULL;
  2138. dh_secbits = EVP_PKEY_security_bits(s->s3->tmp.cert->privatekey);
  2139. }
  2140. if (dh_secbits >= 128) {
  2141. DH *dhp = DH_new();
  2142. BIGNUM *p, *g;
  2143. if (dhp == NULL)
  2144. return NULL;
  2145. g = BN_new();
  2146. if (g == NULL || !BN_set_word(g, 2)) {
  2147. DH_free(dhp);
  2148. BN_free(g);
  2149. return NULL;
  2150. }
  2151. if (dh_secbits >= 192)
  2152. p = BN_get_rfc3526_prime_8192(NULL);
  2153. else
  2154. p = BN_get_rfc3526_prime_3072(NULL);
  2155. if (p == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
  2156. DH_free(dhp);
  2157. BN_free(p);
  2158. BN_free(g);
  2159. return NULL;
  2160. }
  2161. return dhp;
  2162. }
  2163. if (dh_secbits >= 112)
  2164. return DH_get_2048_224();
  2165. return DH_get_1024_160();
  2166. }
  2167. #endif
  2168. static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
  2169. {
  2170. int secbits = -1;
  2171. EVP_PKEY *pkey = X509_get0_pubkey(x);
  2172. if (pkey) {
  2173. /*
  2174. * If no parameters this will return -1 and fail using the default
  2175. * security callback for any non-zero security level. This will
  2176. * reject keys which omit parameters but this only affects DSA and
  2177. * omission of parameters is never (?) done in practice.
  2178. */
  2179. secbits = EVP_PKEY_security_bits(pkey);
  2180. }
  2181. if (s)
  2182. return ssl_security(s, op, secbits, 0, x);
  2183. else
  2184. return ssl_ctx_security(ctx, op, secbits, 0, x);
  2185. }
  2186. static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
  2187. {
  2188. /* Lookup signature algorithm digest */
  2189. int secbits, nid, pknid;
  2190. /* Don't check signature if self signed */
  2191. if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
  2192. return 1;
  2193. if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL))
  2194. secbits = -1;
  2195. /* If digest NID not defined use signature NID */
  2196. if (nid == NID_undef)
  2197. nid = pknid;
  2198. if (s)
  2199. return ssl_security(s, op, secbits, nid, x);
  2200. else
  2201. return ssl_ctx_security(ctx, op, secbits, nid, x);
  2202. }
  2203. int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
  2204. {
  2205. if (vfy)
  2206. vfy = SSL_SECOP_PEER;
  2207. if (is_ee) {
  2208. if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
  2209. return SSL_R_EE_KEY_TOO_SMALL;
  2210. } else {
  2211. if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
  2212. return SSL_R_CA_KEY_TOO_SMALL;
  2213. }
  2214. if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
  2215. return SSL_R_CA_MD_TOO_WEAK;
  2216. return 1;
  2217. }
  2218. /*
  2219. * Check security of a chain, if |sk| includes the end entity certificate then
  2220. * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending
  2221. * one to the peer. Return values: 1 if ok otherwise error code to use
  2222. */
  2223. int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
  2224. {
  2225. int rv, start_idx, i;
  2226. if (x == NULL) {
  2227. x = sk_X509_value(sk, 0);
  2228. start_idx = 1;
  2229. } else
  2230. start_idx = 0;
  2231. rv = ssl_security_cert(s, NULL, x, vfy, 1);
  2232. if (rv != 1)
  2233. return rv;
  2234. for (i = start_idx; i < sk_X509_num(sk); i++) {
  2235. x = sk_X509_value(sk, i);
  2236. rv = ssl_security_cert(s, NULL, x, vfy, 0);
  2237. if (rv != 1)
  2238. return rv;
  2239. }
  2240. return 1;
  2241. }
  2242. /*
  2243. * For TLS 1.2 servers check if we have a certificate which can be used
  2244. * with the signature algorithm "lu" and return index of certificate.
  2245. */
  2246. static int tls12_get_cert_sigalg_idx(const SSL *s, const SIGALG_LOOKUP *lu)
  2247. {
  2248. int sig_idx = lu->sig_idx;
  2249. const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx);
  2250. /* If not recognised or not supported by cipher mask it is not suitable */
  2251. if (clu == NULL
  2252. || (clu->amask & s->s3->tmp.new_cipher->algorithm_auth) == 0
  2253. || (clu->nid == EVP_PKEY_RSA_PSS
  2254. && (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kRSA) != 0))
  2255. return -1;
  2256. return s->s3->tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1;
  2257. }
  2258. /*
  2259. * Returns true if |s| has a usable certificate configured for use
  2260. * with signature scheme |sig|.
  2261. * "Usable" includes a check for presence as well as applying
  2262. * the signature_algorithm_cert restrictions sent by the peer (if any).
  2263. * Returns false if no usable certificate is found.
  2264. */
  2265. static int has_usable_cert(SSL *s, const SIGALG_LOOKUP *sig, int idx)
  2266. {
  2267. const SIGALG_LOOKUP *lu;
  2268. int mdnid, pknid;
  2269. size_t i;
  2270. /* TLS 1.2 callers can override lu->sig_idx, but not TLS 1.3 callers. */
  2271. if (idx == -1)
  2272. idx = sig->sig_idx;
  2273. if (!ssl_has_cert(s, idx))
  2274. return 0;
  2275. if (s->s3->tmp.peer_cert_sigalgs != NULL) {
  2276. for (i = 0; i < s->s3->tmp.peer_cert_sigalgslen; i++) {
  2277. lu = tls1_lookup_sigalg(s->s3->tmp.peer_cert_sigalgs[i]);
  2278. if (lu == NULL
  2279. || !X509_get_signature_info(s->cert->pkeys[idx].x509, &mdnid,
  2280. &pknid, NULL, NULL))
  2281. continue;
  2282. /*
  2283. * TODO this does not differentiate between the
  2284. * rsa_pss_pss_* and rsa_pss_rsae_* schemes since we do not
  2285. * have a chain here that lets us look at the key OID in the
  2286. * signing certificate.
  2287. */
  2288. if (mdnid == lu->hash && pknid == lu->sig)
  2289. return 1;
  2290. }
  2291. return 0;
  2292. }
  2293. return 1;
  2294. }
  2295. /*
  2296. * Choose an appropriate signature algorithm based on available certificates
  2297. * Sets chosen certificate and signature algorithm.
  2298. *
  2299. * For servers if we fail to find a required certificate it is a fatal error,
  2300. * an appropriate error code is set and a TLS alert is sent.
  2301. *
  2302. * For clients fatalerrs is set to 0. If a certificate is not suitable it is not
  2303. * a fatal error: we will either try another certificate or not present one
  2304. * to the server. In this case no error is set.
  2305. */
  2306. int tls_choose_sigalg(SSL *s, int fatalerrs)
  2307. {
  2308. const SIGALG_LOOKUP *lu = NULL;
  2309. int sig_idx = -1;
  2310. s->s3->tmp.cert = NULL;
  2311. s->s3->tmp.sigalg = NULL;
  2312. if (SSL_IS_TLS13(s)) {
  2313. size_t i;
  2314. #ifndef OPENSSL_NO_EC
  2315. int curve = -1;
  2316. #endif
  2317. /* Look for a certificate matching shared sigalgs */
  2318. for (i = 0; i < s->cert->shared_sigalgslen; i++) {
  2319. lu = s->cert->shared_sigalgs[i];
  2320. sig_idx = -1;
  2321. /* Skip SHA1, SHA224, DSA and RSA if not PSS */
  2322. if (lu->hash == NID_sha1
  2323. || lu->hash == NID_sha224
  2324. || lu->sig == EVP_PKEY_DSA
  2325. || lu->sig == EVP_PKEY_RSA)
  2326. continue;
  2327. /* Check that we have a cert, and signature_algorithms_cert */
  2328. if (!tls1_lookup_md(lu, NULL) || !has_usable_cert(s, lu, -1))
  2329. continue;
  2330. if (lu->sig == EVP_PKEY_EC) {
  2331. #ifndef OPENSSL_NO_EC
  2332. if (curve == -1) {
  2333. EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[SSL_PKEY_ECC].privatekey);
  2334. curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
  2335. }
  2336. if (lu->curve != NID_undef && curve != lu->curve)
  2337. continue;
  2338. #else
  2339. continue;
  2340. #endif
  2341. } else if (lu->sig == EVP_PKEY_RSA_PSS) {
  2342. /* validate that key is large enough for the signature algorithm */
  2343. EVP_PKEY *pkey;
  2344. pkey = s->cert->pkeys[lu->sig_idx].privatekey;
  2345. if (!rsa_pss_check_min_key_size(EVP_PKEY_get0(pkey), lu))
  2346. continue;
  2347. }
  2348. break;
  2349. }
  2350. if (i == s->cert->shared_sigalgslen) {
  2351. if (!fatalerrs)
  2352. return 1;
  2353. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_CHOOSE_SIGALG,
  2354. SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  2355. return 0;
  2356. }
  2357. } else {
  2358. /* If ciphersuite doesn't require a cert nothing to do */
  2359. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aCERT))
  2360. return 1;
  2361. if (!s->server && !ssl_has_cert(s, s->cert->key - s->cert->pkeys))
  2362. return 1;
  2363. if (SSL_USE_SIGALGS(s)) {
  2364. size_t i;
  2365. if (s->s3->tmp.peer_sigalgs != NULL) {
  2366. #ifndef OPENSSL_NO_EC
  2367. int curve;
  2368. /* For Suite B need to match signature algorithm to curve */
  2369. if (tls1_suiteb(s)) {
  2370. EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[SSL_PKEY_ECC].privatekey);
  2371. curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
  2372. } else {
  2373. curve = -1;
  2374. }
  2375. #endif
  2376. /*
  2377. * Find highest preference signature algorithm matching
  2378. * cert type
  2379. */
  2380. for (i = 0; i < s->cert->shared_sigalgslen; i++) {
  2381. lu = s->cert->shared_sigalgs[i];
  2382. if (s->server) {
  2383. if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1)
  2384. continue;
  2385. } else {
  2386. int cc_idx = s->cert->key - s->cert->pkeys;
  2387. sig_idx = lu->sig_idx;
  2388. if (cc_idx != sig_idx)
  2389. continue;
  2390. }
  2391. /* Check that we have a cert, and sig_algs_cert */
  2392. if (!has_usable_cert(s, lu, sig_idx))
  2393. continue;
  2394. if (lu->sig == EVP_PKEY_RSA_PSS) {
  2395. /* validate that key is large enough for the signature algorithm */
  2396. EVP_PKEY *pkey = s->cert->pkeys[sig_idx].privatekey;
  2397. if (!rsa_pss_check_min_key_size(EVP_PKEY_get0(pkey), lu))
  2398. continue;
  2399. }
  2400. #ifndef OPENSSL_NO_EC
  2401. if (curve == -1 || lu->curve == curve)
  2402. #endif
  2403. break;
  2404. }
  2405. if (i == s->cert->shared_sigalgslen) {
  2406. if (!fatalerrs)
  2407. return 1;
  2408. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2409. SSL_F_TLS_CHOOSE_SIGALG,
  2410. SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  2411. return 0;
  2412. }
  2413. } else {
  2414. /*
  2415. * If we have no sigalg use defaults
  2416. */
  2417. const uint16_t *sent_sigs;
  2418. size_t sent_sigslen;
  2419. if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
  2420. if (!fatalerrs)
  2421. return 1;
  2422. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG,
  2423. ERR_R_INTERNAL_ERROR);
  2424. return 0;
  2425. }
  2426. /* Check signature matches a type we sent */
  2427. sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
  2428. for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
  2429. if (lu->sigalg == *sent_sigs
  2430. && has_usable_cert(s, lu, lu->sig_idx))
  2431. break;
  2432. }
  2433. if (i == sent_sigslen) {
  2434. if (!fatalerrs)
  2435. return 1;
  2436. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  2437. SSL_F_TLS_CHOOSE_SIGALG,
  2438. SSL_R_WRONG_SIGNATURE_TYPE);
  2439. return 0;
  2440. }
  2441. }
  2442. } else {
  2443. if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
  2444. if (!fatalerrs)
  2445. return 1;
  2446. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG,
  2447. ERR_R_INTERNAL_ERROR);
  2448. return 0;
  2449. }
  2450. }
  2451. }
  2452. if (sig_idx == -1)
  2453. sig_idx = lu->sig_idx;
  2454. s->s3->tmp.cert = &s->cert->pkeys[sig_idx];
  2455. s->cert->key = s->s3->tmp.cert;
  2456. s->s3->tmp.sigalg = lu;
  2457. return 1;
  2458. }
  2459. int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode)
  2460. {
  2461. if (mode != TLSEXT_max_fragment_length_DISABLED
  2462. && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) {
  2463. SSLerr(SSL_F_SSL_CTX_SET_TLSEXT_MAX_FRAGMENT_LENGTH,
  2464. SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  2465. return 0;
  2466. }
  2467. ctx->ext.max_fragment_len_mode = mode;
  2468. return 1;
  2469. }
  2470. int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode)
  2471. {
  2472. if (mode != TLSEXT_max_fragment_length_DISABLED
  2473. && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) {
  2474. SSLerr(SSL_F_SSL_SET_TLSEXT_MAX_FRAGMENT_LENGTH,
  2475. SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  2476. return 0;
  2477. }
  2478. ssl->ext.max_fragment_len_mode = mode;
  2479. return 1;
  2480. }
  2481. uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session)
  2482. {
  2483. return session->ext.max_fragment_len_mode;
  2484. }