]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/openssh/ssh-ecdsa-sk.c
LinuxKPI: 802.11: fix compat code for i386
[FreeBSD/FreeBSD.git] / crypto / openssh / ssh-ecdsa-sk.c
1 /* $OpenBSD: ssh-ecdsa-sk.c,v 1.8 2020/06/22 23:44:27 djm 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 /*
65  * Check FIDO/W3C webauthn signatures clientData field against the expected
66  * format and prepare a hash of it for use in signature verification.
67  *
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.
71  *
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.
75  */
76 static int
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)
81 {
82         int r = SSH_ERR_INTERNAL_ERROR;
83         struct sshbuf *chall = NULL, *m = NULL;
84
85         if ((m = sshbuf_new()) == NULL ||
86             (chall = sshbuf_from(data, datalen)) == NULL) {
87                 r = SSH_ERR_ALLOC_FAIL;
88                 goto out;
89         }
90         /*
91          * Ensure origin contains no quote character and that the flags are
92          * consistent with what we received
93          */
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;
98                 goto out;
99         }
100
101         /*
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.
106          */
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)
115                 goto out;
116 #ifdef DEBUG_SK
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);
122 #endif
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)
125                 goto out;
126
127         /* Prepare hash of clientData */
128         if ((r = ssh_digest_buffer(SSH_DIGEST_SHA256, wrapper,
129             msghash, msghashlen)) != 0)
130                 goto out;
131
132         /* success */
133         r = 0;
134  out:
135         sshbuf_free(chall);
136         sshbuf_free(m);
137         return r;
138 }
139
140 /* ARGSUSED */
141 int
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)
146 {
147         ECDSA_SIG *sig = NULL;
148         BIGNUM *sig_r = NULL, *sig_s = NULL;
149         u_char sig_flags;
150         u_char msghash[32], apphash[32], sighash[32];
151         u_int sig_counter;
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;
157 #ifdef DEBUG_SK
158         char *tmp = NULL;
159 #endif
160
161         if (detailsp != NULL)
162                 *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;
167
168         if (key->ecdsa_nid != NID_X9_62_prime256v1)
169                 return SSH_ERR_INTERNAL_ERROR;
170
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;
176                 goto out;
177         }
178         if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
179                 ret = SSH_ERR_INVALID_FORMAT;
180                 goto out;
181         }
182         if (strcmp(ktype, "webauthn-sk-ecdsa-sha2-nistp256@openssh.com") == 0)
183                 is_webauthn = 1;
184         else if (strcmp(ktype, "sk-ecdsa-sha2-nistp256@openssh.com") != 0) {
185                 ret = SSH_ERR_INVALID_FORMAT;
186                 goto out;
187         }
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;
192                 goto out;
193         }
194         if (is_webauthn) {
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;
199                         goto out;
200                 }
201         }
202         if (sshbuf_len(b) != 0) {
203                 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
204                 goto out;
205         }
206
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;
211                 goto out;
212         }
213         if (sshbuf_len(sigbuf) != 0) {
214                 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
215                 goto out;
216         }
217
218 #ifdef DEBUG_SK
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)));
222         free(tmp);
223         fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
224         free(tmp);
225         fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
226             __func__, sig_flags, sig_counter);
227         if (is_webauthn) {
228                 fprintf(stderr, "%s: webauthn origin: %s\n", __func__,
229                     webauthn_origin);
230                 fprintf(stderr, "%s: webauthn_wrapper:\n", __func__);
231                 sshbuf_dump(webauthn_wrapper, stderr);
232         }
233 #endif
234         if ((sig = ECDSA_SIG_new()) == NULL) {
235                 ret = SSH_ERR_ALLOC_FAIL;
236                 goto out;
237         }
238         if (!ECDSA_SIG_set0(sig, sig_r, sig_s)) {
239                 ret = SSH_ERR_LIBCRYPTO_ERROR;
240                 goto out;
241         }
242         sig_r = sig_s = NULL; /* transferred */
243
244         /* Reconstruct data that was supposedly signed */
245         if ((original_signed = sshbuf_new()) == NULL) {
246                 ret = SSH_ERR_ALLOC_FAIL;
247                 goto out;
248         }
249         if (is_webauthn) {
250                 if ((ret = webauthn_check_prepare_hash(data, datalen,
251                     webauthn_origin, webauthn_wrapper, sig_flags, webauthn_exts,
252                     msghash, sizeof(msghash))) != 0)
253                         goto out;
254         } else if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, datalen,
255             msghash, sizeof(msghash))) != 0)
256                 goto out;
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)
260                 goto out;
261 #ifdef DEBUG_SK
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);
266 #endif
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)
273                 goto out;
274         /* Signature is over H(original_signed) */
275         if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed,
276             sighash, sizeof(sighash))) != 0)
277                 goto out;
278         details->sk_counter = sig_counter;
279         details->sk_flags = sig_flags;
280 #ifdef DEBUG_SK
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);
285 #endif
286
287         /* Verify it */
288         switch (ECDSA_do_verify(sighash, sizeof(sighash), sig, key->ecdsa)) {
289         case 1:
290                 ret = 0;
291                 break;
292         case 0:
293                 ret = SSH_ERR_SIGNATURE_INVALID;
294                 goto out;
295         default:
296                 ret = SSH_ERR_LIBCRYPTO_ERROR;
297                 goto out;
298         }
299         /* success */
300         if (detailsp != NULL) {
301                 *detailsp = details;
302                 details = NULL;
303         }
304  out:
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);
315         sshbuf_free(sigbuf);
316         sshbuf_free(b);
317         ECDSA_SIG_free(sig);
318         BN_clear_free(sig_r);
319         BN_clear_free(sig_s);
320         free(ktype);
321         return ret;
322 }
323
324 #endif /* OPENSSL_HAS_ECC */