1 /* $OpenBSD: ssh-ecdsa-sk.c,v 1.8 2020/06/22 23:44:27 djm Exp $ */
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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
28 /* #define DEBUG_SK 1 */
32 #include <sys/types.h>
35 #include <openssl/bn.h>
36 #include <openssl/ec.h>
37 #include <openssl/ecdsa.h>
38 #include <openssl/evp.h>
42 #include <stdio.h> /* needed for DEBUG_SK only */
44 #include "openbsd-compat/openssl-compat.h"
49 #define SSHKEY_INTERNAL
52 #ifndef OPENSSL_HAS_ECC
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)
60 return SSH_ERR_FEATURE_UNSUPPORTED;
62 #else /* OPENSSL_HAS_ECC */
65 * Check FIDO/W3C webauthn signatures clientData field against the expected
66 * format and prepare a hash of it for use in signature verification.
68 * webauthn signatures do not sign the hash of the message directly, but
69 * instead sign a JSON-like "clientData" wrapper structure that contains the
70 * message hash along with a other information.
72 * Fortunately this structure has a fixed format so it is possible to verify
73 * that the hash of the signed message is present within the clientData
74 * structure without needing to implement any JSON parsing.
77 webauthn_check_prepare_hash(const u_char *data, size_t datalen,
78 const char *origin, const struct sshbuf *wrapper,
79 uint8_t flags, const struct sshbuf *extensions,
80 u_char *msghash, size_t msghashlen)
82 int r = SSH_ERR_INTERNAL_ERROR;
83 struct sshbuf *chall = NULL, *m = NULL;
85 if ((m = sshbuf_new()) == NULL ||
86 (chall = sshbuf_from(data, datalen)) == NULL) {
87 r = SSH_ERR_ALLOC_FAIL;
91 * Ensure origin contains no quote character and that the flags are
92 * consistent with what we received
94 if (strchr(origin, '\"') != NULL ||
95 (flags & 0x40) != 0 /* AD */ ||
96 ((flags & 0x80) == 0 /* ED */) != (sshbuf_len(extensions) == 0)) {
97 r = SSH_ERR_INVALID_FORMAT;
102 * Prepare the preamble to clientData that we expect, poking the
103 * challenge and origin into their canonical positions in the
104 * structure. The crossOrigin flag and any additional extension
105 * fields present are ignored.
107 #define WEBAUTHN_0 "{\"type\":\"webauthn.get\",\"challenge\":\""
108 #define WEBAUTHN_1 "\",\"origin\":\""
109 #define WEBAUTHN_2 "\""
110 if ((r = sshbuf_put(m, WEBAUTHN_0, sizeof(WEBAUTHN_0) - 1)) != 0 ||
111 (r = sshbuf_dtourlb64(chall, m, 0)) != 0 ||
112 (r = sshbuf_put(m, WEBAUTHN_1, sizeof(WEBAUTHN_1) - 1)) != 0 ||
113 (r = sshbuf_put(m, origin, strlen(origin))) != 0 ||
114 (r = sshbuf_put(m, WEBAUTHN_2, sizeof(WEBAUTHN_2) - 1)) != 0)
117 fprintf(stderr, "%s: received origin: %s\n", __func__, origin);
118 fprintf(stderr, "%s: received clientData:\n", __func__);
119 sshbuf_dump(wrapper, stderr);
120 fprintf(stderr, "%s: expected clientData premable:\n", __func__);
121 sshbuf_dump(m, stderr);
123 /* Check that the supplied clientData has the preamble we expect */
124 if ((r = sshbuf_cmp(wrapper, 0, sshbuf_ptr(m), sshbuf_len(m))) != 0)
127 /* Prepare hash of clientData */
128 if ((r = ssh_digest_buffer(SSH_DIGEST_SHA256, wrapper,
129 msghash, msghashlen)) != 0)
142 ssh_ecdsa_sk_verify(const struct sshkey *key,
143 const u_char *signature, size_t signaturelen,
144 const u_char *data, size_t datalen, u_int compat,
145 struct sshkey_sig_details **detailsp)
147 ECDSA_SIG *sig = NULL;
148 BIGNUM *sig_r = NULL, *sig_s = NULL;
150 u_char msghash[32], apphash[32], sighash[32];
152 int is_webauthn = 0, ret = SSH_ERR_INTERNAL_ERROR;
153 struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL;
154 struct sshbuf *webauthn_wrapper = NULL, *webauthn_exts = NULL;
155 char *ktype = NULL, *webauthn_origin = NULL;
156 struct sshkey_sig_details *details = NULL;
161 if (detailsp != NULL)
163 if (key == NULL || key->ecdsa == NULL ||
164 sshkey_type_plain(key->type) != KEY_ECDSA_SK ||
165 signature == NULL || signaturelen == 0)
166 return SSH_ERR_INVALID_ARGUMENT;
168 if (key->ecdsa_nid != NID_X9_62_prime256v1)
169 return SSH_ERR_INTERNAL_ERROR;
171 /* fetch signature */
172 if ((b = sshbuf_from(signature, signaturelen)) == NULL)
173 return SSH_ERR_ALLOC_FAIL;
174 if ((details = calloc(1, sizeof(*details))) == NULL) {
175 ret = SSH_ERR_ALLOC_FAIL;
178 if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
179 ret = SSH_ERR_INVALID_FORMAT;
182 if (strcmp(ktype, "webauthn-sk-ecdsa-sha2-nistp256@openssh.com") == 0)
184 else if (strcmp(ktype, "sk-ecdsa-sha2-nistp256@openssh.com") != 0) {
185 ret = SSH_ERR_INVALID_FORMAT;
188 if (sshbuf_froms(b, &sigbuf) != 0 ||
189 sshbuf_get_u8(b, &sig_flags) != 0 ||
190 sshbuf_get_u32(b, &sig_counter) != 0) {
191 ret = SSH_ERR_INVALID_FORMAT;
195 if (sshbuf_get_cstring(b, &webauthn_origin, NULL) != 0 ||
196 sshbuf_froms(b, &webauthn_wrapper) != 0 ||
197 sshbuf_froms(b, &webauthn_exts) != 0) {
198 ret = SSH_ERR_INVALID_FORMAT;
202 if (sshbuf_len(b) != 0) {
203 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
207 /* parse signature */
208 if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
209 sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
210 ret = SSH_ERR_INVALID_FORMAT;
213 if (sshbuf_len(sigbuf) != 0) {
214 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
219 fprintf(stderr, "%s: data: (len %zu)\n", __func__, datalen);
220 /* sshbuf_dump_data(data, datalen, stderr); */
221 fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r)));
223 fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
225 fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
226 __func__, sig_flags, sig_counter);
228 fprintf(stderr, "%s: webauthn origin: %s\n", __func__,
230 fprintf(stderr, "%s: webauthn_wrapper:\n", __func__);
231 sshbuf_dump(webauthn_wrapper, stderr);
234 if ((sig = ECDSA_SIG_new()) == NULL) {
235 ret = SSH_ERR_ALLOC_FAIL;
238 if (!ECDSA_SIG_set0(sig, sig_r, sig_s)) {
239 ret = SSH_ERR_LIBCRYPTO_ERROR;
242 sig_r = sig_s = NULL; /* transferred */
244 /* Reconstruct data that was supposedly signed */
245 if ((original_signed = sshbuf_new()) == NULL) {
246 ret = SSH_ERR_ALLOC_FAIL;
250 if ((ret = webauthn_check_prepare_hash(data, datalen,
251 webauthn_origin, webauthn_wrapper, sig_flags, webauthn_exts,
252 msghash, sizeof(msghash))) != 0)
254 } else if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, datalen,
255 msghash, sizeof(msghash))) != 0)
257 /* Application value is hashed before signature */
258 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
259 strlen(key->sk_application), apphash, sizeof(apphash))) != 0)
262 fprintf(stderr, "%s: hashed application:\n", __func__);
263 sshbuf_dump_data(apphash, sizeof(apphash), stderr);
264 fprintf(stderr, "%s: hashed message:\n", __func__);
265 sshbuf_dump_data(msghash, sizeof(msghash), stderr);
267 if ((ret = sshbuf_put(original_signed,
268 apphash, sizeof(apphash))) != 0 ||
269 (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 ||
270 (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 ||
271 (ret = sshbuf_putb(original_signed, webauthn_exts)) != 0 ||
272 (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0)
274 /* Signature is over H(original_signed) */
275 if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed,
276 sighash, sizeof(sighash))) != 0)
278 details->sk_counter = sig_counter;
279 details->sk_flags = sig_flags;
281 fprintf(stderr, "%s: signed buf:\n", __func__);
282 sshbuf_dump(original_signed, stderr);
283 fprintf(stderr, "%s: signed hash:\n", __func__);
284 sshbuf_dump_data(sighash, sizeof(sighash), stderr);
288 switch (ECDSA_do_verify(sighash, sizeof(sighash), sig, key->ecdsa)) {
293 ret = SSH_ERR_SIGNATURE_INVALID;
296 ret = SSH_ERR_LIBCRYPTO_ERROR;
300 if (detailsp != NULL) {
305 explicit_bzero(&sig_flags, sizeof(sig_flags));
306 explicit_bzero(&sig_counter, sizeof(sig_counter));
307 explicit_bzero(msghash, sizeof(msghash));
308 explicit_bzero(sighash, sizeof(msghash));
309 explicit_bzero(apphash, sizeof(apphash));
310 sshkey_sig_details_free(details);
311 sshbuf_free(webauthn_wrapper);
312 sshbuf_free(webauthn_exts);
313 free(webauthn_origin);
314 sshbuf_free(original_signed);
318 BN_clear_free(sig_r);
319 BN_clear_free(sig_s);
324 #endif /* OPENSSL_HAS_ECC */