|
| 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 | +} |
0 commit comments