|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
| 5 | +#include <algorithm> |
5 | 6 | #include <string> |
6 | 7 | #include <vector> |
7 | 8 |
|
8 | 9 | #include "absl/status/status.h" |
9 | 10 | #include "absl/strings/str_cat.h" |
| 11 | +#include "absl/strings/str_split.h" |
10 | 12 | #include "openssl/base.h" |
11 | 13 | #include "openssl/rsa.h" |
12 | 14 | #include "quiche/quic/masque/private_tokens.h" |
13 | 15 | #include "quiche/common/platform/api/quiche_command_line_flags.h" |
14 | 16 | #include "quiche/common/platform/api/quiche_logging.h" |
15 | 17 | #include "quiche/common/quiche_status_utils.h" |
16 | 18 |
|
| 19 | +// This tool exists to help test out private tokens as defined in RFCs 9577 and |
| 20 | +// 9578. |
| 21 | + |
| 22 | +// To generate a config based on existing keys in PEM files: |
| 23 | +// blaze run //quiche/quic/masque:private_tokens -- --alsologtostderr |
| 24 | +// --private_key_file=/path/to/private_key.pem |
| 25 | +// --public_key_file==/path/to/public_key.pem |
| 26 | + |
| 27 | +// To test a token against a given public key in base64 format: |
| 28 | +// blaze run //quiche/quic/masque:private_tokens -- --alsologtostderr |
| 29 | +// --encoded_public_key="$PUBLIC_KEY" --token="$TOKEN" |
| 30 | + |
| 31 | +// To test out whether a token matches an issuer URL: |
| 32 | +// blaze run //quiche/quic/masque:private_tokens -- --alsologtostderr |
| 33 | +// --token="$TOKEN" --encoded_public_key="$( |
| 34 | +// curl --silent -H "Accept: application/private-token-issuer-directory" |
| 35 | +// "$ISSUER_URL" | jq -r '.["token-keys"] | map(.["token-key"]) | join(",")')" |
| 36 | + |
17 | 37 | DEFINE_QUICHE_COMMAND_LINE_FLAG(std::string, private_key_file, "", |
18 | 38 | "Path to the PEM-encoded RSA private key."); |
19 | 39 |
|
20 | 40 | DEFINE_QUICHE_COMMAND_LINE_FLAG(std::string, public_key_file, "", |
21 | 41 | "Path to the PEM-encoded RSA public key."); |
22 | 42 |
|
| 43 | +DEFINE_QUICHE_COMMAND_LINE_FLAG( |
| 44 | + std::string, encoded_public_key, "", |
| 45 | + "Base64-encoded public key to use for token validation. Multiple entries " |
| 46 | + "may be passed in by separating them with commas."); |
| 47 | + |
| 48 | +DEFINE_QUICHE_COMMAND_LINE_FLAG(std::string, token, "", "Token to validate."); |
| 49 | + |
23 | 50 | namespace quic { |
24 | 51 | namespace { |
25 | 52 |
|
26 | 53 | absl::Status RunPrivateTokens(int argc, char* argv[]) { |
27 | | - const char* usage = |
28 | | - "Usage: private_tokens --private_key_file=<private-key-file> " |
29 | | - "--public_key_file=<public-key-file>"; |
| 54 | + const char* usage = "Usage: private_tokens"; |
30 | 55 | std::vector<std::string> params = |
31 | 56 | quiche::QuicheParseCommandLineFlags(usage, argc, argv); |
| 57 | + const std::string private_key_file = |
| 58 | + quiche::GetQuicheCommandLineFlag(FLAGS_private_key_file); |
| 59 | + const std::string public_key_file = |
| 60 | + quiche::GetQuicheCommandLineFlag(FLAGS_public_key_file); |
| 61 | + const std::string encoded_public_key_from_flags = |
| 62 | + quiche::GetQuicheCommandLineFlag(FLAGS_encoded_public_key); |
| 63 | + std::vector<std::string> encoded_public_keys = absl::StrSplit( |
| 64 | + encoded_public_key_from_flags, ',', absl::SkipWhitespace()); |
| 65 | + const std::string token_from_flags = |
| 66 | + quiche::GetQuicheCommandLineFlag(FLAGS_token); |
32 | 67 |
|
33 | | - QUICHE_ASSIGN_OR_RETURN(bssl::UniquePtr<RSA> private_key, |
34 | | - ParseRsaPrivateKey(quiche::GetQuicheCommandLineFlag( |
35 | | - FLAGS_private_key_file))); |
36 | | - QUICHE_ASSIGN_OR_RETURN(bssl::UniquePtr<RSA> public_key, |
37 | | - ParseRsaPublicKey(quiche::GetQuicheCommandLineFlag( |
38 | | - FLAGS_public_key_file))); |
39 | | - QUICHE_ASSIGN_OR_RETURN(std::string encoded_public_key, |
40 | | - EncodePrivacyPassPublicKey(public_key.get())); |
| 68 | + bssl::UniquePtr<RSA> public_key; |
| 69 | + std::string encoded_public_key; |
| 70 | + if (!public_key_file.empty()) { |
| 71 | + QUICHE_ASSIGN_OR_RETURN(public_key, ParseRsaPublicKey(public_key_file)); |
| 72 | + QUICHE_ASSIGN_OR_RETURN(encoded_public_key, |
| 73 | + EncodePrivacyPassPublicKey(public_key.get())); |
| 74 | + if (!encoded_public_keys.empty()) { |
| 75 | + if (std::find(encoded_public_keys.begin(), encoded_public_keys.end(), |
| 76 | + encoded_public_key) == encoded_public_keys.end()) { |
| 77 | + return absl::InvalidArgumentError( |
| 78 | + "Public key from --public_key_file does not match " |
| 79 | + "--encoded_public_key"); |
| 80 | + } |
| 81 | + } else { |
| 82 | + encoded_public_keys.push_back(encoded_public_key); |
41 | 83 |
|
42 | | - std::string issuer_config = absl::StrCat( |
43 | | - "{\n \"issuer-request-uri\": \"https://issuer.example.net/request\",\n", |
44 | | - " \"token-keys\": [\n {\n \"token-type\": 2,\n", |
45 | | - " \"token-key\": \"", encoded_public_key, "\",\n }\n ]\n}"); |
| 84 | + std::string issuer_config = absl::StrCat( |
| 85 | + "{\n \"issuer-request-uri\": " |
| 86 | + "\"https://issuer.example.net/request\",\n", |
| 87 | + " \"token-keys\": [\n {\n \"token-type\": 2,\n", |
| 88 | + " \"token-key\": \"", encoded_public_key, "\",\n }\n ]\n}"); |
46 | 89 |
|
47 | | - QUICHE_LOG(INFO) << "The issuer config could look like:\n" << issuer_config; |
| 90 | + QUICHE_LOG(INFO) << "The issuer config could look like:\n" |
| 91 | + << issuer_config; |
| 92 | + } |
| 93 | + } |
48 | 94 |
|
49 | | - QUICHE_ASSIGN_OR_RETURN( |
50 | | - std::string token, |
51 | | - CreateTokenLocally(private_key.get(), public_key.get())); |
| 95 | + if (!token_from_flags.empty()) { |
| 96 | + QUICHE_RETURN_IF_ERROR( |
| 97 | + TokenValidatesFromAtLeastOneKey(encoded_public_keys, token_from_flags)); |
| 98 | + QUICHE_LOG(INFO) << "Validated token from --token"; |
| 99 | + } |
| 100 | + if (!private_key_file.empty()) { |
| 101 | + QUICHE_ASSIGN_OR_RETURN(bssl::UniquePtr<RSA> private_key, |
| 102 | + ParseRsaPrivateKey(private_key_file)); |
| 103 | + if (public_key == nullptr) { |
| 104 | + return absl::InvalidArgumentError( |
| 105 | + "--public_key_file is required when --private_key_file is set."); |
| 106 | + } |
| 107 | + QUICHE_ASSIGN_OR_RETURN( |
| 108 | + std::string generated_token, |
| 109 | + CreateTokenLocally(private_key.get(), public_key.get())); |
52 | 110 |
|
53 | | - std::string auth_header = |
54 | | - absl::StrCat("Authorization: PrivateToken token=\"", token, "\""); |
| 111 | + std::string auth_header = absl::StrCat( |
| 112 | + "Authorization: PrivateToken token=\"", generated_token, "\""); |
55 | 113 |
|
56 | | - QUICHE_LOG(INFO) << "The auth header would look like:\n" << auth_header; |
| 114 | + QUICHE_LOG(INFO) << "The generated auth header would look like:\n" |
| 115 | + << auth_header; |
57 | 116 |
|
58 | | - QUICHE_RETURN_IF_ERROR(ValidateToken(encoded_public_key, token)); |
59 | | - QUICHE_LOG(INFO) << "Token validation succeeded"; |
| 117 | + QUICHE_RETURN_IF_ERROR( |
| 118 | + TokenValidatesFromAtLeastOneKey(encoded_public_keys, generated_token)); |
| 119 | + QUICHE_LOG(INFO) << "Validated locally-generated token"; |
| 120 | + } |
60 | 121 | return absl::OkStatus(); |
61 | 122 | } |
62 | 123 |
|
|
0 commit comments