1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "commands.h"
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <openssl/crypto.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "glome.h"
#include "login/base64.h"
#include "login/config.h"
#include "login/crypto.h"
#define GLOME_CLI_MAX_MESSAGE_LENGTH 4095
#define UNUSED(var) (void)(var)
// Arguments
static const char *key_file = NULL;
static const char *peer_file = NULL;
static const char *tag_b64 = NULL;
static unsigned long counter = 0; // NOLINT(runtime/int)
static bool parse_args(int argc, char **argv) {
int c;
struct option long_options[] = {{"key", required_argument, 0, 'k'},
{"peer", required_argument, 0, 'p'},
{"counter", required_argument, 0, 'c'},
{"tag", required_argument, 0, 't'},
{0, 0, 0, 0}};
// First argument is the command name so skip it.
while ((c = getopt_long(argc - 1, argv + 1, "c:k:p:t:", long_options,
NULL)) != -1) {
switch (c) {
case 'c': {
char *endptr;
errno = 0;
counter = strtoul(optarg, &endptr, 0);
if (errno || counter > 255 || optarg == endptr || *endptr != '\0') {
fprintf(stderr, "'%s' is not a valid counter (0..255)\n", optarg);
return false;
}
break;
}
case 'k':
key_file = optarg;
break;
case 'p':
peer_file = optarg;
break;
case 't':
tag_b64 = optarg;
break;
case '?':
return false;
default:
// option not implemented
abort();
}
}
return true;
}
static bool read_file(const char *fname, uint8_t *buf, const size_t num_bytes) {
FILE *f = fopen(fname, "r");
if (!f) {
fprintf(stderr, "could not open file %s: %s\n", fname, strerror(errno));
return false;
}
if (fread(buf, 1, num_bytes, f) != num_bytes) {
fprintf(stderr, "could not read %zu bytes from file %s", num_bytes, fname);
if (ferror(f)) {
fprintf(stderr, ": %s\n", strerror(errno));
} else {
fputs("\n", stderr);
}
fclose(f);
return false;
}
fclose(f);
return true;
}
static bool read_public_key_file(const char *fname, uint8_t *buf,
size_t buf_len) {
FILE *f = fopen(fname, "r");
if (!f) {
fprintf(stderr, "could not open file %s: %s\n", fname, strerror(errno));
return false;
}
// Allocate enough buffer space to fit the public key and a reasonable amount
// of whitespace.
char encoded_public_key[128] = {0};
if (!fgets(encoded_public_key, sizeof(encoded_public_key), f)) {
perror("could not read from public key file");
fclose(f);
return false;
}
fclose(f);
if (!glome_login_parse_public_key(encoded_public_key, buf, buf_len)) {
fprintf(stderr, "failed to parse public key %s\n", encoded_public_key);
return false;
}
return true;
}
int genkey(int argc, char **argv) {
UNUSED(argc);
UNUSED(argv);
uint8_t private_key[GLOME_MAX_PRIVATE_KEY_LENGTH] = {0};
if (glome_generate_key(private_key, NULL)) {
fprintf(stderr, "unable to generate a new key\n");
return EXIT_FAILURE;
}
if (fwrite(private_key, 1, sizeof private_key, stdout) !=
sizeof private_key) {
perror("unable to write the private key to stdout");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int pubkey(int argc, char **argv) {
UNUSED(argc);
UNUSED(argv);
uint8_t private_key[GLOME_MAX_PRIVATE_KEY_LENGTH] = {0};
uint8_t public_key[GLOME_MAX_PUBLIC_KEY_LENGTH] = {0};
char encoded_public_key[ENCODED_BUFSIZE(GLOME_MAX_PUBLIC_KEY_LENGTH)] = {0};
if (fread(private_key, 1, sizeof private_key, stdin) != sizeof private_key) {
perror("unable to read the private key from stdin");
return EXIT_FAILURE;
}
if (glome_derive_key(private_key, public_key)) {
fprintf(stderr, "unable to generate a new key\n");
return EXIT_FAILURE;
}
if (!base64url_encode(public_key, sizeof public_key,
(uint8_t *)encoded_public_key,
sizeof encoded_public_key)) {
fputs("unable to encode public key\n", stderr);
return EXIT_FAILURE;
}
if (printf("%s %s\n", GLOME_LOGIN_PUBLIC_KEY_ID, encoded_public_key) < 0) {
perror("unable to write the public key to stdout");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// tag_impl reads a private key and a peer key from the given files and computes
// a tag corresponding to a message read from stdin for the communication
// direction determined by verify.
int tag_impl(uint8_t tag[GLOME_MAX_TAG_LENGTH], bool verify,
const char *key_file, const char *peer_file) {
uint8_t private_key[GLOME_MAX_PRIVATE_KEY_LENGTH] = {0};
uint8_t peer_key[GLOME_MAX_PUBLIC_KEY_LENGTH] = {0};
char message[GLOME_CLI_MAX_MESSAGE_LENGTH] = {0};
if (!read_file(key_file, private_key, sizeof private_key) ||
!read_public_key_file(peer_file, peer_key, sizeof(peer_key))) {
return EXIT_FAILURE;
}
size_t msg_len = fread(message, 1, GLOME_CLI_MAX_MESSAGE_LENGTH, stdin);
if (!feof(stdin)) {
fprintf(stderr, "message exceeds maximum supported size of %u\n",
GLOME_CLI_MAX_MESSAGE_LENGTH);
return EXIT_FAILURE;
}
if (glome_tag(verify, counter, private_key, peer_key, (uint8_t *)message,
msg_len, tag)) {
fputs("MAC tag generation failed\n", stderr);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int tag(int argc, char **argv) {
uint8_t tag[GLOME_MAX_TAG_LENGTH] = {0};
if (!parse_args(argc, argv)) {
return EXIT_FAILURE;
}
if (!key_file || !peer_file) {
fprintf(stderr, "not enough arguments for subcommand %s\n", argv[1]);
return EXIT_FAILURE;
}
int res = tag_impl(tag, /*verify=*/false, key_file, peer_file);
if (res) {
return res;
}
char tag_encoded[ENCODED_BUFSIZE(sizeof tag)] = {0};
if (base64url_encode(tag, sizeof tag, (uint8_t *)tag_encoded,
sizeof tag_encoded) == 0) {
fprintf(stderr, "GLOME tag encode failed\n");
return EXIT_FAILURE;
}
puts(tag_encoded);
return EXIT_SUCCESS;
}
int verify(int argc, char **argv) {
uint8_t tag[GLOME_MAX_TAG_LENGTH] = {0};
uint8_t *expected_tag = NULL;
int ret = EXIT_FAILURE;
if (!parse_args(argc, argv)) {
goto out;
}
if (!key_file || !peer_file || !tag_b64) {
fprintf(stderr, "not enough arguments for subcommand %s\n", argv[1]);
goto out;
}
int res = tag_impl(tag, /*verify=*/true, key_file, peer_file);
if (res) {
goto out;
}
// decode the tag
size_t tag_b64_len = strlen(tag_b64);
size_t tag_b64_decoded_len = DECODED_BUFSIZE(tag_b64_len);
expected_tag = malloc(tag_b64_decoded_len);
if (expected_tag == NULL) {
fprintf(stderr, "GLOME tag malloc %ld bytes failed\n", tag_b64_decoded_len);
goto out;
}
size_t expected_tag_len =
base64url_decode((uint8_t *)tag_b64, tag_b64_len, (uint8_t *)expected_tag,
tag_b64_decoded_len);
if (expected_tag_len == 0) {
fprintf(stderr, "GLOME tag decode failed\n");
goto out;
}
if (expected_tag_len > sizeof tag) {
expected_tag_len = sizeof tag;
}
// compare the tag
if (CRYPTO_memcmp(expected_tag, tag, expected_tag_len) != 0) {
fputs("MAC tag verification failed\n", stderr);
goto out;
}
ret = EXIT_SUCCESS;
out:
free(expected_tag);
return ret;
}
static bool parse_login_path(const char *path, char **handshake,
char **message) {
size_t path_len = strlen(path);
if (path_len < 3 || path[0] != 'v' || path[1] != '2' || path[2] != '/') {
fprintf(stderr, "unexpected challenge prefix: %s\n", path);
return false;
}
if (path[path_len - 1] != '/') {
fprintf(stderr, "unexpected challenge suffix: %s\n", path);
return false;
}
const char *start = path + 3;
char *slash = strchr(start, '/');
if (slash == NULL || slash - start == 0) {
fprintf(stderr, "could not parse handshake from %s\n", start);
return false;
}
*handshake = strndup(start, slash - start);
if (*handshake == NULL) {
fprintf(stderr, "failed to duplicate handshake\n");
return false;
}
// Everything left (not including the trailing slash) is the message.
start = slash + 1;
*message = strndup(start, path + path_len - 1 - start);
if (*message == NULL) {
free(*handshake);
*handshake = NULL;
fprintf(stderr, "failed to duplicate message\n");
return false;
}
return true;
}
int login(int argc, char **argv) {
char *handshake = NULL;
char *handshake_b64 = NULL;
char *message = NULL;
int ret = EXIT_FAILURE;
if (!parse_args(argc, argv)) {
return EXIT_FAILURE;
}
char *cmd = argv[optind++];
if (optind >= argc) {
fprintf(stderr, "missing challenge for subcommand %s\n", cmd);
return EXIT_FAILURE;
}
if (!key_file) {
fprintf(stderr, "not enough arguments for subcommand %s\n", cmd);
return EXIT_FAILURE;
}
uint8_t private_key[GLOME_MAX_PRIVATE_KEY_LENGTH] = {0};
if (!read_file(key_file, private_key, sizeof private_key)) {
return EXIT_FAILURE;
}
char *path = strstr(argv[optind], "v2/");
if (path == NULL) {
fprintf(stderr, "unsupported challenge format\n");
goto out;
}
if (!parse_login_path(path, &handshake_b64, &message)) {
return EXIT_FAILURE;
}
size_t handshake_b64_len = strlen(handshake_b64);
handshake = malloc(DECODED_BUFSIZE(handshake_b64_len));
if (handshake == NULL) {
fprintf(stderr, "failed to malloc %ld bytes for base64 decode\n",
DECODED_BUFSIZE(handshake_b64_len));
goto out;
}
int handshake_len = base64url_decode((uint8_t *)handshake_b64,
handshake_b64_len, (uint8_t *)handshake,
DECODED_BUFSIZE(handshake_b64_len));
if (handshake_len == 0) {
fprintf(stderr, "failed to decode handshake in path %s\n", path);
goto out;
}
if (handshake_len < 1 + GLOME_MAX_PUBLIC_KEY_LENGTH ||
handshake_len > 1 + GLOME_MAX_PUBLIC_KEY_LENGTH + GLOME_MAX_TAG_LENGTH) {
fprintf(stderr, "handshake size is invalid in path %s\n", path);
goto out;
}
if ((handshake[0] & 0x80) == 0) {
uint8_t public_key[GLOME_MAX_PUBLIC_KEY_LENGTH] = {0};
if (glome_derive_key(private_key, public_key)) {
fprintf(stderr, "unable to generate a public key\n");
goto out;
}
// Most significant bit is not set for X25519 key (see RFC 7748).
uint8_t public_key_msb = public_key[GLOME_MAX_PUBLIC_KEY_LENGTH - 1];
if (handshake[0] != public_key_msb) {
fprintf(stderr, "unexpected public key prefix\n");
goto out;
}
}
uint8_t peer_key[GLOME_MAX_PRIVATE_KEY_LENGTH] = {0};
memcpy(peer_key, handshake + 1, GLOME_MAX_PUBLIC_KEY_LENGTH);
uint8_t tag[GLOME_MAX_TAG_LENGTH] = {0};
if (glome_tag(true, 0, private_key, peer_key, (uint8_t *)message,
strlen(message), tag)) {
fprintf(stderr, "MAC authcode generation failed\n");
goto out;
}
if (CRYPTO_memcmp(handshake + 1 + GLOME_MAX_PUBLIC_KEY_LENGTH, tag,
handshake_len - 1 - GLOME_MAX_PUBLIC_KEY_LENGTH) != 0) {
fprintf(
stderr,
"The challenge includes a message tag prefix which does not match the "
"message\n");
goto out;
}
if (glome_tag(false, 0, private_key, peer_key, (uint8_t *)message,
strlen(message), tag)) {
fprintf(stderr, "GLOME tag generation failed\n");
goto out;
}
char tag_encoded[ENCODED_BUFSIZE(sizeof tag)] = {0};
if (base64url_encode(tag, sizeof tag, (uint8_t *)tag_encoded,
sizeof tag_encoded) == 0) {
fprintf(stderr, "GLOME tag encode failed\n");
goto out;
}
puts(tag_encoded);
ret = EXIT_SUCCESS;
out:
free(handshake);
free(handshake_b64);
free(message);
return ret;
}
|