xref: /aosp_15_r20/external/cronet/net/test/ssl_test_util.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/test/ssl_test_util.h"
6 
7 #include <string>
8 #include <string_view>
9 
10 #include "third_party/boringssl/src/include/openssl/hpke.h"
11 
12 namespace net {
13 
MakeTestEchKeys(std::string_view public_name,size_t max_name_len,std::vector<uint8_t> * ech_config_list)14 bssl::UniquePtr<SSL_ECH_KEYS> MakeTestEchKeys(
15     std::string_view public_name,
16     size_t max_name_len,
17     std::vector<uint8_t>* ech_config_list) {
18   bssl::ScopedEVP_HPKE_KEY key;
19   if (!EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256())) {
20     return nullptr;
21   }
22 
23   uint8_t* ech_config;
24   size_t ech_config_len;
25   if (!SSL_marshal_ech_config(&ech_config, &ech_config_len,
26                               /*config_id=*/1, key.get(),
27                               std::string(public_name).c_str(), max_name_len)) {
28     return nullptr;
29   }
30   bssl::UniquePtr<uint8_t> scoped_ech_config(ech_config);
31 
32   uint8_t* ech_config_list_raw;
33   size_t ech_config_list_len;
34   bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new());
35   if (!keys ||
36       !SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, ech_config,
37                         ech_config_len, key.get()) ||
38       !SSL_ECH_KEYS_marshal_retry_configs(keys.get(), &ech_config_list_raw,
39                                           &ech_config_list_len)) {
40     return nullptr;
41   }
42   bssl::UniquePtr<uint8_t> scoped_ech_config_list(ech_config_list_raw);
43 
44   ech_config_list->assign(ech_config_list_raw,
45                           ech_config_list_raw + ech_config_list_len);
46   return keys;
47 }
48 
49 }  // namespace net
50