Debian Codegraph @

crt/s2n/tests/cbmc/sources/make_common_datastructures.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <cbmc_proof/make_common_datastructures.h>
17 #include <utils/s2n_safety_macros.h>
18
19 bool s2n_blob_is_bounded(const struct s2n_blob *blob, const size_t max_size) { return (blob->size <= max_size); }
20
21 bool s2n_stuffer_is_bounded(const struct s2n_stuffer *stuffer, const size_t max_size)
22 {
23 return (stuffer->blob.size <= max_size);
24 }
25
26 void cbmc_populate_s2n_blob(struct s2n_blob *blob)
27 {
28 CBMC_ENSURE_REF(blob);
29 if (blob->growable) {
30 blob->data = (blob->allocated == 0) ? NULL : malloc(blob->allocated);
31 } else {
32 blob->data = (blob->size == 0) ? NULL : malloc(blob->size);
33 }
34 }
35
36 struct s2n_blob *cbmc_allocate_s2n_blob()
37 {
38 struct s2n_blob *blob = malloc(sizeof(*blob));
39 cbmc_populate_s2n_blob(blob);
40 return blob;
41 }
42
43 void cbmc_populate_s2n_stuffer(struct s2n_stuffer *stuffer)
44 {
45 CBMC_ENSURE_REF(stuffer);
46 cbmc_populate_s2n_blob(&stuffer->blob);
47 }
48
49 struct s2n_stuffer *cbmc_allocate_s2n_stuffer()
50 {
51 struct s2n_stuffer *stuffer = malloc(sizeof(*stuffer));
52 cbmc_populate_s2n_stuffer(stuffer);
53 return stuffer;
54 }
55
56 const char *ensure_c_str_is_allocated(size_t max_size)
57 {
58 size_t cap;
59 __CPROVER_assume(cap > 0 && cap <= max_size);
60 const char *str = malloc(cap);
61 /* Ensure that its a valid c string. Since all bytes are nondeterminstic, the actual
62 * string length is 0..str_cap
63 */
64 __CPROVER_assume(IMPLIES(str != NULL, str[ cap - 1 ] == '\0'));
65 return str;
66 }
67
68 const char *nondet_c_str_is_allocated(size_t max_size)
69 {
70 size_t cap;
71 __CPROVER_assume(cap > 0 && cap <= max_size);
72 const char *str = malloc(cap);
73 /* Ensure that its a valid c string. Since all bytes are nondeterminstic, the actual
74 * string length is 0..str_cap
75 */
76 __CPROVER_assume(IMPLIES(str != NULL, str[ cap - 1 ] == 0));
77 return str;
78 }
79
80 void cbmc_populate_s2n_stuffer_reservation(struct s2n_stuffer_reservation *reservation)
81 {
82 CBMC_ENSURE_REF(reservation);
83 reservation->stuffer = cbmc_allocate_s2n_stuffer();
84 }
85
86 struct s2n_stuffer_reservation *cbmc_allocate_s2n_stuffer_reservation()
87 {
88 struct s2n_stuffer_reservation *reservation = malloc(sizeof(*reservation));
89 cbmc_populate_s2n_stuffer_reservation(reservation);
90 return reservation;
91 }
92
93 bool s2n_array_is_bounded(const struct s2n_array *array, const size_t max_len, const size_t max_element_size)
94 {
95 return (array->len <= max_len) && (array->element_size <= max_element_size);
96 }
97
98 void cbmc_populate_s2n_array(struct s2n_array *array)
99 {
100 CBMC_ENSURE_REF(array);
101 cbmc_populate_s2n_blob(&array->mem);
102 }
103
104 struct s2n_array *cbmc_allocate_s2n_array()
105 {
106 struct s2n_array *array = malloc(sizeof(*array));
107 cbmc_populate_s2n_array(array);
108 return array;
109 }
110
111 static int nondet_comparator(const void *a, const void *b)
112 {
113 __CPROVER_assert(a != NULL, "a is not NULL");
114 __CPROVER_assert(b != NULL, "b is not NULL");
115 return nondet_int();
116 }
117
118 void cbmc_populate_s2n_dh_params(struct s2n_dh_params *s2n_dh_params)
119 {
120 CBMC_ENSURE_REF(s2n_dh_params);
121 s2n_dh_params->dh = DH_new();
122 }
123
124 struct s2n_dh_params *cbmc_allocate_dh_params()
125 {
126 struct s2n_dh_params *dh_params = malloc(sizeof(*dh_params));
127 cbmc_populate_s2n_dh_params(dh_params);
128 return dh_params;
129 }
130
131 void cbmc_populate_BIGNUM(BIGNUM *bignum)
132 {
133 CBMC_ENSURE_REF(bignum);
134 bignum->d = malloc(sizeof(*(bignum->d)));
135 }
136
137 BIGNUM *cbmc_allocate_BIGNUM()
138 {
139 BIGNUM *bignum = malloc(sizeof(*bignum));
140 cbmc_populate_BIGNUM(bignum);
141 return bignum;
142 }
143
144 void cbmc_populate_EC_GROUP(EC_GROUP *ec_group)
145 {
146 CBMC_ENSURE_REF(ec_group);
147 ec_group->order = cbmc_allocate_BIGNUM();
148 }
149
150 EC_GROUP *cbmc_allocate_EC_GROUP()
151 {
152 EC_GROUP *ec_group = malloc(sizeof(*ec_group));
153 cbmc_populate_EC_GROUP(ec_group);
154 return ec_group;
155 }
156
157 void cbmc_populate_EC_KEY(EC_KEY *ec_key)
158 {
159 CBMC_ENSURE_REF(ec_key);
160 ec_key->group = cbmc_allocate_EC_GROUP();
161 ec_key->priv_key = cbmc_allocate_BIGNUM();
162 }
163
164 EC_KEY *cbmc_allocate_EC_KEY()
165 {
166 EC_KEY *ec_key = malloc(sizeof(*ec_key));
167 cbmc_populate_EC_KEY(ec_key);
168 return ec_key;
169 }
170
171 void cbmc_populate_EVP_PKEY(EVP_PKEY *evp_pkey)
172 {
173 CBMC_ENSURE_REF(evp_pkey);
174 evp_pkey->ec_key = cbmc_allocate_EC_KEY();
175 }
176
177 EVP_PKEY *cbmc_allocate_EVP_PKEY()
178 {
179 EVP_PKEY *evp_pkey = malloc(sizeof(*evp_pkey));
180 cbmc_populate_EVP_PKEY(evp_pkey);
181 return evp_pkey;
182 }
183
184 void cbmc_populate_EVP_PKEY_CTX(EVP_PKEY_CTX *evp_pkey_ctx)
185 {
186 CBMC_ENSURE_REF(evp_pkey_ctx);
187 evp_pkey_ctx->pkey = cbmc_allocate_EVP_PKEY();
188 }
189
190 EVP_PKEY_CTX *cbmc_allocate_EVP_PKEY_CTX()
191 {
192 EVP_PKEY_CTX *evp_pkey_ctx = malloc(sizeof(*evp_pkey_ctx));
193 cbmc_populate_EVP_PKEY_CTX(evp_pkey_ctx);
194 return evp_pkey_ctx;
195 }
196
197 void cbmc_populate_EVP_MD_CTX(EVP_MD_CTX *ctx)
198 {
199 CBMC_ENSURE_REF(ctx);
200 ctx->digest = malloc(sizeof(*(ctx->digest)));
201 ctx->md_data = malloc(EVP_MAX_MD_SIZE);
202 ctx->pctx = cbmc_allocate_EVP_PKEY_CTX();
203 }
204
205 EVP_MD_CTX *cbmc_allocate_EVP_MD_CTX()
206 {
207 EVP_MD_CTX *ctx = malloc(sizeof(*ctx));
208 cbmc_populate_EVP_MD_CTX(ctx);
209 return ctx;
210 }
211
212 void cbmc_populate_s2n_evp_digest(struct s2n_evp_digest *evp_digest) {
213 CBMC_ENSURE_REF(evp_digest);
214 /* `evp_digest->md` is never allocated.
215 * It is always initialized based on the hashing algorithm.
216 * If required, this initialization should be done in the validation function.
217 */
218 evp_digest->ctx = cbmc_allocate_EVP_MD_CTX();
219 }
220
221 struct s2n_evp_digest* cbmc_allocate_s2n_evp_digest()
222 {
223 struct s2n_evp_digest *evp_digest = malloc(sizeof(*evp_digest));
224 cbmc_populate_s2n_evp_digest(evp_digest);
225 return evp_digest;
226 }
227
228 void __CPROVER_file_local_s2n_hash_c_s2n_hash_set_evp_impl(struct s2n_hash_state *);
229
230 void cbmc_populate_s2n_hash_state(struct s2n_hash_state* state)
231 {
232 CBMC_ENSURE_REF(state);
233 cbmc_populate_s2n_evp_digest(&state->digest.high_level.evp);
234 cbmc_populate_s2n_evp_digest(&state->digest.high_level.evp_md5_secondary);
235 /* By populating the EVP fields, we have chosen to test the EVP implementation */
236 __CPROVER_file_local_s2n_hash_c_s2n_hash_set_evp_impl(state);
237 }
238
239 struct s2n_hash_state* cbmc_allocate_s2n_hash_state()
240 {
241 struct s2n_hash_state *state = malloc(sizeof(*state));
242 cbmc_populate_s2n_hash_state(state);
243 return state;
244 }
245
246 void cbmc_populate_s2n_hmac_state(struct s2n_hmac_state *state)
247 {
248 CBMC_ENSURE_REF(state);
249 cbmc_populate_s2n_hash_state(&state->inner);
250 cbmc_populate_s2n_hash_state(&state->inner_just_key);
251 cbmc_populate_s2n_hash_state(&state->outer);
252 cbmc_populate_s2n_hash_state(&state->outer_just_key);
253 }
254
255 struct s2n_hmac_state* cbmc_allocate_s2n_hmac_state()
256 {
257 struct s2n_hmac_state *state = malloc(sizeof(*state));
258 cbmc_populate_s2n_hmac_state(state);
259 return state;
260 }
261
262 void cbmc_populate_s2n_hmac_evp_backup(struct s2n_hmac_evp_backup *backup)
263 {
264 CBMC_ENSURE_REF(backup);
265 cbmc_populate_s2n_evp_digest(&backup->inner.evp);
266 cbmc_populate_s2n_evp_digest(&backup->inner.evp_md5_secondary);
267 cbmc_populate_s2n_evp_digest(&backup->inner_just_key.evp);
268 cbmc_populate_s2n_evp_digest(&backup->inner_just_key.evp_md5_secondary);
269 cbmc_populate_s2n_evp_digest(&backup->outer.evp);
270 cbmc_populate_s2n_evp_digest(&backup->outer.evp_md5_secondary);
271 cbmc_populate_s2n_evp_digest(&backup->outer_just_key.evp);
272 cbmc_populate_s2n_evp_digest(&backup->outer_just_key.evp_md5_secondary);
273 }
274
275 struct s2n_hmac_evp_backup* cbmc_allocate_s2n_hmac_evp_backup()
276 {
277 struct s2n_hmac_evp_backup *backup = malloc(sizeof(*backup));
278 cbmc_populate_s2n_hmac_evp_backup(backup);
279 return backup;
280 }
281
282 void cbmc_populate_s2n_map(struct s2n_map *s2n_map)
283 {
284 CBMC_ENSURE_REF(s2n_map);
285 s2n_map->table = malloc(sizeof(*(s2n_map->table)));
286 if (s2n_map->table != NULL) {
287 cbmc_populate_s2n_blob(&(s2n_map->table->key));
288 cbmc_populate_s2n_blob(&(s2n_map->table->value));
289 }
290 }
291
292 struct s2n_map *cbmc_allocate_s2n_map()
293 {
294 struct s2n_map *s2n_map = malloc(sizeof(*s2n_map));
295 cbmc_populate_s2n_map(s2n_map);
296 return s2n_map;
297 }
298
299 struct s2n_cipher_preferences *cbmc_allocate_s2n_cipher_preferences()
300 {
301 struct s2n_cipher_preferences *s2n_cipher_preferences = malloc(sizeof(*s2n_cipher_preferences));
302 return s2n_cipher_preferences;
303 }
304
305 struct s2n_kem_preferences *cbmc_allocate_s2n_kem_preferences()
306 {
307 struct s2n_kem_preferences *s2n_kem_preferences = malloc(sizeof(*s2n_kem_preferences));
308 return s2n_kem_preferences;
309 }
310
311 struct s2n_signature_preferences *cbmc_allocate_s2n_signature_preferences()
312 {
313 struct s2n_signature_preferences *s2n_signature_preferences = malloc(sizeof(*s2n_signature_preferences));
314 return s2n_signature_preferences;
315 }
316
317 struct s2n_ecc_preferences *cbmc_allocate_s2n_ecc_preferences()
318 {
319 struct s2n_ecc_preferences *s2n_ecc_preferences = malloc(sizeof(*s2n_ecc_preferences));
320 return s2n_ecc_preferences;
321 }
322
323 struct s2n_security_policy *cbmc_allocate_s2n_security_policy()
324 {
325 struct s2n_security_policy *s2n_security_policy = malloc(sizeof(*s2n_security_policy));
326 PTR_ENSURE_REF(s2n_security_policy);
327 s2n_security_policy->cipher_preferences = cbmc_allocate_s2n_cipher_preferences();
328 s2n_security_policy->kem_preferences = cbmc_allocate_s2n_kem_preferences();
329 s2n_security_policy->signature_preferences = cbmc_allocate_s2n_signature_preferences();
330 s2n_security_policy->certificate_signature_preferences = cbmc_allocate_s2n_signature_preferences();
331 s2n_security_policy->ecc_preferences = cbmc_allocate_s2n_ecc_preferences();
332 return s2n_security_policy;
333 }
334
335 X509_STORE *cbmc_allocate_X509_STORE()
336 {
337 X509_STORE *x509_store = malloc(sizeof(*x509_store));
338 return x509_store;
339 }
340
341 void cbmc_populate_s2n_x509_trust_store(struct s2n_x509_trust_store *s2n_x509_trust_store)
342 {
343 CBMC_ENSURE_REF(s2n_x509_trust_store);
344 s2n_x509_trust_store->trust_store = cbmc_allocate_X509_STORE();
345 }
346
347 struct s2n_config *cbmc_allocate_s2n_config()
348 {
349 struct s2n_config *s2n_config = malloc(sizeof(*s2n_config));
350 PTR_ENSURE_REF(s2n_config);
351 s2n_config->dhparams = cbmc_allocate_dh_params();
352 s2n_config->domain_name_to_cert_map = cbmc_allocate_s2n_map();
353 /* `s2n_config->default_certs_by_type` is never allocated.
354 * If required, this initialization should be done in the proof harness.
355 */
356 cbmc_populate_s2n_blob(&s2n_config->application_protocols);
357 s2n_config->security_policy = cbmc_allocate_s2n_security_policy();
358 s2n_config->sys_clock_ctx = malloc(sizeof(*(s2n_config->sys_clock_ctx)));
359 s2n_config->monotonic_clock_ctx = malloc(sizeof(*(s2n_config->monotonic_clock_ctx)));
360 s2n_config->client_hello_cb = malloc(sizeof(*(s2n_config->client_hello_cb))); /* Function pointer. */
361 s2n_config->client_hello_cb_ctx = malloc(sizeof(*(s2n_config->client_hello_cb_ctx)));
362 s2n_config->ticket_keys = cbmc_allocate_s2n_array();
363 s2n_config->cache_store_data = malloc(sizeof(*(s2n_config->cache_store_data)));
364 s2n_config->cache_retrieve_data = malloc(sizeof(*(s2n_config->cache_retrieve_data)));
365 s2n_config->cache_delete_data = malloc(sizeof(*(s2n_config->cache_delete_data)));
366 s2n_config->data_for_verify_host = malloc(sizeof(*(s2n_config->data_for_verify_host)));
367 cbmc_populate_s2n_x509_trust_store(&s2n_config->trust_store);
368 s2n_config->psk_selection_ctx = malloc(sizeof(*(s2n_config->psk_selection_ctx)));
369 s2n_config->key_log_ctx = malloc(sizeof(*(s2n_config->key_log_ctx)));
370 s2n_config->session_ticket_ctx = malloc(sizeof(*(s2n_config->session_ticket_ctx)));
371 return s2n_config;
372 }
373
374 void cbmc_populate_s2n_pkey(struct s2n_pkey *s2n_pkey)
375 {
376 CBMC_ENSURE_REF(s2n_pkey);
377 /* `s2n_pkey->pkey`
378 * `s2n_pkey->size`
379 * `s2n_pkey->sign`
380 * `s2n_pkey->verify`
381 * `s2n_pkey->encrypt`
382 * `s2n_pkey->decrypt`
383 * If required, these initializations should be done in the proof harness.
384 */
385 }
386
387 struct s2n_pkey *cbmc_allocate_s2n_pkey()
388 {
389 struct s2n_pkey *s2n_pkey = malloc(sizeof(*s2n_pkey));
390 cbmc_populate_s2n_pkey(s2n_pkey);
391 return s2n_pkey;
392 }
393
394 void cbmc_populate_s2n_ecc_evp_params(struct s2n_ecc_evp_params *s2n_ecc_evp_params)
395 {
396 CBMC_ENSURE_REF(s2n_ecc_evp_params);
397 /* `s2n_ecc_evp_params->negotiated_curve` is never allocated.
398 * If required, this initialization should be done in the proof harness.
399 */
400 s2n_ecc_evp_params->evp_pkey = malloc(sizeof(*(s2n_ecc_evp_params->evp_pkey)));
401 }
402
403 struct s2n_ecc_named_curve *cbmc_allocate_s2n_ecc_named_curve()
404 {
405 struct s2n_ecc_named_curve *s2n_ecc_named_curve = malloc(sizeof(*s2n_ecc_named_curve));
406 /* `s2n_ecc_named_curve->name`
407 * `s2n_ecc_named_curve->generate_key` are never allocated.
408 * If required, these initializations should be done in the proof harness.
409 */
410 return s2n_ecc_named_curve;
411 }
412
413 struct s2n_kem *cbmc_allocate_s2n_kem()
414 {
415 struct s2n_kem *s2n_kem = malloc(sizeof(*s2n_kem));
416 /* `s2n_kem->name`
417 * `s2n_kem->generate_keypair`
418 * `s2n_kem->encapsulate`
419 * `s2n_kem->decapsulate` are never allocated.
420 * If required, these initializations should be done in the proof harness.
421 */
422 }
423
424 struct s2n_kem_group *cbmc_allocate_s2n_kem_group()
425 {
426 struct s2n_kem_group *s2n_kem_group = malloc(sizeof(*s2n_kem_group));
427 PTR_ENSURE_REF(s2n_kem_group);
428 /* `s2n_kem_group->name` is never allocated.
429 * If required, this initialization should be done in the proof harness.
430 */
431 s2n_kem_group->curve = cbmc_allocate_s2n_ecc_named_curve();
432 s2n_kem_group->kem = cbmc_allocate_s2n_kem();
433 return s2n_kem_group;
434 }
435
436 void cbmc_populate_s2n_kem_params(struct s2n_kem_params *s2n_kem_params)
437 {
438 CBMC_ENSURE_REF(s2n_kem_params);
439 s2n_kem_params->kem = cbmc_allocate_s2n_kem();
440 cbmc_populate_s2n_blob(&(s2n_kem_params->public_key));
441 cbmc_populate_s2n_blob(&(s2n_kem_params->private_key));
442 cbmc_populate_s2n_blob(&(s2n_kem_params->shared_secret));
443 }
444
445 void cbmc_populate_s2n_kem_group_params(struct s2n_kem_group_params *s2n_kem_group_params)
446 {
447 CBMC_ENSURE_REF(s2n_kem_group_params);
448 s2n_kem_group_params->kem_group = cbmc_allocate_s2n_kem_group();
449 cbmc_populate_s2n_kem_params(&(s2n_kem_group_params->kem_params));
450 cbmc_populate_s2n_ecc_evp_params(&(s2n_kem_group_params->ecc_params));
451 }
452
453 struct s2n_kem_group_params *cbmc_allocate_s2n_kem_group_params()
454 {
455 struct s2n_kem_group_params *s2n_kem_group_params = malloc(sizeof(*s2n_kem_group_params));
456 cbmc_populate_s2n_kem_group_params(s2n_kem_group_params);
457 return s2n_kem_group_params;
458 }
459
460 void cbmc_populate_s2n_signature_scheme(struct s2n_signature_scheme *s2n_signature_scheme)
461 {
462 CBMC_ENSURE_REF(s2n_signature_scheme);
463 s2n_signature_scheme->signature_curve = cbmc_allocate_s2n_ecc_named_curve();
464 }
465
466 struct s2n_signature_scheme *cbmc_allocate_s2n_signature_scheme()
467 {
468 struct s2n_signature_scheme *s2n_signature_scheme = malloc(sizeof(*s2n_signature_scheme));
469 cbmc_populate_s2n_signature_scheme(s2n_signature_scheme);
470 return s2n_signature_scheme;
471 }
472
473 struct s2n_kex *cbmc_allocate_s2n_kex()
474 {
475 struct s2n_kex *s2n_kex = malloc(sizeof(*s2n_kex));
476 /* `s2n_kex->hybrid`
477 * `s2n_kex->connection_supported`
478 * `s2n_kex->configure_connection`
479 * `s2n_kex->server_key_recv_read_data`
480 * `s2n_kex->server_key_recv_parse_data`
481 * `s2n_kex->server_key_send`
482 * `s2n_kex->client_key_recv`
483 * `s2n_kex->client_key_send`
484 * `s2n_kex->prf` are never allocated.
485 * If required, these initializations should be done in the proof harness.
486 */
487 return s2n_kex;
488 }
489
490 struct s2n_cipher *cbmc_allocate_s2n_cipher()
491 {
492 struct s2n_cipher *s2n_cipher = malloc(sizeof(*s2n_cipher));
493 /* `s2n_cipher->io.stream`
494 * `s2n_cipher->io.aead`
495 * `s2n_cipher->io.cbc`
496 * `s2n_cipher->io.comp`
497 * `s2n_cipher->is_available`
498 * `s2n_cipher->init`
499 * `s2n_cipher->set_decryption_key`
500 * `s2n_cipher->set_encryption_key`
501 * `s2n_cipher->destroy_key` are never allocated.
502 * If required, these initializations should be done in the proof harness.
503 */
504 return s2n_cipher;
505 }
506
507 struct s2n_record_algorithm *cbmc_allocate_s2n_record_algorithm()
508 {
509 struct s2n_record_algorithm *s2n_record_algorithm = malloc(sizeof(*s2n_record_algorithm));
510 PTR_ENSURE_REF(s2n_record_algorithm);
511 s2n_record_algorithm->cipher = cbmc_allocate_s2n_cipher();
512 return s2n_record_algorithm;
513 }
514
515 struct s2n_cipher_suite *cbmc_allocate_s2n_cipher_suite()
516 {
517 struct s2n_cipher_suite *s2n_cipher_suite = malloc(sizeof(*s2n_cipher_suite));
518 PTR_ENSURE_REF(s2n_cipher_suite);
519 /* `s2n_cipher_suite->name`
520 * `s2n_cipher_suite->all_record_algs`
521 * `s2n_cipher_suite->sslv3_cipher_suite` are never allocated.
522 * If required, these initializations should be done in the proof harness.
523 */
524 s2n_cipher_suite->key_exchange_alg = cbmc_allocate_s2n_kex();
525 s2n_cipher_suite->record_alg = cbmc_allocate_s2n_record_algorithm();
526 s2n_cipher_suite->sslv3_record_alg = cbmc_allocate_s2n_record_algorithm();
527 return s2n_cipher_suite;
528 }
529
530 void cbmc_populate_s2n_session_key(struct s2n_session_key *s2n_session_key)
531 {
532 CBMC_ENSURE_REF(s2n_session_key);
533 s2n_session_key->evp_cipher_ctx = malloc(sizeof(*(s2n_session_key->evp_cipher_ctx)));
534 }
535
536 void cbmc_populate_s2n_kex_parameters(struct s2n_kex_parameters *s2n_kex_parameters)
537 {
538 cbmc_populate_s2n_dh_params(&(s2n_kex_parameters->server_dh_params));
539 cbmc_populate_s2n_ecc_evp_params(&(s2n_kex_parameters->server_ecc_evp_params));
540 /* `s2n_crypto_parameters->mutually_supported_curves`
541 * `s2n_crypto_parameters->client_ecc_evp_params`
542 * `s2n_crypto_parameters->client_kem_group_params`
543 * `s2n_crypto_parameters->mutually_supported_kem_groups` are never allocated.
544 * If required, these initializations should be done in the proof harness.
545 */
546 cbmc_populate_s2n_kem_group_params(&(s2n_kex_parameters->server_kem_group_params));
547 cbmc_populate_s2n_kem_params(&(s2n_kex_parameters->kem_params));
548 cbmc_populate_s2n_blob(&(s2n_kex_parameters->client_key_exchange_message));
549 cbmc_populate_s2n_blob(&(s2n_kex_parameters->client_pq_kem_extension));
550 }
551
552 void cbmc_populate_s2n_crypto_parameters(struct s2n_crypto_parameters *s2n_crypto_parameters)
553 {
554 CBMC_ENSURE_REF(s2n_crypto_parameters);
555 s2n_crypto_parameters->cipher_suite = cbmc_allocate_s2n_cipher_suite();
556 cbmc_populate_s2n_session_key(&(s2n_crypto_parameters->client_key));
557 cbmc_populate_s2n_session_key(&(s2n_crypto_parameters->server_key));
558 cbmc_populate_s2n_hmac_state(&(s2n_crypto_parameters->client_record_mac));
559 cbmc_populate_s2n_hmac_state(&(s2n_crypto_parameters->server_record_mac));
560 }
561
562 struct s2n_crypto_parameters *cbmc_allocate_s2n_crypto_parameters()
563 {
564 struct s2n_crypto_parameters *s2n_crypto_parameters = malloc(sizeof(*s2n_crypto_parameters));
565 cbmc_populate_s2n_crypto_parameters(s2n_crypto_parameters);
566 return s2n_crypto_parameters;
567 }
568
569 void cbmc_populate_s2n_cert(struct s2n_cert *s2n_cert)
570 {
571 CBMC_ENSURE_REF(s2n_cert);
572 cbmc_populate_s2n_blob(&(s2n_cert->raw));
573 /* `s2n_cert->next` is never allocated.
574 * If required, this initialization should be done in the proof harness.
575 */
576 }
577
578 struct s2n_cert *cbmc_allocate_s2n_cert()
579 {
580 struct s2n_cert *s2n_cert = malloc(sizeof(*s2n_cert));
581 cbmc_populate_s2n_cert(s2n_cert);
582 return s2n_cert;
583 }
584
585 void cbmc_populate_s2n_cert_chain(struct s2n_cert_chain *s2n_cert_chain)
586 {
587 CBMC_ENSURE_REF(s2n_cert_chain);
588 s2n_cert_chain->head = cbmc_allocate_s2n_cert();
589 }
590
591 struct s2n_cert_chain *cbmc_allocate_s2n_cert_chain()
592 {
593 struct s2n_cert_chain *s2n_cert_chain = malloc(sizeof(*s2n_cert_chain));
594 cbmc_populate_s2n_cert_chain(s2n_cert_chain);
595 return s2n_cert_chain;
596 }
597
598 void cbmc_populate_s2n_cert_chain_and_key(struct s2n_cert_chain_and_key *s2n_cert_chain_and_key)
599 {
600 CBMC_ENSURE_REF(s2n_cert_chain_and_key);
601 s2n_cert_chain_and_key->cert_chain = cbmc_allocate_s2n_cert_chain();
602 s2n_cert_chain_and_key->private_key = cbmc_allocate_s2n_pkey();
603 cbmc_populate_s2n_blob(&(s2n_cert_chain_and_key->ocsp_status));
604 cbmc_populate_s2n_blob(&(s2n_cert_chain_and_key->sct_list));
605 s2n_cert_chain_and_key->san_names = cbmc_allocate_s2n_array();
606 s2n_cert_chain_and_key->cn_names = cbmc_allocate_s2n_array();
607 s2n_cert_chain_and_key->context = malloc(sizeof(*(s2n_cert_chain_and_key->context)));
608 }
609
610 void cbmc_populate_s2n_handshake_parameters(struct s2n_handshake_parameters *s2n_handshake_parameters)
611 {
612 CBMC_ENSURE_REF(s2n_handshake_parameters);
613 cbmc_populate_s2n_pkey(&(s2n_handshake_parameters->server_public_key));
614 cbmc_populate_s2n_pkey(&(s2n_handshake_parameters->client_public_key));
615 cbmc_populate_s2n_blob(&(s2n_handshake_parameters->client_cert_chain));
616 cbmc_populate_s2n_cert_chain_and_key(s2n_handshake_parameters->our_chain_and_key);
617 s2n_handshake_parameters->server_cert_sig_scheme = cbmc_allocate_s2n_signature_scheme();
618 s2n_handshake_parameters->client_cert_sig_scheme = cbmc_allocate_s2n_signature_scheme();
619 /* `s2n_handshake_parameters->exact_sni_matches`
620 * `s2n_handshake_parameters->wc_sni_matches` are never allocated.
621 * If required, these initializations should be done in the proof harness.
622 */
623 }
624
625 void cbmc_populate_s2n_early_data_config(struct s2n_early_data_config *s2n_early_data_config)
626 {
627 CBMC_ENSURE_REF(s2n_early_data_config);
628 s2n_early_data_config->cipher_suite = cbmc_allocate_s2n_cipher_suite();
629 cbmc_populate_s2n_blob(&(s2n_early_data_config->application_protocol));
630 cbmc_populate_s2n_blob(&(s2n_early_data_config->context));
631 }
632
633 void cbmc_populate_s2n_psk(struct s2n_psk *s2n_psk)
634 {
635 CBMC_ENSURE_REF(s2n_psk);
636 cbmc_populate_s2n_blob(&(s2n_psk->identity));
637 cbmc_populate_s2n_blob(&(s2n_psk->secret));
638 cbmc_populate_s2n_blob(&(s2n_psk->early_secret));
639 cbmc_populate_s2n_early_data_config(&(s2n_psk->early_data_config));
640 }
641
642 struct s2n_psk *cbmc_allocate_s2n_psk()
643 {
644 struct s2n_psk *s2n_psk = malloc(sizeof(*s2n_psk));
645 cbmc_populate_s2n_psk(s2n_psk);
646 return s2n_psk;
647 }
648
649 void cbmc_populate_s2n_psk_parameters(struct s2n_psk_parameters *s2n_psk_parameters)
650 {
651 CBMC_ENSURE_REF(s2n_psk_parameters);
652 cbmc_populate_s2n_blob(&(s2n_psk_parameters->psk_list.mem));
653 s2n_psk_parameters->chosen_psk = cbmc_allocate_s2n_psk();
654 }
655
656 void cbmc_populate_s2n_prf_working_space(struct s2n_prf_working_space *s2n_prf_working_space)
657 {
658 CBMC_ENSURE_REF(s2n_prf_working_space);
659 /* `s2n_prf_working_space->tls.p_hash_hmac_impl` is never allocated.
660 * It is always initialized based on the hashing algorithm.
661 * If required, this initialization should be done in the validation function.
662 */
663 cbmc_populate_s2n_hmac_state(&(s2n_prf_working_space->p_hash.s2n_hmac));
664 }
665
666 struct s2n_prf_working_space* cbmc_allocate_s2n_prf_working_space()
667 {
668 struct s2n_prf_working_space *workspace = malloc(sizeof(*workspace));
669 cbmc_populate_s2n_prf_working_space(workspace);
670 return workspace;
671 }
672
673 void cbmc_populate_s2n_handshake(struct s2n_handshake *s2n_handshake)
674 {
675 CBMC_ENSURE_REF(s2n_handshake);
676 cbmc_populate_s2n_stuffer(&(s2n_handshake->io));
677 cbmc_populate_s2n_hash_state(&(s2n_handshake->hashes->md5));
678 cbmc_populate_s2n_hash_state(&(s2n_handshake->hashes->sha1));
679 cbmc_populate_s2n_hash_state(&(s2n_handshake->hashes->sha224));
680 cbmc_populate_s2n_hash_state(&(s2n_handshake->hashes->sha256));
681 cbmc_populate_s2n_hash_state(&(s2n_handshake->hashes->sha384));
682 cbmc_populate_s2n_hash_state(&(s2n_handshake->hashes->sha512));
683 cbmc_populate_s2n_hash_state(&(s2n_handshake->hashes->md5_sha1));
684 cbmc_populate_s2n_hash_state(&(s2n_handshake->hashes->hash_workspace));
685 /* `s2n_handshake->early_data_async_state.conn` is never allocated.
686 * If required, this initialization should be done in the validation function.
687 */
688 }
689
690 void cbmc_populate_s2n_client_hello(struct s2n_client_hello *s2n_client_hello)
691 {
692 CBMC_ENSURE_REF(s2n_client_hello);
693 cbmc_populate_s2n_blob(&(s2n_client_hello->raw_message));
694 cbmc_populate_s2n_blob(&(s2n_client_hello->cipher_suites));
695 }
696
697 void cbmc_populate_s2n_x509_validator(struct s2n_x509_validator *s2n_x509_validator)
698 {
699 CBMC_ENSURE_REF(s2n_x509_validator);
700 s2n_x509_validator->trust_store = malloc(sizeof(*(s2n_x509_validator->trust_store)));
701 if (s2n_x509_validator->trust_store != NULL) {
702 s2n_x509_validator->trust_store->trust_store = malloc(sizeof(*(s2n_x509_validator->trust_store->trust_store)));
703 }
704 s2n_x509_validator->store_ctx = malloc(sizeof(*(s2n_x509_validator->store_ctx)));
705 s2n_x509_validator->cert_chain_from_wire = malloc(sizeof(*(s2n_x509_validator->cert_chain_from_wire)));
706 }
707
708 void cbmc_populate_s2n_ticket_fields(struct s2n_ticket_fields *s2n_ticket_fields)
709 {
710 CBMC_ENSURE_REF(s2n_ticket_fields);
711 cbmc_populate_s2n_blob(&(s2n_ticket_fields->session_secret));
712 }
713
714 void cbmc_populate_s2n_connection(struct s2n_connection *s2n_connection)
715 {
716 CBMC_ENSURE_REF(s2n_connection);
717 s2n_connection->config = cbmc_allocate_s2n_config();
718 s2n_connection->security_policy_override = cbmc_allocate_s2n_security_policy();
719 s2n_connection->context = malloc(sizeof(*(s2n_connection->context)));
720 s2n_connection->secret_cb = malloc(sizeof(*(s2n_connection->secret_cb))); /* Function pointer. */
721 s2n_connection->secret_cb_context = malloc(sizeof(*(s2n_connection->secret_cb_context)));
722 s2n_connection->send = malloc(sizeof(*(s2n_connection->send))); /* Function pointer. */
723 s2n_connection->recv = malloc(sizeof(*(s2n_connection->recv))); /* Function pointer. */
724 s2n_connection->send_io_context = malloc(sizeof(*(s2n_connection->secret_cb)));
725 s2n_connection->recv_io_context = malloc(sizeof(*(s2n_connection->secret_cb)));
726 cbmc_populate_s2n_crypto_parameters(s2n_connection->initial);
727 cbmc_populate_s2n_crypto_parameters(s2n_connection->secure);
728 cbmc_populate_s2n_kex_parameters(&(s2n_connection->kex_params));
729 s2n_connection->client = cbmc_allocate_s2n_crypto_parameters();
730 s2n_connection->server = cbmc_allocate_s2n_crypto_parameters();
731 cbmc_populate_s2n_handshake_parameters(&(s2n_connection->handshake_params));
732 cbmc_populate_s2n_psk_parameters(&(s2n_connection->psk_params));
733 s2n_connection->prf_space = cbmc_allocate_s2n_prf_working_space();
734 cbmc_populate_s2n_stuffer(&(s2n_connection->header_in));
735 cbmc_populate_s2n_stuffer(&(s2n_connection->in));
736 cbmc_populate_s2n_stuffer(&(s2n_connection->out));
737 cbmc_populate_s2n_stuffer(&(s2n_connection->alert_in));
738 cbmc_populate_s2n_handshake(&(s2n_connection->handshake));
739 cbmc_populate_s2n_blob(&(s2n_connection->status_response));
740 cbmc_populate_s2n_blob(&(s2n_connection->ct_response));
741 cbmc_populate_s2n_blob(&(s2n_connection->our_quic_transport_parameters));
742 cbmc_populate_s2n_blob(&(s2n_connection->peer_quic_transport_parameters));
743 cbmc_populate_s2n_client_hello(&(s2n_connection->client_hello));
744 cbmc_populate_s2n_x509_validator(&(s2n_connection->x509_validator));
745 s2n_connection->verify_host_fn = malloc(sizeof(*(s2n_connection->verify_host_fn))); /* Function pointer. */
746 s2n_connection->data_for_verify_host = malloc(sizeof(*(s2n_connection->data_for_verify_host)));
747 cbmc_populate_s2n_blob(&(s2n_connection->client_ticket));
748 cbmc_populate_s2n_ticket_fields(&(s2n_connection->tls13_ticket_fields));
749 cbmc_populate_s2n_stuffer(&(s2n_connection->client_ticket_to_decrypt));
750 cbmc_populate_s2n_blob(&(s2n_connection->application_protocols_overridden));
751 cbmc_populate_s2n_blob(&(s2n_connection->cookie));
752 cbmc_populate_s2n_blob(&(s2n_connection->server_early_data_context));
753 }
754
755 struct s2n_connection *cbmc_allocate_s2n_connection()
756 {
757 struct s2n_connection *s2n_connection = malloc(sizeof(*s2n_connection));
758 cbmc_populate_s2n_connection(s2n_connection);
759 return s2n_connection;
760 }
761
762 struct s2n_socket_read_io_context *cbmc_allocate_s2n_socket_read_io_context()
763 {
764 struct s2n_socket_read_io_context *s2n_socket_read_io_context = malloc(sizeof(*s2n_socket_read_io_context));
765 return s2n_socket_read_io_context;
766 }
767
768 struct s2n_socket_write_io_context *cbmc_allocate_s2n_socket_write_io_context()
769 {
770 struct s2n_socket_write_io_context *s2n_socket_write_io_context = malloc(sizeof(*s2n_socket_write_io_context));
771 return s2n_socket_write_io_context;
772 }