|
20 | 20 | #include "kms_message_private.h" |
21 | 21 | #include "kms_request_opt_private.h" |
22 | 22 |
|
23 | | -#include <ctype.h> |
24 | | - |
25 | 23 | /* Set a default expiration of 5 minutes for JSON Web Tokens (GCP allows up to |
26 | 24 | * one hour) */ |
27 | 25 | #define JWT_EXPIRATION_SECS 5 * 60 |
28 | 26 |
|
| 27 | +#define KMS_PRINTABLE_ASCII_START ' ' |
| 28 | +#define KMS_PRINTABLE_ASCII_END '~' |
| 29 | + |
| 30 | +/* _oauth_field_chars_are_valid returns true if `value` is safe to interpolate into the JWT claim set of the |
| 31 | + * GCP OAuth request. Reject non-printable ASCII, double quotes, and backslashes. |
| 32 | + * |
| 33 | + * `value` is not included in the error message: it is untrusted, and the error may be logged. */ |
| 34 | +static bool |
| 35 | +_oauth_field_chars_are_valid(kms_request_t *req, const char *name, const char *value) |
| 36 | +{ |
| 37 | + for (const char *i = value; *i != '\0'; i++) { |
| 38 | + const unsigned char uc = (unsigned char)*i; |
| 39 | + |
| 40 | + if (uc < KMS_PRINTABLE_ASCII_START || uc > KMS_PRINTABLE_ASCII_END || uc == '"' || uc == '\\') { |
| 41 | + KMS_ERROR(req, "Invalid character in GCP OAuth request: %s", name); |
| 42 | + return false; |
| 43 | + } |
| 44 | + } |
| 45 | + return true; |
| 46 | +} |
| 47 | + |
29 | 48 | kms_request_t * |
30 | 49 | kms_gcp_request_oauth_new (const char *host, |
31 | 50 | const char *email, |
@@ -57,6 +76,11 @@ kms_gcp_request_oauth_new (const char *host, |
57 | 76 | goto done; |
58 | 77 | } |
59 | 78 |
|
| 79 | + if (!(_oauth_field_chars_are_valid(req, "email", email) && _oauth_field_chars_are_valid(req, "audience", audience) && |
| 80 | + _oauth_field_chars_are_valid(req, "scope", scope))) { |
| 81 | + goto done; |
| 82 | + } |
| 83 | + |
60 | 84 | /* Produce the signed JWT <base64url header>.<base64url claims>.<base64url |
61 | 85 | * signature> */ |
62 | 86 | issued_at = time (NULL); |
@@ -163,18 +187,31 @@ kms_gcp_request_oauth_new (const char *host, |
163 | 187 | * - location is a Cloud location name (e.g. "global", "us-east1"). |
164 | 188 | * - keyVersion is a positive integer. |
165 | 189 | * |
| 190 | + * Each identifier must also contain an alphanumeric character. No valid identifier lacks one, and |
| 191 | + * this keeps a value of only dots from forming a dot-segment ("..") in the request path. |
| 192 | + * |
166 | 193 | * `value` is not included in the error message: it is untrusted, and the error may be logged. */ |
167 | 194 | static bool |
168 | 195 | _check_key_identifier (kms_request_t *req, const char *name, const char *value) |
169 | 196 | { |
170 | 197 | const char *c; |
| 198 | + bool has_alnum = false; |
171 | 199 |
|
172 | 200 | for (c = value; *c != '\0'; c++) { |
173 | | - if (!isalnum ((unsigned char) *c) && *c != '-' && *c != '.' && *c != '_' && |
174 | | - *c != '~') { |
| 201 | + if (!((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z') || |
| 202 | + (*c >= '0' && *c <= '9') || *c == '-' || *c == '.' || *c == '_' || |
| 203 | + *c == '~')) { |
175 | 204 | KMS_ERROR (req, "Invalid character in GCP KMS key identifier: %s", name); |
176 | 205 | return false; |
177 | 206 | } |
| 207 | + if ((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z') || |
| 208 | + (*c >= '0' && *c <= '9')) { |
| 209 | + has_alnum = true; |
| 210 | + } |
| 211 | + } |
| 212 | + if (!has_alnum) { |
| 213 | + KMS_ERROR (req, "Invalid GCP KMS key identifier: %s", name); |
| 214 | + return false; |
178 | 215 | } |
179 | 216 | return true; |
180 | 217 | } |
@@ -222,15 +259,22 @@ _encrypt_decrypt_common (const char *encrypt_decrypt, |
222 | 259 | goto done; |
223 | 260 | } |
224 | 261 |
|
225 | | - if (kms_request_get_error (req)) { |
226 | | - goto done; |
227 | | - } |
228 | | - |
| 262 | + /* Check the key identifiers before checking req for an error. The |
| 263 | + * identifiers are formatted into the request path above, so an invalid |
| 264 | + * identifier may have already failed the generic request line validation in |
| 265 | + * kms_request_new. Report the more specific error naming the field. */ |
229 | 266 | if (!_check_key_identifier (req, "projectId", project_id) || |
230 | 267 | !_check_key_identifier (req, "location", location) || |
231 | 268 | !_check_key_identifier (req, "keyRing", key_ring_name) || |
232 | 269 | !_check_key_identifier (req, "keyName", key_name) || |
233 | | - (key_version && !_check_key_identifier (req, "keyVersion", key_version))) { |
| 270 | + /* An empty key version is treated the same as an absent one: it is |
| 271 | + * omitted from the path above, so there is nothing to validate. */ |
| 272 | + (key_version && *key_version && |
| 273 | + !_check_key_identifier (req, "keyVersion", key_version))) { |
| 274 | + goto done; |
| 275 | + } |
| 276 | + |
| 277 | + if (kms_request_get_error (req)) { |
234 | 278 | goto done; |
235 | 279 | } |
236 | 280 |
|
|
0 commit comments