test_state.cc
Go to the documentation of this file.
1 /* Copyright (c) 2018, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include "test_state.h"
16 
17 #include <openssl/ssl.h>
18 
19 #include "../../crypto/internal.h"
20 #include "../internal.h"
21 
22 using namespace bssl;
23 
25 static int g_state_index = 0;
26 // Some code treats the zero time special, so initialize the clock to a
27 // non-zero time.
28 static timeval g_clock = { 1234, 1234 };
29 
30 static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
31  int index, long argl, void *argp) {
32  delete ((TestState *)ptr);
33 }
34 
35 static void init_once() {
36  g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
37  if (g_state_index < 0) {
38  abort();
39  }
40 }
41 
42 struct timeval *GetClock() {
44  return &g_clock;
45 }
46 
47 void AdvanceClock(unsigned seconds) {
50 }
51 
52 bool SetTestState(SSL *ssl, std::unique_ptr<TestState> state) {
54  // |SSL_set_ex_data| takes ownership of |state| only on success.
55  if (SSL_set_ex_data(ssl, g_state_index, state.get()) == 1) {
56  state.release();
57  return true;
58  }
59  return false;
60 }
61 
62 TestState *GetTestState(const SSL *ssl) {
64  return (TestState *)SSL_get_ex_data(ssl, g_state_index);
65 }
66 
67 static void ssl_ctx_add_session(SSL_SESSION *session, void *void_param) {
68  SSL_CTX *ctx = reinterpret_cast<SSL_CTX *>(void_param);
69  UniquePtr<SSL_SESSION> new_session = SSL_SESSION_dup(
71  if (new_session != nullptr) {
72  SSL_CTX_add_session(ctx, new_session.get());
73  }
74 }
75 
76 void CopySessions(SSL_CTX *dst, const SSL_CTX *src) {
77  lh_SSL_SESSION_doall_arg(src->sessions, ssl_ctx_add_session, dst);
78 }
79 
80 static void push_session(SSL_SESSION *session, void *arg) {
81  auto s = reinterpret_cast<std::vector<SSL_SESSION *> *>(arg);
82  s->push_back(session);
83 }
84 
86  CBB out, ctx_sessions, ticket_keys;
87  uint8_t keys[48];
88  if (!CBB_add_u24_length_prefixed(cbb, &out) ||
89  !CBB_add_u16(&out, 0 /* version */) ||
91  !CBB_add_u8_length_prefixed(&out, &ticket_keys) ||
92  !CBB_add_bytes(&ticket_keys, keys, sizeof(keys)) ||
93  !CBB_add_asn1(&out, &ctx_sessions, CBS_ASN1_SEQUENCE)) {
94  return false;
95  }
96  std::vector<SSL_SESSION *> sessions;
97  lh_SSL_SESSION_doall_arg(ctx->sessions, push_session, &sessions);
98  for (const auto &sess : sessions) {
99  if (!ssl_session_serialize(sess, &ctx_sessions)) {
100  return false;
101  }
102  }
103  return CBB_flush(cbb);
104 }
105 
107  CBS in, sessions, ticket_keys;
109  constexpr uint16_t kVersion = 0;
111  !CBS_get_u16(&in, &version) ||
112  version > kVersion ||
113  !CBS_get_u8_length_prefixed(&in, &ticket_keys) ||
115  CBS_len(&ticket_keys)) ||
116  !CBS_get_asn1(&in, &sessions, CBS_ASN1_SEQUENCE)) {
117  return false;
118  }
119  while (CBS_len(&sessions)) {
120  UniquePtr<SSL_SESSION> session =
121  SSL_SESSION_parse(&sessions, ctx->x509_method, ctx->pool);
122  if (!session) {
123  return false;
124  }
125  SSL_CTX_add_session(ctx, session.get());
126  }
127  return true;
128 }
129 
130 bool TestState::Serialize(CBB *cbb) const {
131  CBB out, pending, text;
132  if (!CBB_add_u24_length_prefixed(cbb, &out) ||
133  !CBB_add_u16(&out, 0 /* version */) ||
135  (pending_session &&
136  !ssl_session_serialize(pending_session.get(), &pending)) ||
138  !CBB_add_bytes(
139  &text, reinterpret_cast<const uint8_t *>(msg_callback_text.data()),
140  msg_callback_text.length()) ||
143  !CBB_flush(cbb)) {
144  return false;
145  }
146  return true;
147 }
148 
149 std::unique_ptr<TestState> TestState::Deserialize(CBS *cbs, SSL_CTX *ctx) {
150  CBS in, pending_session, text;
151  std::unique_ptr<TestState> out_state(new TestState());
153  constexpr uint16_t kVersion = 0;
154  uint64_t sec, usec;
156  !CBS_get_u16(&in, &version) ||
157  version > kVersion ||
158  !CBS_get_u24_length_prefixed(&in, &pending_session) ||
160  return nullptr;
161  }
162  if (CBS_len(&pending_session)) {
163  out_state->pending_session = SSL_SESSION_parse(
164  &pending_session, ctx->x509_method, ctx->pool);
165  if (!out_state->pending_session) {
166  return nullptr;
167  }
168  }
169  out_state->msg_callback_text = std::string(
170  reinterpret_cast<const char *>(CBS_data(&text)), CBS_len(&text));
171  // TODO(2020-05-01): Make this unconditional & merge into above.
172  if (CBS_len(&in) > 0) {
173  if (!CBS_get_asn1_uint64(&in, &sec) ||
174  !CBS_get_asn1_uint64(&in, &usec)) {
175  return nullptr;
176  }
177  g_clock.tv_sec = sec;
178  g_clock.tv_usec = usec;
179  }
180  return out_state;
181 }
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
CBS_get_asn1_uint64
#define CBS_get_asn1_uint64
Definition: boringssl_prefix_symbols.h:1066
absl::time_internal::cctz::seconds
std::chrono::duration< std::int_fast64_t > seconds
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:40
CBS_get_u24_length_prefixed
#define CBS_get_u24_length_prefixed
Definition: boringssl_prefix_symbols.h:1077
CBB_flush
#define CBB_flush
Definition: boringssl_prefix_symbols.h:1045
dst
static const char dst[]
Definition: test-fs-copyfile.c:37
test_server.argp
argp
Definition: test_server.py:33
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
SSL_SESSION_parse
OPENSSL_EXPORT UniquePtr< SSL_SESSION > SSL_SESSION_parse(CBS *cbs, const SSL_X509_METHOD *x509_method, CRYPTO_BUFFER_POOL *pool)
Definition: ssl_asn1.cc:555
CBS_get_u16
#define CBS_get_u16
Definition: boringssl_prefix_symbols.h:1073
cbs_st
Definition: bytestring.h:39
ctx
Definition: benchmark-async.c:30
GetTestState
TestState * GetTestState(const SSL *ssl)
Definition: test_state.cc:62
SSL_SESSION_INCLUDE_TICKET
#define SSL_SESSION_INCLUDE_TICKET
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3171
TestState::Serialize
bool Serialize(CBB *out) const
Definition: test_state.cc:130
CBS_data
#define CBS_data
Definition: boringssl_prefix_symbols.h:1057
keys
const void * keys
Definition: abseil-cpp/absl/random/internal/randen.cc:49
test_state.h
uint16_t
unsigned short uint16_t
Definition: stdint-msvc2008.h:79
CBB_add_u16_length_prefixed
#define CBB_add_u16_length_prefixed
Definition: boringssl_prefix_symbols.h:1028
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
CBS_get_u8_length_prefixed
#define CBS_get_u8_length_prefixed
Definition: boringssl_prefix_symbols.h:1083
SSL_CTX_get_tlsext_ticket_keys
#define SSL_CTX_get_tlsext_ticket_keys
Definition: boringssl_prefix_symbols.h:109
CBS_len
#define CBS_len
Definition: boringssl_prefix_symbols.h:1089
SerializeContextState
bool SerializeContextState(SSL_CTX *ctx, CBB *cbb)
Definition: test_state.cc:85
bssl
Definition: hpke_test.cc:37
CBS_get_asn1
#define CBS_get_asn1
Definition: boringssl_prefix_symbols.h:1061
version
Definition: version.py:1
CRYPTO_once
#define CRYPTO_once
Definition: boringssl_prefix_symbols.h:1182
cbs
const CBS * cbs
Definition: third_party/boringssl-with-bazel/src/crypto/trust_token/internal.h:107
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
ssl_ctx_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3404
grpc_core::pending
P< T > pending()
Definition: try_join_test.cc:50
g_once
static CRYPTO_once_t g_once
Definition: test_state.cc:24
push_session
static void push_session(SSL_SESSION *session, void *arg)
Definition: test_state.cc:80
GetClock
struct timeval * GetClock()
Definition: test_state.cc:42
TestState::Deserialize
static std::unique_ptr< TestState > Deserialize(CBS *cbs, SSL_CTX *ctx)
Definition: test_state.cc:149
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
CBB_add_u16
#define CBB_add_u16
Definition: boringssl_prefix_symbols.h:1027
SSL_set_ex_data
#define SSL_set_ex_data
Definition: boringssl_prefix_symbols.h:462
gen_server_registered_method_bad_client_test_body.text
def text
Definition: gen_server_registered_method_bad_client_test_body.py:50
g_state_index
static int g_state_index
Definition: test_state.cc:25
CBB_add_u8_length_prefixed
#define CBB_add_u8_length_prefixed
Definition: boringssl_prefix_symbols.h:1037
ssl_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3698
conf.version
string version
Definition: doc/python/sphinx/conf.py:36
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
CBB_add_asn1
#define CBB_add_asn1
Definition: boringssl_prefix_symbols.h:1019
CRYPTO_ONCE_INIT
#define CRYPTO_ONCE_INIT
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:471
arg
Definition: cmdline.cc:40
CopySessions
void CopySessions(SSL_CTX *dst, const SSL_CTX *src)
Definition: test_state.cc:76
kVersion
static const BSSL_NAMESPACE_BEGIN unsigned kVersion
Definition: ssl_asn1.cc:151
SSL_SESSION_dup
OPENSSL_EXPORT UniquePtr< SSL_SESSION > SSL_SESSION_dup(SSL_SESSION *session, int dup_flags)
Definition: ssl_session.cc:191
TestState
Definition: test_state.h:27
ssl.h
ssl_session_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3787
SetTestState
bool SetTestState(SSL *ssl, std::unique_ptr< TestState > state)
Definition: test_state.cc:52
ssl_ctx_add_session
static void ssl_ctx_add_session(SSL_SESSION *session, void *void_param)
Definition: test_state.cc:67
CBB_add_bytes
#define CBB_add_bytes
Definition: boringssl_prefix_symbols.h:1025
timeval::tv_sec
long tv_sec
Definition: setup_once.h:121
timeval::tv_usec
long tv_usec
Definition: setup_once.h:122
SSL_get_ex_new_index
#define SSL_get_ex_new_index
Definition: boringssl_prefix_symbols.h:343
timeval
Definition: setup_once.h:113
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
CBS_get_u16_length_prefixed
#define CBS_get_u16_length_prefixed
Definition: boringssl_prefix_symbols.h:1074
g_clock
static timeval g_clock
Definition: test_state.cc:28
SSL_get_ex_data
#define SSL_get_ex_data
Definition: boringssl_prefix_symbols.h:341
arg
struct arg arg
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
AdvanceClock
void AdvanceClock(unsigned seconds)
Definition: test_state.cc:47
init_once
static void init_once()
Definition: test_state.cc:35
CRYPTO_once_t
uint32_t CRYPTO_once_t
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:470
SSL_CTX_add_session
#define SSL_CTX_add_session
Definition: boringssl_prefix_symbols.h:73
CBS_ASN1_SEQUENCE
#define CBS_ASN1_SEQUENCE
Definition: bytestring.h:214
crypto_ex_data_st
Definition: ex_data.h:194
SSL_SESSION_INCLUDE_NONAUTH
#define SSL_SESSION_INCLUDE_NONAUTH
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3172
TestStateExFree
static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int index, long argl, void *argp)
Definition: test_state.cc:30
ssl_session_serialize
OPENSSL_EXPORT int ssl_session_serialize(const SSL_SESSION *in, CBB *cbb)
Definition: ssl_asn1.cc:811
DeserializeContextState
bool DeserializeContextState(CBS *cbs, SSL_CTX *ctx)
Definition: test_state.cc:106
SSL_CTX_set_tlsext_ticket_keys
#define SSL_CTX_set_tlsext_ticket_keys
Definition: boringssl_prefix_symbols.h:210
CBB_add_asn1_uint64
#define CBB_add_asn1_uint64
Definition: boringssl_prefix_symbols.h:1024
cbb_st
Definition: bytestring.h:375
CBB_add_u24_length_prefixed
#define CBB_add_u24_length_prefixed
Definition: boringssl_prefix_symbols.h:1031


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:29