|
Line 0
Link Here
|
|
|
1 |
#include "includes.h" |
| 2 |
|
| 3 |
#ifdef U2F |
| 4 |
|
| 5 |
#include <ctype.h> |
| 6 |
#include <openssl/x509.h> |
| 7 |
#include <u2f-host.h> |
| 8 |
#include <fcntl.h> |
| 9 |
|
| 10 |
#include "key.h" |
| 11 |
#include "hostfile.h" |
| 12 |
#include "auth.h" |
| 13 |
#include "ssh.h" |
| 14 |
#include "ssh2.h" |
| 15 |
#include "log.h" |
| 16 |
#include "dispatch.h" |
| 17 |
#include "misc.h" |
| 18 |
#include "servconf.h" |
| 19 |
#include "packet.h" |
| 20 |
#include "digest.h" |
| 21 |
#include "xmalloc.h" |
| 22 |
#include "monitor_wrap.h" |
| 23 |
|
| 24 |
// Evaluates to the maximum size that base64-encoding 'size' bytes can have, |
| 25 |
// including one byte for a trailing NULL byte. |
| 26 |
#define BASE64_ENCODED_SIZE(size) (((size)+2)/3)*4 + 1 |
| 27 |
|
| 28 |
// Evaluates to the maximum size that base64-decoding 'size' bytes can have. |
| 29 |
#define BASE64_DECODED_SIZE(size) (size * 3/4) |
| 30 |
|
| 31 |
extern ServerOptions options; |
| 32 |
|
| 33 |
static void input_userauth_u2f_auth_response(int, u_int32_t, void *); |
| 34 |
static void input_userauth_u2f_register_response(int type, u_int32_t seq, void *ctxt); |
| 35 |
|
| 36 |
static const int u2f_challenge_len = 32; |
| 37 |
// TODO: to what should we set the appid? |
| 38 |
static const char *appid = "ssh://localhost"; |
| 39 |
|
| 40 |
void u2f_sha256(u_char *dest, u_char *src, size_t srclen) { |
| 41 |
struct ssh_digest_ctx *ctx = ssh_digest_start(SSH_DIGEST_SHA256); |
| 42 |
ssh_digest_update(ctx, src, srclen); |
| 43 |
ssh_digest_final(ctx, dest, ssh_digest_bytes(SSH_DIGEST_SHA256)); |
| 44 |
} |
| 45 |
|
| 46 |
/* We can get away without a JSON parser because all values in the JSON |
| 47 |
* messages used in U2F are (websafe) base64 encoded, therefore we don’t need |
| 48 |
* to care about escaping at all. We can just look for the starting double |
| 49 |
* quote and take everything until the next double quote. |
| 50 |
*/ |
| 51 |
static char * |
| 52 |
extract_json_string(const char *json, const char *key) |
| 53 |
{ |
| 54 |
char *quotedkey; |
| 55 |
char *keypos; |
| 56 |
char *value; |
| 57 |
char *end; |
| 58 |
int quotedkeylen; |
| 59 |
|
| 60 |
quotedkeylen = xasprintf("edkey, "\"%s\"", key); |
| 61 |
if ((keypos = strstr(json, quotedkey)) == NULL) |
| 62 |
return NULL; |
| 63 |
|
| 64 |
keypos += quotedkeylen; |
| 65 |
if (*keypos == ':') |
| 66 |
keypos++; |
| 67 |
while (*keypos != '\0' && isspace(*keypos)) |
| 68 |
keypos++; |
| 69 |
if (*keypos != '"') |
| 70 |
return NULL; |
| 71 |
keypos++; |
| 72 |
value = xstrdup(keypos); |
| 73 |
if ((end = strchr(value, '"')) == NULL) { |
| 74 |
free(value); |
| 75 |
return NULL; |
| 76 |
} |
| 77 |
*end = '\0'; |
| 78 |
return value; |
| 79 |
} |
| 80 |
|
| 81 |
static int |
| 82 |
urlsafe_base64_decode(const char *base64, u_char *buffer, size_t bufferlen) |
| 83 |
{ |
| 84 |
// U2F uses urlsafe base64, which replaces + with - and / with _, so we |
| 85 |
// need to revert that before base64 decoding. |
| 86 |
char *replaced; |
| 87 |
char *pos; |
| 88 |
|
| 89 |
replaced = xstrdup(base64); |
| 90 |
while ((pos = strchr(replaced, '-')) != NULL) |
| 91 |
*pos = '+'; |
| 92 |
while ((pos = strchr(replaced, '_')) != NULL) |
| 93 |
*pos = '/'; |
| 94 |
|
| 95 |
return b64_pton(replaced, buffer, bufferlen); |
| 96 |
} |
| 97 |
|
| 98 |
static int |
| 99 |
urlsafe_base64_encode(u_char const *src, size_t srclength, char *target, size_t targsize) |
| 100 |
{ |
| 101 |
char *pos; |
| 102 |
int len; |
| 103 |
|
| 104 |
if ((len = b64_ntop(src, srclength, target, targsize)) == -1) |
| 105 |
return -1; |
| 106 |
|
| 107 |
while ((pos = strchr(target, '+')) != NULL) |
| 108 |
*pos = '-'; |
| 109 |
|
| 110 |
while ((pos = strchr(target, '/')) != NULL) |
| 111 |
*pos = '_'; |
| 112 |
|
| 113 |
return len; |
| 114 |
} |
| 115 |
|
| 116 |
static Key* |
| 117 |
read_keyfile(FILE *fp, char *filename, struct passwd *pw, u_long *linenum) |
| 118 |
{ |
| 119 |
// TODO: do we need to use a different constant here? |
| 120 |
char line[SSH_MAX_PUBKEY_BYTES]; |
| 121 |
Key *found = NULL; |
| 122 |
|
| 123 |
while (read_keyfile_line(fp, filename, line, sizeof(line), linenum) != -1) { |
| 124 |
char *cp, *key_options; |
| 125 |
if (found != NULL) |
| 126 |
key_free(found); |
| 127 |
found = key_new(KEY_U2F); |
| 128 |
// TODO: auth_clear_options();? |
| 129 |
|
| 130 |
/* Skip leading whitespace, empty and comment lines. */ |
| 131 |
for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 132 |
; |
| 133 |
if (!*cp || *cp == '\n' || *cp == '#') |
| 134 |
continue; |
| 135 |
|
| 136 |
debug("reading key from line %lu", *linenum); |
| 137 |
if (key_read(found, &cp) != 1) { |
| 138 |
debug("key_read failed, skipping line %lu", *linenum); |
| 139 |
continue; |
| 140 |
} |
| 141 |
debug("key type: %d (u2f = %d)", found->type, KEY_U2F); |
| 142 |
if (found->type == KEY_U2F) { |
| 143 |
//if (key_equal(found, key)) { |
| 144 |
//if (auth_parse_options(pw, key_options, filename, *linenum) != 1) |
| 145 |
// continue; |
| 146 |
// TODO: calculate and display a fingerprint of the key handle and pubkey? |
| 147 |
debug("matching key found: file %s, line %lu", filename, *linenum); |
| 148 |
// TODO: store multiple matches in authctx->methoddata, or rather authctxt->keys? (see sshconnect2.c) |
| 149 |
return found; |
| 150 |
} |
| 151 |
} |
| 152 |
return NULL; |
| 153 |
} |
| 154 |
|
| 155 |
/* |
| 156 |
* Read a key from the key files. |
| 157 |
*/ |
| 158 |
Key* |
| 159 |
read_user_u2f_key(struct passwd *pw, u_int key_idx) |
| 160 |
{ |
| 161 |
size_t i; |
| 162 |
// TODO: It might not be safe to pass the key back to the unprivileged |
| 163 |
// process. It probably is, but we should review this. |
| 164 |
|
| 165 |
// In the first step, we need to go through all u2f keys that we have and |
| 166 |
// collect their key handles. |
| 167 |
for (i = 0; i < options.num_authkeys_files; i++) { |
| 168 |
FILE *fp; |
| 169 |
char *file; |
| 170 |
Key *key = NULL; |
| 171 |
u_long linenum = 0; |
| 172 |
if (strcasecmp(options.authorized_keys_files[i], "none") == 0) |
| 173 |
continue; |
| 174 |
file = expand_authorized_keys(options.authorized_keys_files[i], pw); |
| 175 |
debug("need to check %s", file); |
| 176 |
fp = fopen(file, "r"); |
| 177 |
do |
| 178 |
{ |
| 179 |
// TODO: Hackish way to allow getting more than one key |
| 180 |
key_free(key); |
| 181 |
key = read_keyfile(fp, file, pw, &linenum); |
| 182 |
} |
| 183 |
while(key_idx-- > 0); |
| 184 |
fclose(fp); |
| 185 |
free(file); |
| 186 |
if (key != NULL) |
| 187 |
return key; |
| 188 |
} |
| 189 |
return NULL; |
| 190 |
} |
| 191 |
|
| 192 |
static int |
| 193 |
userauth_u2f_register(Authctxt *authctxt) |
| 194 |
{ |
| 195 |
u_char random[u2f_challenge_len]; |
| 196 |
char challenge[BASE64_ENCODED_SIZE(sizeof(random))]; |
| 197 |
char *json; |
| 198 |
|
| 199 |
arc4random_buf(random, sizeof(random)); |
| 200 |
if (urlsafe_base64_encode(random, sizeof(random), challenge, sizeof(challenge)) == -1) |
| 201 |
fatal("urlsafe_base64_encode(arc4random_buf()) failed"); |
| 202 |
|
| 203 |
xasprintf(&json, "{\"challenge\": \"%s\", \"version\": \"U2F_V2\", \"appId\": \"%s\"}", |
| 204 |
challenge, appid); |
| 205 |
|
| 206 |
packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST); |
| 207 |
packet_put_cstring(json); |
| 208 |
packet_send(); |
| 209 |
free(json); |
| 210 |
dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, |
| 211 |
&input_userauth_u2f_register_response); |
| 212 |
authctxt->postponed = 1; |
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
static int |
| 217 |
userauth_u2f_authenticate(Authctxt *authctxt) |
| 218 |
{ |
| 219 |
char challenge[BASE64_ENCODED_SIZE(u2f_challenge_len)]; |
| 220 |
char pubkey[BASE64_ENCODED_SIZE(U2F_PUBKEY_LEN)]; |
| 221 |
char *keyhandle; |
| 222 |
char *json; |
| 223 |
Key *key; |
| 224 |
u_int idx = 0; |
| 225 |
|
| 226 |
// Get multiple keys by increasing idx until key == NULL |
| 227 |
// TODO: send multiple challenges for all keys (or something) |
| 228 |
if ((key = PRIVSEP(read_user_u2f_key(authctxt->pw, idx))) == NULL) { |
| 229 |
error("U2F authentication impossible: no ssh-u2f keys found in the authorized keys file(s)."); |
| 230 |
return (0); |
| 231 |
} |
| 232 |
|
| 233 |
packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST); |
| 234 |
authctxt->u2f_challenge = xmalloc(u2f_challenge_len); |
| 235 |
arc4random_buf(authctxt->u2f_challenge, u2f_challenge_len); |
| 236 |
authctxt->u2f_key = key; |
| 237 |
|
| 238 |
if (urlsafe_base64_encode(authctxt->u2f_challenge, u2f_challenge_len, |
| 239 |
challenge, sizeof(challenge)) == -1) |
| 240 |
fatal("urlsafe_base64_encode(arc4random_buf()) failed"); |
| 241 |
|
| 242 |
if (urlsafe_base64_encode(key->u2f_pubkey, U2F_PUBKEY_LEN, pubkey, sizeof(pubkey)) == -1) |
| 243 |
fatal("urlsafe_base64_encode(key->u2f_pubkey) failed"); |
| 244 |
|
| 245 |
keyhandle = xmalloc(BASE64_ENCODED_SIZE(key->u2f_key_handle_len)); |
| 246 |
if (urlsafe_base64_encode(key->u2f_key_handle, key->u2f_key_handle_len, |
| 247 |
keyhandle, BASE64_ENCODED_SIZE(key->u2f_key_handle_len)) == -1) |
| 248 |
fatal("urlsafe_base64_encode(key->u2f_key_handle) failed"); |
| 249 |
|
| 250 |
xasprintf(&json, "{\"challenge\": \"%s\", \"keyHandle\": \"%s\", \"appId\": \"%s\"}", |
| 251 |
challenge, keyhandle, appid); |
| 252 |
packet_put_cstring(json); |
| 253 |
free(json); |
| 254 |
free(keyhandle); |
| 255 |
packet_send(); |
| 256 |
|
| 257 |
dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, |
| 258 |
&input_userauth_u2f_auth_response); |
| 259 |
authctxt->postponed = 1; |
| 260 |
return (0); |
| 261 |
} |
| 262 |
|
| 263 |
static int |
| 264 |
userauth_u2f(Authctxt *authctxt) |
| 265 |
{ |
| 266 |
int mode = packet_get_int(); |
| 267 |
packet_check_eom(); |
| 268 |
// TODO: shared constants |
| 269 |
if (mode == 0) { |
| 270 |
debug("Starting U2F registration"); |
| 271 |
return userauth_u2f_register(authctxt); |
| 272 |
} else if (mode == 1) { |
| 273 |
debug("Starting U2F authentication"); |
| 274 |
return userauth_u2f_authenticate(authctxt); |
| 275 |
} else { |
| 276 |
error("Unknown U2F mode %d requested by the client.", mode); |
| 277 |
return 0; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
static void |
| 282 |
input_userauth_u2f_register_response(int type, u_int32_t seq, void *ctxt) |
| 283 |
{ |
| 284 |
#define u2f_bounds_check(necessary_bytes) do { \ |
| 285 |
if (restlen < necessary_bytes) { \ |
| 286 |
error("U2F response too short: need %d bytes, but only %d remaining", \ |
| 287 |
necessary_bytes, restlen); \ |
| 288 |
goto out; \ |
| 289 |
} \ |
| 290 |
} while (0) |
| 291 |
|
| 292 |
#define u2f_advance(parsed_bytes) do { \ |
| 293 |
int advance = parsed_bytes; \ |
| 294 |
walk += advance; \ |
| 295 |
restlen -= advance; \ |
| 296 |
} while (0) |
| 297 |
|
| 298 |
Authctxt *authctxt = ctxt; |
| 299 |
char *response, *regdata = NULL, *clientdata = NULL; |
| 300 |
u_char *decoded = NULL; |
| 301 |
u_char *walk = NULL; |
| 302 |
u_char *keyhandle = NULL; |
| 303 |
u_char *pubkey = NULL; |
| 304 |
u_char *signature = NULL; |
| 305 |
u_char *dummy = NULL; |
| 306 |
u_char *cdecoded = NULL; |
| 307 |
X509 *x509 = NULL; |
| 308 |
EVP_PKEY *pkey = NULL; |
| 309 |
EVP_MD_CTX mdctx; |
| 310 |
int restlen; |
| 311 |
int khlen; |
| 312 |
int cdecodedlen; |
| 313 |
int err; |
| 314 |
char errorbuf[4096]; |
| 315 |
u_char digest[ssh_digest_bytes(SSH_DIGEST_SHA256)]; |
| 316 |
|
| 317 |
authctxt->postponed = 0; |
| 318 |
|
| 319 |
response = packet_get_string(NULL); |
| 320 |
packet_check_eom(); |
| 321 |
if ((regdata = extract_json_string(response, "registrationData")) == NULL) { |
| 322 |
error("U2F Response not JSON, or does not contain \"registrationData\""); |
| 323 |
goto out; |
| 324 |
} |
| 325 |
|
| 326 |
decoded = xmalloc(BASE64_DECODED_SIZE(strlen(regdata))); |
| 327 |
restlen = urlsafe_base64_decode(regdata, decoded, BASE64_DECODED_SIZE(strlen(regdata))); |
| 328 |
walk = decoded; |
| 329 |
|
| 330 |
// Header (magic byte) |
| 331 |
u2f_bounds_check(1); |
| 332 |
if (walk[0] != 0x05) { |
| 333 |
error("U2F response does not start with magic byte 0x05"); |
| 334 |
goto out; |
| 335 |
} |
| 336 |
u2f_advance(1); |
| 337 |
|
| 338 |
// Length of the public key |
| 339 |
u2f_bounds_check(U2F_PUBKEY_LEN); |
| 340 |
pubkey = walk; |
| 341 |
u2f_advance(U2F_PUBKEY_LEN); |
| 342 |
|
| 343 |
// Length of the key handle |
| 344 |
u2f_bounds_check(1); |
| 345 |
khlen = walk[0]; |
| 346 |
u2f_advance(1); |
| 347 |
|
| 348 |
// Key handle |
| 349 |
u2f_bounds_check(khlen); |
| 350 |
keyhandle = walk; |
| 351 |
u2f_advance(khlen); |
| 352 |
|
| 353 |
// Attestation certificate |
| 354 |
u2f_bounds_check(1); |
| 355 |
signature = walk; |
| 356 |
if ((x509 = d2i_X509(NULL, &signature, restlen)) == NULL) { |
| 357 |
error("U2F response contains an invalid attestation certificate."); |
| 358 |
goto out; |
| 359 |
} |
| 360 |
|
| 361 |
// U2F dictates that the length of the certificate should be determined by |
| 362 |
// encoding the certificate using DER. |
| 363 |
u2f_advance(i2d_X509(x509, &dummy)); |
| 364 |
free(dummy); |
| 365 |
|
| 366 |
// Ensure we have at least one byte of signature. |
| 367 |
u2f_bounds_check(1); |
| 368 |
|
| 369 |
if ((clientdata = extract_json_string(response, "clientData")) == NULL) { |
| 370 |
error("U2F response JSON lacks the \"clientData\" key."); |
| 371 |
goto out; |
| 372 |
} |
| 373 |
|
| 374 |
cdecoded = xmalloc(BASE64_DECODED_SIZE(strlen(clientdata))); |
| 375 |
cdecodedlen = urlsafe_base64_decode(clientdata, cdecoded, BASE64_DECODED_SIZE(strlen(clientdata))); |
| 376 |
pkey = X509_get_pubkey(x509); |
| 377 |
|
| 378 |
if ((err = EVP_VerifyInit(&mdctx, EVP_ecdsa())) != 1) { |
| 379 |
ERR_error_string(ERR_get_error(), errorbuf); |
| 380 |
fatal("EVP_VerifyInit() failed: %s (reason: %s)", |
| 381 |
errorbuf, ERR_reason_error_string(err)); |
| 382 |
} |
| 383 |
EVP_VerifyUpdate(&mdctx, "\0", 1); |
| 384 |
u2f_sha256(digest, appid, strlen(appid)); |
| 385 |
EVP_VerifyUpdate(&mdctx, digest, sizeof(digest)); |
| 386 |
u2f_sha256(digest, cdecoded, cdecodedlen); |
| 387 |
EVP_VerifyUpdate(&mdctx, digest, sizeof(digest)); |
| 388 |
EVP_VerifyUpdate(&mdctx, keyhandle, khlen); |
| 389 |
EVP_VerifyUpdate(&mdctx, pubkey, U2F_PUBKEY_LEN); |
| 390 |
|
| 391 |
if ((err = EVP_VerifyFinal(&mdctx, walk, restlen, pkey)) == -1) { |
| 392 |
ERR_error_string(ERR_get_error(), errorbuf); |
| 393 |
error("Verifying the U2F registration signature failed: %s (reason: %s)", |
| 394 |
errorbuf, ERR_reason_error_string(err)); |
| 395 |
goto out; |
| 396 |
} |
| 397 |
|
| 398 |
{ |
| 399 |
/* Send the client a ssh-u2f line to append to the authorized_keys file |
| 400 |
* (in order to register the security key that was just used). */ |
| 401 |
char *authorizedkey; |
| 402 |
char key[U2F_PUBKEY_LEN + khlen]; |
| 403 |
char key64[BASE64_ENCODED_SIZE(sizeof(key))]; |
| 404 |
|
| 405 |
memcpy(key, pubkey, U2F_PUBKEY_LEN); |
| 406 |
memcpy(key+U2F_PUBKEY_LEN, keyhandle, khlen); |
| 407 |
|
| 408 |
if (b64_ntop(key, sizeof(key), key64, sizeof(key64)) == -1) |
| 409 |
fatal("b64_ntop(key)"); |
| 410 |
|
| 411 |
xasprintf(&authorizedkey, "ssh-u2f %s my security key", key64); |
| 412 |
packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST); |
| 413 |
packet_put_cstring(authorizedkey); |
| 414 |
packet_send(); |
| 415 |
free(authorizedkey); |
| 416 |
dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL); |
| 417 |
} |
| 418 |
|
| 419 |
out: |
| 420 |
free(regdata); |
| 421 |
free(clientdata); |
| 422 |
free(decoded); |
| 423 |
free(cdecoded); |
| 424 |
if (x509 != NULL) |
| 425 |
X509_free(x509); |
| 426 |
if (pkey != NULL) |
| 427 |
EVP_PKEY_free(pkey); |
| 428 |
userauth_finish(authctxt, 0, "u2f", NULL); |
| 429 |
#undef u2f_bounds_check |
| 430 |
#undef u2f_advance |
| 431 |
} |
| 432 |
|
| 433 |
int |
| 434 |
verify_u2f_user(Key *key, u_char *dgst, size_t dgstlen, u_char *sig, size_t siglen) |
| 435 |
{ |
| 436 |
char errorbuf[4096]; |
| 437 |
int ret = 0; |
| 438 |
EC_KEY *ec; |
| 439 |
u_char *p; |
| 440 |
/* To save bytes, the (common) public key prefix is not included in U2F |
| 441 |
* messages itself. */ |
| 442 |
const int prefix_len = 26; |
| 443 |
const int total_len = U2F_PUBKEY_LEN + prefix_len; |
| 444 |
u_char user_pubkey[total_len] = |
| 445 |
"\x30\x59\x30\x13\x06\x07\x2a\x86\x48\xce\x3d\x02\x01\x06\x08\x2a" |
| 446 |
"\x86\x48\xce\x3d\x03\x01\x07\x03\x42\x00"; |
| 447 |
|
| 448 |
memcpy(user_pubkey+prefix_len, key->u2f_pubkey, U2F_PUBKEY_LEN); |
| 449 |
|
| 450 |
p = user_pubkey; |
| 451 |
if ((ec = d2i_EC_PUBKEY(NULL, &p, total_len)) == NULL) { |
| 452 |
ERR_error_string(ERR_get_error(), errorbuf); |
| 453 |
error("Verifying U2F authentication signature failed: " |
| 454 |
"d2i_EC_PUBKEY() failed: %s (reason: %s)", |
| 455 |
errorbuf, ERR_reason_error_string(ERR_get_error())); |
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
if ((ret = ECDSA_verify(0, dgst, dgstlen, sig, siglen, ec)) == -1) { |
| 460 |
ERR_error_string(ERR_get_error(), errorbuf); |
| 461 |
error("Verifying U2F authentication signature failed: " |
| 462 |
"ECDSA_verify() failed: %s (reason: %s)", |
| 463 |
errorbuf, ERR_reason_error_string(ERR_get_error())); |
| 464 |
goto out; |
| 465 |
} |
| 466 |
|
| 467 |
debug("U2F authentication signature verified."); |
| 468 |
|
| 469 |
out: |
| 470 |
EC_KEY_free(ec); |
| 471 |
return (ret == 1); |
| 472 |
} |
| 473 |
|
| 474 |
// TODO: can we send multiple authrequests at the same time, so that we don’t |
| 475 |
// need multiple round-trips but still support multiple security keys |
| 476 |
// TODO: use auth_info() so that in log messages about accepted auths we will see a message that identifies the key. perhaps we can just use the human readable suffix that you can specify in the authorized_keys file(s)? |
| 477 |
static void |
| 478 |
input_userauth_u2f_auth_response(int type, u_int32_t seq, void *ctxt) |
| 479 |
{ |
| 480 |
int authenticated = 0; |
| 481 |
Authctxt *authctxt = ctxt; |
| 482 |
u_char digest[ssh_digest_bytes(SSH_DIGEST_SHA256)]; |
| 483 |
char *sig = NULL; |
| 484 |
char *clientdata = NULL; |
| 485 |
u_char *decoded = NULL; |
| 486 |
int decodedlen; |
| 487 |
u_char *cdecoded = NULL; |
| 488 |
int cdecodedlen; |
| 489 |
char *resp = packet_get_string(NULL); |
| 490 |
packet_check_eom(); |
| 491 |
|
| 492 |
if ((sig = extract_json_string(resp, "signatureData")) == NULL) { |
| 493 |
error("U2F Response not JSON, or does not contain \"signatureData\""); |
| 494 |
goto out; |
| 495 |
} |
| 496 |
|
| 497 |
if (*sig == '\0') { |
| 498 |
error("U2F authentication failed: empty signature. " |
| 499 |
"Probably the key is not registered (i.e. the configured " |
| 500 |
"key handle/pubkey do not exist on the security key you are using)"); |
| 501 |
goto out; |
| 502 |
} |
| 503 |
|
| 504 |
decoded = xmalloc(BASE64_DECODED_SIZE(strlen(sig))); |
| 505 |
decodedlen = urlsafe_base64_decode(sig, decoded, BASE64_DECODED_SIZE(strlen(sig))); |
| 506 |
// Ensure that the user presence byte, the counter and at least one byte of |
| 507 |
// signature are present. |
| 508 |
if (decodedlen <= (sizeof(u_char) + sizeof(u_int32_t))) { |
| 509 |
error("Decoded U2F signature too short (%d bytes, expected more than %d bytes)", |
| 510 |
decodedlen, sizeof(u_char) + sizeof(u_int32_t)); |
| 511 |
goto out; |
| 512 |
} |
| 513 |
if ((decoded[0] & 0x01) != 0x01) { |
| 514 |
error("No user presence detected. Please touch your security key upon " |
| 515 |
"being prompted when retrying."); |
| 516 |
goto out; |
| 517 |
} |
| 518 |
u_int32_t counter = ntohl(*((u_int32_t*)(decoded + sizeof(u_char)))); |
| 519 |
// TODO: Ideally, we would verify that this counter never decreases to |
| 520 |
// detect cloned security keys. |
| 521 |
debug("usage counter = %d\n", counter); |
| 522 |
|
| 523 |
struct sha_digest_ctx *sha256ctx = ssh_digest_start(SSH_DIGEST_SHA256); |
| 524 |
u2f_sha256(digest, appid, strlen(appid)); |
| 525 |
ssh_digest_update(sha256ctx, digest, sizeof(digest)); |
| 526 |
ssh_digest_update(sha256ctx, decoded, sizeof(u_char)); |
| 527 |
ssh_digest_update(sha256ctx, decoded+1, 4 * sizeof(u_char)); |
| 528 |
|
| 529 |
if ((clientdata = extract_json_string(resp, "clientData")) == NULL) { |
| 530 |
error("U2F response JSON lacks the \"clientData\" key."); |
| 531 |
goto out; |
| 532 |
} |
| 533 |
|
| 534 |
// TODO: verify that the challenge and appid is identical to what we sent out, to verify that there was no man in the middle. |
| 535 |
|
| 536 |
cdecoded = xmalloc(BASE64_DECODED_SIZE(strlen(clientdata))); |
| 537 |
cdecodedlen = urlsafe_base64_decode(clientdata, cdecoded, BASE64_DECODED_SIZE(strlen(clientdata))); |
| 538 |
u2f_sha256(digest, cdecoded, cdecodedlen); |
| 539 |
ssh_digest_update(sha256ctx, digest, sizeof(digest)); |
| 540 |
ssh_digest_final(sha256ctx, digest, sizeof(digest)); |
| 541 |
|
| 542 |
authenticated = PRIVSEP(verify_u2f_user( |
| 543 |
authctxt->u2f_key, digest, sizeof(digest), decoded+5, decodedlen-5)); |
| 544 |
|
| 545 |
authctxt->postponed = 0; |
| 546 |
dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL); |
| 547 |
out: |
| 548 |
free(sig); |
| 549 |
free(clientdata); |
| 550 |
free(decoded); |
| 551 |
free(cdecoded); |
| 552 |
userauth_finish(authctxt, authenticated, "u2f", NULL); |
| 553 |
} |
| 554 |
|
| 555 |
Authmethod method_u2f = { |
| 556 |
"u2f", |
| 557 |
userauth_u2f, |
| 558 |
&options.u2f_authentication |
| 559 |
}; |
| 560 |
|
| 561 |
#endif /* U2F */ |