Annotation of src/usr.bin/ssh/ssh-ecdsa-sk.c, Revision 1.4
1.4 ! djm 1: /* $OpenBSD: ssh-ecdsa-sk.c,v 1.3 2019/11/25 00:38:17 djm Exp $ */
1.1 djm 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 <sys/types.h>
31:
32: #include <openssl/bn.h>
33: #include <openssl/ec.h>
34: #include <openssl/ecdsa.h>
35: #include <openssl/evp.h>
36:
37: #include <string.h>
38: #include <stdio.h> /* needed for DEBUG_SK only */
39:
40: #include "sshbuf.h"
41: #include "ssherr.h"
42: #include "digest.h"
43: #define SSHKEY_INTERNAL
44: #include "sshkey.h"
45:
46: /* ARGSUSED */
47: int
48: ssh_ecdsa_sk_verify(const struct sshkey *key,
49: const u_char *signature, size_t signaturelen,
1.4 ! djm 50: const u_char *data, size_t datalen, u_int compat,
! 51: struct sshkey_sig_details **detailsp)
1.1 djm 52: {
53: ECDSA_SIG *sig = NULL;
54: BIGNUM *sig_r = NULL, *sig_s = NULL;
55: u_char sig_flags;
56: u_char msghash[32], apphash[32], sighash[32];
57: u_int sig_counter;
58: int ret = SSH_ERR_INTERNAL_ERROR;
59: struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL;
60: char *ktype = NULL;
1.4 ! djm 61: struct sshkey_sig_details *details = NULL;
1.1 djm 62: #ifdef DEBUG_SK
63: char *tmp = NULL;
64: #endif
65:
1.4 ! djm 66: if (detailsp != NULL)
! 67: *detailsp = NULL;
1.1 djm 68: if (key == NULL || key->ecdsa == NULL ||
69: sshkey_type_plain(key->type) != KEY_ECDSA_SK ||
70: signature == NULL || signaturelen == 0)
71: return SSH_ERR_INVALID_ARGUMENT;
72:
73: if (key->ecdsa_nid != NID_X9_62_prime256v1)
74: return SSH_ERR_INTERNAL_ERROR;
75:
76: /* fetch signature */
77: if ((b = sshbuf_from(signature, signaturelen)) == NULL)
78: return SSH_ERR_ALLOC_FAIL;
79: if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
1.2 djm 80: sshbuf_froms(b, &sigbuf) != 0 ||
81: sshbuf_get_u8(b, &sig_flags) != 0 ||
82: sshbuf_get_u32(b, &sig_counter) != 0) {
1.1 djm 83: ret = SSH_ERR_INVALID_FORMAT;
84: goto out;
85: }
86: if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
87: ret = SSH_ERR_KEY_TYPE_MISMATCH;
88: goto out;
89: }
90: if (sshbuf_len(b) != 0) {
91: ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
92: goto out;
93: }
94:
95: /* parse signature */
96: if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
1.2 djm 97: sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
1.1 djm 98: ret = SSH_ERR_INVALID_FORMAT;
99: goto out;
100: }
101: if ((sig = ECDSA_SIG_new()) == NULL) {
102: ret = SSH_ERR_ALLOC_FAIL;
103: goto out;
104: }
105: if (!ECDSA_SIG_set0(sig, sig_r, sig_s)) {
106: ret = SSH_ERR_LIBCRYPTO_ERROR;
107: goto out;
108: }
109: #ifdef DEBUG_SK
110: fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r)));
111: free(tmp);
112: fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
113: free(tmp);
114: fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
115: __func__, sig_flags, sig_counter);
116: #endif
117: sig_r = sig_s = NULL; /* transferred */
118:
119: if (sshbuf_len(sigbuf) != 0) {
120: ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
121: goto out;
122: }
123:
124: /* Reconstruct data that was supposedly signed */
1.3 djm 125: if ((original_signed = sshbuf_new()) == NULL) {
126: ret = SSH_ERR_ALLOC_FAIL;
127: goto out;
128: }
1.1 djm 129: if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, datalen,
130: msghash, sizeof(msghash))) != 0)
131: goto out;
132: /* Application value is hashed before signature */
133: if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
134: strlen(key->sk_application), apphash, sizeof(apphash))) != 0)
135: goto out;
136: #ifdef DEBUG_SK
137: fprintf(stderr, "%s: hashed message:\n", __func__);
138: sshbuf_dump_data(msghash, sizeof(msghash), stderr);
139: #endif
140: if ((ret = sshbuf_put(original_signed,
141: apphash, sizeof(apphash))) != 0 ||
142: (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 ||
143: (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 ||
144: (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0)
145: goto out;
146: /* Signature is over H(original_signed) */
147: if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed,
148: sighash, sizeof(sighash))) != 0)
149: goto out;
1.4 ! djm 150: if ((details = calloc(1, sizeof(*details))) == NULL) {
! 151: ret = SSH_ERR_ALLOC_FAIL;
! 152: goto out;
! 153: }
! 154: details->sk_counter = sig_counter;
! 155: details->sk_flags = sig_flags;
1.1 djm 156: #ifdef DEBUG_SK
157: fprintf(stderr, "%s: signed buf:\n", __func__);
158: sshbuf_dump(original_signed, stderr);
159: fprintf(stderr, "%s: signed hash:\n", __func__);
160: sshbuf_dump_data(sighash, sizeof(sighash), stderr);
161: #endif
162:
163: /* Verify it */
164: switch (ECDSA_do_verify(sighash, sizeof(sighash), sig, key->ecdsa)) {
165: case 1:
166: ret = 0;
167: break;
168: case 0:
169: ret = SSH_ERR_SIGNATURE_INVALID;
170: goto out;
171: default:
172: ret = SSH_ERR_LIBCRYPTO_ERROR;
173: goto out;
174: }
1.4 ! djm 175: /* success */
! 176: if (detailsp != NULL) {
! 177: *detailsp = details;
! 178: details = NULL;
! 179: }
1.1 djm 180: out:
181: explicit_bzero(&sig_flags, sizeof(sig_flags));
182: explicit_bzero(&sig_counter, sizeof(sig_counter));
183: explicit_bzero(msghash, sizeof(msghash));
184: explicit_bzero(sighash, sizeof(msghash));
185: explicit_bzero(apphash, sizeof(apphash));
1.4 ! djm 186: sshkey_sig_details_free(details);
1.1 djm 187: sshbuf_free(original_signed);
188: sshbuf_free(sigbuf);
189: sshbuf_free(b);
190: ECDSA_SIG_free(sig);
191: BN_clear_free(sig_r);
192: BN_clear_free(sig_s);
193: free(ktype);
194: return ret;
195: }