internal.h 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011
  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. */
  141. #ifndef OPENSSL_HEADER_SSL_INTERNAL_H
  142. #define OPENSSL_HEADER_SSL_INTERNAL_H
  143. #include <openssl/base.h>
  144. #include <stdlib.h>
  145. #include <limits>
  146. #include <new>
  147. #include <type_traits>
  148. #include <utility>
  149. #include <openssl/aead.h>
  150. #include <openssl/err.h>
  151. #include <openssl/lhash.h>
  152. #include <openssl/mem.h>
  153. #include <openssl/ssl.h>
  154. #include <openssl/span.h>
  155. #include <openssl/stack.h>
  156. #include "../crypto/err/internal.h"
  157. #include "../crypto/internal.h"
  158. #if defined(OPENSSL_WINDOWS)
  159. // Windows defines struct timeval in winsock2.h.
  160. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  161. #include <winsock2.h>
  162. OPENSSL_MSVC_PRAGMA(warning(pop))
  163. #else
  164. #include <sys/time.h>
  165. #endif
  166. namespace bssl {
  167. struct SSL_HANDSHAKE;
  168. struct SSL_PROTOCOL_METHOD;
  169. // C++ utilities.
  170. // New behaves like |new| but uses |OPENSSL_malloc| for memory allocation. It
  171. // returns nullptr on allocation error. It only implements single-object
  172. // allocation and not new T[n].
  173. //
  174. // Note: unlike |new|, this does not support non-public constructors.
  175. template <typename T, typename... Args>
  176. T *New(Args &&... args) {
  177. void *t = OPENSSL_malloc(sizeof(T));
  178. if (t == nullptr) {
  179. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  180. return nullptr;
  181. }
  182. return new (t) T(std::forward<Args>(args)...);
  183. }
  184. // Delete behaves like |delete| but uses |OPENSSL_free| to release memory.
  185. //
  186. // Note: unlike |delete| this does not support non-public destructors.
  187. template <typename T>
  188. void Delete(T *t) {
  189. if (t != nullptr) {
  190. t->~T();
  191. OPENSSL_free(t);
  192. }
  193. }
  194. // All types with kAllowUniquePtr set may be used with UniquePtr. Other types
  195. // may be C structs which require a |BORINGSSL_MAKE_DELETER| registration.
  196. namespace internal {
  197. template <typename T>
  198. struct DeleterImpl<T, typename std::enable_if<T::kAllowUniquePtr>::type> {
  199. static void Free(T *t) { Delete(t); }
  200. };
  201. }
  202. // MakeUnique behaves like |std::make_unique| but returns nullptr on allocation
  203. // error.
  204. template <typename T, typename... Args>
  205. UniquePtr<T> MakeUnique(Args &&... args) {
  206. return UniquePtr<T>(New<T>(std::forward<Args>(args)...));
  207. }
  208. #if defined(BORINGSSL_ALLOW_CXX_RUNTIME)
  209. #define HAS_VIRTUAL_DESTRUCTOR
  210. #define PURE_VIRTUAL = 0
  211. #else
  212. // HAS_VIRTUAL_DESTRUCTOR should be declared in any base class which defines a
  213. // virtual destructor. This avoids a dependency on |_ZdlPv| and prevents the
  214. // class from being used with |delete|.
  215. #define HAS_VIRTUAL_DESTRUCTOR \
  216. void operator delete(void *) { abort(); }
  217. // PURE_VIRTUAL should be used instead of = 0 when defining pure-virtual
  218. // functions. This avoids a dependency on |__cxa_pure_virtual| but loses
  219. // compile-time checking.
  220. #define PURE_VIRTUAL { abort(); }
  221. #endif
  222. // CONSTEXPR_ARRAY works around a VS 2015 bug where ranged for loops don't work
  223. // on constexpr arrays.
  224. #if defined(_MSC_VER) && !defined(__clang__) && _MSC_VER < 1910
  225. #define CONSTEXPR_ARRAY const
  226. #else
  227. #define CONSTEXPR_ARRAY constexpr
  228. #endif
  229. // Array<T> is an owning array of elements of |T|.
  230. template <typename T>
  231. class Array {
  232. public:
  233. // Array's default constructor creates an empty array.
  234. Array() {}
  235. Array(const Array &) = delete;
  236. Array(Array &&other) { *this = std::move(other); }
  237. ~Array() { Reset(); }
  238. Array &operator=(const Array &) = delete;
  239. Array &operator=(Array &&other) {
  240. Reset();
  241. other.Release(&data_, &size_);
  242. return *this;
  243. }
  244. const T *data() const { return data_; }
  245. T *data() { return data_; }
  246. size_t size() const { return size_; }
  247. bool empty() const { return size_ == 0; }
  248. const T &operator[](size_t i) const { return data_[i]; }
  249. T &operator[](size_t i) { return data_[i]; }
  250. T *begin() { return data_; }
  251. const T *cbegin() const { return data_; }
  252. T *end() { return data_ + size_; }
  253. const T *cend() const { return data_ + size_; }
  254. void Reset() { Reset(nullptr, 0); }
  255. // Reset releases the current contents of the array and takes ownership of the
  256. // raw pointer supplied by the caller.
  257. void Reset(T *new_data, size_t new_size) {
  258. for (size_t i = 0; i < size_; i++) {
  259. data_[i].~T();
  260. }
  261. OPENSSL_free(data_);
  262. data_ = new_data;
  263. size_ = new_size;
  264. }
  265. // Release releases ownership of the array to a raw pointer supplied by the
  266. // caller.
  267. void Release(T **out, size_t *out_size) {
  268. *out = data_;
  269. *out_size = size_;
  270. data_ = nullptr;
  271. size_ = 0;
  272. }
  273. // Init replaces the array with a newly-allocated array of |new_size|
  274. // default-constructed copies of |T|. It returns true on success and false on
  275. // error.
  276. //
  277. // Note that if |T| is a primitive type like |uint8_t|, it is uninitialized.
  278. bool Init(size_t new_size) {
  279. Reset();
  280. if (new_size == 0) {
  281. return true;
  282. }
  283. if (new_size > std::numeric_limits<size_t>::max() / sizeof(T)) {
  284. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  285. return false;
  286. }
  287. data_ = reinterpret_cast<T*>(OPENSSL_malloc(new_size * sizeof(T)));
  288. if (data_ == nullptr) {
  289. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  290. return false;
  291. }
  292. size_ = new_size;
  293. for (size_t i = 0; i < size_; i++) {
  294. new (&data_[i]) T;
  295. }
  296. return true;
  297. }
  298. // CopyFrom replaces the array with a newly-allocated copy of |in|. It returns
  299. // true on success and false on error.
  300. bool CopyFrom(Span<const uint8_t> in) {
  301. if (!Init(in.size())) {
  302. return false;
  303. }
  304. OPENSSL_memcpy(data_, in.data(), in.size());
  305. return true;
  306. }
  307. private:
  308. T *data_ = nullptr;
  309. size_t size_ = 0;
  310. };
  311. // CBBFinishArray behaves like |CBB_finish| but stores the result in an Array.
  312. bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out);
  313. // Protocol versions.
  314. //
  315. // Due to DTLS's historical wire version differences and to support multiple
  316. // variants of the same protocol during development, we maintain two notions of
  317. // version.
  318. //
  319. // The "version" or "wire version" is the actual 16-bit value that appears on
  320. // the wire. It uniquely identifies a version and is also used at API
  321. // boundaries. The set of supported versions differs between TLS and DTLS. Wire
  322. // versions are opaque values and may not be compared numerically.
  323. //
  324. // The "protocol version" identifies the high-level handshake variant being
  325. // used. DTLS versions map to the corresponding TLS versions. Draft TLS 1.3
  326. // variants all map to TLS 1.3. Protocol versions are sequential and may be
  327. // compared numerically.
  328. // ssl_protocol_version_from_wire sets |*out| to the protocol version
  329. // corresponding to wire version |version| and returns true. If |version| is not
  330. // a valid TLS or DTLS version, it returns false.
  331. //
  332. // Note this simultaneously handles both DTLS and TLS. Use one of the
  333. // higher-level functions below for most operations.
  334. bool ssl_protocol_version_from_wire(uint16_t *out, uint16_t version);
  335. // ssl_get_version_range sets |*out_min_version| and |*out_max_version| to the
  336. // minimum and maximum enabled protocol versions, respectively.
  337. bool ssl_get_version_range(const SSL *ssl, uint16_t *out_min_version,
  338. uint16_t *out_max_version);
  339. // ssl_supports_version returns whether |hs| supports |version|.
  340. bool ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version);
  341. // ssl_add_supported_versions writes the supported versions of |hs| to |cbb|, in
  342. // decreasing preference order.
  343. bool ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb);
  344. // ssl_negotiate_version negotiates a common version based on |hs|'s preferences
  345. // and the peer preference list in |peer_versions|. On success, it returns true
  346. // and sets |*out_version| to the selected version. Otherwise, it returns false
  347. // and sets |*out_alert| to an alert to send.
  348. bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  349. uint16_t *out_version, const CBS *peer_versions);
  350. // ssl_protocol_version returns |ssl|'s protocol version. It is an error to
  351. // call this function before the version is determined.
  352. uint16_t ssl_protocol_version(const SSL *ssl);
  353. // ssl_is_draft21 returns whether the version corresponds to a draft21 TLS 1.3
  354. // variant.
  355. bool ssl_is_draft21(uint16_t version);
  356. // ssl_is_draft22 returns whether the version corresponds to a draft22 TLS 1.3
  357. // variant.
  358. bool ssl_is_draft22(uint16_t version);
  359. // ssl_is_resumption_experiment returns whether the version corresponds to a
  360. // TLS 1.3 resumption experiment.
  361. bool ssl_is_resumption_experiment(uint16_t version);
  362. // ssl_is_resumption_variant returns whether the version corresponds to a
  363. // TLS 1.3 resumption experiment.
  364. bool ssl_is_resumption_variant(enum tls13_variant_t variant);
  365. // ssl_is_resumption_client_ccs_experiment returns whether the version
  366. // corresponds to a TLS 1.3 resumption experiment that sends a client CCS.
  367. bool ssl_is_resumption_client_ccs_experiment(uint16_t version);
  368. // ssl_is_resumption_record_version_experiment returns whether the version
  369. // corresponds to a TLS 1.3 resumption experiment that modifies the record
  370. // version.
  371. bool ssl_is_resumption_record_version_experiment(uint16_t version);
  372. // Cipher suites.
  373. } // namespace bssl
  374. struct ssl_cipher_st {
  375. // name is the OpenSSL name for the cipher.
  376. const char *name;
  377. // standard_name is the IETF name for the cipher.
  378. const char *standard_name;
  379. // id is the cipher suite value bitwise OR-d with 0x03000000.
  380. uint32_t id;
  381. // algorithm_* determine the cipher suite. See constants below for the values.
  382. uint32_t algorithm_mkey;
  383. uint32_t algorithm_auth;
  384. uint32_t algorithm_enc;
  385. uint32_t algorithm_mac;
  386. uint32_t algorithm_prf;
  387. };
  388. namespace bssl {
  389. // Bits for |algorithm_mkey| (key exchange algorithm).
  390. #define SSL_kRSA 0x00000001u
  391. #define SSL_kECDHE 0x00000002u
  392. // SSL_kPSK is only set for plain PSK, not ECDHE_PSK.
  393. #define SSL_kPSK 0x00000004u
  394. #define SSL_kGENERIC 0x00000008u
  395. // Bits for |algorithm_auth| (server authentication).
  396. #define SSL_aRSA 0x00000001u
  397. #define SSL_aECDSA 0x00000002u
  398. // SSL_aPSK is set for both PSK and ECDHE_PSK.
  399. #define SSL_aPSK 0x00000004u
  400. #define SSL_aGENERIC 0x00000008u
  401. #define SSL_aCERT (SSL_aRSA | SSL_aECDSA)
  402. // Bits for |algorithm_enc| (symmetric encryption).
  403. #define SSL_3DES 0x00000001u
  404. #define SSL_AES128 0x00000002u
  405. #define SSL_AES256 0x00000004u
  406. #define SSL_AES128GCM 0x00000008u
  407. #define SSL_AES256GCM 0x00000010u
  408. #define SSL_eNULL 0x00000020u
  409. #define SSL_CHACHA20POLY1305 0x00000040u
  410. #define SSL_AES (SSL_AES128 | SSL_AES256 | SSL_AES128GCM | SSL_AES256GCM)
  411. // Bits for |algorithm_mac| (symmetric authentication).
  412. #define SSL_SHA1 0x00000001u
  413. #define SSL_SHA256 0x00000002u
  414. #define SSL_SHA384 0x00000004u
  415. // SSL_AEAD is set for all AEADs.
  416. #define SSL_AEAD 0x00000008u
  417. // Bits for |algorithm_prf| (handshake digest).
  418. #define SSL_HANDSHAKE_MAC_DEFAULT 0x1
  419. #define SSL_HANDSHAKE_MAC_SHA256 0x2
  420. #define SSL_HANDSHAKE_MAC_SHA384 0x4
  421. // SSL_MAX_DIGEST is the number of digest types which exist. When adding a new
  422. // one, update the table in ssl_cipher.c.
  423. #define SSL_MAX_DIGEST 4
  424. // ssl_cipher_get_evp_aead sets |*out_aead| to point to the correct EVP_AEAD
  425. // object for |cipher| protocol version |version|. It sets |*out_mac_secret_len|
  426. // and |*out_fixed_iv_len| to the MAC key length and fixed IV length,
  427. // respectively. The MAC key length is zero except for legacy block and stream
  428. // ciphers. It returns true on success and false on error.
  429. bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
  430. size_t *out_mac_secret_len,
  431. size_t *out_fixed_iv_len, const SSL_CIPHER *cipher,
  432. uint16_t version, int is_dtls);
  433. // ssl_get_handshake_digest returns the |EVP_MD| corresponding to |version| and
  434. // |cipher|.
  435. const EVP_MD *ssl_get_handshake_digest(uint16_t version,
  436. const SSL_CIPHER *cipher);
  437. // ssl_create_cipher_list evaluates |rule_str|. It sets |*out_cipher_list| to a
  438. // newly-allocated |ssl_cipher_preference_list_st| containing the result. It
  439. // returns true on success and false on failure. If |strict| is true, nonsense
  440. // will be rejected. If false, nonsense will be silently ignored. An empty
  441. // result is considered an error regardless of |strict|.
  442. bool ssl_create_cipher_list(
  443. struct ssl_cipher_preference_list_st **out_cipher_list,
  444. const char *rule_str, bool strict);
  445. // ssl_cipher_get_value returns the cipher suite id of |cipher|.
  446. uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher);
  447. // ssl_cipher_auth_mask_for_key returns the mask of cipher |algorithm_auth|
  448. // values suitable for use with |key| in TLS 1.2 and below.
  449. uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key);
  450. // ssl_cipher_uses_certificate_auth returns whether |cipher| authenticates the
  451. // server and, optionally, the client with a certificate.
  452. bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher);
  453. // ssl_cipher_requires_server_key_exchange returns whether |cipher| requires a
  454. // ServerKeyExchange message.
  455. //
  456. // This function may return false while still allowing |cipher| an optional
  457. // ServerKeyExchange. This is the case for plain PSK ciphers.
  458. bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher);
  459. // ssl_cipher_get_record_split_len, for TLS 1.0 CBC mode ciphers, returns the
  460. // length of an encrypted 1-byte record, for use in record-splitting. Otherwise
  461. // it returns zero.
  462. size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher);
  463. // Transcript layer.
  464. // SSLTranscript maintains the handshake transcript as a combination of a
  465. // buffer and running hash.
  466. class SSLTranscript {
  467. public:
  468. SSLTranscript();
  469. ~SSLTranscript();
  470. // Init initializes the handshake transcript. If called on an existing
  471. // transcript, it resets the transcript and hash. It returns true on success
  472. // and false on failure.
  473. bool Init();
  474. // InitHash initializes the handshake hash based on the PRF and contents of
  475. // the handshake transcript. Subsequent calls to |Update| will update the
  476. // rolling hash. It returns one on success and zero on failure. It is an error
  477. // to call this function after the handshake buffer is released.
  478. bool InitHash(uint16_t version, const SSL_CIPHER *cipher);
  479. // UpdateForHelloRetryRequest resets the rolling hash with the
  480. // HelloRetryRequest construction. It returns true on success and false on
  481. // failure. It is an error to call this function before the handshake buffer
  482. // is released.
  483. bool UpdateForHelloRetryRequest();
  484. // CopyHashContext copies the hash context into |ctx| and returns true on
  485. // success.
  486. bool CopyHashContext(EVP_MD_CTX *ctx);
  487. Span<const uint8_t> buffer() {
  488. return MakeConstSpan(reinterpret_cast<const uint8_t *>(buffer_->data),
  489. buffer_->length);
  490. }
  491. // FreeBuffer releases the handshake buffer. Subsequent calls to
  492. // |Update| will not update the handshake buffer.
  493. void FreeBuffer();
  494. // DigestLen returns the length of the PRF hash.
  495. size_t DigestLen() const;
  496. // Digest returns the PRF hash. For TLS 1.1 and below, this is
  497. // |EVP_md5_sha1|.
  498. const EVP_MD *Digest() const;
  499. // Update adds |in| to the handshake buffer and handshake hash, whichever is
  500. // enabled. It returns true on success and false on failure.
  501. bool Update(Span<const uint8_t> in);
  502. // GetHash writes the handshake hash to |out| which must have room for at
  503. // least |DigestLen| bytes. On success, it returns true and sets |*out_len| to
  504. // the number of bytes written. Otherwise, it returns false.
  505. bool GetHash(uint8_t *out, size_t *out_len);
  506. // GetSSL3CertVerifyHash writes the SSL 3.0 CertificateVerify hash into the
  507. // bytes pointed to by |out| and writes the number of bytes to
  508. // |*out_len|. |out| must have room for |EVP_MAX_MD_SIZE| bytes. It returns
  509. // one on success and zero on failure.
  510. bool GetSSL3CertVerifyHash(uint8_t *out, size_t *out_len,
  511. const SSL_SESSION *session,
  512. uint16_t signature_algorithm);
  513. // GetFinishedMAC computes the MAC for the Finished message into the bytes
  514. // pointed by |out| and writes the number of bytes to |*out_len|. |out| must
  515. // have room for |EVP_MAX_MD_SIZE| bytes. It returns true on success and false
  516. // on failure.
  517. bool GetFinishedMAC(uint8_t *out, size_t *out_len, const SSL_SESSION *session,
  518. bool from_server);
  519. private:
  520. // buffer_, if non-null, contains the handshake transcript.
  521. UniquePtr<BUF_MEM> buffer_;
  522. // hash, if initialized with an |EVP_MD|, maintains the handshake hash. For
  523. // TLS 1.1 and below, it is the SHA-1 half.
  524. ScopedEVP_MD_CTX hash_;
  525. // md5, if initialized with an |EVP_MD|, maintains the MD5 half of the
  526. // handshake hash for TLS 1.1 and below.
  527. ScopedEVP_MD_CTX md5_;
  528. };
  529. // tls1_prf computes the PRF function for |ssl|. It fills |out|, using |secret|
  530. // as the secret and |label| as the label. |seed1| and |seed2| are concatenated
  531. // to form the seed parameter. It returns true on success and false on failure.
  532. bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out,
  533. Span<const uint8_t> secret, Span<const char> label,
  534. Span<const uint8_t> seed1, Span<const uint8_t> seed2);
  535. // Encryption layer.
  536. // SSLAEADContext contains information about an AEAD that is being used to
  537. // encrypt an SSL connection.
  538. class SSLAEADContext {
  539. public:
  540. SSLAEADContext(uint16_t version, bool is_dtls, const SSL_CIPHER *cipher);
  541. ~SSLAEADContext();
  542. static constexpr bool kAllowUniquePtr = true;
  543. SSLAEADContext(const SSLAEADContext &&) = delete;
  544. SSLAEADContext &operator=(const SSLAEADContext &&) = delete;
  545. // CreateNullCipher creates an |SSLAEADContext| for the null cipher.
  546. static UniquePtr<SSLAEADContext> CreateNullCipher(bool is_dtls);
  547. // Create creates an |SSLAEADContext| using the supplied key material. It
  548. // returns nullptr on error. Only one of |Open| or |Seal| may be used with the
  549. // resulting object, depending on |direction|. |version| is the normalized
  550. // protocol version, so DTLS 1.0 is represented as 0x0301, not 0xffef.
  551. static UniquePtr<SSLAEADContext> Create(enum evp_aead_direction_t direction,
  552. uint16_t version, int is_dtls,
  553. const SSL_CIPHER *cipher,
  554. Span<const uint8_t> enc_key,
  555. Span<const uint8_t> mac_key,
  556. Span<const uint8_t> fixed_iv);
  557. // SetVersionIfNullCipher sets the version the SSLAEADContext for the null
  558. // cipher, to make version-specific determinations in the record layer prior
  559. // to a cipher being selected.
  560. void SetVersionIfNullCipher(uint16_t version);
  561. // ProtocolVersion returns the protocol version associated with this
  562. // SSLAEADContext. It can only be called once |version_| has been set to a
  563. // valid value.
  564. uint16_t ProtocolVersion() const;
  565. // RecordVersion returns the record version that should be used with this
  566. // SSLAEADContext for record construction and crypto.
  567. uint16_t RecordVersion() const;
  568. const SSL_CIPHER *cipher() const { return cipher_; }
  569. // is_null_cipher returns true if this is the null cipher.
  570. bool is_null_cipher() const { return !cipher_; }
  571. // ExplicitNonceLen returns the length of the explicit nonce.
  572. size_t ExplicitNonceLen() const;
  573. // MaxOverhead returns the maximum overhead of calling |Seal|.
  574. size_t MaxOverhead() const;
  575. // SuffixLen calculates the suffix length written by |SealScatter| and writes
  576. // it to |*out_suffix_len|. It returns true on success and false on error.
  577. // |in_len| and |extra_in_len| should equal the argument of the same names
  578. // passed to |SealScatter|.
  579. bool SuffixLen(size_t *out_suffix_len, size_t in_len,
  580. size_t extra_in_len) const;
  581. // Open authenticates and decrypts |in| in-place. On success, it sets |*out|
  582. // to the plaintext in |in| and returns true. Otherwise, it returns
  583. // false. The output will always be |ExplicitNonceLen| bytes ahead of |in|.
  584. bool Open(Span<uint8_t> *out, uint8_t type, uint16_t record_version,
  585. const uint8_t seqnum[8], Span<uint8_t> in);
  586. // Seal encrypts and authenticates |in_len| bytes from |in| and writes the
  587. // result to |out|. It returns true on success and false on error.
  588. //
  589. // If |in| and |out| alias then |out| + |ExplicitNonceLen| must be == |in|.
  590. bool Seal(uint8_t *out, size_t *out_len, size_t max_out, uint8_t type,
  591. uint16_t record_version, const uint8_t seqnum[8], const uint8_t *in,
  592. size_t in_len);
  593. // SealScatter encrypts and authenticates |in_len| bytes from |in| and splits
  594. // the result between |out_prefix|, |out| and |out_suffix|. It returns one on
  595. // success and zero on error.
  596. //
  597. // On successful return, exactly |ExplicitNonceLen| bytes are written to
  598. // |out_prefix|, |in_len| bytes to |out|, and |SuffixLen| bytes to
  599. // |out_suffix|.
  600. //
  601. // |extra_in| may point to an additional plaintext buffer. If present,
  602. // |extra_in_len| additional bytes are encrypted and authenticated, and the
  603. // ciphertext is written to the beginning of |out_suffix|. |SuffixLen| should
  604. // be used to size |out_suffix| accordingly.
  605. //
  606. // If |in| and |out| alias then |out| must be == |in|. Other arguments may not
  607. // alias anything.
  608. bool SealScatter(uint8_t *out_prefix, uint8_t *out, uint8_t *out_suffix,
  609. uint8_t type, uint16_t record_version,
  610. const uint8_t seqnum[8], const uint8_t *in, size_t in_len,
  611. const uint8_t *extra_in, size_t extra_in_len);
  612. bool GetIV(const uint8_t **out_iv, size_t *out_iv_len) const;
  613. private:
  614. // GetAdditionalData writes the additional data into |out| and returns the
  615. // number of bytes written.
  616. size_t GetAdditionalData(uint8_t out[13], uint8_t type,
  617. uint16_t record_version, const uint8_t seqnum[8],
  618. size_t plaintext_len);
  619. const SSL_CIPHER *cipher_;
  620. ScopedEVP_AEAD_CTX ctx_;
  621. // fixed_nonce_ contains any bytes of the nonce that are fixed for all
  622. // records.
  623. uint8_t fixed_nonce_[12];
  624. uint8_t fixed_nonce_len_ = 0, variable_nonce_len_ = 0;
  625. // version_ is the wire version that should be used with this AEAD.
  626. uint16_t version_;
  627. // is_dtls_ is whether DTLS is being used with this AEAD.
  628. bool is_dtls_;
  629. // variable_nonce_included_in_record_ is true if the variable nonce
  630. // for a record is included as a prefix before the ciphertext.
  631. bool variable_nonce_included_in_record_ : 1;
  632. // random_variable_nonce_ is true if the variable nonce is
  633. // randomly generated, rather than derived from the sequence
  634. // number.
  635. bool random_variable_nonce_ : 1;
  636. // omit_length_in_ad_ is true if the length should be omitted in the
  637. // AEAD's ad parameter.
  638. bool omit_length_in_ad_ : 1;
  639. // omit_version_in_ad_ is true if the version should be omitted
  640. // in the AEAD's ad parameter.
  641. bool omit_version_in_ad_ : 1;
  642. // omit_ad_ is true if the AEAD's ad parameter should be omitted.
  643. bool omit_ad_ : 1;
  644. // xor_fixed_nonce_ is true if the fixed nonce should be XOR'd into the
  645. // variable nonce rather than prepended.
  646. bool xor_fixed_nonce_ : 1;
  647. };
  648. // DTLS replay bitmap.
  649. // DTLS1_BITMAP maintains a sliding window of 64 sequence numbers to detect
  650. // replayed packets. It should be initialized by zeroing every field.
  651. struct DTLS1_BITMAP {
  652. // map is a bit mask of the last 64 sequence numbers. Bit
  653. // |1<<i| corresponds to |max_seq_num - i|.
  654. uint64_t map = 0;
  655. // max_seq_num is the largest sequence number seen so far as a 64-bit
  656. // integer.
  657. uint64_t max_seq_num = 0;
  658. };
  659. // Record layer.
  660. // ssl_record_sequence_update increments the sequence number in |seq|. It
  661. // returns one on success and zero on wraparound.
  662. int ssl_record_sequence_update(uint8_t *seq, size_t seq_len);
  663. // ssl_record_prefix_len returns the length of the prefix before the ciphertext
  664. // of a record for |ssl|.
  665. //
  666. // TODO(davidben): Expose this as part of public API once the high-level
  667. // buffer-free APIs are available.
  668. size_t ssl_record_prefix_len(const SSL *ssl);
  669. enum ssl_open_record_t {
  670. ssl_open_record_success,
  671. ssl_open_record_discard,
  672. ssl_open_record_partial,
  673. ssl_open_record_close_notify,
  674. ssl_open_record_error,
  675. };
  676. // tls_open_record decrypts a record from |in| in-place.
  677. //
  678. // If the input did not contain a complete record, it returns
  679. // |ssl_open_record_partial|. It sets |*out_consumed| to the total number of
  680. // bytes necessary. It is guaranteed that a successful call to |tls_open_record|
  681. // will consume at least that many bytes.
  682. //
  683. // Otherwise, it sets |*out_consumed| to the number of bytes of input
  684. // consumed. Note that input may be consumed on all return codes if a record was
  685. // decrypted.
  686. //
  687. // On success, it returns |ssl_open_record_success|. It sets |*out_type| to the
  688. // record type and |*out| to the record body in |in|. Note that |*out| may be
  689. // empty.
  690. //
  691. // If a record was successfully processed but should be discarded, it returns
  692. // |ssl_open_record_discard|.
  693. //
  694. // If a record was successfully processed but is a close_notify, it returns
  695. // |ssl_open_record_close_notify|.
  696. //
  697. // On failure or fatal alert, it returns |ssl_open_record_error| and sets
  698. // |*out_alert| to an alert to emit, or zero if no alert should be emitted.
  699. enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type,
  700. Span<uint8_t> *out, size_t *out_consumed,
  701. uint8_t *out_alert, Span<uint8_t> in);
  702. // dtls_open_record implements |tls_open_record| for DTLS. It only returns
  703. // |ssl_open_record_partial| if |in| was empty and sets |*out_consumed| to
  704. // zero. The caller should read one packet and try again.
  705. enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type,
  706. Span<uint8_t> *out,
  707. size_t *out_consumed,
  708. uint8_t *out_alert, Span<uint8_t> in);
  709. // ssl_seal_align_prefix_len returns the length of the prefix before the start
  710. // of the bulk of the ciphertext when sealing a record with |ssl|. Callers may
  711. // use this to align buffers.
  712. //
  713. // Note when TLS 1.0 CBC record-splitting is enabled, this includes the one byte
  714. // record and is the offset into second record's ciphertext. Thus sealing a
  715. // small record may result in a smaller output than this value.
  716. //
  717. // TODO(davidben): Is this alignment valuable? Record-splitting makes this a
  718. // mess.
  719. size_t ssl_seal_align_prefix_len(const SSL *ssl);
  720. // tls_seal_record seals a new record of type |type| and body |in| and writes it
  721. // to |out|. At most |max_out| bytes will be written. It returns one on success
  722. // and zero on error. If enabled, |tls_seal_record| implements TLS 1.0 CBC 1/n-1
  723. // record splitting and may write two records concatenated.
  724. //
  725. // For a large record, the bulk of the ciphertext will begin
  726. // |ssl_seal_align_prefix_len| bytes into out. Aligning |out| appropriately may
  727. // improve performance. It writes at most |in_len| + |SSL_max_seal_overhead|
  728. // bytes to |out|.
  729. //
  730. // |in| and |out| may not alias.
  731. int tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  732. uint8_t type, const uint8_t *in, size_t in_len);
  733. enum dtls1_use_epoch_t {
  734. dtls1_use_previous_epoch,
  735. dtls1_use_current_epoch,
  736. };
  737. // dtls_max_seal_overhead returns the maximum overhead, in bytes, of sealing a
  738. // record.
  739. size_t dtls_max_seal_overhead(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
  740. // dtls_seal_prefix_len returns the number of bytes of prefix to reserve in
  741. // front of the plaintext when sealing a record in-place.
  742. size_t dtls_seal_prefix_len(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
  743. // dtls_seal_record implements |tls_seal_record| for DTLS. |use_epoch| selects
  744. // which epoch's cipher state to use. Unlike |tls_seal_record|, |in| and |out|
  745. // may alias but, if they do, |in| must be exactly |dtls_seal_prefix_len| bytes
  746. // ahead of |out|.
  747. int dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  748. uint8_t type, const uint8_t *in, size_t in_len,
  749. enum dtls1_use_epoch_t use_epoch);
  750. // ssl_process_alert processes |in| as an alert and updates |ssl|'s shutdown
  751. // state. It returns one of |ssl_open_record_discard|, |ssl_open_record_error|,
  752. // |ssl_open_record_close_notify|, or |ssl_open_record_fatal_alert| as
  753. // appropriate.
  754. enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
  755. Span<const uint8_t> in);
  756. // Private key operations.
  757. // ssl_has_private_key returns one if |ssl| has a private key
  758. // configured and zero otherwise.
  759. int ssl_has_private_key(const SSL *ssl);
  760. // ssl_private_key_* perform the corresponding operation on
  761. // |SSL_PRIVATE_KEY_METHOD|. If there is a custom private key configured, they
  762. // call the corresponding function or |complete| depending on whether there is a
  763. // pending operation. Otherwise, they implement the operation with
  764. // |EVP_PKEY|.
  765. enum ssl_private_key_result_t ssl_private_key_sign(
  766. SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
  767. uint16_t sigalg, Span<const uint8_t> in);
  768. enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs,
  769. uint8_t *out,
  770. size_t *out_len,
  771. size_t max_out,
  772. Span<const uint8_t> in);
  773. // ssl_private_key_supports_signature_algorithm returns whether |hs|'s private
  774. // key supports |sigalg|.
  775. bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
  776. uint16_t sigalg);
  777. // ssl_public_key_verify verifies that the |signature| is valid for the public
  778. // key |pkey| and input |in|, using the signature algorithm |sigalg|.
  779. bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature,
  780. uint16_t sigalg, EVP_PKEY *pkey,
  781. Span<const uint8_t> in);
  782. // Custom extensions
  783. } // namespace bssl
  784. // |SSL_CUSTOM_EXTENSION| is a structure that contains information about
  785. // custom-extension callbacks. It is defined unnamespaced for compatibility with
  786. // |STACK_OF(SSL_CUSTOM_EXTENSION)|.
  787. typedef struct ssl_custom_extension {
  788. SSL_custom_ext_add_cb add_callback;
  789. void *add_arg;
  790. SSL_custom_ext_free_cb free_callback;
  791. SSL_custom_ext_parse_cb parse_callback;
  792. void *parse_arg;
  793. uint16_t value;
  794. } SSL_CUSTOM_EXTENSION;
  795. DEFINE_STACK_OF(SSL_CUSTOM_EXTENSION)
  796. namespace bssl {
  797. void SSL_CUSTOM_EXTENSION_free(SSL_CUSTOM_EXTENSION *custom_extension);
  798. int custom_ext_add_clienthello(SSL_HANDSHAKE *hs, CBB *extensions);
  799. int custom_ext_parse_serverhello(SSL_HANDSHAKE *hs, int *out_alert,
  800. uint16_t value, const CBS *extension);
  801. int custom_ext_parse_clienthello(SSL_HANDSHAKE *hs, int *out_alert,
  802. uint16_t value, const CBS *extension);
  803. int custom_ext_add_serverhello(SSL_HANDSHAKE *hs, CBB *extensions);
  804. // Key shares.
  805. // SSLKeyShare abstracts over Diffie-Hellman-like key exchanges.
  806. class SSLKeyShare {
  807. public:
  808. virtual ~SSLKeyShare() {}
  809. static constexpr bool kAllowUniquePtr = true;
  810. HAS_VIRTUAL_DESTRUCTOR
  811. // Create returns a SSLKeyShare instance for use with group |group_id| or
  812. // nullptr on error.
  813. static UniquePtr<SSLKeyShare> Create(uint16_t group_id);
  814. // GroupID returns the group ID.
  815. virtual uint16_t GroupID() const PURE_VIRTUAL;
  816. // Offer generates a keypair and writes the public value to
  817. // |out_public_key|. It returns true on success and false on error.
  818. virtual bool Offer(CBB *out_public_key) PURE_VIRTUAL;
  819. // Accept performs a key exchange against the |peer_key| generated by |offer|.
  820. // On success, it returns true, writes the public value to |out_public_key|,
  821. // and sets |*out_secret| the shared secret. On failure, it returns false and
  822. // sets |*out_alert| to an alert to send to the peer.
  823. //
  824. // The default implementation calls |Offer| and then |Finish|, assuming a key
  825. // exchange protocol where the peers are symmetric.
  826. virtual bool Accept(CBB *out_public_key, Array<uint8_t> *out_secret,
  827. uint8_t *out_alert, Span<const uint8_t> peer_key);
  828. // Finish performs a key exchange against the |peer_key| generated by
  829. // |Accept|. On success, it returns true and sets |*out_secret| to the shared
  830. // secret. On failure, it returns zero and sets |*out_alert| to an alert to
  831. // send to the peer.
  832. virtual bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
  833. Span<const uint8_t> peer_key) PURE_VIRTUAL;
  834. };
  835. // ssl_nid_to_group_id looks up the group corresponding to |nid|. On success, it
  836. // sets |*out_group_id| to the group ID and returns one. Otherwise, it returns
  837. // zero.
  838. int ssl_nid_to_group_id(uint16_t *out_group_id, int nid);
  839. // ssl_name_to_group_id looks up the group corresponding to the |name| string
  840. // of length |len|. On success, it sets |*out_group_id| to the group ID and
  841. // returns one. Otherwise, it returns zero.
  842. int ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len);
  843. // Handshake messages.
  844. struct SSLMessage {
  845. bool is_v2_hello;
  846. uint8_t type;
  847. CBS body;
  848. // raw is the entire serialized handshake message, including the TLS or DTLS
  849. // message header.
  850. CBS raw;
  851. };
  852. // SSL_MAX_HANDSHAKE_FLIGHT is the number of messages, including
  853. // ChangeCipherSpec, in the longest handshake flight. Currently this is the
  854. // client's second leg in a full handshake when client certificates, NPN, and
  855. // Channel ID, are all enabled.
  856. #define SSL_MAX_HANDSHAKE_FLIGHT 7
  857. extern const uint8_t kHelloRetryRequest[SSL3_RANDOM_SIZE];
  858. // ssl_max_handshake_message_len returns the maximum number of bytes permitted
  859. // in a handshake message for |ssl|.
  860. size_t ssl_max_handshake_message_len(const SSL *ssl);
  861. // tls_can_accept_handshake_data returns whether |ssl| is able to accept more
  862. // data into handshake buffer.
  863. bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert);
  864. // tls_has_unprocessed_handshake_data returns whether there is buffered
  865. // handshake data that has not been consumed by |get_message|.
  866. bool tls_has_unprocessed_handshake_data(const SSL *ssl);
  867. // dtls_has_unprocessed_handshake_data behaves like
  868. // |tls_has_unprocessed_handshake_data| for DTLS.
  869. bool dtls_has_unprocessed_handshake_data(const SSL *ssl);
  870. struct DTLS_OUTGOING_MESSAGE {
  871. DTLS_OUTGOING_MESSAGE() {}
  872. DTLS_OUTGOING_MESSAGE(const DTLS_OUTGOING_MESSAGE &) = delete;
  873. DTLS_OUTGOING_MESSAGE &operator=(const DTLS_OUTGOING_MESSAGE &) = delete;
  874. ~DTLS_OUTGOING_MESSAGE() { Clear(); }
  875. void Clear();
  876. uint8_t *data = nullptr;
  877. uint32_t len = 0;
  878. uint16_t epoch = 0;
  879. bool is_ccs = false;
  880. };
  881. // dtls_clear_outgoing_messages releases all buffered outgoing messages.
  882. void dtls_clear_outgoing_messages(SSL *ssl);
  883. // Callbacks.
  884. // ssl_do_info_callback calls |ssl|'s info callback, if set.
  885. void ssl_do_info_callback(const SSL *ssl, int type, int value);
  886. // ssl_do_msg_callback calls |ssl|'s message callback, if set.
  887. void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
  888. Span<const uint8_t> in);
  889. // Transport buffers.
  890. class SSLBuffer {
  891. public:
  892. SSLBuffer() {}
  893. ~SSLBuffer() { Clear(); }
  894. SSLBuffer(const SSLBuffer &) = delete;
  895. SSLBuffer &operator=(const SSLBuffer &) = delete;
  896. uint8_t *data() { return buf_ + offset_; }
  897. size_t size() const { return size_; }
  898. bool empty() const { return size_ == 0; }
  899. size_t cap() const { return cap_; }
  900. Span<uint8_t> span() { return MakeSpan(data(), size()); }
  901. Span<uint8_t> remaining() {
  902. return MakeSpan(data() + size(), cap() - size());
  903. }
  904. // Clear releases the buffer.
  905. void Clear();
  906. // EnsureCap ensures the buffer has capacity at least |new_cap|, aligned such
  907. // that data written after |header_len| is aligned to a
  908. // |SSL3_ALIGN_PAYLOAD|-byte boundary. It returns true on success and false
  909. // on error.
  910. bool EnsureCap(size_t header_len, size_t new_cap);
  911. // DidWrite extends the buffer by |len|. The caller must have filled in to
  912. // this point.
  913. void DidWrite(size_t len);
  914. // Consume consumes |len| bytes from the front of the buffer. The memory
  915. // consumed will remain valid until the next call to |DiscardConsumed| or
  916. // |Clear|.
  917. void Consume(size_t len);
  918. // DiscardConsumed discards the consumed bytes from the buffer. If the buffer
  919. // is now empty, it releases memory used by it.
  920. void DiscardConsumed();
  921. private:
  922. // buf_ is the memory allocated for this buffer.
  923. uint8_t *buf_ = nullptr;
  924. // offset_ is the offset into |buf_| which the buffer contents start at.
  925. uint16_t offset_ = 0;
  926. // size_ is the size of the buffer contents from |buf_| + |offset_|.
  927. uint16_t size_ = 0;
  928. // cap_ is how much memory beyond |buf_| + |offset_| is available.
  929. uint16_t cap_ = 0;
  930. };
  931. // ssl_read_buffer_extend_to extends the read buffer to the desired length. For
  932. // TLS, it reads to the end of the buffer until the buffer is |len| bytes
  933. // long. For DTLS, it reads a new packet and ignores |len|. It returns one on
  934. // success, zero on EOF, and a negative number on error.
  935. //
  936. // It is an error to call |ssl_read_buffer_extend_to| in DTLS when the buffer is
  937. // non-empty.
  938. int ssl_read_buffer_extend_to(SSL *ssl, size_t len);
  939. // ssl_handle_open_record handles the result of passing |ssl->s3->read_buffer|
  940. // to a record-processing function. If |ret| is a success or if the caller
  941. // should retry, it returns one and sets |*out_retry|. Otherwise, it returns <=
  942. // 0.
  943. int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret,
  944. size_t consumed, uint8_t alert);
  945. // ssl_write_buffer_flush flushes the write buffer to the transport. It returns
  946. // one on success and <= 0 on error. For DTLS, whether or not the write
  947. // succeeds, the write buffer will be cleared.
  948. int ssl_write_buffer_flush(SSL *ssl);
  949. // Certificate functions.
  950. // ssl_has_certificate returns one if a certificate and private key are
  951. // configured and zero otherwise.
  952. int ssl_has_certificate(const SSL *ssl);
  953. // ssl_parse_cert_chain parses a certificate list from |cbs| in the format used
  954. // by a TLS Certificate message. On success, it advances |cbs| and returns
  955. // true. Otherwise, it returns false and sets |*out_alert| to an alert to send
  956. // to the peer.
  957. //
  958. // If the list is non-empty then |*out_chain| and |*out_pubkey| will be set to
  959. // the certificate chain and the leaf certificate's public key
  960. // respectively. Otherwise, both will be set to nullptr.
  961. //
  962. // If the list is non-empty and |out_leaf_sha256| is non-NULL, it writes the
  963. // SHA-256 hash of the leaf to |out_leaf_sha256|.
  964. bool ssl_parse_cert_chain(uint8_t *out_alert,
  965. UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain,
  966. UniquePtr<EVP_PKEY> *out_pubkey,
  967. uint8_t *out_leaf_sha256, CBS *cbs,
  968. CRYPTO_BUFFER_POOL *pool);
  969. // ssl_add_cert_chain adds |ssl|'s certificate chain to |cbb| in the format used
  970. // by a TLS Certificate message. If there is no certificate chain, it emits an
  971. // empty certificate list. It returns one on success and zero on error.
  972. int ssl_add_cert_chain(SSL *ssl, CBB *cbb);
  973. // ssl_cert_check_digital_signature_key_usage parses the DER-encoded, X.509
  974. // certificate in |in| and returns one if doesn't specify a key usage or, if it
  975. // does, if it includes digitalSignature. Otherwise it pushes to the error
  976. // queue and returns zero.
  977. int ssl_cert_check_digital_signature_key_usage(const CBS *in);
  978. // ssl_cert_parse_pubkey extracts the public key from the DER-encoded, X.509
  979. // certificate in |in|. It returns an allocated |EVP_PKEY| or else returns
  980. // nullptr and pushes to the error queue.
  981. UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in);
  982. // ssl_parse_client_CA_list parses a CA list from |cbs| in the format used by a
  983. // TLS CertificateRequest message. On success, it returns a newly-allocated
  984. // |CRYPTO_BUFFER| list and advances |cbs|. Otherwise, it returns nullptr and
  985. // sets |*out_alert| to an alert to send to the peer.
  986. UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl,
  987. uint8_t *out_alert,
  988. CBS *cbs);
  989. // ssl_has_client_CAs returns there are configured CAs.
  990. bool ssl_has_client_CAs(SSL *ssl);
  991. // ssl_add_client_CA_list adds the configured CA list to |cbb| in the format
  992. // used by a TLS CertificateRequest message. It returns one on success and zero
  993. // on error.
  994. int ssl_add_client_CA_list(SSL *ssl, CBB *cbb);
  995. // ssl_check_leaf_certificate returns one if |pkey| and |leaf| are suitable as
  996. // a server's leaf certificate for |hs|. Otherwise, it returns zero and pushes
  997. // an error on the error queue.
  998. int ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
  999. const CRYPTO_BUFFER *leaf);
  1000. // ssl_on_certificate_selected is called once the certificate has been selected.
  1001. // It finalizes the certificate and initializes |hs->local_pubkey|. It returns
  1002. // one on success and zero on error.
  1003. int ssl_on_certificate_selected(SSL_HANDSHAKE *hs);
  1004. // TLS 1.3 key derivation.
  1005. // tls13_init_key_schedule initializes the handshake hash and key derivation
  1006. // state, and incorporates the PSK. The cipher suite and PRF hash must have been
  1007. // selected at this point. It returns one on success and zero on error.
  1008. int tls13_init_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk,
  1009. size_t psk_len);
  1010. // tls13_init_early_key_schedule initializes the handshake hash and key
  1011. // derivation state from the resumption secret and incorporates the PSK to
  1012. // derive the early secrets. It returns one on success and zero on error.
  1013. int tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk,
  1014. size_t psk_len);
  1015. // tls13_advance_key_schedule incorporates |in| into the key schedule with
  1016. // HKDF-Extract. It returns one on success and zero on error.
  1017. int tls13_advance_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *in,
  1018. size_t len);
  1019. // tls13_set_traffic_key sets the read or write traffic keys to
  1020. // |traffic_secret|. It returns one on success and zero on error.
  1021. int tls13_set_traffic_key(SSL *ssl, enum evp_aead_direction_t direction,
  1022. const uint8_t *traffic_secret,
  1023. size_t traffic_secret_len);
  1024. // tls13_derive_early_secrets derives the early traffic secret. It returns one
  1025. // on success and zero on error.
  1026. int tls13_derive_early_secrets(SSL_HANDSHAKE *hs);
  1027. // tls13_derive_handshake_secrets derives the handshake traffic secret. It
  1028. // returns one on success and zero on error.
  1029. int tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs);
  1030. // tls13_rotate_traffic_key derives the next read or write traffic secret. It
  1031. // returns one on success and zero on error.
  1032. int tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction);
  1033. // tls13_derive_application_secrets derives the initial application data traffic
  1034. // and exporter secrets based on the handshake transcripts and |master_secret|.
  1035. // It returns one on success and zero on error.
  1036. int tls13_derive_application_secrets(SSL_HANDSHAKE *hs);
  1037. // tls13_derive_resumption_secret derives the |resumption_secret|.
  1038. int tls13_derive_resumption_secret(SSL_HANDSHAKE *hs);
  1039. // tls13_export_keying_material provides an exporter interface to use the
  1040. // |exporter_secret|.
  1041. int tls13_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
  1042. const char *label, size_t label_len,
  1043. const uint8_t *context, size_t context_len,
  1044. int use_context);
  1045. // tls13_finished_mac calculates the MAC of the handshake transcript to verify
  1046. // the integrity of the Finished message, and stores the result in |out| and
  1047. // length in |out_len|. |is_server| is 1 if this is for the Server Finished and
  1048. // 0 for the Client Finished.
  1049. int tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out,
  1050. size_t *out_len, int is_server);
  1051. // tls13_derive_session_psk calculates the PSK for this session based on the
  1052. // resumption master secret and |nonce|. It returns true on success, and false
  1053. // on failure.
  1054. bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce);
  1055. // tls13_write_psk_binder calculates the PSK binder value and replaces the last
  1056. // bytes of |msg| with the resulting value. It returns 1 on success, and 0 on
  1057. // failure.
  1058. int tls13_write_psk_binder(SSL_HANDSHAKE *hs, uint8_t *msg, size_t len);
  1059. // tls13_verify_psk_binder verifies that the handshake transcript, truncated
  1060. // up to the binders has a valid signature using the value of |session|'s
  1061. // resumption secret. It returns 1 on success, and 0 on failure.
  1062. int tls13_verify_psk_binder(SSL_HANDSHAKE *hs, SSL_SESSION *session,
  1063. const SSLMessage &msg, CBS *binders);
  1064. // Handshake functions.
  1065. enum ssl_hs_wait_t {
  1066. ssl_hs_error,
  1067. ssl_hs_ok,
  1068. ssl_hs_read_server_hello,
  1069. ssl_hs_read_message,
  1070. ssl_hs_flush,
  1071. ssl_hs_certificate_selection_pending,
  1072. ssl_hs_x509_lookup,
  1073. ssl_hs_channel_id_lookup,
  1074. ssl_hs_private_key_operation,
  1075. ssl_hs_pending_session,
  1076. ssl_hs_pending_ticket,
  1077. ssl_hs_early_return,
  1078. ssl_hs_early_data_rejected,
  1079. ssl_hs_read_end_of_early_data,
  1080. ssl_hs_read_change_cipher_spec,
  1081. ssl_hs_certificate_verify,
  1082. };
  1083. struct SSL_HANDSHAKE {
  1084. explicit SSL_HANDSHAKE(SSL *ssl);
  1085. ~SSL_HANDSHAKE();
  1086. static constexpr bool kAllowUniquePtr = true;
  1087. // ssl is a non-owning pointer to the parent |SSL| object.
  1088. SSL *ssl;
  1089. // wait contains the operation the handshake is currently blocking on or
  1090. // |ssl_hs_ok| if none.
  1091. enum ssl_hs_wait_t wait = ssl_hs_ok;
  1092. // state is the internal state for the TLS 1.2 and below handshake. Its
  1093. // values depend on |do_handshake| but the starting state is always zero.
  1094. int state = 0;
  1095. // tls13_state is the internal state for the TLS 1.3 handshake. Its values
  1096. // depend on |do_handshake| but the starting state is always zero.
  1097. int tls13_state = 0;
  1098. // min_version is the minimum accepted protocol version, taking account both
  1099. // |SSL_OP_NO_*| and |SSL_CTX_set_min_proto_version| APIs.
  1100. uint16_t min_version = 0;
  1101. // max_version is the maximum accepted protocol version, taking account both
  1102. // |SSL_OP_NO_*| and |SSL_CTX_set_max_proto_version| APIs.
  1103. uint16_t max_version = 0;
  1104. // session_id is the session ID in the ClientHello, used for the experimental
  1105. // TLS 1.3 variant.
  1106. uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0};
  1107. uint8_t session_id_len = 0;
  1108. size_t hash_len = 0;
  1109. uint8_t secret[EVP_MAX_MD_SIZE] = {0};
  1110. uint8_t early_traffic_secret[EVP_MAX_MD_SIZE] = {0};
  1111. uint8_t client_handshake_secret[EVP_MAX_MD_SIZE] = {0};
  1112. uint8_t server_handshake_secret[EVP_MAX_MD_SIZE] = {0};
  1113. uint8_t client_traffic_secret_0[EVP_MAX_MD_SIZE] = {0};
  1114. uint8_t server_traffic_secret_0[EVP_MAX_MD_SIZE] = {0};
  1115. uint8_t expected_client_finished[EVP_MAX_MD_SIZE] = {0};
  1116. union {
  1117. // sent is a bitset where the bits correspond to elements of kExtensions
  1118. // in t1_lib.c. Each bit is set if that extension was sent in a
  1119. // ClientHello. It's not used by servers.
  1120. uint32_t sent = 0;
  1121. // received is a bitset, like |sent|, but is used by servers to record
  1122. // which extensions were received from a client.
  1123. uint32_t received;
  1124. } extensions;
  1125. union {
  1126. // sent is a bitset where the bits correspond to elements of
  1127. // |client_custom_extensions| in the |SSL_CTX|. Each bit is set if that
  1128. // extension was sent in a ClientHello. It's not used by servers.
  1129. uint16_t sent = 0;
  1130. // received is a bitset, like |sent|, but is used by servers to record
  1131. // which custom extensions were received from a client. The bits here
  1132. // correspond to |server_custom_extensions|.
  1133. uint16_t received;
  1134. } custom_extensions;
  1135. // retry_group is the group ID selected by the server in HelloRetryRequest in
  1136. // TLS 1.3.
  1137. uint16_t retry_group = 0;
  1138. // error, if |wait| is |ssl_hs_error|, is the error the handshake failed on.
  1139. UniquePtr<ERR_SAVE_STATE> error;
  1140. // key_share is the current key exchange instance.
  1141. UniquePtr<SSLKeyShare> key_share;
  1142. // transcript is the current handshake transcript.
  1143. SSLTranscript transcript;
  1144. // cookie is the value of the cookie received from the server, if any.
  1145. Array<uint8_t> cookie;
  1146. // key_share_bytes is the value of the previously sent KeyShare extension by
  1147. // the client in TLS 1.3.
  1148. Array<uint8_t> key_share_bytes;
  1149. // ecdh_public_key, for servers, is the key share to be sent to the client in
  1150. // TLS 1.3.
  1151. Array<uint8_t> ecdh_public_key;
  1152. // peer_sigalgs are the signature algorithms that the peer supports. These are
  1153. // taken from the contents of the signature algorithms extension for a server
  1154. // or from the CertificateRequest for a client.
  1155. Array<uint16_t> peer_sigalgs;
  1156. // peer_supported_group_list contains the supported group IDs advertised by
  1157. // the peer. This is only set on the server's end. The server does not
  1158. // advertise this extension to the client.
  1159. Array<uint16_t> peer_supported_group_list;
  1160. // peer_key is the peer's ECDH key for a TLS 1.2 client.
  1161. Array<uint8_t> peer_key;
  1162. // server_params, in a TLS 1.2 server, stores the ServerKeyExchange
  1163. // parameters. It has client and server randoms prepended for signing
  1164. // convenience.
  1165. Array<uint8_t> server_params;
  1166. // peer_psk_identity_hint, on the client, is the psk_identity_hint sent by the
  1167. // server when using a TLS 1.2 PSK key exchange.
  1168. UniquePtr<char> peer_psk_identity_hint;
  1169. // ca_names, on the client, contains the list of CAs received in a
  1170. // CertificateRequest message.
  1171. UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names;
  1172. // cached_x509_ca_names contains a cache of parsed versions of the elements of
  1173. // |ca_names|. This pointer is left non-owning so only
  1174. // |ssl_crypto_x509_method| needs to link against crypto/x509.
  1175. STACK_OF(X509_NAME) *cached_x509_ca_names = nullptr;
  1176. // certificate_types, on the client, contains the set of certificate types
  1177. // received in a CertificateRequest message.
  1178. Array<uint8_t> certificate_types;
  1179. // local_pubkey is the public key we are authenticating as.
  1180. UniquePtr<EVP_PKEY> local_pubkey;
  1181. // peer_pubkey is the public key parsed from the peer's leaf certificate.
  1182. UniquePtr<EVP_PKEY> peer_pubkey;
  1183. // new_session is the new mutable session being established by the current
  1184. // handshake. It should not be cached.
  1185. UniquePtr<SSL_SESSION> new_session;
  1186. // early_session is the session corresponding to the current 0-RTT state on
  1187. // the client if |in_early_data| is true.
  1188. UniquePtr<SSL_SESSION> early_session;
  1189. // new_cipher is the cipher being negotiated in this handshake.
  1190. const SSL_CIPHER *new_cipher = nullptr;
  1191. // key_block is the record-layer key block for TLS 1.2 and earlier.
  1192. Array<uint8_t> key_block;
  1193. // scts_requested is true if the SCT extension is in the ClientHello.
  1194. bool scts_requested:1;
  1195. // needs_psk_binder is true if the ClientHello has a placeholder PSK binder to
  1196. // be filled in.
  1197. bool needs_psk_binder:1;
  1198. bool received_hello_retry_request:1;
  1199. bool sent_hello_retry_request:1;
  1200. bool received_custom_extension:1;
  1201. // handshake_finalized is true once the handshake has completed, at which
  1202. // point accessors should use the established state.
  1203. bool handshake_finalized:1;
  1204. // accept_psk_mode stores whether the client's PSK mode is compatible with our
  1205. // preferences.
  1206. bool accept_psk_mode:1;
  1207. // cert_request is true if a client certificate was requested.
  1208. bool cert_request:1;
  1209. // certificate_status_expected is true if OCSP stapling was negotiated and the
  1210. // server is expected to send a CertificateStatus message. (This is used on
  1211. // both the client and server sides.)
  1212. bool certificate_status_expected:1;
  1213. // ocsp_stapling_requested is true if a client requested OCSP stapling.
  1214. bool ocsp_stapling_requested:1;
  1215. // should_ack_sni is used by a server and indicates that the SNI extension
  1216. // should be echoed in the ServerHello.
  1217. bool should_ack_sni:1;
  1218. // in_false_start is true if there is a pending client handshake in False
  1219. // Start. The client may write data at this point.
  1220. bool in_false_start:1;
  1221. // in_early_data is true if there is a pending handshake that has progressed
  1222. // enough to send and receive early data.
  1223. bool in_early_data:1;
  1224. // early_data_offered is true if the client sent the early_data extension.
  1225. bool early_data_offered:1;
  1226. // can_early_read is true if application data may be read at this point in the
  1227. // handshake.
  1228. bool can_early_read:1;
  1229. // can_early_write is true if application data may be written at this point in
  1230. // the handshake.
  1231. bool can_early_write:1;
  1232. // next_proto_neg_seen is one of NPN was negotiated.
  1233. bool next_proto_neg_seen:1;
  1234. // ticket_expected is true if a TLS 1.2 NewSessionTicket message is to be sent
  1235. // or received.
  1236. bool ticket_expected:1;
  1237. // extended_master_secret is true if the extended master secret extension is
  1238. // negotiated in this handshake.
  1239. bool extended_master_secret:1;
  1240. // pending_private_key_op is true if there is a pending private key operation
  1241. // in progress.
  1242. bool pending_private_key_op:1;
  1243. // client_version is the value sent or received in the ClientHello version.
  1244. uint16_t client_version = 0;
  1245. // early_data_read is the amount of early data that has been read by the
  1246. // record layer.
  1247. uint16_t early_data_read = 0;
  1248. // early_data_written is the amount of early data that has been written by the
  1249. // record layer.
  1250. uint16_t early_data_written = 0;
  1251. };
  1252. UniquePtr<SSL_HANDSHAKE> ssl_handshake_new(SSL *ssl);
  1253. // ssl_check_message_type checks if |msg| has type |type|. If so it returns
  1254. // one. Otherwise, it sends an alert and returns zero.
  1255. bool ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type);
  1256. // ssl_run_handshake runs the TLS handshake. It returns one on success and <= 0
  1257. // on error. It sets |out_early_return| to one if we've completed the handshake
  1258. // early.
  1259. int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return);
  1260. // The following are implementations of |do_handshake| for the client and
  1261. // server.
  1262. enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs);
  1263. enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs);
  1264. enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs);
  1265. enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs);
  1266. // The following functions return human-readable representations of the TLS
  1267. // handshake states for debugging.
  1268. const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs);
  1269. const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs);
  1270. const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs);
  1271. const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs);
  1272. // tls13_post_handshake processes a post-handshake message. It returns one on
  1273. // success and zero on failure.
  1274. int tls13_post_handshake(SSL *ssl, const SSLMessage &msg);
  1275. int tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg,
  1276. int allow_anonymous);
  1277. int tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg);
  1278. // tls13_process_finished processes |msg| as a Finished message from the
  1279. // peer. If |use_saved_value| is one, the verify_data is compared against
  1280. // |hs->expected_client_finished| rather than computed fresh.
  1281. int tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg,
  1282. int use_saved_value);
  1283. int tls13_add_certificate(SSL_HANDSHAKE *hs);
  1284. // tls13_add_certificate_verify adds a TLS 1.3 CertificateVerify message to the
  1285. // handshake. If it returns |ssl_private_key_retry|, it should be called again
  1286. // to retry when the signing operation is completed.
  1287. enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs);
  1288. int tls13_add_finished(SSL_HANDSHAKE *hs);
  1289. int tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg);
  1290. bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
  1291. Array<uint8_t> *out_secret,
  1292. uint8_t *out_alert, CBS *contents);
  1293. bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
  1294. Array<uint8_t> *out_secret,
  1295. uint8_t *out_alert, CBS *contents);
  1296. bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
  1297. bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
  1298. uint8_t *out_alert,
  1299. CBS *contents);
  1300. bool ssl_ext_pre_shared_key_parse_clienthello(
  1301. SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
  1302. uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents);
  1303. bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
  1304. // ssl_is_sct_list_valid does a shallow parse of the SCT list in |contents| and
  1305. // returns one iff it's valid.
  1306. int ssl_is_sct_list_valid(const CBS *contents);
  1307. int ssl_write_client_hello(SSL_HANDSHAKE *hs);
  1308. enum ssl_cert_verify_context_t {
  1309. ssl_cert_verify_server,
  1310. ssl_cert_verify_client,
  1311. ssl_cert_verify_channel_id,
  1312. };
  1313. // tls13_get_cert_verify_signature_input generates the message to be signed for
  1314. // TLS 1.3's CertificateVerify message. |cert_verify_context| determines the
  1315. // type of signature. It sets |*out| to a newly allocated buffer containing the
  1316. // result. This function returns true on success and false on failure.
  1317. bool tls13_get_cert_verify_signature_input(
  1318. SSL_HANDSHAKE *hs, Array<uint8_t> *out,
  1319. enum ssl_cert_verify_context_t cert_verify_context);
  1320. // ssl_is_alpn_protocol_allowed returns whether |protocol| is a valid server
  1321. // selection for |ssl|'s client preferences.
  1322. bool ssl_is_alpn_protocol_allowed(const SSL *ssl, Span<const uint8_t> protocol);
  1323. // ssl_negotiate_alpn negotiates the ALPN extension, if applicable. It returns
  1324. // true on successful negotiation or if nothing was negotiated. It returns false
  1325. // and sets |*out_alert| to an alert on error.
  1326. bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1327. const SSL_CLIENT_HELLO *client_hello);
  1328. struct SSL_EXTENSION_TYPE {
  1329. uint16_t type;
  1330. bool *out_present;
  1331. CBS *out_data;
  1332. };
  1333. // ssl_parse_extensions parses a TLS extensions block out of |cbs| and advances
  1334. // it. It writes the parsed extensions to pointers denoted by |ext_types|. On
  1335. // success, it fills in the |out_present| and |out_data| fields and returns one.
  1336. // Otherwise, it sets |*out_alert| to an alert to send and returns zero. Unknown
  1337. // extensions are rejected unless |ignore_unknown| is 1.
  1338. int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert,
  1339. const SSL_EXTENSION_TYPE *ext_types,
  1340. size_t num_ext_types, int ignore_unknown);
  1341. // ssl_verify_peer_cert verifies the peer certificate for |hs|.
  1342. enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs);
  1343. enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs);
  1344. bool ssl_send_finished(SSL_HANDSHAKE *hs);
  1345. bool ssl_output_cert_chain(SSL *ssl);
  1346. // SSLKEYLOGFILE functions.
  1347. // ssl_log_secret logs |secret| with label |label|, if logging is enabled for
  1348. // |ssl|. It returns one on success and zero on failure.
  1349. int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
  1350. size_t secret_len);
  1351. // ClientHello functions.
  1352. int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out,
  1353. const SSLMessage &msg);
  1354. int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
  1355. CBS *out, uint16_t extension_type);
  1356. int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
  1357. uint16_t id);
  1358. // GREASE.
  1359. enum ssl_grease_index_t {
  1360. ssl_grease_cipher = 0,
  1361. ssl_grease_group,
  1362. ssl_grease_extension1,
  1363. ssl_grease_extension2,
  1364. ssl_grease_version,
  1365. ssl_grease_ticket_extension,
  1366. };
  1367. // ssl_get_grease_value returns a GREASE value for |ssl|. For a given
  1368. // connection, the values for each index will be deterministic. This allows the
  1369. // same ClientHello be sent twice for a HelloRetryRequest or the same group be
  1370. // advertised in both supported_groups and key_shares.
  1371. uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index);
  1372. // Signature algorithms.
  1373. // tls1_parse_peer_sigalgs parses |sigalgs| as the list of peer signature
  1374. // algorithms and saves them on |hs|. It returns one on success and zero on
  1375. // error.
  1376. int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *sigalgs);
  1377. // tls1_get_legacy_signature_algorithm sets |*out| to the signature algorithm
  1378. // that should be used with |pkey| in TLS 1.1 and earlier. It returns one on
  1379. // success and zero if |pkey| may not be used at those versions.
  1380. int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey);
  1381. // tls1_choose_signature_algorithm sets |*out| to a signature algorithm for use
  1382. // with |hs|'s private key based on the peer's preferences and the algorithms
  1383. // supported. It returns one on success and zero on error.
  1384. int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out);
  1385. // tls12_add_verify_sigalgs adds the signature algorithms acceptable for the
  1386. // peer signature to |out|. It returns one on success and zero on error.
  1387. int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out);
  1388. // tls12_check_peer_sigalg checks if |sigalg| is acceptable for the peer
  1389. // signature. It returns one on success and zero on error, setting |*out_alert|
  1390. // to an alert to send.
  1391. int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg);
  1392. // Underdocumented functions.
  1393. //
  1394. // Functions below here haven't been touched up and may be underdocumented.
  1395. #define TLSEXT_CHANNEL_ID_SIZE 128
  1396. // From RFC4492, used in encoding the curve type in ECParameters
  1397. #define NAMED_CURVE_TYPE 3
  1398. struct CERT {
  1399. EVP_PKEY *privatekey;
  1400. // chain contains the certificate chain, with the leaf at the beginning. The
  1401. // first element of |chain| may be NULL to indicate that the leaf certificate
  1402. // has not yet been set.
  1403. // If |chain| != NULL -> len(chain) >= 1
  1404. // If |chain[0]| == NULL -> len(chain) >= 2.
  1405. // |chain[1..]| != NULL
  1406. STACK_OF(CRYPTO_BUFFER) *chain;
  1407. // x509_chain may contain a parsed copy of |chain[1..]|. This is only used as
  1408. // a cache in order to implement “get0” functions that return a non-owning
  1409. // pointer to the certificate chain.
  1410. STACK_OF(X509) *x509_chain;
  1411. // x509_leaf may contain a parsed copy of the first element of |chain|. This
  1412. // is only used as a cache in order to implement “get0” functions that return
  1413. // a non-owning pointer to the certificate chain.
  1414. X509 *x509_leaf;
  1415. // x509_stash contains the last |X509| object append to the chain. This is a
  1416. // workaround for some third-party code that continue to use an |X509| object
  1417. // even after passing ownership with an “add0” function.
  1418. X509 *x509_stash;
  1419. // key_method, if non-NULL, is a set of callbacks to call for private key
  1420. // operations.
  1421. const SSL_PRIVATE_KEY_METHOD *key_method;
  1422. // x509_method contains pointers to functions that might deal with |X509|
  1423. // compatibility, or might be a no-op, depending on the application.
  1424. const SSL_X509_METHOD *x509_method;
  1425. // sigalgs, if non-NULL, is the set of signature algorithms supported by
  1426. // |privatekey| in decreasing order of preference.
  1427. uint16_t *sigalgs;
  1428. size_t num_sigalgs;
  1429. // Certificate setup callback: if set is called whenever a
  1430. // certificate may be required (client or server). the callback
  1431. // can then examine any appropriate parameters and setup any
  1432. // certificates required. This allows advanced applications
  1433. // to select certificates on the fly: for example based on
  1434. // supported signature algorithms or curves.
  1435. int (*cert_cb)(SSL *ssl, void *arg);
  1436. void *cert_cb_arg;
  1437. // Optional X509_STORE for certificate validation. If NULL the parent SSL_CTX
  1438. // store is used instead.
  1439. X509_STORE *verify_store;
  1440. // Signed certificate timestamp list to be sent to the client, if requested
  1441. CRYPTO_BUFFER *signed_cert_timestamp_list;
  1442. // OCSP response to be sent to the client, if requested.
  1443. CRYPTO_BUFFER *ocsp_response;
  1444. // sid_ctx partitions the session space within a shared session cache or
  1445. // ticket key. Only sessions with a matching value will be accepted.
  1446. uint8_t sid_ctx_length;
  1447. uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH];
  1448. // If enable_early_data is true, early data can be sent and accepted.
  1449. bool enable_early_data:1;
  1450. };
  1451. // |SSL_PROTOCOL_METHOD| abstracts between TLS and DTLS.
  1452. struct SSL_PROTOCOL_METHOD {
  1453. bool is_dtls;
  1454. bool (*ssl_new)(SSL *ssl);
  1455. void (*ssl_free)(SSL *ssl);
  1456. // get_message sets |*out| to the current handshake message and returns true
  1457. // if one has been received. It returns false if more input is needed.
  1458. bool (*get_message)(SSL *ssl, SSLMessage *out);
  1459. // next_message is called to release the current handshake message.
  1460. void (*next_message)(SSL *ssl);
  1461. // Use the |ssl_open_handshake| wrapper.
  1462. ssl_open_record_t (*open_handshake)(SSL *ssl, size_t *out_consumed,
  1463. uint8_t *out_alert, Span<uint8_t> in);
  1464. // Use the |ssl_open_change_cipher_spec| wrapper.
  1465. ssl_open_record_t (*open_change_cipher_spec)(SSL *ssl, size_t *out_consumed,
  1466. uint8_t *out_alert,
  1467. Span<uint8_t> in);
  1468. // Use the |ssl_open_app_data| wrapper.
  1469. ssl_open_record_t (*open_app_data)(SSL *ssl, Span<uint8_t> *out,
  1470. size_t *out_consumed, uint8_t *out_alert,
  1471. Span<uint8_t> in);
  1472. int (*write_app_data)(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
  1473. int len);
  1474. int (*dispatch_alert)(SSL *ssl);
  1475. // init_message begins a new handshake message of type |type|. |cbb| is the
  1476. // root CBB to be passed into |finish_message|. |*body| is set to a child CBB
  1477. // the caller should write to. It returns true on success and false on error.
  1478. bool (*init_message)(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
  1479. // finish_message finishes a handshake message. It sets |*out_msg| to the
  1480. // serialized message. It returns true on success and false on error.
  1481. bool (*finish_message)(SSL *ssl, CBB *cbb, bssl::Array<uint8_t> *out_msg);
  1482. // add_message adds a handshake message to the pending flight. It returns
  1483. // true on success and false on error.
  1484. bool (*add_message)(SSL *ssl, bssl::Array<uint8_t> msg);
  1485. // add_change_cipher_spec adds a ChangeCipherSpec record to the pending
  1486. // flight. It returns true on success and false on error.
  1487. bool (*add_change_cipher_spec)(SSL *ssl);
  1488. // add_alert adds an alert to the pending flight. It returns true on success
  1489. // and false on error.
  1490. bool (*add_alert)(SSL *ssl, uint8_t level, uint8_t desc);
  1491. // flush_flight flushes the pending flight to the transport. It returns one on
  1492. // success and <= 0 on error.
  1493. int (*flush_flight)(SSL *ssl);
  1494. // on_handshake_complete is called when the handshake is complete.
  1495. void (*on_handshake_complete)(SSL *ssl);
  1496. // set_read_state sets |ssl|'s read cipher state to |aead_ctx|. It returns
  1497. // true on success and false if changing the read state is forbidden at this
  1498. // point.
  1499. bool (*set_read_state)(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx);
  1500. // set_write_state sets |ssl|'s write cipher state to |aead_ctx|. It returns
  1501. // true on success and false if changing the write state is forbidden at this
  1502. // point.
  1503. bool (*set_write_state)(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx);
  1504. };
  1505. // The following wrappers call |open_*| but handle |read_shutdown| correctly.
  1506. // ssl_open_handshake processes a record from |in| for reading a handshake
  1507. // message.
  1508. ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed,
  1509. uint8_t *out_alert, Span<uint8_t> in);
  1510. // ssl_open_change_cipher_spec processes a record from |in| for reading a
  1511. // ChangeCipherSpec.
  1512. ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
  1513. uint8_t *out_alert,
  1514. Span<uint8_t> in);
  1515. // ssl_open_app_data processes a record from |in| for reading application data.
  1516. // On success, it returns |ssl_open_record_success| and sets |*out| to the
  1517. // input. If it encounters a post-handshake message, it returns
  1518. // |ssl_open_record_discard|. The caller should then retry, after processing any
  1519. // messages received with |get_message|.
  1520. ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out,
  1521. size_t *out_consumed, uint8_t *out_alert,
  1522. Span<uint8_t> in);
  1523. // ssl_crypto_x509_method provides the |SSL_X509_METHOD| functions using
  1524. // crypto/x509.
  1525. extern const SSL_X509_METHOD ssl_crypto_x509_method;
  1526. // ssl_noop_x509_method provides the |SSL_X509_METHOD| functions that avoid
  1527. // crypto/x509.
  1528. extern const SSL_X509_METHOD ssl_noop_x509_method;
  1529. // ssl_cipher_preference_list_st contains a list of SSL_CIPHERs with
  1530. // equal-preference groups. For TLS clients, the groups are moot because the
  1531. // server picks the cipher and groups cannot be expressed on the wire. However,
  1532. // for servers, the equal-preference groups allow the client's preferences to
  1533. // be partially respected. (This only has an effect with
  1534. // SSL_OP_CIPHER_SERVER_PREFERENCE).
  1535. //
  1536. // The equal-preference groups are expressed by grouping SSL_CIPHERs together.
  1537. // All elements of a group have the same priority: no ordering is expressed
  1538. // within a group.
  1539. //
  1540. // The values in |ciphers| are in one-to-one correspondence with
  1541. // |in_group_flags|. (That is, sk_SSL_CIPHER_num(ciphers) is the number of
  1542. // bytes in |in_group_flags|.) The bytes in |in_group_flags| are either 1, to
  1543. // indicate that the corresponding SSL_CIPHER is not the last element of a
  1544. // group, or 0 to indicate that it is.
  1545. //
  1546. // For example, if |in_group_flags| contains all zeros then that indicates a
  1547. // traditional, fully-ordered preference. Every SSL_CIPHER is the last element
  1548. // of the group (i.e. they are all in a one-element group).
  1549. //
  1550. // For a more complex example, consider:
  1551. // ciphers: A B C D E F
  1552. // in_group_flags: 1 1 0 0 1 0
  1553. //
  1554. // That would express the following, order:
  1555. //
  1556. // A E
  1557. // B -> D -> F
  1558. // C
  1559. struct ssl_cipher_preference_list_st {
  1560. STACK_OF(SSL_CIPHER) *ciphers;
  1561. uint8_t *in_group_flags;
  1562. };
  1563. struct tlsext_ticket_key {
  1564. static constexpr bool kAllowUniquePtr = true;
  1565. uint8_t name[SSL_TICKET_KEY_NAME_LEN];
  1566. uint8_t hmac_key[16];
  1567. uint8_t aes_key[16];
  1568. // next_rotation_tv_sec is the time (in seconds from the epoch) when the
  1569. // current key should be superseded by a new key, or the time when a previous
  1570. // key should be dropped. If zero, then the key should not be automatically
  1571. // rotated.
  1572. uint64_t next_rotation_tv_sec;
  1573. };
  1574. } // namespace bssl
  1575. DECLARE_LHASH_OF(SSL_SESSION)
  1576. namespace bssl {
  1577. // SSLContext backs the public |SSL_CTX| type. Due to compatibility constraints,
  1578. // it is a base class for |ssl_ctx_st|.
  1579. struct SSLContext {
  1580. const SSL_PROTOCOL_METHOD *method;
  1581. const SSL_X509_METHOD *x509_method;
  1582. // lock is used to protect various operations on this object.
  1583. CRYPTO_MUTEX lock;
  1584. // conf_max_version is the maximum acceptable protocol version configured by
  1585. // |SSL_CTX_set_max_proto_version|. Note this version is normalized in DTLS
  1586. // and is further constrainted by |SSL_OP_NO_*|.
  1587. uint16_t conf_max_version;
  1588. // conf_min_version is the minimum acceptable protocol version configured by
  1589. // |SSL_CTX_set_min_proto_version|. Note this version is normalized in DTLS
  1590. // and is further constrainted by |SSL_OP_NO_*|.
  1591. uint16_t conf_min_version;
  1592. // tls13_variant is the variant of TLS 1.3 we are using for this
  1593. // configuration.
  1594. enum tls13_variant_t tls13_variant;
  1595. struct ssl_cipher_preference_list_st *cipher_list;
  1596. X509_STORE *cert_store;
  1597. LHASH_OF(SSL_SESSION) *sessions;
  1598. // Most session-ids that will be cached, default is
  1599. // SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited.
  1600. unsigned long session_cache_size;
  1601. SSL_SESSION *session_cache_head;
  1602. SSL_SESSION *session_cache_tail;
  1603. // handshakes_since_cache_flush is the number of successful handshakes since
  1604. // the last cache flush.
  1605. int handshakes_since_cache_flush;
  1606. // This can have one of 2 values, ored together,
  1607. // SSL_SESS_CACHE_CLIENT,
  1608. // SSL_SESS_CACHE_SERVER,
  1609. // Default is SSL_SESSION_CACHE_SERVER, which means only
  1610. // SSL_accept which cache SSL_SESSIONS.
  1611. int session_cache_mode;
  1612. // session_timeout is the default lifetime for new sessions in TLS 1.2 and
  1613. // earlier, in seconds.
  1614. uint32_t session_timeout;
  1615. // session_psk_dhe_timeout is the default lifetime for new sessions in TLS
  1616. // 1.3, in seconds.
  1617. uint32_t session_psk_dhe_timeout;
  1618. // If this callback is not null, it will be called each time a session id is
  1619. // added to the cache. If this function returns 1, it means that the
  1620. // callback will do a SSL_SESSION_free() when it has finished using it.
  1621. // Otherwise, on 0, it means the callback has finished with it. If
  1622. // remove_session_cb is not null, it will be called when a session-id is
  1623. // removed from the cache. After the call, OpenSSL will SSL_SESSION_free()
  1624. // it.
  1625. int (*new_session_cb)(SSL *ssl, SSL_SESSION *sess);
  1626. void (*remove_session_cb)(SSL_CTX *ctx, SSL_SESSION *sess);
  1627. SSL_SESSION *(*get_session_cb)(SSL *ssl, const uint8_t *data, int len,
  1628. int *copy);
  1629. SSL_SESSION *(*get_session_cb_legacy)(SSL *ssl, uint8_t *data, int len,
  1630. int *copy);
  1631. CRYPTO_refcount_t references;
  1632. // if defined, these override the X509_verify_cert() calls
  1633. int (*app_verify_callback)(X509_STORE_CTX *store_ctx, void *arg);
  1634. void *app_verify_arg;
  1635. enum ssl_verify_result_t (*custom_verify_callback)(SSL *ssl,
  1636. uint8_t *out_alert);
  1637. // Default password callback.
  1638. pem_password_cb *default_passwd_callback;
  1639. // Default password callback user data.
  1640. void *default_passwd_callback_userdata;
  1641. // get client cert callback
  1642. int (*client_cert_cb)(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey);
  1643. // get channel id callback
  1644. void (*channel_id_cb)(SSL *ssl, EVP_PKEY **out_pkey);
  1645. CRYPTO_EX_DATA ex_data;
  1646. // custom_*_extensions stores any callback sets for custom extensions. Note
  1647. // that these pointers will be NULL if the stack would otherwise be empty.
  1648. STACK_OF(SSL_CUSTOM_EXTENSION) *client_custom_extensions;
  1649. STACK_OF(SSL_CUSTOM_EXTENSION) *server_custom_extensions;
  1650. // Default values used when no per-SSL value is defined follow
  1651. void (*info_callback)(const SSL *ssl, int type, int value);
  1652. // what we put in client cert requests
  1653. STACK_OF(CRYPTO_BUFFER) *client_CA;
  1654. // cached_x509_client_CA is a cache of parsed versions of the elements of
  1655. // |client_CA|.
  1656. STACK_OF(X509_NAME) *cached_x509_client_CA;
  1657. // Default values to use in SSL structures follow (these are copied by
  1658. // SSL_new)
  1659. uint32_t options;
  1660. uint32_t mode;
  1661. uint32_t max_cert_list;
  1662. CERT *cert;
  1663. // callback that allows applications to peek at protocol messages
  1664. void (*msg_callback)(int write_p, int version, int content_type,
  1665. const void *buf, size_t len, SSL *ssl, void *arg);
  1666. void *msg_callback_arg;
  1667. int verify_mode;
  1668. int (*default_verify_callback)(
  1669. int ok, X509_STORE_CTX *ctx); // called 'verify_callback' in the SSL
  1670. X509_VERIFY_PARAM *param;
  1671. // select_certificate_cb is called before most ClientHello processing and
  1672. // before the decision whether to resume a session is made. See
  1673. // |ssl_select_cert_result_t| for details of the return values.
  1674. enum ssl_select_cert_result_t (*select_certificate_cb)(
  1675. const SSL_CLIENT_HELLO *);
  1676. // dos_protection_cb is called once the resumption decision for a ClientHello
  1677. // has been made. It returns one to continue the handshake or zero to
  1678. // abort.
  1679. int (*dos_protection_cb) (const SSL_CLIENT_HELLO *);
  1680. // Maximum amount of data to send in one fragment. actual record size can be
  1681. // more than this due to padding and MAC overheads.
  1682. uint16_t max_send_fragment;
  1683. // TLS extensions servername callback
  1684. int (*tlsext_servername_callback)(SSL *, int *, void *);
  1685. void *tlsext_servername_arg;
  1686. // RFC 4507 session ticket keys. |tlsext_ticket_key_current| may be NULL
  1687. // before the first handshake and |tlsext_ticket_key_prev| may be NULL at any
  1688. // time. Automatically generated ticket keys are rotated as needed at
  1689. // handshake time. Hence, all access must be synchronized through |lock|.
  1690. struct tlsext_ticket_key *tlsext_ticket_key_current;
  1691. struct tlsext_ticket_key *tlsext_ticket_key_prev;
  1692. // Callback to support customisation of ticket key setting
  1693. int (*tlsext_ticket_key_cb)(SSL *ssl, uint8_t *name, uint8_t *iv,
  1694. EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc);
  1695. // Server-only: psk_identity_hint is the default identity hint to send in
  1696. // PSK-based key exchanges.
  1697. char *psk_identity_hint;
  1698. unsigned int (*psk_client_callback)(SSL *ssl, const char *hint,
  1699. char *identity,
  1700. unsigned int max_identity_len,
  1701. uint8_t *psk, unsigned int max_psk_len);
  1702. unsigned int (*psk_server_callback)(SSL *ssl, const char *identity,
  1703. uint8_t *psk, unsigned int max_psk_len);
  1704. // Next protocol negotiation information
  1705. // (for experimental NPN extension).
  1706. // For a server, this contains a callback function by which the set of
  1707. // advertised protocols can be provided.
  1708. int (*next_protos_advertised_cb)(SSL *ssl, const uint8_t **out,
  1709. unsigned *out_len, void *arg);
  1710. void *next_protos_advertised_cb_arg;
  1711. // For a client, this contains a callback function that selects the
  1712. // next protocol from the list provided by the server.
  1713. int (*next_proto_select_cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
  1714. const uint8_t *in, unsigned in_len, void *arg);
  1715. void *next_proto_select_cb_arg;
  1716. // ALPN information
  1717. // (we are in the process of transitioning from NPN to ALPN.)
  1718. // For a server, this contains a callback function that allows the
  1719. // server to select the protocol for the connection.
  1720. // out: on successful return, this must point to the raw protocol
  1721. // name (without the length prefix).
  1722. // outlen: on successful return, this contains the length of |*out|.
  1723. // in: points to the client's list of supported protocols in
  1724. // wire-format.
  1725. // inlen: the length of |in|.
  1726. int (*alpn_select_cb)(SSL *ssl, const uint8_t **out, uint8_t *out_len,
  1727. const uint8_t *in, unsigned in_len, void *arg);
  1728. void *alpn_select_cb_arg;
  1729. // For a client, this contains the list of supported protocols in wire
  1730. // format.
  1731. uint8_t *alpn_client_proto_list;
  1732. unsigned alpn_client_proto_list_len;
  1733. // SRTP profiles we are willing to do from RFC 5764
  1734. STACK_OF(SRTP_PROTECTION_PROFILE) *srtp_profiles;
  1735. // Supported group values inherited by SSL structure
  1736. size_t supported_group_list_len;
  1737. uint16_t *supported_group_list;
  1738. // The client's Channel ID private key.
  1739. EVP_PKEY *tlsext_channel_id_private;
  1740. // keylog_callback, if not NULL, is the key logging callback. See
  1741. // |SSL_CTX_set_keylog_callback|.
  1742. void (*keylog_callback)(const SSL *ssl, const char *line);
  1743. // current_time_cb, if not NULL, is the function to use to get the current
  1744. // time. It sets |*out_clock| to the current time. The |ssl| argument is
  1745. // always NULL. See |SSL_CTX_set_current_time_cb|.
  1746. void (*current_time_cb)(const SSL *ssl, struct timeval *out_clock);
  1747. // pool is used for all |CRYPTO_BUFFER|s in case we wish to share certificate
  1748. // memory.
  1749. CRYPTO_BUFFER_POOL *pool;
  1750. // ticket_aead_method contains function pointers for opening and sealing
  1751. // session tickets.
  1752. const SSL_TICKET_AEAD_METHOD *ticket_aead_method;
  1753. // verify_sigalgs, if not empty, is the set of signature algorithms
  1754. // accepted from the peer in decreasing order of preference.
  1755. uint16_t *verify_sigalgs;
  1756. size_t num_verify_sigalgs;
  1757. // retain_only_sha256_of_client_certs is true if we should compute the SHA256
  1758. // hash of the peer's certificate and then discard it to save memory and
  1759. // session space. Only effective on the server side.
  1760. bool retain_only_sha256_of_client_certs:1;
  1761. // quiet_shutdown is true if the connection should not send a close_notify on
  1762. // shutdown.
  1763. bool quiet_shutdown:1;
  1764. // ocsp_stapling_enabled is only used by client connections and indicates
  1765. // whether OCSP stapling will be requested.
  1766. bool ocsp_stapling_enabled:1;
  1767. // If true, a client will request certificate timestamps.
  1768. bool signed_cert_timestamps_enabled:1;
  1769. // tlsext_channel_id_enabled is one if Channel ID is enabled and zero
  1770. // otherwise. For a server, means that we'll accept Channel IDs from clients.
  1771. // For a client, means that we'll advertise support.
  1772. bool tlsext_channel_id_enabled:1;
  1773. // grease_enabled is one if draft-davidben-tls-grease-01 is enabled and zero
  1774. // otherwise.
  1775. bool grease_enabled:1;
  1776. // allow_unknown_alpn_protos is one if the client allows unsolicited ALPN
  1777. // protocols from the peer.
  1778. bool allow_unknown_alpn_protos:1;
  1779. // ed25519_enabled is one if Ed25519 is advertised in the handshake.
  1780. bool ed25519_enabled:1;
  1781. };
  1782. // An ssl_shutdown_t describes the shutdown state of one end of the connection,
  1783. // whether it is alive or has been shutdown via close_notify or fatal alert.
  1784. enum ssl_shutdown_t {
  1785. ssl_shutdown_none = 0,
  1786. ssl_shutdown_close_notify = 1,
  1787. ssl_shutdown_error = 2,
  1788. };
  1789. struct SSL3_STATE {
  1790. static constexpr bool kAllowUniquePtr = true;
  1791. SSL3_STATE();
  1792. ~SSL3_STATE();
  1793. uint8_t read_sequence[8] = {0};
  1794. uint8_t write_sequence[8] = {0};
  1795. uint8_t server_random[SSL3_RANDOM_SIZE] = {0};
  1796. uint8_t client_random[SSL3_RANDOM_SIZE] = {0};
  1797. // read_buffer holds data from the transport to be processed.
  1798. SSLBuffer read_buffer;
  1799. // write_buffer holds data to be written to the transport.
  1800. SSLBuffer write_buffer;
  1801. // pending_app_data is the unconsumed application data. It points into
  1802. // |read_buffer|.
  1803. Span<uint8_t> pending_app_data;
  1804. // partial write - check the numbers match
  1805. unsigned int wnum = 0; // number of bytes sent so far
  1806. int wpend_tot = 0; // number bytes written
  1807. int wpend_type = 0;
  1808. int wpend_ret = 0; // number of bytes submitted
  1809. const uint8_t *wpend_buf = nullptr;
  1810. // read_shutdown is the shutdown state for the read half of the connection.
  1811. enum ssl_shutdown_t read_shutdown = ssl_shutdown_none;
  1812. // write_shutdown is the shutdown state for the write half of the connection.
  1813. enum ssl_shutdown_t write_shutdown = ssl_shutdown_none;
  1814. // read_error, if |read_shutdown| is |ssl_shutdown_error|, is the error for
  1815. // the receive half of the connection.
  1816. UniquePtr<ERR_SAVE_STATE> read_error;
  1817. int alert_dispatch = 0;
  1818. int total_renegotiations = 0;
  1819. // This holds a variable that indicates what we were doing when a 0 or -1 is
  1820. // returned. This is needed for non-blocking IO so we know what request
  1821. // needs re-doing when in SSL_accept or SSL_connect
  1822. int rwstate = SSL_NOTHING;
  1823. // early_data_skipped is the amount of early data that has been skipped by the
  1824. // record layer.
  1825. uint16_t early_data_skipped = 0;
  1826. // empty_record_count is the number of consecutive empty records received.
  1827. uint8_t empty_record_count = 0;
  1828. // warning_alert_count is the number of consecutive warning alerts
  1829. // received.
  1830. uint8_t warning_alert_count = 0;
  1831. // key_update_count is the number of consecutive KeyUpdates received.
  1832. uint8_t key_update_count = 0;
  1833. // skip_early_data instructs the record layer to skip unexpected early data
  1834. // messages when 0RTT is rejected.
  1835. bool skip_early_data:1;
  1836. // have_version is true if the connection's final version is known. Otherwise
  1837. // the version has not been negotiated yet.
  1838. bool have_version:1;
  1839. // v2_hello_done is true if the peer's V2ClientHello, if any, has been handled
  1840. // and future messages should use the record layer.
  1841. bool v2_hello_done:1;
  1842. // is_v2_hello is true if the current handshake message was derived from a
  1843. // V2ClientHello rather than received from the peer directly.
  1844. bool is_v2_hello:1;
  1845. // has_message is true if the current handshake message has been returned
  1846. // at least once by |get_message| and false otherwise.
  1847. bool has_message:1;
  1848. // initial_handshake_complete is true if the initial handshake has
  1849. // completed.
  1850. bool initial_handshake_complete:1;
  1851. // session_reused indicates whether a session was resumed.
  1852. bool session_reused:1;
  1853. bool send_connection_binding:1;
  1854. // In a client, this means that the server supported Channel ID and that a
  1855. // Channel ID was sent. In a server it means that we echoed support for
  1856. // Channel IDs and that tlsext_channel_id will be valid after the
  1857. // handshake.
  1858. bool tlsext_channel_id_valid:1;
  1859. // key_update_pending is true if we have a KeyUpdate acknowledgment
  1860. // outstanding.
  1861. bool key_update_pending:1;
  1862. // wpend_pending is true if we have a pending write outstanding.
  1863. bool wpend_pending:1;
  1864. uint8_t send_alert[2] = {0};
  1865. // hs_buf is the buffer of handshake data to process.
  1866. UniquePtr<BUF_MEM> hs_buf;
  1867. // pending_flight is the pending outgoing flight. This is used to flush each
  1868. // handshake flight in a single write. |write_buffer| must be written out
  1869. // before this data.
  1870. UniquePtr<BUF_MEM> pending_flight;
  1871. // pending_flight_offset is the number of bytes of |pending_flight| which have
  1872. // been successfully written.
  1873. uint32_t pending_flight_offset = 0;
  1874. // aead_read_ctx is the current read cipher state.
  1875. UniquePtr<SSLAEADContext> aead_read_ctx;
  1876. // aead_write_ctx is the current write cipher state.
  1877. UniquePtr<SSLAEADContext> aead_write_ctx;
  1878. // hs is the handshake state for the current handshake or NULL if there isn't
  1879. // one.
  1880. UniquePtr<SSL_HANDSHAKE> hs;
  1881. uint8_t write_traffic_secret[EVP_MAX_MD_SIZE] = {0};
  1882. uint8_t read_traffic_secret[EVP_MAX_MD_SIZE] = {0};
  1883. uint8_t exporter_secret[EVP_MAX_MD_SIZE] = {0};
  1884. uint8_t early_exporter_secret[EVP_MAX_MD_SIZE] = {0};
  1885. uint8_t write_traffic_secret_len = 0;
  1886. uint8_t read_traffic_secret_len = 0;
  1887. uint8_t exporter_secret_len = 0;
  1888. uint8_t early_exporter_secret_len = 0;
  1889. // Connection binding to prevent renegotiation attacks
  1890. uint8_t previous_client_finished[12] = {0};
  1891. uint8_t previous_client_finished_len = 0;
  1892. uint8_t previous_server_finished_len = 0;
  1893. uint8_t previous_server_finished[12] = {0};
  1894. // established_session is the session established by the connection. This
  1895. // session is only filled upon the completion of the handshake and is
  1896. // immutable.
  1897. UniquePtr<SSL_SESSION> established_session;
  1898. // Next protocol negotiation. For the client, this is the protocol that we
  1899. // sent in NextProtocol and is set when handling ServerHello extensions.
  1900. //
  1901. // For a server, this is the client's selected_protocol from NextProtocol and
  1902. // is set when handling the NextProtocol message, before the Finished
  1903. // message.
  1904. Array<uint8_t> next_proto_negotiated;
  1905. // ALPN information
  1906. // (we are in the process of transitioning from NPN to ALPN.)
  1907. // In a server these point to the selected ALPN protocol after the
  1908. // ClientHello has been processed. In a client these contain the protocol
  1909. // that the server selected once the ServerHello has been processed.
  1910. Array<uint8_t> alpn_selected;
  1911. // hostname, on the server, is the value of the SNI extension.
  1912. UniquePtr<char> hostname;
  1913. // For a server:
  1914. // If |tlsext_channel_id_valid| is true, then this contains the
  1915. // verified Channel ID from the client: a P256 point, (x,y), where
  1916. // each are big-endian values.
  1917. uint8_t tlsext_channel_id[64] = {0};
  1918. // ticket_age_skew is the difference, in seconds, between the client-sent
  1919. // ticket age and the server-computed value in TLS 1.3 server connections
  1920. // which resumed a session.
  1921. int32_t ticket_age_skew = 0;
  1922. };
  1923. // lengths of messages
  1924. #define DTLS1_COOKIE_LENGTH 256
  1925. #define DTLS1_RT_HEADER_LENGTH 13
  1926. #define DTLS1_HM_HEADER_LENGTH 12
  1927. #define DTLS1_CCS_HEADER_LENGTH 1
  1928. #define DTLS1_AL_HEADER_LENGTH 2
  1929. struct hm_header_st {
  1930. uint8_t type;
  1931. uint32_t msg_len;
  1932. uint16_t seq;
  1933. uint32_t frag_off;
  1934. uint32_t frag_len;
  1935. };
  1936. // An hm_fragment is an incoming DTLS message, possibly not yet assembled.
  1937. struct hm_fragment {
  1938. static constexpr bool kAllowUniquePtr = true;
  1939. hm_fragment() {}
  1940. hm_fragment(const hm_fragment &) = delete;
  1941. hm_fragment &operator=(const hm_fragment &) = delete;
  1942. ~hm_fragment();
  1943. // type is the type of the message.
  1944. uint8_t type = 0;
  1945. // seq is the sequence number of this message.
  1946. uint16_t seq = 0;
  1947. // msg_len is the length of the message body.
  1948. uint32_t msg_len = 0;
  1949. // data is a pointer to the message, including message header. It has length
  1950. // |DTLS1_HM_HEADER_LENGTH| + |msg_len|.
  1951. uint8_t *data = nullptr;
  1952. // reassembly is a bitmask of |msg_len| bits corresponding to which parts of
  1953. // the message have been received. It is NULL if the message is complete.
  1954. uint8_t *reassembly = nullptr;
  1955. };
  1956. struct OPENSSL_timeval {
  1957. uint64_t tv_sec;
  1958. uint32_t tv_usec;
  1959. };
  1960. struct DTLS1_STATE {
  1961. static constexpr bool kAllowUniquePtr = true;
  1962. DTLS1_STATE();
  1963. ~DTLS1_STATE();
  1964. // has_change_cipher_spec is true if we have received a ChangeCipherSpec from
  1965. // the peer in this epoch.
  1966. bool has_change_cipher_spec:1;
  1967. // outgoing_messages_complete is true if |outgoing_messages| has been
  1968. // completed by an attempt to flush it. Future calls to |add_message| and
  1969. // |add_change_cipher_spec| will start a new flight.
  1970. bool outgoing_messages_complete:1;
  1971. // flight_has_reply is true if the current outgoing flight is complete and has
  1972. // processed at least one message. This is used to detect whether we or the
  1973. // peer sent the final flight.
  1974. bool flight_has_reply:1;
  1975. uint8_t cookie[DTLS1_COOKIE_LENGTH] = {0};
  1976. size_t cookie_len = 0;
  1977. // The current data and handshake epoch. This is initially undefined, and
  1978. // starts at zero once the initial handshake is completed.
  1979. uint16_t r_epoch = 0;
  1980. uint16_t w_epoch = 0;
  1981. // records being received in the current epoch
  1982. DTLS1_BITMAP bitmap;
  1983. uint16_t handshake_write_seq = 0;
  1984. uint16_t handshake_read_seq = 0;
  1985. // save last sequence number for retransmissions
  1986. uint8_t last_write_sequence[8] = {0};
  1987. UniquePtr<SSLAEADContext> last_aead_write_ctx;
  1988. // incoming_messages is a ring buffer of incoming handshake messages that have
  1989. // yet to be processed. The front of the ring buffer is message number
  1990. // |handshake_read_seq|, at position |handshake_read_seq| %
  1991. // |SSL_MAX_HANDSHAKE_FLIGHT|.
  1992. UniquePtr<hm_fragment> incoming_messages[SSL_MAX_HANDSHAKE_FLIGHT];
  1993. // outgoing_messages is the queue of outgoing messages from the last handshake
  1994. // flight.
  1995. DTLS_OUTGOING_MESSAGE outgoing_messages[SSL_MAX_HANDSHAKE_FLIGHT];
  1996. uint8_t outgoing_messages_len = 0;
  1997. // outgoing_written is the number of outgoing messages that have been
  1998. // written.
  1999. uint8_t outgoing_written = 0;
  2000. // outgoing_offset is the number of bytes of the next outgoing message have
  2001. // been written.
  2002. uint32_t outgoing_offset = 0;
  2003. unsigned mtu = 0; // max DTLS packet size
  2004. // num_timeouts is the number of times the retransmit timer has fired since
  2005. // the last time it was reset.
  2006. unsigned num_timeouts = 0;
  2007. // Indicates when the last handshake msg or heartbeat sent will
  2008. // timeout.
  2009. struct OPENSSL_timeval next_timeout = {0, 0};
  2010. // timeout_duration_ms is the timeout duration in milliseconds.
  2011. unsigned timeout_duration_ms = 0;
  2012. };
  2013. // SSLConnection backs the public |SSL| type. Due to compatibility constraints,
  2014. // it is a base class for |ssl_st|.
  2015. struct SSLConnection {
  2016. // method is the method table corresponding to the current protocol (DTLS or
  2017. // TLS).
  2018. const SSL_PROTOCOL_METHOD *method;
  2019. // version is the protocol version.
  2020. uint16_t version;
  2021. // conf_max_version is the maximum acceptable protocol version configured by
  2022. // |SSL_set_max_proto_version|. Note this version is normalized in DTLS and is
  2023. // further constrainted by |SSL_OP_NO_*|.
  2024. uint16_t conf_max_version;
  2025. // conf_min_version is the minimum acceptable protocol version configured by
  2026. // |SSL_set_min_proto_version|. Note this version is normalized in DTLS and is
  2027. // further constrainted by |SSL_OP_NO_*|.
  2028. uint16_t conf_min_version;
  2029. // tls13_variant is the variant of TLS 1.3 we are using for this
  2030. // configuration.
  2031. enum tls13_variant_t tls13_variant;
  2032. uint16_t max_send_fragment;
  2033. // There are 2 BIO's even though they are normally both the same. This is so
  2034. // data can be read and written to different handlers
  2035. BIO *rbio; // used by SSL_read
  2036. BIO *wbio; // used by SSL_write
  2037. // do_handshake runs the handshake. On completion, it returns |ssl_hs_ok|.
  2038. // Otherwise, it returns a value corresponding to what operation is needed to
  2039. // progress.
  2040. enum ssl_hs_wait_t (*do_handshake)(SSL_HANDSHAKE *hs);
  2041. SSL3_STATE *s3; // SSLv3 variables
  2042. DTLS1_STATE *d1; // DTLSv1 variables
  2043. // callback that allows applications to peek at protocol messages
  2044. void (*msg_callback)(int write_p, int version, int content_type,
  2045. const void *buf, size_t len, SSL *ssl, void *arg);
  2046. void *msg_callback_arg;
  2047. X509_VERIFY_PARAM *param;
  2048. // crypto
  2049. struct ssl_cipher_preference_list_st *cipher_list;
  2050. // session info
  2051. // client cert?
  2052. // This is used to hold the server certificate used
  2053. CERT *cert;
  2054. // initial_timeout_duration_ms is the default DTLS timeout duration in
  2055. // milliseconds. It's used to initialize the timer any time it's restarted.
  2056. unsigned initial_timeout_duration_ms;
  2057. // session is the configured session to be offered by the client. This session
  2058. // is immutable.
  2059. SSL_SESSION *session;
  2060. int (*verify_callback)(int ok,
  2061. X509_STORE_CTX *ctx); // fail if callback returns 0
  2062. enum ssl_verify_result_t (*custom_verify_callback)(SSL *ssl,
  2063. uint8_t *out_alert);
  2064. void (*info_callback)(const SSL *ssl, int type, int value);
  2065. // Server-only: psk_identity_hint is the identity hint to send in
  2066. // PSK-based key exchanges.
  2067. char *psk_identity_hint;
  2068. unsigned int (*psk_client_callback)(SSL *ssl, const char *hint,
  2069. char *identity,
  2070. unsigned int max_identity_len,
  2071. uint8_t *psk, unsigned int max_psk_len);
  2072. unsigned int (*psk_server_callback)(SSL *ssl, const char *identity,
  2073. uint8_t *psk, unsigned int max_psk_len);
  2074. SSL_CTX *ctx;
  2075. // extra application data
  2076. CRYPTO_EX_DATA ex_data;
  2077. // for server side, keep the list of CA_dn we can use
  2078. STACK_OF(CRYPTO_BUFFER) *client_CA;
  2079. // cached_x509_client_CA is a cache of parsed versions of the elements of
  2080. // |client_CA|.
  2081. STACK_OF(X509_NAME) *cached_x509_client_CA;
  2082. uint32_t options; // protocol behaviour
  2083. uint32_t mode; // API behaviour
  2084. uint32_t max_cert_list;
  2085. char *tlsext_hostname;
  2086. size_t supported_group_list_len;
  2087. uint16_t *supported_group_list; // our list
  2088. // session_ctx is the |SSL_CTX| used for the session cache and related
  2089. // settings.
  2090. SSL_CTX *session_ctx;
  2091. // srtp_profiles is the list of configured SRTP protection profiles for
  2092. // DTLS-SRTP.
  2093. STACK_OF(SRTP_PROTECTION_PROFILE) *srtp_profiles;
  2094. // srtp_profile is the selected SRTP protection profile for
  2095. // DTLS-SRTP.
  2096. const SRTP_PROTECTION_PROFILE *srtp_profile;
  2097. // The client's Channel ID private key.
  2098. EVP_PKEY *tlsext_channel_id_private;
  2099. // For a client, this contains the list of supported protocols in wire
  2100. // format.
  2101. uint8_t *alpn_client_proto_list;
  2102. unsigned alpn_client_proto_list_len;
  2103. // renegotiate_mode controls how peer renegotiation attempts are handled.
  2104. enum ssl_renegotiate_mode_t renegotiate_mode;
  2105. // verify_mode is a bitmask of |SSL_VERIFY_*| values.
  2106. uint8_t verify_mode;
  2107. // server is true iff the this SSL* is the server half. Note: before the SSL*
  2108. // is initialized by either SSL_set_accept_state or SSL_set_connect_state,
  2109. // the side is not determined. In this state, server is always false.
  2110. bool server:1;
  2111. // quiet_shutdown is true if the connection should not send a close_notify on
  2112. // shutdown.
  2113. bool quiet_shutdown:1;
  2114. // Enable signed certificate time stamps. Currently client only.
  2115. bool signed_cert_timestamps_enabled:1;
  2116. // ocsp_stapling_enabled is only used by client connections and indicates
  2117. // whether OCSP stapling will be requested.
  2118. bool ocsp_stapling_enabled:1;
  2119. // tlsext_channel_id_enabled is copied from the |SSL_CTX|. For a server,
  2120. // means that we'll accept Channel IDs from clients. For a client, means that
  2121. // we'll advertise support.
  2122. bool tlsext_channel_id_enabled:1;
  2123. // retain_only_sha256_of_client_certs is true if we should compute the SHA256
  2124. // hash of the peer's certificate and then discard it to save memory and
  2125. // session space. Only effective on the server side.
  2126. bool retain_only_sha256_of_client_certs:1;
  2127. // early_data_accepted is true if early data was accepted by the server.
  2128. bool early_data_accepted:1;
  2129. };
  2130. // From draft-ietf-tls-tls13-18, used in determining PSK modes.
  2131. #define SSL_PSK_DHE_KE 0x1
  2132. // From draft-ietf-tls-tls13-16, used in determining whether to respond with a
  2133. // KeyUpdate.
  2134. #define SSL_KEY_UPDATE_NOT_REQUESTED 0
  2135. #define SSL_KEY_UPDATE_REQUESTED 1
  2136. // kMaxEarlyDataAccepted is the advertised number of plaintext bytes of early
  2137. // data that will be accepted. This value should be slightly below
  2138. // kMaxEarlyDataSkipped in tls_record.c, which is measured in ciphertext.
  2139. static const size_t kMaxEarlyDataAccepted = 14336;
  2140. CERT *ssl_cert_new(const SSL_X509_METHOD *x509_method);
  2141. CERT *ssl_cert_dup(CERT *cert);
  2142. void ssl_cert_clear_certs(CERT *cert);
  2143. void ssl_cert_free(CERT *cert);
  2144. int ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer);
  2145. int ssl_is_key_type_supported(int key_type);
  2146. // ssl_compare_public_and_private_key returns one if |pubkey| is the public
  2147. // counterpart to |privkey|. Otherwise it returns zero and pushes a helpful
  2148. // message on the error queue.
  2149. int ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
  2150. const EVP_PKEY *privkey);
  2151. int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey);
  2152. int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server);
  2153. int ssl_encrypt_ticket(SSL *ssl, CBB *out, const SSL_SESSION *session);
  2154. int ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx);
  2155. // ssl_session_new returns a newly-allocated blank |SSL_SESSION| or nullptr on
  2156. // error.
  2157. UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method);
  2158. // SSL_SESSION_parse parses an |SSL_SESSION| from |cbs| and advances |cbs| over
  2159. // the parsed data.
  2160. UniquePtr<SSL_SESSION> SSL_SESSION_parse(CBS *cbs,
  2161. const SSL_X509_METHOD *x509_method,
  2162. CRYPTO_BUFFER_POOL *pool);
  2163. // ssl_session_is_context_valid returns one if |session|'s session ID context
  2164. // matches the one set on |ssl| and zero otherwise.
  2165. int ssl_session_is_context_valid(const SSL *ssl, const SSL_SESSION *session);
  2166. // ssl_session_is_time_valid returns one if |session| is still valid and zero if
  2167. // it has expired.
  2168. int ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session);
  2169. // ssl_session_is_resumable returns one if |session| is resumable for |hs| and
  2170. // zero otherwise.
  2171. int ssl_session_is_resumable(const SSL_HANDSHAKE *hs,
  2172. const SSL_SESSION *session);
  2173. // ssl_session_protocol_version returns the protocol version associated with
  2174. // |session|. Note that despite the name, this is not the same as
  2175. // |SSL_SESSION_get_protocol_version|. The latter is based on upstream's name.
  2176. uint16_t ssl_session_protocol_version(const SSL_SESSION *session);
  2177. // ssl_session_get_digest returns the digest used in |session|.
  2178. const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session);
  2179. void ssl_set_session(SSL *ssl, SSL_SESSION *session);
  2180. // ssl_get_prev_session looks up the previous session based on |client_hello|.
  2181. // On success, it sets |*out_session| to the session or nullptr if none was
  2182. // found. If the session could not be looked up synchronously, it returns
  2183. // |ssl_hs_pending_session| and should be called again. If a ticket could not be
  2184. // decrypted immediately it returns |ssl_hs_pending_ticket| and should also
  2185. // be called again. Otherwise, it returns |ssl_hs_error|.
  2186. enum ssl_hs_wait_t ssl_get_prev_session(SSL *ssl,
  2187. UniquePtr<SSL_SESSION> *out_session,
  2188. bool *out_tickets_supported,
  2189. bool *out_renew_ticket,
  2190. const SSL_CLIENT_HELLO *client_hello);
  2191. // The following flags determine which parts of the session are duplicated.
  2192. #define SSL_SESSION_DUP_AUTH_ONLY 0x0
  2193. #define SSL_SESSION_INCLUDE_TICKET 0x1
  2194. #define SSL_SESSION_INCLUDE_NONAUTH 0x2
  2195. #define SSL_SESSION_DUP_ALL \
  2196. (SSL_SESSION_INCLUDE_TICKET | SSL_SESSION_INCLUDE_NONAUTH)
  2197. // SSL_SESSION_dup returns a newly-allocated |SSL_SESSION| with a copy of the
  2198. // fields in |session| or nullptr on error. The new session is non-resumable and
  2199. // must be explicitly marked resumable once it has been filled in.
  2200. OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session,
  2201. int dup_flags);
  2202. // ssl_session_rebase_time updates |session|'s start time to the current time,
  2203. // adjusting the timeout so the expiration time is unchanged.
  2204. void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session);
  2205. // ssl_session_renew_timeout calls |ssl_session_rebase_time| and renews
  2206. // |session|'s timeout to |timeout| (measured from the current time). The
  2207. // renewal is clamped to the session's auth_timeout.
  2208. void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session,
  2209. uint32_t timeout);
  2210. void ssl_cipher_preference_list_free(
  2211. struct ssl_cipher_preference_list_st *cipher_list);
  2212. // ssl_get_cipher_preferences returns the cipher preference list for TLS 1.2 and
  2213. // below.
  2214. const struct ssl_cipher_preference_list_st *ssl_get_cipher_preferences(
  2215. const SSL *ssl);
  2216. void ssl_update_cache(SSL_HANDSHAKE *hs, int mode);
  2217. int ssl_send_alert(SSL *ssl, int level, int desc);
  2218. bool ssl3_get_message(SSL *ssl, SSLMessage *out);
  2219. ssl_open_record_t ssl3_open_handshake(SSL *ssl, size_t *out_consumed,
  2220. uint8_t *out_alert, Span<uint8_t> in);
  2221. void ssl3_next_message(SSL *ssl);
  2222. int ssl3_dispatch_alert(SSL *ssl);
  2223. ssl_open_record_t ssl3_open_app_data(SSL *ssl, Span<uint8_t> *out,
  2224. size_t *out_consumed, uint8_t *out_alert,
  2225. Span<uint8_t> in);
  2226. ssl_open_record_t ssl3_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
  2227. uint8_t *out_alert,
  2228. Span<uint8_t> in);
  2229. int ssl3_write_app_data(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
  2230. int len);
  2231. bool ssl3_new(SSL *ssl);
  2232. void ssl3_free(SSL *ssl);
  2233. bool ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
  2234. bool ssl3_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
  2235. bool ssl3_add_message(SSL *ssl, Array<uint8_t> msg);
  2236. bool ssl3_add_change_cipher_spec(SSL *ssl);
  2237. bool ssl3_add_alert(SSL *ssl, uint8_t level, uint8_t desc);
  2238. int ssl3_flush_flight(SSL *ssl);
  2239. bool dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
  2240. bool dtls1_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
  2241. bool dtls1_add_message(SSL *ssl, Array<uint8_t> msg);
  2242. bool dtls1_add_change_cipher_spec(SSL *ssl);
  2243. bool dtls1_add_alert(SSL *ssl, uint8_t level, uint8_t desc);
  2244. int dtls1_flush_flight(SSL *ssl);
  2245. // ssl_add_message_cbb finishes the handshake message in |cbb| and adds it to
  2246. // the pending flight. It returns true on success and false on error.
  2247. bool ssl_add_message_cbb(SSL *ssl, CBB *cbb);
  2248. // ssl_hash_message incorporates |msg| into the handshake hash. It returns true
  2249. // on success and false on allocation failure.
  2250. bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg);
  2251. ssl_open_record_t dtls1_open_app_data(SSL *ssl, Span<uint8_t> *out,
  2252. size_t *out_consumed, uint8_t *out_alert,
  2253. Span<uint8_t> in);
  2254. ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
  2255. uint8_t *out_alert,
  2256. Span<uint8_t> in);
  2257. int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake,
  2258. const uint8_t *buf, int len);
  2259. // dtls1_write_record sends a record. It returns one on success and <= 0 on
  2260. // error.
  2261. int dtls1_write_record(SSL *ssl, int type, const uint8_t *buf, size_t len,
  2262. enum dtls1_use_epoch_t use_epoch);
  2263. int dtls1_retransmit_outgoing_messages(SSL *ssl);
  2264. bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
  2265. CBS *out_body);
  2266. bool dtls1_check_timeout_num(SSL *ssl);
  2267. void dtls1_start_timer(SSL *ssl);
  2268. void dtls1_stop_timer(SSL *ssl);
  2269. bool dtls1_is_timer_expired(SSL *ssl);
  2270. unsigned int dtls1_min_mtu(void);
  2271. bool dtls1_new(SSL *ssl);
  2272. void dtls1_free(SSL *ssl);
  2273. bool dtls1_get_message(SSL *ssl, SSLMessage *out);
  2274. ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed,
  2275. uint8_t *out_alert, Span<uint8_t> in);
  2276. void dtls1_next_message(SSL *ssl);
  2277. int dtls1_dispatch_alert(SSL *ssl);
  2278. int tls1_change_cipher_state(SSL_HANDSHAKE *hs, evp_aead_direction_t direction);
  2279. int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out,
  2280. Span<const uint8_t> premaster);
  2281. // tls1_get_grouplist returns the locally-configured group preference list.
  2282. Span<const uint16_t> tls1_get_grouplist(const SSL *ssl);
  2283. // tls1_check_group_id returns one if |group_id| is consistent with
  2284. // locally-configured group preferences.
  2285. int tls1_check_group_id(const SSL *ssl, uint16_t group_id);
  2286. // tls1_get_shared_group sets |*out_group_id| to the first preferred shared
  2287. // group between client and server preferences and returns one. If none may be
  2288. // found, it returns zero.
  2289. int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id);
  2290. // tls1_set_curves converts the array of |ncurves| NIDs pointed to by |curves|
  2291. // into a newly allocated array of TLS group IDs. On success, the function
  2292. // returns one and writes the array to |*out_group_ids| and its size to
  2293. // |*out_group_ids_len|. Otherwise, it returns zero.
  2294. int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
  2295. const int *curves, size_t ncurves);
  2296. // tls1_set_curves_list converts the string of curves pointed to by |curves|
  2297. // into a newly allocated array of TLS group IDs. On success, the function
  2298. // returns one and writes the array to |*out_group_ids| and its size to
  2299. // |*out_group_ids_len|. Otherwise, it returns zero.
  2300. int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
  2301. const char *curves);
  2302. // ssl_add_clienthello_tlsext writes ClientHello extensions to |out|. It
  2303. // returns one on success and zero on failure. The |header_len| argument is the
  2304. // length of the ClientHello written so far and is used to compute the padding
  2305. // length. (It does not include the record header.)
  2306. int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len);
  2307. int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out);
  2308. int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
  2309. const SSL_CLIENT_HELLO *client_hello);
  2310. int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs);
  2311. #define tlsext_tick_md EVP_sha256
  2312. // ssl_process_ticket processes a session ticket from the client. It returns
  2313. // one of:
  2314. // |ssl_ticket_aead_success|: |*out_session| is set to the parsed session and
  2315. // |*out_renew_ticket| is set to whether the ticket should be renewed.
  2316. // |ssl_ticket_aead_ignore_ticket|: |*out_renew_ticket| is set to whether a
  2317. // fresh ticket should be sent, but the given ticket cannot be used.
  2318. // |ssl_ticket_aead_retry|: the ticket could not be immediately decrypted.
  2319. // Retry later.
  2320. // |ssl_ticket_aead_error|: an error occured that is fatal to the connection.
  2321. enum ssl_ticket_aead_result_t ssl_process_ticket(
  2322. SSL *ssl, UniquePtr<SSL_SESSION> *out_session, bool *out_renew_ticket,
  2323. const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
  2324. size_t session_id_len);
  2325. // tls1_verify_channel_id processes |msg| as a Channel ID message, and verifies
  2326. // the signature. If the key is valid, it saves the Channel ID and returns
  2327. // one. Otherwise, it returns zero.
  2328. int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg);
  2329. // tls1_write_channel_id generates a Channel ID message and puts the output in
  2330. // |cbb|. |ssl->tlsext_channel_id_private| must already be set before calling.
  2331. // This function returns true on success and false on error.
  2332. bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb);
  2333. // tls1_channel_id_hash computes the hash to be signed by Channel ID and writes
  2334. // it to |out|, which must contain at least |EVP_MAX_MD_SIZE| bytes. It returns
  2335. // one on success and zero on failure.
  2336. int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len);
  2337. int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs);
  2338. // ssl_do_channel_id_callback checks runs |ssl->ctx->channel_id_cb| if
  2339. // necessary. It returns one on success and zero on fatal error. Note that, on
  2340. // success, |ssl->tlsext_channel_id_private| may be unset, in which case the
  2341. // operation should be retried later.
  2342. int ssl_do_channel_id_callback(SSL *ssl);
  2343. // ssl_can_write returns one if |ssl| is allowed to write and zero otherwise.
  2344. int ssl_can_write(const SSL *ssl);
  2345. // ssl_can_read returns one if |ssl| is allowed to read and zero otherwise.
  2346. int ssl_can_read(const SSL *ssl);
  2347. void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock);
  2348. void ssl_ctx_get_current_time(const SSL_CTX *ctx,
  2349. struct OPENSSL_timeval *out_clock);
  2350. // ssl_reset_error_state resets state for |SSL_get_error|.
  2351. void ssl_reset_error_state(SSL *ssl);
  2352. // ssl_set_read_error sets |ssl|'s read half into an error state, saving the
  2353. // current state of the error queue.
  2354. void ssl_set_read_error(SSL* ssl);
  2355. } // namespace bssl
  2356. // Opaque C types.
  2357. //
  2358. // The following types are exported to C code as public typedefs, so they must
  2359. // be defined outside of the namespace.
  2360. // ssl_method_st backs the public |SSL_METHOD| type. It is a compatibility
  2361. // structure to support the legacy version-locked methods.
  2362. struct ssl_method_st {
  2363. // version, if non-zero, is the only protocol version acceptable to an
  2364. // SSL_CTX initialized from this method.
  2365. uint16_t version;
  2366. // method is the underlying SSL_PROTOCOL_METHOD that initializes the
  2367. // SSL_CTX.
  2368. const bssl::SSL_PROTOCOL_METHOD *method;
  2369. // x509_method contains pointers to functions that might deal with |X509|
  2370. // compatibility, or might be a no-op, depending on the application.
  2371. const SSL_X509_METHOD *x509_method;
  2372. };
  2373. struct ssl_x509_method_st {
  2374. // check_client_CA_list returns one if |names| is a good list of X.509
  2375. // distinguished names and zero otherwise. This is used to ensure that we can
  2376. // reject unparsable values at handshake time when using crypto/x509.
  2377. int (*check_client_CA_list)(STACK_OF(CRYPTO_BUFFER) *names);
  2378. // cert_clear frees and NULLs all X509 certificate-related state.
  2379. void (*cert_clear)(bssl::CERT *cert);
  2380. // cert_free frees all X509-related state.
  2381. void (*cert_free)(bssl::CERT *cert);
  2382. // cert_flush_cached_chain drops any cached |X509|-based certificate chain
  2383. // from |cert|.
  2384. // cert_dup duplicates any needed fields from |cert| to |new_cert|.
  2385. void (*cert_dup)(bssl::CERT *new_cert, const bssl::CERT *cert);
  2386. void (*cert_flush_cached_chain)(bssl::CERT *cert);
  2387. // cert_flush_cached_chain drops any cached |X509|-based leaf certificate
  2388. // from |cert|.
  2389. void (*cert_flush_cached_leaf)(bssl::CERT *cert);
  2390. // session_cache_objects fills out |sess->x509_peer| and |sess->x509_chain|
  2391. // from |sess->certs| and erases |sess->x509_chain_without_leaf|. It returns
  2392. // one on success or zero on error.
  2393. int (*session_cache_objects)(SSL_SESSION *session);
  2394. // session_dup duplicates any needed fields from |session| to |new_session|.
  2395. // It returns one on success or zero on error.
  2396. int (*session_dup)(SSL_SESSION *new_session, const SSL_SESSION *session);
  2397. // session_clear frees any X509-related state from |session|.
  2398. void (*session_clear)(SSL_SESSION *session);
  2399. // session_verify_cert_chain verifies the certificate chain in |session|,
  2400. // sets |session->verify_result| and returns one on success or zero on
  2401. // error.
  2402. int (*session_verify_cert_chain)(SSL_SESSION *session, SSL *ssl,
  2403. uint8_t *out_alert);
  2404. // hs_flush_cached_ca_names drops any cached |X509_NAME|s from |hs|.
  2405. void (*hs_flush_cached_ca_names)(bssl::SSL_HANDSHAKE *hs);
  2406. // ssl_new does any neccessary initialisation of |ssl|. It returns one on
  2407. // success or zero on error.
  2408. int (*ssl_new)(SSL *ssl);
  2409. // ssl_free frees anything created by |ssl_new|.
  2410. void (*ssl_free)(SSL *ssl);
  2411. // ssl_flush_cached_client_CA drops any cached |X509_NAME|s from |ssl|.
  2412. void (*ssl_flush_cached_client_CA)(SSL *ssl);
  2413. // ssl_auto_chain_if_needed runs the deprecated auto-chaining logic if
  2414. // necessary. On success, it updates |ssl|'s certificate configuration as
  2415. // needed and returns one. Otherwise, it returns zero.
  2416. int (*ssl_auto_chain_if_needed)(SSL *ssl);
  2417. // ssl_ctx_new does any neccessary initialisation of |ctx|. It returns one on
  2418. // success or zero on error.
  2419. int (*ssl_ctx_new)(SSL_CTX *ctx);
  2420. // ssl_ctx_free frees anything created by |ssl_ctx_new|.
  2421. void (*ssl_ctx_free)(SSL_CTX *ctx);
  2422. // ssl_ctx_flush_cached_client_CA drops any cached |X509_NAME|s from |ctx|.
  2423. void (*ssl_ctx_flush_cached_client_CA)(SSL_CTX *ssl);
  2424. };
  2425. // The following types back public C-exposed types which must live in the global
  2426. // namespace. We use subclassing so the implementations may be C++ types with
  2427. // methods and destructor without polluting the global namespace.
  2428. struct ssl_ctx_st : public bssl::SSLContext {};
  2429. struct ssl_st : public bssl::SSLConnection {};
  2430. #endif // OPENSSL_HEADER_SSL_INTERNAL_H