xref: /openssh-portable/ssh-ecdsa-sk.c (revision 64bc1210)
1 /* $OpenBSD: ssh-ecdsa-sk.c,v 1.6 2020/06/22 05:56:23 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 /* ARGSUSED */
53 int
54 ssh_ecdsa_sk_verify(const struct sshkey *key,
55     const u_char *signature, size_t signaturelen,
56     const u_char *data, size_t datalen, u_int compat,
57     struct sshkey_sig_details **detailsp)
58 {
59 #ifdef OPENSSL_HAS_ECC
60 	ECDSA_SIG *sig = NULL;
61 	BIGNUM *sig_r = NULL, *sig_s = NULL;
62 	u_char sig_flags;
63 	u_char msghash[32], apphash[32], sighash[32];
64 	u_int sig_counter;
65 	int ret = SSH_ERR_INTERNAL_ERROR;
66 	struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL;
67 	char *ktype = NULL;
68 	struct sshkey_sig_details *details = NULL;
69 #ifdef DEBUG_SK
70 	char *tmp = NULL;
71 #endif
72 
73 	if (detailsp != NULL)
74 		*detailsp = NULL;
75 	if (key == NULL || key->ecdsa == NULL ||
76 	    sshkey_type_plain(key->type) != KEY_ECDSA_SK ||
77 	    signature == NULL || signaturelen == 0)
78 		return SSH_ERR_INVALID_ARGUMENT;
79 
80 	if (key->ecdsa_nid != NID_X9_62_prime256v1)
81 		return SSH_ERR_INTERNAL_ERROR;
82 
83 	/* fetch signature */
84 	if ((b = sshbuf_from(signature, signaturelen)) == NULL)
85 		return SSH_ERR_ALLOC_FAIL;
86 	if ((details = calloc(1, sizeof(*details))) == NULL) {
87 		ret = SSH_ERR_ALLOC_FAIL;
88 		goto out;
89 	}
90 	if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
91 		ret = SSH_ERR_INVALID_FORMAT;
92 		goto out;
93 	}
94 	if (strcmp(ktype, "sk-ecdsa-sha2-nistp256@openssh.com") != 0) {
95 		ret = SSH_ERR_INVALID_FORMAT;
96 		goto out;
97 	}
98 	if (sshbuf_froms(b, &sigbuf) != 0 ||
99 	    sshbuf_get_u8(b, &sig_flags) != 0 ||
100 	    sshbuf_get_u32(b, &sig_counter) != 0) {
101 		ret = SSH_ERR_INVALID_FORMAT;
102 		goto out;
103 	}
104 	if (sshbuf_len(b) != 0) {
105 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
106 		goto out;
107 	}
108 
109 	/* parse signature */
110 	if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
111 	    sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
112 		ret = SSH_ERR_INVALID_FORMAT;
113 		goto out;
114 	}
115 	if (sshbuf_len(sigbuf) != 0) {
116 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
117 		goto out;
118 	}
119 #ifdef DEBUG_SK
120 	fprintf(stderr, "%s: data: (len %zu)\n", __func__, datalen);
121 	/* sshbuf_dump_data(data, datalen, stderr); */
122 	fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r)));
123 	free(tmp);
124 	fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
125 	free(tmp);
126 	fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
127 	    __func__, sig_flags, sig_counter);
128 #endif
129 	if ((sig = ECDSA_SIG_new()) == NULL) {
130 		ret = SSH_ERR_ALLOC_FAIL;
131 		goto out;
132 	}
133 	if (!ECDSA_SIG_set0(sig, sig_r, sig_s)) {
134 		ret = SSH_ERR_LIBCRYPTO_ERROR;
135 		goto out;
136 	}
137 	sig_r = sig_s = NULL; /* transferred */
138 
139 	/* Reconstruct data that was supposedly signed */
140 	if ((original_signed = sshbuf_new()) == NULL) {
141 		ret = SSH_ERR_ALLOC_FAIL;
142 		goto out;
143 	}
144 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, datalen,
145 	    msghash, sizeof(msghash))) != 0)
146 		goto out;
147 	/* Application value is hashed before signature */
148 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
149 	    strlen(key->sk_application), apphash, sizeof(apphash))) != 0)
150 		goto out;
151 #ifdef DEBUG_SK
152 	fprintf(stderr, "%s: hashed application:\n", __func__);
153 	sshbuf_dump_data(apphash, sizeof(apphash), stderr);
154 	fprintf(stderr, "%s: hashed message:\n", __func__);
155 	sshbuf_dump_data(msghash, sizeof(msghash), stderr);
156 #endif
157 	if ((ret = sshbuf_put(original_signed,
158 	    apphash, sizeof(apphash))) != 0 ||
159 	    (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 ||
160 	    (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 ||
161 	    (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0)
162 		goto out;
163 	/* Signature is over H(original_signed) */
164 	if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed,
165 	    sighash, sizeof(sighash))) != 0)
166 		goto out;
167 	details->sk_counter = sig_counter;
168 	details->sk_flags = sig_flags;
169 #ifdef DEBUG_SK
170 	fprintf(stderr, "%s: signed buf:\n", __func__);
171 	sshbuf_dump(original_signed, stderr);
172 	fprintf(stderr, "%s: signed hash:\n", __func__);
173 	sshbuf_dump_data(sighash, sizeof(sighash), stderr);
174 #endif
175 
176 	/* Verify it */
177 	switch (ECDSA_do_verify(sighash, sizeof(sighash), sig, key->ecdsa)) {
178 	case 1:
179 		ret = 0;
180 		break;
181 	case 0:
182 		ret = SSH_ERR_SIGNATURE_INVALID;
183 		goto out;
184 	default:
185 		ret = SSH_ERR_LIBCRYPTO_ERROR;
186 		goto out;
187 	}
188 	/* success */
189 	if (detailsp != NULL) {
190 		*detailsp = details;
191 		details = NULL;
192 	}
193  out:
194 	explicit_bzero(&sig_flags, sizeof(sig_flags));
195 	explicit_bzero(&sig_counter, sizeof(sig_counter));
196 	explicit_bzero(msghash, sizeof(msghash));
197 	explicit_bzero(sighash, sizeof(msghash));
198 	explicit_bzero(apphash, sizeof(apphash));
199 	sshkey_sig_details_free(details);
200 	sshbuf_free(original_signed);
201 	sshbuf_free(sigbuf);
202 	sshbuf_free(b);
203 	ECDSA_SIG_free(sig);
204 	BN_clear_free(sig_r);
205 	BN_clear_free(sig_s);
206 	free(ktype);
207 	return ret;
208 #else
209 	return SSH_ERR_INTERNAL_ERROR;
210 #endif
211 }
212