Annotation of src/usr.bin/ssh/ssh-ecdsa-sk.c, Revision 1.2
1.2 ! djm 1: /* $OpenBSD: ssh-ecdsa-sk.c,v 1.1 2019/10/31 21:15:14 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,
50: const u_char *data, size_t datalen, u_int compat)
51: {
52: ECDSA_SIG *sig = NULL;
53: BIGNUM *sig_r = NULL, *sig_s = NULL;
54: u_char sig_flags;
55: u_char msghash[32], apphash[32], sighash[32];
56: u_int sig_counter;
57: int ret = SSH_ERR_INTERNAL_ERROR;
58: struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL;
59: char *ktype = NULL;
60: #ifdef DEBUG_SK
61: char *tmp = NULL;
62: #endif
63:
64: if (key == NULL || key->ecdsa == NULL ||
65: sshkey_type_plain(key->type) != KEY_ECDSA_SK ||
66: signature == NULL || signaturelen == 0)
67: return SSH_ERR_INVALID_ARGUMENT;
68:
69: if (key->ecdsa_nid != NID_X9_62_prime256v1)
70: return SSH_ERR_INTERNAL_ERROR;
71:
72: /* fetch signature */
73: if ((b = sshbuf_from(signature, signaturelen)) == NULL)
74: return SSH_ERR_ALLOC_FAIL;
75: if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
1.2 ! djm 76: sshbuf_froms(b, &sigbuf) != 0 ||
! 77: sshbuf_get_u8(b, &sig_flags) != 0 ||
! 78: sshbuf_get_u32(b, &sig_counter) != 0) {
1.1 djm 79: ret = SSH_ERR_INVALID_FORMAT;
80: goto out;
81: }
82: if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
83: ret = SSH_ERR_KEY_TYPE_MISMATCH;
84: goto out;
85: }
86: if (sshbuf_len(b) != 0) {
87: ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
88: goto out;
89: }
90:
91: /* parse signature */
92: if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
1.2 ! djm 93: sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
1.1 djm 94: ret = SSH_ERR_INVALID_FORMAT;
95: goto out;
96: }
97: if ((sig = ECDSA_SIG_new()) == NULL) {
98: ret = SSH_ERR_ALLOC_FAIL;
99: goto out;
100: }
101: if (!ECDSA_SIG_set0(sig, sig_r, sig_s)) {
102: ret = SSH_ERR_LIBCRYPTO_ERROR;
103: goto out;
104: }
105: #ifdef DEBUG_SK
106: fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r)));
107: free(tmp);
108: fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
109: free(tmp);
110: fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
111: __func__, sig_flags, sig_counter);
112: #endif
113: sig_r = sig_s = NULL; /* transferred */
114:
115: if (sshbuf_len(sigbuf) != 0) {
116: ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
117: goto out;
118: }
119:
120: /* Reconstruct data that was supposedly signed */
121: if ((original_signed = sshbuf_new()) == NULL)
122: return SSH_ERR_ALLOC_FAIL;
123: if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, datalen,
124: msghash, sizeof(msghash))) != 0)
125: goto out;
126: /* Application value is hashed before signature */
127: if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
128: strlen(key->sk_application), apphash, sizeof(apphash))) != 0)
129: goto out;
130: #ifdef DEBUG_SK
131: fprintf(stderr, "%s: hashed message:\n", __func__);
132: sshbuf_dump_data(msghash, sizeof(msghash), stderr);
133: #endif
134: if ((ret = sshbuf_put(original_signed,
135: apphash, sizeof(apphash))) != 0 ||
136: (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 ||
137: (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 ||
138: (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0)
139: goto out;
140: /* Signature is over H(original_signed) */
141: if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed,
142: sighash, sizeof(sighash))) != 0)
143: goto out;
144: #ifdef DEBUG_SK
145: fprintf(stderr, "%s: signed buf:\n", __func__);
146: sshbuf_dump(original_signed, stderr);
147: fprintf(stderr, "%s: signed hash:\n", __func__);
148: sshbuf_dump_data(sighash, sizeof(sighash), stderr);
149: #endif
150:
151: /* Verify it */
152: switch (ECDSA_do_verify(sighash, sizeof(sighash), sig, key->ecdsa)) {
153: case 1:
154: ret = 0;
155: break;
156: case 0:
157: ret = SSH_ERR_SIGNATURE_INVALID;
158: goto out;
159: default:
160: ret = SSH_ERR_LIBCRYPTO_ERROR;
161: goto out;
162: }
163:
164: out:
165: explicit_bzero(&sig_flags, sizeof(sig_flags));
166: explicit_bzero(&sig_counter, sizeof(sig_counter));
167: explicit_bzero(msghash, sizeof(msghash));
168: explicit_bzero(sighash, sizeof(msghash));
169: explicit_bzero(apphash, sizeof(apphash));
170: sshbuf_free(original_signed);
171: sshbuf_free(sigbuf);
172: sshbuf_free(b);
173: ECDSA_SIG_free(sig);
174: BN_clear_free(sig_r);
175: BN_clear_free(sig_s);
176: free(ktype);
177: return ret;
178: }