blob: 40f0dc8c05de31350aa18e9f567ac5b7534faf30 [file] [log] [blame]
djm@openbsd.orge0d38ae2019-11-26 03:04:27 +00001/* $OpenBSD: ssh-ecdsa-sk.c,v 1.5 2019/11/26 03:04:27 djm Exp $ */
djm@openbsd.org02bb0762019-10-31 21:15:14 +00002/*
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
Damien Miller764d51e2019-11-01 13:34:49 +110032#ifdef ENABLE_SK
33
djm@openbsd.org02bb0762019-10-31 21:15:14 +000034#include <sys/types.h>
35
36#include <openssl/bn.h>
37#include <openssl/ec.h>
38#include <openssl/ecdsa.h>
39#include <openssl/evp.h>
40
41#include <string.h>
42#include <stdio.h> /* needed for DEBUG_SK only */
43
Darren Tucker2f95d432019-11-20 16:34:11 +110044#include "openbsd-compat/openssl-compat.h"
45
djm@openbsd.org02bb0762019-10-31 21:15:14 +000046#include "sshbuf.h"
47#include "ssherr.h"
48#include "digest.h"
49#define SSHKEY_INTERNAL
50#include "sshkey.h"
51
52/* ARGSUSED */
53int
54ssh_ecdsa_sk_verify(const struct sshkey *key,
55 const u_char *signature, size_t signaturelen,
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +000056 const u_char *data, size_t datalen, u_int compat,
57 struct sshkey_sig_details **detailsp)
djm@openbsd.org02bb0762019-10-31 21:15:14 +000058{
59 ECDSA_SIG *sig = NULL;
60 BIGNUM *sig_r = NULL, *sig_s = NULL;
61 u_char sig_flags;
62 u_char msghash[32], apphash[32], sighash[32];
63 u_int sig_counter;
64 int ret = SSH_ERR_INTERNAL_ERROR;
65 struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL;
66 char *ktype = NULL;
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +000067 struct sshkey_sig_details *details = NULL;
djm@openbsd.org02bb0762019-10-31 21:15:14 +000068#ifdef DEBUG_SK
69 char *tmp = NULL;
70#endif
71
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +000072 if (detailsp != NULL)
73 *detailsp = NULL;
djm@openbsd.org02bb0762019-10-31 21:15:14 +000074 if (key == NULL || key->ecdsa == NULL ||
75 sshkey_type_plain(key->type) != KEY_ECDSA_SK ||
76 signature == NULL || signaturelen == 0)
77 return SSH_ERR_INVALID_ARGUMENT;
78
79 if (key->ecdsa_nid != NID_X9_62_prime256v1)
80 return SSH_ERR_INTERNAL_ERROR;
81
82 /* fetch signature */
83 if ((b = sshbuf_from(signature, signaturelen)) == NULL)
84 return SSH_ERR_ALLOC_FAIL;
85 if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
djm@openbsd.orga70d92f2019-11-19 22:23:19 +000086 sshbuf_froms(b, &sigbuf) != 0 ||
87 sshbuf_get_u8(b, &sig_flags) != 0 ||
88 sshbuf_get_u32(b, &sig_counter) != 0) {
djm@openbsd.org02bb0762019-10-31 21:15:14 +000089 ret = SSH_ERR_INVALID_FORMAT;
90 goto out;
91 }
92 if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
93 ret = SSH_ERR_KEY_TYPE_MISMATCH;
94 goto out;
95 }
96 if (sshbuf_len(b) != 0) {
97 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
98 goto out;
99 }
100
101 /* parse signature */
102 if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
djm@openbsd.orga70d92f2019-11-19 22:23:19 +0000103 sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
djm@openbsd.org02bb0762019-10-31 21:15:14 +0000104 ret = SSH_ERR_INVALID_FORMAT;
105 goto out;
106 }
107 if ((sig = ECDSA_SIG_new()) == NULL) {
108 ret = SSH_ERR_ALLOC_FAIL;
109 goto out;
110 }
111 if (!ECDSA_SIG_set0(sig, sig_r, sig_s)) {
112 ret = SSH_ERR_LIBCRYPTO_ERROR;
113 goto out;
114 }
115#ifdef DEBUG_SK
djm@openbsd.orge0d38ae2019-11-26 03:04:27 +0000116 fprintf(stderr, "%s: data: (len %zu)\n", __func__, datalen);
117 /* sshbuf_dump_data(data, datalen, stderr); */
djm@openbsd.org02bb0762019-10-31 21:15:14 +0000118 fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r)));
119 free(tmp);
120 fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
121 free(tmp);
122 fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
123 __func__, sig_flags, sig_counter);
124#endif
125 sig_r = sig_s = NULL; /* transferred */
126
127 if (sshbuf_len(sigbuf) != 0) {
128 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
129 goto out;
130 }
131
132 /* Reconstruct data that was supposedly signed */
djm@openbsd.orgd2b0f882019-11-25 00:38:17 +0000133 if ((original_signed = sshbuf_new()) == NULL) {
134 ret = SSH_ERR_ALLOC_FAIL;
135 goto out;
136 }
djm@openbsd.org02bb0762019-10-31 21:15:14 +0000137 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, datalen,
138 msghash, sizeof(msghash))) != 0)
139 goto out;
140 /* Application value is hashed before signature */
141 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
142 strlen(key->sk_application), apphash, sizeof(apphash))) != 0)
143 goto out;
144#ifdef DEBUG_SK
djm@openbsd.orge0d38ae2019-11-26 03:04:27 +0000145 fprintf(stderr, "%s: hashed application:\n", __func__);
146 sshbuf_dump_data(apphash, sizeof(apphash), stderr);
djm@openbsd.org02bb0762019-10-31 21:15:14 +0000147 fprintf(stderr, "%s: hashed message:\n", __func__);
148 sshbuf_dump_data(msghash, sizeof(msghash), stderr);
149#endif
150 if ((ret = sshbuf_put(original_signed,
151 apphash, sizeof(apphash))) != 0 ||
152 (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 ||
153 (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 ||
154 (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0)
155 goto out;
156 /* Signature is over H(original_signed) */
157 if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed,
158 sighash, sizeof(sighash))) != 0)
159 goto out;
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000160 if ((details = calloc(1, sizeof(*details))) == NULL) {
161 ret = SSH_ERR_ALLOC_FAIL;
162 goto out;
163 }
164 details->sk_counter = sig_counter;
165 details->sk_flags = sig_flags;
djm@openbsd.org02bb0762019-10-31 21:15:14 +0000166#ifdef DEBUG_SK
167 fprintf(stderr, "%s: signed buf:\n", __func__);
168 sshbuf_dump(original_signed, stderr);
169 fprintf(stderr, "%s: signed hash:\n", __func__);
170 sshbuf_dump_data(sighash, sizeof(sighash), stderr);
171#endif
172
173 /* Verify it */
174 switch (ECDSA_do_verify(sighash, sizeof(sighash), sig, key->ecdsa)) {
175 case 1:
176 ret = 0;
177 break;
178 case 0:
179 ret = SSH_ERR_SIGNATURE_INVALID;
180 goto out;
181 default:
182 ret = SSH_ERR_LIBCRYPTO_ERROR;
183 goto out;
184 }
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000185 /* success */
186 if (detailsp != NULL) {
187 *detailsp = details;
188 details = NULL;
189 }
djm@openbsd.org02bb0762019-10-31 21:15:14 +0000190 out:
191 explicit_bzero(&sig_flags, sizeof(sig_flags));
192 explicit_bzero(&sig_counter, sizeof(sig_counter));
193 explicit_bzero(msghash, sizeof(msghash));
194 explicit_bzero(sighash, sizeof(msghash));
195 explicit_bzero(apphash, sizeof(apphash));
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000196 sshkey_sig_details_free(details);
djm@openbsd.org02bb0762019-10-31 21:15:14 +0000197 sshbuf_free(original_signed);
198 sshbuf_free(sigbuf);
199 sshbuf_free(b);
200 ECDSA_SIG_free(sig);
201 BN_clear_free(sig_r);
202 BN_clear_free(sig_s);
203 free(ktype);
204 return ret;
205}
Damien Miller764d51e2019-11-01 13:34:49 +1100206#endif /* ENABLE_SK */