ssl_lib.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736
  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/dh.h>
  147. #include <openssl/err.h>
  148. #include <openssl/lhash.h>
  149. #include <openssl/mem.h>
  150. #include <openssl/rand.h>
  151. #include "internal.h"
  152. #include "../crypto/internal.h"
  153. #if defined(OPENSSL_WINDOWS)
  154. #include <sys/timeb.h>
  155. #else
  156. #include <sys/socket.h>
  157. #include <sys/time.h>
  158. #endif
  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. OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
  169. SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
  170. ssl_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. int SSL_library_init(void) {
  178. CRYPTO_library_init();
  179. return 1;
  180. }
  181. static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
  182. const uint8_t *session_id = sess->session_id;
  183. uint8_t tmp_storage[sizeof(uint32_t)];
  184. if (sess->session_id_length < sizeof(tmp_storage)) {
  185. OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage));
  186. OPENSSL_memcpy(tmp_storage, sess->session_id, sess->session_id_length);
  187. session_id = tmp_storage;
  188. }
  189. uint32_t hash =
  190. ((uint32_t)session_id[0]) |
  191. ((uint32_t)session_id[1] << 8) |
  192. ((uint32_t)session_id[2] << 16) |
  193. ((uint32_t)session_id[3] << 24);
  194. return hash;
  195. }
  196. /* NB: If this function (or indeed the hash function which uses a sort of
  197. * coarser function than this one) is changed, ensure
  198. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  199. * able to construct an SSL_SESSION that will collide with any existing session
  200. * with a matching session ID. */
  201. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  202. if (a->ssl_version != b->ssl_version) {
  203. return 1;
  204. }
  205. if (a->session_id_length != b->session_id_length) {
  206. return 1;
  207. }
  208. return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
  209. }
  210. SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
  211. SSL_CTX *ret = NULL;
  212. if (method == NULL) {
  213. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  214. return NULL;
  215. }
  216. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  217. OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  218. goto err;
  219. }
  220. ret = OPENSSL_malloc(sizeof(SSL_CTX));
  221. if (ret == NULL) {
  222. goto err;
  223. }
  224. OPENSSL_memset(ret, 0, sizeof(SSL_CTX));
  225. ret->method = method->method;
  226. ret->x509_method = method->x509_method;
  227. CRYPTO_MUTEX_init(&ret->lock);
  228. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  229. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  230. ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  231. ret->session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
  232. ret->references = 1;
  233. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  234. ret->verify_mode = SSL_VERIFY_NONE;
  235. ret->cert = ssl_cert_new(method->x509_method);
  236. if (ret->cert == NULL) {
  237. goto err;
  238. }
  239. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  240. if (ret->sessions == NULL) {
  241. goto err;
  242. }
  243. ret->cert_store = X509_STORE_new();
  244. if (ret->cert_store == NULL) {
  245. goto err;
  246. }
  247. ssl_create_cipher_list(ret->method, &ret->cipher_list,
  248. SSL_DEFAULT_CIPHER_LIST, 1 /* strict */);
  249. if (ret->cipher_list == NULL ||
  250. sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
  251. OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  252. goto err2;
  253. }
  254. ret->param = X509_VERIFY_PARAM_new();
  255. if (!ret->param) {
  256. goto err;
  257. }
  258. ret->client_CA = sk_X509_NAME_new_null();
  259. if (ret->client_CA == NULL) {
  260. goto err;
  261. }
  262. CRYPTO_new_ex_data(&ret->ex_data);
  263. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  264. /* Setup RFC4507 ticket keys */
  265. if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
  266. !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
  267. !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
  268. ret->options |= SSL_OP_NO_TICKET;
  269. }
  270. /* Disable the auto-chaining feature by default. Once this has stuck without
  271. * problems, the feature will be removed entirely. */
  272. ret->mode = SSL_MODE_NO_AUTO_CHAIN;
  273. /* Lock the SSL_CTX to the specified version, for compatibility with legacy
  274. * uses of SSL_METHOD. */
  275. if (!SSL_CTX_set_max_proto_version(ret, method->version) ||
  276. !SSL_CTX_set_min_proto_version(ret, method->version)) {
  277. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  278. goto err2;
  279. }
  280. return ret;
  281. err:
  282. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  283. err2:
  284. SSL_CTX_free(ret);
  285. return NULL;
  286. }
  287. int SSL_CTX_up_ref(SSL_CTX *ctx) {
  288. CRYPTO_refcount_inc(&ctx->references);
  289. return 1;
  290. }
  291. void SSL_CTX_free(SSL_CTX *ctx) {
  292. if (ctx == NULL ||
  293. !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
  294. return;
  295. }
  296. X509_VERIFY_PARAM_free(ctx->param);
  297. /* Free internal session cache. However: the remove_cb() may reference the
  298. * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
  299. * sessions were flushed. As the ex_data handling routines might also touch
  300. * the session cache, the most secure solution seems to be: empty (flush) the
  301. * cache, then free ex_data, then finally free the cache. (See ticket
  302. * [openssl.org #212].) */
  303. SSL_CTX_flush_sessions(ctx, 0);
  304. CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
  305. CRYPTO_MUTEX_cleanup(&ctx->lock);
  306. lh_SSL_SESSION_free(ctx->sessions);
  307. X509_STORE_free(ctx->cert_store);
  308. ssl_cipher_preference_list_free(ctx->cipher_list);
  309. ssl_cert_free(ctx->cert);
  310. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
  311. SSL_CUSTOM_EXTENSION_free);
  312. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
  313. SSL_CUSTOM_EXTENSION_free);
  314. sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
  315. sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
  316. OPENSSL_free(ctx->psk_identity_hint);
  317. OPENSSL_free(ctx->supported_group_list);
  318. OPENSSL_free(ctx->alpn_client_proto_list);
  319. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  320. OPENSSL_free(ctx);
  321. }
  322. SSL *SSL_new(SSL_CTX *ctx) {
  323. if (ctx == NULL) {
  324. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
  325. return NULL;
  326. }
  327. if (ctx->method == NULL) {
  328. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  329. return NULL;
  330. }
  331. SSL *ssl = OPENSSL_malloc(sizeof(SSL));
  332. if (ssl == NULL) {
  333. goto err;
  334. }
  335. OPENSSL_memset(ssl, 0, sizeof(SSL));
  336. ssl->min_version = ctx->min_version;
  337. ssl->max_version = ctx->max_version;
  338. /* RFC 6347 states that implementations SHOULD use an initial timer value of
  339. * 1 second. */
  340. ssl->initial_timeout_duration_ms = 1000;
  341. ssl->options = ctx->options;
  342. ssl->mode = ctx->mode;
  343. ssl->max_cert_list = ctx->max_cert_list;
  344. ssl->cert = ssl_cert_dup(ctx->cert);
  345. if (ssl->cert == NULL) {
  346. goto err;
  347. }
  348. ssl->msg_callback = ctx->msg_callback;
  349. ssl->msg_callback_arg = ctx->msg_callback_arg;
  350. ssl->verify_mode = ctx->verify_mode;
  351. ssl->verify_callback = ctx->default_verify_callback;
  352. ssl->retain_only_sha256_of_client_certs =
  353. ctx->retain_only_sha256_of_client_certs;
  354. ssl->param = X509_VERIFY_PARAM_new();
  355. if (!ssl->param) {
  356. goto err;
  357. }
  358. X509_VERIFY_PARAM_inherit(ssl->param, ctx->param);
  359. ssl->quiet_shutdown = ctx->quiet_shutdown;
  360. ssl->max_send_fragment = ctx->max_send_fragment;
  361. SSL_CTX_up_ref(ctx);
  362. ssl->ctx = ctx;
  363. SSL_CTX_up_ref(ctx);
  364. ssl->initial_ctx = ctx;
  365. if (ctx->supported_group_list) {
  366. ssl->supported_group_list = BUF_memdup(ctx->supported_group_list,
  367. ctx->supported_group_list_len * 2);
  368. if (!ssl->supported_group_list) {
  369. goto err;
  370. }
  371. ssl->supported_group_list_len = ctx->supported_group_list_len;
  372. }
  373. if (ctx->alpn_client_proto_list) {
  374. ssl->alpn_client_proto_list = BUF_memdup(ctx->alpn_client_proto_list,
  375. ctx->alpn_client_proto_list_len);
  376. if (ssl->alpn_client_proto_list == NULL) {
  377. goto err;
  378. }
  379. ssl->alpn_client_proto_list_len = ctx->alpn_client_proto_list_len;
  380. }
  381. ssl->method = ctx->method;
  382. if (!ssl->method->ssl_new(ssl)) {
  383. goto err;
  384. }
  385. ssl->rwstate = SSL_NOTHING;
  386. CRYPTO_new_ex_data(&ssl->ex_data);
  387. ssl->psk_identity_hint = NULL;
  388. if (ctx->psk_identity_hint) {
  389. ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
  390. if (ssl->psk_identity_hint == NULL) {
  391. goto err;
  392. }
  393. }
  394. ssl->psk_client_callback = ctx->psk_client_callback;
  395. ssl->psk_server_callback = ctx->psk_server_callback;
  396. ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
  397. if (ctx->tlsext_channel_id_private) {
  398. EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
  399. ssl->tlsext_channel_id_private = ctx->tlsext_channel_id_private;
  400. }
  401. ssl->signed_cert_timestamps_enabled = ctx->signed_cert_timestamps_enabled;
  402. ssl->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
  403. return ssl;
  404. err:
  405. SSL_free(ssl);
  406. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  407. return NULL;
  408. }
  409. void SSL_free(SSL *ssl) {
  410. if (ssl == NULL) {
  411. return;
  412. }
  413. X509_VERIFY_PARAM_free(ssl->param);
  414. CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
  415. BIO_free_all(ssl->rbio);
  416. BIO_free_all(ssl->wbio);
  417. BUF_MEM_free(ssl->init_buf);
  418. /* add extra stuff */
  419. ssl_cipher_preference_list_free(ssl->cipher_list);
  420. SSL_SESSION_free(ssl->session);
  421. ssl_cert_free(ssl->cert);
  422. OPENSSL_free(ssl->tlsext_hostname);
  423. SSL_CTX_free(ssl->initial_ctx);
  424. OPENSSL_free(ssl->supported_group_list);
  425. OPENSSL_free(ssl->alpn_client_proto_list);
  426. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  427. OPENSSL_free(ssl->psk_identity_hint);
  428. sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
  429. sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
  430. if (ssl->method != NULL) {
  431. ssl->method->ssl_free(ssl);
  432. }
  433. SSL_CTX_free(ssl->ctx);
  434. OPENSSL_free(ssl);
  435. }
  436. void SSL_set_connect_state(SSL *ssl) {
  437. ssl->server = 0;
  438. ssl->handshake_func = ssl3_connect;
  439. }
  440. void SSL_set_accept_state(SSL *ssl) {
  441. ssl->server = 1;
  442. ssl->handshake_func = ssl3_accept;
  443. }
  444. void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
  445. BIO_free_all(ssl->rbio);
  446. ssl->rbio = rbio;
  447. }
  448. void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
  449. BIO_free_all(ssl->wbio);
  450. ssl->wbio = wbio;
  451. }
  452. void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
  453. /* For historical reasons, this function has many different cases in ownership
  454. * handling. */
  455. /* If nothing has changed, do nothing */
  456. if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
  457. return;
  458. }
  459. /* If the two arguments are equal, one fewer reference is granted than
  460. * taken. */
  461. if (rbio != NULL && rbio == wbio) {
  462. BIO_up_ref(rbio);
  463. }
  464. /* If only the wbio is changed, adopt only one reference. */
  465. if (rbio == SSL_get_rbio(ssl)) {
  466. SSL_set0_wbio(ssl, wbio);
  467. return;
  468. }
  469. /* There is an asymmetry here for historical reasons. If only the rbio is
  470. * changed AND the rbio and wbio were originally different, then we only adopt
  471. * one reference. */
  472. if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
  473. SSL_set0_rbio(ssl, rbio);
  474. return;
  475. }
  476. /* Otherwise, adopt both references. */
  477. SSL_set0_rbio(ssl, rbio);
  478. SSL_set0_wbio(ssl, wbio);
  479. }
  480. BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
  481. BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
  482. void ssl_reset_error_state(SSL *ssl) {
  483. /* Functions which use |SSL_get_error| must reset I/O and error state on
  484. * entry. */
  485. ssl->rwstate = SSL_NOTHING;
  486. ERR_clear_error();
  487. ERR_clear_system_error();
  488. }
  489. int SSL_do_handshake(SSL *ssl) {
  490. ssl_reset_error_state(ssl);
  491. if (ssl->handshake_func == NULL) {
  492. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  493. return -1;
  494. }
  495. if (!SSL_in_init(ssl)) {
  496. return 1;
  497. }
  498. if (ssl->s3->hs == NULL) {
  499. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  500. return -1;
  501. }
  502. /* Run the handshake. */
  503. assert(ssl->s3->hs != NULL);
  504. int ret = ssl->handshake_func(ssl->s3->hs);
  505. if (ret <= 0) {
  506. return ret;
  507. }
  508. /* Destroy the handshake object if the handshake has completely finished. */
  509. if (!SSL_in_init(ssl)) {
  510. ssl_handshake_free(ssl->s3->hs);
  511. ssl->s3->hs = NULL;
  512. }
  513. return 1;
  514. }
  515. int SSL_connect(SSL *ssl) {
  516. if (ssl->handshake_func == NULL) {
  517. /* Not properly initialized yet */
  518. SSL_set_connect_state(ssl);
  519. }
  520. return SSL_do_handshake(ssl);
  521. }
  522. int SSL_accept(SSL *ssl) {
  523. if (ssl->handshake_func == NULL) {
  524. /* Not properly initialized yet */
  525. SSL_set_accept_state(ssl);
  526. }
  527. return SSL_do_handshake(ssl);
  528. }
  529. static int ssl_do_renegotiate(SSL *ssl) {
  530. /* We do not accept renegotiations as a server or SSL 3.0. SSL 3.0 will be
  531. * removed entirely in the future and requires retaining more data for
  532. * renegotiation_info. */
  533. if (ssl->server || ssl->version == SSL3_VERSION) {
  534. goto no_renegotiation;
  535. }
  536. if (ssl->s3->tmp.message_type != SSL3_MT_HELLO_REQUEST ||
  537. ssl->init_num != 0) {
  538. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  539. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
  540. return 0;
  541. }
  542. switch (ssl->renegotiate_mode) {
  543. case ssl_renegotiate_ignore:
  544. /* Ignore the HelloRequest. */
  545. return 1;
  546. case ssl_renegotiate_once:
  547. if (ssl->s3->total_renegotiations != 0) {
  548. goto no_renegotiation;
  549. }
  550. break;
  551. case ssl_renegotiate_never:
  552. goto no_renegotiation;
  553. case ssl_renegotiate_freely:
  554. break;
  555. }
  556. /* Renegotiation is only supported at quiescent points in the application
  557. * protocol, namely in HTTPS, just before reading the HTTP response. Require
  558. * the record-layer be idle and avoid complexities of sending a handshake
  559. * record while an application_data record is being written. */
  560. if (ssl_write_buffer_is_pending(ssl)) {
  561. goto no_renegotiation;
  562. }
  563. /* Begin a new handshake. */
  564. if (ssl->s3->hs != NULL) {
  565. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  566. return 0;
  567. }
  568. ssl->s3->hs = ssl_handshake_new(ssl);
  569. if (ssl->s3->hs == NULL) {
  570. return 0;
  571. }
  572. ssl->s3->total_renegotiations++;
  573. return 1;
  574. no_renegotiation:
  575. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
  576. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  577. return 0;
  578. }
  579. static int ssl_do_post_handshake(SSL *ssl) {
  580. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  581. return ssl_do_renegotiate(ssl);
  582. }
  583. return tls13_post_handshake(ssl);
  584. }
  585. static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) {
  586. ssl_reset_error_state(ssl);
  587. if (ssl->handshake_func == NULL) {
  588. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  589. return -1;
  590. }
  591. for (;;) {
  592. /* Complete the current handshake, if any. False Start will cause
  593. * |SSL_do_handshake| to return mid-handshake, so this may require multiple
  594. * iterations. */
  595. while (SSL_in_init(ssl)) {
  596. int ret = SSL_do_handshake(ssl);
  597. if (ret < 0) {
  598. return ret;
  599. }
  600. if (ret == 0) {
  601. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  602. return -1;
  603. }
  604. }
  605. int got_handshake;
  606. int ret = ssl->method->read_app_data(ssl, &got_handshake, buf, num, peek);
  607. if (ret > 0 || !got_handshake) {
  608. ssl->s3->key_update_count = 0;
  609. return ret;
  610. }
  611. /* Handle the post-handshake message and try again. */
  612. if (!ssl_do_post_handshake(ssl)) {
  613. return -1;
  614. }
  615. ssl->method->release_current_message(ssl, 1 /* free buffer */);
  616. }
  617. }
  618. int SSL_read(SSL *ssl, void *buf, int num) {
  619. return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */);
  620. }
  621. int SSL_peek(SSL *ssl, void *buf, int num) {
  622. return ssl_read_impl(ssl, buf, num, 1 /* peek */);
  623. }
  624. int SSL_write(SSL *ssl, const void *buf, int num) {
  625. ssl_reset_error_state(ssl);
  626. if (ssl->handshake_func == NULL) {
  627. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  628. return -1;
  629. }
  630. if (ssl->s3->send_shutdown != ssl_shutdown_none) {
  631. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  632. return -1;
  633. }
  634. /* If necessary, complete the handshake implicitly. */
  635. if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) {
  636. int ret = SSL_do_handshake(ssl);
  637. if (ret < 0) {
  638. return ret;
  639. }
  640. if (ret == 0) {
  641. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  642. return -1;
  643. }
  644. }
  645. return ssl->method->write_app_data(ssl, buf, num);
  646. }
  647. int SSL_shutdown(SSL *ssl) {
  648. ssl_reset_error_state(ssl);
  649. if (ssl->handshake_func == NULL) {
  650. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  651. return -1;
  652. }
  653. /* If we are in the middle of a handshake, silently succeed. Consumers often
  654. * call this function before |SSL_free|, whether the handshake succeeded or
  655. * not. We assume the caller has already handled failed handshakes. */
  656. if (SSL_in_init(ssl)) {
  657. return 1;
  658. }
  659. if (ssl->quiet_shutdown) {
  660. /* Do nothing if configured not to send a close_notify. */
  661. ssl->s3->send_shutdown = ssl_shutdown_close_notify;
  662. ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
  663. return 1;
  664. }
  665. /* This function completes in two stages. It sends a close_notify and then it
  666. * waits for a close_notify to come in. Perform exactly one action and return
  667. * whether or not it succeeds. */
  668. if (ssl->s3->send_shutdown != ssl_shutdown_close_notify) {
  669. /* Send a close_notify. */
  670. if (ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
  671. return -1;
  672. }
  673. } else if (ssl->s3->alert_dispatch) {
  674. /* Finish sending the close_notify. */
  675. if (ssl->method->dispatch_alert(ssl) <= 0) {
  676. return -1;
  677. }
  678. } else if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
  679. /* Wait for the peer's close_notify. */
  680. ssl->method->read_close_notify(ssl);
  681. if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
  682. return -1;
  683. }
  684. }
  685. /* Return 0 for unidirectional shutdown and 1 for bidirectional shutdown. */
  686. return ssl->s3->recv_shutdown == ssl_shutdown_close_notify;
  687. }
  688. int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
  689. if (ssl->s3->alert_dispatch) {
  690. if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
  691. ssl->s3->send_alert[1] != alert) {
  692. /* We are already attempting to write a different alert. */
  693. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  694. return -1;
  695. }
  696. return ssl->method->dispatch_alert(ssl);
  697. }
  698. return ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  699. }
  700. void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
  701. ctx->enable_early_data = !!enabled;
  702. }
  703. static int bio_retry_reason_to_error(int reason) {
  704. switch (reason) {
  705. case BIO_RR_CONNECT:
  706. return SSL_ERROR_WANT_CONNECT;
  707. case BIO_RR_ACCEPT:
  708. return SSL_ERROR_WANT_ACCEPT;
  709. default:
  710. return SSL_ERROR_SYSCALL;
  711. }
  712. }
  713. int SSL_get_error(const SSL *ssl, int ret_code) {
  714. if (ret_code > 0) {
  715. return SSL_ERROR_NONE;
  716. }
  717. /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  718. * where we do encode the error */
  719. uint32_t err = ERR_peek_error();
  720. if (err != 0) {
  721. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  722. return SSL_ERROR_SYSCALL;
  723. }
  724. return SSL_ERROR_SSL;
  725. }
  726. if (ret_code == 0) {
  727. if (ssl->s3->recv_shutdown == ssl_shutdown_close_notify) {
  728. return SSL_ERROR_ZERO_RETURN;
  729. }
  730. /* An EOF was observed which violates the protocol, and the underlying
  731. * transport does not participate in the error queue. Bubble up to the
  732. * caller. */
  733. return SSL_ERROR_SYSCALL;
  734. }
  735. switch (ssl->rwstate) {
  736. case SSL_PENDING_SESSION:
  737. return SSL_ERROR_PENDING_SESSION;
  738. case SSL_CERTIFICATE_SELECTION_PENDING:
  739. return SSL_ERROR_PENDING_CERTIFICATE;
  740. case SSL_READING: {
  741. BIO *bio = SSL_get_rbio(ssl);
  742. if (BIO_should_read(bio)) {
  743. return SSL_ERROR_WANT_READ;
  744. }
  745. if (BIO_should_write(bio)) {
  746. /* TODO(davidben): OpenSSL historically checked for writes on the read
  747. * BIO. Can this be removed? */
  748. return SSL_ERROR_WANT_WRITE;
  749. }
  750. if (BIO_should_io_special(bio)) {
  751. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  752. }
  753. break;
  754. }
  755. case SSL_WRITING: {
  756. BIO *bio = SSL_get_wbio(ssl);
  757. if (BIO_should_write(bio)) {
  758. return SSL_ERROR_WANT_WRITE;
  759. }
  760. if (BIO_should_read(bio)) {
  761. /* TODO(davidben): OpenSSL historically checked for reads on the write
  762. * BIO. Can this be removed? */
  763. return SSL_ERROR_WANT_READ;
  764. }
  765. if (BIO_should_io_special(bio)) {
  766. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  767. }
  768. break;
  769. }
  770. case SSL_X509_LOOKUP:
  771. return SSL_ERROR_WANT_X509_LOOKUP;
  772. case SSL_CHANNEL_ID_LOOKUP:
  773. return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
  774. case SSL_PRIVATE_KEY_OPERATION:
  775. return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
  776. }
  777. return SSL_ERROR_SYSCALL;
  778. }
  779. static int set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  780. uint16_t version) {
  781. if (version == 0) {
  782. *out = method->min_version;
  783. return 1;
  784. }
  785. if (version == TLS1_3_VERSION) {
  786. version = TLS1_3_DRAFT_VERSION;
  787. }
  788. return method->version_from_wire(out, version);
  789. }
  790. static int set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  791. uint16_t version) {
  792. if (version == 0) {
  793. *out = method->max_version;
  794. /* TODO(svaldez): Enable TLS 1.3 by default once fully implemented. */
  795. if (*out > TLS1_2_VERSION) {
  796. *out = TLS1_2_VERSION;
  797. }
  798. return 1;
  799. }
  800. if (version == TLS1_3_VERSION) {
  801. version = TLS1_3_DRAFT_VERSION;
  802. }
  803. return method->version_from_wire(out, version);
  804. }
  805. int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) {
  806. return set_min_version(ctx->method, &ctx->min_version, version);
  807. }
  808. int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) {
  809. return set_max_version(ctx->method, &ctx->max_version, version);
  810. }
  811. int SSL_set_min_proto_version(SSL *ssl, uint16_t version) {
  812. return set_min_version(ssl->method, &ssl->min_version, version);
  813. }
  814. int SSL_set_max_proto_version(SSL *ssl, uint16_t version) {
  815. return set_max_version(ssl->method, &ssl->max_version, version);
  816. }
  817. uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
  818. ctx->options |= options;
  819. return ctx->options;
  820. }
  821. uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
  822. ctx->options &= ~options;
  823. return ctx->options;
  824. }
  825. uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
  826. uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
  827. ssl->options |= options;
  828. return ssl->options;
  829. }
  830. uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
  831. ssl->options &= ~options;
  832. return ssl->options;
  833. }
  834. uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
  835. uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
  836. ctx->mode |= mode;
  837. return ctx->mode;
  838. }
  839. uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
  840. ctx->mode &= ~mode;
  841. return ctx->mode;
  842. }
  843. uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
  844. uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
  845. ssl->mode |= mode;
  846. return ssl->mode;
  847. }
  848. uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
  849. ssl->mode &= ~mode;
  850. return ssl->mode;
  851. }
  852. uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
  853. void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
  854. ctx->pool = pool;
  855. }
  856. int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
  857. size_t max_out) {
  858. /* tls-unique is not defined for SSL 3.0 or TLS 1.3. */
  859. if (!ssl->s3->initial_handshake_complete ||
  860. ssl3_protocol_version(ssl) < TLS1_VERSION ||
  861. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  862. goto err;
  863. }
  864. /* The tls-unique value is the first Finished message in the handshake, which
  865. * is the client's in a full handshake and the server's for a resumption. See
  866. * https://tools.ietf.org/html/rfc5929#section-3.1. */
  867. const uint8_t *finished = ssl->s3->previous_client_finished;
  868. size_t finished_len = ssl->s3->previous_client_finished_len;
  869. if (ssl->session != NULL) {
  870. /* tls-unique is broken for resumed sessions unless EMS is used. */
  871. if (!ssl->session->extended_master_secret) {
  872. goto err;
  873. }
  874. finished = ssl->s3->previous_server_finished;
  875. finished_len = ssl->s3->previous_server_finished_len;
  876. }
  877. *out_len = finished_len;
  878. if (finished_len > max_out) {
  879. *out_len = max_out;
  880. }
  881. OPENSSL_memcpy(out, finished, *out_len);
  882. return 1;
  883. err:
  884. *out_len = 0;
  885. OPENSSL_memset(out, 0, max_out);
  886. return 0;
  887. }
  888. static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
  889. size_t sid_ctx_len) {
  890. if (sid_ctx_len > sizeof(cert->sid_ctx)) {
  891. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  892. return 0;
  893. }
  894. OPENSSL_COMPILE_ASSERT(sizeof(cert->sid_ctx) < 256, sid_ctx_too_large);
  895. cert->sid_ctx_length = (uint8_t)sid_ctx_len;
  896. OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
  897. return 1;
  898. }
  899. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
  900. size_t sid_ctx_len) {
  901. return set_session_id_context(ctx->cert, sid_ctx, sid_ctx_len);
  902. }
  903. int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
  904. size_t sid_ctx_len) {
  905. return set_session_id_context(ssl->cert, sid_ctx, sid_ctx_len);
  906. }
  907. const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
  908. *out_len = ssl->cert->sid_ctx_length;
  909. return ssl->cert->sid_ctx;
  910. }
  911. void ssl_cipher_preference_list_free(
  912. struct ssl_cipher_preference_list_st *cipher_list) {
  913. if (cipher_list == NULL) {
  914. return;
  915. }
  916. sk_SSL_CIPHER_free(cipher_list->ciphers);
  917. OPENSSL_free(cipher_list->in_group_flags);
  918. OPENSSL_free(cipher_list);
  919. }
  920. void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
  921. int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
  922. int SSL_get_rfd(const SSL *ssl) {
  923. int ret = -1;
  924. BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
  925. if (b != NULL) {
  926. BIO_get_fd(b, &ret);
  927. }
  928. return ret;
  929. }
  930. int SSL_get_wfd(const SSL *ssl) {
  931. int ret = -1;
  932. BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
  933. if (b != NULL) {
  934. BIO_get_fd(b, &ret);
  935. }
  936. return ret;
  937. }
  938. int SSL_set_fd(SSL *ssl, int fd) {
  939. BIO *bio = BIO_new(BIO_s_socket());
  940. if (bio == NULL) {
  941. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  942. return 0;
  943. }
  944. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  945. SSL_set_bio(ssl, bio, bio);
  946. return 1;
  947. }
  948. int SSL_set_wfd(SSL *ssl, int fd) {
  949. BIO *rbio = SSL_get_rbio(ssl);
  950. if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
  951. BIO_get_fd(rbio, NULL) != fd) {
  952. BIO *bio = BIO_new(BIO_s_socket());
  953. if (bio == NULL) {
  954. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  955. return 0;
  956. }
  957. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  958. SSL_set0_wbio(ssl, bio);
  959. } else {
  960. /* Copy the rbio over to the wbio. */
  961. BIO_up_ref(rbio);
  962. SSL_set0_wbio(ssl, rbio);
  963. }
  964. return 1;
  965. }
  966. int SSL_set_rfd(SSL *ssl, int fd) {
  967. BIO *wbio = SSL_get_wbio(ssl);
  968. if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
  969. BIO_get_fd(wbio, NULL) != fd) {
  970. BIO *bio = BIO_new(BIO_s_socket());
  971. if (bio == NULL) {
  972. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  973. return 0;
  974. }
  975. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  976. SSL_set0_rbio(ssl, bio);
  977. } else {
  978. /* Copy the wbio over to the rbio. */
  979. BIO_up_ref(wbio);
  980. SSL_set0_rbio(ssl, wbio);
  981. }
  982. return 1;
  983. }
  984. static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
  985. size_t in_len) {
  986. if (out_len > in_len) {
  987. out_len = in_len;
  988. }
  989. OPENSSL_memcpy(out, in, out_len);
  990. return in_len;
  991. }
  992. size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
  993. if (!ssl->s3->initial_handshake_complete ||
  994. ssl3_protocol_version(ssl) < TLS1_VERSION ||
  995. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  996. return 0;
  997. }
  998. if (ssl->server) {
  999. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1000. ssl->s3->previous_server_finished_len);
  1001. }
  1002. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1003. ssl->s3->previous_client_finished_len);
  1004. }
  1005. size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
  1006. if (!ssl->s3->initial_handshake_complete ||
  1007. ssl3_protocol_version(ssl) < TLS1_VERSION ||
  1008. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1009. return 0;
  1010. }
  1011. if (ssl->server) {
  1012. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1013. ssl->s3->previous_client_finished_len);
  1014. }
  1015. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1016. ssl->s3->previous_server_finished_len);
  1017. }
  1018. int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
  1019. int SSL_get_extms_support(const SSL *ssl) {
  1020. /* TLS 1.3 does not require extended master secret and always reports as
  1021. * supporting it. */
  1022. if (!ssl->s3->have_version) {
  1023. return 0;
  1024. }
  1025. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1026. return 1;
  1027. }
  1028. /* If the initial handshake completed, query the established session. */
  1029. if (ssl->s3->established_session != NULL) {
  1030. return ssl->s3->established_session->extended_master_secret;
  1031. }
  1032. /* Otherwise, query the in-progress handshake. */
  1033. if (ssl->s3->hs != NULL) {
  1034. return ssl->s3->hs->extended_master_secret;
  1035. }
  1036. assert(0);
  1037. return 0;
  1038. }
  1039. int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
  1040. int SSL_get_read_ahead(const SSL *ssl) { return 0; }
  1041. void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
  1042. void SSL_set_read_ahead(SSL *ssl, int yes) { }
  1043. int SSL_pending(const SSL *ssl) {
  1044. if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
  1045. return 0;
  1046. }
  1047. return ssl->s3->rrec.length;
  1048. }
  1049. /* Fix this so it checks all the valid key/cert options */
  1050. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  1051. return ssl_cert_check_private_key(ctx->cert, ctx->cert->privatekey);
  1052. }
  1053. /* Fix this function so that it takes an optional type parameter */
  1054. int SSL_check_private_key(const SSL *ssl) {
  1055. return ssl_cert_check_private_key(ssl->cert, ssl->cert->privatekey);
  1056. }
  1057. long SSL_get_default_timeout(const SSL *ssl) {
  1058. return SSL_DEFAULT_SESSION_TIMEOUT;
  1059. }
  1060. int SSL_renegotiate(SSL *ssl) {
  1061. /* Caller-initiated renegotiation is not supported. */
  1062. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1063. return 0;
  1064. }
  1065. int SSL_renegotiate_pending(SSL *ssl) {
  1066. return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
  1067. }
  1068. int SSL_total_renegotiations(const SSL *ssl) {
  1069. return ssl->s3->total_renegotiations;
  1070. }
  1071. size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
  1072. return ctx->max_cert_list;
  1073. }
  1074. void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
  1075. if (max_cert_list > kMaxHandshakeSize) {
  1076. max_cert_list = kMaxHandshakeSize;
  1077. }
  1078. ctx->max_cert_list = (uint32_t)max_cert_list;
  1079. }
  1080. size_t SSL_get_max_cert_list(const SSL *ssl) {
  1081. return ssl->max_cert_list;
  1082. }
  1083. void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
  1084. if (max_cert_list > kMaxHandshakeSize) {
  1085. max_cert_list = kMaxHandshakeSize;
  1086. }
  1087. ssl->max_cert_list = (uint32_t)max_cert_list;
  1088. }
  1089. int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
  1090. if (max_send_fragment < 512) {
  1091. max_send_fragment = 512;
  1092. }
  1093. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1094. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1095. }
  1096. ctx->max_send_fragment = (uint16_t)max_send_fragment;
  1097. return 1;
  1098. }
  1099. int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
  1100. if (max_send_fragment < 512) {
  1101. max_send_fragment = 512;
  1102. }
  1103. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1104. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1105. }
  1106. ssl->max_send_fragment = (uint16_t)max_send_fragment;
  1107. return 1;
  1108. }
  1109. int SSL_set_mtu(SSL *ssl, unsigned mtu) {
  1110. if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
  1111. return 0;
  1112. }
  1113. ssl->d1->mtu = mtu;
  1114. return 1;
  1115. }
  1116. int SSL_get_secure_renegotiation_support(const SSL *ssl) {
  1117. if (!ssl->s3->have_version) {
  1118. return 0;
  1119. }
  1120. return ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
  1121. ssl->s3->send_connection_binding;
  1122. }
  1123. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
  1124. size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
  1125. return lh_SSL_SESSION_num_items(ctx->sessions);
  1126. }
  1127. unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
  1128. unsigned long ret = ctx->session_cache_size;
  1129. ctx->session_cache_size = size;
  1130. return ret;
  1131. }
  1132. unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
  1133. return ctx->session_cache_size;
  1134. }
  1135. int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
  1136. int ret = ctx->session_cache_mode;
  1137. ctx->session_cache_mode = mode;
  1138. return ret;
  1139. }
  1140. int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
  1141. return ctx->session_cache_mode;
  1142. }
  1143. int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
  1144. if (out == NULL) {
  1145. return 48;
  1146. }
  1147. if (len != 48) {
  1148. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1149. return 0;
  1150. }
  1151. uint8_t *out_bytes = out;
  1152. OPENSSL_memcpy(out_bytes, ctx->tlsext_tick_key_name, 16);
  1153. OPENSSL_memcpy(out_bytes + 16, ctx->tlsext_tick_hmac_key, 16);
  1154. OPENSSL_memcpy(out_bytes + 32, ctx->tlsext_tick_aes_key, 16);
  1155. return 1;
  1156. }
  1157. int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
  1158. if (in == NULL) {
  1159. return 48;
  1160. }
  1161. if (len != 48) {
  1162. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1163. return 0;
  1164. }
  1165. const uint8_t *in_bytes = in;
  1166. OPENSSL_memcpy(ctx->tlsext_tick_key_name, in_bytes, 16);
  1167. OPENSSL_memcpy(ctx->tlsext_tick_hmac_key, in_bytes + 16, 16);
  1168. OPENSSL_memcpy(ctx->tlsext_tick_aes_key, in_bytes + 32, 16);
  1169. return 1;
  1170. }
  1171. int SSL_CTX_set_tlsext_ticket_key_cb(
  1172. SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
  1173. EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
  1174. int encrypt)) {
  1175. ctx->tlsext_ticket_key_cb = callback;
  1176. return 1;
  1177. }
  1178. int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
  1179. return tls1_set_curves(&ctx->supported_group_list,
  1180. &ctx->supported_group_list_len, curves,
  1181. curves_len);
  1182. }
  1183. int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
  1184. return tls1_set_curves(&ssl->supported_group_list,
  1185. &ssl->supported_group_list_len, curves,
  1186. curves_len);
  1187. }
  1188. int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
  1189. return tls1_set_curves_list(&ctx->supported_group_list,
  1190. &ctx->supported_group_list_len, curves);
  1191. }
  1192. int SSL_set1_curves_list(SSL *ssl, const char *curves) {
  1193. return tls1_set_curves_list(&ssl->supported_group_list,
  1194. &ssl->supported_group_list_len, curves);
  1195. }
  1196. uint16_t SSL_get_curve_id(const SSL *ssl) {
  1197. /* TODO(davidben): This checks the wrong session if there is a renegotiation in
  1198. * progress. */
  1199. SSL_SESSION *session = SSL_get_session(ssl);
  1200. if (session == NULL) {
  1201. return 0;
  1202. }
  1203. return session->group_id;
  1204. }
  1205. int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
  1206. DH_free(ctx->cert->dh_tmp);
  1207. ctx->cert->dh_tmp = DHparams_dup(dh);
  1208. if (ctx->cert->dh_tmp == NULL) {
  1209. OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
  1210. return 0;
  1211. }
  1212. return 1;
  1213. }
  1214. int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
  1215. DH_free(ssl->cert->dh_tmp);
  1216. ssl->cert->dh_tmp = DHparams_dup(dh);
  1217. if (ssl->cert->dh_tmp == NULL) {
  1218. OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
  1219. return 0;
  1220. }
  1221. return 1;
  1222. }
  1223. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
  1224. if (ssl == NULL) {
  1225. return NULL;
  1226. }
  1227. const struct ssl_cipher_preference_list_st *prefs =
  1228. ssl_get_cipher_preferences(ssl);
  1229. if (prefs == NULL) {
  1230. return NULL;
  1231. }
  1232. return prefs->ciphers;
  1233. }
  1234. const char *SSL_get_cipher_list(const SSL *ssl, int n) {
  1235. const SSL_CIPHER *c;
  1236. STACK_OF(SSL_CIPHER) *sk;
  1237. if (ssl == NULL) {
  1238. return NULL;
  1239. }
  1240. sk = SSL_get_ciphers(ssl);
  1241. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  1242. return NULL;
  1243. }
  1244. c = sk_SSL_CIPHER_value(sk, n);
  1245. if (c == NULL) {
  1246. return NULL;
  1247. }
  1248. return c->name;
  1249. }
  1250. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  1251. STACK_OF(SSL_CIPHER) *cipher_list =
  1252. ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
  1253. 0 /* not strict */);
  1254. if (cipher_list == NULL) {
  1255. return 0;
  1256. }
  1257. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1258. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1259. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1260. return 0;
  1261. }
  1262. return 1;
  1263. }
  1264. int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
  1265. STACK_OF(SSL_CIPHER) *cipher_list =
  1266. ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
  1267. 1 /* strict */);
  1268. if (cipher_list == NULL) {
  1269. return 0;
  1270. }
  1271. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1272. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1273. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1274. return 0;
  1275. }
  1276. return 1;
  1277. }
  1278. int SSL_set_cipher_list(SSL *ssl, const char *str) {
  1279. STACK_OF(SSL_CIPHER) *cipher_list =
  1280. ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
  1281. 0 /* not strict */);
  1282. if (cipher_list == NULL) {
  1283. return 0;
  1284. }
  1285. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1286. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1287. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1288. return 0;
  1289. }
  1290. return 1;
  1291. }
  1292. int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
  1293. STACK_OF(SSL_CIPHER) *cipher_list =
  1294. ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
  1295. 1 /* strict */);
  1296. if (cipher_list == NULL) {
  1297. return 0;
  1298. }
  1299. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1300. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1301. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1302. return 0;
  1303. }
  1304. return 1;
  1305. }
  1306. const char *SSL_get_servername(const SSL *ssl, const int type) {
  1307. if (type != TLSEXT_NAMETYPE_host_name) {
  1308. return NULL;
  1309. }
  1310. /* Historically, |SSL_get_servername| was also the configuration getter
  1311. * corresponding to |SSL_set_tlsext_host_name|. */
  1312. if (ssl->tlsext_hostname != NULL) {
  1313. return ssl->tlsext_hostname;
  1314. }
  1315. /* During the handshake, report the handshake value. */
  1316. if (ssl->s3->hs != NULL) {
  1317. return ssl->s3->hs->hostname;
  1318. }
  1319. /* SSL_get_servername may also be called after the handshake to look up the
  1320. * SNI value.
  1321. *
  1322. * TODO(davidben): This is almost unused. Can we remove it? */
  1323. SSL_SESSION *session = SSL_get_session(ssl);
  1324. if (session == NULL) {
  1325. return NULL;
  1326. }
  1327. return session->tlsext_hostname;
  1328. }
  1329. int SSL_get_servername_type(const SSL *ssl) {
  1330. SSL_SESSION *session = SSL_get_session(ssl);
  1331. if (session == NULL || session->tlsext_hostname == NULL) {
  1332. return -1;
  1333. }
  1334. return TLSEXT_NAMETYPE_host_name;
  1335. }
  1336. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1337. ctx->signed_cert_timestamps_enabled = 1;
  1338. }
  1339. void SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1340. ssl->signed_cert_timestamps_enabled = 1;
  1341. }
  1342. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1343. ctx->ocsp_stapling_enabled = 1;
  1344. }
  1345. void SSL_enable_ocsp_stapling(SSL *ssl) {
  1346. ssl->ocsp_stapling_enabled = 1;
  1347. }
  1348. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1349. size_t *out_len) {
  1350. SSL_SESSION *session = SSL_get_session(ssl);
  1351. *out_len = 0;
  1352. *out = NULL;
  1353. if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
  1354. return;
  1355. }
  1356. *out = session->tlsext_signed_cert_timestamp_list;
  1357. *out_len = session->tlsext_signed_cert_timestamp_list_length;
  1358. }
  1359. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1360. size_t *out_len) {
  1361. SSL_SESSION *session = SSL_get_session(ssl);
  1362. *out_len = 0;
  1363. *out = NULL;
  1364. if (ssl->server || !session || !session->ocsp_response) {
  1365. return;
  1366. }
  1367. *out = session->ocsp_response;
  1368. *out_len = session->ocsp_response_length;
  1369. }
  1370. int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
  1371. OPENSSL_free(ssl->tlsext_hostname);
  1372. ssl->tlsext_hostname = NULL;
  1373. if (name == NULL) {
  1374. return 1;
  1375. }
  1376. size_t len = strlen(name);
  1377. if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
  1378. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
  1379. return 0;
  1380. }
  1381. ssl->tlsext_hostname = BUF_strdup(name);
  1382. if (ssl->tlsext_hostname == NULL) {
  1383. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1384. return 0;
  1385. }
  1386. return 1;
  1387. }
  1388. int SSL_CTX_set_tlsext_servername_callback(
  1389. SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
  1390. ctx->tlsext_servername_callback = callback;
  1391. return 1;
  1392. }
  1393. int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
  1394. ctx->tlsext_servername_arg = arg;
  1395. return 1;
  1396. }
  1397. int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
  1398. const uint8_t *server, unsigned server_len,
  1399. const uint8_t *client, unsigned client_len) {
  1400. unsigned int i, j;
  1401. const uint8_t *result;
  1402. int status = OPENSSL_NPN_UNSUPPORTED;
  1403. /* For each protocol in server preference order, see if we support it. */
  1404. for (i = 0; i < server_len;) {
  1405. for (j = 0; j < client_len;) {
  1406. if (server[i] == client[j] &&
  1407. OPENSSL_memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  1408. /* We found a match */
  1409. result = &server[i];
  1410. status = OPENSSL_NPN_NEGOTIATED;
  1411. goto found;
  1412. }
  1413. j += client[j];
  1414. j++;
  1415. }
  1416. i += server[i];
  1417. i++;
  1418. }
  1419. /* There's no overlap between our protocols and the server's list. */
  1420. result = client;
  1421. status = OPENSSL_NPN_NO_OVERLAP;
  1422. found:
  1423. *out = (uint8_t *)result + 1;
  1424. *out_len = result[0];
  1425. return status;
  1426. }
  1427. void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
  1428. unsigned *out_len) {
  1429. *out_data = ssl->s3->next_proto_negotiated;
  1430. if (*out_data == NULL) {
  1431. *out_len = 0;
  1432. } else {
  1433. *out_len = ssl->s3->next_proto_negotiated_len;
  1434. }
  1435. }
  1436. void SSL_CTX_set_next_protos_advertised_cb(
  1437. SSL_CTX *ctx,
  1438. int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
  1439. void *arg) {
  1440. ctx->next_protos_advertised_cb = cb;
  1441. ctx->next_protos_advertised_cb_arg = arg;
  1442. }
  1443. void SSL_CTX_set_next_proto_select_cb(
  1444. SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
  1445. const uint8_t *in, unsigned in_len, void *arg),
  1446. void *arg) {
  1447. ctx->next_proto_select_cb = cb;
  1448. ctx->next_proto_select_cb_arg = arg;
  1449. }
  1450. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1451. unsigned protos_len) {
  1452. OPENSSL_free(ctx->alpn_client_proto_list);
  1453. ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1454. if (!ctx->alpn_client_proto_list) {
  1455. return 1;
  1456. }
  1457. ctx->alpn_client_proto_list_len = protos_len;
  1458. return 0;
  1459. }
  1460. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1461. OPENSSL_free(ssl->alpn_client_proto_list);
  1462. ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1463. if (!ssl->alpn_client_proto_list) {
  1464. return 1;
  1465. }
  1466. ssl->alpn_client_proto_list_len = protos_len;
  1467. return 0;
  1468. }
  1469. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1470. int (*cb)(SSL *ssl, const uint8_t **out,
  1471. uint8_t *out_len, const uint8_t *in,
  1472. unsigned in_len, void *arg),
  1473. void *arg) {
  1474. ctx->alpn_select_cb = cb;
  1475. ctx->alpn_select_cb_arg = arg;
  1476. }
  1477. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
  1478. unsigned *out_len) {
  1479. *out_data = NULL;
  1480. if (ssl->s3) {
  1481. *out_data = ssl->s3->alpn_selected;
  1482. }
  1483. if (*out_data == NULL) {
  1484. *out_len = 0;
  1485. } else {
  1486. *out_len = ssl->s3->alpn_selected_len;
  1487. }
  1488. }
  1489. void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
  1490. ctx->tlsext_channel_id_enabled = !!enabled;
  1491. }
  1492. int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
  1493. SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
  1494. return 1;
  1495. }
  1496. void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
  1497. ssl->tlsext_channel_id_enabled = !!enabled;
  1498. }
  1499. int SSL_enable_tls_channel_id(SSL *ssl) {
  1500. SSL_set_tls_channel_id_enabled(ssl, 1);
  1501. return 1;
  1502. }
  1503. static int is_p256_key(EVP_PKEY *private_key) {
  1504. const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
  1505. return ec_key != NULL &&
  1506. EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
  1507. NID_X9_62_prime256v1;
  1508. }
  1509. int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
  1510. if (!is_p256_key(private_key)) {
  1511. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1512. return 0;
  1513. }
  1514. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  1515. EVP_PKEY_up_ref(private_key);
  1516. ctx->tlsext_channel_id_private = private_key;
  1517. ctx->tlsext_channel_id_enabled = 1;
  1518. return 1;
  1519. }
  1520. int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
  1521. if (!is_p256_key(private_key)) {
  1522. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1523. return 0;
  1524. }
  1525. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  1526. EVP_PKEY_up_ref(private_key);
  1527. ssl->tlsext_channel_id_private = private_key;
  1528. ssl->tlsext_channel_id_enabled = 1;
  1529. return 1;
  1530. }
  1531. size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
  1532. if (!ssl->s3->tlsext_channel_id_valid) {
  1533. return 0;
  1534. }
  1535. OPENSSL_memcpy(out, ssl->s3->tlsext_channel_id,
  1536. (max_out < 64) ? max_out : 64);
  1537. return 64;
  1538. }
  1539. size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
  1540. if (ssl->server || ssl->s3->hs == NULL) {
  1541. *out_types = NULL;
  1542. return 0;
  1543. }
  1544. *out_types = ssl->s3->hs->certificate_types;
  1545. return ssl->s3->hs->num_certificate_types;
  1546. }
  1547. void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) {
  1548. SSL *const ssl = hs->ssl;
  1549. SSL_CTX *ctx = ssl->initial_ctx;
  1550. /* Never cache sessions with empty session IDs. */
  1551. if (ssl->s3->established_session->session_id_length == 0 ||
  1552. (ctx->session_cache_mode & mode) != mode) {
  1553. return;
  1554. }
  1555. /* Clients never use the internal session cache. */
  1556. int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
  1557. SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1558. /* A client may see new sessions on abbreviated handshakes if the server
  1559. * decides to renew the ticket. Once the handshake is completed, it should be
  1560. * inserted into the cache. */
  1561. if (ssl->s3->established_session != ssl->session ||
  1562. (!ssl->server && hs->ticket_expected)) {
  1563. if (use_internal_cache) {
  1564. SSL_CTX_add_session(ctx, ssl->s3->established_session);
  1565. }
  1566. if (ctx->new_session_cb != NULL) {
  1567. SSL_SESSION_up_ref(ssl->s3->established_session);
  1568. if (!ctx->new_session_cb(ssl, ssl->s3->established_session)) {
  1569. /* |new_session_cb|'s return value signals whether it took ownership. */
  1570. SSL_SESSION_free(ssl->s3->established_session);
  1571. }
  1572. }
  1573. }
  1574. if (use_internal_cache &&
  1575. !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
  1576. /* Automatically flush the internal session cache every 255 connections. */
  1577. int flush_cache = 0;
  1578. CRYPTO_MUTEX_lock_write(&ctx->lock);
  1579. ctx->handshakes_since_cache_flush++;
  1580. if (ctx->handshakes_since_cache_flush >= 255) {
  1581. flush_cache = 1;
  1582. ctx->handshakes_since_cache_flush = 0;
  1583. }
  1584. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  1585. if (flush_cache) {
  1586. struct timeval now;
  1587. ssl_get_current_time(ssl, &now);
  1588. SSL_CTX_flush_sessions(ctx, (long)now.tv_sec);
  1589. }
  1590. }
  1591. }
  1592. static const char *ssl_get_version(int version) {
  1593. switch (version) {
  1594. /* Report TLS 1.3 draft version as TLS 1.3 in the public API. */
  1595. case TLS1_3_DRAFT_VERSION:
  1596. return "TLSv1.3";
  1597. case TLS1_2_VERSION:
  1598. return "TLSv1.2";
  1599. case TLS1_1_VERSION:
  1600. return "TLSv1.1";
  1601. case TLS1_VERSION:
  1602. return "TLSv1";
  1603. case SSL3_VERSION:
  1604. return "SSLv3";
  1605. case DTLS1_VERSION:
  1606. return "DTLSv1";
  1607. case DTLS1_2_VERSION:
  1608. return "DTLSv1.2";
  1609. default:
  1610. return "unknown";
  1611. }
  1612. }
  1613. const char *SSL_get_version(const SSL *ssl) {
  1614. return ssl_get_version(ssl->version);
  1615. }
  1616. const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
  1617. return ssl_get_version(session->ssl_version);
  1618. }
  1619. EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
  1620. if (ssl->cert != NULL) {
  1621. return ssl->cert->privatekey;
  1622. }
  1623. return NULL;
  1624. }
  1625. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  1626. if (ctx->cert != NULL) {
  1627. return ctx->cert->privatekey;
  1628. }
  1629. return NULL;
  1630. }
  1631. const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
  1632. if (ssl->s3->aead_write_ctx == NULL) {
  1633. return NULL;
  1634. }
  1635. return ssl->s3->aead_write_ctx->cipher;
  1636. }
  1637. int SSL_session_reused(const SSL *ssl) {
  1638. return ssl->s3->session_reused;
  1639. }
  1640. const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
  1641. const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
  1642. int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
  1643. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  1644. ctx->quiet_shutdown = (mode != 0);
  1645. }
  1646. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  1647. return ctx->quiet_shutdown;
  1648. }
  1649. void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
  1650. ssl->quiet_shutdown = (mode != 0);
  1651. }
  1652. int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
  1653. void SSL_set_shutdown(SSL *ssl, int mode) {
  1654. /* It is an error to clear any bits that have already been set. (We can't try
  1655. * to get a second close_notify or send two.) */
  1656. assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
  1657. if (mode & SSL_RECEIVED_SHUTDOWN &&
  1658. ssl->s3->recv_shutdown == ssl_shutdown_none) {
  1659. ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
  1660. }
  1661. if (mode & SSL_SENT_SHUTDOWN &&
  1662. ssl->s3->send_shutdown == ssl_shutdown_none) {
  1663. ssl->s3->send_shutdown = ssl_shutdown_close_notify;
  1664. }
  1665. }
  1666. int SSL_get_shutdown(const SSL *ssl) {
  1667. int ret = 0;
  1668. if (ssl->s3->recv_shutdown != ssl_shutdown_none) {
  1669. /* Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
  1670. * and fatal alert. */
  1671. ret |= SSL_RECEIVED_SHUTDOWN;
  1672. }
  1673. if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) {
  1674. /* Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify. */
  1675. ret |= SSL_SENT_SHUTDOWN;
  1676. }
  1677. return ret;
  1678. }
  1679. int SSL_version(const SSL *ssl) {
  1680. /* Report TLS 1.3 draft version as TLS 1.3 in the public API. */
  1681. if (ssl->version == TLS1_3_DRAFT_VERSION) {
  1682. return TLS1_3_VERSION;
  1683. }
  1684. return ssl->version;
  1685. }
  1686. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
  1687. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  1688. if (ssl->ctx == ctx) {
  1689. return ssl->ctx;
  1690. }
  1691. /* One cannot change the X.509 callbacks during a connection. */
  1692. if (ssl->ctx->x509_method != ctx->x509_method) {
  1693. assert(0);
  1694. return NULL;
  1695. }
  1696. if (ctx == NULL) {
  1697. ctx = ssl->initial_ctx;
  1698. }
  1699. ssl_cert_free(ssl->cert);
  1700. ssl->cert = ssl_cert_dup(ctx->cert);
  1701. SSL_CTX_up_ref(ctx);
  1702. SSL_CTX_free(ssl->ctx);
  1703. ssl->ctx = ctx;
  1704. return ssl->ctx;
  1705. }
  1706. void SSL_set_info_callback(SSL *ssl,
  1707. void (*cb)(const SSL *ssl, int type, int value)) {
  1708. ssl->info_callback = cb;
  1709. }
  1710. void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
  1711. int value) {
  1712. return ssl->info_callback;
  1713. }
  1714. int SSL_state(const SSL *ssl) {
  1715. return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
  1716. }
  1717. void SSL_set_state(SSL *ssl, int state) { }
  1718. char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
  1719. if (len <= 0) {
  1720. return NULL;
  1721. }
  1722. buf[0] = '\0';
  1723. return buf;
  1724. }
  1725. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1726. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  1727. int index;
  1728. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  1729. dup_func, free_func)) {
  1730. return -1;
  1731. }
  1732. return index;
  1733. }
  1734. int SSL_set_ex_data(SSL *ssl, int idx, void *arg) {
  1735. return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg);
  1736. }
  1737. void *SSL_get_ex_data(const SSL *ssl, int idx) {
  1738. return CRYPTO_get_ex_data(&ssl->ex_data, idx);
  1739. }
  1740. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1741. CRYPTO_EX_dup *dup_func,
  1742. CRYPTO_EX_free *free_func) {
  1743. int index;
  1744. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  1745. dup_func, free_func)) {
  1746. return -1;
  1747. }
  1748. return index;
  1749. }
  1750. int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) {
  1751. return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg);
  1752. }
  1753. void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
  1754. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1755. }
  1756. int SSL_want(const SSL *ssl) { return ssl->rwstate; }
  1757. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  1758. RSA *(*cb)(SSL *ssl, int is_export,
  1759. int keylength)) {
  1760. }
  1761. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  1762. int keylength)) {
  1763. }
  1764. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  1765. DH *(*callback)(SSL *ssl, int is_export,
  1766. int keylength)) {
  1767. ctx->cert->dh_tmp_cb = callback;
  1768. }
  1769. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
  1770. int keylength)) {
  1771. ssl->cert->dh_tmp_cb = callback;
  1772. }
  1773. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  1774. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1775. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1776. return 0;
  1777. }
  1778. OPENSSL_free(ctx->psk_identity_hint);
  1779. if (identity_hint != NULL) {
  1780. ctx->psk_identity_hint = BUF_strdup(identity_hint);
  1781. if (ctx->psk_identity_hint == NULL) {
  1782. return 0;
  1783. }
  1784. } else {
  1785. ctx->psk_identity_hint = NULL;
  1786. }
  1787. return 1;
  1788. }
  1789. int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
  1790. if (ssl == NULL) {
  1791. return 0;
  1792. }
  1793. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1794. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1795. return 0;
  1796. }
  1797. /* Clear currently configured hint, if any. */
  1798. OPENSSL_free(ssl->psk_identity_hint);
  1799. ssl->psk_identity_hint = NULL;
  1800. /* Treat the empty hint as not supplying one. Plain PSK makes it possible to
  1801. * send either no hint (omit ServerKeyExchange) or an empty hint, while
  1802. * ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
  1803. * so we interpret empty and missing as identical. */
  1804. if (identity_hint != NULL && identity_hint[0] != '\0') {
  1805. ssl->psk_identity_hint = BUF_strdup(identity_hint);
  1806. if (ssl->psk_identity_hint == NULL) {
  1807. return 0;
  1808. }
  1809. }
  1810. return 1;
  1811. }
  1812. const char *SSL_get_psk_identity_hint(const SSL *ssl) {
  1813. if (ssl == NULL) {
  1814. return NULL;
  1815. }
  1816. return ssl->psk_identity_hint;
  1817. }
  1818. const char *SSL_get_psk_identity(const SSL *ssl) {
  1819. if (ssl == NULL) {
  1820. return NULL;
  1821. }
  1822. SSL_SESSION *session = SSL_get_session(ssl);
  1823. if (session == NULL) {
  1824. return NULL;
  1825. }
  1826. return session->psk_identity;
  1827. }
  1828. void SSL_set_psk_client_callback(
  1829. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1830. unsigned max_identity_len, uint8_t *psk,
  1831. unsigned max_psk_len)) {
  1832. ssl->psk_client_callback = cb;
  1833. }
  1834. void SSL_CTX_set_psk_client_callback(
  1835. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1836. unsigned max_identity_len, uint8_t *psk,
  1837. unsigned max_psk_len)) {
  1838. ctx->psk_client_callback = cb;
  1839. }
  1840. void SSL_set_psk_server_callback(
  1841. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  1842. unsigned max_psk_len)) {
  1843. ssl->psk_server_callback = cb;
  1844. }
  1845. void SSL_CTX_set_psk_server_callback(
  1846. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
  1847. uint8_t *psk, unsigned max_psk_len)) {
  1848. ctx->psk_server_callback = cb;
  1849. }
  1850. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  1851. void (*cb)(int write_p, int version,
  1852. int content_type, const void *buf,
  1853. size_t len, SSL *ssl, void *arg)) {
  1854. ctx->msg_callback = cb;
  1855. }
  1856. void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
  1857. ctx->msg_callback_arg = arg;
  1858. }
  1859. void SSL_set_msg_callback(SSL *ssl,
  1860. void (*cb)(int write_p, int version, int content_type,
  1861. const void *buf, size_t len, SSL *ssl,
  1862. void *arg)) {
  1863. ssl->msg_callback = cb;
  1864. }
  1865. void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
  1866. ssl->msg_callback_arg = arg;
  1867. }
  1868. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
  1869. void (*cb)(const SSL *ssl, const char *line)) {
  1870. ctx->keylog_callback = cb;
  1871. }
  1872. void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
  1873. const char *line) {
  1874. return ctx->keylog_callback;
  1875. }
  1876. void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
  1877. void (*cb)(const SSL *ssl,
  1878. struct timeval *out_clock)) {
  1879. ctx->current_time_cb = cb;
  1880. }
  1881. static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
  1882. static const char hextable[] = "0123456789abcdef";
  1883. uint8_t *out;
  1884. if (!CBB_add_space(cbb, &out, in_len * 2)) {
  1885. return 0;
  1886. }
  1887. for (size_t i = 0; i < in_len; i++) {
  1888. *(out++) = (uint8_t)hextable[in[i] >> 4];
  1889. *(out++) = (uint8_t)hextable[in[i] & 0xf];
  1890. }
  1891. return 1;
  1892. }
  1893. int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
  1894. size_t secret_len) {
  1895. if (ssl->ctx->keylog_callback == NULL) {
  1896. return 1;
  1897. }
  1898. CBB cbb;
  1899. uint8_t *out;
  1900. size_t out_len;
  1901. if (!CBB_init(&cbb, strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
  1902. secret_len * 2 + 1) ||
  1903. !CBB_add_bytes(&cbb, (const uint8_t *)label, strlen(label)) ||
  1904. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  1905. !cbb_add_hex(&cbb, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  1906. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  1907. !cbb_add_hex(&cbb, secret, secret_len) ||
  1908. !CBB_add_u8(&cbb, 0 /* NUL */) ||
  1909. !CBB_finish(&cbb, &out, &out_len)) {
  1910. CBB_cleanup(&cbb);
  1911. return 0;
  1912. }
  1913. ssl->ctx->keylog_callback(ssl, (const char *)out);
  1914. OPENSSL_free(out);
  1915. return 1;
  1916. }
  1917. int SSL_is_init_finished(const SSL *ssl) {
  1918. return !SSL_in_init(ssl);
  1919. }
  1920. int SSL_in_init(const SSL *ssl) {
  1921. SSL_HANDSHAKE *hs = ssl->s3->hs;
  1922. return hs != NULL && hs->state != SSL_ST_OK;
  1923. }
  1924. int SSL_in_false_start(const SSL *ssl) {
  1925. if (ssl->s3->hs == NULL) {
  1926. return 0;
  1927. }
  1928. return ssl->s3->hs->in_false_start;
  1929. }
  1930. int SSL_cutthrough_complete(const SSL *ssl) {
  1931. return SSL_in_false_start(ssl);
  1932. }
  1933. void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
  1934. size_t *ssl_session_size) {
  1935. *ssl_size = sizeof(SSL);
  1936. *ssl_ctx_size = sizeof(SSL_CTX);
  1937. *ssl_session_size = sizeof(SSL_SESSION);
  1938. }
  1939. int ssl3_can_false_start(const SSL *ssl) {
  1940. const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl);
  1941. /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
  1942. return !SSL_is_dtls(ssl) &&
  1943. SSL_version(ssl) == TLS1_2_VERSION &&
  1944. (ssl->s3->alpn_selected != NULL ||
  1945. ssl->s3->next_proto_negotiated != NULL) &&
  1946. cipher != NULL &&
  1947. cipher->algorithm_mkey == SSL_kECDHE &&
  1948. cipher->algorithm_mac == SSL_AEAD;
  1949. }
  1950. const struct {
  1951. uint16_t version;
  1952. uint32_t flag;
  1953. } kVersions[] = {
  1954. {SSL3_VERSION, SSL_OP_NO_SSLv3},
  1955. {TLS1_VERSION, SSL_OP_NO_TLSv1},
  1956. {TLS1_1_VERSION, SSL_OP_NO_TLSv1_1},
  1957. {TLS1_2_VERSION, SSL_OP_NO_TLSv1_2},
  1958. {TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
  1959. };
  1960. static const size_t kVersionsLen = OPENSSL_ARRAY_SIZE(kVersions);
  1961. int ssl_get_version_range(const SSL *ssl, uint16_t *out_min_version,
  1962. uint16_t *out_max_version) {
  1963. /* For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
  1964. * DTLS 1.0 should be mapped to TLS 1.1. */
  1965. uint32_t options = ssl->options;
  1966. if (SSL_is_dtls(ssl)) {
  1967. options &= ~SSL_OP_NO_TLSv1_1;
  1968. if (options & SSL_OP_NO_DTLSv1) {
  1969. options |= SSL_OP_NO_TLSv1_1;
  1970. }
  1971. }
  1972. uint16_t min_version = ssl->min_version;
  1973. uint16_t max_version = ssl->max_version;
  1974. /* Bound the range to only those implemented in this protocol. */
  1975. if (min_version < ssl->method->min_version) {
  1976. min_version = ssl->method->min_version;
  1977. }
  1978. if (max_version > ssl->method->max_version) {
  1979. max_version = ssl->method->max_version;
  1980. }
  1981. /* OpenSSL's API for controlling versions entails blacklisting individual
  1982. * protocols. This has two problems. First, on the client, the protocol can
  1983. * only express a contiguous range of versions. Second, a library consumer
  1984. * trying to set a maximum version cannot disable protocol versions that get
  1985. * added in a future version of the library.
  1986. *
  1987. * To account for both of these, OpenSSL interprets the client-side bitmask
  1988. * as a min/max range by picking the lowest contiguous non-empty range of
  1989. * enabled protocols. Note that this means it is impossible to set a maximum
  1990. * version of the higest supported TLS version in a future-proof way. */
  1991. int any_enabled = 0;
  1992. for (size_t i = 0; i < kVersionsLen; i++) {
  1993. /* Only look at the versions already enabled. */
  1994. if (min_version > kVersions[i].version) {
  1995. continue;
  1996. }
  1997. if (max_version < kVersions[i].version) {
  1998. break;
  1999. }
  2000. if (!(options & kVersions[i].flag)) {
  2001. /* The minimum version is the first enabled version. */
  2002. if (!any_enabled) {
  2003. any_enabled = 1;
  2004. min_version = kVersions[i].version;
  2005. }
  2006. continue;
  2007. }
  2008. /* If there is a disabled version after the first enabled one, all versions
  2009. * after it are implicitly disabled. */
  2010. if (any_enabled) {
  2011. max_version = kVersions[i-1].version;
  2012. break;
  2013. }
  2014. }
  2015. if (!any_enabled) {
  2016. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  2017. return 0;
  2018. }
  2019. *out_min_version = min_version;
  2020. *out_max_version = max_version;
  2021. return 1;
  2022. }
  2023. uint16_t ssl3_protocol_version(const SSL *ssl) {
  2024. assert(ssl->s3->have_version);
  2025. uint16_t version;
  2026. if (!ssl->method->version_from_wire(&version, ssl->version)) {
  2027. /* TODO(davidben): Use the internal version representation for ssl->version
  2028. * and map to the public API representation at API boundaries. */
  2029. assert(0);
  2030. return 0;
  2031. }
  2032. return version;
  2033. }
  2034. int SSL_is_server(const SSL *ssl) { return ssl->server; }
  2035. int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
  2036. void SSL_CTX_set_select_certificate_cb(SSL_CTX *ctx,
  2037. int (*cb)(const SSL_CLIENT_HELLO *)) {
  2038. ctx->select_certificate_cb = cb;
  2039. }
  2040. void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
  2041. int (*cb)(const SSL_CLIENT_HELLO *)) {
  2042. ctx->dos_protection_cb = cb;
  2043. }
  2044. void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
  2045. ssl->renegotiate_mode = mode;
  2046. }
  2047. int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
  2048. const uint8_t **out_write_iv, size_t *out_iv_len) {
  2049. if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
  2050. return 0;
  2051. }
  2052. size_t write_iv_len;
  2053. if (!EVP_AEAD_CTX_get_iv(&ssl->s3->aead_read_ctx->ctx, out_read_iv,
  2054. out_iv_len) ||
  2055. !EVP_AEAD_CTX_get_iv(&ssl->s3->aead_write_ctx->ctx, out_write_iv,
  2056. &write_iv_len) ||
  2057. *out_iv_len != write_iv_len) {
  2058. return 0;
  2059. }
  2060. return 1;
  2061. }
  2062. static uint64_t be_to_u64(const uint8_t in[8]) {
  2063. return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
  2064. (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
  2065. (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
  2066. (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
  2067. }
  2068. uint64_t SSL_get_read_sequence(const SSL *ssl) {
  2069. /* TODO(davidben): Internally represent sequence numbers as uint64_t. */
  2070. if (SSL_is_dtls(ssl)) {
  2071. /* max_seq_num already includes the epoch. */
  2072. assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
  2073. return ssl->d1->bitmap.max_seq_num;
  2074. }
  2075. return be_to_u64(ssl->s3->read_sequence);
  2076. }
  2077. uint64_t SSL_get_write_sequence(const SSL *ssl) {
  2078. uint64_t ret = be_to_u64(ssl->s3->write_sequence);
  2079. if (SSL_is_dtls(ssl)) {
  2080. assert((ret >> 48) == 0);
  2081. ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
  2082. }
  2083. return ret;
  2084. }
  2085. uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
  2086. /* TODO(davidben): This checks the wrong session if there is a renegotiation
  2087. * in progress. */
  2088. SSL_SESSION *session = SSL_get_session(ssl);
  2089. if (session == NULL) {
  2090. return 0;
  2091. }
  2092. return session->peer_signature_algorithm;
  2093. }
  2094. size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2095. if (max_out == 0) {
  2096. return sizeof(ssl->s3->client_random);
  2097. }
  2098. if (max_out > sizeof(ssl->s3->client_random)) {
  2099. max_out = sizeof(ssl->s3->client_random);
  2100. }
  2101. OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
  2102. return max_out;
  2103. }
  2104. size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2105. if (max_out == 0) {
  2106. return sizeof(ssl->s3->server_random);
  2107. }
  2108. if (max_out > sizeof(ssl->s3->server_random)) {
  2109. max_out = sizeof(ssl->s3->server_random);
  2110. }
  2111. OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
  2112. return max_out;
  2113. }
  2114. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
  2115. SSL_HANDSHAKE *hs = ssl->s3->hs;
  2116. if (hs == NULL) {
  2117. return NULL;
  2118. }
  2119. return hs->new_cipher;
  2120. }
  2121. void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
  2122. ssl->retain_only_sha256_of_client_certs = !!enabled;
  2123. }
  2124. void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
  2125. ctx->retain_only_sha256_of_client_certs = !!enabled;
  2126. }
  2127. void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
  2128. ctx->grease_enabled = !!enabled;
  2129. }
  2130. void SSL_CTX_set_short_header_enabled(SSL_CTX *ctx, int enabled) {
  2131. ctx->short_header_enabled = !!enabled;
  2132. }
  2133. int SSL_clear(SSL *ssl) {
  2134. /* In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
  2135. * established session to be offered the next time around. wpa_supplicant
  2136. * depends on this behavior, so emulate it. */
  2137. SSL_SESSION *session = NULL;
  2138. if (!ssl->server && ssl->s3->established_session != NULL) {
  2139. session = ssl->s3->established_session;
  2140. SSL_SESSION_up_ref(session);
  2141. }
  2142. /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
  2143. * |SSL_clear| because it is per-connection state rather than configuration
  2144. * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
  2145. * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
  2146. * |ssl3_new|. */
  2147. ssl->rwstate = SSL_NOTHING;
  2148. BUF_MEM_free(ssl->init_buf);
  2149. ssl->init_buf = NULL;
  2150. ssl->init_msg = NULL;
  2151. ssl->init_num = 0;
  2152. /* The ssl->d1->mtu is simultaneously configuration (preserved across
  2153. * clear) and connection-specific state (gets reset).
  2154. *
  2155. * TODO(davidben): Avoid this. */
  2156. unsigned mtu = 0;
  2157. if (ssl->d1 != NULL) {
  2158. mtu = ssl->d1->mtu;
  2159. }
  2160. ssl->method->ssl_free(ssl);
  2161. if (!ssl->method->ssl_new(ssl)) {
  2162. SSL_SESSION_free(session);
  2163. return 0;
  2164. }
  2165. if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  2166. ssl->d1->mtu = mtu;
  2167. }
  2168. if (session != NULL) {
  2169. SSL_set_session(ssl, session);
  2170. SSL_SESSION_free(session);
  2171. }
  2172. return 1;
  2173. }
  2174. void ssl_do_info_callback(const SSL *ssl, int type, int value) {
  2175. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  2176. if (ssl->info_callback != NULL) {
  2177. cb = ssl->info_callback;
  2178. } else if (ssl->ctx->info_callback != NULL) {
  2179. cb = ssl->ctx->info_callback;
  2180. }
  2181. if (cb != NULL) {
  2182. cb(ssl, type, value);
  2183. }
  2184. }
  2185. void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
  2186. const void *buf, size_t len) {
  2187. if (ssl->msg_callback == NULL) {
  2188. return;
  2189. }
  2190. /* |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
  2191. * a V2ClientHello. */
  2192. int version;
  2193. switch (content_type) {
  2194. case 0:
  2195. /* V2ClientHello */
  2196. version = SSL2_VERSION;
  2197. break;
  2198. case SSL3_RT_HEADER:
  2199. version = 0;
  2200. break;
  2201. default:
  2202. version = SSL_version(ssl);
  2203. }
  2204. ssl->msg_callback(is_write, version, content_type, buf, len, ssl,
  2205. ssl->msg_callback_arg);
  2206. }
  2207. int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
  2208. int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
  2209. int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
  2210. int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
  2211. int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
  2212. int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
  2213. int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
  2214. int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
  2215. int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
  2216. int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
  2217. int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
  2218. int SSL_num_renegotiations(const SSL *ssl) {
  2219. return SSL_total_renegotiations(ssl);
  2220. }
  2221. int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
  2222. int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
  2223. int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
  2224. int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
  2225. void ERR_load_SSL_strings(void) {}
  2226. void SSL_load_error_strings(void) {}
  2227. int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
  2228. int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
  2229. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2230. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2231. return 0;
  2232. }
  2233. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2234. return SSL_CTX_set1_curves(ctx, &nid, 1);
  2235. }
  2236. int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
  2237. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2238. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2239. return 0;
  2240. }
  2241. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2242. return SSL_set1_curves(ssl, &nid, 1);
  2243. }
  2244. void ssl_get_current_time(const SSL *ssl, struct timeval *out_clock) {
  2245. if (ssl->ctx->current_time_cb != NULL) {
  2246. ssl->ctx->current_time_cb(ssl, out_clock);
  2247. return;
  2248. }
  2249. #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  2250. out_clock->tv_sec = 1234;
  2251. out_clock->tv_usec = 1234;
  2252. #elif defined(OPENSSL_WINDOWS)
  2253. struct _timeb time;
  2254. _ftime(&time);
  2255. out_clock->tv_sec = time.time;
  2256. out_clock->tv_usec = time.millitm * 1000;
  2257. #else
  2258. gettimeofday(out_clock, NULL);
  2259. #endif
  2260. }
  2261. int SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
  2262. return SSL_CTX_set_min_proto_version(ctx, version);
  2263. }
  2264. int SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
  2265. return SSL_CTX_set_max_proto_version(ctx, version);
  2266. }
  2267. int SSL_set_min_version(SSL *ssl, uint16_t version) {
  2268. return SSL_set_min_proto_version(ssl, version);
  2269. }
  2270. int SSL_set_max_version(SSL *ssl, uint16_t version) {
  2271. return SSL_set_max_proto_version(ssl, version);
  2272. }