Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/ssl/test/test_state.cc

raw
1 // Copyright (c) 2018, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include "test_state.h"
5
6 #include <openssl/ssl.h>
7
8 #include "../../crypto/internal.h"
9 #include "../internal.h"
10
11 using namespace bssl;
12
13 static CRYPTO_once_t g_once = CRYPTO_ONCE_INIT;
14 static int g_state_index = 0;
15 // Some code treats the zero time special, so initialize the clock to a
16 // non-zero time.
17 static timeval g_clock = { 1234, 1234 };
18
19 static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
20 int index, long argl, void *argp) {
21 delete ((TestState *)ptr);
22 }
23
24 static void init_once() {
25 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
26 if (g_state_index < 0) {
27 abort();
28 }
29 }
30
31 struct timeval *GetClock() {
32 CRYPTO_once(&g_once, init_once);
33 return &g_clock;
34 }
35
36 void AdvanceClock(unsigned seconds) {
37 CRYPTO_once(&g_once, init_once);
38 g_clock.tv_sec += seconds;
39 }
40
41 bool SetTestState(SSL *ssl, std::unique_ptr<TestState> state) {
42 CRYPTO_once(&g_once, init_once);
43 // |SSL_set_ex_data| takes ownership of |state| only on success.
44 if (SSL_set_ex_data(ssl, g_state_index, state.get()) == 1) {
45 state.release();
46 return true;
47 }
48 return false;
49 }
50
51 TestState *GetTestState(const SSL *ssl) {
52 CRYPTO_once(&g_once, init_once);
53 return (TestState *)SSL_get_ex_data(ssl, g_state_index);
54 }
55
56 static void ssl_ctx_add_session(SSL_SESSION *session, void *void_param) {
57 SSL_CTX *ctx = reinterpret_cast<SSL_CTX *>(void_param);
58 UniquePtr<SSL_SESSION> new_session = SSL_SESSION_dup(
59 session, SSL_SESSION_INCLUDE_NONAUTH | SSL_SESSION_INCLUDE_TICKET);
60 if (new_session != nullptr) {
61 SSL_CTX_add_session(ctx, new_session.get());
62 }
63 }
64
65 void CopySessions(SSL_CTX *dst, const SSL_CTX *src) {
66 lh_SSL_SESSION_doall_arg(src->sessions, ssl_ctx_add_session, dst);
67 }
68
69 static void push_session(SSL_SESSION *session, void *arg) {
70 auto s = reinterpret_cast<std::vector<SSL_SESSION *> *>(arg);
71 s->push_back(session);
72 }
73
74 bool SerializeContextState(SSL_CTX *ctx, CBB *cbb) {
75 CBB out, ctx_sessions, ticket_keys;
76 uint8_t keys[48];
77 if (!CBB_add_u24_length_prefixed(cbb, &out) ||
78 !CBB_add_u16(&out, 0 /* version */) ||
79 !SSL_CTX_get_tlsext_ticket_keys(ctx, &keys, sizeof(keys)) ||
80 !CBB_add_u8_length_prefixed(&out, &ticket_keys) ||
81 !CBB_add_bytes(&ticket_keys, keys, sizeof(keys)) ||
82 !CBB_add_asn1(&out, &ctx_sessions, CBS_ASN1_SEQUENCE)) {
83 return false;
84 }
85 std::vector<SSL_SESSION *> sessions;
86 lh_SSL_SESSION_doall_arg(ctx->sessions, push_session, &sessions);
87 for (const auto &sess : sessions) {
88 if (!ssl_session_serialize(sess, &ctx_sessions)) {
89 return false;
90 }
91 }
92 return CBB_flush(cbb);
93 }
94
95 bool DeserializeContextState(CBS *cbs, SSL_CTX *ctx) {
96 CBS in, sessions, ticket_keys;
97 uint16_t version;
98 constexpr uint16_t kVersion = 0;
99 if (!CBS_get_u24_length_prefixed(cbs, &in) ||
100 !CBS_get_u16(&in, &version) ||
101 version > kVersion ||
102 !CBS_get_u8_length_prefixed(&in, &ticket_keys) ||
103 !SSL_CTX_set_tlsext_ticket_keys(ctx, CBS_data(&ticket_keys),
104 CBS_len(&ticket_keys)) ||
105 !CBS_get_asn1(&in, &sessions, CBS_ASN1_SEQUENCE)) {
106 return false;
107 }
108 while (CBS_len(&sessions)) {
109 UniquePtr<SSL_SESSION> session =
110 SSL_SESSION_parse(&sessions, ctx->x509_method, ctx->pool);
111 if (!session) {
112 return false;
113 }
114 SSL_CTX_add_session(ctx, session.get());
115 }
116 return true;
117 }
118
119 bool TestState::Serialize(CBB *cbb) const {
120 CBB out, pending, text;
121 if (!CBB_add_u24_length_prefixed(cbb, &out) ||
122 !CBB_add_u16(&out, 0 /* version */) ||
123 !CBB_add_u24_length_prefixed(&out, &pending) ||
124 (pending_session &&
125 !ssl_session_serialize(pending_session.get(), &pending)) ||
126 !CBB_add_u16_length_prefixed(&out, &text) ||
127 !CBB_add_bytes(
128 &text, reinterpret_cast<const uint8_t *>(msg_callback_text.data()),
129 msg_callback_text.length()) ||
130 !CBB_add_asn1_uint64(&out, g_clock.tv_sec) ||
131 !CBB_add_asn1_uint64(&out, g_clock.tv_usec) ||
132 !CBB_flush(cbb)) {
133 return false;
134 }
135 return true;
136 }
137
138 std::unique_ptr<TestState> TestState::Deserialize(CBS *cbs, SSL_CTX *ctx) {
139 CBS in, pending_session, text;
140 std::unique_ptr<TestState> out_state(new TestState());
141 uint16_t version;
142 constexpr uint16_t kVersion = 0;
143 uint64_t sec, usec;
144 if (!CBS_get_u24_length_prefixed(cbs, &in) ||
145 !CBS_get_u16(&in, &version) ||
146 version > kVersion ||
147 !CBS_get_u24_length_prefixed(&in, &pending_session) ||
148 !CBS_get_u16_length_prefixed(&in, &text)) {
149 return nullptr;
150 }
151 if (CBS_len(&pending_session)) {
152 out_state->pending_session = SSL_SESSION_parse(
153 &pending_session, ctx->x509_method, ctx->pool);
154 if (!out_state->pending_session) {
155 return nullptr;
156 }
157 }
158 out_state->msg_callback_text = std::string(
159 reinterpret_cast<const char *>(CBS_data(&text)), CBS_len(&text));
160 // TODO(2020-05-01): Make this unconditional & merge into above.
161 if (CBS_len(&in) > 0) {
162 if (!CBS_get_asn1_uint64(&in, &sec) ||
163 !CBS_get_asn1_uint64(&in, &usec)) {
164 return nullptr;
165 }
166 g_clock.tv_sec = sec;
167 g_clock.tv_usec = usec;
168 }
169 return out_state;
170 }