Skip to content

Commit 7a817f4

Browse files
dmcardleBoringssl LUCI CQ
authored and
Boringssl LUCI CQ
committed
Add 'generate-ech' command to bssl tool
The tool generates three files: an ECHConfig, its corresponding private key, and the ECHConfig wrapped in an ECHConfigList. For example, the following invocation generates the files: bssl generate-ech \ -out-ech-config-list ech_config_list.data \ -out-ech-config ech_config.data \ -out-private-key ech.key \ -public-name foo.example \ -config-id 0 Now, we can pass the ECHConfig and private key into the 'server' and 'client' commands: bssl server -accept 4430 \ -ech-config ech_config.data \ -ech-key ech.key bssl client -connect localhost:4430 \ -ech-config-list ech_config_list.data Bug: 275 Change-Id: Id4342855483fb01aa956f9aff356105c4a8ca4f6 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/48466 Reviewed-by: David Benjamin <[email protected]> Commit-Queue: David Benjamin <[email protected]>
1 parent e38cf79 commit 7a817f4

File tree

6 files changed

+157
-18
lines changed

6 files changed

+157
-18
lines changed

tool/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_executable(
1010
digest.cc
1111
fd.cc
1212
file.cc
13+
generate_ech.cc
1314
generate_ed25519.cc
1415
genrsa.cc
1516
pkcs12.cc

tool/file.cc

+18
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
1313
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
1414

15+
#include <openssl/bytestring.h>
16+
17+
#include <errno.h>
1518
#include <stdio.h>
19+
#include <string.h>
1620

1721
#include <algorithm>
1822
#include <vector>
@@ -48,3 +52,17 @@ bool ReadAll(std::vector<uint8_t> *out, FILE *file) {
4852
}
4953
}
5054
}
55+
56+
bool WriteToFile(const std::string &path, bssl::Span<const uint8_t> in) {
57+
ScopedFILE file(fopen(path.c_str(), "wb"));
58+
if (!file) {
59+
fprintf(stderr, "Failed to open '%s': %s\n", path.c_str(), strerror(errno));
60+
return false;
61+
}
62+
if (fwrite(in.data(), in.size(), 1, file.get()) != 1) {
63+
fprintf(stderr, "Failed to write to '%s': %s\n", path.c_str(),
64+
strerror(errno));
65+
return false;
66+
}
67+
return true;
68+
}

tool/generate_ech.cc

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/* Copyright (c) 2021, 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 <stdio.h>
16+
17+
#include <vector>
18+
19+
#include <openssl/bytestring.h>
20+
#include <openssl/hpke.h>
21+
#include <openssl/span.h>
22+
#include <openssl/ssl.h>
23+
24+
#include "internal.h"
25+
26+
27+
static const struct argument kArguments[] = {
28+
{
29+
"-out-ech-config-list",
30+
kRequiredArgument,
31+
"The path where the ECHConfigList should be written.",
32+
},
33+
{
34+
"-out-ech-config",
35+
kRequiredArgument,
36+
"The path where the ECHConfig should be written.",
37+
},
38+
{
39+
"-out-private-key",
40+
kRequiredArgument,
41+
"The path where the private key should be written.",
42+
},
43+
{
44+
"-public-name",
45+
kRequiredArgument,
46+
"The public name for the new ECHConfig.",
47+
},
48+
{
49+
"-config-id",
50+
kRequiredArgument,
51+
"The config ID for the new ECHConfig, from 0 to 255. Config IDs may be "
52+
"reused, but should be unique among active configs on a server for "
53+
"performance.",
54+
},
55+
{
56+
"-max-name-length",
57+
kOptionalArgument,
58+
"The length of the longest name in the anonymity set, to guide client "
59+
"padding.",
60+
},
61+
{
62+
"",
63+
kOptionalArgument,
64+
"",
65+
},
66+
};
67+
68+
bool GenerateECH(const std::vector<std::string> &args) {
69+
std::map<std::string, std::string> args_map;
70+
if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
71+
PrintUsage(kArguments);
72+
return false;
73+
}
74+
75+
unsigned config_id;
76+
if (!GetUnsigned(&config_id, "-config-id", 0, args_map) ||
77+
config_id > std::numeric_limits<uint8_t>::max()) {
78+
fprintf(stderr, "Error parsing -config-id argument\n");
79+
return false;
80+
}
81+
82+
unsigned max_name_len = 0;
83+
if (args_map.count("-max-name-length") != 0 &&
84+
!GetUnsigned(&max_name_len, "-max-name-length", 0, args_map)) {
85+
fprintf(stderr, "Error parsing -max-name-length argument\n");
86+
return false;
87+
}
88+
89+
bssl::ScopedEVP_HPKE_KEY key;
90+
uint8_t public_key[EVP_HPKE_MAX_PUBLIC_KEY_LENGTH];
91+
uint8_t private_key[EVP_HPKE_MAX_PRIVATE_KEY_LENGTH];
92+
size_t public_key_len, private_key_len;
93+
if (!EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256()) ||
94+
!EVP_HPKE_KEY_public_key(key.get(), public_key, &public_key_len,
95+
sizeof(public_key)) ||
96+
!EVP_HPKE_KEY_private_key(key.get(), private_key, &private_key_len,
97+
sizeof(private_key))) {
98+
fprintf(stderr, "Failed to generate the HPKE keypair\n");
99+
return false;
100+
}
101+
102+
uint8_t *ech_config;
103+
size_t ech_config_len;
104+
if (!SSL_marshal_ech_config(
105+
&ech_config, &ech_config_len, static_cast<uint8_t>(config_id),
106+
key.get(), args_map["-public-name"].c_str(), size_t{max_name_len})) {
107+
fprintf(stderr, "Failed to serialize the ECHConfigList\n");
108+
return false;
109+
}
110+
bssl::UniquePtr<uint8_t> free_ech_config(ech_config);
111+
112+
bssl::ScopedCBB cbb;
113+
CBB body;
114+
if (!CBB_init(cbb.get(), ech_config_len + sizeof(uint16_t)) ||
115+
!CBB_add_u16_length_prefixed(cbb.get(), &body) ||
116+
!CBB_add_bytes(&body, ech_config, ech_config_len) ||
117+
!CBB_flush(cbb.get())) {
118+
fprintf(stderr, "Failed to serialize the ECHConfigList\n");
119+
return false;
120+
}
121+
if (!WriteToFile(
122+
args_map["-out-ech-config-list"],
123+
bssl::MakeConstSpan(CBB_data(cbb.get()), CBB_len(cbb.get()))) ||
124+
!WriteToFile(args_map["-out-ech-config"],
125+
bssl::MakeConstSpan(ech_config, ech_config_len)) ||
126+
!WriteToFile(args_map["-out-private-key"],
127+
bssl::MakeConstSpan(private_key, private_key_len))) {
128+
fprintf(stderr, "Failed to write ECHConfig or private key to file\n");
129+
return false;
130+
}
131+
return true;
132+
}

tool/generate_ed25519.cc

+2-18
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,6 @@ static const struct argument kArguments[] = {
3434
},
3535
};
3636

37-
static bool WriteToFile(const std::string &path, const uint8_t *in,
38-
size_t in_len) {
39-
ScopedFILE file(fopen(path.c_str(), "wb"));
40-
if (!file) {
41-
fprintf(stderr, "Failed to open '%s': %s\n", path.c_str(), strerror(errno));
42-
return false;
43-
}
44-
if (fwrite(in, in_len, 1, file.get()) != 1) {
45-
fprintf(stderr, "Failed to write to '%s': %s\n", path.c_str(),
46-
strerror(errno));
47-
return false;
48-
}
49-
return true;
50-
}
51-
5237
bool GenerateEd25519Key(const std::vector<std::string> &args) {
5338
std::map<std::string, std::string> args_map;
5439

@@ -60,7 +45,6 @@ bool GenerateEd25519Key(const std::vector<std::string> &args) {
6045
uint8_t public_key[32], private_key[64];
6146
ED25519_keypair(public_key, private_key);
6247

63-
return WriteToFile(args_map["-out-public"], public_key, sizeof(public_key)) &&
64-
WriteToFile(args_map["-out-private"], private_key,
65-
sizeof(private_key));
48+
return WriteToFile(args_map["-out-public"], public_key) &&
49+
WriteToFile(args_map["-out-private"], private_key);
6650
}

tool/internal.h

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define OPENSSL_HEADER_TOOL_INTERNAL_H
1717

1818
#include <openssl/base.h>
19+
#include <openssl/span.h>
1920

2021
#include <string>
2122
#include <utility>
@@ -120,10 +121,12 @@ bool GetUnsigned(unsigned *out, const std::string &arg_name,
120121
const std::map<std::string, std::string> &args);
121122

122123
bool ReadAll(std::vector<uint8_t> *out, FILE *in);
124+
bool WriteToFile(const std::string &path, bssl::Span<const uint8_t> in);
123125

124126
bool Ciphers(const std::vector<std::string> &args);
125127
bool Client(const std::vector<std::string> &args);
126128
bool DoPKCS12(const std::vector<std::string> &args);
129+
bool GenerateECH(const std::vector<std::string> &args);
127130
bool GenerateEd25519Key(const std::vector<std::string> &args);
128131
bool GenerateRSAKey(const std::vector<std::string> &args);
129132
bool MD5Sum(const std::vector<std::string> &args);

tool/tool.cc

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ static const Tool kTools[] = {
4545
{ "ciphers", Ciphers },
4646
{ "client", Client },
4747
{ "isfips", IsFIPS },
48+
{ "generate-ech", GenerateECH},
4849
{ "generate-ed25519", GenerateEd25519Key },
4950
{ "genrsa", GenerateRSAKey },
5051
{ "md5sum", MD5Sum },

0 commit comments

Comments
 (0)