t1_lib.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. /* We need access to the deprecated low level HMAC APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/hmac.h>
  16. #include <openssl/core_names.h>
  17. #include <openssl/ocsp.h>
  18. #include <openssl/conf.h>
  19. #include <openssl/x509v3.h>
  20. #include <openssl/dh.h>
  21. #include <openssl/bn.h>
  22. #include <openssl/provider.h>
  23. #include "internal/nelem.h"
  24. #include "internal/evp.h"
  25. #include "internal/tlsgroups.h"
  26. #include "ssl_local.h"
  27. #include <openssl/ct.h>
  28. DEFINE_STACK_OF_CONST(SSL_CIPHER)
  29. DEFINE_STACK_OF(X509)
  30. DEFINE_STACK_OF(X509_NAME)
  31. static const SIGALG_LOOKUP *find_sig_alg(SSL *s, X509 *x, EVP_PKEY *pkey);
  32. static int tls12_sigalg_allowed(const SSL *s, int op, const SIGALG_LOOKUP *lu);
  33. SSL3_ENC_METHOD const TLSv1_enc_data = {
  34. tls1_enc,
  35. tls1_mac,
  36. tls1_setup_key_block,
  37. tls1_generate_master_secret,
  38. tls1_change_cipher_state,
  39. tls1_final_finish_mac,
  40. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  41. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  42. tls1_alert_code,
  43. tls1_export_keying_material,
  44. 0,
  45. ssl3_set_handshake_header,
  46. tls_close_construct_packet,
  47. ssl3_handshake_write
  48. };
  49. SSL3_ENC_METHOD const TLSv1_1_enc_data = {
  50. tls1_enc,
  51. tls1_mac,
  52. tls1_setup_key_block,
  53. tls1_generate_master_secret,
  54. tls1_change_cipher_state,
  55. tls1_final_finish_mac,
  56. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  57. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  58. tls1_alert_code,
  59. tls1_export_keying_material,
  60. SSL_ENC_FLAG_EXPLICIT_IV,
  61. ssl3_set_handshake_header,
  62. tls_close_construct_packet,
  63. ssl3_handshake_write
  64. };
  65. SSL3_ENC_METHOD const TLSv1_2_enc_data = {
  66. tls1_enc,
  67. tls1_mac,
  68. tls1_setup_key_block,
  69. tls1_generate_master_secret,
  70. tls1_change_cipher_state,
  71. tls1_final_finish_mac,
  72. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  73. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  74. tls1_alert_code,
  75. tls1_export_keying_material,
  76. SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
  77. | SSL_ENC_FLAG_TLS1_2_CIPHERS,
  78. ssl3_set_handshake_header,
  79. tls_close_construct_packet,
  80. ssl3_handshake_write
  81. };
  82. SSL3_ENC_METHOD const TLSv1_3_enc_data = {
  83. tls13_enc,
  84. tls1_mac,
  85. tls13_setup_key_block,
  86. tls13_generate_master_secret,
  87. tls13_change_cipher_state,
  88. tls13_final_finish_mac,
  89. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  90. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  91. tls13_alert_code,
  92. tls13_export_keying_material,
  93. SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF,
  94. ssl3_set_handshake_header,
  95. tls_close_construct_packet,
  96. ssl3_handshake_write
  97. };
  98. long tls1_default_timeout(void)
  99. {
  100. /*
  101. * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
  102. * http, the cache would over fill
  103. */
  104. return (60 * 60 * 2);
  105. }
  106. int tls1_new(SSL *s)
  107. {
  108. if (!ssl3_new(s))
  109. return 0;
  110. if (!s->method->ssl_clear(s))
  111. return 0;
  112. return 1;
  113. }
  114. void tls1_free(SSL *s)
  115. {
  116. OPENSSL_free(s->ext.session_ticket);
  117. ssl3_free(s);
  118. }
  119. int tls1_clear(SSL *s)
  120. {
  121. if (!ssl3_clear(s))
  122. return 0;
  123. if (s->method->version == TLS_ANY_VERSION)
  124. s->version = TLS_MAX_VERSION_INTERNAL;
  125. else
  126. s->version = s->method->version;
  127. return 1;
  128. }
  129. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
  130. /* Legacy NID to group_id mapping. Only works for groups we know about */
  131. static struct {
  132. int nid;
  133. uint16_t group_id;
  134. } nid_to_group[] = {
  135. {NID_sect163k1, OSSL_TLS_GROUP_ID_sect163k1},
  136. {NID_sect163r1, OSSL_TLS_GROUP_ID_sect163r1},
  137. {NID_sect163r2, OSSL_TLS_GROUP_ID_sect163r2},
  138. {NID_sect193r1, OSSL_TLS_GROUP_ID_sect193r1},
  139. {NID_sect193r2, OSSL_TLS_GROUP_ID_sect193r2},
  140. {NID_sect233k1, OSSL_TLS_GROUP_ID_sect233k1},
  141. {NID_sect233r1, OSSL_TLS_GROUP_ID_sect233r1},
  142. {NID_sect239k1, OSSL_TLS_GROUP_ID_sect239k1},
  143. {NID_sect283k1, OSSL_TLS_GROUP_ID_sect283k1},
  144. {NID_sect283r1, OSSL_TLS_GROUP_ID_sect283r1},
  145. {NID_sect409k1, OSSL_TLS_GROUP_ID_sect409k1},
  146. {NID_sect409r1, OSSL_TLS_GROUP_ID_sect409r1},
  147. {NID_sect571k1, OSSL_TLS_GROUP_ID_sect571k1},
  148. {NID_sect571r1, OSSL_TLS_GROUP_ID_sect571r1},
  149. {NID_secp160k1, OSSL_TLS_GROUP_ID_secp160k1},
  150. {NID_secp160r1, OSSL_TLS_GROUP_ID_secp160r1},
  151. {NID_secp160r2, OSSL_TLS_GROUP_ID_secp160r2},
  152. {NID_secp192k1, OSSL_TLS_GROUP_ID_secp192k1},
  153. {NID_X9_62_prime192v1, OSSL_TLS_GROUP_ID_secp192r1},
  154. {NID_secp224k1, OSSL_TLS_GROUP_ID_secp224k1},
  155. {NID_secp224r1, OSSL_TLS_GROUP_ID_secp224r1},
  156. {NID_secp256k1, OSSL_TLS_GROUP_ID_secp256k1},
  157. {NID_X9_62_prime256v1, OSSL_TLS_GROUP_ID_secp256r1},
  158. {NID_secp384r1, OSSL_TLS_GROUP_ID_secp384r1},
  159. {NID_secp521r1, OSSL_TLS_GROUP_ID_secp521r1},
  160. {NID_brainpoolP256r1, OSSL_TLS_GROUP_ID_brainpoolP256r1},
  161. {NID_brainpoolP384r1, OSSL_TLS_GROUP_ID_brainpoolP384r1},
  162. {NID_brainpoolP512r1, OSSL_TLS_GROUP_ID_brainpoolP512r1},
  163. {EVP_PKEY_X25519, OSSL_TLS_GROUP_ID_x25519},
  164. {EVP_PKEY_X448, OSSL_TLS_GROUP_ID_x448},
  165. {NID_id_tc26_gost_3410_2012_256_paramSetA, 0x0022},
  166. {NID_id_tc26_gost_3410_2012_256_paramSetB, 0x0023},
  167. {NID_id_tc26_gost_3410_2012_256_paramSetC, 0x0024},
  168. {NID_id_tc26_gost_3410_2012_256_paramSetD, 0x0025},
  169. {NID_id_tc26_gost_3410_2012_512_paramSetA, 0x0026},
  170. {NID_id_tc26_gost_3410_2012_512_paramSetB, 0x0027},
  171. {NID_id_tc26_gost_3410_2012_512_paramSetC, 0x0028},
  172. {NID_ffdhe2048, OSSL_TLS_GROUP_ID_ffdhe2048},
  173. {NID_ffdhe3072, OSSL_TLS_GROUP_ID_ffdhe3072},
  174. {NID_ffdhe4096, OSSL_TLS_GROUP_ID_ffdhe4096},
  175. {NID_ffdhe6144, OSSL_TLS_GROUP_ID_ffdhe6144},
  176. {NID_ffdhe8192, OSSL_TLS_GROUP_ID_ffdhe8192}
  177. };
  178. #endif
  179. #ifndef OPENSSL_NO_EC
  180. static const unsigned char ecformats_default[] = {
  181. TLSEXT_ECPOINTFORMAT_uncompressed,
  182. TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
  183. TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
  184. };
  185. #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */
  186. /* The default curves */
  187. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
  188. static const uint16_t supported_groups_default[] = {
  189. # ifndef OPENSSL_NO_EC
  190. 29, /* X25519 (29) */
  191. 23, /* secp256r1 (23) */
  192. 30, /* X448 (30) */
  193. 25, /* secp521r1 (25) */
  194. 24, /* secp384r1 (24) */
  195. # endif
  196. # ifndef OPENSSL_NO_GOST
  197. 34, /* GC256A (34) */
  198. 35, /* GC256B (35) */
  199. 36, /* GC256C (36) */
  200. 37, /* GC256D (37) */
  201. 38, /* GC512A (38) */
  202. 39, /* GC512B (39) */
  203. 40, /* GC512C (40) */
  204. # endif
  205. # ifndef OPENSSL_NO_DH
  206. 0x100, /* ffdhe2048 (0x100) */
  207. 0x101, /* ffdhe3072 (0x101) */
  208. 0x102, /* ffdhe4096 (0x102) */
  209. 0x103, /* ffdhe6144 (0x103) */
  210. 0x104, /* ffdhe8192 (0x104) */
  211. # endif
  212. };
  213. #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */
  214. #ifndef OPENSSL_NO_EC
  215. static const uint16_t suiteb_curves[] = {
  216. TLSEXT_curve_P_256,
  217. TLSEXT_curve_P_384
  218. };
  219. #endif
  220. struct provider_group_data_st {
  221. SSL_CTX *ctx;
  222. OSSL_PROVIDER *provider;
  223. };
  224. #define TLS_GROUP_LIST_MALLOC_BLOCK_SIZE 10
  225. static OSSL_CALLBACK add_provider_groups;
  226. static int add_provider_groups(const OSSL_PARAM params[], void *data)
  227. {
  228. struct provider_group_data_st *pgd = data;
  229. SSL_CTX *ctx = pgd->ctx;
  230. OSSL_PROVIDER *provider = pgd->provider;
  231. const OSSL_PARAM *p;
  232. TLS_GROUP_INFO *ginf = NULL;
  233. EVP_KEYMGMT *keymgmt;
  234. unsigned int gid;
  235. int ret = 0;
  236. if (ctx->group_list_max_len == ctx->group_list_len) {
  237. TLS_GROUP_INFO *tmp = NULL;
  238. if (ctx->group_list_max_len == 0)
  239. tmp = OPENSSL_malloc(sizeof(TLS_GROUP_INFO)
  240. * TLS_GROUP_LIST_MALLOC_BLOCK_SIZE);
  241. else
  242. tmp = OPENSSL_realloc(ctx->group_list,
  243. (ctx->group_list_max_len
  244. + TLS_GROUP_LIST_MALLOC_BLOCK_SIZE)
  245. * sizeof(TLS_GROUP_INFO));
  246. if (tmp == NULL) {
  247. SSLerr(0, ERR_R_MALLOC_FAILURE);
  248. return 0;
  249. }
  250. ctx->group_list = tmp;
  251. memset(tmp + ctx->group_list_max_len,
  252. 0,
  253. sizeof(TLS_GROUP_INFO) * TLS_GROUP_LIST_MALLOC_BLOCK_SIZE);
  254. ctx->group_list_max_len += TLS_GROUP_LIST_MALLOC_BLOCK_SIZE;
  255. }
  256. ginf = &ctx->group_list[ctx->group_list_len];
  257. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME);
  258. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) {
  259. SSLerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
  260. goto err;
  261. }
  262. ginf->tlsname = OPENSSL_strdup(p->data);
  263. if (ginf->tlsname == NULL) {
  264. SSLerr(0, ERR_R_MALLOC_FAILURE);
  265. goto err;
  266. }
  267. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL);
  268. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) {
  269. SSLerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
  270. goto err;
  271. }
  272. ginf->realname = OPENSSL_strdup(p->data);
  273. if (ginf->realname == NULL) {
  274. SSLerr(0, ERR_R_MALLOC_FAILURE);
  275. goto err;
  276. }
  277. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ID);
  278. if (p == NULL || !OSSL_PARAM_get_uint(p, &gid) || gid > UINT16_MAX) {
  279. SSLerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
  280. goto err;
  281. }
  282. ginf->group_id = (uint16_t)gid;
  283. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ALG);
  284. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) {
  285. SSLerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
  286. goto err;
  287. }
  288. ginf->algorithm = OPENSSL_strdup(p->data);
  289. if (ginf->algorithm == NULL) {
  290. SSLerr(0, ERR_R_MALLOC_FAILURE);
  291. goto err;
  292. }
  293. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS);
  294. if (p == NULL || !OSSL_PARAM_get_uint(p, &ginf->secbits)) {
  295. SSLerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
  296. goto err;
  297. }
  298. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_TLS);
  299. if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mintls)) {
  300. SSLerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
  301. goto err;
  302. }
  303. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_TLS);
  304. if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxtls)) {
  305. SSLerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
  306. goto err;
  307. }
  308. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS);
  309. if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mindtls)) {
  310. SSLerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
  311. goto err;
  312. }
  313. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS);
  314. if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxdtls)) {
  315. SSLerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
  316. goto err;
  317. }
  318. /*
  319. * Now check that the algorithm is actually usable for our property query
  320. * string. Regardless of the result we still return success because we have
  321. * successfully processed this group, even though we may decide not to use
  322. * it.
  323. */
  324. ret = 1;
  325. keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, ginf->algorithm, ctx->propq);
  326. if (keymgmt != NULL) {
  327. /*
  328. * We have successfully fetched the algorithm - however if the provider
  329. * doesn't match this one then we ignore it.
  330. *
  331. * Note: We're cheating a little here. Technically if the same algorithm
  332. * is available from more than one provider then it is undefined which
  333. * implementation you will get back. Theoretically this could be
  334. * different every time...we assume here that you'll always get the
  335. * same one back if you repeat the exact same fetch. Is this a reasonable
  336. * assumption to make (in which case perhaps we should document this
  337. * behaviour)?
  338. */
  339. if (EVP_KEYMGMT_provider(keymgmt) == provider) {
  340. /* We have a match - so we will use this group */
  341. ctx->group_list_len++;
  342. ginf = NULL;
  343. }
  344. EVP_KEYMGMT_free(keymgmt);
  345. }
  346. err:
  347. if (ginf != NULL) {
  348. OPENSSL_free(ginf->tlsname);
  349. OPENSSL_free(ginf->realname);
  350. OPENSSL_free(ginf->algorithm);
  351. ginf->tlsname = ginf->realname = NULL;
  352. }
  353. return ret;
  354. }
  355. static int discover_provider_groups(OSSL_PROVIDER *provider, void *vctx)
  356. {
  357. struct provider_group_data_st pgd;
  358. pgd.ctx = vctx;
  359. pgd.provider = provider;
  360. return OSSL_PROVIDER_get_capabilities(provider, "TLS-GROUP",
  361. add_provider_groups, &pgd);
  362. }
  363. int ssl_load_groups(SSL_CTX *ctx)
  364. {
  365. return OSSL_PROVIDER_do_all(ctx->libctx, discover_provider_groups, ctx);
  366. }
  367. static uint16_t tls1_group_name2id(SSL_CTX *ctx, const char *name)
  368. {
  369. size_t i;
  370. int nid = NID_undef;
  371. /* See if we can identify a nid for this name */
  372. #ifndef OPENSSL_NO_EC
  373. nid = EC_curve_nist2nid(name);
  374. #endif
  375. if (nid == NID_undef)
  376. nid = OBJ_sn2nid(name);
  377. if (nid == NID_undef)
  378. nid = OBJ_ln2nid(name);
  379. for (i = 0; i < ctx->group_list_len; i++) {
  380. if (strcmp(ctx->group_list[i].tlsname, name) == 0
  381. || (nid != NID_undef
  382. && nid == tls1_group_id2nid(ctx->group_list[i].group_id,
  383. 0)))
  384. return ctx->group_list[i].group_id;
  385. }
  386. return 0;
  387. }
  388. const TLS_GROUP_INFO *tls1_group_id_lookup(SSL_CTX *ctx, uint16_t group_id)
  389. {
  390. size_t i;
  391. for (i = 0; i < ctx->group_list_len; i++) {
  392. if (ctx->group_list[i].group_id == group_id)
  393. return &ctx->group_list[i];
  394. }
  395. return NULL;
  396. }
  397. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
  398. int tls1_group_id2nid(uint16_t group_id, int include_unknown)
  399. {
  400. size_t i;
  401. if (group_id == 0)
  402. return NID_undef;
  403. /*
  404. * Return well known Group NIDs - for backwards compatibility. This won't
  405. * work for groups we don't know about.
  406. */
  407. for (i = 0; i < OSSL_NELEM(nid_to_group); i++)
  408. {
  409. if (nid_to_group[i].group_id == group_id)
  410. return nid_to_group[i].nid;
  411. }
  412. if (!include_unknown)
  413. return NID_undef;
  414. return TLSEXT_nid_unknown | (int)group_id;
  415. }
  416. static uint16_t tls1_nid2group_id(int nid)
  417. {
  418. size_t i;
  419. /*
  420. * Return well known Group ids - for backwards compatibility. This won't
  421. * work for groups we don't know about.
  422. */
  423. for (i = 0; i < OSSL_NELEM(nid_to_group); i++)
  424. {
  425. if (nid_to_group[i].nid == nid)
  426. return nid_to_group[i].group_id;
  427. }
  428. return 0;
  429. }
  430. #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */
  431. /*
  432. * Set *pgroups to the supported groups list and *pgroupslen to
  433. * the number of groups supported.
  434. */
  435. void tls1_get_supported_groups(SSL *s, const uint16_t **pgroups,
  436. size_t *pgroupslen)
  437. {
  438. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  439. /* For Suite B mode only include P-256, P-384 */
  440. switch (tls1_suiteb(s)) {
  441. # ifndef OPENSSL_NO_EC
  442. case SSL_CERT_FLAG_SUITEB_128_LOS:
  443. *pgroups = suiteb_curves;
  444. *pgroupslen = OSSL_NELEM(suiteb_curves);
  445. break;
  446. case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
  447. *pgroups = suiteb_curves;
  448. *pgroupslen = 1;
  449. break;
  450. case SSL_CERT_FLAG_SUITEB_192_LOS:
  451. *pgroups = suiteb_curves + 1;
  452. *pgroupslen = 1;
  453. break;
  454. # endif
  455. default:
  456. if (s->ext.supportedgroups == NULL) {
  457. *pgroups = supported_groups_default;
  458. *pgroupslen = OSSL_NELEM(supported_groups_default);
  459. } else {
  460. *pgroups = s->ext.supportedgroups;
  461. *pgroupslen = s->ext.supportedgroups_len;
  462. }
  463. break;
  464. }
  465. #else
  466. *pgroups = NULL;
  467. *pgroupslen = 0;
  468. #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */
  469. }
  470. int tls_valid_group(SSL *s, uint16_t group_id, int minversion, int maxversion)
  471. {
  472. const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(s->ctx, group_id);
  473. int ret;
  474. if (ginfo == NULL)
  475. return 0;
  476. if (SSL_IS_DTLS(s)) {
  477. if (ginfo->mindtls < 0 || ginfo->maxdtls < 0)
  478. return 0;
  479. if (ginfo->maxdtls == 0)
  480. ret = 1;
  481. else
  482. ret = DTLS_VERSION_LE(minversion, ginfo->maxdtls);
  483. if (ginfo->mindtls > 0)
  484. ret &= DTLS_VERSION_GE(maxversion, ginfo->mindtls);
  485. } else {
  486. if (ginfo->mintls < 0 || ginfo->maxtls < 0)
  487. return 0;
  488. if (ginfo->maxtls == 0)
  489. ret = 1;
  490. else
  491. ret = (minversion <= ginfo->maxtls);
  492. if (ginfo->mintls > 0)
  493. ret &= (maxversion >= ginfo->mintls);
  494. }
  495. return ret;
  496. }
  497. /* See if group is allowed by security callback */
  498. int tls_group_allowed(SSL *s, uint16_t group, int op)
  499. {
  500. const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(s->ctx, group);
  501. unsigned char gtmp[2];
  502. if (ginfo == NULL)
  503. return 0;
  504. gtmp[0] = group >> 8;
  505. gtmp[1] = group & 0xff;
  506. return ssl_security(s, op, ginfo->secbits,
  507. tls1_group_id2nid(ginfo->group_id, 0), (void *)gtmp);
  508. }
  509. /* Return 1 if "id" is in "list" */
  510. static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen)
  511. {
  512. size_t i;
  513. for (i = 0; i < listlen; i++)
  514. if (list[i] == id)
  515. return 1;
  516. return 0;
  517. }
  518. /*-
  519. * For nmatch >= 0, return the id of the |nmatch|th shared group or 0
  520. * if there is no match.
  521. * For nmatch == -1, return number of matches
  522. * For nmatch == -2, return the id of the group to use for
  523. * a tmp key, or 0 if there is no match.
  524. */
  525. uint16_t tls1_shared_group(SSL *s, int nmatch)
  526. {
  527. const uint16_t *pref, *supp;
  528. size_t num_pref, num_supp, i;
  529. int k;
  530. /* Can't do anything on client side */
  531. if (s->server == 0)
  532. return 0;
  533. if (nmatch == -2) {
  534. if (tls1_suiteb(s)) {
  535. /*
  536. * For Suite B ciphersuite determines curve: we already know
  537. * these are acceptable due to previous checks.
  538. */
  539. unsigned long cid = s->s3.tmp.new_cipher->id;
  540. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
  541. return TLSEXT_curve_P_256;
  542. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
  543. return TLSEXT_curve_P_384;
  544. /* Should never happen */
  545. return 0;
  546. }
  547. /* If not Suite B just return first preference shared curve */
  548. nmatch = 0;
  549. }
  550. /*
  551. * If server preference set, our groups are the preference order
  552. * otherwise peer decides.
  553. */
  554. if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
  555. tls1_get_supported_groups(s, &pref, &num_pref);
  556. tls1_get_peer_groups(s, &supp, &num_supp);
  557. } else {
  558. tls1_get_peer_groups(s, &pref, &num_pref);
  559. tls1_get_supported_groups(s, &supp, &num_supp);
  560. }
  561. for (k = 0, i = 0; i < num_pref; i++) {
  562. uint16_t id = pref[i];
  563. if (!tls1_in_list(id, supp, num_supp)
  564. || !tls_group_allowed(s, id, SSL_SECOP_CURVE_SHARED))
  565. continue;
  566. if (nmatch == k)
  567. return id;
  568. k++;
  569. }
  570. if (nmatch == -1)
  571. return k;
  572. /* Out of range (nmatch > k). */
  573. return 0;
  574. }
  575. int tls1_set_groups(uint16_t **pext, size_t *pextlen,
  576. int *groups, size_t ngroups)
  577. {
  578. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  579. uint16_t *glist;
  580. size_t i;
  581. /*
  582. * Bitmap of groups included to detect duplicates: two variables are added
  583. * to detect duplicates as some values are more than 32.
  584. */
  585. unsigned long *dup_list = NULL;
  586. unsigned long dup_list_egrp = 0;
  587. unsigned long dup_list_dhgrp = 0;
  588. if (ngroups == 0) {
  589. SSLerr(SSL_F_TLS1_SET_GROUPS, SSL_R_BAD_LENGTH);
  590. return 0;
  591. }
  592. if ((glist = OPENSSL_malloc(ngroups * sizeof(*glist))) == NULL) {
  593. SSLerr(SSL_F_TLS1_SET_GROUPS, ERR_R_MALLOC_FAILURE);
  594. return 0;
  595. }
  596. for (i = 0; i < ngroups; i++) {
  597. unsigned long idmask;
  598. uint16_t id;
  599. id = tls1_nid2group_id(groups[i]);
  600. if ((id & 0x00FF) >= (sizeof(unsigned long) * 8))
  601. goto err;
  602. idmask = 1L << (id & 0x00FF);
  603. dup_list = (id < 0x100) ? &dup_list_egrp : &dup_list_dhgrp;
  604. if (!id || ((*dup_list) & idmask))
  605. goto err;
  606. *dup_list |= idmask;
  607. glist[i] = id;
  608. }
  609. OPENSSL_free(*pext);
  610. *pext = glist;
  611. *pextlen = ngroups;
  612. return 1;
  613. err:
  614. OPENSSL_free(glist);
  615. return 0;
  616. #else
  617. return 0;
  618. #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */
  619. }
  620. /* TODO(3.0): An arbitrary amount for now. Take another look at this */
  621. # define MAX_GROUPLIST 40
  622. typedef struct {
  623. SSL_CTX *ctx;
  624. size_t gidcnt;
  625. uint16_t gid_arr[MAX_GROUPLIST];
  626. } gid_cb_st;
  627. static int gid_cb(const char *elem, int len, void *arg)
  628. {
  629. gid_cb_st *garg = arg;
  630. size_t i;
  631. uint16_t gid = 0;
  632. char etmp[20];
  633. if (elem == NULL)
  634. return 0;
  635. if (garg->gidcnt == MAX_GROUPLIST)
  636. return 0;
  637. if (len > (int)(sizeof(etmp) - 1))
  638. return 0;
  639. memcpy(etmp, elem, len);
  640. etmp[len] = 0;
  641. gid = tls1_group_name2id(garg->ctx, etmp);
  642. if (gid == 0)
  643. return 0;
  644. for (i = 0; i < garg->gidcnt; i++)
  645. if (garg->gid_arr[i] == gid)
  646. return 0;
  647. garg->gid_arr[garg->gidcnt++] = gid;
  648. return 1;
  649. }
  650. /* Set groups based on a colon separated list */
  651. int tls1_set_groups_list(SSL_CTX *ctx, uint16_t **pext, size_t *pextlen,
  652. const char *str)
  653. {
  654. gid_cb_st gcb;
  655. uint16_t *tmparr;
  656. gcb.gidcnt = 0;
  657. gcb.ctx = ctx;
  658. if (!CONF_parse_list(str, ':', 1, gid_cb, &gcb))
  659. return 0;
  660. if (pext == NULL)
  661. return 1;
  662. /*
  663. * gid_cb ensurse there are no duplicates so we can just go ahead and set
  664. * the result
  665. */
  666. tmparr = OPENSSL_memdup(gcb.gid_arr, gcb.gidcnt * sizeof(*tmparr));
  667. if (tmparr == NULL)
  668. return 0;
  669. *pext = tmparr;
  670. *pextlen = gcb.gidcnt;
  671. return 1;
  672. }
  673. /* Check a group id matches preferences */
  674. int tls1_check_group_id(SSL *s, uint16_t group_id, int check_own_groups)
  675. {
  676. const uint16_t *groups;
  677. size_t groups_len;
  678. if (group_id == 0)
  679. return 0;
  680. /* Check for Suite B compliance */
  681. if (tls1_suiteb(s) && s->s3.tmp.new_cipher != NULL) {
  682. unsigned long cid = s->s3.tmp.new_cipher->id;
  683. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
  684. if (group_id != TLSEXT_curve_P_256)
  685. return 0;
  686. } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
  687. if (group_id != TLSEXT_curve_P_384)
  688. return 0;
  689. } else {
  690. /* Should never happen */
  691. return 0;
  692. }
  693. }
  694. if (check_own_groups) {
  695. /* Check group is one of our preferences */
  696. tls1_get_supported_groups(s, &groups, &groups_len);
  697. if (!tls1_in_list(group_id, groups, groups_len))
  698. return 0;
  699. }
  700. if (!tls_group_allowed(s, group_id, SSL_SECOP_CURVE_CHECK))
  701. return 0;
  702. /* For clients, nothing more to check */
  703. if (!s->server)
  704. return 1;
  705. /* Check group is one of peers preferences */
  706. tls1_get_peer_groups(s, &groups, &groups_len);
  707. /*
  708. * RFC 4492 does not require the supported elliptic curves extension
  709. * so if it is not sent we can just choose any curve.
  710. * It is invalid to send an empty list in the supported groups
  711. * extension, so groups_len == 0 always means no extension.
  712. */
  713. if (groups_len == 0)
  714. return 1;
  715. return tls1_in_list(group_id, groups, groups_len);
  716. }
  717. #ifndef OPENSSL_NO_EC
  718. void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
  719. size_t *num_formats)
  720. {
  721. /*
  722. * If we have a custom point format list use it otherwise use default
  723. */
  724. if (s->ext.ecpointformats) {
  725. *pformats = s->ext.ecpointformats;
  726. *num_formats = s->ext.ecpointformats_len;
  727. } else {
  728. *pformats = ecformats_default;
  729. /* For Suite B we don't support char2 fields */
  730. if (tls1_suiteb(s))
  731. *num_formats = sizeof(ecformats_default) - 1;
  732. else
  733. *num_formats = sizeof(ecformats_default);
  734. }
  735. }
  736. /* Check a key is compatible with compression extension */
  737. static int tls1_check_pkey_comp(SSL *s, EVP_PKEY *pkey)
  738. {
  739. const EC_KEY *ec;
  740. const EC_GROUP *grp;
  741. unsigned char comp_id;
  742. size_t i;
  743. /* If not an EC key nothing to check */
  744. if (!EVP_PKEY_is_a(pkey, "EC"))
  745. return 1;
  746. ec = EVP_PKEY_get0_EC_KEY(pkey);
  747. grp = EC_KEY_get0_group(ec);
  748. /* Get required compression id */
  749. if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
  750. comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
  751. } else if (SSL_IS_TLS13(s)) {
  752. /*
  753. * ec_point_formats extension is not used in TLSv1.3 so we ignore
  754. * this check.
  755. */
  756. return 1;
  757. } else {
  758. int field_type = EC_GROUP_get_field_type(grp);
  759. if (field_type == NID_X9_62_prime_field)
  760. comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
  761. else if (field_type == NID_X9_62_characteristic_two_field)
  762. comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
  763. else
  764. return 0;
  765. }
  766. /*
  767. * If point formats extension present check it, otherwise everything is
  768. * supported (see RFC4492).
  769. */
  770. if (s->ext.peer_ecpointformats == NULL)
  771. return 1;
  772. for (i = 0; i < s->ext.peer_ecpointformats_len; i++) {
  773. if (s->ext.peer_ecpointformats[i] == comp_id)
  774. return 1;
  775. }
  776. return 0;
  777. }
  778. /* Return group id of a key */
  779. static uint16_t tls1_get_group_id(EVP_PKEY *pkey)
  780. {
  781. int curve_nid = evp_pkey_get_EC_KEY_curve_nid(pkey);
  782. if (curve_nid == NID_undef)
  783. return 0;
  784. return tls1_nid2group_id(curve_nid);
  785. }
  786. /*
  787. * Check cert parameters compatible with extensions: currently just checks EC
  788. * certificates have compatible curves and compression.
  789. */
  790. static int tls1_check_cert_param(SSL *s, X509 *x, int check_ee_md)
  791. {
  792. uint16_t group_id;
  793. EVP_PKEY *pkey;
  794. pkey = X509_get0_pubkey(x);
  795. if (pkey == NULL)
  796. return 0;
  797. /* If not EC nothing to do */
  798. if (!EVP_PKEY_is_a(pkey, "EC"))
  799. return 1;
  800. /* Check compression */
  801. if (!tls1_check_pkey_comp(s, pkey))
  802. return 0;
  803. group_id = tls1_get_group_id(pkey);
  804. /*
  805. * For a server we allow the certificate to not be in our list of supported
  806. * groups.
  807. */
  808. if (!tls1_check_group_id(s, group_id, !s->server))
  809. return 0;
  810. /*
  811. * Special case for suite B. We *MUST* sign using SHA256+P-256 or
  812. * SHA384+P-384.
  813. */
  814. if (check_ee_md && tls1_suiteb(s)) {
  815. int check_md;
  816. size_t i;
  817. /* Check to see we have necessary signing algorithm */
  818. if (group_id == TLSEXT_curve_P_256)
  819. check_md = NID_ecdsa_with_SHA256;
  820. else if (group_id == TLSEXT_curve_P_384)
  821. check_md = NID_ecdsa_with_SHA384;
  822. else
  823. return 0; /* Should never happen */
  824. for (i = 0; i < s->shared_sigalgslen; i++) {
  825. if (check_md == s->shared_sigalgs[i]->sigandhash)
  826. return 1;;
  827. }
  828. return 0;
  829. }
  830. return 1;
  831. }
  832. /*
  833. * tls1_check_ec_tmp_key - Check EC temporary key compatibility
  834. * @s: SSL connection
  835. * @cid: Cipher ID we're considering using
  836. *
  837. * Checks that the kECDHE cipher suite we're considering using
  838. * is compatible with the client extensions.
  839. *
  840. * Returns 0 when the cipher can't be used or 1 when it can.
  841. */
  842. int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
  843. {
  844. /* If not Suite B just need a shared group */
  845. if (!tls1_suiteb(s))
  846. return tls1_shared_group(s, 0) != 0;
  847. /*
  848. * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
  849. * curves permitted.
  850. */
  851. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
  852. return tls1_check_group_id(s, TLSEXT_curve_P_256, 1);
  853. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
  854. return tls1_check_group_id(s, TLSEXT_curve_P_384, 1);
  855. return 0;
  856. }
  857. #else
  858. static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
  859. {
  860. return 1;
  861. }
  862. #endif /* OPENSSL_NO_EC */
  863. /* Default sigalg schemes */
  864. static const uint16_t tls12_sigalgs[] = {
  865. #ifndef OPENSSL_NO_EC
  866. TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
  867. TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
  868. TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
  869. TLSEXT_SIGALG_ed25519,
  870. TLSEXT_SIGALG_ed448,
  871. #endif
  872. TLSEXT_SIGALG_rsa_pss_pss_sha256,
  873. TLSEXT_SIGALG_rsa_pss_pss_sha384,
  874. TLSEXT_SIGALG_rsa_pss_pss_sha512,
  875. TLSEXT_SIGALG_rsa_pss_rsae_sha256,
  876. TLSEXT_SIGALG_rsa_pss_rsae_sha384,
  877. TLSEXT_SIGALG_rsa_pss_rsae_sha512,
  878. TLSEXT_SIGALG_rsa_pkcs1_sha256,
  879. TLSEXT_SIGALG_rsa_pkcs1_sha384,
  880. TLSEXT_SIGALG_rsa_pkcs1_sha512,
  881. #ifndef OPENSSL_NO_EC
  882. TLSEXT_SIGALG_ecdsa_sha224,
  883. TLSEXT_SIGALG_ecdsa_sha1,
  884. #endif
  885. TLSEXT_SIGALG_rsa_pkcs1_sha224,
  886. TLSEXT_SIGALG_rsa_pkcs1_sha1,
  887. #ifndef OPENSSL_NO_DSA
  888. TLSEXT_SIGALG_dsa_sha224,
  889. TLSEXT_SIGALG_dsa_sha1,
  890. TLSEXT_SIGALG_dsa_sha256,
  891. TLSEXT_SIGALG_dsa_sha384,
  892. TLSEXT_SIGALG_dsa_sha512,
  893. #endif
  894. #ifndef OPENSSL_NO_GOST
  895. TLSEXT_SIGALG_gostr34102012_256_intrinsic,
  896. TLSEXT_SIGALG_gostr34102012_512_intrinsic,
  897. TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,
  898. TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,
  899. TLSEXT_SIGALG_gostr34102001_gostr3411,
  900. #endif
  901. };
  902. #ifndef OPENSSL_NO_EC
  903. static const uint16_t suiteb_sigalgs[] = {
  904. TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
  905. TLSEXT_SIGALG_ecdsa_secp384r1_sha384
  906. };
  907. #endif
  908. static const SIGALG_LOOKUP sigalg_lookup_tbl[] = {
  909. #ifndef OPENSSL_NO_EC
  910. {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
  911. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  912. NID_ecdsa_with_SHA256, NID_X9_62_prime256v1, 1},
  913. {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
  914. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  915. NID_ecdsa_with_SHA384, NID_secp384r1, 1},
  916. {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
  917. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  918. NID_ecdsa_with_SHA512, NID_secp521r1, 1},
  919. {"ed25519", TLSEXT_SIGALG_ed25519,
  920. NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519,
  921. NID_undef, NID_undef, 1},
  922. {"ed448", TLSEXT_SIGALG_ed448,
  923. NID_undef, -1, EVP_PKEY_ED448, SSL_PKEY_ED448,
  924. NID_undef, NID_undef, 1},
  925. {NULL, TLSEXT_SIGALG_ecdsa_sha224,
  926. NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  927. NID_ecdsa_with_SHA224, NID_undef, 1},
  928. {NULL, TLSEXT_SIGALG_ecdsa_sha1,
  929. NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  930. NID_ecdsa_with_SHA1, NID_undef, 1},
  931. #endif
  932. {"rsa_pss_rsae_sha256", TLSEXT_SIGALG_rsa_pss_rsae_sha256,
  933. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
  934. NID_undef, NID_undef, 1},
  935. {"rsa_pss_rsae_sha384", TLSEXT_SIGALG_rsa_pss_rsae_sha384,
  936. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
  937. NID_undef, NID_undef, 1},
  938. {"rsa_pss_rsae_sha512", TLSEXT_SIGALG_rsa_pss_rsae_sha512,
  939. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
  940. NID_undef, NID_undef, 1},
  941. {"rsa_pss_pss_sha256", TLSEXT_SIGALG_rsa_pss_pss_sha256,
  942. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
  943. NID_undef, NID_undef, 1},
  944. {"rsa_pss_pss_sha384", TLSEXT_SIGALG_rsa_pss_pss_sha384,
  945. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
  946. NID_undef, NID_undef, 1},
  947. {"rsa_pss_pss_sha512", TLSEXT_SIGALG_rsa_pss_pss_sha512,
  948. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
  949. NID_undef, NID_undef, 1},
  950. {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256,
  951. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  952. NID_sha256WithRSAEncryption, NID_undef, 1},
  953. {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384,
  954. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  955. NID_sha384WithRSAEncryption, NID_undef, 1},
  956. {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512,
  957. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  958. NID_sha512WithRSAEncryption, NID_undef, 1},
  959. {"rsa_pkcs1_sha224", TLSEXT_SIGALG_rsa_pkcs1_sha224,
  960. NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  961. NID_sha224WithRSAEncryption, NID_undef, 1},
  962. {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1,
  963. NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  964. NID_sha1WithRSAEncryption, NID_undef, 1},
  965. #ifndef OPENSSL_NO_DSA
  966. {NULL, TLSEXT_SIGALG_dsa_sha256,
  967. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  968. NID_dsa_with_SHA256, NID_undef, 1},
  969. {NULL, TLSEXT_SIGALG_dsa_sha384,
  970. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  971. NID_undef, NID_undef, 1},
  972. {NULL, TLSEXT_SIGALG_dsa_sha512,
  973. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  974. NID_undef, NID_undef, 1},
  975. {NULL, TLSEXT_SIGALG_dsa_sha224,
  976. NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  977. NID_undef, NID_undef, 1},
  978. {NULL, TLSEXT_SIGALG_dsa_sha1,
  979. NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  980. NID_dsaWithSHA1, NID_undef, 1},
  981. #endif
  982. #ifndef OPENSSL_NO_GOST
  983. {NULL, TLSEXT_SIGALG_gostr34102012_256_intrinsic,
  984. NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX,
  985. NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256,
  986. NID_undef, NID_undef, 1},
  987. {NULL, TLSEXT_SIGALG_gostr34102012_512_intrinsic,
  988. NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX,
  989. NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512,
  990. NID_undef, NID_undef, 1},
  991. {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,
  992. NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX,
  993. NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256,
  994. NID_undef, NID_undef, 1},
  995. {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,
  996. NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX,
  997. NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512,
  998. NID_undef, NID_undef, 1},
  999. {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411,
  1000. NID_id_GostR3411_94, SSL_MD_GOST94_IDX,
  1001. NID_id_GostR3410_2001, SSL_PKEY_GOST01,
  1002. NID_undef, NID_undef, 1}
  1003. #endif
  1004. };
  1005. /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */
  1006. static const SIGALG_LOOKUP legacy_rsa_sigalg = {
  1007. "rsa_pkcs1_md5_sha1", 0,
  1008. NID_md5_sha1, SSL_MD_MD5_SHA1_IDX,
  1009. EVP_PKEY_RSA, SSL_PKEY_RSA,
  1010. NID_undef, NID_undef, 1
  1011. };
  1012. /*
  1013. * Default signature algorithm values used if signature algorithms not present.
  1014. * From RFC5246. Note: order must match certificate index order.
  1015. */
  1016. static const uint16_t tls_default_sigalg[] = {
  1017. TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */
  1018. 0, /* SSL_PKEY_RSA_PSS_SIGN */
  1019. TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */
  1020. TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */
  1021. TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */
  1022. TLSEXT_SIGALG_gostr34102012_256_intrinsic, /* SSL_PKEY_GOST12_256 */
  1023. TLSEXT_SIGALG_gostr34102012_512_intrinsic, /* SSL_PKEY_GOST12_512 */
  1024. 0, /* SSL_PKEY_ED25519 */
  1025. 0, /* SSL_PKEY_ED448 */
  1026. };
  1027. int ssl_setup_sig_algs(SSL_CTX *ctx)
  1028. {
  1029. size_t i;
  1030. const SIGALG_LOOKUP *lu;
  1031. SIGALG_LOOKUP *cache
  1032. = OPENSSL_malloc(sizeof(*lu) * OSSL_NELEM(sigalg_lookup_tbl));
  1033. EVP_PKEY *tmpkey = EVP_PKEY_new();
  1034. int ret = 0;
  1035. if (cache == NULL || tmpkey == NULL)
  1036. goto err;
  1037. ERR_set_mark();
  1038. for (i = 0, lu = sigalg_lookup_tbl;
  1039. i < OSSL_NELEM(sigalg_lookup_tbl); lu++, i++) {
  1040. EVP_PKEY_CTX *pctx;
  1041. cache[i] = *lu;
  1042. /*
  1043. * Check hash is available.
  1044. * TODO(3.0): This test is not perfect. A provider could have support
  1045. * for a signature scheme, but not a particular hash. However the hash
  1046. * could be available from some other loaded provider. In that case it
  1047. * could be that the signature is available, and the hash is available
  1048. * independently - but not as a combination. We ignore this for now.
  1049. */
  1050. if (lu->hash != NID_undef
  1051. && ctx->ssl_digest_methods[lu->hash_idx] == NULL) {
  1052. cache[i].enabled = 0;
  1053. continue;
  1054. }
  1055. if (!EVP_PKEY_set_type(tmpkey, lu->sig)) {
  1056. cache[i].enabled = 0;
  1057. continue;
  1058. }
  1059. pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, tmpkey, ctx->propq);
  1060. /* If unable to create pctx we assume the sig algorithm is unavailable */
  1061. if (pctx == NULL)
  1062. cache[i].enabled = 0;
  1063. EVP_PKEY_CTX_free(pctx);
  1064. }
  1065. ERR_pop_to_mark();
  1066. ctx->sigalg_lookup_cache = cache;
  1067. cache = NULL;
  1068. ret = 1;
  1069. err:
  1070. OPENSSL_free(cache);
  1071. EVP_PKEY_free(tmpkey);
  1072. return ret;
  1073. }
  1074. /* Lookup TLS signature algorithm */
  1075. static const SIGALG_LOOKUP *tls1_lookup_sigalg(const SSL *s, uint16_t sigalg)
  1076. {
  1077. size_t i;
  1078. const SIGALG_LOOKUP *lu;
  1079. for (i = 0, lu = s->ctx->sigalg_lookup_cache;
  1080. /* cache should have the same number of elements as sigalg_lookup_tbl */
  1081. i < OSSL_NELEM(sigalg_lookup_tbl);
  1082. lu++, i++) {
  1083. if (lu->sigalg == sigalg)
  1084. return lu;
  1085. }
  1086. return NULL;
  1087. }
  1088. /* Lookup hash: return 0 if invalid or not enabled */
  1089. int tls1_lookup_md(SSL_CTX *ctx, const SIGALG_LOOKUP *lu, const EVP_MD **pmd)
  1090. {
  1091. const EVP_MD *md;
  1092. if (lu == NULL)
  1093. return 0;
  1094. /* lu->hash == NID_undef means no associated digest */
  1095. if (lu->hash == NID_undef) {
  1096. md = NULL;
  1097. } else {
  1098. md = ssl_md(ctx, lu->hash_idx);
  1099. if (md == NULL)
  1100. return 0;
  1101. }
  1102. if (pmd)
  1103. *pmd = md;
  1104. return 1;
  1105. }
  1106. /*
  1107. * Check if key is large enough to generate RSA-PSS signature.
  1108. *
  1109. * The key must greater than or equal to 2 * hash length + 2.
  1110. * SHA512 has a hash length of 64 bytes, which is incompatible
  1111. * with a 128 byte (1024 bit) key.
  1112. */
  1113. #define RSA_PSS_MINIMUM_KEY_SIZE(md) (2 * EVP_MD_size(md) + 2)
  1114. static int rsa_pss_check_min_key_size(SSL_CTX *ctx, const EVP_PKEY *pkey,
  1115. const SIGALG_LOOKUP *lu)
  1116. {
  1117. const EVP_MD *md;
  1118. if (pkey == NULL)
  1119. return 0;
  1120. if (!tls1_lookup_md(ctx, lu, &md) || md == NULL)
  1121. return 0;
  1122. if (EVP_PKEY_size(pkey) < RSA_PSS_MINIMUM_KEY_SIZE(md))
  1123. return 0;
  1124. return 1;
  1125. }
  1126. /*
  1127. * Returns a signature algorithm when the peer did not send a list of supported
  1128. * signature algorithms. The signature algorithm is fixed for the certificate
  1129. * type. |idx| is a certificate type index (SSL_PKEY_*). When |idx| is -1 the
  1130. * certificate type from |s| will be used.
  1131. * Returns the signature algorithm to use, or NULL on error.
  1132. */
  1133. static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx)
  1134. {
  1135. if (idx == -1) {
  1136. if (s->server) {
  1137. size_t i;
  1138. /* Work out index corresponding to ciphersuite */
  1139. for (i = 0; i < SSL_PKEY_NUM; i++) {
  1140. const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(i);
  1141. if (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) {
  1142. idx = i;
  1143. break;
  1144. }
  1145. }
  1146. /*
  1147. * Some GOST ciphersuites allow more than one signature algorithms
  1148. * */
  1149. if (idx == SSL_PKEY_GOST01 && s->s3.tmp.new_cipher->algorithm_auth != SSL_aGOST01) {
  1150. int real_idx;
  1151. for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST01;
  1152. real_idx--) {
  1153. if (s->cert->pkeys[real_idx].privatekey != NULL) {
  1154. idx = real_idx;
  1155. break;
  1156. }
  1157. }
  1158. }
  1159. /*
  1160. * As both SSL_PKEY_GOST12_512 and SSL_PKEY_GOST12_256 indices can be used
  1161. * with new (aGOST12-only) ciphersuites, we should find out which one is available really.
  1162. */
  1163. else if (idx == SSL_PKEY_GOST12_256) {
  1164. int real_idx;
  1165. for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST12_256;
  1166. real_idx--) {
  1167. if (s->cert->pkeys[real_idx].privatekey != NULL) {
  1168. idx = real_idx;
  1169. break;
  1170. }
  1171. }
  1172. }
  1173. } else {
  1174. idx = s->cert->key - s->cert->pkeys;
  1175. }
  1176. }
  1177. if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg))
  1178. return NULL;
  1179. if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) {
  1180. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, tls_default_sigalg[idx]);
  1181. if (!tls1_lookup_md(s->ctx, lu, NULL))
  1182. return NULL;
  1183. if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu))
  1184. return NULL;
  1185. return lu;
  1186. }
  1187. if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, &legacy_rsa_sigalg))
  1188. return NULL;
  1189. return &legacy_rsa_sigalg;
  1190. }
  1191. /* Set peer sigalg based key type */
  1192. int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey)
  1193. {
  1194. size_t idx;
  1195. const SIGALG_LOOKUP *lu;
  1196. if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL)
  1197. return 0;
  1198. lu = tls1_get_legacy_sigalg(s, idx);
  1199. if (lu == NULL)
  1200. return 0;
  1201. s->s3.tmp.peer_sigalg = lu;
  1202. return 1;
  1203. }
  1204. size_t tls12_get_psigalgs(SSL *s, int sent, const uint16_t **psigs)
  1205. {
  1206. /*
  1207. * If Suite B mode use Suite B sigalgs only, ignore any other
  1208. * preferences.
  1209. */
  1210. #ifndef OPENSSL_NO_EC
  1211. switch (tls1_suiteb(s)) {
  1212. case SSL_CERT_FLAG_SUITEB_128_LOS:
  1213. *psigs = suiteb_sigalgs;
  1214. return OSSL_NELEM(suiteb_sigalgs);
  1215. case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
  1216. *psigs = suiteb_sigalgs;
  1217. return 1;
  1218. case SSL_CERT_FLAG_SUITEB_192_LOS:
  1219. *psigs = suiteb_sigalgs + 1;
  1220. return 1;
  1221. }
  1222. #endif
  1223. /*
  1224. * We use client_sigalgs (if not NULL) if we're a server
  1225. * and sending a certificate request or if we're a client and
  1226. * determining which shared algorithm to use.
  1227. */
  1228. if ((s->server == sent) && s->cert->client_sigalgs != NULL) {
  1229. *psigs = s->cert->client_sigalgs;
  1230. return s->cert->client_sigalgslen;
  1231. } else if (s->cert->conf_sigalgs) {
  1232. *psigs = s->cert->conf_sigalgs;
  1233. return s->cert->conf_sigalgslen;
  1234. } else {
  1235. *psigs = tls12_sigalgs;
  1236. return OSSL_NELEM(tls12_sigalgs);
  1237. }
  1238. }
  1239. #ifndef OPENSSL_NO_EC
  1240. /*
  1241. * Called by servers only. Checks that we have a sig alg that supports the
  1242. * specified EC curve.
  1243. */
  1244. int tls_check_sigalg_curve(const SSL *s, int curve)
  1245. {
  1246. const uint16_t *sigs;
  1247. size_t siglen, i;
  1248. if (s->cert->conf_sigalgs) {
  1249. sigs = s->cert->conf_sigalgs;
  1250. siglen = s->cert->conf_sigalgslen;
  1251. } else {
  1252. sigs = tls12_sigalgs;
  1253. siglen = OSSL_NELEM(tls12_sigalgs);
  1254. }
  1255. for (i = 0; i < siglen; i++) {
  1256. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, sigs[i]);
  1257. if (lu == NULL)
  1258. continue;
  1259. if (lu->sig == EVP_PKEY_EC
  1260. && lu->curve != NID_undef
  1261. && curve == lu->curve)
  1262. return 1;
  1263. }
  1264. return 0;
  1265. }
  1266. #endif
  1267. /*
  1268. * Return the number of security bits for the signature algorithm, or 0 on
  1269. * error.
  1270. */
  1271. static int sigalg_security_bits(SSL_CTX *ctx, const SIGALG_LOOKUP *lu)
  1272. {
  1273. const EVP_MD *md = NULL;
  1274. int secbits = 0;
  1275. if (!tls1_lookup_md(ctx, lu, &md))
  1276. return 0;
  1277. if (md != NULL)
  1278. {
  1279. int md_type = EVP_MD_type(md);
  1280. /* Security bits: half digest bits */
  1281. secbits = EVP_MD_size(md) * 4;
  1282. /*
  1283. * SHA1 and MD5 are known to be broken. Reduce security bits so that
  1284. * they're no longer accepted at security level 1. The real values don't
  1285. * really matter as long as they're lower than 80, which is our
  1286. * security level 1.
  1287. * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for
  1288. * SHA1 at 2^63.4 and MD5+SHA1 at 2^67.2
  1289. * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
  1290. * puts a chosen-prefix attack for MD5 at 2^39.
  1291. */
  1292. if (md_type == NID_sha1)
  1293. secbits = 64;
  1294. else if (md_type == NID_md5_sha1)
  1295. secbits = 67;
  1296. else if (md_type == NID_md5)
  1297. secbits = 39;
  1298. } else {
  1299. /* Values from https://tools.ietf.org/html/rfc8032#section-8.5 */
  1300. if (lu->sigalg == TLSEXT_SIGALG_ed25519)
  1301. secbits = 128;
  1302. else if (lu->sigalg == TLSEXT_SIGALG_ed448)
  1303. secbits = 224;
  1304. }
  1305. return secbits;
  1306. }
  1307. /*
  1308. * Check signature algorithm is consistent with sent supported signature
  1309. * algorithms and if so set relevant digest and signature scheme in
  1310. * s.
  1311. */
  1312. int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey)
  1313. {
  1314. const uint16_t *sent_sigs;
  1315. const EVP_MD *md = NULL;
  1316. char sigalgstr[2];
  1317. size_t sent_sigslen, i, cidx;
  1318. int pkeyid = -1;
  1319. const SIGALG_LOOKUP *lu;
  1320. int secbits = 0;
  1321. pkeyid = EVP_PKEY_id(pkey);
  1322. /* Should never happen */
  1323. if (pkeyid == -1)
  1324. return -1;
  1325. if (SSL_IS_TLS13(s)) {
  1326. /* Disallow DSA for TLS 1.3 */
  1327. if (pkeyid == EVP_PKEY_DSA) {
  1328. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG,
  1329. SSL_R_WRONG_SIGNATURE_TYPE);
  1330. return 0;
  1331. }
  1332. /* Only allow PSS for TLS 1.3 */
  1333. if (pkeyid == EVP_PKEY_RSA)
  1334. pkeyid = EVP_PKEY_RSA_PSS;
  1335. }
  1336. lu = tls1_lookup_sigalg(s, sig);
  1337. /*
  1338. * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type
  1339. * is consistent with signature: RSA keys can be used for RSA-PSS
  1340. */
  1341. if (lu == NULL
  1342. || (SSL_IS_TLS13(s) && (lu->hash == NID_sha1 || lu->hash == NID_sha224))
  1343. || (pkeyid != lu->sig
  1344. && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) {
  1345. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG,
  1346. SSL_R_WRONG_SIGNATURE_TYPE);
  1347. return 0;
  1348. }
  1349. /* Check the sigalg is consistent with the key OID */
  1350. if (!ssl_cert_lookup_by_nid(EVP_PKEY_id(pkey), &cidx)
  1351. || lu->sig_idx != (int)cidx) {
  1352. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG,
  1353. SSL_R_WRONG_SIGNATURE_TYPE);
  1354. return 0;
  1355. }
  1356. #ifndef OPENSSL_NO_EC
  1357. if (pkeyid == EVP_PKEY_EC) {
  1358. /* Check point compression is permitted */
  1359. if (!tls1_check_pkey_comp(s, pkey)) {
  1360. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1361. SSL_F_TLS12_CHECK_PEER_SIGALG,
  1362. SSL_R_ILLEGAL_POINT_COMPRESSION);
  1363. return 0;
  1364. }
  1365. /* For TLS 1.3 or Suite B check curve matches signature algorithm */
  1366. if (SSL_IS_TLS13(s) || tls1_suiteb(s)) {
  1367. int curve = evp_pkey_get_EC_KEY_curve_nid(pkey);
  1368. if (lu->curve != NID_undef && curve != lu->curve) {
  1369. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1370. SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
  1371. return 0;
  1372. }
  1373. }
  1374. if (!SSL_IS_TLS13(s)) {
  1375. /* Check curve matches extensions */
  1376. if (!tls1_check_group_id(s, tls1_get_group_id(pkey), 1)) {
  1377. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1378. SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
  1379. return 0;
  1380. }
  1381. if (tls1_suiteb(s)) {
  1382. /* Check sigalg matches a permissible Suite B value */
  1383. if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256
  1384. && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) {
  1385. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1386. SSL_F_TLS12_CHECK_PEER_SIGALG,
  1387. SSL_R_WRONG_SIGNATURE_TYPE);
  1388. return 0;
  1389. }
  1390. }
  1391. }
  1392. } else if (tls1_suiteb(s)) {
  1393. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
  1394. SSL_R_WRONG_SIGNATURE_TYPE);
  1395. return 0;
  1396. }
  1397. #endif
  1398. /* Check signature matches a type we sent */
  1399. sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
  1400. for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
  1401. if (sig == *sent_sigs)
  1402. break;
  1403. }
  1404. /* Allow fallback to SHA1 if not strict mode */
  1405. if (i == sent_sigslen && (lu->hash != NID_sha1
  1406. || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
  1407. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
  1408. SSL_R_WRONG_SIGNATURE_TYPE);
  1409. return 0;
  1410. }
  1411. if (!tls1_lookup_md(s->ctx, lu, &md)) {
  1412. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
  1413. SSL_R_UNKNOWN_DIGEST);
  1414. return 0;
  1415. }
  1416. /*
  1417. * Make sure security callback allows algorithm. For historical
  1418. * reasons we have to pass the sigalg as a two byte char array.
  1419. */
  1420. sigalgstr[0] = (sig >> 8) & 0xff;
  1421. sigalgstr[1] = sig & 0xff;
  1422. secbits = sigalg_security_bits(s->ctx, lu);
  1423. if (secbits == 0 ||
  1424. !ssl_security(s, SSL_SECOP_SIGALG_CHECK, secbits,
  1425. md != NULL ? EVP_MD_type(md) : NID_undef,
  1426. (void *)sigalgstr)) {
  1427. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
  1428. SSL_R_WRONG_SIGNATURE_TYPE);
  1429. return 0;
  1430. }
  1431. /* Store the sigalg the peer uses */
  1432. s->s3.tmp.peer_sigalg = lu;
  1433. return 1;
  1434. }
  1435. int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid)
  1436. {
  1437. if (s->s3.tmp.peer_sigalg == NULL)
  1438. return 0;
  1439. *pnid = s->s3.tmp.peer_sigalg->sig;
  1440. return 1;
  1441. }
  1442. int SSL_get_signature_type_nid(const SSL *s, int *pnid)
  1443. {
  1444. if (s->s3.tmp.sigalg == NULL)
  1445. return 0;
  1446. *pnid = s->s3.tmp.sigalg->sig;
  1447. return 1;
  1448. }
  1449. /*
  1450. * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
  1451. * supported, doesn't appear in supported signature algorithms, isn't supported
  1452. * by the enabled protocol versions or by the security level.
  1453. *
  1454. * This function should only be used for checking which ciphers are supported
  1455. * by the client.
  1456. *
  1457. * Call ssl_cipher_disabled() to check that it's enabled or not.
  1458. */
  1459. int ssl_set_client_disabled(SSL *s)
  1460. {
  1461. s->s3.tmp.mask_a = 0;
  1462. s->s3.tmp.mask_k = 0;
  1463. ssl_set_sig_mask(&s->s3.tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
  1464. if (ssl_get_min_max_version(s, &s->s3.tmp.min_ver,
  1465. &s->s3.tmp.max_ver, NULL) != 0)
  1466. return 0;
  1467. #ifndef OPENSSL_NO_PSK
  1468. /* with PSK there must be client callback set */
  1469. if (!s->psk_client_callback) {
  1470. s->s3.tmp.mask_a |= SSL_aPSK;
  1471. s->s3.tmp.mask_k |= SSL_PSK;
  1472. }
  1473. #endif /* OPENSSL_NO_PSK */
  1474. #ifndef OPENSSL_NO_SRP
  1475. if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
  1476. s->s3.tmp.mask_a |= SSL_aSRP;
  1477. s->s3.tmp.mask_k |= SSL_kSRP;
  1478. }
  1479. #endif
  1480. return 1;
  1481. }
  1482. /*
  1483. * ssl_cipher_disabled - check that a cipher is disabled or not
  1484. * @s: SSL connection that you want to use the cipher on
  1485. * @c: cipher to check
  1486. * @op: Security check that you want to do
  1487. * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3
  1488. *
  1489. * Returns 1 when it's disabled, 0 when enabled.
  1490. */
  1491. int ssl_cipher_disabled(const SSL *s, const SSL_CIPHER *c, int op, int ecdhe)
  1492. {
  1493. if (c->algorithm_mkey & s->s3.tmp.mask_k
  1494. || c->algorithm_auth & s->s3.tmp.mask_a)
  1495. return 1;
  1496. if (s->s3.tmp.max_ver == 0)
  1497. return 1;
  1498. if (!SSL_IS_DTLS(s)) {
  1499. int min_tls = c->min_tls;
  1500. /*
  1501. * For historical reasons we will allow ECHDE to be selected by a server
  1502. * in SSLv3 if we are a client
  1503. */
  1504. if (min_tls == TLS1_VERSION && ecdhe
  1505. && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0)
  1506. min_tls = SSL3_VERSION;
  1507. if ((min_tls > s->s3.tmp.max_ver) || (c->max_tls < s->s3.tmp.min_ver))
  1508. return 1;
  1509. }
  1510. if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3.tmp.max_ver)
  1511. || DTLS_VERSION_LT(c->max_dtls, s->s3.tmp.min_ver)))
  1512. return 1;
  1513. return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
  1514. }
  1515. int tls_use_ticket(SSL *s)
  1516. {
  1517. if ((s->options & SSL_OP_NO_TICKET))
  1518. return 0;
  1519. return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
  1520. }
  1521. int tls1_set_server_sigalgs(SSL *s)
  1522. {
  1523. size_t i;
  1524. /* Clear any shared signature algorithms */
  1525. OPENSSL_free(s->shared_sigalgs);
  1526. s->shared_sigalgs = NULL;
  1527. s->shared_sigalgslen = 0;
  1528. /* Clear certificate validity flags */
  1529. for (i = 0; i < SSL_PKEY_NUM; i++)
  1530. s->s3.tmp.valid_flags[i] = 0;
  1531. /*
  1532. * If peer sent no signature algorithms check to see if we support
  1533. * the default algorithm for each certificate type
  1534. */
  1535. if (s->s3.tmp.peer_cert_sigalgs == NULL
  1536. && s->s3.tmp.peer_sigalgs == NULL) {
  1537. const uint16_t *sent_sigs;
  1538. size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
  1539. for (i = 0; i < SSL_PKEY_NUM; i++) {
  1540. const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i);
  1541. size_t j;
  1542. if (lu == NULL)
  1543. continue;
  1544. /* Check default matches a type we sent */
  1545. for (j = 0; j < sent_sigslen; j++) {
  1546. if (lu->sigalg == sent_sigs[j]) {
  1547. s->s3.tmp.valid_flags[i] = CERT_PKEY_SIGN;
  1548. break;
  1549. }
  1550. }
  1551. }
  1552. return 1;
  1553. }
  1554. if (!tls1_process_sigalgs(s)) {
  1555. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1556. SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_INTERNAL_ERROR);
  1557. return 0;
  1558. }
  1559. if (s->shared_sigalgs != NULL)
  1560. return 1;
  1561. /* Fatal error if no shared signature algorithms */
  1562. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS1_SET_SERVER_SIGALGS,
  1563. SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
  1564. return 0;
  1565. }
  1566. /*-
  1567. * Gets the ticket information supplied by the client if any.
  1568. *
  1569. * hello: The parsed ClientHello data
  1570. * ret: (output) on return, if a ticket was decrypted, then this is set to
  1571. * point to the resulting session.
  1572. */
  1573. SSL_TICKET_STATUS tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello,
  1574. SSL_SESSION **ret)
  1575. {
  1576. size_t size;
  1577. RAW_EXTENSION *ticketext;
  1578. *ret = NULL;
  1579. s->ext.ticket_expected = 0;
  1580. /*
  1581. * If tickets disabled or not supported by the protocol version
  1582. * (e.g. TLSv1.3) behave as if no ticket present to permit stateful
  1583. * resumption.
  1584. */
  1585. if (s->version <= SSL3_VERSION || !tls_use_ticket(s))
  1586. return SSL_TICKET_NONE;
  1587. ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket];
  1588. if (!ticketext->present)
  1589. return SSL_TICKET_NONE;
  1590. size = PACKET_remaining(&ticketext->data);
  1591. return tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size,
  1592. hello->session_id, hello->session_id_len, ret);
  1593. }
  1594. /*-
  1595. * tls_decrypt_ticket attempts to decrypt a session ticket.
  1596. *
  1597. * If s->tls_session_secret_cb is set and we're not doing TLSv1.3 then we are
  1598. * expecting a pre-shared key ciphersuite, in which case we have no use for
  1599. * session tickets and one will never be decrypted, nor will
  1600. * s->ext.ticket_expected be set to 1.
  1601. *
  1602. * Side effects:
  1603. * Sets s->ext.ticket_expected to 1 if the server will have to issue
  1604. * a new session ticket to the client because the client indicated support
  1605. * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
  1606. * a session ticket or we couldn't use the one it gave us, or if
  1607. * s->ctx->ext.ticket_key_cb asked to renew the client's ticket.
  1608. * Otherwise, s->ext.ticket_expected is set to 0.
  1609. *
  1610. * etick: points to the body of the session ticket extension.
  1611. * eticklen: the length of the session tickets extension.
  1612. * sess_id: points at the session ID.
  1613. * sesslen: the length of the session ID.
  1614. * psess: (output) on return, if a ticket was decrypted, then this is set to
  1615. * point to the resulting session.
  1616. */
  1617. SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick,
  1618. size_t eticklen, const unsigned char *sess_id,
  1619. size_t sesslen, SSL_SESSION **psess)
  1620. {
  1621. SSL_SESSION *sess = NULL;
  1622. unsigned char *sdec;
  1623. const unsigned char *p;
  1624. int slen, renew_ticket = 0, declen;
  1625. SSL_TICKET_STATUS ret = SSL_TICKET_FATAL_ERR_OTHER;
  1626. size_t mlen;
  1627. unsigned char tick_hmac[EVP_MAX_MD_SIZE];
  1628. SSL_HMAC *hctx = NULL;
  1629. EVP_CIPHER_CTX *ctx = NULL;
  1630. SSL_CTX *tctx = s->session_ctx;
  1631. if (eticklen == 0) {
  1632. /*
  1633. * The client will accept a ticket but doesn't currently have
  1634. * one (TLSv1.2 and below), or treated as a fatal error in TLSv1.3
  1635. */
  1636. ret = SSL_TICKET_EMPTY;
  1637. goto end;
  1638. }
  1639. if (!SSL_IS_TLS13(s) && s->ext.session_secret_cb) {
  1640. /*
  1641. * Indicate that the ticket couldn't be decrypted rather than
  1642. * generating the session from ticket now, trigger
  1643. * abbreviated handshake based on external mechanism to
  1644. * calculate the master secret later.
  1645. */
  1646. ret = SSL_TICKET_NO_DECRYPT;
  1647. goto end;
  1648. }
  1649. /* Need at least keyname + iv */
  1650. if (eticklen < TLSEXT_KEYNAME_LENGTH + EVP_MAX_IV_LENGTH) {
  1651. ret = SSL_TICKET_NO_DECRYPT;
  1652. goto end;
  1653. }
  1654. /* Initialize session ticket encryption and HMAC contexts */
  1655. hctx = ssl_hmac_new(tctx);
  1656. if (hctx == NULL) {
  1657. ret = SSL_TICKET_FATAL_ERR_MALLOC;
  1658. goto end;
  1659. }
  1660. ctx = EVP_CIPHER_CTX_new();
  1661. if (ctx == NULL) {
  1662. ret = SSL_TICKET_FATAL_ERR_MALLOC;
  1663. goto end;
  1664. }
  1665. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1666. if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL)
  1667. #else
  1668. if (tctx->ext.ticket_key_evp_cb != NULL)
  1669. #endif
  1670. {
  1671. unsigned char *nctick = (unsigned char *)etick;
  1672. int rv = 0;
  1673. if (tctx->ext.ticket_key_evp_cb != NULL)
  1674. rv = tctx->ext.ticket_key_evp_cb(s, nctick,
  1675. nctick + TLSEXT_KEYNAME_LENGTH,
  1676. ctx,
  1677. ssl_hmac_get0_EVP_MAC_CTX(hctx),
  1678. 0);
  1679. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1680. else if (tctx->ext.ticket_key_cb != NULL)
  1681. /* if 0 is returned, write an empty ticket */
  1682. rv = tctx->ext.ticket_key_cb(s, nctick,
  1683. nctick + TLSEXT_KEYNAME_LENGTH,
  1684. ctx, ssl_hmac_get0_HMAC_CTX(hctx), 0);
  1685. #endif
  1686. if (rv < 0) {
  1687. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1688. goto end;
  1689. }
  1690. if (rv == 0) {
  1691. ret = SSL_TICKET_NO_DECRYPT;
  1692. goto end;
  1693. }
  1694. if (rv == 2)
  1695. renew_ticket = 1;
  1696. } else {
  1697. EVP_CIPHER *aes256cbc = NULL;
  1698. /* Check key name matches */
  1699. if (memcmp(etick, tctx->ext.tick_key_name,
  1700. TLSEXT_KEYNAME_LENGTH) != 0) {
  1701. ret = SSL_TICKET_NO_DECRYPT;
  1702. goto end;
  1703. }
  1704. aes256cbc = EVP_CIPHER_fetch(s->ctx->libctx, "AES-256-CBC",
  1705. s->ctx->propq);
  1706. if (aes256cbc == NULL
  1707. || ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key,
  1708. sizeof(tctx->ext.secure->tick_hmac_key),
  1709. "SHA256") <= 0
  1710. || EVP_DecryptInit_ex(ctx, aes256cbc, NULL,
  1711. tctx->ext.secure->tick_aes_key,
  1712. etick + TLSEXT_KEYNAME_LENGTH) <= 0) {
  1713. EVP_CIPHER_free(aes256cbc);
  1714. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1715. goto end;
  1716. }
  1717. EVP_CIPHER_free(aes256cbc);
  1718. if (SSL_IS_TLS13(s))
  1719. renew_ticket = 1;
  1720. }
  1721. /*
  1722. * Attempt to process session ticket, first conduct sanity and integrity
  1723. * checks on ticket.
  1724. */
  1725. mlen = ssl_hmac_size(hctx);
  1726. if (mlen == 0) {
  1727. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1728. goto end;
  1729. }
  1730. /* Sanity check ticket length: must exceed keyname + IV + HMAC */
  1731. if (eticklen <=
  1732. TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) {
  1733. ret = SSL_TICKET_NO_DECRYPT;
  1734. goto end;
  1735. }
  1736. eticklen -= mlen;
  1737. /* Check HMAC of encrypted ticket */
  1738. if (ssl_hmac_update(hctx, etick, eticklen) <= 0
  1739. || ssl_hmac_final(hctx, tick_hmac, NULL, sizeof(tick_hmac)) <= 0) {
  1740. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1741. goto end;
  1742. }
  1743. if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
  1744. ret = SSL_TICKET_NO_DECRYPT;
  1745. goto end;
  1746. }
  1747. /* Attempt to decrypt session data */
  1748. /* Move p after IV to start of encrypted ticket, update length */
  1749. p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
  1750. eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
  1751. sdec = OPENSSL_malloc(eticklen);
  1752. if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p,
  1753. (int)eticklen) <= 0) {
  1754. OPENSSL_free(sdec);
  1755. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1756. goto end;
  1757. }
  1758. if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) {
  1759. OPENSSL_free(sdec);
  1760. ret = SSL_TICKET_NO_DECRYPT;
  1761. goto end;
  1762. }
  1763. slen += declen;
  1764. p = sdec;
  1765. sess = d2i_SSL_SESSION(NULL, &p, slen);
  1766. slen -= p - sdec;
  1767. OPENSSL_free(sdec);
  1768. if (sess) {
  1769. /* Some additional consistency checks */
  1770. if (slen != 0) {
  1771. SSL_SESSION_free(sess);
  1772. sess = NULL;
  1773. ret = SSL_TICKET_NO_DECRYPT;
  1774. goto end;
  1775. }
  1776. /*
  1777. * The session ID, if non-empty, is used by some clients to detect
  1778. * that the ticket has been accepted. So we copy it to the session
  1779. * structure. If it is empty set length to zero as required by
  1780. * standard.
  1781. */
  1782. if (sesslen) {
  1783. memcpy(sess->session_id, sess_id, sesslen);
  1784. sess->session_id_length = sesslen;
  1785. }
  1786. if (renew_ticket)
  1787. ret = SSL_TICKET_SUCCESS_RENEW;
  1788. else
  1789. ret = SSL_TICKET_SUCCESS;
  1790. goto end;
  1791. }
  1792. ERR_clear_error();
  1793. /*
  1794. * For session parse failure, indicate that we need to send a new ticket.
  1795. */
  1796. ret = SSL_TICKET_NO_DECRYPT;
  1797. end:
  1798. EVP_CIPHER_CTX_free(ctx);
  1799. ssl_hmac_free(hctx);
  1800. /*
  1801. * If set, the decrypt_ticket_cb() is called unless a fatal error was
  1802. * detected above. The callback is responsible for checking |ret| before it
  1803. * performs any action
  1804. */
  1805. if (s->session_ctx->decrypt_ticket_cb != NULL
  1806. && (ret == SSL_TICKET_EMPTY
  1807. || ret == SSL_TICKET_NO_DECRYPT
  1808. || ret == SSL_TICKET_SUCCESS
  1809. || ret == SSL_TICKET_SUCCESS_RENEW)) {
  1810. size_t keyname_len = eticklen;
  1811. int retcb;
  1812. if (keyname_len > TLSEXT_KEYNAME_LENGTH)
  1813. keyname_len = TLSEXT_KEYNAME_LENGTH;
  1814. retcb = s->session_ctx->decrypt_ticket_cb(s, sess, etick, keyname_len,
  1815. ret,
  1816. s->session_ctx->ticket_cb_data);
  1817. switch (retcb) {
  1818. case SSL_TICKET_RETURN_ABORT:
  1819. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1820. break;
  1821. case SSL_TICKET_RETURN_IGNORE:
  1822. ret = SSL_TICKET_NONE;
  1823. SSL_SESSION_free(sess);
  1824. sess = NULL;
  1825. break;
  1826. case SSL_TICKET_RETURN_IGNORE_RENEW:
  1827. if (ret != SSL_TICKET_EMPTY && ret != SSL_TICKET_NO_DECRYPT)
  1828. ret = SSL_TICKET_NO_DECRYPT;
  1829. /* else the value of |ret| will already do the right thing */
  1830. SSL_SESSION_free(sess);
  1831. sess = NULL;
  1832. break;
  1833. case SSL_TICKET_RETURN_USE:
  1834. case SSL_TICKET_RETURN_USE_RENEW:
  1835. if (ret != SSL_TICKET_SUCCESS
  1836. && ret != SSL_TICKET_SUCCESS_RENEW)
  1837. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1838. else if (retcb == SSL_TICKET_RETURN_USE)
  1839. ret = SSL_TICKET_SUCCESS;
  1840. else
  1841. ret = SSL_TICKET_SUCCESS_RENEW;
  1842. break;
  1843. default:
  1844. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1845. }
  1846. }
  1847. if (s->ext.session_secret_cb == NULL || SSL_IS_TLS13(s)) {
  1848. switch (ret) {
  1849. case SSL_TICKET_NO_DECRYPT:
  1850. case SSL_TICKET_SUCCESS_RENEW:
  1851. case SSL_TICKET_EMPTY:
  1852. s->ext.ticket_expected = 1;
  1853. }
  1854. }
  1855. *psess = sess;
  1856. return ret;
  1857. }
  1858. /* Check to see if a signature algorithm is allowed */
  1859. static int tls12_sigalg_allowed(const SSL *s, int op, const SIGALG_LOOKUP *lu)
  1860. {
  1861. unsigned char sigalgstr[2];
  1862. int secbits;
  1863. if (lu == NULL || !lu->enabled)
  1864. return 0;
  1865. /* DSA is not allowed in TLS 1.3 */
  1866. if (SSL_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA)
  1867. return 0;
  1868. /* TODO(OpenSSL1.2) fully axe DSA/etc. in ClientHello per TLS 1.3 spec */
  1869. if (!s->server && !SSL_IS_DTLS(s) && s->s3.tmp.min_ver >= TLS1_3_VERSION
  1870. && (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX
  1871. || lu->hash_idx == SSL_MD_MD5_IDX
  1872. || lu->hash_idx == SSL_MD_SHA224_IDX))
  1873. return 0;
  1874. /* See if public key algorithm allowed */
  1875. if (ssl_cert_is_disabled(lu->sig_idx))
  1876. return 0;
  1877. if (lu->sig == NID_id_GostR3410_2012_256
  1878. || lu->sig == NID_id_GostR3410_2012_512
  1879. || lu->sig == NID_id_GostR3410_2001) {
  1880. /* We never allow GOST sig algs on the server with TLSv1.3 */
  1881. if (s->server && SSL_IS_TLS13(s))
  1882. return 0;
  1883. if (!s->server
  1884. && s->method->version == TLS_ANY_VERSION
  1885. && s->s3.tmp.max_ver >= TLS1_3_VERSION) {
  1886. int i, num;
  1887. STACK_OF(SSL_CIPHER) *sk;
  1888. /*
  1889. * We're a client that could negotiate TLSv1.3. We only allow GOST
  1890. * sig algs if we could negotiate TLSv1.2 or below and we have GOST
  1891. * ciphersuites enabled.
  1892. */
  1893. if (s->s3.tmp.min_ver >= TLS1_3_VERSION)
  1894. return 0;
  1895. sk = SSL_get_ciphers(s);
  1896. num = sk != NULL ? sk_SSL_CIPHER_num(sk) : 0;
  1897. for (i = 0; i < num; i++) {
  1898. const SSL_CIPHER *c;
  1899. c = sk_SSL_CIPHER_value(sk, i);
  1900. /* Skip disabled ciphers */
  1901. if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))
  1902. continue;
  1903. if ((c->algorithm_mkey & (SSL_kGOST | SSL_kGOST18)) != 0)
  1904. break;
  1905. }
  1906. if (i == num)
  1907. return 0;
  1908. }
  1909. }
  1910. /* Finally see if security callback allows it */
  1911. secbits = sigalg_security_bits(s->ctx, lu);
  1912. sigalgstr[0] = (lu->sigalg >> 8) & 0xff;
  1913. sigalgstr[1] = lu->sigalg & 0xff;
  1914. return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr);
  1915. }
  1916. /*
  1917. * Get a mask of disabled public key algorithms based on supported signature
  1918. * algorithms. For example if no signature algorithm supports RSA then RSA is
  1919. * disabled.
  1920. */
  1921. void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
  1922. {
  1923. const uint16_t *sigalgs;
  1924. size_t i, sigalgslen;
  1925. uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA;
  1926. /*
  1927. * Go through all signature algorithms seeing if we support any
  1928. * in disabled_mask.
  1929. */
  1930. sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs);
  1931. for (i = 0; i < sigalgslen; i++, sigalgs++) {
  1932. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, *sigalgs);
  1933. const SSL_CERT_LOOKUP *clu;
  1934. if (lu == NULL)
  1935. continue;
  1936. clu = ssl_cert_lookup_by_idx(lu->sig_idx);
  1937. if (clu == NULL)
  1938. continue;
  1939. /* If algorithm is disabled see if we can enable it */
  1940. if ((clu->amask & disabled_mask) != 0
  1941. && tls12_sigalg_allowed(s, op, lu))
  1942. disabled_mask &= ~clu->amask;
  1943. }
  1944. *pmask_a |= disabled_mask;
  1945. }
  1946. int tls12_copy_sigalgs(SSL *s, WPACKET *pkt,
  1947. const uint16_t *psig, size_t psiglen)
  1948. {
  1949. size_t i;
  1950. int rv = 0;
  1951. for (i = 0; i < psiglen; i++, psig++) {
  1952. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, *psig);
  1953. if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu))
  1954. continue;
  1955. if (!WPACKET_put_bytes_u16(pkt, *psig))
  1956. return 0;
  1957. /*
  1958. * If TLS 1.3 must have at least one valid TLS 1.3 message
  1959. * signing algorithm: i.e. neither RSA nor SHA1/SHA224
  1960. */
  1961. if (rv == 0 && (!SSL_IS_TLS13(s)
  1962. || (lu->sig != EVP_PKEY_RSA
  1963. && lu->hash != NID_sha1
  1964. && lu->hash != NID_sha224)))
  1965. rv = 1;
  1966. }
  1967. if (rv == 0)
  1968. SSLerr(SSL_F_TLS12_COPY_SIGALGS, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  1969. return rv;
  1970. }
  1971. /* Given preference and allowed sigalgs set shared sigalgs */
  1972. static size_t tls12_shared_sigalgs(SSL *s, const SIGALG_LOOKUP **shsig,
  1973. const uint16_t *pref, size_t preflen,
  1974. const uint16_t *allow, size_t allowlen)
  1975. {
  1976. const uint16_t *ptmp, *atmp;
  1977. size_t i, j, nmatch = 0;
  1978. for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) {
  1979. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, *ptmp);
  1980. /* Skip disabled hashes or signature algorithms */
  1981. if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu))
  1982. continue;
  1983. for (j = 0, atmp = allow; j < allowlen; j++, atmp++) {
  1984. if (*ptmp == *atmp) {
  1985. nmatch++;
  1986. if (shsig)
  1987. *shsig++ = lu;
  1988. break;
  1989. }
  1990. }
  1991. }
  1992. return nmatch;
  1993. }
  1994. /* Set shared signature algorithms for SSL structures */
  1995. static int tls1_set_shared_sigalgs(SSL *s)
  1996. {
  1997. const uint16_t *pref, *allow, *conf;
  1998. size_t preflen, allowlen, conflen;
  1999. size_t nmatch;
  2000. const SIGALG_LOOKUP **salgs = NULL;
  2001. CERT *c = s->cert;
  2002. unsigned int is_suiteb = tls1_suiteb(s);
  2003. OPENSSL_free(s->shared_sigalgs);
  2004. s->shared_sigalgs = NULL;
  2005. s->shared_sigalgslen = 0;
  2006. /* If client use client signature algorithms if not NULL */
  2007. if (!s->server && c->client_sigalgs && !is_suiteb) {
  2008. conf = c->client_sigalgs;
  2009. conflen = c->client_sigalgslen;
  2010. } else if (c->conf_sigalgs && !is_suiteb) {
  2011. conf = c->conf_sigalgs;
  2012. conflen = c->conf_sigalgslen;
  2013. } else
  2014. conflen = tls12_get_psigalgs(s, 0, &conf);
  2015. if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
  2016. pref = conf;
  2017. preflen = conflen;
  2018. allow = s->s3.tmp.peer_sigalgs;
  2019. allowlen = s->s3.tmp.peer_sigalgslen;
  2020. } else {
  2021. allow = conf;
  2022. allowlen = conflen;
  2023. pref = s->s3.tmp.peer_sigalgs;
  2024. preflen = s->s3.tmp.peer_sigalgslen;
  2025. }
  2026. nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
  2027. if (nmatch) {
  2028. if ((salgs = OPENSSL_malloc(nmatch * sizeof(*salgs))) == NULL) {
  2029. SSLerr(SSL_F_TLS1_SET_SHARED_SIGALGS, ERR_R_MALLOC_FAILURE);
  2030. return 0;
  2031. }
  2032. nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
  2033. } else {
  2034. salgs = NULL;
  2035. }
  2036. s->shared_sigalgs = salgs;
  2037. s->shared_sigalgslen = nmatch;
  2038. return 1;
  2039. }
  2040. int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen)
  2041. {
  2042. unsigned int stmp;
  2043. size_t size, i;
  2044. uint16_t *buf;
  2045. size = PACKET_remaining(pkt);
  2046. /* Invalid data length */
  2047. if (size == 0 || (size & 1) != 0)
  2048. return 0;
  2049. size >>= 1;
  2050. if ((buf = OPENSSL_malloc(size * sizeof(*buf))) == NULL) {
  2051. SSLerr(SSL_F_TLS1_SAVE_U16, ERR_R_MALLOC_FAILURE);
  2052. return 0;
  2053. }
  2054. for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++)
  2055. buf[i] = stmp;
  2056. if (i != size) {
  2057. OPENSSL_free(buf);
  2058. return 0;
  2059. }
  2060. OPENSSL_free(*pdest);
  2061. *pdest = buf;
  2062. *pdestlen = size;
  2063. return 1;
  2064. }
  2065. int tls1_save_sigalgs(SSL *s, PACKET *pkt, int cert)
  2066. {
  2067. /* Extension ignored for inappropriate versions */
  2068. if (!SSL_USE_SIGALGS(s))
  2069. return 1;
  2070. /* Should never happen */
  2071. if (s->cert == NULL)
  2072. return 0;
  2073. if (cert)
  2074. return tls1_save_u16(pkt, &s->s3.tmp.peer_cert_sigalgs,
  2075. &s->s3.tmp.peer_cert_sigalgslen);
  2076. else
  2077. return tls1_save_u16(pkt, &s->s3.tmp.peer_sigalgs,
  2078. &s->s3.tmp.peer_sigalgslen);
  2079. }
  2080. /* Set preferred digest for each key type */
  2081. int tls1_process_sigalgs(SSL *s)
  2082. {
  2083. size_t i;
  2084. uint32_t *pvalid = s->s3.tmp.valid_flags;
  2085. if (!tls1_set_shared_sigalgs(s))
  2086. return 0;
  2087. for (i = 0; i < SSL_PKEY_NUM; i++)
  2088. pvalid[i] = 0;
  2089. for (i = 0; i < s->shared_sigalgslen; i++) {
  2090. const SIGALG_LOOKUP *sigptr = s->shared_sigalgs[i];
  2091. int idx = sigptr->sig_idx;
  2092. /* Ignore PKCS1 based sig algs in TLSv1.3 */
  2093. if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA)
  2094. continue;
  2095. /* If not disabled indicate we can explicitly sign */
  2096. if (pvalid[idx] == 0 && !ssl_cert_is_disabled(idx))
  2097. pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
  2098. }
  2099. return 1;
  2100. }
  2101. int SSL_get_sigalgs(SSL *s, int idx,
  2102. int *psign, int *phash, int *psignhash,
  2103. unsigned char *rsig, unsigned char *rhash)
  2104. {
  2105. uint16_t *psig = s->s3.tmp.peer_sigalgs;
  2106. size_t numsigalgs = s->s3.tmp.peer_sigalgslen;
  2107. if (psig == NULL || numsigalgs > INT_MAX)
  2108. return 0;
  2109. if (idx >= 0) {
  2110. const SIGALG_LOOKUP *lu;
  2111. if (idx >= (int)numsigalgs)
  2112. return 0;
  2113. psig += idx;
  2114. if (rhash != NULL)
  2115. *rhash = (unsigned char)((*psig >> 8) & 0xff);
  2116. if (rsig != NULL)
  2117. *rsig = (unsigned char)(*psig & 0xff);
  2118. lu = tls1_lookup_sigalg(s, *psig);
  2119. if (psign != NULL)
  2120. *psign = lu != NULL ? lu->sig : NID_undef;
  2121. if (phash != NULL)
  2122. *phash = lu != NULL ? lu->hash : NID_undef;
  2123. if (psignhash != NULL)
  2124. *psignhash = lu != NULL ? lu->sigandhash : NID_undef;
  2125. }
  2126. return (int)numsigalgs;
  2127. }
  2128. int SSL_get_shared_sigalgs(SSL *s, int idx,
  2129. int *psign, int *phash, int *psignhash,
  2130. unsigned char *rsig, unsigned char *rhash)
  2131. {
  2132. const SIGALG_LOOKUP *shsigalgs;
  2133. if (s->shared_sigalgs == NULL
  2134. || idx < 0
  2135. || idx >= (int)s->shared_sigalgslen
  2136. || s->shared_sigalgslen > INT_MAX)
  2137. return 0;
  2138. shsigalgs = s->shared_sigalgs[idx];
  2139. if (phash != NULL)
  2140. *phash = shsigalgs->hash;
  2141. if (psign != NULL)
  2142. *psign = shsigalgs->sig;
  2143. if (psignhash != NULL)
  2144. *psignhash = shsigalgs->sigandhash;
  2145. if (rsig != NULL)
  2146. *rsig = (unsigned char)(shsigalgs->sigalg & 0xff);
  2147. if (rhash != NULL)
  2148. *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff);
  2149. return (int)s->shared_sigalgslen;
  2150. }
  2151. /* Maximum possible number of unique entries in sigalgs array */
  2152. #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2)
  2153. typedef struct {
  2154. size_t sigalgcnt;
  2155. /* TLSEXT_SIGALG_XXX values */
  2156. uint16_t sigalgs[TLS_MAX_SIGALGCNT];
  2157. } sig_cb_st;
  2158. static void get_sigorhash(int *psig, int *phash, const char *str)
  2159. {
  2160. if (strcmp(str, "RSA") == 0) {
  2161. *psig = EVP_PKEY_RSA;
  2162. } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) {
  2163. *psig = EVP_PKEY_RSA_PSS;
  2164. } else if (strcmp(str, "DSA") == 0) {
  2165. *psig = EVP_PKEY_DSA;
  2166. } else if (strcmp(str, "ECDSA") == 0) {
  2167. *psig = EVP_PKEY_EC;
  2168. } else {
  2169. *phash = OBJ_sn2nid(str);
  2170. if (*phash == NID_undef)
  2171. *phash = OBJ_ln2nid(str);
  2172. }
  2173. }
  2174. /* Maximum length of a signature algorithm string component */
  2175. #define TLS_MAX_SIGSTRING_LEN 40
  2176. static int sig_cb(const char *elem, int len, void *arg)
  2177. {
  2178. sig_cb_st *sarg = arg;
  2179. size_t i;
  2180. const SIGALG_LOOKUP *s;
  2181. char etmp[TLS_MAX_SIGSTRING_LEN], *p;
  2182. int sig_alg = NID_undef, hash_alg = NID_undef;
  2183. if (elem == NULL)
  2184. return 0;
  2185. if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT)
  2186. return 0;
  2187. if (len > (int)(sizeof(etmp) - 1))
  2188. return 0;
  2189. memcpy(etmp, elem, len);
  2190. etmp[len] = 0;
  2191. p = strchr(etmp, '+');
  2192. /*
  2193. * We only allow SignatureSchemes listed in the sigalg_lookup_tbl;
  2194. * if there's no '+' in the provided name, look for the new-style combined
  2195. * name. If not, match both sig+hash to find the needed SIGALG_LOOKUP.
  2196. * Just sig+hash is not unique since TLS 1.3 adds rsa_pss_pss_* and
  2197. * rsa_pss_rsae_* that differ only by public key OID; in such cases
  2198. * we will pick the _rsae_ variant, by virtue of them appearing earlier
  2199. * in the table.
  2200. */
  2201. if (p == NULL) {
  2202. for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
  2203. i++, s++) {
  2204. if (s->name != NULL && strcmp(etmp, s->name) == 0) {
  2205. sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;
  2206. break;
  2207. }
  2208. }
  2209. if (i == OSSL_NELEM(sigalg_lookup_tbl))
  2210. return 0;
  2211. } else {
  2212. *p = 0;
  2213. p++;
  2214. if (*p == 0)
  2215. return 0;
  2216. get_sigorhash(&sig_alg, &hash_alg, etmp);
  2217. get_sigorhash(&sig_alg, &hash_alg, p);
  2218. if (sig_alg == NID_undef || hash_alg == NID_undef)
  2219. return 0;
  2220. for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
  2221. i++, s++) {
  2222. if (s->hash == hash_alg && s->sig == sig_alg) {
  2223. sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;
  2224. break;
  2225. }
  2226. }
  2227. if (i == OSSL_NELEM(sigalg_lookup_tbl))
  2228. return 0;
  2229. }
  2230. /* Reject duplicates */
  2231. for (i = 0; i < sarg->sigalgcnt - 1; i++) {
  2232. if (sarg->sigalgs[i] == sarg->sigalgs[sarg->sigalgcnt - 1]) {
  2233. sarg->sigalgcnt--;
  2234. return 0;
  2235. }
  2236. }
  2237. return 1;
  2238. }
  2239. /*
  2240. * Set supported signature algorithms based on a colon separated list of the
  2241. * form sig+hash e.g. RSA+SHA512:DSA+SHA512
  2242. */
  2243. int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
  2244. {
  2245. sig_cb_st sig;
  2246. sig.sigalgcnt = 0;
  2247. if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
  2248. return 0;
  2249. if (c == NULL)
  2250. return 1;
  2251. return tls1_set_raw_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
  2252. }
  2253. int tls1_set_raw_sigalgs(CERT *c, const uint16_t *psigs, size_t salglen,
  2254. int client)
  2255. {
  2256. uint16_t *sigalgs;
  2257. if ((sigalgs = OPENSSL_malloc(salglen * sizeof(*sigalgs))) == NULL) {
  2258. SSLerr(SSL_F_TLS1_SET_RAW_SIGALGS, ERR_R_MALLOC_FAILURE);
  2259. return 0;
  2260. }
  2261. memcpy(sigalgs, psigs, salglen * sizeof(*sigalgs));
  2262. if (client) {
  2263. OPENSSL_free(c->client_sigalgs);
  2264. c->client_sigalgs = sigalgs;
  2265. c->client_sigalgslen = salglen;
  2266. } else {
  2267. OPENSSL_free(c->conf_sigalgs);
  2268. c->conf_sigalgs = sigalgs;
  2269. c->conf_sigalgslen = salglen;
  2270. }
  2271. return 1;
  2272. }
  2273. int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
  2274. {
  2275. uint16_t *sigalgs, *sptr;
  2276. size_t i;
  2277. if (salglen & 1)
  2278. return 0;
  2279. if ((sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs))) == NULL) {
  2280. SSLerr(SSL_F_TLS1_SET_SIGALGS, ERR_R_MALLOC_FAILURE);
  2281. return 0;
  2282. }
  2283. for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
  2284. size_t j;
  2285. const SIGALG_LOOKUP *curr;
  2286. int md_id = *psig_nids++;
  2287. int sig_id = *psig_nids++;
  2288. for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl);
  2289. j++, curr++) {
  2290. if (curr->hash == md_id && curr->sig == sig_id) {
  2291. *sptr++ = curr->sigalg;
  2292. break;
  2293. }
  2294. }
  2295. if (j == OSSL_NELEM(sigalg_lookup_tbl))
  2296. goto err;
  2297. }
  2298. if (client) {
  2299. OPENSSL_free(c->client_sigalgs);
  2300. c->client_sigalgs = sigalgs;
  2301. c->client_sigalgslen = salglen / 2;
  2302. } else {
  2303. OPENSSL_free(c->conf_sigalgs);
  2304. c->conf_sigalgs = sigalgs;
  2305. c->conf_sigalgslen = salglen / 2;
  2306. }
  2307. return 1;
  2308. err:
  2309. OPENSSL_free(sigalgs);
  2310. return 0;
  2311. }
  2312. static int tls1_check_sig_alg(SSL *s, X509 *x, int default_nid)
  2313. {
  2314. int sig_nid, use_pc_sigalgs = 0;
  2315. size_t i;
  2316. const SIGALG_LOOKUP *sigalg;
  2317. size_t sigalgslen;
  2318. if (default_nid == -1)
  2319. return 1;
  2320. sig_nid = X509_get_signature_nid(x);
  2321. if (default_nid)
  2322. return sig_nid == default_nid ? 1 : 0;
  2323. if (SSL_IS_TLS13(s) && s->s3.tmp.peer_cert_sigalgs != NULL) {
  2324. /*
  2325. * If we're in TLSv1.3 then we only get here if we're checking the
  2326. * chain. If the peer has specified peer_cert_sigalgs then we use them
  2327. * otherwise we default to normal sigalgs.
  2328. */
  2329. sigalgslen = s->s3.tmp.peer_cert_sigalgslen;
  2330. use_pc_sigalgs = 1;
  2331. } else {
  2332. sigalgslen = s->shared_sigalgslen;
  2333. }
  2334. for (i = 0; i < sigalgslen; i++) {
  2335. sigalg = use_pc_sigalgs
  2336. ? tls1_lookup_sigalg(s, s->s3.tmp.peer_cert_sigalgs[i])
  2337. : s->shared_sigalgs[i];
  2338. if (sigalg != NULL && sig_nid == sigalg->sigandhash)
  2339. return 1;
  2340. }
  2341. return 0;
  2342. }
  2343. /* Check to see if a certificate issuer name matches list of CA names */
  2344. static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
  2345. {
  2346. const X509_NAME *nm;
  2347. int i;
  2348. nm = X509_get_issuer_name(x);
  2349. for (i = 0; i < sk_X509_NAME_num(names); i++) {
  2350. if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
  2351. return 1;
  2352. }
  2353. return 0;
  2354. }
  2355. /*
  2356. * Check certificate chain is consistent with TLS extensions and is usable by
  2357. * server. This servers two purposes: it allows users to check chains before
  2358. * passing them to the server and it allows the server to check chains before
  2359. * attempting to use them.
  2360. */
  2361. /* Flags which need to be set for a certificate when strict mode not set */
  2362. #define CERT_PKEY_VALID_FLAGS \
  2363. (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
  2364. /* Strict mode flags */
  2365. #define CERT_PKEY_STRICT_FLAGS \
  2366. (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
  2367. | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
  2368. int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
  2369. int idx)
  2370. {
  2371. int i;
  2372. int rv = 0;
  2373. int check_flags = 0, strict_mode;
  2374. CERT_PKEY *cpk = NULL;
  2375. CERT *c = s->cert;
  2376. uint32_t *pvalid;
  2377. unsigned int suiteb_flags = tls1_suiteb(s);
  2378. /* idx == -1 means checking server chains */
  2379. if (idx != -1) {
  2380. /* idx == -2 means checking client certificate chains */
  2381. if (idx == -2) {
  2382. cpk = c->key;
  2383. idx = (int)(cpk - c->pkeys);
  2384. } else
  2385. cpk = c->pkeys + idx;
  2386. pvalid = s->s3.tmp.valid_flags + idx;
  2387. x = cpk->x509;
  2388. pk = cpk->privatekey;
  2389. chain = cpk->chain;
  2390. strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
  2391. /* If no cert or key, forget it */
  2392. if (!x || !pk)
  2393. goto end;
  2394. } else {
  2395. size_t certidx;
  2396. if (!x || !pk)
  2397. return 0;
  2398. if (ssl_cert_lookup_by_pkey(pk, &certidx) == NULL)
  2399. return 0;
  2400. idx = certidx;
  2401. pvalid = s->s3.tmp.valid_flags + idx;
  2402. if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
  2403. check_flags = CERT_PKEY_STRICT_FLAGS;
  2404. else
  2405. check_flags = CERT_PKEY_VALID_FLAGS;
  2406. strict_mode = 1;
  2407. }
  2408. if (suiteb_flags) {
  2409. int ok;
  2410. if (check_flags)
  2411. check_flags |= CERT_PKEY_SUITEB;
  2412. ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
  2413. if (ok == X509_V_OK)
  2414. rv |= CERT_PKEY_SUITEB;
  2415. else if (!check_flags)
  2416. goto end;
  2417. }
  2418. /*
  2419. * Check all signature algorithms are consistent with signature
  2420. * algorithms extension if TLS 1.2 or later and strict mode.
  2421. */
  2422. if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
  2423. int default_nid;
  2424. int rsign = 0;
  2425. if (s->s3.tmp.peer_cert_sigalgs != NULL
  2426. || s->s3.tmp.peer_sigalgs != NULL) {
  2427. default_nid = 0;
  2428. /* If no sigalgs extension use defaults from RFC5246 */
  2429. } else {
  2430. switch (idx) {
  2431. case SSL_PKEY_RSA:
  2432. rsign = EVP_PKEY_RSA;
  2433. default_nid = NID_sha1WithRSAEncryption;
  2434. break;
  2435. case SSL_PKEY_DSA_SIGN:
  2436. rsign = EVP_PKEY_DSA;
  2437. default_nid = NID_dsaWithSHA1;
  2438. break;
  2439. case SSL_PKEY_ECC:
  2440. rsign = EVP_PKEY_EC;
  2441. default_nid = NID_ecdsa_with_SHA1;
  2442. break;
  2443. case SSL_PKEY_GOST01:
  2444. rsign = NID_id_GostR3410_2001;
  2445. default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
  2446. break;
  2447. case SSL_PKEY_GOST12_256:
  2448. rsign = NID_id_GostR3410_2012_256;
  2449. default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
  2450. break;
  2451. case SSL_PKEY_GOST12_512:
  2452. rsign = NID_id_GostR3410_2012_512;
  2453. default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
  2454. break;
  2455. default:
  2456. default_nid = -1;
  2457. break;
  2458. }
  2459. }
  2460. /*
  2461. * If peer sent no signature algorithms extension and we have set
  2462. * preferred signature algorithms check we support sha1.
  2463. */
  2464. if (default_nid > 0 && c->conf_sigalgs) {
  2465. size_t j;
  2466. const uint16_t *p = c->conf_sigalgs;
  2467. for (j = 0; j < c->conf_sigalgslen; j++, p++) {
  2468. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, *p);
  2469. if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign)
  2470. break;
  2471. }
  2472. if (j == c->conf_sigalgslen) {
  2473. if (check_flags)
  2474. goto skip_sigs;
  2475. else
  2476. goto end;
  2477. }
  2478. }
  2479. /* Check signature algorithm of each cert in chain */
  2480. if (SSL_IS_TLS13(s)) {
  2481. /*
  2482. * We only get here if the application has called SSL_check_chain(),
  2483. * so check_flags is always set.
  2484. */
  2485. if (find_sig_alg(s, x, pk) != NULL)
  2486. rv |= CERT_PKEY_EE_SIGNATURE;
  2487. } else if (!tls1_check_sig_alg(s, x, default_nid)) {
  2488. if (!check_flags)
  2489. goto end;
  2490. } else
  2491. rv |= CERT_PKEY_EE_SIGNATURE;
  2492. rv |= CERT_PKEY_CA_SIGNATURE;
  2493. for (i = 0; i < sk_X509_num(chain); i++) {
  2494. if (!tls1_check_sig_alg(s, sk_X509_value(chain, i), default_nid)) {
  2495. if (check_flags) {
  2496. rv &= ~CERT_PKEY_CA_SIGNATURE;
  2497. break;
  2498. } else
  2499. goto end;
  2500. }
  2501. }
  2502. }
  2503. /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
  2504. else if (check_flags)
  2505. rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
  2506. skip_sigs:
  2507. /* Check cert parameters are consistent */
  2508. if (tls1_check_cert_param(s, x, 1))
  2509. rv |= CERT_PKEY_EE_PARAM;
  2510. else if (!check_flags)
  2511. goto end;
  2512. if (!s->server)
  2513. rv |= CERT_PKEY_CA_PARAM;
  2514. /* In strict mode check rest of chain too */
  2515. else if (strict_mode) {
  2516. rv |= CERT_PKEY_CA_PARAM;
  2517. for (i = 0; i < sk_X509_num(chain); i++) {
  2518. X509 *ca = sk_X509_value(chain, i);
  2519. if (!tls1_check_cert_param(s, ca, 0)) {
  2520. if (check_flags) {
  2521. rv &= ~CERT_PKEY_CA_PARAM;
  2522. break;
  2523. } else
  2524. goto end;
  2525. }
  2526. }
  2527. }
  2528. if (!s->server && strict_mode) {
  2529. STACK_OF(X509_NAME) *ca_dn;
  2530. int check_type = 0;
  2531. if (EVP_PKEY_is_a(pk, "RSA"))
  2532. check_type = TLS_CT_RSA_SIGN;
  2533. else if (EVP_PKEY_is_a(pk, "DSA"))
  2534. check_type = TLS_CT_DSS_SIGN;
  2535. else if (EVP_PKEY_is_a(pk, "EC"))
  2536. check_type = TLS_CT_ECDSA_SIGN;
  2537. if (check_type) {
  2538. const uint8_t *ctypes = s->s3.tmp.ctype;
  2539. size_t j;
  2540. for (j = 0; j < s->s3.tmp.ctype_len; j++, ctypes++) {
  2541. if (*ctypes == check_type) {
  2542. rv |= CERT_PKEY_CERT_TYPE;
  2543. break;
  2544. }
  2545. }
  2546. if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
  2547. goto end;
  2548. } else {
  2549. rv |= CERT_PKEY_CERT_TYPE;
  2550. }
  2551. ca_dn = s->s3.tmp.peer_ca_names;
  2552. if (!sk_X509_NAME_num(ca_dn))
  2553. rv |= CERT_PKEY_ISSUER_NAME;
  2554. if (!(rv & CERT_PKEY_ISSUER_NAME)) {
  2555. if (ssl_check_ca_name(ca_dn, x))
  2556. rv |= CERT_PKEY_ISSUER_NAME;
  2557. }
  2558. if (!(rv & CERT_PKEY_ISSUER_NAME)) {
  2559. for (i = 0; i < sk_X509_num(chain); i++) {
  2560. X509 *xtmp = sk_X509_value(chain, i);
  2561. if (ssl_check_ca_name(ca_dn, xtmp)) {
  2562. rv |= CERT_PKEY_ISSUER_NAME;
  2563. break;
  2564. }
  2565. }
  2566. }
  2567. if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
  2568. goto end;
  2569. } else
  2570. rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
  2571. if (!check_flags || (rv & check_flags) == check_flags)
  2572. rv |= CERT_PKEY_VALID;
  2573. end:
  2574. if (TLS1_get_version(s) >= TLS1_2_VERSION)
  2575. rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN);
  2576. else
  2577. rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
  2578. /*
  2579. * When checking a CERT_PKEY structure all flags are irrelevant if the
  2580. * chain is invalid.
  2581. */
  2582. if (!check_flags) {
  2583. if (rv & CERT_PKEY_VALID) {
  2584. *pvalid = rv;
  2585. } else {
  2586. /* Preserve sign and explicit sign flag, clear rest */
  2587. *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
  2588. return 0;
  2589. }
  2590. }
  2591. return rv;
  2592. }
  2593. /* Set validity of certificates in an SSL structure */
  2594. void tls1_set_cert_validity(SSL *s)
  2595. {
  2596. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA);
  2597. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN);
  2598. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
  2599. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
  2600. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
  2601. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
  2602. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
  2603. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519);
  2604. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED448);
  2605. }
  2606. /* User level utility function to check a chain is suitable */
  2607. int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
  2608. {
  2609. return tls1_check_chain(s, x, pk, chain, -1);
  2610. }
  2611. #ifndef OPENSSL_NO_DH
  2612. DH *ssl_get_auto_dh(SSL *s)
  2613. {
  2614. DH *dhp;
  2615. BIGNUM *p, *g;
  2616. int dh_secbits = 80;
  2617. if (s->cert->dh_tmp_auto != 2) {
  2618. if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
  2619. if (s->s3.tmp.new_cipher->strength_bits == 256)
  2620. dh_secbits = 128;
  2621. else
  2622. dh_secbits = 80;
  2623. } else {
  2624. if (s->s3.tmp.cert == NULL)
  2625. return NULL;
  2626. dh_secbits = EVP_PKEY_security_bits(s->s3.tmp.cert->privatekey);
  2627. }
  2628. }
  2629. dhp = DH_new();
  2630. if (dhp == NULL)
  2631. return NULL;
  2632. g = BN_new();
  2633. if (g == NULL || !BN_set_word(g, 2)) {
  2634. DH_free(dhp);
  2635. BN_free(g);
  2636. return NULL;
  2637. }
  2638. if (dh_secbits >= 192)
  2639. p = BN_get_rfc3526_prime_8192(NULL);
  2640. else if (dh_secbits >= 152)
  2641. p = BN_get_rfc3526_prime_4096(NULL);
  2642. else if (dh_secbits >= 128)
  2643. p = BN_get_rfc3526_prime_3072(NULL);
  2644. else if (dh_secbits >= 112)
  2645. p = BN_get_rfc3526_prime_2048(NULL);
  2646. else
  2647. p = BN_get_rfc2409_prime_1024(NULL);
  2648. if (p == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
  2649. DH_free(dhp);
  2650. BN_free(p);
  2651. BN_free(g);
  2652. return NULL;
  2653. }
  2654. return dhp;
  2655. }
  2656. #endif
  2657. static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
  2658. {
  2659. int secbits = -1;
  2660. EVP_PKEY *pkey = X509_get0_pubkey(x);
  2661. if (pkey) {
  2662. /*
  2663. * If no parameters this will return -1 and fail using the default
  2664. * security callback for any non-zero security level. This will
  2665. * reject keys which omit parameters but this only affects DSA and
  2666. * omission of parameters is never (?) done in practice.
  2667. */
  2668. secbits = EVP_PKEY_security_bits(pkey);
  2669. }
  2670. if (s)
  2671. return ssl_security(s, op, secbits, 0, x);
  2672. else
  2673. return ssl_ctx_security(ctx, op, secbits, 0, x);
  2674. }
  2675. static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
  2676. {
  2677. /* Lookup signature algorithm digest */
  2678. int secbits, nid, pknid;
  2679. /* Don't check signature if self signed */
  2680. if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
  2681. return 1;
  2682. if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL))
  2683. secbits = -1;
  2684. /* If digest NID not defined use signature NID */
  2685. if (nid == NID_undef)
  2686. nid = pknid;
  2687. if (s)
  2688. return ssl_security(s, op, secbits, nid, x);
  2689. else
  2690. return ssl_ctx_security(ctx, op, secbits, nid, x);
  2691. }
  2692. int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
  2693. {
  2694. if (vfy)
  2695. vfy = SSL_SECOP_PEER;
  2696. if (is_ee) {
  2697. if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
  2698. return SSL_R_EE_KEY_TOO_SMALL;
  2699. } else {
  2700. if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
  2701. return SSL_R_CA_KEY_TOO_SMALL;
  2702. }
  2703. if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
  2704. return SSL_R_CA_MD_TOO_WEAK;
  2705. return 1;
  2706. }
  2707. /*
  2708. * Check security of a chain, if |sk| includes the end entity certificate then
  2709. * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending
  2710. * one to the peer. Return values: 1 if ok otherwise error code to use
  2711. */
  2712. int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
  2713. {
  2714. int rv, start_idx, i;
  2715. if (x == NULL) {
  2716. x = sk_X509_value(sk, 0);
  2717. start_idx = 1;
  2718. } else
  2719. start_idx = 0;
  2720. rv = ssl_security_cert(s, NULL, x, vfy, 1);
  2721. if (rv != 1)
  2722. return rv;
  2723. for (i = start_idx; i < sk_X509_num(sk); i++) {
  2724. x = sk_X509_value(sk, i);
  2725. rv = ssl_security_cert(s, NULL, x, vfy, 0);
  2726. if (rv != 1)
  2727. return rv;
  2728. }
  2729. return 1;
  2730. }
  2731. /*
  2732. * For TLS 1.2 servers check if we have a certificate which can be used
  2733. * with the signature algorithm "lu" and return index of certificate.
  2734. */
  2735. static int tls12_get_cert_sigalg_idx(const SSL *s, const SIGALG_LOOKUP *lu)
  2736. {
  2737. int sig_idx = lu->sig_idx;
  2738. const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx);
  2739. /* If not recognised or not supported by cipher mask it is not suitable */
  2740. if (clu == NULL
  2741. || (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0
  2742. || (clu->nid == EVP_PKEY_RSA_PSS
  2743. && (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kRSA) != 0))
  2744. return -1;
  2745. return s->s3.tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1;
  2746. }
  2747. /*
  2748. * Checks the given cert against signature_algorithm_cert restrictions sent by
  2749. * the peer (if any) as well as whether the hash from the sigalg is usable with
  2750. * the key.
  2751. * Returns true if the cert is usable and false otherwise.
  2752. */
  2753. static int check_cert_usable(SSL *s, const SIGALG_LOOKUP *sig, X509 *x,
  2754. EVP_PKEY *pkey)
  2755. {
  2756. const SIGALG_LOOKUP *lu;
  2757. int mdnid, pknid, supported;
  2758. size_t i;
  2759. /*
  2760. * If the given EVP_PKEY cannot supporting signing with this sigalg,
  2761. * the answer is simply 'no'.
  2762. */
  2763. ERR_set_mark();
  2764. supported = EVP_PKEY_supports_digest_nid(pkey, sig->hash);
  2765. ERR_pop_to_mark();
  2766. if (supported == 0)
  2767. return 0;
  2768. /*
  2769. * The TLS 1.3 signature_algorithms_cert extension places restrictions
  2770. * on the sigalg with which the certificate was signed (by its issuer).
  2771. */
  2772. if (s->s3.tmp.peer_cert_sigalgs != NULL) {
  2773. if (!X509_get_signature_info(x, &mdnid, &pknid, NULL, NULL))
  2774. return 0;
  2775. for (i = 0; i < s->s3.tmp.peer_cert_sigalgslen; i++) {
  2776. lu = tls1_lookup_sigalg(s, s->s3.tmp.peer_cert_sigalgs[i]);
  2777. if (lu == NULL)
  2778. continue;
  2779. /*
  2780. * TODO this does not differentiate between the
  2781. * rsa_pss_pss_* and rsa_pss_rsae_* schemes since we do not
  2782. * have a chain here that lets us look at the key OID in the
  2783. * signing certificate.
  2784. */
  2785. if (mdnid == lu->hash && pknid == lu->sig)
  2786. return 1;
  2787. }
  2788. return 0;
  2789. }
  2790. /*
  2791. * Without signat_algorithms_cert, any certificate for which we have
  2792. * a viable public key is permitted.
  2793. */
  2794. return 1;
  2795. }
  2796. /*
  2797. * Returns true if |s| has a usable certificate configured for use
  2798. * with signature scheme |sig|.
  2799. * "Usable" includes a check for presence as well as applying
  2800. * the signature_algorithm_cert restrictions sent by the peer (if any).
  2801. * Returns false if no usable certificate is found.
  2802. */
  2803. static int has_usable_cert(SSL *s, const SIGALG_LOOKUP *sig, int idx)
  2804. {
  2805. /* TLS 1.2 callers can override sig->sig_idx, but not TLS 1.3 callers. */
  2806. if (idx == -1)
  2807. idx = sig->sig_idx;
  2808. if (!ssl_has_cert(s, idx))
  2809. return 0;
  2810. return check_cert_usable(s, sig, s->cert->pkeys[idx].x509,
  2811. s->cert->pkeys[idx].privatekey);
  2812. }
  2813. /*
  2814. * Returns true if the supplied cert |x| and key |pkey| is usable with the
  2815. * specified signature scheme |sig|, or false otherwise.
  2816. */
  2817. static int is_cert_usable(SSL *s, const SIGALG_LOOKUP *sig, X509 *x,
  2818. EVP_PKEY *pkey)
  2819. {
  2820. size_t idx;
  2821. if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL)
  2822. return 0;
  2823. /* Check the key is consistent with the sig alg */
  2824. if ((int)idx != sig->sig_idx)
  2825. return 0;
  2826. return check_cert_usable(s, sig, x, pkey);
  2827. }
  2828. /*
  2829. * Find a signature scheme that works with the supplied certificate |x| and key
  2830. * |pkey|. |x| and |pkey| may be NULL in which case we additionally look at our
  2831. * available certs/keys to find one that works.
  2832. */
  2833. static const SIGALG_LOOKUP *find_sig_alg(SSL *s, X509 *x, EVP_PKEY *pkey)
  2834. {
  2835. const SIGALG_LOOKUP *lu = NULL;
  2836. size_t i;
  2837. #ifndef OPENSSL_NO_EC
  2838. int curve = -1;
  2839. #endif
  2840. EVP_PKEY *tmppkey;
  2841. /* Look for a shared sigalgs matching possible certificates */
  2842. for (i = 0; i < s->shared_sigalgslen; i++) {
  2843. lu = s->shared_sigalgs[i];
  2844. /* Skip SHA1, SHA224, DSA and RSA if not PSS */
  2845. if (lu->hash == NID_sha1
  2846. || lu->hash == NID_sha224
  2847. || lu->sig == EVP_PKEY_DSA
  2848. || lu->sig == EVP_PKEY_RSA)
  2849. continue;
  2850. /* Check that we have a cert, and signature_algorithms_cert */
  2851. if (!tls1_lookup_md(s->ctx, lu, NULL))
  2852. continue;
  2853. if ((pkey == NULL && !has_usable_cert(s, lu, -1))
  2854. || (pkey != NULL && !is_cert_usable(s, lu, x, pkey)))
  2855. continue;
  2856. tmppkey = (pkey != NULL) ? pkey
  2857. : s->cert->pkeys[lu->sig_idx].privatekey;
  2858. if (lu->sig == EVP_PKEY_EC) {
  2859. #ifndef OPENSSL_NO_EC
  2860. if (curve == -1)
  2861. curve = evp_pkey_get_EC_KEY_curve_nid(tmppkey);
  2862. if (lu->curve != NID_undef && curve != lu->curve)
  2863. continue;
  2864. #else
  2865. continue;
  2866. #endif
  2867. } else if (lu->sig == EVP_PKEY_RSA_PSS) {
  2868. /* validate that key is large enough for the signature algorithm */
  2869. if (!rsa_pss_check_min_key_size(s->ctx, tmppkey, lu))
  2870. continue;
  2871. }
  2872. break;
  2873. }
  2874. if (i == s->shared_sigalgslen)
  2875. return NULL;
  2876. return lu;
  2877. }
  2878. /*
  2879. * Choose an appropriate signature algorithm based on available certificates
  2880. * Sets chosen certificate and signature algorithm.
  2881. *
  2882. * For servers if we fail to find a required certificate it is a fatal error,
  2883. * an appropriate error code is set and a TLS alert is sent.
  2884. *
  2885. * For clients fatalerrs is set to 0. If a certificate is not suitable it is not
  2886. * a fatal error: we will either try another certificate or not present one
  2887. * to the server. In this case no error is set.
  2888. */
  2889. int tls_choose_sigalg(SSL *s, int fatalerrs)
  2890. {
  2891. const SIGALG_LOOKUP *lu = NULL;
  2892. int sig_idx = -1;
  2893. s->s3.tmp.cert = NULL;
  2894. s->s3.tmp.sigalg = NULL;
  2895. if (SSL_IS_TLS13(s)) {
  2896. lu = find_sig_alg(s, NULL, NULL);
  2897. if (lu == NULL) {
  2898. if (!fatalerrs)
  2899. return 1;
  2900. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_CHOOSE_SIGALG,
  2901. SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  2902. return 0;
  2903. }
  2904. } else {
  2905. /* If ciphersuite doesn't require a cert nothing to do */
  2906. if (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aCERT))
  2907. return 1;
  2908. if (!s->server && !ssl_has_cert(s, s->cert->key - s->cert->pkeys))
  2909. return 1;
  2910. if (SSL_USE_SIGALGS(s)) {
  2911. size_t i;
  2912. if (s->s3.tmp.peer_sigalgs != NULL) {
  2913. #ifndef OPENSSL_NO_EC
  2914. int curve = -1;
  2915. /* For Suite B need to match signature algorithm to curve */
  2916. if (tls1_suiteb(s))
  2917. curve =
  2918. evp_pkey_get_EC_KEY_curve_nid(s->cert->pkeys[SSL_PKEY_ECC]
  2919. .privatekey);
  2920. #endif
  2921. /*
  2922. * Find highest preference signature algorithm matching
  2923. * cert type
  2924. */
  2925. for (i = 0; i < s->shared_sigalgslen; i++) {
  2926. lu = s->shared_sigalgs[i];
  2927. if (s->server) {
  2928. if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1)
  2929. continue;
  2930. } else {
  2931. int cc_idx = s->cert->key - s->cert->pkeys;
  2932. sig_idx = lu->sig_idx;
  2933. if (cc_idx != sig_idx)
  2934. continue;
  2935. }
  2936. /* Check that we have a cert, and sig_algs_cert */
  2937. if (!has_usable_cert(s, lu, sig_idx))
  2938. continue;
  2939. if (lu->sig == EVP_PKEY_RSA_PSS) {
  2940. /* validate that key is large enough for the signature algorithm */
  2941. EVP_PKEY *pkey = s->cert->pkeys[sig_idx].privatekey;
  2942. if (!rsa_pss_check_min_key_size(s->ctx, pkey, lu))
  2943. continue;
  2944. }
  2945. #ifndef OPENSSL_NO_EC
  2946. if (curve == -1 || lu->curve == curve)
  2947. #endif
  2948. break;
  2949. }
  2950. #ifndef OPENSSL_NO_GOST
  2951. /*
  2952. * Some Windows-based implementations do not send GOST algorithms indication
  2953. * in supported_algorithms extension, so when we have GOST-based ciphersuite,
  2954. * we have to assume GOST support.
  2955. */
  2956. if (i == s->shared_sigalgslen && s->s3.tmp.new_cipher->algorithm_auth & (SSL_aGOST01 | SSL_aGOST12)) {
  2957. if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
  2958. if (!fatalerrs)
  2959. return 1;
  2960. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2961. SSL_F_TLS_CHOOSE_SIGALG,
  2962. SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  2963. return 0;
  2964. } else {
  2965. i = 0;
  2966. sig_idx = lu->sig_idx;
  2967. }
  2968. }
  2969. #endif
  2970. if (i == s->shared_sigalgslen) {
  2971. if (!fatalerrs)
  2972. return 1;
  2973. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2974. SSL_F_TLS_CHOOSE_SIGALG,
  2975. SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  2976. return 0;
  2977. }
  2978. } else {
  2979. /*
  2980. * If we have no sigalg use defaults
  2981. */
  2982. const uint16_t *sent_sigs;
  2983. size_t sent_sigslen;
  2984. if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
  2985. if (!fatalerrs)
  2986. return 1;
  2987. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG,
  2988. SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  2989. return 0;
  2990. }
  2991. /* Check signature matches a type we sent */
  2992. sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
  2993. for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
  2994. if (lu->sigalg == *sent_sigs
  2995. && has_usable_cert(s, lu, lu->sig_idx))
  2996. break;
  2997. }
  2998. if (i == sent_sigslen) {
  2999. if (!fatalerrs)
  3000. return 1;
  3001. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  3002. SSL_F_TLS_CHOOSE_SIGALG,
  3003. SSL_R_WRONG_SIGNATURE_TYPE);
  3004. return 0;
  3005. }
  3006. }
  3007. } else {
  3008. if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
  3009. if (!fatalerrs)
  3010. return 1;
  3011. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG,
  3012. SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  3013. return 0;
  3014. }
  3015. }
  3016. }
  3017. if (sig_idx == -1)
  3018. sig_idx = lu->sig_idx;
  3019. s->s3.tmp.cert = &s->cert->pkeys[sig_idx];
  3020. s->cert->key = s->s3.tmp.cert;
  3021. s->s3.tmp.sigalg = lu;
  3022. return 1;
  3023. }
  3024. int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode)
  3025. {
  3026. if (mode != TLSEXT_max_fragment_length_DISABLED
  3027. && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) {
  3028. SSLerr(SSL_F_SSL_CTX_SET_TLSEXT_MAX_FRAGMENT_LENGTH,
  3029. SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  3030. return 0;
  3031. }
  3032. ctx->ext.max_fragment_len_mode = mode;
  3033. return 1;
  3034. }
  3035. int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode)
  3036. {
  3037. if (mode != TLSEXT_max_fragment_length_DISABLED
  3038. && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) {
  3039. SSLerr(SSL_F_SSL_SET_TLSEXT_MAX_FRAGMENT_LENGTH,
  3040. SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  3041. return 0;
  3042. }
  3043. ssl->ext.max_fragment_len_mode = mode;
  3044. return 1;
  3045. }
  3046. uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session)
  3047. {
  3048. return session->ext.max_fragment_len_mode;
  3049. }
  3050. /*
  3051. * Helper functions for HMAC access with legacy support included.
  3052. */
  3053. SSL_HMAC *ssl_hmac_new(const SSL_CTX *ctx)
  3054. {
  3055. SSL_HMAC *ret = OPENSSL_zalloc(sizeof(*ret));
  3056. EVP_MAC *mac = NULL;
  3057. if (ret == NULL)
  3058. return NULL;
  3059. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3060. if (ctx->ext.ticket_key_evp_cb == NULL
  3061. && ctx->ext.ticket_key_cb != NULL) {
  3062. ret->old_ctx = HMAC_CTX_new();
  3063. if (ret->old_ctx == NULL)
  3064. goto err;
  3065. return ret;
  3066. }
  3067. #endif
  3068. mac = EVP_MAC_fetch(ctx->libctx, "HMAC", NULL);
  3069. if (mac == NULL || (ret->ctx = EVP_MAC_new_ctx(mac)) == NULL)
  3070. goto err;
  3071. EVP_MAC_free(mac);
  3072. return ret;
  3073. err:
  3074. EVP_MAC_free_ctx(ret->ctx);
  3075. EVP_MAC_free(mac);
  3076. OPENSSL_free(ret);
  3077. return NULL;
  3078. }
  3079. void ssl_hmac_free(SSL_HMAC *ctx)
  3080. {
  3081. if (ctx != NULL) {
  3082. EVP_MAC_free_ctx(ctx->ctx);
  3083. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3084. HMAC_CTX_free(ctx->old_ctx);
  3085. #endif
  3086. OPENSSL_free(ctx);
  3087. }
  3088. }
  3089. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3090. HMAC_CTX *ssl_hmac_get0_HMAC_CTX(SSL_HMAC *ctx)
  3091. {
  3092. return ctx->old_ctx;
  3093. }
  3094. #endif
  3095. EVP_MAC_CTX *ssl_hmac_get0_EVP_MAC_CTX(SSL_HMAC *ctx)
  3096. {
  3097. return ctx->ctx;
  3098. }
  3099. int ssl_hmac_init(SSL_HMAC *ctx, void *key, size_t len, char *md)
  3100. {
  3101. OSSL_PARAM params[3], *p = params;
  3102. if (ctx->ctx != NULL) {
  3103. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, md, 0);
  3104. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, key, len);
  3105. *p = OSSL_PARAM_construct_end();
  3106. if (EVP_MAC_set_ctx_params(ctx->ctx, params) && EVP_MAC_init(ctx->ctx))
  3107. return 1;
  3108. }
  3109. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3110. if (ctx->old_ctx != NULL)
  3111. return HMAC_Init_ex(ctx->old_ctx, key, len,
  3112. EVP_get_digestbyname(md), NULL);
  3113. #endif
  3114. return 0;
  3115. }
  3116. int ssl_hmac_update(SSL_HMAC *ctx, const unsigned char *data, size_t len)
  3117. {
  3118. if (ctx->ctx != NULL)
  3119. return EVP_MAC_update(ctx->ctx, data, len);
  3120. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3121. if (ctx->old_ctx != NULL)
  3122. return HMAC_Update(ctx->old_ctx, data, len);
  3123. #endif
  3124. return 0;
  3125. }
  3126. int ssl_hmac_final(SSL_HMAC *ctx, unsigned char *md, size_t *len,
  3127. size_t max_size)
  3128. {
  3129. if (ctx->ctx != NULL)
  3130. return EVP_MAC_final(ctx->ctx, md, len, max_size);
  3131. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3132. if (ctx->old_ctx != NULL) {
  3133. unsigned int l;
  3134. if (HMAC_Final(ctx->old_ctx, md, &l) > 0) {
  3135. if (len != NULL)
  3136. *len = l;
  3137. return 1;
  3138. }
  3139. }
  3140. #endif
  3141. return 0;
  3142. }
  3143. size_t ssl_hmac_size(const SSL_HMAC *ctx)
  3144. {
  3145. if (ctx->ctx != NULL)
  3146. return EVP_MAC_size(ctx->ctx);
  3147. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3148. if (ctx->old_ctx != NULL)
  3149. return HMAC_size(ctx->old_ctx);
  3150. #endif
  3151. return 0;
  3152. }