Coverage Report

Created: 2026-02-24 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssh/ssh-ecdsa-sk.c
Line
Count
Source
1
/* $OpenBSD: ssh-ecdsa-sk.c,v 1.21 2026/02/06 22:59:18 dtucker Exp $ */
2
/*
3
 * Copyright (c) 2000 Markus Friedl.  All rights reserved.
4
 * Copyright (c) 2010 Damien Miller.  All rights reserved.
5
 * Copyright (c) 2019 Google Inc.  All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
28
/* #define DEBUG_SK 1 */
29
30
#include "includes.h"
31
32
#include <sys/types.h>
33
34
#ifdef WITH_OPENSSL
35
#include <openssl/bn.h>
36
#include <openssl/ec.h>
37
#include <openssl/ecdsa.h>
38
#include <openssl/evp.h>
39
#endif
40
41
#include <string.h>
42
#include <stdio.h> /* needed for DEBUG_SK only */
43
44
#include "openbsd-compat/openssl-compat.h"
45
46
#include "sshbuf.h"
47
#include "ssherr.h"
48
#include "digest.h"
49
#define SSHKEY_INTERNAL
50
#include "sshkey.h"
51
52
#ifndef OPENSSL_HAS_ECC
53
/* ARGSUSED */
54
int
55
ssh_ecdsa_sk_verify(const struct sshkey *key,
56
    const u_char *signature, size_t signaturelen,
57
    const u_char *data, size_t datalen, u_int compat,
58
    struct sshkey_sig_details **detailsp)
59
{
60
  return SSH_ERR_FEATURE_UNSUPPORTED;
61
}
62
#else /* OPENSSL_HAS_ECC */
63
64
/* Reuse some ECDSA internals */
65
extern struct sshkey_impl_funcs sshkey_ecdsa_funcs;
66
67
static void
68
ssh_ecdsa_sk_cleanup(struct sshkey *k)
69
9.47k
{
70
9.47k
  sshkey_sk_cleanup(k);
71
9.47k
  sshkey_ecdsa_funcs.cleanup(k);
72
9.47k
}
73
74
static int
75
ssh_ecdsa_sk_equal(const struct sshkey *a, const struct sshkey *b)
76
3.42k
{
77
3.42k
  if (!sshkey_sk_fields_equal(a, b))
78
0
    return 0;
79
3.42k
  if (!sshkey_ecdsa_funcs.equal(a, b))
80
0
    return 0;
81
3.42k
  return 1;
82
3.42k
}
83
84
static int
85
ssh_ecdsa_sk_serialize_public(const struct sshkey *key, struct sshbuf *b,
86
    enum sshkey_serialize_rep opts)
87
15
{
88
15
  int r;
89
90
15
  if ((r = sshkey_ecdsa_funcs.serialize_public(key, b, opts)) != 0)
91
0
    return r;
92
15
  if ((r = sshkey_serialize_sk(key, b)) != 0)
93
0
    return r;
94
95
15
  return 0;
96
15
}
97
98
static int
99
ssh_ecdsa_sk_serialize_private(const struct sshkey *key, struct sshbuf *b,
100
    enum sshkey_serialize_rep opts)
101
0
{
102
0
  int r;
103
104
0
  if (!sshkey_is_cert(key)) {
105
0
    if ((r = sshkey_ecdsa_funcs.serialize_public(key,
106
0
        b, opts)) != 0)
107
0
      return r;
108
0
  }
109
0
  if ((r = sshkey_serialize_private_sk(key, b)) != 0)
110
0
    return r;
111
112
0
  return 0;
113
0
}
114
115
static int
116
ssh_ecdsa_sk_copy_public(const struct sshkey *from, struct sshkey *to)
117
0
{
118
0
  int r;
119
120
0
  if ((r = sshkey_ecdsa_funcs.copy_public(from, to)) != 0)
121
0
    return r;
122
0
  if ((r = sshkey_copy_public_sk(from, to)) != 0)
123
0
    return r;
124
0
  return 0;
125
0
}
126
127
static int
128
ssh_ecdsa_sk_deserialize_public(const char *ktype, struct sshbuf *b,
129
    struct sshkey *key)
130
6.03k
{
131
6.03k
  int r;
132
133
6.03k
  if ((r = sshkey_ecdsa_funcs.deserialize_public(ktype, b, key)) != 0)
134
22
    return r;
135
6.01k
  if ((r = sshkey_deserialize_sk(b, key)) != 0)
136
2
    return r;
137
6.00k
  return 0;
138
6.01k
}
139
140
static int
141
ssh_ecdsa_sk_deserialize_private(const char *ktype, struct sshbuf *b,
142
    struct sshkey *key)
143
3.44k
{
144
3.44k
  int r;
145
146
3.44k
  if (!sshkey_is_cert(key)) {
147
3.44k
    if ((r = sshkey_ecdsa_funcs.deserialize_public(ktype,
148
3.44k
        b, key)) != 0)
149
5
      return r;
150
3.44k
  }
151
3.43k
  if ((r = sshkey_private_deserialize_sk(b, key)) != 0)
152
6
    return r;
153
154
3.43k
  return 0;
155
3.43k
}
156
157
/*
158
 * Check FIDO/W3C webauthn signatures clientData field against the expected
159
 * format and prepare a hash of it for use in signature verification.
160
 *
161
 * webauthn signatures do not sign the hash of the message directly, but
162
 * instead sign a JSON-like "clientData" wrapper structure that contains the
163
 * message hash along with a other information.
164
 *
165
 * Fortunately this structure has a fixed format so it is possible to verify
166
 * that the hash of the signed message is present within the clientData
167
 * structure without needing to implement any JSON parsing.
168
 */
169
static int
170
webauthn_check_prepare_hash(const u_char *data, size_t datalen,
171
    const char *origin, const struct sshbuf *wrapper,
172
    uint8_t flags, const struct sshbuf *extensions,
173
    u_char *msghash, size_t msghashlen)
174
223
{
175
223
  int r = SSH_ERR_INTERNAL_ERROR;
176
223
  struct sshbuf *chall = NULL, *m = NULL;
177
178
223
  if ((m = sshbuf_new()) == NULL ||
179
223
      (chall = sshbuf_from(data, datalen)) == NULL) {
180
0
    r = SSH_ERR_ALLOC_FAIL;
181
0
    goto out;
182
0
  }
183
  /*
184
   * Ensure origin contains no quote character and that the flags are
185
   * consistent with what we received
186
   */
187
223
  if (strchr(origin, '\"') != NULL ||
188
221
      (flags & 0x40) != 0 /* AD */ ||
189
219
      ((flags & 0x80) == 0 /* ED */) != (sshbuf_len(extensions) == 0)) {
190
10
    r = SSH_ERR_INVALID_FORMAT;
191
10
    goto out;
192
10
  }
193
194
  /*
195
   * Prepare the preamble to clientData that we expect, poking the
196
   * challenge and origin into their canonical positions in the
197
   * structure. The crossOrigin flag and any additional extension
198
   * fields present are ignored.
199
   */
200
426
#define WEBAUTHN_0  "{\"type\":\"webauthn.get\",\"challenge\":\""
201
426
#define WEBAUTHN_1  "\",\"origin\":\""
202
426
#define WEBAUTHN_2  "\""
203
213
  if ((r = sshbuf_put(m, WEBAUTHN_0, sizeof(WEBAUTHN_0) - 1)) != 0 ||
204
213
      (r = sshbuf_dtourlb64(chall, m, 0)) != 0 ||
205
213
      (r = sshbuf_put(m, WEBAUTHN_1, sizeof(WEBAUTHN_1) - 1)) != 0 ||
206
213
      (r = sshbuf_put(m, origin, strlen(origin))) != 0 ||
207
213
      (r = sshbuf_put(m, WEBAUTHN_2, sizeof(WEBAUTHN_2) - 1)) != 0)
208
0
    goto out;
209
#ifdef DEBUG_SK
210
  fprintf(stderr, "%s: received origin: %s\n", __func__, origin);
211
  fprintf(stderr, "%s: received clientData:\n", __func__);
212
  sshbuf_dump(wrapper, stderr);
213
  fprintf(stderr, "%s: expected clientData preamble:\n", __func__);
214
  sshbuf_dump(m, stderr);
215
#endif
216
  /* Check that the supplied clientData has the preamble we expect */
217
213
  if ((r = sshbuf_cmp(wrapper, 0, sshbuf_ptr(m), sshbuf_len(m))) != 0)
218
213
    goto out;
219
220
  /* Prepare hash of clientData */
221
0
  if ((r = ssh_digest_buffer(SSH_DIGEST_SHA256, wrapper,
222
0
      msghash, msghashlen)) != 0)
223
0
    goto out;
224
225
  /* success */
226
0
  r = 0;
227
223
 out:
228
223
  sshbuf_free(chall);
229
223
  sshbuf_free(m);
230
223
  return r;
231
0
}
232
233
static int
234
ssh_ecdsa_sk_verify(const struct sshkey *key,
235
    const u_char *sig, size_t siglen,
236
    const u_char *data, size_t dlen, const char *alg, u_int compat,
237
    struct sshkey_sig_details **detailsp)
238
863
{
239
863
  ECDSA_SIG *esig = NULL;
240
863
  EVP_MD_CTX *md_ctx = NULL;
241
863
  BIGNUM *sig_r = NULL, *sig_s = NULL;
242
863
  u_char sig_flags;
243
863
  u_char msghash[32], apphash[32];
244
863
  u_int sig_counter;
245
863
  u_char *sigb = NULL, *cp;
246
863
  int is_webauthn = 0, ret = SSH_ERR_INTERNAL_ERROR, len = 0;
247
863
  struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL;
248
863
  struct sshbuf *webauthn_wrapper = NULL, *webauthn_exts = NULL;
249
863
  char *ktype = NULL, *webauthn_origin = NULL;
250
863
  struct sshkey_sig_details *details = NULL;
251
#ifdef DEBUG_SK
252
  char *tmp = NULL;
253
#endif
254
255
863
  if (detailsp != NULL)
256
0
    *detailsp = NULL;
257
863
  if (key == NULL || key->pkey == NULL ||
258
863
      sshkey_type_plain(key->type) != KEY_ECDSA_SK ||
259
863
      sig == NULL || siglen == 0)
260
0
    return SSH_ERR_INVALID_ARGUMENT;
261
262
863
  if (key->ecdsa_nid != NID_X9_62_prime256v1)
263
0
    return SSH_ERR_INTERNAL_ERROR;
264
265
  /* fetch signature */
266
863
  if ((b = sshbuf_from(sig, siglen)) == NULL)
267
0
    return SSH_ERR_ALLOC_FAIL;
268
863
  if ((details = calloc(1, sizeof(*details))) == NULL) {
269
0
    ret = SSH_ERR_ALLOC_FAIL;
270
0
    goto out;
271
0
  }
272
863
  if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
273
27
    ret = SSH_ERR_INVALID_FORMAT;
274
27
    goto out;
275
27
  }
276
836
  if (strcmp(ktype, "webauthn-sk-ecdsa-sha2-nistp256@openssh.com") == 0 ||
277
591
      strcmp(ktype, "webauthn-sk-ecdsa-sha2-nistp256-cert-v01@openssh.com")
278
591
        == 0)
279
247
    is_webauthn = 1;
280
589
  else if (strcmp(ktype, "sk-ecdsa-sha2-nistp256@openssh.com") != 0) {
281
550
    ret = SSH_ERR_INVALID_FORMAT;
282
550
    goto out;
283
550
  }
284
286
  if (sshbuf_froms(b, &sigbuf) != 0 ||
285
278
      sshbuf_get_u8(b, &sig_flags) != 0 ||
286
276
      sshbuf_get_u32(b, &sig_counter) != 0) {
287
13
    ret = SSH_ERR_INVALID_FORMAT;
288
13
    goto out;
289
13
  }
290
273
  if (is_webauthn) {
291
243
    if (sshbuf_get_cstring(b, &webauthn_origin, NULL) != 0 ||
292
239
        sshbuf_froms(b, &webauthn_wrapper) != 0 ||
293
235
        sshbuf_froms(b, &webauthn_exts) != 0) {
294
13
      ret = SSH_ERR_INVALID_FORMAT;
295
13
      goto out;
296
13
    }
297
243
  }
298
260
  if (sshbuf_len(b) != 0) {
299
3
    ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
300
3
    goto out;
301
3
  }
302
303
  /* parse signature */
304
257
  if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
305
252
      sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
306
7
    ret = SSH_ERR_INVALID_FORMAT;
307
7
    goto out;
308
7
  }
309
250
  if (sshbuf_len(sigbuf) != 0) {
310
16
    ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
311
16
    goto out;
312
16
  }
313
314
#ifdef DEBUG_SK
315
  fprintf(stderr, "%s: data: (len %zu)\n", __func__, dlen);
316
  /* sshbuf_dump_data(data, datalen, stderr); */
317
  fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r)));
318
  free(tmp);
319
  fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
320
  free(tmp);
321
  fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
322
      __func__, sig_flags, sig_counter);
323
  if (is_webauthn) {
324
    fprintf(stderr, "%s: webauthn origin: %s\n", __func__,
325
        webauthn_origin);
326
    fprintf(stderr, "%s: webauthn_wrapper:\n", __func__);
327
    sshbuf_dump(webauthn_wrapper, stderr);
328
  }
329
#endif
330
234
  if ((esig = ECDSA_SIG_new()) == NULL) {
331
0
    ret = SSH_ERR_ALLOC_FAIL;
332
0
    goto out;
333
0
  }
334
234
  if (!ECDSA_SIG_set0(esig, sig_r, sig_s)) {
335
0
    ret = SSH_ERR_LIBCRYPTO_ERROR;
336
0
    goto out;
337
0
  }
338
234
  sig_r = sig_s = NULL; /* transferred */
339
340
  /* Reconstruct data that was supposedly signed */
341
234
  if ((original_signed = sshbuf_new()) == NULL) {
342
0
    ret = SSH_ERR_ALLOC_FAIL;
343
0
    goto out;
344
0
  }
345
234
  if (is_webauthn) {
346
223
    if ((ret = webauthn_check_prepare_hash(data, dlen,
347
223
        webauthn_origin, webauthn_wrapper, sig_flags, webauthn_exts,
348
223
        msghash, sizeof(msghash))) != 0)
349
223
      goto out;
350
223
  } else if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, dlen,
351
11
      msghash, sizeof(msghash))) != 0)
352
0
    goto out;
353
  /* Application value is hashed before signature */
354
11
  if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
355
11
      strlen(key->sk_application), apphash, sizeof(apphash))) != 0)
356
0
    goto out;
357
#ifdef DEBUG_SK
358
  fprintf(stderr, "%s: hashed application:\n", __func__);
359
  sshbuf_dump_data(apphash, sizeof(apphash), stderr);
360
  fprintf(stderr, "%s: hashed message:\n", __func__);
361
  sshbuf_dump_data(msghash, sizeof(msghash), stderr);
362
#endif
363
11
  if ((ret = sshbuf_put(original_signed,
364
11
      apphash, sizeof(apphash))) != 0 ||
365
11
      (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 ||
366
11
      (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 ||
367
11
      (ret = sshbuf_putb(original_signed, webauthn_exts)) != 0 ||
368
11
      (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0)
369
0
    goto out;
370
11
  details->sk_counter = sig_counter;
371
11
  details->sk_flags = sig_flags;
372
#ifdef DEBUG_SK
373
  fprintf(stderr, "%s: signed buf:\n", __func__);
374
  sshbuf_dump(original_signed, stderr);
375
#endif
376
377
11
  if ((md_ctx = EVP_MD_CTX_new()) == NULL) {
378
0
    ret = SSH_ERR_ALLOC_FAIL;
379
0
    goto out;
380
0
  }
381
11
  if ((len = i2d_ECDSA_SIG(esig, NULL)) <= 0) {
382
0
    len = 0;
383
0
    ret = SSH_ERR_LIBCRYPTO_ERROR;
384
0
    goto out;
385
0
  }
386
11
  if ((sigb = calloc(1, len)) == NULL) {
387
0
    ret = SSH_ERR_ALLOC_FAIL;
388
0
    goto out;
389
0
  }
390
11
  cp = sigb; /* ASN1_item_i2d increments the pointer past the object */
391
11
  if (i2d_ECDSA_SIG(esig, &cp) != len) {
392
0
    ret = SSH_ERR_LIBCRYPTO_ERROR;
393
0
    goto out;
394
0
  }
395
#ifdef DEBUG_SK
396
  fprintf(stderr, "%s: signed hash:\n", __func__);
397
  sshbuf_dump_data(sigb, len, stderr);
398
#endif
399
  /* Verify it */
400
11
  if (EVP_DigestVerifyInit(md_ctx, NULL, EVP_sha256(), NULL,
401
11
      key->pkey) != 1) {
402
0
    ret = SSH_ERR_LIBCRYPTO_ERROR;
403
0
    goto out;
404
0
  }
405
11
  switch (EVP_DigestVerify(md_ctx, sigb, len,
406
11
      sshbuf_ptr(original_signed), sshbuf_len(original_signed))) {
407
0
  case 1:
408
0
    ret = 0;
409
0
    break;
410
11
  case 0:
411
11
    ret = SSH_ERR_SIGNATURE_INVALID;
412
11
    goto out;
413
0
  default:
414
0
    ret = SSH_ERR_LIBCRYPTO_ERROR;
415
0
    goto out;
416
11
  }
417
  /* success */
418
0
  if (detailsp != NULL) {
419
0
    *detailsp = details;
420
0
    details = NULL;
421
0
  }
422
863
 out:
423
863
  explicit_bzero(&sig_flags, sizeof(sig_flags));
424
863
  explicit_bzero(&sig_counter, sizeof(sig_counter));
425
863
  explicit_bzero(msghash, sizeof(msghash));
426
863
  explicit_bzero(apphash, sizeof(apphash));
427
863
  sshkey_sig_details_free(details);
428
863
  sshbuf_free(webauthn_wrapper);
429
863
  sshbuf_free(webauthn_exts);
430
863
  free(webauthn_origin);
431
863
  sshbuf_free(original_signed);
432
863
  sshbuf_free(sigbuf);
433
863
  sshbuf_free(b);
434
863
  ECDSA_SIG_free(esig);
435
863
  BN_clear_free(sig_r);
436
863
  BN_clear_free(sig_s);
437
863
  free(ktype);
438
863
  freezero(sigb, len);
439
863
  EVP_MD_CTX_free(md_ctx);
440
863
  return ret;
441
0
}
442
443
static const struct sshkey_impl_funcs sshkey_ecdsa_sk_funcs = {
444
  /* .size = */   NULL,
445
  /* .alloc = */    NULL,
446
  /* .cleanup = */  ssh_ecdsa_sk_cleanup,
447
  /* .equal = */    ssh_ecdsa_sk_equal,
448
  /* .ssh_serialize_public = */ ssh_ecdsa_sk_serialize_public,
449
  /* .ssh_deserialize_public = */ ssh_ecdsa_sk_deserialize_public,
450
  /* .ssh_serialize_private = */ ssh_ecdsa_sk_serialize_private,
451
  /* .ssh_deserialize_private = */ ssh_ecdsa_sk_deserialize_private,
452
  /* .generate = */ NULL,
453
  /* .copy_public = */  ssh_ecdsa_sk_copy_public,
454
  /* .sign = */   NULL,
455
  /* .verify = */   ssh_ecdsa_sk_verify,
456
};
457
458
const struct sshkey_impl sshkey_ecdsa_sk_impl = {
459
  /* .name = */   "sk-ecdsa-sha2-nistp256@openssh.com",
460
  /* .shortname = */  "ECDSA-SK",
461
  /* .sigalg = */   NULL,
462
  /* .type = */   KEY_ECDSA_SK,
463
  /* .nid = */    NID_X9_62_prime256v1,
464
  /* .cert = */   0,
465
  /* .sigonly = */  0,
466
  /* .keybits = */  256,
467
  /* .funcs = */    &sshkey_ecdsa_sk_funcs,
468
};
469
470
const struct sshkey_impl sshkey_ecdsa_sk_cert_impl = {
471
  /* .name = */   "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com",
472
  /* .shortname = */  "ECDSA-SK-CERT",
473
  /* .sigalg = */   NULL,
474
  /* .type = */   KEY_ECDSA_SK_CERT,
475
  /* .nid = */    NID_X9_62_prime256v1,
476
  /* .cert = */   1,
477
  /* .sigonly = */  0,
478
  /* .keybits = */  256,
479
  /* .funcs = */    &sshkey_ecdsa_sk_funcs,
480
};
481
482
const struct sshkey_impl sshkey_ecdsa_sk_webauthn_impl = {
483
  /* .name = */   "webauthn-sk-ecdsa-sha2-nistp256@openssh.com",
484
  /* .shortname = */  "ECDSA-SK",
485
  /* .sigalg = */   NULL,
486
  /* .type = */   KEY_ECDSA_SK,
487
  /* .nid = */    NID_X9_62_prime256v1,
488
  /* .cert = */   0,
489
  /* .sigonly = */  1,
490
  /* .keybits = */  256,
491
  /* .funcs = */    &sshkey_ecdsa_sk_funcs,
492
};
493
494
const struct sshkey_impl sshkey_ecdsa_sk_webauthn_cert_impl = {
495
  /* .name = */   "webauthn-sk-ecdsa-sha2-nistp256-cert-v01@openssh.com",
496
  /* .shortname = */  "ECDSA-SK-CERT",
497
  /* .sigalg = */   NULL,
498
  /* .type = */   KEY_ECDSA_SK_CERT,
499
  /* .nid = */    NID_X9_62_prime256v1,
500
  /* .cert = */   1,
501
  /* .sigonly = */  1,
502
  /* .keybits = */  256,
503
  /* .funcs = */    &sshkey_ecdsa_sk_funcs,
504
};
505
506
#endif /* OPENSSL_HAS_ECC */