|
Line 0
Link Here
|
|
|
1 |
/* |
| 2 |
* Copyright (c) 2014 Google Inc. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* 1. Redistributions of source code must retain the above copyright |
| 8 |
* notice, this list of conditions and the following disclaimer. |
| 9 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer in the |
| 11 |
* documentation and/or other materials provided with the distribution. |
| 12 |
* |
| 13 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "includes.h" |
| 26 |
|
| 27 |
#ifdef U2F |
| 28 |
|
| 29 |
#include <ctype.h> |
| 30 |
#include <openssl/x509.h> |
| 31 |
#include <openssl/err.h> |
| 32 |
#include <u2f-host.h> |
| 33 |
#include <fcntl.h> |
| 34 |
|
| 35 |
#include "key.h" |
| 36 |
#include "hostfile.h" |
| 37 |
#include "auth.h" |
| 38 |
#include "auth-options.h" |
| 39 |
#include "ssh.h" |
| 40 |
#include "ssh2.h" |
| 41 |
#include "log.h" |
| 42 |
#include "dispatch.h" |
| 43 |
#include "misc.h" |
| 44 |
#include "servconf.h" |
| 45 |
#include "packet.h" |
| 46 |
#include "digest.h" |
| 47 |
#include "xmalloc.h" |
| 48 |
#include "monitor_wrap.h" |
| 49 |
#include "u2f.h" |
| 50 |
|
| 51 |
// Evaluates to the maximum size that base64-encoding 'size' bytes can have, |
| 52 |
// including one byte for a trailing NULL byte. |
| 53 |
#define BASE64_ENCODED_SIZE(size) (((size)+2)/3)*4 + 1 |
| 54 |
|
| 55 |
// Evaluates to the maximum size that base64-decoding 'size' bytes can have. |
| 56 |
#define BASE64_DECODED_SIZE(size) ((size) * 3/4) |
| 57 |
|
| 58 |
extern ServerOptions options; |
| 59 |
|
| 60 |
static void input_userauth_u2f_auth_response(int, u_int32_t, void *); |
| 61 |
static void input_userauth_u2f_register_response(int type, u_int32_t seq, void *ctxt); |
| 62 |
|
| 63 |
static const int u2f_challenge_len = 32; |
| 64 |
// We set the application id to the fixed identifier “openssh”. Theoretically, |
| 65 |
// it should be an HTTPS URL, listing further origins that are acceptable. |
| 66 |
// However, since the SSH client cannot fetch such a URL anyway, we don’t |
| 67 |
// bother setting the appid to anything meaningful. |
| 68 |
// |
| 69 |
// In case we need to do that in the future, we can easily make the appid a |
| 70 |
// configuration option. |
| 71 |
static const char *appid = "openssh"; |
| 72 |
|
| 73 |
void u2f_sha256(u_char *dest, const u_char *src, size_t srclen) { |
| 74 |
struct ssh_digest_ctx *ctx = ssh_digest_start(SSH_DIGEST_SHA256); |
| 75 |
ssh_digest_update(ctx, src, srclen); |
| 76 |
ssh_digest_final(ctx, dest, ssh_digest_bytes(SSH_DIGEST_SHA256)); |
| 77 |
} |
| 78 |
|
| 79 |
/* We can get away without a JSON parser because all values in the JSON |
| 80 |
* messages used in U2F are (websafe) base64 encoded, therefore we don’t need |
| 81 |
* to care about escaping at all. We can just look for the starting double |
| 82 |
* quote and take everything until the next double quote. |
| 83 |
*/ |
| 84 |
static char * |
| 85 |
extract_json_string(const char *json, const char *key) |
| 86 |
{ |
| 87 |
char *quotedkey; |
| 88 |
char *keypos; |
| 89 |
char *value; |
| 90 |
char *end; |
| 91 |
int quotedkeylen; |
| 92 |
|
| 93 |
quotedkeylen = xasprintf("edkey, "\"%s\"", key); |
| 94 |
keypos = strstr(json, quotedkey); |
| 95 |
free(quotedkey); |
| 96 |
if (keypos == NULL) |
| 97 |
return NULL; |
| 98 |
|
| 99 |
keypos += quotedkeylen; |
| 100 |
if (*keypos == ':') |
| 101 |
keypos++; |
| 102 |
while (*keypos != '\0' && isspace(*keypos)) |
| 103 |
keypos++; |
| 104 |
if (*keypos != '"') |
| 105 |
return NULL; |
| 106 |
keypos++; |
| 107 |
value = xstrdup(keypos); |
| 108 |
if ((end = strchr(value, '"')) == NULL) { |
| 109 |
free(value); |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
*end = '\0'; |
| 113 |
return value; |
| 114 |
} |
| 115 |
|
| 116 |
static int |
| 117 |
urlsafe_base64_decode(const char *base64, u_char *buffer, size_t bufferlen) |
| 118 |
{ |
| 119 |
// U2F uses urlsafe base64, which replaces + with - and / with _, so we |
| 120 |
// need to revert that before base64 decoding. |
| 121 |
char *replaced; |
| 122 |
char *pos; |
| 123 |
int ret; |
| 124 |
|
| 125 |
replaced = xstrdup(base64); |
| 126 |
while ((pos = strchr(replaced, '-')) != NULL) |
| 127 |
*pos = '+'; |
| 128 |
while ((pos = strchr(replaced, '_')) != NULL) |
| 129 |
*pos = '/'; |
| 130 |
|
| 131 |
ret = b64_pton(replaced, buffer, bufferlen); |
| 132 |
free(replaced); |
| 133 |
return ret; |
| 134 |
} |
| 135 |
|
| 136 |
static int |
| 137 |
urlsafe_base64_encode(u_char const *src, size_t srclength, char *target, size_t targsize) |
| 138 |
{ |
| 139 |
char *pos; |
| 140 |
int len; |
| 141 |
|
| 142 |
if ((len = b64_ntop(src, srclength, target, targsize)) == -1) |
| 143 |
return -1; |
| 144 |
|
| 145 |
while ((pos = strchr(target, '+')) != NULL) |
| 146 |
*pos = '-'; |
| 147 |
|
| 148 |
while ((pos = strchr(target, '/')) != NULL) |
| 149 |
*pos = '_'; |
| 150 |
|
| 151 |
return len; |
| 152 |
} |
| 153 |
|
| 154 |
static Key* |
| 155 |
read_keyfile(FILE *fp, char *filename, struct passwd *pw, u_long *linenum) |
| 156 |
{ |
| 157 |
char line[SSH_MAX_PUBKEY_BYTES]; |
| 158 |
Key *found = NULL; |
| 159 |
|
| 160 |
while (read_keyfile_line(fp, filename, line, sizeof(line), linenum) != -1) { |
| 161 |
char *cp; |
| 162 |
if (found != NULL) |
| 163 |
key_free(found); |
| 164 |
found = key_new(KEY_U2F); |
| 165 |
auth_clear_options(); |
| 166 |
|
| 167 |
/* Skip leading whitespace, empty and comment lines. */ |
| 168 |
for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 169 |
; |
| 170 |
if (!*cp || *cp == '\n' || *cp == '#') |
| 171 |
continue; |
| 172 |
|
| 173 |
if (key_read(found, &cp) != 1) { |
| 174 |
continue; |
| 175 |
} |
| 176 |
if (found->type == KEY_U2F) { |
| 177 |
// TODO: calculate and display a fingerprint of the key handle and pubkey? |
| 178 |
debug("ssh-u2f key found: file %s, line %lu", filename, *linenum); |
| 179 |
return found; |
| 180 |
} |
| 181 |
} |
| 182 |
return NULL; |
| 183 |
} |
| 184 |
|
| 185 |
/* |
| 186 |
* Read a key from the key files. |
| 187 |
*/ |
| 188 |
Key* |
| 189 |
read_user_u2f_key(struct passwd *pw, u_int key_idx) |
| 190 |
{ |
| 191 |
size_t i; |
| 192 |
// TODO: It might not be safe to pass the key back to the unprivileged |
| 193 |
// process. It probably is, but we should review this. |
| 194 |
|
| 195 |
// In the first step, we need to go through all u2f keys that we have and |
| 196 |
// collect their key handles. |
| 197 |
for (i = 0; i < options.num_authkeys_files; i++) { |
| 198 |
FILE *fp; |
| 199 |
char *file; |
| 200 |
Key *key = NULL; |
| 201 |
u_long linenum = 0; |
| 202 |
if (strcasecmp(options.authorized_keys_files[i], "none") == 0) |
| 203 |
continue; |
| 204 |
file = expand_authorized_keys(options.authorized_keys_files[i], pw); |
| 205 |
debug("looking for ssh-u2f keys in %s", file); |
| 206 |
if ((fp = fopen(file, "r")) == NULL) { |
| 207 |
free(file); |
| 208 |
continue; |
| 209 |
} |
| 210 |
do |
| 211 |
{ |
| 212 |
// TODO: Hackish way to allow getting more than one key |
| 213 |
key_free(key); |
| 214 |
key = read_keyfile(fp, file, pw, &linenum); |
| 215 |
} |
| 216 |
while(key_idx-- > 0); |
| 217 |
fclose(fp); |
| 218 |
free(file); |
| 219 |
if (key != NULL) |
| 220 |
return key; |
| 221 |
} |
| 222 |
return NULL; |
| 223 |
} |
| 224 |
|
| 225 |
static int |
| 226 |
userauth_u2f_register(Authctxt *authctxt) |
| 227 |
{ |
| 228 |
u_char random[u2f_challenge_len]; |
| 229 |
char challenge[BASE64_ENCODED_SIZE(sizeof(random))]; |
| 230 |
char *json; |
| 231 |
|
| 232 |
arc4random_buf(random, sizeof(random)); |
| 233 |
if (urlsafe_base64_encode(random, sizeof(random), challenge, sizeof(challenge)) == -1) |
| 234 |
fatal("urlsafe_base64_encode(arc4random_buf()) failed"); |
| 235 |
|
| 236 |
xasprintf(&json, "{\"challenge\": \"%s\", \"version\": \"U2F_V2\", \"appId\": \"%s\"}", |
| 237 |
challenge, appid); |
| 238 |
|
| 239 |
packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST); |
| 240 |
packet_put_cstring(json); |
| 241 |
packet_send(); |
| 242 |
free(json); |
| 243 |
dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, |
| 244 |
&input_userauth_u2f_register_response); |
| 245 |
authctxt->postponed = 1; |
| 246 |
return 0; |
| 247 |
} |
| 248 |
|
| 249 |
static int |
| 250 |
userauth_u2f_authenticate(Authctxt *authctxt) |
| 251 |
{ |
| 252 |
char pubkey[BASE64_ENCODED_SIZE(U2F_PUBKEY_LEN)]; |
| 253 |
char *keyhandle; |
| 254 |
char *json; |
| 255 |
Key *key; |
| 256 |
u_char *challenge; |
| 257 |
|
| 258 |
if ((key = PRIVSEP(read_user_u2f_key(authctxt->pw, authctxt->u2f_attempt))) == NULL) { |
| 259 |
if (authctxt->u2f_attempt == 0) { |
| 260 |
char *reason = "Skipping U2F authentication: no ssh-u2f keys found in the authorized keys file(s)."; |
| 261 |
debug("%s", reason); |
| 262 |
auth_debug_add("%s", reason); |
| 263 |
authctxt->postponed = 0; |
| 264 |
return (1); |
| 265 |
} else { |
| 266 |
debug("terminating u2f authentication unsuccessfully, no more keys to try."); |
| 267 |
userauth_finish(authctxt, 0, "u2f", NULL); |
| 268 |
return (0); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST); |
| 273 |
challenge = xmalloc(u2f_challenge_len); |
| 274 |
arc4random_buf(challenge, u2f_challenge_len); |
| 275 |
free(authctxt->u2f_challenge); |
| 276 |
key_free(authctxt->u2f_key); |
| 277 |
authctxt->u2f_challenge = xmalloc(BASE64_ENCODED_SIZE(u2f_challenge_len)); |
| 278 |
authctxt->u2f_key = key; |
| 279 |
|
| 280 |
if (urlsafe_base64_encode(challenge, u2f_challenge_len, |
| 281 |
authctxt->u2f_challenge, BASE64_ENCODED_SIZE(u2f_challenge_len)) == -1) |
| 282 |
fatal("urlsafe_base64_encode(arc4random_buf()) failed"); |
| 283 |
|
| 284 |
if (urlsafe_base64_encode(key->u2f_pubkey, U2F_PUBKEY_LEN, pubkey, sizeof(pubkey)) == -1) |
| 285 |
fatal("urlsafe_base64_encode(key->u2f_pubkey) failed"); |
| 286 |
|
| 287 |
keyhandle = xmalloc(BASE64_ENCODED_SIZE(key->u2f_key_handle_len)); |
| 288 |
if (urlsafe_base64_encode(key->u2f_key_handle, key->u2f_key_handle_len, |
| 289 |
keyhandle, BASE64_ENCODED_SIZE(key->u2f_key_handle_len)) == -1) |
| 290 |
fatal("urlsafe_base64_encode(key->u2f_key_handle) failed"); |
| 291 |
|
| 292 |
xasprintf(&json, "{\"challenge\": \"%s\", \"keyHandle\": \"%s\", \"appId\": \"%s\"}", |
| 293 |
authctxt->u2f_challenge, keyhandle, appid); |
| 294 |
packet_put_cstring(json); |
| 295 |
free(json); |
| 296 |
free(keyhandle); |
| 297 |
free(challenge); |
| 298 |
packet_send(); |
| 299 |
|
| 300 |
dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, |
| 301 |
&input_userauth_u2f_auth_response); |
| 302 |
authctxt->postponed = 1; |
| 303 |
return (0); |
| 304 |
} |
| 305 |
|
| 306 |
static int |
| 307 |
userauth_u2f(Authctxt *authctxt) |
| 308 |
{ |
| 309 |
int mode = packet_get_int(); |
| 310 |
packet_check_eom(); |
| 311 |
if (mode == U2F_MODE_REGISTRATION) { |
| 312 |
debug("Starting U2F registration"); |
| 313 |
return userauth_u2f_register(authctxt); |
| 314 |
} else if (mode == U2F_MODE_AUTHENTICATION) { |
| 315 |
debug("Starting U2F authentication"); |
| 316 |
authctxt->u2f_attempt = 0; |
| 317 |
authctxt->u2f_challenge = NULL; |
| 318 |
authctxt->u2f_key = NULL; |
| 319 |
return userauth_u2f_authenticate(authctxt); |
| 320 |
} else { |
| 321 |
error("Unknown U2F mode %d requested by the client.", mode); |
| 322 |
return 0; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
static void |
| 327 |
input_userauth_u2f_register_response(int type, u_int32_t seq, void *ctxt) |
| 328 |
{ |
| 329 |
#define u2f_bounds_check(necessary_bytes) do { \ |
| 330 |
if (restlen < necessary_bytes) { \ |
| 331 |
error("U2F response too short: need %d bytes, but only %d remaining", \ |
| 332 |
necessary_bytes, restlen); \ |
| 333 |
goto out; \ |
| 334 |
} \ |
| 335 |
} while (0) |
| 336 |
|
| 337 |
#define u2f_advance(parsed_bytes) do { \ |
| 338 |
int advance = parsed_bytes; \ |
| 339 |
walk += advance; \ |
| 340 |
restlen -= advance; \ |
| 341 |
} while (0) |
| 342 |
|
| 343 |
Authctxt *authctxt = ctxt; |
| 344 |
char *response, *regdata = NULL, *clientdata = NULL; |
| 345 |
u_char *decoded = NULL; |
| 346 |
u_char *walk = NULL; |
| 347 |
u_char *keyhandle = NULL; |
| 348 |
u_char *pubkey = NULL; |
| 349 |
u_char *signature = NULL; |
| 350 |
u_char *dummy = NULL; |
| 351 |
u_char *cdecoded = NULL; |
| 352 |
X509 *x509 = NULL; |
| 353 |
EVP_PKEY *pkey = NULL; |
| 354 |
EVP_MD_CTX mdctx; |
| 355 |
int restlen; |
| 356 |
int khlen; |
| 357 |
int cdecodedlen; |
| 358 |
int err; |
| 359 |
char errorbuf[4096]; |
| 360 |
u_char digest[ssh_digest_bytes(SSH_DIGEST_SHA256)]; |
| 361 |
|
| 362 |
authctxt->postponed = 0; |
| 363 |
|
| 364 |
response = packet_get_string(NULL); |
| 365 |
packet_check_eom(); |
| 366 |
if ((regdata = extract_json_string(response, "registrationData")) == NULL) { |
| 367 |
error("U2F Response not JSON, or does not contain \"registrationData\""); |
| 368 |
goto out; |
| 369 |
} |
| 370 |
|
| 371 |
decoded = xmalloc(BASE64_DECODED_SIZE(strlen(regdata))); |
| 372 |
restlen = urlsafe_base64_decode(regdata, decoded, BASE64_DECODED_SIZE(strlen(regdata))); |
| 373 |
walk = decoded; |
| 374 |
|
| 375 |
// Header (magic byte) |
| 376 |
u2f_bounds_check(1); |
| 377 |
if (walk[0] != 0x05) { |
| 378 |
error("U2F response does not start with magic byte 0x05"); |
| 379 |
goto out; |
| 380 |
} |
| 381 |
u2f_advance(1); |
| 382 |
|
| 383 |
// Length of the public key |
| 384 |
u2f_bounds_check(U2F_PUBKEY_LEN); |
| 385 |
pubkey = walk; |
| 386 |
u2f_advance(U2F_PUBKEY_LEN); |
| 387 |
|
| 388 |
// Length of the key handle |
| 389 |
u2f_bounds_check(1); |
| 390 |
khlen = walk[0]; |
| 391 |
if (khlen <= 0) { |
| 392 |
error("Invalid key handle length: %d", khlen); |
| 393 |
goto out; |
| 394 |
} |
| 395 |
u2f_advance(1); |
| 396 |
|
| 397 |
// Key handle |
| 398 |
u2f_bounds_check(khlen); |
| 399 |
keyhandle = walk; |
| 400 |
u2f_advance(khlen); |
| 401 |
|
| 402 |
// Attestation certificate |
| 403 |
u2f_bounds_check(1); |
| 404 |
signature = walk; |
| 405 |
if ((x509 = d2i_X509(NULL, (const unsigned char **)&signature, restlen)) == NULL) { |
| 406 |
error("U2F response contains an invalid attestation certificate."); |
| 407 |
goto out; |
| 408 |
} |
| 409 |
|
| 410 |
// U2F dictates that the length of the certificate should be determined by |
| 411 |
// encoding the certificate using DER. |
| 412 |
u2f_advance(i2d_X509(x509, &dummy)); |
| 413 |
free(dummy); |
| 414 |
|
| 415 |
// Ensure we have at least one byte of signature. |
| 416 |
u2f_bounds_check(1); |
| 417 |
|
| 418 |
if ((clientdata = extract_json_string(response, "clientData")) == NULL) { |
| 419 |
error("U2F response JSON lacks the \"clientData\" key."); |
| 420 |
goto out; |
| 421 |
} |
| 422 |
|
| 423 |
cdecoded = xmalloc(BASE64_DECODED_SIZE(strlen(clientdata))); |
| 424 |
cdecodedlen = urlsafe_base64_decode(clientdata, cdecoded, BASE64_DECODED_SIZE(strlen(clientdata))); |
| 425 |
pkey = X509_get_pubkey(x509); |
| 426 |
|
| 427 |
if ((err = EVP_VerifyInit(&mdctx, EVP_sha256())) != 1) { |
| 428 |
ERR_error_string(ERR_get_error(), errorbuf); |
| 429 |
fatal("EVP_VerifyInit() failed: %s (reason: %s)", |
| 430 |
errorbuf, ERR_reason_error_string(err)); |
| 431 |
} |
| 432 |
EVP_VerifyUpdate(&mdctx, "\0", 1); |
| 433 |
u2f_sha256(digest, appid, strlen(appid)); |
| 434 |
EVP_VerifyUpdate(&mdctx, digest, sizeof(digest)); |
| 435 |
u2f_sha256(digest, cdecoded, cdecodedlen); |
| 436 |
EVP_VerifyUpdate(&mdctx, digest, sizeof(digest)); |
| 437 |
EVP_VerifyUpdate(&mdctx, keyhandle, khlen); |
| 438 |
EVP_VerifyUpdate(&mdctx, pubkey, U2F_PUBKEY_LEN); |
| 439 |
|
| 440 |
err = EVP_VerifyFinal(&mdctx, walk, restlen, pkey); |
| 441 |
if (err == 0) { |
| 442 |
error("Verifying the U2F registration signature failed: invalid signature"); |
| 443 |
goto out; |
| 444 |
} else if (err == -1) { |
| 445 |
long e = ERR_get_error(); |
| 446 |
ERR_error_string(e, errorbuf); |
| 447 |
error("Verifying the U2F registration signature failed: %s (raw %lu) (reason: %s)", |
| 448 |
errorbuf, e, ERR_reason_error_string(err)); |
| 449 |
goto out; |
| 450 |
} |
| 451 |
|
| 452 |
{ |
| 453 |
/* Send the client a ssh-u2f line to append to the authorized_keys file |
| 454 |
* (in order to register the security key that was just used). */ |
| 455 |
char *authorizedkey; |
| 456 |
char key[U2F_PUBKEY_LEN + khlen]; |
| 457 |
char key64[BASE64_ENCODED_SIZE(sizeof(key))]; |
| 458 |
|
| 459 |
memcpy(key, pubkey, U2F_PUBKEY_LEN); |
| 460 |
memcpy(key+U2F_PUBKEY_LEN, keyhandle, khlen); |
| 461 |
|
| 462 |
if (b64_ntop(key, sizeof(key), key64, sizeof(key64)) == -1) |
| 463 |
fatal("b64_ntop(key)"); |
| 464 |
|
| 465 |
xasprintf(&authorizedkey, "ssh-u2f %s my security key", key64); |
| 466 |
packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST); |
| 467 |
packet_put_cstring(authorizedkey); |
| 468 |
packet_send(); |
| 469 |
free(authorizedkey); |
| 470 |
dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL); |
| 471 |
} |
| 472 |
|
| 473 |
out: |
| 474 |
free(regdata); |
| 475 |
free(clientdata); |
| 476 |
free(decoded); |
| 477 |
free(cdecoded); |
| 478 |
if (x509 != NULL) |
| 479 |
X509_free(x509); |
| 480 |
if (pkey != NULL) |
| 481 |
EVP_PKEY_free(pkey); |
| 482 |
userauth_finish(authctxt, 0, "u2f", NULL); |
| 483 |
#undef u2f_bounds_check |
| 484 |
#undef u2f_advance |
| 485 |
} |
| 486 |
|
| 487 |
int |
| 488 |
verify_u2f_user(Key *key, u_char *dgst, size_t dgstlen, u_char *sig, size_t siglen) |
| 489 |
{ |
| 490 |
char errorbuf[4096]; |
| 491 |
int ret = 0; |
| 492 |
EC_KEY *ec; |
| 493 |
u_char *p; |
| 494 |
/* To save bytes, the (common) public key prefix is not included in U2F |
| 495 |
* messages itself. */ |
| 496 |
#define PREFIX_LEN 26 |
| 497 |
#define TOTAL_LEN U2F_PUBKEY_LEN + PREFIX_LEN |
| 498 |
u_char user_pubkey[TOTAL_LEN] = |
| 499 |
"\x30\x59\x30\x13\x06\x07\x2a\x86\x48\xce\x3d\x02\x01\x06\x08\x2a" |
| 500 |
"\x86\x48\xce\x3d\x03\x01\x07\x03\x42\x00"; |
| 501 |
|
| 502 |
memcpy(user_pubkey+PREFIX_LEN, key->u2f_pubkey, U2F_PUBKEY_LEN); |
| 503 |
|
| 504 |
p = user_pubkey; |
| 505 |
if ((ec = d2i_EC_PUBKEY(NULL, (const unsigned char **)&p, TOTAL_LEN)) == NULL) { |
| 506 |
ERR_error_string(ERR_get_error(), errorbuf); |
| 507 |
error("Verifying U2F authentication signature failed: " |
| 508 |
"d2i_EC_PUBKEY() failed: %s (reason: %s)", |
| 509 |
errorbuf, ERR_reason_error_string(ERR_get_error())); |
| 510 |
return 0; |
| 511 |
} |
| 512 |
|
| 513 |
if ((ret = ECDSA_verify(0, dgst, dgstlen, sig, siglen, ec)) == -1) { |
| 514 |
ERR_error_string(ERR_get_error(), errorbuf); |
| 515 |
error("Verifying U2F authentication signature failed: " |
| 516 |
"ECDSA_verify() failed: %s (reason: %s)", |
| 517 |
errorbuf, ERR_reason_error_string(ERR_get_error())); |
| 518 |
goto out; |
| 519 |
} |
| 520 |
|
| 521 |
debug("U2F authentication signature verified: %s.", (ret == 1 ? "valid" : "invalid")); |
| 522 |
|
| 523 |
out: |
| 524 |
EC_KEY_free(ec); |
| 525 |
return (ret == 1); |
| 526 |
#undef TOTAL_LEN |
| 527 |
#undef PREFIX_LEN |
| 528 |
} |
| 529 |
|
| 530 |
static void |
| 531 |
input_userauth_u2f_auth_response(int type, u_int32_t seq, void *ctxt) |
| 532 |
{ |
| 533 |
int authenticated = 0; |
| 534 |
Authctxt *authctxt = ctxt; |
| 535 |
u_char digest[ssh_digest_bytes(SSH_DIGEST_SHA256)]; |
| 536 |
char *sig = NULL; |
| 537 |
char *clientdata = NULL; |
| 538 |
u_char *decoded = NULL; |
| 539 |
int decodedlen; |
| 540 |
u_char *cdecoded = NULL; |
| 541 |
int cdecodedlen; |
| 542 |
char *received_challenge = NULL; |
| 543 |
char *resp = packet_get_string(NULL); |
| 544 |
packet_check_eom(); |
| 545 |
|
| 546 |
if ((sig = extract_json_string(resp, "signatureData")) == NULL) { |
| 547 |
error("U2F Response not JSON, or does not contain \"signatureData\""); |
| 548 |
goto out; |
| 549 |
} |
| 550 |
|
| 551 |
if (*sig == '\0') { |
| 552 |
error("U2F authentication failed: empty signature. " |
| 553 |
"Probably the key is not registered (i.e. the configured " |
| 554 |
"key handle/pubkey do not exist on the security key you are using)"); |
| 555 |
goto out; |
| 556 |
} |
| 557 |
|
| 558 |
decoded = xmalloc(BASE64_DECODED_SIZE(strlen(sig))); |
| 559 |
decodedlen = urlsafe_base64_decode(sig, decoded, BASE64_DECODED_SIZE(strlen(sig))); |
| 560 |
// Ensure that the user presence byte, the counter and at least one byte of |
| 561 |
// signature are present. |
| 562 |
if (decodedlen <= (int)(sizeof(u_char) + sizeof(u_int32_t))) { |
| 563 |
error("Decoded U2F signature too short (%d bytes, expected more than %d bytes)", |
| 564 |
decodedlen, (int)(sizeof(u_char) + sizeof(u_int32_t))); |
| 565 |
goto out; |
| 566 |
} |
| 567 |
if ((decoded[0] & 0x01) != 0x01) { |
| 568 |
error("No user presence detected. Please touch your security key upon " |
| 569 |
"being prompted when retrying."); |
| 570 |
goto out; |
| 571 |
} |
| 572 |
u_int32_t counter = ntohl(*((u_int32_t*)(decoded + sizeof(u_char)))); |
| 573 |
// XXX: Ideally, we would verify that this counter never decreases to |
| 574 |
// detect cloned security keys. However, since OpenSSH never writes any |
| 575 |
// data to disk, we cannot keep track of the counter. |
| 576 |
debug("usage counter = %d\n", counter); |
| 577 |
|
| 578 |
struct ssh_digest_ctx *sha256ctx = ssh_digest_start(SSH_DIGEST_SHA256); |
| 579 |
u2f_sha256(digest, appid, strlen(appid)); |
| 580 |
ssh_digest_update(sha256ctx, digest, sizeof(digest)); |
| 581 |
ssh_digest_update(sha256ctx, decoded, sizeof(u_char)); |
| 582 |
ssh_digest_update(sha256ctx, decoded+1, 4 * sizeof(u_char)); |
| 583 |
|
| 584 |
if ((clientdata = extract_json_string(resp, "clientData")) == NULL) { |
| 585 |
error("U2F response JSON lacks the \"clientData\" key."); |
| 586 |
goto out; |
| 587 |
} |
| 588 |
|
| 589 |
cdecoded = xcalloc(1, BASE64_DECODED_SIZE(strlen(clientdata))+1); |
| 590 |
cdecodedlen = urlsafe_base64_decode(clientdata, cdecoded, BASE64_DECODED_SIZE(strlen(clientdata))); |
| 591 |
|
| 592 |
// XXX: We intentionally do not verify the "origin" field because that |
| 593 |
// would always require end-to-end connectivity, i.e. both server and |
| 594 |
// client need to share the understanding of the server’s hostname. As an |
| 595 |
// example, if the client connects to the server as ssh-gateway.example.net |
| 596 |
// (which could be a CNAME pointing to fra01.example.net), but the server |
| 597 |
// has the hostname fra01.example.net, this would break. |
| 598 |
|
| 599 |
if ((received_challenge = extract_json_string(cdecoded, "challenge")) == NULL) { |
| 600 |
error("U2F response clientData lacks the \"challenge\" key."); |
| 601 |
goto out; |
| 602 |
} |
| 603 |
if (strcmp(received_challenge, authctxt->u2f_challenge) != 0) { |
| 604 |
error("U2F response challenge bytes differ from what was sent. Man in the middle?"); |
| 605 |
free(received_challenge); |
| 606 |
goto out; |
| 607 |
} |
| 608 |
free(received_challenge); |
| 609 |
|
| 610 |
u2f_sha256(digest, cdecoded, cdecodedlen); |
| 611 |
ssh_digest_update(sha256ctx, digest, sizeof(digest)); |
| 612 |
ssh_digest_final(sha256ctx, digest, sizeof(digest)); |
| 613 |
|
| 614 |
authenticated = PRIVSEP(verify_u2f_user( |
| 615 |
authctxt->u2f_key, digest, sizeof(digest), decoded+5, decodedlen-5)); |
| 616 |
|
| 617 |
authctxt->postponed = 0; |
| 618 |
dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL); |
| 619 |
out: |
| 620 |
free(sig); |
| 621 |
free(clientdata); |
| 622 |
free(decoded); |
| 623 |
free(cdecoded); |
| 624 |
authctxt->u2f_attempt++; |
| 625 |
if (authenticated) { |
| 626 |
userauth_finish(authctxt, 1, "u2f", NULL); |
| 627 |
} else { |
| 628 |
// Try again, perhaps there are more keys to use. |
| 629 |
userauth_u2f_authenticate(authctxt); |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
Authmethod method_u2f = { |
| 634 |
"u2f", |
| 635 |
userauth_u2f, |
| 636 |
&options.u2f_authentication |
| 637 |
}; |
| 638 |
|
| 639 |
#endif /* U2F */ |