Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
před 7 roky
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
před 7 roky
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
před 7 roky
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
před 7 roky
Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
před 7 roky
Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
před 7 roky
Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
před 7 roky
Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
před 7 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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 2005 Nokia. All rights reserved.
  59. *
  60. * The portions of the attached software ("Contribution") is developed by
  61. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  62. * license.
  63. *
  64. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  65. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  66. * support (see RFC 4279) to OpenSSL.
  67. *
  68. * No patent licenses or other rights except those expressly stated in
  69. * the OpenSSL open source license shall be deemed granted or received
  70. * expressly, by implication, estoppel, or otherwise.
  71. *
  72. * No assurances are provided by Nokia that the Contribution does not
  73. * infringe the patent or other intellectual property rights of any third
  74. * party or that the license provides you with all the necessary rights
  75. * to make use of the Contribution.
  76. *
  77. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  78. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  79. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  80. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  81. * OTHERWISE. */
  82. #include <openssl/ssl.h>
  83. #include <limits.h>
  84. #include <string.h>
  85. #include <utility>
  86. #include <openssl/buf.h>
  87. #include <openssl/bytestring.h>
  88. #include <openssl/err.h>
  89. #include <openssl/mem.h>
  90. #include <openssl/x509.h>
  91. #include "../crypto/internal.h"
  92. #include "internal.h"
  93. BSSL_NAMESPACE_BEGIN
  94. // An SSL_SESSION is serialized as the following ASN.1 structure:
  95. //
  96. // SSLSession ::= SEQUENCE {
  97. // version INTEGER (1), -- session structure version
  98. // sslVersion INTEGER, -- protocol version number
  99. // cipher OCTET STRING, -- two bytes long
  100. // sessionID OCTET STRING,
  101. // masterKey OCTET STRING,
  102. // time [1] INTEGER, -- seconds since UNIX epoch
  103. // timeout [2] INTEGER, -- in seconds
  104. // peer [3] Certificate OPTIONAL,
  105. // sessionIDContext [4] OCTET STRING OPTIONAL,
  106. // verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes
  107. // pskIdentity [8] OCTET STRING OPTIONAL,
  108. // ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only
  109. // ticket [10] OCTET STRING OPTIONAL, -- client-only
  110. // peerSHA256 [13] OCTET STRING OPTIONAL,
  111. // originalHandshakeHash [14] OCTET STRING OPTIONAL,
  112. // signedCertTimestampList [15] OCTET STRING OPTIONAL,
  113. // -- contents of SCT extension
  114. // ocspResponse [16] OCTET STRING OPTIONAL,
  115. // -- stapled OCSP response from the server
  116. // extendedMasterSecret [17] BOOLEAN OPTIONAL,
  117. // groupID [18] INTEGER OPTIONAL,
  118. // certChain [19] SEQUENCE OF Certificate OPTIONAL,
  119. // ticketAgeAdd [21] OCTET STRING OPTIONAL,
  120. // isServer [22] BOOLEAN DEFAULT TRUE,
  121. // peerSignatureAlgorithm [23] INTEGER OPTIONAL,
  122. // ticketMaxEarlyData [24] INTEGER OPTIONAL,
  123. // authTimeout [25] INTEGER OPTIONAL, -- defaults to timeout
  124. // earlyALPN [26] OCTET STRING OPTIONAL,
  125. // }
  126. //
  127. // Note: historically this serialization has included other optional
  128. // fields. Their presence is currently treated as a parse error, except for
  129. // hostName, which is ignored.
  130. //
  131. // keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
  132. // hostName [6] OCTET STRING OPTIONAL,
  133. // pskIdentityHint [7] OCTET STRING OPTIONAL,
  134. // compressionMethod [11] OCTET STRING OPTIONAL,
  135. // srpUsername [12] OCTET STRING OPTIONAL,
  136. // ticketFlags [20] INTEGER OPTIONAL,
  137. static const unsigned kVersion = 1;
  138. static const unsigned kTimeTag =
  139. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
  140. static const unsigned kTimeoutTag =
  141. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
  142. static const unsigned kPeerTag =
  143. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
  144. static const unsigned kSessionIDContextTag =
  145. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
  146. static const unsigned kVerifyResultTag =
  147. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
  148. static const unsigned kHostNameTag =
  149. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
  150. static const unsigned kPSKIdentityTag =
  151. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
  152. static const unsigned kTicketLifetimeHintTag =
  153. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
  154. static const unsigned kTicketTag =
  155. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
  156. static const unsigned kPeerSHA256Tag =
  157. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
  158. static const unsigned kOriginalHandshakeHashTag =
  159. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
  160. static const unsigned kSignedCertTimestampListTag =
  161. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
  162. static const unsigned kOCSPResponseTag =
  163. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
  164. static const unsigned kExtendedMasterSecretTag =
  165. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
  166. static const unsigned kGroupIDTag =
  167. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18;
  168. static const unsigned kCertChainTag =
  169. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19;
  170. static const unsigned kTicketAgeAddTag =
  171. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21;
  172. static const unsigned kIsServerTag =
  173. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22;
  174. static const unsigned kPeerSignatureAlgorithmTag =
  175. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 23;
  176. static const unsigned kTicketMaxEarlyDataTag =
  177. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 24;
  178. static const unsigned kAuthTimeoutTag =
  179. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 25;
  180. static const unsigned kEarlyALPNTag =
  181. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 26;
  182. static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, CBB *cbb,
  183. int for_ticket) {
  184. if (in == NULL || in->cipher == NULL) {
  185. return 0;
  186. }
  187. CBB session, child, child2;
  188. if (!CBB_add_asn1(cbb, &session, CBS_ASN1_SEQUENCE) ||
  189. !CBB_add_asn1_uint64(&session, kVersion) ||
  190. !CBB_add_asn1_uint64(&session, in->ssl_version) ||
  191. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  192. !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
  193. // The session ID is irrelevant for a session ticket.
  194. !CBB_add_asn1_octet_string(&session, in->session_id,
  195. for_ticket ? 0 : in->session_id_length) ||
  196. !CBB_add_asn1_octet_string(&session, in->master_key,
  197. in->master_key_length) ||
  198. !CBB_add_asn1(&session, &child, kTimeTag) ||
  199. !CBB_add_asn1_uint64(&child, in->time) ||
  200. !CBB_add_asn1(&session, &child, kTimeoutTag) ||
  201. !CBB_add_asn1_uint64(&child, in->timeout)) {
  202. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  203. return 0;
  204. }
  205. // The peer certificate is only serialized if the SHA-256 isn't
  206. // serialized instead.
  207. if (sk_CRYPTO_BUFFER_num(in->certs.get()) > 0 && !in->peer_sha256_valid) {
  208. const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs.get(), 0);
  209. if (!CBB_add_asn1(&session, &child, kPeerTag) ||
  210. !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
  211. CRYPTO_BUFFER_len(buffer))) {
  212. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  213. return 0;
  214. }
  215. }
  216. // Although it is OPTIONAL and usually empty, OpenSSL has
  217. // historically always encoded the sid_ctx.
  218. if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
  219. !CBB_add_asn1_octet_string(&child, in->sid_ctx, in->sid_ctx_length)) {
  220. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  221. return 0;
  222. }
  223. if (in->verify_result != X509_V_OK) {
  224. if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
  225. !CBB_add_asn1_uint64(&child, in->verify_result)) {
  226. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  227. return 0;
  228. }
  229. }
  230. if (in->psk_identity) {
  231. if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
  232. !CBB_add_asn1_octet_string(&child,
  233. (const uint8_t *)in->psk_identity.get(),
  234. strlen(in->psk_identity.get()))) {
  235. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  236. return 0;
  237. }
  238. }
  239. if (in->ticket_lifetime_hint > 0) {
  240. if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
  241. !CBB_add_asn1_uint64(&child, in->ticket_lifetime_hint)) {
  242. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  243. return 0;
  244. }
  245. }
  246. if (!in->ticket.empty() && !for_ticket) {
  247. if (!CBB_add_asn1(&session, &child, kTicketTag) ||
  248. !CBB_add_asn1_octet_string(&child, in->ticket.data(),
  249. in->ticket.size())) {
  250. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  251. return 0;
  252. }
  253. }
  254. if (in->peer_sha256_valid) {
  255. if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
  256. !CBB_add_asn1_octet_string(&child, in->peer_sha256,
  257. sizeof(in->peer_sha256))) {
  258. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  259. return 0;
  260. }
  261. }
  262. if (in->original_handshake_hash_len > 0) {
  263. if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
  264. !CBB_add_asn1_octet_string(&child, in->original_handshake_hash,
  265. in->original_handshake_hash_len)) {
  266. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  267. return 0;
  268. }
  269. }
  270. if (in->signed_cert_timestamp_list != nullptr) {
  271. if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
  272. !CBB_add_asn1_octet_string(
  273. &child, CRYPTO_BUFFER_data(in->signed_cert_timestamp_list.get()),
  274. CRYPTO_BUFFER_len(in->signed_cert_timestamp_list.get()))) {
  275. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  276. return 0;
  277. }
  278. }
  279. if (in->ocsp_response != nullptr) {
  280. if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
  281. !CBB_add_asn1_octet_string(
  282. &child, CRYPTO_BUFFER_data(in->ocsp_response.get()),
  283. CRYPTO_BUFFER_len(in->ocsp_response.get()))) {
  284. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  285. return 0;
  286. }
  287. }
  288. if (in->extended_master_secret) {
  289. if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
  290. !CBB_add_asn1_bool(&child, true)) {
  291. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  292. return 0;
  293. }
  294. }
  295. if (in->group_id > 0 &&
  296. (!CBB_add_asn1(&session, &child, kGroupIDTag) ||
  297. !CBB_add_asn1_uint64(&child, in->group_id))) {
  298. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  299. return 0;
  300. }
  301. // The certificate chain is only serialized if the leaf's SHA-256 isn't
  302. // serialized instead.
  303. if (in->certs != NULL &&
  304. !in->peer_sha256_valid &&
  305. sk_CRYPTO_BUFFER_num(in->certs.get()) >= 2) {
  306. if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
  307. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  308. return 0;
  309. }
  310. for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs.get()); i++) {
  311. const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs.get(), i);
  312. if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
  313. CRYPTO_BUFFER_len(buffer))) {
  314. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  315. return 0;
  316. }
  317. }
  318. }
  319. if (in->ticket_age_add_valid) {
  320. if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) ||
  321. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  322. !CBB_add_u32(&child2, in->ticket_age_add)) {
  323. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  324. return 0;
  325. }
  326. }
  327. if (!in->is_server) {
  328. if (!CBB_add_asn1(&session, &child, kIsServerTag) ||
  329. !CBB_add_asn1_bool(&child, false)) {
  330. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  331. return 0;
  332. }
  333. }
  334. if (in->peer_signature_algorithm != 0 &&
  335. (!CBB_add_asn1(&session, &child, kPeerSignatureAlgorithmTag) ||
  336. !CBB_add_asn1_uint64(&child, in->peer_signature_algorithm))) {
  337. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  338. return 0;
  339. }
  340. if (in->ticket_max_early_data != 0 &&
  341. (!CBB_add_asn1(&session, &child, kTicketMaxEarlyDataTag) ||
  342. !CBB_add_asn1_uint64(&child, in->ticket_max_early_data))) {
  343. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  344. return 0;
  345. }
  346. if (in->timeout != in->auth_timeout &&
  347. (!CBB_add_asn1(&session, &child, kAuthTimeoutTag) ||
  348. !CBB_add_asn1_uint64(&child, in->auth_timeout))) {
  349. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  350. return 0;
  351. }
  352. if (!in->early_alpn.empty()) {
  353. if (!CBB_add_asn1(&session, &child, kEarlyALPNTag) ||
  354. !CBB_add_asn1_octet_string(&child, in->early_alpn.data(),
  355. in->early_alpn.size())) {
  356. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  357. return 0;
  358. }
  359. }
  360. return CBB_flush(cbb);
  361. }
  362. // SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING explicitly
  363. // tagged with |tag| from |cbs| and saves it in |*out|. If the element was not
  364. // found, it sets |*out| to NULL. It returns one on success, whether or not the
  365. // element was found, and zero on decode error.
  366. static int SSL_SESSION_parse_string(CBS *cbs, UniquePtr<char> *out, unsigned tag) {
  367. CBS value;
  368. int present;
  369. if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
  370. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  371. return 0;
  372. }
  373. if (present) {
  374. if (CBS_contains_zero_byte(&value)) {
  375. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  376. return 0;
  377. }
  378. char *raw = nullptr;
  379. if (!CBS_strdup(&value, &raw)) {
  380. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  381. return 0;
  382. }
  383. out->reset(raw);
  384. } else {
  385. out->reset();
  386. }
  387. return 1;
  388. }
  389. // SSL_SESSION_parse_octet_string gets an optional ASN.1 OCTET STRING explicitly
  390. // tagged with |tag| from |cbs| and stows it in |*out|. It returns one on
  391. // success, whether or not the element was found, and zero on decode error.
  392. static bool SSL_SESSION_parse_octet_string(CBS *cbs, Array<uint8_t> *out,
  393. unsigned tag) {
  394. CBS value;
  395. if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
  396. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  397. return false;
  398. }
  399. return out->CopyFrom(value);
  400. }
  401. static int SSL_SESSION_parse_crypto_buffer(CBS *cbs,
  402. UniquePtr<CRYPTO_BUFFER> *out,
  403. unsigned tag,
  404. CRYPTO_BUFFER_POOL *pool) {
  405. if (!CBS_peek_asn1_tag(cbs, tag)) {
  406. return 1;
  407. }
  408. CBS child, value;
  409. if (!CBS_get_asn1(cbs, &child, tag) ||
  410. !CBS_get_asn1(&child, &value, CBS_ASN1_OCTETSTRING) ||
  411. CBS_len(&child) != 0) {
  412. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  413. return 0;
  414. }
  415. out->reset(CRYPTO_BUFFER_new_from_CBS(&value, pool));
  416. if (*out == nullptr) {
  417. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  418. return 0;
  419. }
  420. return 1;
  421. }
  422. // SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
  423. // explicitly tagged with |tag| of size at most |max_out|.
  424. static int SSL_SESSION_parse_bounded_octet_string(
  425. CBS *cbs, uint8_t *out, uint8_t *out_len, uint8_t max_out, unsigned tag) {
  426. CBS value;
  427. if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
  428. CBS_len(&value) > max_out) {
  429. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  430. return 0;
  431. }
  432. OPENSSL_memcpy(out, CBS_data(&value), CBS_len(&value));
  433. *out_len = (uint8_t)CBS_len(&value);
  434. return 1;
  435. }
  436. static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
  437. long default_value) {
  438. uint64_t value;
  439. if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
  440. (uint64_t)default_value) ||
  441. value > LONG_MAX) {
  442. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  443. return 0;
  444. }
  445. *out = (long)value;
  446. return 1;
  447. }
  448. static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
  449. uint32_t default_value) {
  450. uint64_t value;
  451. if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
  452. (uint64_t)default_value) ||
  453. value > 0xffffffff) {
  454. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  455. return 0;
  456. }
  457. *out = (uint32_t)value;
  458. return 1;
  459. }
  460. static int SSL_SESSION_parse_u16(CBS *cbs, uint16_t *out, unsigned tag,
  461. uint16_t default_value) {
  462. uint64_t value;
  463. if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
  464. (uint64_t)default_value) ||
  465. value > 0xffff) {
  466. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  467. return 0;
  468. }
  469. *out = (uint16_t)value;
  470. return 1;
  471. }
  472. UniquePtr<SSL_SESSION> SSL_SESSION_parse(CBS *cbs,
  473. const SSL_X509_METHOD *x509_method,
  474. CRYPTO_BUFFER_POOL *pool) {
  475. UniquePtr<SSL_SESSION> ret = ssl_session_new(x509_method);
  476. if (!ret) {
  477. return nullptr;
  478. }
  479. CBS session;
  480. uint64_t version, ssl_version;
  481. uint16_t unused;
  482. if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
  483. !CBS_get_asn1_uint64(&session, &version) ||
  484. version != kVersion ||
  485. !CBS_get_asn1_uint64(&session, &ssl_version) ||
  486. // Require sessions have versions valid in either TLS or DTLS. The session
  487. // will not be used by the handshake if not applicable, but, for
  488. // simplicity, never parse a session that does not pass
  489. // |ssl_protocol_version_from_wire|.
  490. ssl_version > UINT16_MAX ||
  491. !ssl_protocol_version_from_wire(&unused, ssl_version)) {
  492. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  493. return nullptr;
  494. }
  495. ret->ssl_version = ssl_version;
  496. CBS cipher;
  497. uint16_t cipher_value;
  498. if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
  499. !CBS_get_u16(&cipher, &cipher_value) ||
  500. CBS_len(&cipher) != 0) {
  501. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  502. return nullptr;
  503. }
  504. ret->cipher = SSL_get_cipher_by_value(cipher_value);
  505. if (ret->cipher == NULL) {
  506. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
  507. return nullptr;
  508. }
  509. CBS session_id, master_key;
  510. if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
  511. CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
  512. !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
  513. CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
  514. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  515. return nullptr;
  516. }
  517. OPENSSL_memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
  518. ret->session_id_length = CBS_len(&session_id);
  519. OPENSSL_memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
  520. ret->master_key_length = CBS_len(&master_key);
  521. CBS child;
  522. uint64_t timeout;
  523. if (!CBS_get_asn1(&session, &child, kTimeTag) ||
  524. !CBS_get_asn1_uint64(&child, &ret->time) ||
  525. !CBS_get_asn1(&session, &child, kTimeoutTag) ||
  526. !CBS_get_asn1_uint64(&child, &timeout) ||
  527. timeout > UINT32_MAX) {
  528. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  529. return nullptr;
  530. }
  531. ret->timeout = (uint32_t)timeout;
  532. CBS peer;
  533. int has_peer;
  534. if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
  535. (has_peer && CBS_len(&peer) == 0)) {
  536. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  537. return nullptr;
  538. }
  539. // |peer| is processed with the certificate chain.
  540. if (!SSL_SESSION_parse_bounded_octet_string(
  541. &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
  542. kSessionIDContextTag) ||
  543. !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
  544. X509_V_OK)) {
  545. return nullptr;
  546. }
  547. // Skip the historical hostName field.
  548. CBS unused_hostname;
  549. if (!CBS_get_optional_asn1(&session, &unused_hostname, nullptr,
  550. kHostNameTag)) {
  551. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  552. return nullptr;
  553. }
  554. if (!SSL_SESSION_parse_string(&session, &ret->psk_identity,
  555. kPSKIdentityTag) ||
  556. !SSL_SESSION_parse_u32(&session, &ret->ticket_lifetime_hint,
  557. kTicketLifetimeHintTag, 0) ||
  558. !SSL_SESSION_parse_octet_string(&session, &ret->ticket, kTicketTag)) {
  559. return nullptr;
  560. }
  561. if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
  562. CBS peer_sha256;
  563. if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
  564. !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
  565. CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
  566. CBS_len(&child) != 0) {
  567. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  568. return nullptr;
  569. }
  570. OPENSSL_memcpy(ret->peer_sha256, CBS_data(&peer_sha256),
  571. sizeof(ret->peer_sha256));
  572. ret->peer_sha256_valid = 1;
  573. } else {
  574. ret->peer_sha256_valid = 0;
  575. }
  576. if (!SSL_SESSION_parse_bounded_octet_string(
  577. &session, ret->original_handshake_hash,
  578. &ret->original_handshake_hash_len,
  579. sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
  580. !SSL_SESSION_parse_crypto_buffer(&session,
  581. &ret->signed_cert_timestamp_list,
  582. kSignedCertTimestampListTag, pool) ||
  583. !SSL_SESSION_parse_crypto_buffer(&session, &ret->ocsp_response,
  584. kOCSPResponseTag, pool)) {
  585. return nullptr;
  586. }
  587. int extended_master_secret;
  588. if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
  589. kExtendedMasterSecretTag,
  590. 0 /* default to false */)) {
  591. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  592. return nullptr;
  593. }
  594. ret->extended_master_secret = !!extended_master_secret;
  595. if (!SSL_SESSION_parse_u16(&session, &ret->group_id, kGroupIDTag, 0)) {
  596. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  597. return nullptr;
  598. }
  599. CBS cert_chain;
  600. CBS_init(&cert_chain, NULL, 0);
  601. int has_cert_chain;
  602. if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
  603. kCertChainTag) ||
  604. (has_cert_chain && CBS_len(&cert_chain) == 0)) {
  605. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  606. return nullptr;
  607. }
  608. if (has_cert_chain && !has_peer) {
  609. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  610. return nullptr;
  611. }
  612. if (has_peer || has_cert_chain) {
  613. ret->certs.reset(sk_CRYPTO_BUFFER_new_null());
  614. if (ret->certs == nullptr) {
  615. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  616. return nullptr;
  617. }
  618. if (has_peer) {
  619. UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new_from_CBS(&peer, pool));
  620. if (!buffer ||
  621. !PushToStack(ret->certs.get(), std::move(buffer))) {
  622. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  623. return nullptr;
  624. }
  625. }
  626. while (CBS_len(&cert_chain) > 0) {
  627. CBS cert;
  628. if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) ||
  629. CBS_len(&cert) == 0) {
  630. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  631. return nullptr;
  632. }
  633. UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new_from_CBS(&cert, pool));
  634. if (buffer == nullptr ||
  635. !PushToStack(ret->certs.get(), std::move(buffer))) {
  636. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  637. return nullptr;
  638. }
  639. }
  640. }
  641. CBS age_add;
  642. int age_add_present;
  643. if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present,
  644. kTicketAgeAddTag) ||
  645. (age_add_present &&
  646. !CBS_get_u32(&age_add, &ret->ticket_age_add)) ||
  647. CBS_len(&age_add) != 0) {
  648. return nullptr;
  649. }
  650. ret->ticket_age_add_valid = age_add_present != 0;
  651. int is_server;
  652. if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag,
  653. 1 /* default to true */)) {
  654. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  655. return nullptr;
  656. }
  657. /* TODO: in time we can include |is_server| for servers too, then we can
  658. enforce that client and server sessions are never mixed up. */
  659. ret->is_server = is_server;
  660. if (!SSL_SESSION_parse_u16(&session, &ret->peer_signature_algorithm,
  661. kPeerSignatureAlgorithmTag, 0) ||
  662. !SSL_SESSION_parse_u32(&session, &ret->ticket_max_early_data,
  663. kTicketMaxEarlyDataTag, 0) ||
  664. !SSL_SESSION_parse_u32(&session, &ret->auth_timeout, kAuthTimeoutTag,
  665. ret->timeout) ||
  666. !SSL_SESSION_parse_octet_string(&session, &ret->early_alpn,
  667. kEarlyALPNTag) ||
  668. CBS_len(&session) != 0) {
  669. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  670. return nullptr;
  671. }
  672. if (!x509_method->session_cache_objects(ret.get())) {
  673. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  674. return nullptr;
  675. }
  676. return ret;
  677. }
  678. int ssl_session_serialize(const SSL_SESSION *in, CBB *cbb) {
  679. return SSL_SESSION_to_bytes_full(in, cbb, 0);
  680. }
  681. BSSL_NAMESPACE_END
  682. using namespace bssl;
  683. int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
  684. size_t *out_len) {
  685. if (in->not_resumable) {
  686. // If the caller has an unresumable session, e.g. if |SSL_get_session| were
  687. // called on a TLS 1.3 or False Started connection, serialize with a
  688. // placeholder value so it is not accidentally deserialized into a resumable
  689. // one.
  690. static const char kNotResumableSession[] = "NOT RESUMABLE";
  691. *out_len = strlen(kNotResumableSession);
  692. *out_data = (uint8_t *)BUF_memdup(kNotResumableSession, *out_len);
  693. if (*out_data == NULL) {
  694. return 0;
  695. }
  696. return 1;
  697. }
  698. ScopedCBB cbb;
  699. if (!CBB_init(cbb.get(), 256) ||
  700. !SSL_SESSION_to_bytes_full(in, cbb.get(), 0) ||
  701. !CBB_finish(cbb.get(), out_data, out_len)) {
  702. return 0;
  703. }
  704. return 1;
  705. }
  706. int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
  707. size_t *out_len) {
  708. ScopedCBB cbb;
  709. if (!CBB_init(cbb.get(), 256) ||
  710. !SSL_SESSION_to_bytes_full(in, cbb.get(), 1) ||
  711. !CBB_finish(cbb.get(), out_data, out_len)) {
  712. return 0;
  713. }
  714. return 1;
  715. }
  716. int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
  717. uint8_t *out;
  718. size_t len;
  719. if (!SSL_SESSION_to_bytes(in, &out, &len)) {
  720. return -1;
  721. }
  722. if (len > INT_MAX) {
  723. OPENSSL_free(out);
  724. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  725. return -1;
  726. }
  727. if (pp) {
  728. OPENSSL_memcpy(*pp, out, len);
  729. *pp += len;
  730. }
  731. OPENSSL_free(out);
  732. return len;
  733. }
  734. SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len,
  735. const SSL_CTX *ctx) {
  736. CBS cbs;
  737. CBS_init(&cbs, in, in_len);
  738. UniquePtr<SSL_SESSION> ret =
  739. SSL_SESSION_parse(&cbs, ctx->x509_method, ctx->pool);
  740. if (!ret) {
  741. return NULL;
  742. }
  743. if (CBS_len(&cbs) != 0) {
  744. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  745. return NULL;
  746. }
  747. return ret.release();
  748. }