t1_lib.c 77 KB

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