選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ssl_lib.c 76 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726
  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 <stdio.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/obj.h>
  151. #include <openssl/rand.h>
  152. #include <openssl/x509v3.h>
  153. #include "internal.h"
  154. #include "../crypto/internal.h"
  155. /* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
  156. * to avoid downstream churn. */
  157. OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
  158. /* Some error codes are special. Ensure the make_errors.go script never
  159. * regresses this. */
  160. OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
  161. SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
  162. ssl_alert_reason_code_mismatch);
  163. /* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
  164. static const size_t kMaxHandshakeSize = (1u << 24) - 1;
  165. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
  166. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  167. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
  168. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  169. int SSL_library_init(void) {
  170. CRYPTO_library_init();
  171. return 1;
  172. }
  173. static uint32_t ssl_session_hash(const SSL_SESSION *a) {
  174. uint32_t hash =
  175. ((uint32_t)a->session_id[0]) |
  176. ((uint32_t)a->session_id[1] << 8) |
  177. ((uint32_t)a->session_id[2] << 16) |
  178. ((uint32_t)a->session_id[3] << 24);
  179. return hash;
  180. }
  181. /* NB: If this function (or indeed the hash function which uses a sort of
  182. * coarser function than this one) is changed, ensure
  183. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  184. * able to construct an SSL_SESSION that will collide with any existing session
  185. * with a matching session ID. */
  186. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  187. if (a->ssl_version != b->ssl_version) {
  188. return 1;
  189. }
  190. if (a->session_id_length != b->session_id_length) {
  191. return 1;
  192. }
  193. return memcmp(a->session_id, b->session_id, a->session_id_length);
  194. }
  195. SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
  196. SSL_CTX *ret = NULL;
  197. if (method == NULL) {
  198. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  199. return NULL;
  200. }
  201. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  202. OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  203. goto err;
  204. }
  205. ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
  206. if (ret == NULL) {
  207. goto err;
  208. }
  209. memset(ret, 0, sizeof(SSL_CTX));
  210. ret->method = method->method;
  211. CRYPTO_MUTEX_init(&ret->lock);
  212. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  213. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  214. /* We take the system default */
  215. ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  216. ret->references = 1;
  217. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  218. ret->verify_mode = SSL_VERIFY_NONE;
  219. ret->cert = ssl_cert_new();
  220. if (ret->cert == NULL) {
  221. goto err;
  222. }
  223. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  224. if (ret->sessions == NULL) {
  225. goto err;
  226. }
  227. ret->cert_store = X509_STORE_new();
  228. if (ret->cert_store == NULL) {
  229. goto err;
  230. }
  231. ssl_create_cipher_list(ret->method, &ret->cipher_list,
  232. &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
  233. if (ret->cipher_list == NULL ||
  234. sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
  235. OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  236. goto err2;
  237. }
  238. ret->param = X509_VERIFY_PARAM_new();
  239. if (!ret->param) {
  240. goto err;
  241. }
  242. ret->client_CA = sk_X509_NAME_new_null();
  243. if (ret->client_CA == NULL) {
  244. goto err;
  245. }
  246. CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
  247. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  248. /* Setup RFC4507 ticket keys */
  249. if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
  250. !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
  251. !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
  252. ret->options |= SSL_OP_NO_TICKET;
  253. }
  254. /* Default is to connect to non-RI servers. When RI is more widely deployed
  255. * might change this. */
  256. ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
  257. /* Lock the SSL_CTX to the specified version, for compatibility with legacy
  258. * uses of SSL_METHOD. */
  259. if (method->version != 0) {
  260. SSL_CTX_set_max_version(ret, method->version);
  261. SSL_CTX_set_min_version(ret, method->version);
  262. }
  263. return ret;
  264. err:
  265. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  266. err2:
  267. SSL_CTX_free(ret);
  268. return NULL;
  269. }
  270. void SSL_CTX_free(SSL_CTX *ctx) {
  271. if (ctx == NULL ||
  272. !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
  273. return;
  274. }
  275. X509_VERIFY_PARAM_free(ctx->param);
  276. /* Free internal session cache. However: the remove_cb() may reference the
  277. * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
  278. * sessions were flushed. As the ex_data handling routines might also touch
  279. * the session cache, the most secure solution seems to be: empty (flush) the
  280. * cache, then free ex_data, then finally free the cache. (See ticket
  281. * [openssl.org #212].) */
  282. SSL_CTX_flush_sessions(ctx, 0);
  283. CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
  284. CRYPTO_MUTEX_cleanup(&ctx->lock);
  285. lh_SSL_SESSION_free(ctx->sessions);
  286. X509_STORE_free(ctx->cert_store);
  287. ssl_cipher_preference_list_free(ctx->cipher_list);
  288. sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
  289. ssl_cipher_preference_list_free(ctx->cipher_list_tls10);
  290. ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
  291. ssl_cert_free(ctx->cert);
  292. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
  293. SSL_CUSTOM_EXTENSION_free);
  294. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
  295. SSL_CUSTOM_EXTENSION_free);
  296. sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
  297. sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
  298. OPENSSL_free(ctx->psk_identity_hint);
  299. OPENSSL_free(ctx->tlsext_ellipticcurvelist);
  300. OPENSSL_free(ctx->alpn_client_proto_list);
  301. OPENSSL_free(ctx->ocsp_response);
  302. OPENSSL_free(ctx->signed_cert_timestamp_list);
  303. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  304. OPENSSL_free(ctx);
  305. }
  306. SSL *SSL_new(SSL_CTX *ctx) {
  307. SSL *s;
  308. if (ctx == NULL) {
  309. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
  310. return NULL;
  311. }
  312. if (ctx->method == NULL) {
  313. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  314. return NULL;
  315. }
  316. s = (SSL *)OPENSSL_malloc(sizeof(SSL));
  317. if (s == NULL) {
  318. goto err;
  319. }
  320. memset(s, 0, sizeof(SSL));
  321. s->min_version = ctx->min_version;
  322. s->max_version = ctx->max_version;
  323. s->options = ctx->options;
  324. s->mode = ctx->mode;
  325. s->max_cert_list = ctx->max_cert_list;
  326. s->cert = ssl_cert_dup(ctx->cert);
  327. if (s->cert == NULL) {
  328. goto err;
  329. }
  330. s->msg_callback = ctx->msg_callback;
  331. s->msg_callback_arg = ctx->msg_callback_arg;
  332. s->verify_mode = ctx->verify_mode;
  333. s->sid_ctx_length = ctx->sid_ctx_length;
  334. assert(s->sid_ctx_length <= sizeof s->sid_ctx);
  335. memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
  336. s->verify_callback = ctx->default_verify_callback;
  337. s->param = X509_VERIFY_PARAM_new();
  338. if (!s->param) {
  339. goto err;
  340. }
  341. X509_VERIFY_PARAM_inherit(s->param, ctx->param);
  342. s->quiet_shutdown = ctx->quiet_shutdown;
  343. s->max_send_fragment = ctx->max_send_fragment;
  344. CRYPTO_refcount_inc(&ctx->references);
  345. s->ctx = ctx;
  346. CRYPTO_refcount_inc(&ctx->references);
  347. s->initial_ctx = ctx;
  348. if (ctx->tlsext_ellipticcurvelist) {
  349. s->tlsext_ellipticcurvelist =
  350. BUF_memdup(ctx->tlsext_ellipticcurvelist,
  351. ctx->tlsext_ellipticcurvelist_length * 2);
  352. if (!s->tlsext_ellipticcurvelist) {
  353. goto err;
  354. }
  355. s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
  356. }
  357. if (s->ctx->alpn_client_proto_list) {
  358. s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
  359. s->ctx->alpn_client_proto_list_len);
  360. if (s->alpn_client_proto_list == NULL) {
  361. goto err;
  362. }
  363. s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
  364. }
  365. s->verify_result = X509_V_OK;
  366. s->method = ctx->method;
  367. if (!s->method->ssl_new(s)) {
  368. goto err;
  369. }
  370. s->enc_method = ssl3_get_enc_method(s->version);
  371. assert(s->enc_method != NULL);
  372. s->rwstate = SSL_NOTHING;
  373. CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
  374. s->psk_identity_hint = NULL;
  375. if (ctx->psk_identity_hint) {
  376. s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
  377. if (s->psk_identity_hint == NULL) {
  378. goto err;
  379. }
  380. }
  381. s->psk_client_callback = ctx->psk_client_callback;
  382. s->psk_server_callback = ctx->psk_server_callback;
  383. s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
  384. if (ctx->tlsext_channel_id_private) {
  385. s->tlsext_channel_id_private =
  386. EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
  387. }
  388. s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
  389. s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
  390. return s;
  391. err:
  392. SSL_free(s);
  393. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  394. return NULL;
  395. }
  396. void SSL_free(SSL *ssl) {
  397. if (ssl == NULL) {
  398. return;
  399. }
  400. X509_VERIFY_PARAM_free(ssl->param);
  401. CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
  402. if (ssl->bbio != NULL) {
  403. /* If the buffering BIO is in place, pop it off */
  404. if (ssl->bbio == ssl->wbio) {
  405. ssl->wbio = BIO_pop(ssl->wbio);
  406. }
  407. BIO_free(ssl->bbio);
  408. ssl->bbio = NULL;
  409. }
  410. int free_wbio = ssl->wbio != ssl->rbio;
  411. BIO_free_all(ssl->rbio);
  412. if (free_wbio) {
  413. BIO_free_all(ssl->wbio);
  414. }
  415. BUF_MEM_free(ssl->init_buf);
  416. /* add extra stuff */
  417. ssl_cipher_preference_list_free(ssl->cipher_list);
  418. sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
  419. ssl_clear_bad_session(ssl);
  420. SSL_SESSION_free(ssl->session);
  421. ssl_clear_cipher_ctx(ssl);
  422. ssl_cert_free(ssl->cert);
  423. OPENSSL_free(ssl->tlsext_hostname);
  424. SSL_CTX_free(ssl->initial_ctx);
  425. OPENSSL_free(ssl->tlsext_ellipticcurvelist);
  426. OPENSSL_free(ssl->alpn_client_proto_list);
  427. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  428. OPENSSL_free(ssl->psk_identity_hint);
  429. sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
  430. OPENSSL_free(ssl->next_proto_negotiated);
  431. sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
  432. if (ssl->method != NULL) {
  433. ssl->method->ssl_free(ssl);
  434. }
  435. SSL_CTX_free(ssl->ctx);
  436. OPENSSL_free(ssl);
  437. }
  438. void SSL_set_connect_state(SSL *ssl) {
  439. ssl->server = 0;
  440. ssl->shutdown = 0;
  441. ssl->state = SSL_ST_CONNECT;
  442. ssl->handshake_func = ssl->method->ssl_connect;
  443. /* clear the current cipher */
  444. ssl_clear_cipher_ctx(ssl);
  445. }
  446. void SSL_set_accept_state(SSL *ssl) {
  447. ssl->server = 1;
  448. ssl->shutdown = 0;
  449. ssl->state = SSL_ST_ACCEPT;
  450. ssl->handshake_func = ssl->method->ssl_accept;
  451. /* clear the current cipher */
  452. ssl_clear_cipher_ctx(ssl);
  453. }
  454. void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
  455. /* If the output buffering BIO is still in place, remove it. */
  456. if (ssl->bbio != NULL) {
  457. if (ssl->wbio == ssl->bbio) {
  458. ssl->wbio = ssl->wbio->next_bio;
  459. ssl->bbio->next_bio = NULL;
  460. }
  461. }
  462. if (ssl->rbio != rbio) {
  463. BIO_free_all(ssl->rbio);
  464. }
  465. if (ssl->wbio != wbio && ssl->rbio != ssl->wbio) {
  466. BIO_free_all(ssl->wbio);
  467. }
  468. ssl->rbio = rbio;
  469. ssl->wbio = wbio;
  470. }
  471. BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
  472. BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
  473. int SSL_do_handshake(SSL *ssl) {
  474. if (ssl->handshake_func == NULL) {
  475. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  476. return -1;
  477. }
  478. if (!SSL_in_init(ssl)) {
  479. return 1;
  480. }
  481. return ssl->handshake_func(ssl);
  482. }
  483. int SSL_connect(SSL *ssl) {
  484. if (ssl->handshake_func == 0) {
  485. /* Not properly initialized yet */
  486. SSL_set_connect_state(ssl);
  487. }
  488. if (ssl->handshake_func != ssl->method->ssl_connect) {
  489. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  490. return -1;
  491. }
  492. return ssl->handshake_func(ssl);
  493. }
  494. int SSL_accept(SSL *ssl) {
  495. if (ssl->handshake_func == 0) {
  496. /* Not properly initialized yet */
  497. SSL_set_accept_state(ssl);
  498. }
  499. if (ssl->handshake_func != ssl->method->ssl_accept) {
  500. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  501. return -1;
  502. }
  503. return ssl->handshake_func(ssl);
  504. }
  505. int SSL_read(SSL *ssl, void *buf, int num) {
  506. if (ssl->handshake_func == 0) {
  507. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  508. return -1;
  509. }
  510. if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
  511. ssl->rwstate = SSL_NOTHING;
  512. return 0;
  513. }
  514. ERR_clear_system_error();
  515. return ssl->method->ssl_read_app_data(ssl, buf, num, 0);
  516. }
  517. int SSL_peek(SSL *ssl, void *buf, int num) {
  518. if (ssl->handshake_func == 0) {
  519. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  520. return -1;
  521. }
  522. if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
  523. return 0;
  524. }
  525. ERR_clear_system_error();
  526. return ssl->method->ssl_read_app_data(ssl, buf, num, 1);
  527. }
  528. int SSL_write(SSL *ssl, const void *buf, int num) {
  529. if (ssl->handshake_func == 0) {
  530. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  531. return -1;
  532. }
  533. if (ssl->shutdown & SSL_SENT_SHUTDOWN) {
  534. ssl->rwstate = SSL_NOTHING;
  535. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  536. return -1;
  537. }
  538. ERR_clear_system_error();
  539. return ssl->method->ssl_write_app_data(ssl, buf, num);
  540. }
  541. int SSL_shutdown(SSL *ssl) {
  542. /* Note that this function behaves differently from what one might expect.
  543. * Return values are 0 for no success (yet), 1 for success; but calling it
  544. * once is usually not enough, even if blocking I/O is used (see
  545. * ssl3_shutdown). */
  546. if (ssl->handshake_func == 0) {
  547. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  548. return -1;
  549. }
  550. if (SSL_in_init(ssl)) {
  551. return 1;
  552. }
  553. /* Do nothing if configured not to send a close_notify. */
  554. if (ssl->quiet_shutdown) {
  555. ssl->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
  556. return 1;
  557. }
  558. if (!(ssl->shutdown & SSL_SENT_SHUTDOWN)) {
  559. ssl->shutdown |= SSL_SENT_SHUTDOWN;
  560. ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
  561. /* our shutdown alert has been sent now, and if it still needs to be
  562. * written, ssl->s3->alert_dispatch will be true */
  563. if (ssl->s3->alert_dispatch) {
  564. return -1; /* return WANT_WRITE */
  565. }
  566. } else if (ssl->s3->alert_dispatch) {
  567. /* resend it if not sent */
  568. int ret = ssl->method->ssl_dispatch_alert(ssl);
  569. if (ret == -1) {
  570. /* we only get to return -1 here the 2nd/Nth invocation, we must have
  571. * already signalled return 0 upon a previous invoation, return
  572. * WANT_WRITE */
  573. return ret;
  574. }
  575. } else if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
  576. /* If we are waiting for a close from our peer, we are closed */
  577. ssl->method->ssl_read_close_notify(ssl);
  578. if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
  579. return -1; /* return WANT_READ */
  580. }
  581. }
  582. if (ssl->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
  583. !ssl->s3->alert_dispatch) {
  584. return 1;
  585. } else {
  586. return 0;
  587. }
  588. }
  589. int SSL_get_error(const SSL *ssl, int ret_code) {
  590. int reason;
  591. uint32_t err;
  592. BIO *bio;
  593. if (ret_code > 0) {
  594. return SSL_ERROR_NONE;
  595. }
  596. /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  597. * where we do encode the error */
  598. err = ERR_peek_error();
  599. if (err != 0) {
  600. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  601. return SSL_ERROR_SYSCALL;
  602. }
  603. return SSL_ERROR_SSL;
  604. }
  605. if (ret_code == 0) {
  606. if ((ssl->shutdown & SSL_RECEIVED_SHUTDOWN) &&
  607. (ssl->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
  608. /* The socket was cleanly shut down with a close_notify. */
  609. return SSL_ERROR_ZERO_RETURN;
  610. }
  611. /* An EOF was observed which violates the protocol, and the underlying
  612. * transport does not participate in the error queue. Bubble up to the
  613. * caller. */
  614. return SSL_ERROR_SYSCALL;
  615. }
  616. if (SSL_want_session(ssl)) {
  617. return SSL_ERROR_PENDING_SESSION;
  618. }
  619. if (SSL_want_certificate(ssl)) {
  620. return SSL_ERROR_PENDING_CERTIFICATE;
  621. }
  622. if (SSL_want_read(ssl)) {
  623. bio = SSL_get_rbio(ssl);
  624. if (BIO_should_read(bio)) {
  625. return SSL_ERROR_WANT_READ;
  626. }
  627. if (BIO_should_write(bio)) {
  628. /* This one doesn't make too much sense ... We never try to write to the
  629. * rbio, and an application program where rbio and wbio are separate
  630. * couldn't even know what it should wait for. However if we ever set
  631. * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
  632. * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
  633. * around that bug; so it might be safer to keep it. */
  634. return SSL_ERROR_WANT_WRITE;
  635. }
  636. if (BIO_should_io_special(bio)) {
  637. reason = BIO_get_retry_reason(bio);
  638. if (reason == BIO_RR_CONNECT) {
  639. return SSL_ERROR_WANT_CONNECT;
  640. }
  641. if (reason == BIO_RR_ACCEPT) {
  642. return SSL_ERROR_WANT_ACCEPT;
  643. }
  644. return SSL_ERROR_SYSCALL; /* unknown */
  645. }
  646. }
  647. if (SSL_want_write(ssl)) {
  648. bio = SSL_get_wbio(ssl);
  649. if (BIO_should_write(bio)) {
  650. return SSL_ERROR_WANT_WRITE;
  651. }
  652. if (BIO_should_read(bio)) {
  653. /* See above (SSL_want_read(ssl) with BIO_should_write(bio)) */
  654. return SSL_ERROR_WANT_READ;
  655. }
  656. if (BIO_should_io_special(bio)) {
  657. reason = BIO_get_retry_reason(bio);
  658. if (reason == BIO_RR_CONNECT) {
  659. return SSL_ERROR_WANT_CONNECT;
  660. }
  661. if (reason == BIO_RR_ACCEPT) {
  662. return SSL_ERROR_WANT_ACCEPT;
  663. }
  664. return SSL_ERROR_SYSCALL;
  665. }
  666. }
  667. if (SSL_want_x509_lookup(ssl)) {
  668. return SSL_ERROR_WANT_X509_LOOKUP;
  669. }
  670. if (SSL_want_channel_id_lookup(ssl)) {
  671. return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
  672. }
  673. if (SSL_want_private_key_operation(ssl)) {
  674. return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
  675. }
  676. return SSL_ERROR_SYSCALL;
  677. }
  678. void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
  679. ctx->min_version = version;
  680. }
  681. void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
  682. ctx->max_version = version;
  683. }
  684. void SSL_set_min_version(SSL *ssl, uint16_t version) {
  685. ssl->min_version = version;
  686. }
  687. void SSL_set_max_version(SSL *ssl, uint16_t version) {
  688. ssl->max_version = version;
  689. }
  690. uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
  691. ctx->options |= options;
  692. return ctx->options;
  693. }
  694. uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
  695. ctx->options &= ~options;
  696. return ctx->options;
  697. }
  698. uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
  699. uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
  700. ssl->options |= options;
  701. return ssl->options;
  702. }
  703. uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
  704. ssl->options &= ~options;
  705. return ssl->options;
  706. }
  707. uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
  708. uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
  709. ctx->mode |= mode;
  710. return ctx->mode;
  711. }
  712. uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
  713. ctx->mode &= ~mode;
  714. return ctx->mode;
  715. }
  716. uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
  717. uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
  718. ssl->mode |= mode;
  719. return ssl->mode;
  720. }
  721. uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
  722. ssl->mode &= ~mode;
  723. return ssl->mode;
  724. }
  725. uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
  726. X509 *SSL_get_peer_certificate(const SSL *ssl) {
  727. if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) {
  728. return NULL;
  729. }
  730. return X509_up_ref(ssl->session->peer);
  731. }
  732. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
  733. if (ssl == NULL || ssl->session == NULL) {
  734. return NULL;
  735. }
  736. return ssl->session->cert_chain;
  737. }
  738. int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
  739. size_t max_out) {
  740. /* The tls-unique value is the first Finished message in the handshake, which
  741. * is the client's in a full handshake and the server's for a resumption. See
  742. * https://tools.ietf.org/html/rfc5929#section-3.1. */
  743. const uint8_t *finished = ssl->s3->previous_client_finished;
  744. size_t finished_len = ssl->s3->previous_client_finished_len;
  745. if (ssl->hit) {
  746. /* tls-unique is broken for resumed sessions unless EMS is used. */
  747. if (!ssl->session->extended_master_secret) {
  748. goto err;
  749. }
  750. finished = ssl->s3->previous_server_finished;
  751. finished_len = ssl->s3->previous_server_finished_len;
  752. }
  753. if (!ssl->s3->initial_handshake_complete ||
  754. ssl->version < TLS1_VERSION) {
  755. goto err;
  756. }
  757. *out_len = finished_len;
  758. if (finished_len > max_out) {
  759. *out_len = max_out;
  760. }
  761. memcpy(out, finished, *out_len);
  762. return 1;
  763. err:
  764. *out_len = 0;
  765. memset(out, 0, max_out);
  766. return 0;
  767. }
  768. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
  769. unsigned sid_ctx_len) {
  770. if (sid_ctx_len > sizeof(ctx->sid_ctx)) {
  771. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  772. return 0;
  773. }
  774. ctx->sid_ctx_length = sid_ctx_len;
  775. memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
  776. return 1;
  777. }
  778. int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
  779. unsigned sid_ctx_len) {
  780. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  781. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  782. return 0;
  783. }
  784. ssl->sid_ctx_length = sid_ctx_len;
  785. memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
  786. return 1;
  787. }
  788. int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
  789. return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
  790. }
  791. int SSL_set_purpose(SSL *ssl, int purpose) {
  792. return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose);
  793. }
  794. int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
  795. return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
  796. }
  797. int SSL_set_trust(SSL *ssl, int trust) {
  798. return X509_VERIFY_PARAM_set_trust(ssl->param, trust);
  799. }
  800. int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
  801. return X509_VERIFY_PARAM_set1(ctx->param, param);
  802. }
  803. int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
  804. return X509_VERIFY_PARAM_set1(ssl->param, param);
  805. }
  806. void ssl_cipher_preference_list_free(
  807. struct ssl_cipher_preference_list_st *cipher_list) {
  808. if (cipher_list == NULL) {
  809. return;
  810. }
  811. sk_SSL_CIPHER_free(cipher_list->ciphers);
  812. OPENSSL_free(cipher_list->in_group_flags);
  813. OPENSSL_free(cipher_list);
  814. }
  815. struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
  816. struct ssl_cipher_preference_list_st *cipher_list) {
  817. struct ssl_cipher_preference_list_st *ret = NULL;
  818. size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
  819. ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
  820. if (!ret) {
  821. goto err;
  822. }
  823. ret->ciphers = NULL;
  824. ret->in_group_flags = NULL;
  825. ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
  826. if (!ret->ciphers) {
  827. goto err;
  828. }
  829. ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
  830. if (!ret->in_group_flags) {
  831. goto err;
  832. }
  833. return ret;
  834. err:
  835. ssl_cipher_preference_list_free(ret);
  836. return NULL;
  837. }
  838. struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
  839. STACK_OF(SSL_CIPHER) *ciphers) {
  840. struct ssl_cipher_preference_list_st *ret = NULL;
  841. size_t n = sk_SSL_CIPHER_num(ciphers);
  842. ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
  843. if (!ret) {
  844. goto err;
  845. }
  846. ret->ciphers = NULL;
  847. ret->in_group_flags = NULL;
  848. ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
  849. if (!ret->ciphers) {
  850. goto err;
  851. }
  852. ret->in_group_flags = OPENSSL_malloc(n);
  853. if (!ret->in_group_flags) {
  854. goto err;
  855. }
  856. memset(ret->in_group_flags, 0, n);
  857. return ret;
  858. err:
  859. ssl_cipher_preference_list_free(ret);
  860. return NULL;
  861. }
  862. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
  863. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
  864. void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
  865. int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
  866. int SSL_get_rfd(const SSL *ssl) {
  867. int ret = -1;
  868. BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
  869. if (b != NULL) {
  870. BIO_get_fd(b, &ret);
  871. }
  872. return ret;
  873. }
  874. int SSL_get_wfd(const SSL *ssl) {
  875. int ret = -1;
  876. BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
  877. if (b != NULL) {
  878. BIO_get_fd(b, &ret);
  879. }
  880. return ret;
  881. }
  882. int SSL_set_fd(SSL *ssl, int fd) {
  883. BIO *bio = BIO_new(BIO_s_fd());
  884. if (bio == NULL) {
  885. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  886. return 0;
  887. }
  888. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  889. SSL_set_bio(ssl, bio, bio);
  890. return 1;
  891. }
  892. int SSL_set_wfd(SSL *ssl, int fd) {
  893. if (ssl->rbio == NULL ||
  894. BIO_method_type(ssl->rbio) != BIO_TYPE_FD ||
  895. BIO_get_fd(ssl->rbio, NULL) != fd) {
  896. BIO *bio = BIO_new(BIO_s_fd());
  897. if (bio == NULL) {
  898. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  899. return 0;
  900. }
  901. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  902. SSL_set_bio(ssl, SSL_get_rbio(ssl), bio);
  903. } else {
  904. SSL_set_bio(ssl, SSL_get_rbio(ssl), SSL_get_rbio(ssl));
  905. }
  906. return 1;
  907. }
  908. int SSL_set_rfd(SSL *ssl, int fd) {
  909. if (ssl->wbio == NULL || BIO_method_type(ssl->wbio) != BIO_TYPE_FD ||
  910. BIO_get_fd(ssl->wbio, NULL) != fd) {
  911. BIO *bio = BIO_new(BIO_s_fd());
  912. if (bio == NULL) {
  913. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  914. return 0;
  915. }
  916. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  917. SSL_set_bio(ssl, bio, SSL_get_wbio(ssl));
  918. } else {
  919. SSL_set_bio(ssl, SSL_get_wbio(ssl), SSL_get_wbio(ssl));
  920. }
  921. return 1;
  922. }
  923. size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
  924. size_t ret = 0;
  925. if (ssl->s3 != NULL) {
  926. ret = ssl->s3->tmp.finish_md_len;
  927. if (count > ret) {
  928. count = ret;
  929. }
  930. memcpy(buf, ssl->s3->tmp.finish_md, count);
  931. }
  932. return ret;
  933. }
  934. size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
  935. size_t ret = 0;
  936. if (ssl->s3 != NULL) {
  937. ret = ssl->s3->tmp.peer_finish_md_len;
  938. if (count > ret) {
  939. count = ret;
  940. }
  941. memcpy(buf, ssl->s3->tmp.peer_finish_md, count);
  942. }
  943. return ret;
  944. }
  945. int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
  946. int SSL_get_verify_depth(const SSL *ssl) {
  947. return X509_VERIFY_PARAM_get_depth(ssl->param);
  948. }
  949. int SSL_get_extms_support(const SSL *ssl) {
  950. return ssl->s3->tmp.extended_master_secret == 1;
  951. }
  952. int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
  953. return ssl->verify_callback;
  954. }
  955. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
  956. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
  957. return X509_VERIFY_PARAM_get_depth(ctx->param);
  958. }
  959. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
  960. int ok, X509_STORE_CTX *store_ctx) {
  961. return ctx->default_verify_callback;
  962. }
  963. void SSL_set_verify(SSL *ssl, int mode,
  964. int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
  965. ssl->verify_mode = mode;
  966. if (callback != NULL) {
  967. ssl->verify_callback = callback;
  968. }
  969. }
  970. void SSL_set_verify_depth(SSL *ssl, int depth) {
  971. X509_VERIFY_PARAM_set_depth(ssl->param, depth);
  972. }
  973. int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
  974. int SSL_get_read_ahead(const SSL *s) { return 0; }
  975. void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
  976. void SSL_set_read_ahead(SSL *s, int yes) { }
  977. int SSL_pending(const SSL *ssl) {
  978. if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
  979. return 0;
  980. }
  981. return ssl->s3->rrec.length;
  982. }
  983. /* Fix this so it checks all the valid key/cert options */
  984. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  985. if (ctx->cert->x509 == NULL) {
  986. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  987. return 0;
  988. }
  989. if (ctx->cert->privatekey == NULL) {
  990. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  991. return 0;
  992. }
  993. return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
  994. }
  995. /* Fix this function so that it takes an optional type parameter */
  996. int SSL_check_private_key(const SSL *ssl) {
  997. if (ssl->cert->x509 == NULL) {
  998. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  999. return 0;
  1000. }
  1001. if (ssl->cert->privatekey == NULL) {
  1002. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1003. return 0;
  1004. }
  1005. return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
  1006. }
  1007. long SSL_get_default_timeout(const SSL *ssl) {
  1008. return SSL_DEFAULT_SESSION_TIMEOUT;
  1009. }
  1010. int SSL_renegotiate(SSL *ssl) {
  1011. /* Caller-initiated renegotiation is not supported. */
  1012. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1013. return 0;
  1014. }
  1015. int SSL_renegotiate_pending(SSL *ssl) {
  1016. return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
  1017. }
  1018. size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
  1019. return ctx->max_cert_list;
  1020. }
  1021. void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
  1022. if (max_cert_list > kMaxHandshakeSize) {
  1023. max_cert_list = kMaxHandshakeSize;
  1024. }
  1025. ctx->max_cert_list = (uint32_t)max_cert_list;
  1026. }
  1027. size_t SSL_get_max_cert_list(const SSL *ssl) {
  1028. return ssl->max_cert_list;
  1029. }
  1030. void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
  1031. if (max_cert_list > kMaxHandshakeSize) {
  1032. max_cert_list = kMaxHandshakeSize;
  1033. }
  1034. ssl->max_cert_list = (uint32_t)max_cert_list;
  1035. }
  1036. void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
  1037. if (max_send_fragment < 512) {
  1038. max_send_fragment = 512;
  1039. }
  1040. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1041. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1042. }
  1043. ctx->max_send_fragment = (uint16_t)max_send_fragment;
  1044. }
  1045. void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
  1046. if (max_send_fragment < 512) {
  1047. max_send_fragment = 512;
  1048. }
  1049. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1050. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1051. }
  1052. ssl->max_send_fragment = (uint16_t)max_send_fragment;
  1053. }
  1054. int SSL_set_mtu(SSL *ssl, unsigned mtu) {
  1055. if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
  1056. return 0;
  1057. }
  1058. ssl->d1->mtu = mtu;
  1059. return 1;
  1060. }
  1061. int SSL_get_secure_renegotiation_support(const SSL *ssl) {
  1062. return ssl->s3->send_connection_binding;
  1063. }
  1064. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
  1065. size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
  1066. return lh_SSL_SESSION_num_items(ctx->sessions);
  1067. }
  1068. unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
  1069. unsigned long ret = ctx->session_cache_size;
  1070. ctx->session_cache_size = size;
  1071. return ret;
  1072. }
  1073. unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
  1074. return ctx->session_cache_size;
  1075. }
  1076. int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
  1077. int ret = ctx->session_cache_mode;
  1078. ctx->session_cache_mode = mode;
  1079. return ret;
  1080. }
  1081. int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
  1082. return ctx->session_cache_mode;
  1083. }
  1084. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
  1085. if (ssl == NULL) {
  1086. return NULL;
  1087. }
  1088. if (ssl->cipher_list != NULL) {
  1089. return ssl->cipher_list->ciphers;
  1090. }
  1091. if (ssl->version >= TLS1_1_VERSION && ssl->ctx != NULL &&
  1092. ssl->ctx->cipher_list_tls11 != NULL) {
  1093. return ssl->ctx->cipher_list_tls11->ciphers;
  1094. }
  1095. if (ssl->version >= TLS1_VERSION && ssl->ctx != NULL &&
  1096. ssl->ctx->cipher_list_tls10 != NULL) {
  1097. return ssl->ctx->cipher_list_tls10->ciphers;
  1098. }
  1099. if (ssl->ctx != NULL && ssl->ctx->cipher_list != NULL) {
  1100. return ssl->ctx->cipher_list->ciphers;
  1101. }
  1102. return NULL;
  1103. }
  1104. /* return a STACK of the ciphers available for the SSL and in order of
  1105. * algorithm id */
  1106. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) {
  1107. if (s == NULL) {
  1108. return NULL;
  1109. }
  1110. if (s->cipher_list_by_id != NULL) {
  1111. return s->cipher_list_by_id;
  1112. }
  1113. if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
  1114. return s->ctx->cipher_list_by_id;
  1115. }
  1116. return NULL;
  1117. }
  1118. const char *SSL_get_cipher_list(const SSL *ssl, int n) {
  1119. const SSL_CIPHER *c;
  1120. STACK_OF(SSL_CIPHER) *sk;
  1121. if (ssl == NULL) {
  1122. return NULL;
  1123. }
  1124. sk = SSL_get_ciphers(ssl);
  1125. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  1126. return NULL;
  1127. }
  1128. c = sk_SSL_CIPHER_value(sk, n);
  1129. if (c == NULL) {
  1130. return NULL;
  1131. }
  1132. return c->name;
  1133. }
  1134. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  1135. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1136. ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id, str);
  1137. if (cipher_list == NULL) {
  1138. return 0;
  1139. }
  1140. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1141. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1142. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1143. return 0;
  1144. }
  1145. return 1;
  1146. }
  1147. int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) {
  1148. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1149. ctx->method, &ctx->cipher_list_tls10, NULL, str);
  1150. if (cipher_list == NULL) {
  1151. return 0;
  1152. }
  1153. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1154. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1155. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1156. return 0;
  1157. }
  1158. return 1;
  1159. }
  1160. int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
  1161. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1162. ctx->method, &ctx->cipher_list_tls11, NULL, str);
  1163. if (cipher_list == NULL) {
  1164. return 0;
  1165. }
  1166. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1167. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1168. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1169. return 0;
  1170. }
  1171. return 1;
  1172. }
  1173. int SSL_set_cipher_list(SSL *ssl, const char *str) {
  1174. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1175. ssl->ctx->method, &ssl->cipher_list, &ssl->cipher_list_by_id, str);
  1176. if (cipher_list == NULL) {
  1177. return 0;
  1178. }
  1179. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1180. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1181. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1182. return 0;
  1183. }
  1184. return 1;
  1185. }
  1186. STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
  1187. CBS cipher_suites = *cbs;
  1188. const SSL_CIPHER *c;
  1189. STACK_OF(SSL_CIPHER) *sk;
  1190. if (s->s3) {
  1191. s->s3->send_connection_binding = 0;
  1192. }
  1193. if (CBS_len(&cipher_suites) % 2 != 0) {
  1194. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  1195. return NULL;
  1196. }
  1197. sk = sk_SSL_CIPHER_new_null();
  1198. if (sk == NULL) {
  1199. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1200. goto err;
  1201. }
  1202. while (CBS_len(&cipher_suites) > 0) {
  1203. uint16_t cipher_suite;
  1204. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  1205. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1206. goto err;
  1207. }
  1208. /* Check for SCSV. */
  1209. if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
  1210. /* SCSV is fatal if renegotiating. */
  1211. if (s->s3->initial_handshake_complete) {
  1212. OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
  1213. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1214. goto err;
  1215. }
  1216. s->s3->send_connection_binding = 1;
  1217. continue;
  1218. }
  1219. /* Check for FALLBACK_SCSV. */
  1220. if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
  1221. uint16_t max_version = ssl3_get_max_server_version(s);
  1222. if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
  1223. : (uint16_t)s->version < max_version) {
  1224. OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
  1225. ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
  1226. goto err;
  1227. }
  1228. continue;
  1229. }
  1230. c = SSL_get_cipher_by_value(cipher_suite);
  1231. if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
  1232. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1233. goto err;
  1234. }
  1235. }
  1236. return sk;
  1237. err:
  1238. sk_SSL_CIPHER_free(sk);
  1239. return NULL;
  1240. }
  1241. const char *SSL_get_servername(const SSL *ssl, const int type) {
  1242. if (type != TLSEXT_NAMETYPE_host_name) {
  1243. return NULL;
  1244. }
  1245. /* Historically, |SSL_get_servername| was also the configuration getter
  1246. * corresponding to |SSL_set_tlsext_host_name|. */
  1247. if (ssl->tlsext_hostname != NULL) {
  1248. return ssl->tlsext_hostname;
  1249. }
  1250. if (ssl->session == NULL) {
  1251. return NULL;
  1252. }
  1253. return ssl->session->tlsext_hostname;
  1254. }
  1255. int SSL_get_servername_type(const SSL *ssl) {
  1256. if (ssl->session != NULL && ssl->session->tlsext_hostname != NULL) {
  1257. return TLSEXT_NAMETYPE_host_name;
  1258. }
  1259. return -1;
  1260. }
  1261. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1262. ctx->signed_cert_timestamps_enabled = 1;
  1263. }
  1264. int SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1265. ssl->signed_cert_timestamps_enabled = 1;
  1266. return 1;
  1267. }
  1268. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1269. ctx->ocsp_stapling_enabled = 1;
  1270. }
  1271. int SSL_enable_ocsp_stapling(SSL *ssl) {
  1272. ssl->ocsp_stapling_enabled = 1;
  1273. return 1;
  1274. }
  1275. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1276. size_t *out_len) {
  1277. SSL_SESSION *session = ssl->session;
  1278. *out_len = 0;
  1279. *out = NULL;
  1280. if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
  1281. return;
  1282. }
  1283. *out = session->tlsext_signed_cert_timestamp_list;
  1284. *out_len = session->tlsext_signed_cert_timestamp_list_length;
  1285. }
  1286. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1287. size_t *out_len) {
  1288. SSL_SESSION *session = ssl->session;
  1289. *out_len = 0;
  1290. *out = NULL;
  1291. if (ssl->server || !session || !session->ocsp_response) {
  1292. return;
  1293. }
  1294. *out = session->ocsp_response;
  1295. *out_len = session->ocsp_response_length;
  1296. }
  1297. int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
  1298. size_t list_len) {
  1299. OPENSSL_free(ctx->signed_cert_timestamp_list);
  1300. ctx->signed_cert_timestamp_list_length = 0;
  1301. ctx->signed_cert_timestamp_list = BUF_memdup(list, list_len);
  1302. if (ctx->signed_cert_timestamp_list == NULL) {
  1303. return 0;
  1304. }
  1305. ctx->signed_cert_timestamp_list_length = list_len;
  1306. return 1;
  1307. }
  1308. int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
  1309. size_t response_len) {
  1310. OPENSSL_free(ctx->ocsp_response);
  1311. ctx->ocsp_response_length = 0;
  1312. ctx->ocsp_response = BUF_memdup(response, response_len);
  1313. if (ctx->ocsp_response == NULL) {
  1314. return 0;
  1315. }
  1316. ctx->ocsp_response_length = response_len;
  1317. return 1;
  1318. }
  1319. int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
  1320. const uint8_t *server, unsigned server_len,
  1321. const uint8_t *client, unsigned client_len) {
  1322. unsigned int i, j;
  1323. const uint8_t *result;
  1324. int status = OPENSSL_NPN_UNSUPPORTED;
  1325. /* For each protocol in server preference order, see if we support it. */
  1326. for (i = 0; i < server_len;) {
  1327. for (j = 0; j < client_len;) {
  1328. if (server[i] == client[j] &&
  1329. memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  1330. /* We found a match */
  1331. result = &server[i];
  1332. status = OPENSSL_NPN_NEGOTIATED;
  1333. goto found;
  1334. }
  1335. j += client[j];
  1336. j++;
  1337. }
  1338. i += server[i];
  1339. i++;
  1340. }
  1341. /* There's no overlap between our protocols and the server's list. */
  1342. result = client;
  1343. status = OPENSSL_NPN_NO_OVERLAP;
  1344. found:
  1345. *out = (uint8_t *)result + 1;
  1346. *out_len = result[0];
  1347. return status;
  1348. }
  1349. void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
  1350. unsigned *out_len) {
  1351. *out_data = ssl->next_proto_negotiated;
  1352. if (*out_data == NULL) {
  1353. *out_len = 0;
  1354. } else {
  1355. *out_len = ssl->next_proto_negotiated_len;
  1356. }
  1357. }
  1358. void SSL_CTX_set_next_protos_advertised_cb(
  1359. SSL_CTX *ctx,
  1360. int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
  1361. void *arg) {
  1362. ctx->next_protos_advertised_cb = cb;
  1363. ctx->next_protos_advertised_cb_arg = arg;
  1364. }
  1365. void SSL_CTX_set_next_proto_select_cb(
  1366. SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
  1367. const uint8_t *in, unsigned in_len, void *arg),
  1368. void *arg) {
  1369. ctx->next_proto_select_cb = cb;
  1370. ctx->next_proto_select_cb_arg = arg;
  1371. }
  1372. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1373. unsigned protos_len) {
  1374. OPENSSL_free(ctx->alpn_client_proto_list);
  1375. ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1376. if (!ctx->alpn_client_proto_list) {
  1377. return 1;
  1378. }
  1379. ctx->alpn_client_proto_list_len = protos_len;
  1380. return 0;
  1381. }
  1382. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1383. OPENSSL_free(ssl->alpn_client_proto_list);
  1384. ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1385. if (!ssl->alpn_client_proto_list) {
  1386. return 1;
  1387. }
  1388. ssl->alpn_client_proto_list_len = protos_len;
  1389. return 0;
  1390. }
  1391. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1392. int (*cb)(SSL *ssl, const uint8_t **out,
  1393. uint8_t *out_len, const uint8_t *in,
  1394. unsigned in_len, void *arg),
  1395. void *arg) {
  1396. ctx->alpn_select_cb = cb;
  1397. ctx->alpn_select_cb_arg = arg;
  1398. }
  1399. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
  1400. unsigned *out_len) {
  1401. *out_data = NULL;
  1402. if (ssl->s3) {
  1403. *out_data = ssl->s3->alpn_selected;
  1404. }
  1405. if (*out_data == NULL) {
  1406. *out_len = 0;
  1407. } else {
  1408. *out_len = ssl->s3->alpn_selected_len;
  1409. }
  1410. }
  1411. int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
  1412. const char *label, size_t label_len,
  1413. const uint8_t *context, size_t context_len,
  1414. int use_context) {
  1415. if (ssl->version < TLS1_VERSION) {
  1416. return 0;
  1417. }
  1418. return ssl->enc_method->export_keying_material(
  1419. ssl, out, out_len, label, label_len, context, context_len, use_context);
  1420. }
  1421. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  1422. int (*cb)(X509_STORE_CTX *store_ctx,
  1423. void *arg),
  1424. void *arg) {
  1425. ctx->app_verify_callback = cb;
  1426. ctx->app_verify_arg = arg;
  1427. }
  1428. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  1429. int (*cb)(int, X509_STORE_CTX *)) {
  1430. ctx->verify_mode = mode;
  1431. ctx->default_verify_callback = cb;
  1432. }
  1433. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
  1434. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1435. }
  1436. void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
  1437. void *arg) {
  1438. ssl_cert_set_cert_cb(ctx->cert, cb, arg);
  1439. }
  1440. void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
  1441. ssl_cert_set_cert_cb(ssl->cert, cb, arg);
  1442. }
  1443. void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
  1444. uint32_t *out_mask_a) {
  1445. CERT *c = s->cert;
  1446. int have_rsa_cert = 0, dh_tmp;
  1447. uint32_t mask_k, mask_a;
  1448. int have_ecc_cert = 0, ecdsa_ok;
  1449. X509 *x;
  1450. dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
  1451. if (s->cert->x509 != NULL && ssl_has_private_key(s)) {
  1452. if (ssl_private_key_type(s) == EVP_PKEY_RSA) {
  1453. have_rsa_cert = 1;
  1454. } else if (ssl_private_key_type(s) == EVP_PKEY_EC) {
  1455. have_ecc_cert = 1;
  1456. }
  1457. }
  1458. mask_k = 0;
  1459. mask_a = 0;
  1460. if (dh_tmp) {
  1461. mask_k |= SSL_kDHE;
  1462. }
  1463. if (have_rsa_cert) {
  1464. mask_k |= SSL_kRSA;
  1465. mask_a |= SSL_aRSA;
  1466. }
  1467. /* An ECC certificate may be usable for ECDSA cipher suites depending on the
  1468. * key usage extension and on the client's curve preferences. */
  1469. if (have_ecc_cert) {
  1470. x = c->x509;
  1471. /* This call populates extension flags (ex_flags). */
  1472. X509_check_purpose(x, -1, 0);
  1473. ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
  1474. ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
  1475. : 1;
  1476. if (!tls1_check_ec_cert(s, x)) {
  1477. ecdsa_ok = 0;
  1478. }
  1479. if (ecdsa_ok) {
  1480. mask_a |= SSL_aECDSA;
  1481. }
  1482. }
  1483. /* If we are considering an ECC cipher suite that uses an ephemeral EC
  1484. * key, check it. */
  1485. if (tls1_check_ec_tmp_key(s)) {
  1486. mask_k |= SSL_kECDHE;
  1487. }
  1488. /* PSK requires a server callback. */
  1489. if (s->psk_server_callback != NULL) {
  1490. mask_k |= SSL_kPSK;
  1491. mask_a |= SSL_aPSK;
  1492. }
  1493. *out_mask_k = mask_k;
  1494. *out_mask_a = mask_a;
  1495. }
  1496. void ssl_update_cache(SSL *ssl, int mode) {
  1497. SSL_CTX *ctx = ssl->initial_ctx;
  1498. /* Never cache sessions with empty session IDs. */
  1499. if (ssl->session->session_id_length == 0 ||
  1500. (ctx->session_cache_mode & mode) != mode) {
  1501. return;
  1502. }
  1503. /* Clients never use the internal session cache. */
  1504. int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
  1505. SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1506. /* A client may see new sessions on abbreviated handshakes if the server
  1507. * decides to renew the ticket. Once the handshake is completed, it should be
  1508. * inserted into the cache. */
  1509. if (!ssl->hit || (!ssl->server && ssl->tlsext_ticket_expected)) {
  1510. if (use_internal_cache) {
  1511. SSL_CTX_add_session(ctx, ssl->session);
  1512. }
  1513. if (ctx->new_session_cb != NULL &&
  1514. !ctx->new_session_cb(ssl, SSL_SESSION_up_ref(ssl->session))) {
  1515. /* |new_session_cb|'s return value signals whether it took ownership. */
  1516. SSL_SESSION_free(ssl->session);
  1517. }
  1518. }
  1519. if (use_internal_cache &&
  1520. !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
  1521. /* Automatically flush the internal session cache every 255 connections. */
  1522. int flush_cache = 0;
  1523. CRYPTO_MUTEX_lock_write(&ctx->lock);
  1524. ctx->handshakes_since_cache_flush++;
  1525. if (ctx->handshakes_since_cache_flush >= 255) {
  1526. flush_cache = 1;
  1527. ctx->handshakes_since_cache_flush = 0;
  1528. }
  1529. CRYPTO_MUTEX_unlock(&ctx->lock);
  1530. if (flush_cache) {
  1531. SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
  1532. }
  1533. }
  1534. }
  1535. static const char *ssl_get_version(int version) {
  1536. switch (version) {
  1537. case TLS1_2_VERSION:
  1538. return "TLSv1.2";
  1539. case TLS1_1_VERSION:
  1540. return "TLSv1.1";
  1541. case TLS1_VERSION:
  1542. return "TLSv1";
  1543. case SSL3_VERSION:
  1544. return "SSLv3";
  1545. case DTLS1_VERSION:
  1546. return "DTLSv1";
  1547. case DTLS1_2_VERSION:
  1548. return "DTLSv1.2";
  1549. default:
  1550. return "unknown";
  1551. }
  1552. }
  1553. const char *SSL_get_version(const SSL *ssl) {
  1554. return ssl_get_version(ssl->version);
  1555. }
  1556. const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
  1557. return ssl_get_version(session->ssl_version);
  1558. }
  1559. const char* SSL_get_curve_name(uint16_t curve_id) {
  1560. return tls1_ec_curve_id2name(curve_id);
  1561. }
  1562. void ssl_clear_cipher_ctx(SSL *s) {
  1563. SSL_AEAD_CTX_free(s->aead_read_ctx);
  1564. s->aead_read_ctx = NULL;
  1565. SSL_AEAD_CTX_free(s->aead_write_ctx);
  1566. s->aead_write_ctx = NULL;
  1567. }
  1568. X509 *SSL_get_certificate(const SSL *s) {
  1569. if (s->cert != NULL) {
  1570. return s->cert->x509;
  1571. }
  1572. return NULL;
  1573. }
  1574. EVP_PKEY *SSL_get_privatekey(const SSL *s) {
  1575. if (s->cert != NULL) {
  1576. return s->cert->privatekey;
  1577. }
  1578. return NULL;
  1579. }
  1580. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
  1581. if (ctx->cert != NULL) {
  1582. return ctx->cert->x509;
  1583. }
  1584. return NULL;
  1585. }
  1586. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  1587. if (ctx->cert != NULL) {
  1588. return ctx->cert->privatekey;
  1589. }
  1590. return NULL;
  1591. }
  1592. const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
  1593. if (ssl->aead_write_ctx == NULL) {
  1594. return NULL;
  1595. }
  1596. return ssl->aead_write_ctx->cipher;
  1597. }
  1598. const COMP_METHOD *SSL_get_current_compression(SSL *s) { return NULL; }
  1599. const COMP_METHOD *SSL_get_current_expansion(SSL *s) { return NULL; }
  1600. int ssl_init_wbio_buffer(SSL *s, int push) {
  1601. BIO *bbio;
  1602. if (s->bbio == NULL) {
  1603. bbio = BIO_new(BIO_f_buffer());
  1604. if (bbio == NULL) {
  1605. return 0;
  1606. }
  1607. s->bbio = bbio;
  1608. } else {
  1609. bbio = s->bbio;
  1610. if (s->bbio == s->wbio) {
  1611. s->wbio = BIO_pop(s->wbio);
  1612. }
  1613. }
  1614. BIO_reset(bbio);
  1615. if (!BIO_set_read_buffer_size(bbio, 1)) {
  1616. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1617. return 0;
  1618. }
  1619. if (push) {
  1620. if (s->wbio != bbio) {
  1621. s->wbio = BIO_push(bbio, s->wbio);
  1622. }
  1623. } else {
  1624. if (s->wbio == bbio) {
  1625. s->wbio = BIO_pop(bbio);
  1626. }
  1627. }
  1628. return 1;
  1629. }
  1630. void ssl_free_wbio_buffer(SSL *s) {
  1631. if (s->bbio == NULL) {
  1632. return;
  1633. }
  1634. if (s->bbio == s->wbio) {
  1635. /* remove buffering */
  1636. s->wbio = BIO_pop(s->wbio);
  1637. }
  1638. BIO_free(s->bbio);
  1639. s->bbio = NULL;
  1640. }
  1641. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  1642. ctx->quiet_shutdown = (mode != 0);
  1643. }
  1644. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  1645. return ctx->quiet_shutdown;
  1646. }
  1647. void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
  1648. ssl->quiet_shutdown = (mode != 0);
  1649. }
  1650. int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
  1651. void SSL_set_shutdown(SSL *ssl, int mode) {
  1652. /* It is an error to clear any bits that have already been set. (We can't try
  1653. * to get a second close_notify or send two.) */
  1654. assert((ssl->shutdown & mode) == ssl->shutdown);
  1655. ssl->shutdown |= mode;
  1656. }
  1657. int SSL_get_shutdown(const SSL *ssl) { return ssl->shutdown; }
  1658. int SSL_version(const SSL *ssl) { return ssl->version; }
  1659. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
  1660. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  1661. if (ssl->ctx == ctx) {
  1662. return ssl->ctx;
  1663. }
  1664. if (ctx == NULL) {
  1665. ctx = ssl->initial_ctx;
  1666. }
  1667. ssl_cert_free(ssl->cert);
  1668. ssl->cert = ssl_cert_dup(ctx->cert);
  1669. CRYPTO_refcount_inc(&ctx->references);
  1670. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  1671. ssl->ctx = ctx;
  1672. ssl->sid_ctx_length = ctx->sid_ctx_length;
  1673. assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
  1674. memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
  1675. return ssl->ctx;
  1676. }
  1677. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
  1678. return X509_STORE_set_default_paths(ctx->cert_store);
  1679. }
  1680. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
  1681. const char *ca_dir) {
  1682. return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
  1683. }
  1684. void SSL_set_info_callback(SSL *ssl,
  1685. void (*cb)(const SSL *ssl, int type, int value)) {
  1686. ssl->info_callback = cb;
  1687. }
  1688. void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
  1689. int value) {
  1690. return ssl->info_callback;
  1691. }
  1692. int SSL_state(const SSL *ssl) { return ssl->state; }
  1693. void SSL_set_state(SSL *ssl, int state) { }
  1694. void SSL_set_verify_result(SSL *ssl, long result) {
  1695. ssl->verify_result = result;
  1696. }
  1697. long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
  1698. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  1699. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  1700. int index;
  1701. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  1702. new_func, dup_func, free_func)) {
  1703. return -1;
  1704. }
  1705. return index;
  1706. }
  1707. int SSL_set_ex_data(SSL *ssl, int idx, void *arg) {
  1708. return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg);
  1709. }
  1710. void *SSL_get_ex_data(const SSL *ssl, int idx) {
  1711. return CRYPTO_get_ex_data(&ssl->ex_data, idx);
  1712. }
  1713. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  1714. CRYPTO_EX_dup *dup_func,
  1715. CRYPTO_EX_free *free_func) {
  1716. int index;
  1717. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  1718. new_func, dup_func, free_func)) {
  1719. return -1;
  1720. }
  1721. return index;
  1722. }
  1723. int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) {
  1724. return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg);
  1725. }
  1726. void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
  1727. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1728. }
  1729. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
  1730. return ctx->cert_store;
  1731. }
  1732. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  1733. X509_STORE_free(ctx->cert_store);
  1734. ctx->cert_store = store;
  1735. }
  1736. int SSL_want(const SSL *ssl) { return ssl->rwstate; }
  1737. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  1738. RSA *(*cb)(SSL *ssl, int is_export,
  1739. int keylength)) {
  1740. }
  1741. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  1742. int keylength)) {
  1743. }
  1744. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  1745. DH *(*callback)(SSL *ssl, int is_export,
  1746. int keylength)) {
  1747. ctx->cert->dh_tmp_cb = callback;
  1748. }
  1749. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
  1750. int keylength)) {
  1751. ssl->cert->dh_tmp_cb = callback;
  1752. }
  1753. void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
  1754. EC_KEY *(*callback)(SSL *ssl, int is_export,
  1755. int keylength)) {
  1756. ctx->cert->ecdh_tmp_cb = callback;
  1757. }
  1758. void SSL_set_tmp_ecdh_callback(SSL *ssl,
  1759. EC_KEY *(*callback)(SSL *ssl, int is_export,
  1760. int keylength)) {
  1761. ssl->cert->ecdh_tmp_cb = callback;
  1762. }
  1763. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  1764. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1765. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1766. return 0;
  1767. }
  1768. OPENSSL_free(ctx->psk_identity_hint);
  1769. if (identity_hint != NULL) {
  1770. ctx->psk_identity_hint = BUF_strdup(identity_hint);
  1771. if (ctx->psk_identity_hint == NULL) {
  1772. return 0;
  1773. }
  1774. } else {
  1775. ctx->psk_identity_hint = NULL;
  1776. }
  1777. return 1;
  1778. }
  1779. int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
  1780. if (ssl == NULL) {
  1781. return 0;
  1782. }
  1783. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1784. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1785. return 0;
  1786. }
  1787. /* Clear currently configured hint, if any. */
  1788. OPENSSL_free(ssl->psk_identity_hint);
  1789. ssl->psk_identity_hint = NULL;
  1790. if (identity_hint != NULL) {
  1791. ssl->psk_identity_hint = BUF_strdup(identity_hint);
  1792. if (ssl->psk_identity_hint == NULL) {
  1793. return 0;
  1794. }
  1795. }
  1796. return 1;
  1797. }
  1798. const char *SSL_get_psk_identity_hint(const SSL *ssl) {
  1799. if (ssl == NULL) {
  1800. return NULL;
  1801. }
  1802. return ssl->psk_identity_hint;
  1803. }
  1804. const char *SSL_get_psk_identity(const SSL *ssl) {
  1805. if (ssl == NULL || ssl->session == NULL) {
  1806. return NULL;
  1807. }
  1808. return ssl->session->psk_identity;
  1809. }
  1810. void SSL_set_psk_client_callback(
  1811. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1812. unsigned max_identity_len, uint8_t *psk,
  1813. unsigned max_psk_len)) {
  1814. ssl->psk_client_callback = cb;
  1815. }
  1816. void SSL_CTX_set_psk_client_callback(
  1817. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1818. unsigned max_identity_len, uint8_t *psk,
  1819. unsigned max_psk_len)) {
  1820. ctx->psk_client_callback = cb;
  1821. }
  1822. void SSL_set_psk_server_callback(
  1823. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  1824. unsigned max_psk_len)) {
  1825. ssl->psk_server_callback = cb;
  1826. }
  1827. void SSL_CTX_set_psk_server_callback(
  1828. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
  1829. uint8_t *psk, unsigned max_psk_len)) {
  1830. ctx->psk_server_callback = cb;
  1831. }
  1832. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  1833. void (*cb)(int write_p, int version,
  1834. int content_type, const void *buf,
  1835. size_t len, SSL *ssl, void *arg)) {
  1836. ctx->msg_callback = cb;
  1837. }
  1838. void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
  1839. ctx->msg_callback_arg = arg;
  1840. }
  1841. void SSL_set_msg_callback(SSL *ssl,
  1842. void (*cb)(int write_p, int version, int content_type,
  1843. const void *buf, size_t len, SSL *ssl,
  1844. void *arg)) {
  1845. ssl->msg_callback = cb;
  1846. }
  1847. void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
  1848. ssl->msg_callback_arg = arg;
  1849. }
  1850. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
  1851. void (*cb)(const SSL *ssl, const char *line)) {
  1852. ctx->keylog_callback = cb;
  1853. }
  1854. static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
  1855. static const char hextable[] = "0123456789abcdef";
  1856. uint8_t *out;
  1857. size_t i;
  1858. if (!CBB_add_space(cbb, &out, in_len * 2)) {
  1859. return 0;
  1860. }
  1861. for (i = 0; i < in_len; i++) {
  1862. *(out++) = (uint8_t)hextable[in[i] >> 4];
  1863. *(out++) = (uint8_t)hextable[in[i] & 0xf];
  1864. }
  1865. return 1;
  1866. }
  1867. int ssl_log_rsa_client_key_exchange(const SSL *ssl,
  1868. const uint8_t *encrypted_premaster,
  1869. size_t encrypted_premaster_len,
  1870. const uint8_t *premaster,
  1871. size_t premaster_len) {
  1872. if (ssl->ctx->keylog_callback == NULL) {
  1873. return 1;
  1874. }
  1875. if (encrypted_premaster_len < 8) {
  1876. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1877. return 0;
  1878. }
  1879. CBB cbb;
  1880. uint8_t *out;
  1881. size_t out_len;
  1882. if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
  1883. !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
  1884. /* Only the first 8 bytes of the encrypted premaster secret are
  1885. * logged. */
  1886. !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
  1887. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  1888. !cbb_add_hex(&cbb, premaster, premaster_len) ||
  1889. !CBB_add_u8(&cbb, 0 /* NUL */) ||
  1890. !CBB_finish(&cbb, &out, &out_len)) {
  1891. CBB_cleanup(&cbb);
  1892. return 0;
  1893. }
  1894. ssl->ctx->keylog_callback(ssl, (const char *)out);
  1895. OPENSSL_free(out);
  1896. return 1;
  1897. }
  1898. int ssl_log_master_secret(const SSL *ssl, const uint8_t *client_random,
  1899. size_t client_random_len, const uint8_t *master,
  1900. size_t master_len) {
  1901. if (ssl->ctx->keylog_callback == NULL) {
  1902. return 1;
  1903. }
  1904. if (client_random_len != 32) {
  1905. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1906. return 0;
  1907. }
  1908. CBB cbb;
  1909. uint8_t *out;
  1910. size_t out_len;
  1911. if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
  1912. !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
  1913. !cbb_add_hex(&cbb, client_random, 32) ||
  1914. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  1915. !cbb_add_hex(&cbb, master, master_len) ||
  1916. !CBB_add_u8(&cbb, 0 /* NUL */) ||
  1917. !CBB_finish(&cbb, &out, &out_len)) {
  1918. CBB_cleanup(&cbb);
  1919. return 0;
  1920. }
  1921. ssl->ctx->keylog_callback(ssl, (const char *)out);
  1922. OPENSSL_free(out);
  1923. return 1;
  1924. }
  1925. int SSL_is_init_finished(const SSL *ssl) {
  1926. return ssl->state == SSL_ST_OK;
  1927. }
  1928. int SSL_in_init(const SSL *ssl) {
  1929. return (ssl->state & SSL_ST_INIT) != 0;
  1930. }
  1931. int SSL_in_false_start(const SSL *ssl) {
  1932. return ssl->s3->tmp.in_false_start;
  1933. }
  1934. int SSL_cutthrough_complete(const SSL *s) {
  1935. return SSL_in_false_start(s);
  1936. }
  1937. void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
  1938. size_t *ssl_session_size) {
  1939. *ssl_size = sizeof(SSL);
  1940. *ssl_ctx_size = sizeof(SSL_CTX);
  1941. *ssl_session_size = sizeof(SSL_SESSION);
  1942. }
  1943. int ssl3_can_false_start(const SSL *s) {
  1944. const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
  1945. /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
  1946. return !SSL_IS_DTLS(s) &&
  1947. SSL_version(s) >= TLS1_2_VERSION &&
  1948. (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
  1949. cipher != NULL &&
  1950. cipher->algorithm_mkey == SSL_kECDHE &&
  1951. (cipher->algorithm_enc == SSL_AES128GCM ||
  1952. cipher->algorithm_enc == SSL_AES256GCM ||
  1953. cipher->algorithm_enc == SSL_CHACHA20POLY1305_OLD);
  1954. }
  1955. const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
  1956. switch (version) {
  1957. case SSL3_VERSION:
  1958. return &SSLv3_enc_data;
  1959. case TLS1_VERSION:
  1960. return &TLSv1_enc_data;
  1961. case DTLS1_VERSION:
  1962. case TLS1_1_VERSION:
  1963. return &TLSv1_1_enc_data;
  1964. case DTLS1_2_VERSION:
  1965. case TLS1_2_VERSION:
  1966. return &TLSv1_2_enc_data;
  1967. default:
  1968. return NULL;
  1969. }
  1970. }
  1971. uint16_t ssl3_get_max_server_version(const SSL *s) {
  1972. uint16_t max_version;
  1973. if (SSL_IS_DTLS(s)) {
  1974. max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
  1975. if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
  1976. return DTLS1_2_VERSION;
  1977. }
  1978. if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
  1979. return DTLS1_VERSION;
  1980. }
  1981. return 0;
  1982. }
  1983. max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
  1984. if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
  1985. return TLS1_2_VERSION;
  1986. }
  1987. if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
  1988. return TLS1_1_VERSION;
  1989. }
  1990. if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
  1991. return TLS1_VERSION;
  1992. }
  1993. if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
  1994. return SSL3_VERSION;
  1995. }
  1996. return 0;
  1997. }
  1998. uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
  1999. uint16_t version = 0;
  2000. if (SSL_IS_DTLS(s)) {
  2001. /* Clamp client_version to max_version. */
  2002. if (s->max_version != 0 && client_version < s->max_version) {
  2003. client_version = s->max_version;
  2004. }
  2005. if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
  2006. version = DTLS1_2_VERSION;
  2007. } else if (client_version <= DTLS1_VERSION &&
  2008. !(s->options & SSL_OP_NO_DTLSv1)) {
  2009. version = DTLS1_VERSION;
  2010. }
  2011. /* Check against min_version. */
  2012. if (version != 0 && s->min_version != 0 && version > s->min_version) {
  2013. return 0;
  2014. }
  2015. return version;
  2016. } else {
  2017. /* Clamp client_version to max_version. */
  2018. if (s->max_version != 0 && client_version > s->max_version) {
  2019. client_version = s->max_version;
  2020. }
  2021. if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
  2022. version = TLS1_2_VERSION;
  2023. } else if (client_version >= TLS1_1_VERSION &&
  2024. !(s->options & SSL_OP_NO_TLSv1_1)) {
  2025. version = TLS1_1_VERSION;
  2026. } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
  2027. version = TLS1_VERSION;
  2028. } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
  2029. version = SSL3_VERSION;
  2030. }
  2031. /* Check against min_version. */
  2032. if (version != 0 && s->min_version != 0 && version < s->min_version) {
  2033. return 0;
  2034. }
  2035. return version;
  2036. }
  2037. }
  2038. uint16_t ssl3_get_max_client_version(SSL *s) {
  2039. uint32_t options = s->options;
  2040. uint16_t version = 0;
  2041. /* OpenSSL's API for controlling versions entails blacklisting individual
  2042. * protocols. This has two problems. First, on the client, the protocol can
  2043. * only express a contiguous range of versions. Second, a library consumer
  2044. * trying to set a maximum version cannot disable protocol versions that get
  2045. * added in a future version of the library.
  2046. *
  2047. * To account for both of these, OpenSSL interprets the client-side bitmask
  2048. * as a min/max range by picking the lowest contiguous non-empty range of
  2049. * enabled protocols. Note that this means it is impossible to set a maximum
  2050. * version of TLS 1.2 in a future-proof way.
  2051. *
  2052. * By this scheme, the maximum version is the lowest version V such that V is
  2053. * enabled and V+1 is disabled or unimplemented. */
  2054. if (SSL_IS_DTLS(s)) {
  2055. if (!(options & SSL_OP_NO_DTLSv1_2)) {
  2056. version = DTLS1_2_VERSION;
  2057. }
  2058. if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
  2059. version = DTLS1_VERSION;
  2060. }
  2061. if (s->max_version != 0 && version < s->max_version) {
  2062. version = s->max_version;
  2063. }
  2064. } else {
  2065. if (!(options & SSL_OP_NO_TLSv1_2)) {
  2066. version = TLS1_2_VERSION;
  2067. }
  2068. if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
  2069. version = TLS1_1_VERSION;
  2070. }
  2071. if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
  2072. version = TLS1_VERSION;
  2073. }
  2074. if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
  2075. version = SSL3_VERSION;
  2076. }
  2077. if (s->max_version != 0 && version > s->max_version) {
  2078. version = s->max_version;
  2079. }
  2080. }
  2081. return version;
  2082. }
  2083. int ssl3_is_version_enabled(SSL *s, uint16_t version) {
  2084. if (SSL_IS_DTLS(s)) {
  2085. if (s->max_version != 0 && version < s->max_version) {
  2086. return 0;
  2087. }
  2088. if (s->min_version != 0 && version > s->min_version) {
  2089. return 0;
  2090. }
  2091. switch (version) {
  2092. case DTLS1_VERSION:
  2093. return !(s->options & SSL_OP_NO_DTLSv1);
  2094. case DTLS1_2_VERSION:
  2095. return !(s->options & SSL_OP_NO_DTLSv1_2);
  2096. default:
  2097. return 0;
  2098. }
  2099. } else {
  2100. if (s->max_version != 0 && version > s->max_version) {
  2101. return 0;
  2102. }
  2103. if (s->min_version != 0 && version < s->min_version) {
  2104. return 0;
  2105. }
  2106. switch (version) {
  2107. case SSL3_VERSION:
  2108. return !(s->options & SSL_OP_NO_SSLv3);
  2109. case TLS1_VERSION:
  2110. return !(s->options & SSL_OP_NO_TLSv1);
  2111. case TLS1_1_VERSION:
  2112. return !(s->options & SSL_OP_NO_TLSv1_1);
  2113. case TLS1_2_VERSION:
  2114. return !(s->options & SSL_OP_NO_TLSv1_2);
  2115. default:
  2116. return 0;
  2117. }
  2118. }
  2119. }
  2120. uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
  2121. if (!SSL_IS_DTLS(s)) {
  2122. return wire_version;
  2123. }
  2124. uint16_t tls_version = ~wire_version;
  2125. uint16_t version = tls_version + 0x0201;
  2126. /* If either component overflowed, clamp it so comparisons still work. */
  2127. if ((version >> 8) < (tls_version >> 8)) {
  2128. version = 0xff00 | (version & 0xff);
  2129. }
  2130. if ((version & 0xff) < (tls_version & 0xff)) {
  2131. version = (version & 0xff00) | 0xff;
  2132. }
  2133. /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
  2134. if (version == TLS1_VERSION) {
  2135. version = TLS1_1_VERSION;
  2136. }
  2137. return version;
  2138. }
  2139. int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
  2140. int SSL_is_server(SSL *ssl) { return ssl->server; }
  2141. void SSL_CTX_set_select_certificate_cb(
  2142. SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
  2143. ctx->select_certificate_cb = cb;
  2144. }
  2145. void SSL_CTX_set_dos_protection_cb(
  2146. SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
  2147. ctx->dos_protection_cb = cb;
  2148. }
  2149. void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
  2150. ssl->renegotiate_mode = mode;
  2151. }
  2152. void SSL_set_reject_peer_renegotiations(SSL *ssl, int reject) {
  2153. SSL_set_renegotiate_mode(
  2154. ssl, reject ? ssl_renegotiate_never : ssl_renegotiate_freely);
  2155. }
  2156. int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
  2157. const RC4_KEY **write_key) {
  2158. if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
  2159. return 0;
  2160. }
  2161. return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
  2162. EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
  2163. }
  2164. int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
  2165. const uint8_t **out_write_iv, size_t *out_iv_len) {
  2166. if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
  2167. return 0;
  2168. }
  2169. size_t write_iv_len;
  2170. if (!EVP_AEAD_CTX_get_iv(&ssl->aead_read_ctx->ctx, out_read_iv, out_iv_len) ||
  2171. !EVP_AEAD_CTX_get_iv(&ssl->aead_write_ctx->ctx, out_write_iv,
  2172. &write_iv_len) ||
  2173. *out_iv_len != write_iv_len) {
  2174. return 0;
  2175. }
  2176. return 1;
  2177. }
  2178. uint8_t SSL_get_server_key_exchange_hash(const SSL *ssl) {
  2179. return ssl->s3->tmp.server_key_exchange_hash;
  2180. }
  2181. int SSL_clear(SSL *ssl) {
  2182. if (ssl->method == NULL) {
  2183. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
  2184. return 0;
  2185. }
  2186. if (ssl_clear_bad_session(ssl)) {
  2187. SSL_SESSION_free(ssl->session);
  2188. ssl->session = NULL;
  2189. }
  2190. ssl->hit = 0;
  2191. ssl->shutdown = 0;
  2192. /* SSL_clear may be called before or after the |ssl| is initialized in either
  2193. * accept or connect state. In the latter case, SSL_clear should preserve the
  2194. * half and reset |ssl->state| accordingly. */
  2195. if (ssl->handshake_func != NULL) {
  2196. if (ssl->server) {
  2197. SSL_set_accept_state(ssl);
  2198. } else {
  2199. SSL_set_connect_state(ssl);
  2200. }
  2201. } else {
  2202. assert(ssl->state == 0);
  2203. }
  2204. /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
  2205. * |SSL_clear| because it is per-connection state rather than configuration
  2206. * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
  2207. * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
  2208. * |ssl3_new|. */
  2209. ssl->rwstate = SSL_NOTHING;
  2210. BUF_MEM_free(ssl->init_buf);
  2211. ssl->init_buf = NULL;
  2212. ssl_clear_cipher_ctx(ssl);
  2213. OPENSSL_free(ssl->next_proto_negotiated);
  2214. ssl->next_proto_negotiated = NULL;
  2215. ssl->next_proto_negotiated_len = 0;
  2216. /* The ssl->d1->mtu is simultaneously configuration (preserved across
  2217. * clear) and connection-specific state (gets reset).
  2218. *
  2219. * TODO(davidben): Avoid this. */
  2220. unsigned mtu = 0;
  2221. if (ssl->d1 != NULL) {
  2222. mtu = ssl->d1->mtu;
  2223. }
  2224. ssl->method->ssl_free(ssl);
  2225. if (!ssl->method->ssl_new(ssl)) {
  2226. return 0;
  2227. }
  2228. ssl->enc_method = ssl3_get_enc_method(ssl->version);
  2229. assert(ssl->enc_method != NULL);
  2230. if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  2231. ssl->d1->mtu = mtu;
  2232. }
  2233. ssl->client_version = ssl->version;
  2234. return 1;
  2235. }
  2236. int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
  2237. int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
  2238. int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
  2239. int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
  2240. int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
  2241. int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
  2242. int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
  2243. int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
  2244. int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
  2245. int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
  2246. int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
  2247. void ERR_load_SSL_strings(void) {}
  2248. void SSL_load_error_strings(void) {}