ssl_lib.cc 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  114. */
  115. /* ====================================================================
  116. * Copyright 2005 Nokia. All rights reserved.
  117. *
  118. * The portions of the attached software ("Contribution") is developed by
  119. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  120. * license.
  121. *
  122. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  123. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  124. * support (see RFC 4279) to OpenSSL.
  125. *
  126. * No patent licenses or other rights except those expressly stated in
  127. * the OpenSSL open source license shall be deemed granted or received
  128. * expressly, by implication, estoppel, or otherwise.
  129. *
  130. * No assurances are provided by Nokia that the Contribution does not
  131. * infringe the patent or other intellectual property rights of any third
  132. * party or that the license provides you with all the necessary rights
  133. * to make use of the Contribution.
  134. *
  135. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  136. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  137. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  138. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  139. * OTHERWISE. */
  140. #include <openssl/ssl.h>
  141. #include <assert.h>
  142. #include <stdlib.h>
  143. #include <string.h>
  144. #include <openssl/bytestring.h>
  145. #include <openssl/crypto.h>
  146. #include <openssl/err.h>
  147. #include <openssl/lhash.h>
  148. #include <openssl/mem.h>
  149. #include <openssl/rand.h>
  150. #include "internal.h"
  151. #include "../crypto/internal.h"
  152. #if defined(OPENSSL_WINDOWS)
  153. #include <sys/timeb.h>
  154. #else
  155. #include <sys/socket.h>
  156. #include <sys/time.h>
  157. #endif
  158. BSSL_NAMESPACE_BEGIN
  159. // |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
  160. // to avoid downstream churn.
  161. OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
  162. // The following errors are no longer emitted, but are used in nginx without
  163. // #ifdefs.
  164. OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG)
  165. OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED)
  166. // Some error codes are special. Ensure the make_errors.go script never
  167. // regresses this.
  168. static_assert(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
  169. SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
  170. "alert reason code mismatch");
  171. // kMaxHandshakeSize is the maximum size, in bytes, of a handshake message.
  172. static const size_t kMaxHandshakeSize = (1u << 24) - 1;
  173. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
  174. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  175. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
  176. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  177. bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out) {
  178. uint8_t *ptr;
  179. size_t len;
  180. if (!CBB_finish(cbb, &ptr, &len)) {
  181. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  182. return false;
  183. }
  184. out->Reset(ptr, len);
  185. return true;
  186. }
  187. void ssl_reset_error_state(SSL *ssl) {
  188. // Functions which use |SSL_get_error| must reset I/O and error state on
  189. // entry.
  190. ssl->s3->rwstate = SSL_ERROR_NONE;
  191. ERR_clear_error();
  192. ERR_clear_system_error();
  193. }
  194. void ssl_set_read_error(SSL* ssl) {
  195. ssl->s3->read_shutdown = ssl_shutdown_error;
  196. ssl->s3->read_error.reset(ERR_save_state());
  197. }
  198. static bool check_read_error(const SSL *ssl) {
  199. if (ssl->s3->read_shutdown == ssl_shutdown_error) {
  200. ERR_restore_state(ssl->s3->read_error.get());
  201. return false;
  202. }
  203. return true;
  204. }
  205. bool ssl_can_write(const SSL *ssl) {
  206. return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write;
  207. }
  208. bool ssl_can_read(const SSL *ssl) {
  209. return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read;
  210. }
  211. ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed,
  212. uint8_t *out_alert, Span<uint8_t> in) {
  213. *out_consumed = 0;
  214. if (!check_read_error(ssl)) {
  215. *out_alert = 0;
  216. return ssl_open_record_error;
  217. }
  218. auto ret = ssl->method->open_handshake(ssl, out_consumed, out_alert, in);
  219. if (ret == ssl_open_record_error) {
  220. ssl_set_read_error(ssl);
  221. }
  222. return ret;
  223. }
  224. ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
  225. uint8_t *out_alert,
  226. Span<uint8_t> in) {
  227. *out_consumed = 0;
  228. if (!check_read_error(ssl)) {
  229. *out_alert = 0;
  230. return ssl_open_record_error;
  231. }
  232. auto ret =
  233. ssl->method->open_change_cipher_spec(ssl, out_consumed, out_alert, in);
  234. if (ret == ssl_open_record_error) {
  235. ssl_set_read_error(ssl);
  236. }
  237. return ret;
  238. }
  239. ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out,
  240. size_t *out_consumed, uint8_t *out_alert,
  241. Span<uint8_t> in) {
  242. *out_consumed = 0;
  243. if (!check_read_error(ssl)) {
  244. *out_alert = 0;
  245. return ssl_open_record_error;
  246. }
  247. auto ret = ssl->method->open_app_data(ssl, out, out_consumed, out_alert, in);
  248. if (ret == ssl_open_record_error) {
  249. ssl_set_read_error(ssl);
  250. }
  251. return ret;
  252. }
  253. void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) {
  254. SSL *const ssl = hs->ssl;
  255. SSL_CTX *ctx = ssl->session_ctx.get();
  256. // Never cache sessions with empty session IDs.
  257. if (ssl->s3->established_session->session_id_length == 0 ||
  258. ssl->s3->established_session->not_resumable ||
  259. (ctx->session_cache_mode & mode) != mode) {
  260. return;
  261. }
  262. // Clients never use the internal session cache.
  263. int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
  264. SSL_SESS_CACHE_NO_INTERNAL_STORE);
  265. // A client may see new sessions on abbreviated handshakes if the server
  266. // decides to renew the ticket. Once the handshake is completed, it should be
  267. // inserted into the cache.
  268. if (ssl->s3->established_session.get() != ssl->session.get() ||
  269. (!ssl->server && hs->ticket_expected)) {
  270. if (use_internal_cache) {
  271. SSL_CTX_add_session(ctx, ssl->s3->established_session.get());
  272. }
  273. if (ctx->new_session_cb != NULL) {
  274. UniquePtr<SSL_SESSION> ref = UpRef(ssl->s3->established_session);
  275. if (ctx->new_session_cb(ssl, ref.get())) {
  276. // |new_session_cb|'s return value signals whether it took ownership.
  277. ref.release();
  278. }
  279. }
  280. }
  281. if (use_internal_cache &&
  282. !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
  283. // Automatically flush the internal session cache every 255 connections.
  284. int flush_cache = 0;
  285. CRYPTO_MUTEX_lock_write(&ctx->lock);
  286. ctx->handshakes_since_cache_flush++;
  287. if (ctx->handshakes_since_cache_flush >= 255) {
  288. flush_cache = 1;
  289. ctx->handshakes_since_cache_flush = 0;
  290. }
  291. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  292. if (flush_cache) {
  293. struct OPENSSL_timeval now;
  294. ssl_get_current_time(ssl, &now);
  295. SSL_CTX_flush_sessions(ctx, now.tv_sec);
  296. }
  297. }
  298. }
  299. static bool cbb_add_hex(CBB *cbb, Span<const uint8_t> in) {
  300. static const char hextable[] = "0123456789abcdef";
  301. uint8_t *out;
  302. if (!CBB_add_space(cbb, &out, in.size() * 2)) {
  303. return false;
  304. }
  305. for (uint8_t b : in) {
  306. *(out++) = (uint8_t)hextable[b >> 4];
  307. *(out++) = (uint8_t)hextable[b & 0xf];
  308. }
  309. return true;
  310. }
  311. bool ssl_log_secret(const SSL *ssl, const char *label,
  312. Span<const uint8_t> secret) {
  313. if (ssl->ctx->keylog_callback == NULL) {
  314. return true;
  315. }
  316. ScopedCBB cbb;
  317. Array<uint8_t> line;
  318. if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
  319. secret.size() * 2 + 1) ||
  320. !CBB_add_bytes(cbb.get(), reinterpret_cast<const uint8_t *>(label),
  321. strlen(label)) ||
  322. !CBB_add_u8(cbb.get(), ' ') ||
  323. !cbb_add_hex(cbb.get(), ssl->s3->client_random) ||
  324. !CBB_add_u8(cbb.get(), ' ') ||
  325. !cbb_add_hex(cbb.get(), secret) ||
  326. !CBB_add_u8(cbb.get(), 0 /* NUL */) ||
  327. !CBBFinishArray(cbb.get(), &line)) {
  328. return false;
  329. }
  330. ssl->ctx->keylog_callback(ssl, reinterpret_cast<const char *>(line.data()));
  331. return true;
  332. }
  333. void ssl_do_info_callback(const SSL *ssl, int type, int value) {
  334. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  335. if (ssl->info_callback != NULL) {
  336. cb = ssl->info_callback;
  337. } else if (ssl->ctx->info_callback != NULL) {
  338. cb = ssl->ctx->info_callback;
  339. }
  340. if (cb != NULL) {
  341. cb(ssl, type, value);
  342. }
  343. }
  344. void ssl_do_msg_callback(const SSL *ssl, int is_write, int content_type,
  345. Span<const uint8_t> in) {
  346. if (ssl->msg_callback == NULL) {
  347. return;
  348. }
  349. // |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
  350. // a V2ClientHello.
  351. int version;
  352. switch (content_type) {
  353. case 0:
  354. // V2ClientHello
  355. version = SSL2_VERSION;
  356. break;
  357. case SSL3_RT_HEADER:
  358. version = 0;
  359. break;
  360. default:
  361. version = SSL_version(ssl);
  362. }
  363. ssl->msg_callback(is_write, version, content_type, in.data(), in.size(),
  364. const_cast<SSL *>(ssl), ssl->msg_callback_arg);
  365. }
  366. void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock) {
  367. // TODO(martinkr): Change callers to |ssl_ctx_get_current_time| and drop the
  368. // |ssl| arg from |current_time_cb| if possible.
  369. ssl_ctx_get_current_time(ssl->ctx.get(), out_clock);
  370. }
  371. void ssl_ctx_get_current_time(const SSL_CTX *ctx,
  372. struct OPENSSL_timeval *out_clock) {
  373. if (ctx->current_time_cb != NULL) {
  374. // TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See
  375. // https://crbug.com/boringssl/155.
  376. struct timeval clock;
  377. ctx->current_time_cb(nullptr /* ssl */, &clock);
  378. if (clock.tv_sec < 0) {
  379. assert(0);
  380. out_clock->tv_sec = 0;
  381. out_clock->tv_usec = 0;
  382. } else {
  383. out_clock->tv_sec = (uint64_t)clock.tv_sec;
  384. out_clock->tv_usec = (uint32_t)clock.tv_usec;
  385. }
  386. return;
  387. }
  388. #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  389. out_clock->tv_sec = 1234;
  390. out_clock->tv_usec = 1234;
  391. #elif defined(OPENSSL_WINDOWS)
  392. struct _timeb time;
  393. _ftime(&time);
  394. if (time.time < 0) {
  395. assert(0);
  396. out_clock->tv_sec = 0;
  397. out_clock->tv_usec = 0;
  398. } else {
  399. out_clock->tv_sec = time.time;
  400. out_clock->tv_usec = time.millitm * 1000;
  401. }
  402. #else
  403. struct timeval clock;
  404. gettimeofday(&clock, NULL);
  405. if (clock.tv_sec < 0) {
  406. assert(0);
  407. out_clock->tv_sec = 0;
  408. out_clock->tv_usec = 0;
  409. } else {
  410. out_clock->tv_sec = (uint64_t)clock.tv_sec;
  411. out_clock->tv_usec = (uint32_t)clock.tv_usec;
  412. }
  413. #endif
  414. }
  415. void SSL_CTX_set_handoff_mode(SSL_CTX *ctx, bool on) {
  416. ctx->handoff = on;
  417. }
  418. static bool ssl_can_renegotiate(const SSL *ssl) {
  419. if (ssl->server || SSL_is_dtls(ssl)) {
  420. return false;
  421. }
  422. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  423. return false;
  424. }
  425. // The config has already been shed.
  426. if (!ssl->config) {
  427. return false;
  428. }
  429. switch (ssl->renegotiate_mode) {
  430. case ssl_renegotiate_ignore:
  431. case ssl_renegotiate_never:
  432. return false;
  433. case ssl_renegotiate_freely:
  434. return true;
  435. case ssl_renegotiate_once:
  436. return ssl->s3->total_renegotiations == 0;
  437. }
  438. assert(0);
  439. return false;
  440. }
  441. static void ssl_maybe_shed_handshake_config(SSL *ssl) {
  442. if (ssl->s3->hs != nullptr ||
  443. ssl->config == nullptr ||
  444. !ssl->config->shed_handshake_config ||
  445. ssl_can_renegotiate(ssl)) {
  446. return;
  447. }
  448. ssl->config.reset();
  449. }
  450. void SSL_set_handoff_mode(SSL *ssl, bool on) {
  451. if (!ssl->config) {
  452. return;
  453. }
  454. ssl->config->handoff = on;
  455. }
  456. bool SSL_get_traffic_secrets(const SSL *ssl,
  457. Span<const uint8_t> *out_read_traffic_secret,
  458. Span<const uint8_t> *out_write_traffic_secret) {
  459. if (SSL_version(ssl) < TLS1_3_VERSION) {
  460. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  461. return false;
  462. }
  463. if (!ssl->s3->initial_handshake_complete) {
  464. OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE);
  465. return false;
  466. }
  467. *out_read_traffic_secret = Span<const uint8_t>(
  468. ssl->s3->read_traffic_secret, ssl->s3->read_traffic_secret_len);
  469. *out_write_traffic_secret = Span<const uint8_t>(
  470. ssl->s3->write_traffic_secret, ssl->s3->write_traffic_secret_len);
  471. return true;
  472. }
  473. BSSL_NAMESPACE_END
  474. using namespace bssl;
  475. int SSL_library_init(void) {
  476. CRYPTO_library_init();
  477. return 1;
  478. }
  479. int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
  480. CRYPTO_library_init();
  481. return 1;
  482. }
  483. static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
  484. return ssl_hash_session_id(
  485. MakeConstSpan(sess->session_id, sess->session_id_length));
  486. }
  487. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  488. if (a->session_id_length != b->session_id_length) {
  489. return 1;
  490. }
  491. return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
  492. }
  493. ssl_ctx_st::ssl_ctx_st(const SSL_METHOD *ssl_method)
  494. : method(ssl_method->method),
  495. x509_method(ssl_method->x509_method),
  496. retain_only_sha256_of_client_certs(false),
  497. quiet_shutdown(false),
  498. ocsp_stapling_enabled(false),
  499. signed_cert_timestamps_enabled(false),
  500. channel_id_enabled(false),
  501. grease_enabled(false),
  502. allow_unknown_alpn_protos(false),
  503. ed25519_enabled(false),
  504. rsa_pss_rsae_certs_enabled(true),
  505. false_start_allowed_without_alpn(false),
  506. ignore_tls13_downgrade(false),
  507. handoff(false),
  508. enable_early_data(false),
  509. pq_experiment_signal(false) {
  510. CRYPTO_MUTEX_init(&lock);
  511. CRYPTO_new_ex_data(&ex_data);
  512. }
  513. ssl_ctx_st::~ssl_ctx_st() {
  514. // Free the internal session cache. Note that this calls the caller-supplied
  515. // remove callback, so we must do it before clearing ex_data. (See ticket
  516. // [openssl.org #212].)
  517. SSL_CTX_flush_sessions(this, 0);
  518. CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, this, &ex_data);
  519. CRYPTO_MUTEX_cleanup(&lock);
  520. lh_SSL_SESSION_free(sessions);
  521. x509_method->ssl_ctx_free(this);
  522. }
  523. SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
  524. if (method == NULL) {
  525. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  526. return nullptr;
  527. }
  528. UniquePtr<SSL_CTX> ret = MakeUnique<SSL_CTX>(method);
  529. if (!ret) {
  530. return nullptr;
  531. }
  532. ret->cert = MakeUnique<CERT>(method->x509_method);
  533. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  534. ret->client_CA.reset(sk_CRYPTO_BUFFER_new_null());
  535. if (ret->cert == nullptr ||
  536. ret->sessions == nullptr ||
  537. ret->client_CA == nullptr ||
  538. !ret->x509_method->ssl_ctx_new(ret.get())) {
  539. return nullptr;
  540. }
  541. if (!SSL_CTX_set_strict_cipher_list(ret.get(), SSL_DEFAULT_CIPHER_LIST) ||
  542. // Lock the SSL_CTX to the specified version, for compatibility with
  543. // legacy uses of SSL_METHOD.
  544. !SSL_CTX_set_max_proto_version(ret.get(), method->version) ||
  545. !SSL_CTX_set_min_proto_version(ret.get(), method->version)) {
  546. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  547. return nullptr;
  548. }
  549. return ret.release();
  550. }
  551. int SSL_CTX_up_ref(SSL_CTX *ctx) {
  552. CRYPTO_refcount_inc(&ctx->references);
  553. return 1;
  554. }
  555. void SSL_CTX_free(SSL_CTX *ctx) {
  556. if (ctx == NULL ||
  557. !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
  558. return;
  559. }
  560. ctx->~ssl_ctx_st();
  561. OPENSSL_free(ctx);
  562. }
  563. ssl_st::ssl_st(SSL_CTX *ctx_arg)
  564. : method(ctx_arg->method),
  565. max_send_fragment(ctx_arg->max_send_fragment),
  566. msg_callback(ctx_arg->msg_callback),
  567. msg_callback_arg(ctx_arg->msg_callback_arg),
  568. ctx(UpRef(ctx_arg)),
  569. session_ctx(UpRef(ctx_arg)),
  570. options(ctx->options),
  571. mode(ctx->mode),
  572. max_cert_list(ctx->max_cert_list),
  573. server(false),
  574. quiet_shutdown(ctx->quiet_shutdown),
  575. enable_early_data(ctx->enable_early_data) {
  576. CRYPTO_new_ex_data(&ex_data);
  577. }
  578. ssl_st::~ssl_st() {
  579. CRYPTO_free_ex_data(&g_ex_data_class_ssl, this, &ex_data);
  580. // |config| refers to |this|, so we must release it earlier.
  581. config.reset();
  582. if (method != NULL) {
  583. method->ssl_free(this);
  584. }
  585. }
  586. SSL *SSL_new(SSL_CTX *ctx) {
  587. if (ctx == nullptr) {
  588. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
  589. return nullptr;
  590. }
  591. UniquePtr<SSL> ssl = MakeUnique<SSL>(ctx);
  592. if (ssl == nullptr) {
  593. return nullptr;
  594. }
  595. ssl->config = MakeUnique<SSL_CONFIG>(ssl.get());
  596. if (ssl->config == nullptr) {
  597. return nullptr;
  598. }
  599. ssl->config->conf_min_version = ctx->conf_min_version;
  600. ssl->config->conf_max_version = ctx->conf_max_version;
  601. ssl->config->cert = ssl_cert_dup(ctx->cert.get());
  602. if (ssl->config->cert == nullptr) {
  603. return nullptr;
  604. }
  605. ssl->config->verify_mode = ctx->verify_mode;
  606. ssl->config->verify_callback = ctx->default_verify_callback;
  607. ssl->config->custom_verify_callback = ctx->custom_verify_callback;
  608. ssl->config->retain_only_sha256_of_client_certs =
  609. ctx->retain_only_sha256_of_client_certs;
  610. if (!ssl->config->supported_group_list.CopyFrom(ctx->supported_group_list) ||
  611. !ssl->config->alpn_client_proto_list.CopyFrom(
  612. ctx->alpn_client_proto_list) ||
  613. !ssl->config->verify_sigalgs.CopyFrom(ctx->verify_sigalgs)) {
  614. return nullptr;
  615. }
  616. if (ctx->psk_identity_hint) {
  617. ssl->config->psk_identity_hint.reset(
  618. BUF_strdup(ctx->psk_identity_hint.get()));
  619. if (ssl->config->psk_identity_hint == nullptr) {
  620. return nullptr;
  621. }
  622. }
  623. ssl->config->psk_client_callback = ctx->psk_client_callback;
  624. ssl->config->psk_server_callback = ctx->psk_server_callback;
  625. ssl->config->channel_id_enabled = ctx->channel_id_enabled;
  626. ssl->config->channel_id_private = UpRef(ctx->channel_id_private);
  627. ssl->config->signed_cert_timestamps_enabled =
  628. ctx->signed_cert_timestamps_enabled;
  629. ssl->config->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
  630. ssl->config->handoff = ctx->handoff;
  631. ssl->config->ignore_tls13_downgrade = ctx->ignore_tls13_downgrade;
  632. ssl->quic_method = ctx->quic_method;
  633. if (!ssl->method->ssl_new(ssl.get()) ||
  634. !ssl->ctx->x509_method->ssl_new(ssl->s3->hs.get())) {
  635. return nullptr;
  636. }
  637. return ssl.release();
  638. }
  639. SSL_CONFIG::SSL_CONFIG(SSL *ssl_arg)
  640. : ssl(ssl_arg),
  641. signed_cert_timestamps_enabled(false),
  642. ocsp_stapling_enabled(false),
  643. channel_id_enabled(false),
  644. enforce_rsa_key_usage(false),
  645. retain_only_sha256_of_client_certs(false),
  646. handoff(false),
  647. shed_handshake_config(false),
  648. ignore_tls13_downgrade(false),
  649. jdk11_workaround(false) {
  650. assert(ssl);
  651. }
  652. SSL_CONFIG::~SSL_CONFIG() {
  653. if (ssl->ctx != nullptr) {
  654. ssl->ctx->x509_method->ssl_config_free(this);
  655. }
  656. }
  657. void SSL_free(SSL *ssl) {
  658. Delete(ssl);
  659. }
  660. void SSL_set_connect_state(SSL *ssl) {
  661. ssl->server = false;
  662. ssl->do_handshake = ssl_client_handshake;
  663. }
  664. void SSL_set_accept_state(SSL *ssl) {
  665. ssl->server = true;
  666. ssl->do_handshake = ssl_server_handshake;
  667. }
  668. void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
  669. ssl->rbio.reset(rbio);
  670. }
  671. void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
  672. ssl->wbio.reset(wbio);
  673. }
  674. void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
  675. // For historical reasons, this function has many different cases in ownership
  676. // handling.
  677. // If nothing has changed, do nothing
  678. if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
  679. return;
  680. }
  681. // If the two arguments are equal, one fewer reference is granted than
  682. // taken.
  683. if (rbio != NULL && rbio == wbio) {
  684. BIO_up_ref(rbio);
  685. }
  686. // If only the wbio is changed, adopt only one reference.
  687. if (rbio == SSL_get_rbio(ssl)) {
  688. SSL_set0_wbio(ssl, wbio);
  689. return;
  690. }
  691. // There is an asymmetry here for historical reasons. If only the rbio is
  692. // changed AND the rbio and wbio were originally different, then we only adopt
  693. // one reference.
  694. if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
  695. SSL_set0_rbio(ssl, rbio);
  696. return;
  697. }
  698. // Otherwise, adopt both references.
  699. SSL_set0_rbio(ssl, rbio);
  700. SSL_set0_wbio(ssl, wbio);
  701. }
  702. BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio.get(); }
  703. BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio.get(); }
  704. size_t SSL_quic_max_handshake_flight_len(const SSL *ssl,
  705. enum ssl_encryption_level_t level) {
  706. // Limits flights to 16K by default when there are no large
  707. // (certificate-carrying) messages.
  708. static const size_t kDefaultLimit = 16384;
  709. switch (level) {
  710. case ssl_encryption_initial:
  711. return kDefaultLimit;
  712. case ssl_encryption_early_data:
  713. // QUIC does not send EndOfEarlyData.
  714. return 0;
  715. case ssl_encryption_handshake:
  716. if (ssl->server) {
  717. // Servers may receive Certificate message if configured to request
  718. // client certificates.
  719. if (!!(ssl->config->verify_mode & SSL_VERIFY_PEER) &&
  720. ssl->max_cert_list > kDefaultLimit) {
  721. return ssl->max_cert_list;
  722. }
  723. } else {
  724. // Clients may receive both Certificate message and a CertificateRequest
  725. // message.
  726. if (2*ssl->max_cert_list > kDefaultLimit) {
  727. return 2*ssl->max_cert_list;
  728. }
  729. }
  730. return kDefaultLimit;
  731. case ssl_encryption_application:
  732. // Note there is not actually a bound on the number of NewSessionTickets
  733. // one may send in a row. This level may need more involved flow
  734. // control. See https://github.com/quicwg/base-drafts/issues/1834.
  735. return kDefaultLimit;
  736. }
  737. return 0;
  738. }
  739. enum ssl_encryption_level_t SSL_quic_read_level(const SSL *ssl) {
  740. return ssl->s3->read_level;
  741. }
  742. enum ssl_encryption_level_t SSL_quic_write_level(const SSL *ssl) {
  743. return ssl->s3->write_level;
  744. }
  745. int SSL_provide_quic_data(SSL *ssl, enum ssl_encryption_level_t level,
  746. const uint8_t *data, size_t len) {
  747. if (ssl->quic_method == nullptr) {
  748. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  749. return 0;
  750. }
  751. if (level != ssl->s3->read_level) {
  752. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_ENCRYPTION_LEVEL_RECEIVED);
  753. return 0;
  754. }
  755. size_t new_len = (ssl->s3->hs_buf ? ssl->s3->hs_buf->length : 0) + len;
  756. if (new_len < len ||
  757. new_len > SSL_quic_max_handshake_flight_len(ssl, level)) {
  758. OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  759. return 0;
  760. }
  761. return tls_append_handshake_data(ssl, MakeConstSpan(data, len));
  762. }
  763. int SSL_do_handshake(SSL *ssl) {
  764. ssl_reset_error_state(ssl);
  765. if (ssl->do_handshake == NULL) {
  766. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  767. return -1;
  768. }
  769. if (!SSL_in_init(ssl)) {
  770. return 1;
  771. }
  772. // Run the handshake.
  773. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  774. bool early_return = false;
  775. int ret = ssl_run_handshake(hs, &early_return);
  776. ssl_do_info_callback(
  777. ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret);
  778. if (ret <= 0) {
  779. return ret;
  780. }
  781. // Destroy the handshake object if the handshake has completely finished.
  782. if (!early_return) {
  783. ssl->s3->hs.reset();
  784. ssl_maybe_shed_handshake_config(ssl);
  785. }
  786. return 1;
  787. }
  788. int SSL_connect(SSL *ssl) {
  789. if (ssl->do_handshake == NULL) {
  790. // Not properly initialized yet
  791. SSL_set_connect_state(ssl);
  792. }
  793. return SSL_do_handshake(ssl);
  794. }
  795. int SSL_accept(SSL *ssl) {
  796. if (ssl->do_handshake == NULL) {
  797. // Not properly initialized yet
  798. SSL_set_accept_state(ssl);
  799. }
  800. return SSL_do_handshake(ssl);
  801. }
  802. static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) {
  803. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  804. return tls13_post_handshake(ssl, msg);
  805. }
  806. // Check for renegotiation on the server before parsing to use the correct
  807. // error. Renegotiation is triggered by a different message for servers.
  808. if (ssl->server) {
  809. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  810. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
  811. return 0;
  812. }
  813. if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) {
  814. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  815. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
  816. return 0;
  817. }
  818. if (ssl->renegotiate_mode == ssl_renegotiate_ignore) {
  819. return 1; // Ignore the HelloRequest.
  820. }
  821. if (!ssl_can_renegotiate(ssl) ||
  822. // Renegotiation is only supported at quiescent points in the application
  823. // protocol, namely in HTTPS, just before reading the HTTP response.
  824. // Require the record-layer be idle and avoid complexities of sending a
  825. // handshake record while an application_data record is being written.
  826. !ssl->s3->write_buffer.empty() ||
  827. ssl->s3->write_shutdown != ssl_shutdown_none) {
  828. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  829. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
  830. return 0;
  831. }
  832. // Begin a new handshake.
  833. if (ssl->s3->hs != nullptr) {
  834. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  835. return 0;
  836. }
  837. ssl->s3->hs = ssl_handshake_new(ssl);
  838. if (ssl->s3->hs == nullptr) {
  839. return 0;
  840. }
  841. ssl->s3->total_renegotiations++;
  842. return 1;
  843. }
  844. int SSL_process_quic_post_handshake(SSL *ssl) {
  845. ssl_reset_error_state(ssl);
  846. if (SSL_in_init(ssl)) {
  847. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  848. return 0;
  849. }
  850. // Replay post-handshake message errors.
  851. if (!check_read_error(ssl)) {
  852. return 0;
  853. }
  854. // Process any buffered post-handshake messages.
  855. SSLMessage msg;
  856. while (ssl->method->get_message(ssl, &msg)) {
  857. // Handle the post-handshake message and try again.
  858. if (!ssl_do_post_handshake(ssl, msg)) {
  859. ssl_set_read_error(ssl);
  860. return 0;
  861. }
  862. ssl->method->next_message(ssl);
  863. }
  864. return 1;
  865. }
  866. static int ssl_read_impl(SSL *ssl) {
  867. ssl_reset_error_state(ssl);
  868. if (ssl->do_handshake == NULL) {
  869. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  870. return -1;
  871. }
  872. // Replay post-handshake message errors.
  873. if (!check_read_error(ssl)) {
  874. return -1;
  875. }
  876. while (ssl->s3->pending_app_data.empty()) {
  877. // Complete the current handshake, if any. False Start will cause
  878. // |SSL_do_handshake| to return mid-handshake, so this may require multiple
  879. // iterations.
  880. while (!ssl_can_read(ssl)) {
  881. int ret = SSL_do_handshake(ssl);
  882. if (ret < 0) {
  883. return ret;
  884. }
  885. if (ret == 0) {
  886. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  887. return -1;
  888. }
  889. }
  890. // Process any buffered post-handshake messages.
  891. SSLMessage msg;
  892. if (ssl->method->get_message(ssl, &msg)) {
  893. // If we received an interrupt in early read (EndOfEarlyData), loop again
  894. // for the handshake to process it.
  895. if (SSL_in_init(ssl)) {
  896. ssl->s3->hs->can_early_read = false;
  897. continue;
  898. }
  899. // Handle the post-handshake message and try again.
  900. if (!ssl_do_post_handshake(ssl, msg)) {
  901. ssl_set_read_error(ssl);
  902. return -1;
  903. }
  904. ssl->method->next_message(ssl);
  905. continue; // Loop again. We may have begun a new handshake.
  906. }
  907. uint8_t alert = SSL_AD_DECODE_ERROR;
  908. size_t consumed = 0;
  909. auto ret = ssl_open_app_data(ssl, &ssl->s3->pending_app_data, &consumed,
  910. &alert, ssl->s3->read_buffer.span());
  911. bool retry;
  912. int bio_ret = ssl_handle_open_record(ssl, &retry, ret, consumed, alert);
  913. if (bio_ret <= 0) {
  914. return bio_ret;
  915. }
  916. if (!retry) {
  917. assert(!ssl->s3->pending_app_data.empty());
  918. ssl->s3->key_update_count = 0;
  919. }
  920. }
  921. return 1;
  922. }
  923. int SSL_read(SSL *ssl, void *buf, int num) {
  924. int ret = SSL_peek(ssl, buf, num);
  925. if (ret <= 0) {
  926. return ret;
  927. }
  928. // TODO(davidben): In DTLS, should the rest of the record be discarded? DTLS
  929. // is not a stream. See https://crbug.com/boringssl/65.
  930. ssl->s3->pending_app_data =
  931. ssl->s3->pending_app_data.subspan(static_cast<size_t>(ret));
  932. if (ssl->s3->pending_app_data.empty()) {
  933. ssl->s3->read_buffer.DiscardConsumed();
  934. }
  935. return ret;
  936. }
  937. int SSL_peek(SSL *ssl, void *buf, int num) {
  938. if (ssl->quic_method != nullptr) {
  939. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  940. return 0;
  941. }
  942. int ret = ssl_read_impl(ssl);
  943. if (ret <= 0) {
  944. return ret;
  945. }
  946. if (num <= 0) {
  947. return num;
  948. }
  949. size_t todo =
  950. std::min(ssl->s3->pending_app_data.size(), static_cast<size_t>(num));
  951. OPENSSL_memcpy(buf, ssl->s3->pending_app_data.data(), todo);
  952. return static_cast<int>(todo);
  953. }
  954. int SSL_write(SSL *ssl, const void *buf, int num) {
  955. ssl_reset_error_state(ssl);
  956. if (ssl->quic_method != nullptr) {
  957. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  958. return 0;
  959. }
  960. if (ssl->do_handshake == NULL) {
  961. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  962. return -1;
  963. }
  964. if (ssl->s3->write_shutdown != ssl_shutdown_none) {
  965. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  966. return -1;
  967. }
  968. int ret = 0;
  969. bool needs_handshake = false;
  970. do {
  971. // If necessary, complete the handshake implicitly.
  972. if (!ssl_can_write(ssl)) {
  973. ret = SSL_do_handshake(ssl);
  974. if (ret < 0) {
  975. return ret;
  976. }
  977. if (ret == 0) {
  978. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  979. return -1;
  980. }
  981. }
  982. ret = ssl->method->write_app_data(ssl, &needs_handshake,
  983. (const uint8_t *)buf, num);
  984. } while (needs_handshake);
  985. return ret;
  986. }
  987. int SSL_key_update(SSL *ssl, int request_type) {
  988. ssl_reset_error_state(ssl);
  989. if (ssl->do_handshake == NULL) {
  990. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  991. return 0;
  992. }
  993. if (ssl->ctx->quic_method != nullptr) {
  994. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  995. return 0;
  996. }
  997. if (!ssl->s3->initial_handshake_complete) {
  998. OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE);
  999. return 0;
  1000. }
  1001. if (ssl_protocol_version(ssl) < TLS1_3_VERSION) {
  1002. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  1003. return 0;
  1004. }
  1005. if (!ssl->s3->key_update_pending &&
  1006. !tls13_add_key_update(ssl, request_type)) {
  1007. return 0;
  1008. }
  1009. return 1;
  1010. }
  1011. int SSL_shutdown(SSL *ssl) {
  1012. ssl_reset_error_state(ssl);
  1013. if (ssl->do_handshake == NULL) {
  1014. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  1015. return -1;
  1016. }
  1017. // If we are in the middle of a handshake, silently succeed. Consumers often
  1018. // call this function before |SSL_free|, whether the handshake succeeded or
  1019. // not. We assume the caller has already handled failed handshakes.
  1020. if (SSL_in_init(ssl)) {
  1021. return 1;
  1022. }
  1023. if (ssl->quiet_shutdown) {
  1024. // Do nothing if configured not to send a close_notify.
  1025. ssl->s3->write_shutdown = ssl_shutdown_close_notify;
  1026. ssl->s3->read_shutdown = ssl_shutdown_close_notify;
  1027. return 1;
  1028. }
  1029. // This function completes in two stages. It sends a close_notify and then it
  1030. // waits for a close_notify to come in. Perform exactly one action and return
  1031. // whether or not it succeeds.
  1032. if (ssl->s3->write_shutdown != ssl_shutdown_close_notify) {
  1033. // Send a close_notify.
  1034. if (ssl_send_alert_impl(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
  1035. return -1;
  1036. }
  1037. } else if (ssl->s3->alert_dispatch) {
  1038. // Finish sending the close_notify.
  1039. if (ssl->method->dispatch_alert(ssl) <= 0) {
  1040. return -1;
  1041. }
  1042. } else if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
  1043. if (SSL_is_dtls(ssl)) {
  1044. // Bidirectional shutdown doesn't make sense for an unordered
  1045. // transport. DTLS alerts also aren't delivered reliably, so we may even
  1046. // time out because the peer never received our close_notify. Report to
  1047. // the caller that the channel has fully shut down.
  1048. if (ssl->s3->read_shutdown == ssl_shutdown_error) {
  1049. ERR_restore_state(ssl->s3->read_error.get());
  1050. return -1;
  1051. }
  1052. ssl->s3->read_shutdown = ssl_shutdown_close_notify;
  1053. } else {
  1054. // Process records until an error, close_notify, or application data.
  1055. if (ssl_read_impl(ssl) > 0) {
  1056. // We received some unexpected application data.
  1057. OPENSSL_PUT_ERROR(SSL, SSL_R_APPLICATION_DATA_ON_SHUTDOWN);
  1058. return -1;
  1059. }
  1060. if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
  1061. return -1;
  1062. }
  1063. }
  1064. }
  1065. // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown.
  1066. return ssl->s3->read_shutdown == ssl_shutdown_close_notify;
  1067. }
  1068. int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
  1069. if (ssl->s3->alert_dispatch) {
  1070. if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
  1071. ssl->s3->send_alert[1] != alert) {
  1072. // We are already attempting to write a different alert.
  1073. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  1074. return -1;
  1075. }
  1076. return ssl->method->dispatch_alert(ssl);
  1077. }
  1078. return ssl_send_alert_impl(ssl, SSL3_AL_FATAL, alert);
  1079. }
  1080. void SSL_CTX_enable_pq_experiment_signal(SSL_CTX *ctx) {
  1081. ctx->pq_experiment_signal = true;
  1082. }
  1083. int SSL_pq_experiment_signal_seen(const SSL *ssl) {
  1084. return ssl->s3->pq_experiment_signal_seen;
  1085. }
  1086. int SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params,
  1087. size_t params_len) {
  1088. return ssl->config && ssl->config->quic_transport_params.CopyFrom(
  1089. MakeConstSpan(params, params_len));
  1090. }
  1091. void SSL_get_peer_quic_transport_params(const SSL *ssl,
  1092. const uint8_t **out_params,
  1093. size_t *out_params_len) {
  1094. *out_params = ssl->s3->peer_quic_transport_params.data();
  1095. *out_params_len = ssl->s3->peer_quic_transport_params.size();
  1096. }
  1097. void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
  1098. ctx->enable_early_data = !!enabled;
  1099. }
  1100. void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
  1101. ssl->enable_early_data = !!enabled;
  1102. }
  1103. int SSL_in_early_data(const SSL *ssl) {
  1104. if (ssl->s3->hs == NULL) {
  1105. return 0;
  1106. }
  1107. return ssl->s3->hs->in_early_data;
  1108. }
  1109. int SSL_early_data_accepted(const SSL *ssl) {
  1110. return ssl->s3->early_data_accepted;
  1111. }
  1112. void SSL_reset_early_data_reject(SSL *ssl) {
  1113. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  1114. if (hs == NULL ||
  1115. hs->wait != ssl_hs_early_data_rejected) {
  1116. abort();
  1117. }
  1118. hs->wait = ssl_hs_ok;
  1119. hs->in_early_data = false;
  1120. hs->early_session.reset();
  1121. // Discard any unfinished writes from the perspective of |SSL_write|'s
  1122. // retry. The handshake will transparently flush out the pending record
  1123. // (discarded by the server) to keep the framing correct.
  1124. ssl->s3->wpend_pending = false;
  1125. }
  1126. enum ssl_early_data_reason_t SSL_get_early_data_reason(const SSL *ssl) {
  1127. return ssl->s3->early_data_reason;
  1128. }
  1129. static int bio_retry_reason_to_error(int reason) {
  1130. switch (reason) {
  1131. case BIO_RR_CONNECT:
  1132. return SSL_ERROR_WANT_CONNECT;
  1133. case BIO_RR_ACCEPT:
  1134. return SSL_ERROR_WANT_ACCEPT;
  1135. default:
  1136. return SSL_ERROR_SYSCALL;
  1137. }
  1138. }
  1139. int SSL_get_error(const SSL *ssl, int ret_code) {
  1140. if (ret_code > 0) {
  1141. return SSL_ERROR_NONE;
  1142. }
  1143. // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  1144. // where we do encode the error
  1145. uint32_t err = ERR_peek_error();
  1146. if (err != 0) {
  1147. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  1148. return SSL_ERROR_SYSCALL;
  1149. }
  1150. return SSL_ERROR_SSL;
  1151. }
  1152. if (ret_code == 0) {
  1153. if (ssl->s3->read_shutdown == ssl_shutdown_close_notify) {
  1154. return SSL_ERROR_ZERO_RETURN;
  1155. }
  1156. // An EOF was observed which violates the protocol, and the underlying
  1157. // transport does not participate in the error queue. Bubble up to the
  1158. // caller.
  1159. return SSL_ERROR_SYSCALL;
  1160. }
  1161. switch (ssl->s3->rwstate) {
  1162. case SSL_ERROR_PENDING_SESSION:
  1163. case SSL_ERROR_PENDING_CERTIFICATE:
  1164. case SSL_ERROR_HANDOFF:
  1165. case SSL_ERROR_HANDBACK:
  1166. case SSL_ERROR_WANT_X509_LOOKUP:
  1167. case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP:
  1168. case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
  1169. case SSL_ERROR_PENDING_TICKET:
  1170. case SSL_ERROR_EARLY_DATA_REJECTED:
  1171. case SSL_ERROR_WANT_CERTIFICATE_VERIFY:
  1172. return ssl->s3->rwstate;
  1173. case SSL_ERROR_WANT_READ: {
  1174. if (ssl->quic_method) {
  1175. return SSL_ERROR_WANT_READ;
  1176. }
  1177. BIO *bio = SSL_get_rbio(ssl);
  1178. if (BIO_should_read(bio)) {
  1179. return SSL_ERROR_WANT_READ;
  1180. }
  1181. if (BIO_should_write(bio)) {
  1182. // TODO(davidben): OpenSSL historically checked for writes on the read
  1183. // BIO. Can this be removed?
  1184. return SSL_ERROR_WANT_WRITE;
  1185. }
  1186. if (BIO_should_io_special(bio)) {
  1187. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  1188. }
  1189. break;
  1190. }
  1191. case SSL_ERROR_WANT_WRITE: {
  1192. BIO *bio = SSL_get_wbio(ssl);
  1193. if (BIO_should_write(bio)) {
  1194. return SSL_ERROR_WANT_WRITE;
  1195. }
  1196. if (BIO_should_read(bio)) {
  1197. // TODO(davidben): OpenSSL historically checked for reads on the write
  1198. // BIO. Can this be removed?
  1199. return SSL_ERROR_WANT_READ;
  1200. }
  1201. if (BIO_should_io_special(bio)) {
  1202. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  1203. }
  1204. break;
  1205. }
  1206. }
  1207. return SSL_ERROR_SYSCALL;
  1208. }
  1209. const char *SSL_error_description(int err) {
  1210. switch (err) {
  1211. case SSL_ERROR_NONE:
  1212. return "NONE";
  1213. case SSL_ERROR_SSL:
  1214. return "SSL";
  1215. case SSL_ERROR_WANT_READ:
  1216. return "WANT_READ";
  1217. case SSL_ERROR_WANT_WRITE:
  1218. return "WANT_WRITE";
  1219. case SSL_ERROR_WANT_X509_LOOKUP:
  1220. return "WANT_X509_LOOKUP";
  1221. case SSL_ERROR_SYSCALL:
  1222. return "SYSCALL";
  1223. case SSL_ERROR_ZERO_RETURN:
  1224. return "ZERO_RETURN";
  1225. case SSL_ERROR_WANT_CONNECT:
  1226. return "WANT_CONNECT";
  1227. case SSL_ERROR_WANT_ACCEPT:
  1228. return "WANT_ACCEPT";
  1229. case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP:
  1230. return "WANT_CHANNEL_ID_LOOKUP";
  1231. case SSL_ERROR_PENDING_SESSION:
  1232. return "PENDING_SESSION";
  1233. case SSL_ERROR_PENDING_CERTIFICATE:
  1234. return "PENDING_CERTIFICATE";
  1235. case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
  1236. return "WANT_PRIVATE_KEY_OPERATION";
  1237. case SSL_ERROR_PENDING_TICKET:
  1238. return "PENDING_TICKET";
  1239. case SSL_ERROR_EARLY_DATA_REJECTED:
  1240. return "EARLY_DATA_REJECTED";
  1241. case SSL_ERROR_WANT_CERTIFICATE_VERIFY:
  1242. return "WANT_CERTIFICATE_VERIFY";
  1243. case SSL_ERROR_HANDOFF:
  1244. return "HANDOFF";
  1245. case SSL_ERROR_HANDBACK:
  1246. return "HANDBACK";
  1247. default:
  1248. return nullptr;
  1249. }
  1250. }
  1251. uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
  1252. ctx->options |= options;
  1253. return ctx->options;
  1254. }
  1255. uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
  1256. ctx->options &= ~options;
  1257. return ctx->options;
  1258. }
  1259. uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
  1260. uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
  1261. ssl->options |= options;
  1262. return ssl->options;
  1263. }
  1264. uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
  1265. ssl->options &= ~options;
  1266. return ssl->options;
  1267. }
  1268. uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
  1269. uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
  1270. ctx->mode |= mode;
  1271. return ctx->mode;
  1272. }
  1273. uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
  1274. ctx->mode &= ~mode;
  1275. return ctx->mode;
  1276. }
  1277. uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
  1278. uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
  1279. ssl->mode |= mode;
  1280. return ssl->mode;
  1281. }
  1282. uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
  1283. ssl->mode &= ~mode;
  1284. return ssl->mode;
  1285. }
  1286. uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
  1287. void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
  1288. ctx->pool = pool;
  1289. }
  1290. int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
  1291. size_t max_out) {
  1292. *out_len = 0;
  1293. OPENSSL_memset(out, 0, max_out);
  1294. // tls-unique is not defined for TLS 1.3.
  1295. if (!ssl->s3->initial_handshake_complete ||
  1296. ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1297. return 0;
  1298. }
  1299. // The tls-unique value is the first Finished message in the handshake, which
  1300. // is the client's in a full handshake and the server's for a resumption. See
  1301. // https://tools.ietf.org/html/rfc5929#section-3.1.
  1302. const uint8_t *finished = ssl->s3->previous_client_finished;
  1303. size_t finished_len = ssl->s3->previous_client_finished_len;
  1304. if (ssl->session != NULL) {
  1305. // tls-unique is broken for resumed sessions unless EMS is used.
  1306. if (!ssl->session->extended_master_secret) {
  1307. return 0;
  1308. }
  1309. finished = ssl->s3->previous_server_finished;
  1310. finished_len = ssl->s3->previous_server_finished_len;
  1311. }
  1312. *out_len = finished_len;
  1313. if (finished_len > max_out) {
  1314. *out_len = max_out;
  1315. }
  1316. OPENSSL_memcpy(out, finished, *out_len);
  1317. return 1;
  1318. }
  1319. static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
  1320. size_t sid_ctx_len) {
  1321. if (sid_ctx_len > sizeof(cert->sid_ctx)) {
  1322. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  1323. return 0;
  1324. }
  1325. static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large");
  1326. cert->sid_ctx_length = (uint8_t)sid_ctx_len;
  1327. OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
  1328. return 1;
  1329. }
  1330. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
  1331. size_t sid_ctx_len) {
  1332. return set_session_id_context(ctx->cert.get(), sid_ctx, sid_ctx_len);
  1333. }
  1334. int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
  1335. size_t sid_ctx_len) {
  1336. if (!ssl->config) {
  1337. return 0;
  1338. }
  1339. return set_session_id_context(ssl->config->cert.get(), sid_ctx, sid_ctx_len);
  1340. }
  1341. const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
  1342. if (!ssl->config) {
  1343. assert(ssl->config);
  1344. *out_len = 0;
  1345. return NULL;
  1346. }
  1347. *out_len = ssl->config->cert->sid_ctx_length;
  1348. return ssl->config->cert->sid_ctx;
  1349. }
  1350. void SSL_certs_clear(SSL *ssl) {
  1351. if (!ssl->config) {
  1352. return;
  1353. }
  1354. ssl_cert_clear_certs(ssl->config->cert.get());
  1355. }
  1356. int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
  1357. int SSL_get_rfd(const SSL *ssl) {
  1358. int ret = -1;
  1359. BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
  1360. if (b != NULL) {
  1361. BIO_get_fd(b, &ret);
  1362. }
  1363. return ret;
  1364. }
  1365. int SSL_get_wfd(const SSL *ssl) {
  1366. int ret = -1;
  1367. BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
  1368. if (b != NULL) {
  1369. BIO_get_fd(b, &ret);
  1370. }
  1371. return ret;
  1372. }
  1373. int SSL_set_fd(SSL *ssl, int fd) {
  1374. BIO *bio = BIO_new(BIO_s_socket());
  1375. if (bio == NULL) {
  1376. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1377. return 0;
  1378. }
  1379. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1380. SSL_set_bio(ssl, bio, bio);
  1381. return 1;
  1382. }
  1383. int SSL_set_wfd(SSL *ssl, int fd) {
  1384. BIO *rbio = SSL_get_rbio(ssl);
  1385. if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
  1386. BIO_get_fd(rbio, NULL) != fd) {
  1387. BIO *bio = BIO_new(BIO_s_socket());
  1388. if (bio == NULL) {
  1389. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1390. return 0;
  1391. }
  1392. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1393. SSL_set0_wbio(ssl, bio);
  1394. } else {
  1395. // Copy the rbio over to the wbio.
  1396. BIO_up_ref(rbio);
  1397. SSL_set0_wbio(ssl, rbio);
  1398. }
  1399. return 1;
  1400. }
  1401. int SSL_set_rfd(SSL *ssl, int fd) {
  1402. BIO *wbio = SSL_get_wbio(ssl);
  1403. if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
  1404. BIO_get_fd(wbio, NULL) != fd) {
  1405. BIO *bio = BIO_new(BIO_s_socket());
  1406. if (bio == NULL) {
  1407. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1408. return 0;
  1409. }
  1410. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1411. SSL_set0_rbio(ssl, bio);
  1412. } else {
  1413. // Copy the wbio over to the rbio.
  1414. BIO_up_ref(wbio);
  1415. SSL_set0_rbio(ssl, wbio);
  1416. }
  1417. return 1;
  1418. }
  1419. static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
  1420. size_t in_len) {
  1421. if (out_len > in_len) {
  1422. out_len = in_len;
  1423. }
  1424. OPENSSL_memcpy(out, in, out_len);
  1425. return in_len;
  1426. }
  1427. size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
  1428. if (!ssl->s3->initial_handshake_complete ||
  1429. ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1430. return 0;
  1431. }
  1432. if (ssl->server) {
  1433. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1434. ssl->s3->previous_server_finished_len);
  1435. }
  1436. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1437. ssl->s3->previous_client_finished_len);
  1438. }
  1439. size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
  1440. if (!ssl->s3->initial_handshake_complete ||
  1441. ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1442. return 0;
  1443. }
  1444. if (ssl->server) {
  1445. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1446. ssl->s3->previous_client_finished_len);
  1447. }
  1448. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1449. ssl->s3->previous_server_finished_len);
  1450. }
  1451. int SSL_get_verify_mode(const SSL *ssl) {
  1452. if (!ssl->config) {
  1453. assert(ssl->config);
  1454. return -1;
  1455. }
  1456. return ssl->config->verify_mode;
  1457. }
  1458. int SSL_get_extms_support(const SSL *ssl) {
  1459. // TLS 1.3 does not require extended master secret and always reports as
  1460. // supporting it.
  1461. if (!ssl->s3->have_version) {
  1462. return 0;
  1463. }
  1464. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1465. return 1;
  1466. }
  1467. // If the initial handshake completed, query the established session.
  1468. if (ssl->s3->established_session != NULL) {
  1469. return ssl->s3->established_session->extended_master_secret;
  1470. }
  1471. // Otherwise, query the in-progress handshake.
  1472. if (ssl->s3->hs != NULL) {
  1473. return ssl->s3->hs->extended_master_secret;
  1474. }
  1475. assert(0);
  1476. return 0;
  1477. }
  1478. int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
  1479. int SSL_get_read_ahead(const SSL *ssl) { return 0; }
  1480. int SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { return 1; }
  1481. int SSL_set_read_ahead(SSL *ssl, int yes) { return 1; }
  1482. int SSL_pending(const SSL *ssl) {
  1483. return static_cast<int>(ssl->s3->pending_app_data.size());
  1484. }
  1485. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  1486. return ssl_cert_check_private_key(ctx->cert.get(),
  1487. ctx->cert->privatekey.get());
  1488. }
  1489. int SSL_check_private_key(const SSL *ssl) {
  1490. if (!ssl->config) {
  1491. return 0;
  1492. }
  1493. return ssl_cert_check_private_key(ssl->config->cert.get(),
  1494. ssl->config->cert->privatekey.get());
  1495. }
  1496. long SSL_get_default_timeout(const SSL *ssl) {
  1497. return SSL_DEFAULT_SESSION_TIMEOUT;
  1498. }
  1499. int SSL_renegotiate(SSL *ssl) {
  1500. // Caller-initiated renegotiation is not supported.
  1501. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1502. return 0;
  1503. }
  1504. int SSL_renegotiate_pending(SSL *ssl) {
  1505. return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
  1506. }
  1507. int SSL_total_renegotiations(const SSL *ssl) {
  1508. return ssl->s3->total_renegotiations;
  1509. }
  1510. size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
  1511. return ctx->max_cert_list;
  1512. }
  1513. void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
  1514. if (max_cert_list > kMaxHandshakeSize) {
  1515. max_cert_list = kMaxHandshakeSize;
  1516. }
  1517. ctx->max_cert_list = (uint32_t)max_cert_list;
  1518. }
  1519. size_t SSL_get_max_cert_list(const SSL *ssl) {
  1520. return ssl->max_cert_list;
  1521. }
  1522. void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
  1523. if (max_cert_list > kMaxHandshakeSize) {
  1524. max_cert_list = kMaxHandshakeSize;
  1525. }
  1526. ssl->max_cert_list = (uint32_t)max_cert_list;
  1527. }
  1528. int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
  1529. if (max_send_fragment < 512) {
  1530. max_send_fragment = 512;
  1531. }
  1532. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1533. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1534. }
  1535. ctx->max_send_fragment = (uint16_t)max_send_fragment;
  1536. return 1;
  1537. }
  1538. int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
  1539. if (max_send_fragment < 512) {
  1540. max_send_fragment = 512;
  1541. }
  1542. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1543. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1544. }
  1545. ssl->max_send_fragment = (uint16_t)max_send_fragment;
  1546. return 1;
  1547. }
  1548. int SSL_set_mtu(SSL *ssl, unsigned mtu) {
  1549. if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
  1550. return 0;
  1551. }
  1552. ssl->d1->mtu = mtu;
  1553. return 1;
  1554. }
  1555. int SSL_get_secure_renegotiation_support(const SSL *ssl) {
  1556. if (!ssl->s3->have_version) {
  1557. return 0;
  1558. }
  1559. return ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
  1560. ssl->s3->send_connection_binding;
  1561. }
  1562. size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
  1563. MutexReadLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock));
  1564. return lh_SSL_SESSION_num_items(ctx->sessions);
  1565. }
  1566. unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
  1567. unsigned long ret = ctx->session_cache_size;
  1568. ctx->session_cache_size = size;
  1569. return ret;
  1570. }
  1571. unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
  1572. return ctx->session_cache_size;
  1573. }
  1574. int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
  1575. int ret = ctx->session_cache_mode;
  1576. ctx->session_cache_mode = mode;
  1577. return ret;
  1578. }
  1579. int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
  1580. return ctx->session_cache_mode;
  1581. }
  1582. int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
  1583. if (out == NULL) {
  1584. return 48;
  1585. }
  1586. if (len != 48) {
  1587. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1588. return 0;
  1589. }
  1590. // The default ticket keys are initialized lazily. Trigger a key
  1591. // rotation to initialize them.
  1592. if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
  1593. return 0;
  1594. }
  1595. uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out);
  1596. MutexReadLock lock(&ctx->lock);
  1597. OPENSSL_memcpy(out_bytes, ctx->ticket_key_current->name, 16);
  1598. OPENSSL_memcpy(out_bytes + 16, ctx->ticket_key_current->hmac_key, 16);
  1599. OPENSSL_memcpy(out_bytes + 32, ctx->ticket_key_current->aes_key, 16);
  1600. return 1;
  1601. }
  1602. int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
  1603. if (in == NULL) {
  1604. return 48;
  1605. }
  1606. if (len != 48) {
  1607. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1608. return 0;
  1609. }
  1610. auto key = MakeUnique<TicketKey>();
  1611. if (!key) {
  1612. return 0;
  1613. }
  1614. const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in);
  1615. OPENSSL_memcpy(key->name, in_bytes, 16);
  1616. OPENSSL_memcpy(key->hmac_key, in_bytes + 16, 16);
  1617. OPENSSL_memcpy(key->aes_key, in_bytes + 32, 16);
  1618. // Disable automatic key rotation for manually-configured keys. This is now
  1619. // the caller's responsibility.
  1620. key->next_rotation_tv_sec = 0;
  1621. ctx->ticket_key_current = std::move(key);
  1622. ctx->ticket_key_prev.reset();
  1623. return 1;
  1624. }
  1625. int SSL_CTX_set_tlsext_ticket_key_cb(
  1626. SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
  1627. EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
  1628. int encrypt)) {
  1629. ctx->ticket_key_cb = callback;
  1630. return 1;
  1631. }
  1632. int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
  1633. return tls1_set_curves(&ctx->supported_group_list,
  1634. MakeConstSpan(curves, curves_len));
  1635. }
  1636. int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
  1637. if (!ssl->config) {
  1638. return 0;
  1639. }
  1640. return tls1_set_curves(&ssl->config->supported_group_list,
  1641. MakeConstSpan(curves, curves_len));
  1642. }
  1643. int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
  1644. return tls1_set_curves_list(&ctx->supported_group_list, curves);
  1645. }
  1646. int SSL_set1_curves_list(SSL *ssl, const char *curves) {
  1647. if (!ssl->config) {
  1648. return 0;
  1649. }
  1650. return tls1_set_curves_list(&ssl->config->supported_group_list, curves);
  1651. }
  1652. uint16_t SSL_get_curve_id(const SSL *ssl) {
  1653. // TODO(davidben): This checks the wrong session if there is a renegotiation
  1654. // in progress.
  1655. SSL_SESSION *session = SSL_get_session(ssl);
  1656. if (session == NULL) {
  1657. return 0;
  1658. }
  1659. return session->group_id;
  1660. }
  1661. int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
  1662. return 1;
  1663. }
  1664. int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
  1665. return 1;
  1666. }
  1667. STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
  1668. return ctx->cipher_list->ciphers.get();
  1669. }
  1670. int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) {
  1671. if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers.get())) {
  1672. return 0;
  1673. }
  1674. return ctx->cipher_list->in_group_flags[i];
  1675. }
  1676. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
  1677. if (ssl == NULL) {
  1678. return NULL;
  1679. }
  1680. if (ssl->config == NULL) {
  1681. assert(ssl->config);
  1682. return NULL;
  1683. }
  1684. return ssl->config->cipher_list ? ssl->config->cipher_list->ciphers.get()
  1685. : ssl->ctx->cipher_list->ciphers.get();
  1686. }
  1687. const char *SSL_get_cipher_list(const SSL *ssl, int n) {
  1688. if (ssl == NULL) {
  1689. return NULL;
  1690. }
  1691. STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
  1692. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  1693. return NULL;
  1694. }
  1695. const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
  1696. if (c == NULL) {
  1697. return NULL;
  1698. }
  1699. return c->name;
  1700. }
  1701. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  1702. return ssl_create_cipher_list(&ctx->cipher_list, str, false /* not strict */);
  1703. }
  1704. int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
  1705. return ssl_create_cipher_list(&ctx->cipher_list, str, true /* strict */);
  1706. }
  1707. int SSL_set_cipher_list(SSL *ssl, const char *str) {
  1708. if (!ssl->config) {
  1709. return 0;
  1710. }
  1711. return ssl_create_cipher_list(&ssl->config->cipher_list, str,
  1712. false /* not strict */);
  1713. }
  1714. int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
  1715. if (!ssl->config) {
  1716. return 0;
  1717. }
  1718. return ssl_create_cipher_list(&ssl->config->cipher_list, str,
  1719. true /* strict */);
  1720. }
  1721. const char *SSL_get_servername(const SSL *ssl, const int type) {
  1722. if (type != TLSEXT_NAMETYPE_host_name) {
  1723. return NULL;
  1724. }
  1725. // Historically, |SSL_get_servername| was also the configuration getter
  1726. // corresponding to |SSL_set_tlsext_host_name|.
  1727. if (ssl->hostname != nullptr) {
  1728. return ssl->hostname.get();
  1729. }
  1730. return ssl->s3->hostname.get();
  1731. }
  1732. int SSL_get_servername_type(const SSL *ssl) {
  1733. if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == NULL) {
  1734. return -1;
  1735. }
  1736. return TLSEXT_NAMETYPE_host_name;
  1737. }
  1738. void SSL_CTX_set_custom_verify(
  1739. SSL_CTX *ctx, int mode,
  1740. enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
  1741. ctx->verify_mode = mode;
  1742. ctx->custom_verify_callback = callback;
  1743. }
  1744. void SSL_set_custom_verify(
  1745. SSL *ssl, int mode,
  1746. enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
  1747. if (!ssl->config) {
  1748. return;
  1749. }
  1750. ssl->config->verify_mode = mode;
  1751. ssl->config->custom_verify_callback = callback;
  1752. }
  1753. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1754. ctx->signed_cert_timestamps_enabled = true;
  1755. }
  1756. void SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1757. if (!ssl->config) {
  1758. return;
  1759. }
  1760. ssl->config->signed_cert_timestamps_enabled = true;
  1761. }
  1762. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1763. ctx->ocsp_stapling_enabled = true;
  1764. }
  1765. void SSL_enable_ocsp_stapling(SSL *ssl) {
  1766. if (!ssl->config) {
  1767. return;
  1768. }
  1769. ssl->config->ocsp_stapling_enabled = true;
  1770. }
  1771. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1772. size_t *out_len) {
  1773. SSL_SESSION *session = SSL_get_session(ssl);
  1774. if (ssl->server || !session || !session->signed_cert_timestamp_list) {
  1775. *out_len = 0;
  1776. *out = NULL;
  1777. return;
  1778. }
  1779. *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list.get());
  1780. *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list.get());
  1781. }
  1782. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1783. size_t *out_len) {
  1784. SSL_SESSION *session = SSL_get_session(ssl);
  1785. if (ssl->server || !session || !session->ocsp_response) {
  1786. *out_len = 0;
  1787. *out = NULL;
  1788. return;
  1789. }
  1790. *out = CRYPTO_BUFFER_data(session->ocsp_response.get());
  1791. *out_len = CRYPTO_BUFFER_len(session->ocsp_response.get());
  1792. }
  1793. int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
  1794. ssl->hostname.reset();
  1795. if (name == nullptr) {
  1796. return 1;
  1797. }
  1798. size_t len = strlen(name);
  1799. if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
  1800. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
  1801. return 0;
  1802. }
  1803. ssl->hostname.reset(BUF_strdup(name));
  1804. if (ssl->hostname == nullptr) {
  1805. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1806. return 0;
  1807. }
  1808. return 1;
  1809. }
  1810. int SSL_CTX_set_tlsext_servername_callback(
  1811. SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
  1812. ctx->servername_callback = callback;
  1813. return 1;
  1814. }
  1815. int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
  1816. ctx->servername_arg = arg;
  1817. return 1;
  1818. }
  1819. int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer,
  1820. unsigned peer_len, const uint8_t *supported,
  1821. unsigned supported_len) {
  1822. const uint8_t *result;
  1823. int status;
  1824. // For each protocol in peer preference order, see if we support it.
  1825. for (unsigned i = 0; i < peer_len;) {
  1826. for (unsigned j = 0; j < supported_len;) {
  1827. if (peer[i] == supported[j] &&
  1828. OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) {
  1829. // We found a match
  1830. result = &peer[i];
  1831. status = OPENSSL_NPN_NEGOTIATED;
  1832. goto found;
  1833. }
  1834. j += supported[j];
  1835. j++;
  1836. }
  1837. i += peer[i];
  1838. i++;
  1839. }
  1840. // There's no overlap between our protocols and the peer's list.
  1841. result = supported;
  1842. status = OPENSSL_NPN_NO_OVERLAP;
  1843. found:
  1844. *out = (uint8_t *)result + 1;
  1845. *out_len = result[0];
  1846. return status;
  1847. }
  1848. void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
  1849. unsigned *out_len) {
  1850. *out_data = ssl->s3->next_proto_negotiated.data();
  1851. *out_len = ssl->s3->next_proto_negotiated.size();
  1852. }
  1853. void SSL_CTX_set_next_protos_advertised_cb(
  1854. SSL_CTX *ctx,
  1855. int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
  1856. void *arg) {
  1857. ctx->next_protos_advertised_cb = cb;
  1858. ctx->next_protos_advertised_cb_arg = arg;
  1859. }
  1860. void SSL_CTX_set_next_proto_select_cb(
  1861. SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
  1862. const uint8_t *in, unsigned in_len, void *arg),
  1863. void *arg) {
  1864. ctx->next_proto_select_cb = cb;
  1865. ctx->next_proto_select_cb_arg = arg;
  1866. }
  1867. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1868. unsigned protos_len) {
  1869. // Note this function's calling convention is backwards.
  1870. return ctx->alpn_client_proto_list.CopyFrom(MakeConstSpan(protos, protos_len))
  1871. ? 0
  1872. : 1;
  1873. }
  1874. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1875. // Note this function's calling convention is backwards.
  1876. if (!ssl->config) {
  1877. return 1;
  1878. }
  1879. return ssl->config->alpn_client_proto_list.CopyFrom(
  1880. MakeConstSpan(protos, protos_len))
  1881. ? 0
  1882. : 1;
  1883. }
  1884. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1885. int (*cb)(SSL *ssl, const uint8_t **out,
  1886. uint8_t *out_len, const uint8_t *in,
  1887. unsigned in_len, void *arg),
  1888. void *arg) {
  1889. ctx->alpn_select_cb = cb;
  1890. ctx->alpn_select_cb_arg = arg;
  1891. }
  1892. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
  1893. unsigned *out_len) {
  1894. if (SSL_in_early_data(ssl) && !ssl->server) {
  1895. *out_data = ssl->s3->hs->early_session->early_alpn.data();
  1896. *out_len = ssl->s3->hs->early_session->early_alpn.size();
  1897. } else {
  1898. *out_data = ssl->s3->alpn_selected.data();
  1899. *out_len = ssl->s3->alpn_selected.size();
  1900. }
  1901. }
  1902. void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
  1903. ctx->allow_unknown_alpn_protos = !!enabled;
  1904. }
  1905. int SSL_CTX_add_cert_compression_alg(SSL_CTX *ctx, uint16_t alg_id,
  1906. ssl_cert_compression_func_t compress,
  1907. ssl_cert_decompression_func_t decompress) {
  1908. assert(compress != nullptr || decompress != nullptr);
  1909. for (const auto *alg : ctx->cert_compression_algs.get()) {
  1910. if (alg->alg_id == alg_id) {
  1911. return 0;
  1912. }
  1913. }
  1914. UniquePtr<CertCompressionAlg> alg = MakeUnique<CertCompressionAlg>();
  1915. if (alg == nullptr) {
  1916. return 0;
  1917. }
  1918. alg->alg_id = alg_id;
  1919. alg->compress = compress;
  1920. alg->decompress = decompress;
  1921. if (ctx->cert_compression_algs == nullptr) {
  1922. ctx->cert_compression_algs.reset(sk_CertCompressionAlg_new_null());
  1923. if (ctx->cert_compression_algs == nullptr) {
  1924. return 0;
  1925. }
  1926. }
  1927. if (!PushToStack(ctx->cert_compression_algs.get(), std::move(alg))) {
  1928. if (sk_CertCompressionAlg_num(ctx->cert_compression_algs.get()) == 0) {
  1929. ctx->cert_compression_algs.reset();
  1930. }
  1931. return 0;
  1932. }
  1933. return 1;
  1934. }
  1935. void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
  1936. ctx->channel_id_enabled = !!enabled;
  1937. }
  1938. int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
  1939. SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
  1940. return 1;
  1941. }
  1942. void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
  1943. if (!ssl->config) {
  1944. return;
  1945. }
  1946. ssl->config->channel_id_enabled = !!enabled;
  1947. }
  1948. int SSL_enable_tls_channel_id(SSL *ssl) {
  1949. SSL_set_tls_channel_id_enabled(ssl, 1);
  1950. return 1;
  1951. }
  1952. static int is_p256_key(EVP_PKEY *private_key) {
  1953. const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
  1954. return ec_key != NULL &&
  1955. EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
  1956. NID_X9_62_prime256v1;
  1957. }
  1958. int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
  1959. if (!is_p256_key(private_key)) {
  1960. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1961. return 0;
  1962. }
  1963. ctx->channel_id_private = UpRef(private_key);
  1964. ctx->channel_id_enabled = true;
  1965. return 1;
  1966. }
  1967. int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
  1968. if (!ssl->config) {
  1969. return 0;
  1970. }
  1971. if (!is_p256_key(private_key)) {
  1972. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1973. return 0;
  1974. }
  1975. ssl->config->channel_id_private = UpRef(private_key);
  1976. ssl->config->channel_id_enabled = true;
  1977. return 1;
  1978. }
  1979. size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
  1980. if (!ssl->s3->channel_id_valid) {
  1981. return 0;
  1982. }
  1983. OPENSSL_memcpy(out, ssl->s3->channel_id, (max_out < 64) ? max_out : 64);
  1984. return 64;
  1985. }
  1986. int SSL_set_token_binding_params(SSL *ssl, const uint8_t *params, size_t len) {
  1987. if (!ssl->config) {
  1988. return 0;
  1989. }
  1990. if (len > 256) {
  1991. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  1992. return 0;
  1993. }
  1994. return ssl->config->token_binding_params.CopyFrom(MakeConstSpan(params, len));
  1995. }
  1996. int SSL_is_token_binding_negotiated(const SSL *ssl) {
  1997. return ssl->s3->token_binding_negotiated;
  1998. }
  1999. uint8_t SSL_get_negotiated_token_binding_param(const SSL *ssl) {
  2000. return ssl->s3->negotiated_token_binding_param;
  2001. }
  2002. size_t SSL_get0_certificate_types(const SSL *ssl, const uint8_t **out_types) {
  2003. Span<const uint8_t> types;
  2004. if (!ssl->server && ssl->s3->hs != nullptr) {
  2005. types = ssl->s3->hs->certificate_types;
  2006. }
  2007. *out_types = types.data();
  2008. return types.size();
  2009. }
  2010. size_t SSL_get0_peer_verify_algorithms(const SSL *ssl,
  2011. const uint16_t **out_sigalgs) {
  2012. Span<const uint16_t> sigalgs;
  2013. if (ssl->s3->hs != nullptr) {
  2014. sigalgs = ssl->s3->hs->peer_sigalgs;
  2015. }
  2016. *out_sigalgs = sigalgs.data();
  2017. return sigalgs.size();
  2018. }
  2019. EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
  2020. if (!ssl->config) {
  2021. assert(ssl->config);
  2022. return NULL;
  2023. }
  2024. if (ssl->config->cert != NULL) {
  2025. return ssl->config->cert->privatekey.get();
  2026. }
  2027. return NULL;
  2028. }
  2029. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  2030. if (ctx->cert != NULL) {
  2031. return ctx->cert->privatekey.get();
  2032. }
  2033. return NULL;
  2034. }
  2035. const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
  2036. const SSL_SESSION *session = SSL_get_session(ssl);
  2037. return session == nullptr ? nullptr : session->cipher;
  2038. }
  2039. int SSL_session_reused(const SSL *ssl) {
  2040. return ssl->s3->session_reused || SSL_in_early_data(ssl);
  2041. }
  2042. const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
  2043. const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
  2044. int SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
  2045. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  2046. ctx->quiet_shutdown = (mode != 0);
  2047. }
  2048. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  2049. return ctx->quiet_shutdown;
  2050. }
  2051. void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
  2052. ssl->quiet_shutdown = (mode != 0);
  2053. }
  2054. int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
  2055. void SSL_set_shutdown(SSL *ssl, int mode) {
  2056. // It is an error to clear any bits that have already been set. (We can't try
  2057. // to get a second close_notify or send two.)
  2058. assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
  2059. if (mode & SSL_RECEIVED_SHUTDOWN &&
  2060. ssl->s3->read_shutdown == ssl_shutdown_none) {
  2061. ssl->s3->read_shutdown = ssl_shutdown_close_notify;
  2062. }
  2063. if (mode & SSL_SENT_SHUTDOWN &&
  2064. ssl->s3->write_shutdown == ssl_shutdown_none) {
  2065. ssl->s3->write_shutdown = ssl_shutdown_close_notify;
  2066. }
  2067. }
  2068. int SSL_get_shutdown(const SSL *ssl) {
  2069. int ret = 0;
  2070. if (ssl->s3->read_shutdown != ssl_shutdown_none) {
  2071. // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
  2072. // and fatal alert.
  2073. ret |= SSL_RECEIVED_SHUTDOWN;
  2074. }
  2075. if (ssl->s3->write_shutdown == ssl_shutdown_close_notify) {
  2076. // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify.
  2077. ret |= SSL_SENT_SHUTDOWN;
  2078. }
  2079. return ret;
  2080. }
  2081. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx.get(); }
  2082. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  2083. if (!ssl->config) {
  2084. return NULL;
  2085. }
  2086. if (ssl->ctx.get() == ctx) {
  2087. return ssl->ctx.get();
  2088. }
  2089. // One cannot change the X.509 callbacks during a connection.
  2090. if (ssl->ctx->x509_method != ctx->x509_method) {
  2091. assert(0);
  2092. return NULL;
  2093. }
  2094. UniquePtr<CERT> new_cert = ssl_cert_dup(ctx->cert.get());
  2095. if (!new_cert) {
  2096. return nullptr;
  2097. }
  2098. ssl->config->cert = std::move(new_cert);
  2099. ssl->ctx = UpRef(ctx);
  2100. ssl->enable_early_data = ssl->ctx->enable_early_data;
  2101. return ssl->ctx.get();
  2102. }
  2103. void SSL_set_info_callback(SSL *ssl,
  2104. void (*cb)(const SSL *ssl, int type, int value)) {
  2105. ssl->info_callback = cb;
  2106. }
  2107. void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
  2108. int value) {
  2109. return ssl->info_callback;
  2110. }
  2111. int SSL_state(const SSL *ssl) {
  2112. return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
  2113. }
  2114. void SSL_set_state(SSL *ssl, int state) { }
  2115. char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
  2116. if (len <= 0) {
  2117. return NULL;
  2118. }
  2119. buf[0] = '\0';
  2120. return buf;
  2121. }
  2122. int SSL_CTX_set_quic_method(SSL_CTX *ctx, const SSL_QUIC_METHOD *quic_method) {
  2123. if (ctx->method->is_dtls) {
  2124. return 0;
  2125. }
  2126. ctx->quic_method = quic_method;
  2127. return 1;
  2128. }
  2129. int SSL_set_quic_method(SSL *ssl, const SSL_QUIC_METHOD *quic_method) {
  2130. if (ssl->method->is_dtls) {
  2131. return 0;
  2132. }
  2133. ssl->quic_method = quic_method;
  2134. return 1;
  2135. }
  2136. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  2137. CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
  2138. int index;
  2139. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  2140. free_func)) {
  2141. return -1;
  2142. }
  2143. return index;
  2144. }
  2145. int SSL_set_ex_data(SSL *ssl, int idx, void *data) {
  2146. return CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
  2147. }
  2148. void *SSL_get_ex_data(const SSL *ssl, int idx) {
  2149. return CRYPTO_get_ex_data(&ssl->ex_data, idx);
  2150. }
  2151. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  2152. CRYPTO_EX_dup *dup_unused,
  2153. CRYPTO_EX_free *free_func) {
  2154. int index;
  2155. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  2156. free_func)) {
  2157. return -1;
  2158. }
  2159. return index;
  2160. }
  2161. int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) {
  2162. return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  2163. }
  2164. void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
  2165. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  2166. }
  2167. int SSL_want(const SSL *ssl) { return ssl->s3->rwstate; }
  2168. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  2169. RSA *(*cb)(SSL *ssl, int is_export,
  2170. int keylength)) {}
  2171. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  2172. int keylength)) {}
  2173. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  2174. DH *(*cb)(SSL *ssl, int is_export,
  2175. int keylength)) {}
  2176. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export,
  2177. int keylength)) {}
  2178. static int use_psk_identity_hint(UniquePtr<char> *out,
  2179. const char *identity_hint) {
  2180. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  2181. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  2182. return 0;
  2183. }
  2184. // Clear currently configured hint, if any.
  2185. out->reset();
  2186. // Treat the empty hint as not supplying one. Plain PSK makes it possible to
  2187. // send either no hint (omit ServerKeyExchange) or an empty hint, while
  2188. // ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
  2189. // so we interpret empty and missing as identical.
  2190. if (identity_hint != NULL && identity_hint[0] != '\0') {
  2191. out->reset(BUF_strdup(identity_hint));
  2192. if (*out == nullptr) {
  2193. return 0;
  2194. }
  2195. }
  2196. return 1;
  2197. }
  2198. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  2199. return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint);
  2200. }
  2201. int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
  2202. if (!ssl->config) {
  2203. return 0;
  2204. }
  2205. return use_psk_identity_hint(&ssl->config->psk_identity_hint, identity_hint);
  2206. }
  2207. const char *SSL_get_psk_identity_hint(const SSL *ssl) {
  2208. if (ssl == NULL) {
  2209. return NULL;
  2210. }
  2211. if (ssl->config == NULL) {
  2212. assert(ssl->config);
  2213. return NULL;
  2214. }
  2215. return ssl->config->psk_identity_hint.get();
  2216. }
  2217. const char *SSL_get_psk_identity(const SSL *ssl) {
  2218. if (ssl == NULL) {
  2219. return NULL;
  2220. }
  2221. SSL_SESSION *session = SSL_get_session(ssl);
  2222. if (session == NULL) {
  2223. return NULL;
  2224. }
  2225. return session->psk_identity.get();
  2226. }
  2227. void SSL_set_psk_client_callback(
  2228. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  2229. unsigned max_identity_len, uint8_t *psk,
  2230. unsigned max_psk_len)) {
  2231. if (!ssl->config) {
  2232. return;
  2233. }
  2234. ssl->config->psk_client_callback = cb;
  2235. }
  2236. void SSL_CTX_set_psk_client_callback(
  2237. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  2238. unsigned max_identity_len, uint8_t *psk,
  2239. unsigned max_psk_len)) {
  2240. ctx->psk_client_callback = cb;
  2241. }
  2242. void SSL_set_psk_server_callback(
  2243. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  2244. unsigned max_psk_len)) {
  2245. if (!ssl->config) {
  2246. return;
  2247. }
  2248. ssl->config->psk_server_callback = cb;
  2249. }
  2250. void SSL_CTX_set_psk_server_callback(
  2251. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
  2252. uint8_t *psk, unsigned max_psk_len)) {
  2253. ctx->psk_server_callback = cb;
  2254. }
  2255. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  2256. void (*cb)(int write_p, int version,
  2257. int content_type, const void *buf,
  2258. size_t len, SSL *ssl, void *arg)) {
  2259. ctx->msg_callback = cb;
  2260. }
  2261. void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
  2262. ctx->msg_callback_arg = arg;
  2263. }
  2264. void SSL_set_msg_callback(SSL *ssl,
  2265. void (*cb)(int write_p, int version, int content_type,
  2266. const void *buf, size_t len, SSL *ssl,
  2267. void *arg)) {
  2268. ssl->msg_callback = cb;
  2269. }
  2270. void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
  2271. ssl->msg_callback_arg = arg;
  2272. }
  2273. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
  2274. void (*cb)(const SSL *ssl, const char *line)) {
  2275. ctx->keylog_callback = cb;
  2276. }
  2277. void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
  2278. const char *line) {
  2279. return ctx->keylog_callback;
  2280. }
  2281. void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
  2282. void (*cb)(const SSL *ssl,
  2283. struct timeval *out_clock)) {
  2284. ctx->current_time_cb = cb;
  2285. }
  2286. int SSL_is_init_finished(const SSL *ssl) {
  2287. return !SSL_in_init(ssl);
  2288. }
  2289. int SSL_in_init(const SSL *ssl) {
  2290. // This returns false once all the handshake state has been finalized, to
  2291. // allow callbacks and getters based on SSL_in_init to return the correct
  2292. // values.
  2293. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  2294. return hs != nullptr && !hs->handshake_finalized;
  2295. }
  2296. int SSL_in_false_start(const SSL *ssl) {
  2297. if (ssl->s3->hs == NULL) {
  2298. return 0;
  2299. }
  2300. return ssl->s3->hs->in_false_start;
  2301. }
  2302. int SSL_cutthrough_complete(const SSL *ssl) {
  2303. return SSL_in_false_start(ssl);
  2304. }
  2305. int SSL_is_server(const SSL *ssl) { return ssl->server; }
  2306. int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
  2307. void SSL_CTX_set_select_certificate_cb(
  2308. SSL_CTX *ctx,
  2309. enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
  2310. ctx->select_certificate_cb = cb;
  2311. }
  2312. void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
  2313. int (*cb)(const SSL_CLIENT_HELLO *)) {
  2314. ctx->dos_protection_cb = cb;
  2315. }
  2316. void SSL_CTX_set_reverify_on_resume(SSL_CTX *ctx, int enabled) {
  2317. ctx->reverify_on_resume = !!enabled;
  2318. }
  2319. void SSL_set_enforce_rsa_key_usage(SSL *ssl, int enabled) {
  2320. if (!ssl->config) {
  2321. return;
  2322. }
  2323. ssl->config->enforce_rsa_key_usage = !!enabled;
  2324. }
  2325. void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
  2326. ssl->renegotiate_mode = mode;
  2327. // Check if |ssl_can_renegotiate| has changed and the configuration may now be
  2328. // shed. HTTP clients may initially allow renegotiation for HTTP/1.1, and then
  2329. // disable after the handshake once the ALPN protocol is known to be HTTP/2.
  2330. ssl_maybe_shed_handshake_config(ssl);
  2331. }
  2332. int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
  2333. const uint8_t **out_write_iv, size_t *out_iv_len) {
  2334. size_t write_iv_len;
  2335. if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) ||
  2336. !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) ||
  2337. *out_iv_len != write_iv_len) {
  2338. return 0;
  2339. }
  2340. return 1;
  2341. }
  2342. static uint64_t be_to_u64(const uint8_t in[8]) {
  2343. return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
  2344. (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
  2345. (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
  2346. (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
  2347. }
  2348. uint64_t SSL_get_read_sequence(const SSL *ssl) {
  2349. // TODO(davidben): Internally represent sequence numbers as uint64_t.
  2350. if (SSL_is_dtls(ssl)) {
  2351. // max_seq_num already includes the epoch.
  2352. assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
  2353. return ssl->d1->bitmap.max_seq_num;
  2354. }
  2355. return be_to_u64(ssl->s3->read_sequence);
  2356. }
  2357. uint64_t SSL_get_write_sequence(const SSL *ssl) {
  2358. uint64_t ret = be_to_u64(ssl->s3->write_sequence);
  2359. if (SSL_is_dtls(ssl)) {
  2360. assert((ret >> 48) == 0);
  2361. ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
  2362. }
  2363. return ret;
  2364. }
  2365. uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
  2366. // TODO(davidben): This checks the wrong session if there is a renegotiation
  2367. // in progress.
  2368. SSL_SESSION *session = SSL_get_session(ssl);
  2369. if (session == NULL) {
  2370. return 0;
  2371. }
  2372. return session->peer_signature_algorithm;
  2373. }
  2374. size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2375. if (max_out == 0) {
  2376. return sizeof(ssl->s3->client_random);
  2377. }
  2378. if (max_out > sizeof(ssl->s3->client_random)) {
  2379. max_out = sizeof(ssl->s3->client_random);
  2380. }
  2381. OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
  2382. return max_out;
  2383. }
  2384. size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2385. if (max_out == 0) {
  2386. return sizeof(ssl->s3->server_random);
  2387. }
  2388. if (max_out > sizeof(ssl->s3->server_random)) {
  2389. max_out = sizeof(ssl->s3->server_random);
  2390. }
  2391. OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
  2392. return max_out;
  2393. }
  2394. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
  2395. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  2396. if (hs == NULL) {
  2397. return NULL;
  2398. }
  2399. return hs->new_cipher;
  2400. }
  2401. void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
  2402. if (!ssl->config) {
  2403. return;
  2404. }
  2405. ssl->config->retain_only_sha256_of_client_certs = !!enabled;
  2406. }
  2407. void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
  2408. ctx->retain_only_sha256_of_client_certs = !!enabled;
  2409. }
  2410. void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
  2411. ctx->grease_enabled = !!enabled;
  2412. }
  2413. int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
  2414. return ssl->s3->ticket_age_skew;
  2415. }
  2416. void SSL_CTX_set_false_start_allowed_without_alpn(SSL_CTX *ctx, int allowed) {
  2417. ctx->false_start_allowed_without_alpn = !!allowed;
  2418. }
  2419. int SSL_is_tls13_downgrade(const SSL *ssl) { return ssl->s3->tls13_downgrade; }
  2420. void SSL_CTX_set_ignore_tls13_downgrade(SSL_CTX *ctx, int ignore) {
  2421. ctx->ignore_tls13_downgrade = !!ignore;
  2422. }
  2423. void SSL_set_ignore_tls13_downgrade(SSL *ssl, int ignore) {
  2424. if (!ssl->config) {
  2425. return;
  2426. }
  2427. ssl->config->ignore_tls13_downgrade = !!ignore;
  2428. }
  2429. void SSL_set_shed_handshake_config(SSL *ssl, int enable) {
  2430. if (!ssl->config) {
  2431. return;
  2432. }
  2433. ssl->config->shed_handshake_config = !!enable;
  2434. }
  2435. void SSL_set_jdk11_workaround(SSL *ssl, int enable) {
  2436. if (!ssl->config) {
  2437. return;
  2438. }
  2439. ssl->config->jdk11_workaround = !!enable;
  2440. }
  2441. int SSL_clear(SSL *ssl) {
  2442. if (!ssl->config) {
  2443. return 0; // SSL_clear may not be used after shedding config.
  2444. }
  2445. // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
  2446. // established session to be offered the next time around. wpa_supplicant
  2447. // depends on this behavior, so emulate it.
  2448. UniquePtr<SSL_SESSION> session;
  2449. if (!ssl->server && ssl->s3->established_session != NULL) {
  2450. session = UpRef(ssl->s3->established_session);
  2451. }
  2452. // The ssl->d1->mtu is simultaneously configuration (preserved across
  2453. // clear) and connection-specific state (gets reset).
  2454. //
  2455. // TODO(davidben): Avoid this.
  2456. unsigned mtu = 0;
  2457. if (ssl->d1 != NULL) {
  2458. mtu = ssl->d1->mtu;
  2459. }
  2460. ssl->method->ssl_free(ssl);
  2461. if (!ssl->method->ssl_new(ssl)) {
  2462. return 0;
  2463. }
  2464. if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  2465. ssl->d1->mtu = mtu;
  2466. }
  2467. if (session != nullptr) {
  2468. SSL_set_session(ssl, session.get());
  2469. }
  2470. return 1;
  2471. }
  2472. int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
  2473. int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
  2474. int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
  2475. int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
  2476. int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
  2477. int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
  2478. int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
  2479. int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
  2480. int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
  2481. int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
  2482. int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
  2483. int SSL_num_renegotiations(const SSL *ssl) {
  2484. return SSL_total_renegotiations(ssl);
  2485. }
  2486. int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
  2487. int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
  2488. int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
  2489. int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
  2490. void ERR_load_SSL_strings(void) {}
  2491. void SSL_load_error_strings(void) {}
  2492. int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
  2493. int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
  2494. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2495. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2496. return 0;
  2497. }
  2498. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2499. return SSL_CTX_set1_curves(ctx, &nid, 1);
  2500. }
  2501. int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
  2502. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2503. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2504. return 0;
  2505. }
  2506. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2507. return SSL_set1_curves(ssl, &nid, 1);
  2508. }
  2509. void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx,
  2510. const SSL_TICKET_AEAD_METHOD *aead_method) {
  2511. ctx->ticket_aead_method = aead_method;
  2512. }
  2513. int SSL_set_tlsext_status_type(SSL *ssl, int type) {
  2514. if (!ssl->config) {
  2515. return 0;
  2516. }
  2517. ssl->config->ocsp_stapling_enabled = type == TLSEXT_STATUSTYPE_ocsp;
  2518. return 1;
  2519. }
  2520. int SSL_get_tlsext_status_type(const SSL *ssl) {
  2521. if (ssl->server) {
  2522. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  2523. return hs != nullptr && hs->ocsp_stapling_requested
  2524. ? TLSEXT_STATUSTYPE_ocsp
  2525. : TLSEXT_STATUSTYPE_nothing;
  2526. }
  2527. return ssl->config != nullptr && ssl->config->ocsp_stapling_enabled
  2528. ? TLSEXT_STATUSTYPE_ocsp
  2529. : TLSEXT_STATUSTYPE_nothing;
  2530. }
  2531. int SSL_set_tlsext_status_ocsp_resp(SSL *ssl, uint8_t *resp, size_t resp_len) {
  2532. if (SSL_set_ocsp_response(ssl, resp, resp_len)) {
  2533. OPENSSL_free(resp);
  2534. return 1;
  2535. }
  2536. return 0;
  2537. }
  2538. size_t SSL_get_tlsext_status_ocsp_resp(const SSL *ssl, const uint8_t **out) {
  2539. size_t ret;
  2540. SSL_get0_ocsp_response(ssl, out, &ret);
  2541. return ret;
  2542. }
  2543. int SSL_CTX_set_tlsext_status_cb(SSL_CTX *ctx,
  2544. int (*callback)(SSL *ssl, void *arg)) {
  2545. ctx->legacy_ocsp_callback = callback;
  2546. return 1;
  2547. }
  2548. int SSL_CTX_set_tlsext_status_arg(SSL_CTX *ctx, void *arg) {
  2549. ctx->legacy_ocsp_callback_arg = arg;
  2550. return 1;
  2551. }