djm@openbsd.org | e0d38ae | 2019-11-26 03:04:27 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-ed25519-sk.c,v 1.4 2019/11/26 03:04:27 djm Exp $ */ |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2019 Markus Friedl. All rights reserved. |
| 4 | * |
| 5 | * Permission to use, copy, modify, and distribute this software for any |
| 6 | * purpose with or without fee is hereby granted, provided that the above |
| 7 | * copyright notice and this permission notice appear in all copies. |
| 8 | * |
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 | */ |
djm@openbsd.org | e0d38ae | 2019-11-26 03:04:27 +0000 | [diff] [blame] | 17 | |
| 18 | /* #define DEBUG_SK 1 */ |
| 19 | |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 20 | #define SSHKEY_INTERNAL |
| 21 | #include <sys/types.h> |
| 22 | #include <limits.h> |
| 23 | |
| 24 | #include "crypto_api.h" |
| 25 | |
| 26 | #include <string.h> |
| 27 | #include <stdarg.h> |
| 28 | |
| 29 | #include "log.h" |
| 30 | #include "sshbuf.h" |
| 31 | #include "sshkey.h" |
| 32 | #include "ssherr.h" |
| 33 | #include "ssh.h" |
| 34 | #include "digest.h" |
| 35 | |
| 36 | int |
| 37 | ssh_ed25519_sk_verify(const struct sshkey *key, |
| 38 | const u_char *signature, size_t signaturelen, |
djm@openbsd.org | b7e74ea | 2019-11-25 00:51:37 +0000 | [diff] [blame] | 39 | const u_char *data, size_t datalen, u_int compat, |
| 40 | struct sshkey_sig_details **detailsp) |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 41 | { |
| 42 | struct sshbuf *b = NULL; |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 43 | struct sshbuf *encoded = NULL; |
| 44 | char *ktype = NULL; |
| 45 | const u_char *sigblob; |
| 46 | const u_char *sm; |
| 47 | u_char *m = NULL; |
| 48 | u_char apphash[32]; |
| 49 | u_char msghash[32]; |
| 50 | u_char sig_flags; |
| 51 | u_int sig_counter; |
| 52 | size_t len; |
| 53 | unsigned long long smlen = 0, mlen = 0; |
| 54 | int r = SSH_ERR_INTERNAL_ERROR; |
| 55 | int ret; |
djm@openbsd.org | b7e74ea | 2019-11-25 00:51:37 +0000 | [diff] [blame] | 56 | struct sshkey_sig_details *details = NULL; |
| 57 | |
| 58 | if (detailsp != NULL) |
| 59 | *detailsp = NULL; |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 60 | |
| 61 | if (key == NULL || |
| 62 | sshkey_type_plain(key->type) != KEY_ED25519_SK || |
| 63 | key->ed25519_pk == NULL || |
| 64 | signature == NULL || signaturelen == 0) |
| 65 | return SSH_ERR_INVALID_ARGUMENT; |
| 66 | |
| 67 | if ((b = sshbuf_from(signature, signaturelen)) == NULL) |
| 68 | return SSH_ERR_ALLOC_FAIL; |
| 69 | if (sshbuf_get_cstring(b, &ktype, NULL) != 0 || |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 70 | sshbuf_get_string_direct(b, &sigblob, &len) != 0 || |
| 71 | sshbuf_get_u8(b, &sig_flags) != 0 || |
| 72 | sshbuf_get_u32(b, &sig_counter) != 0) { |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 73 | r = SSH_ERR_INVALID_FORMAT; |
| 74 | goto out; |
| 75 | } |
djm@openbsd.org | e0d38ae | 2019-11-26 03:04:27 +0000 | [diff] [blame] | 76 | #ifdef DEBUG_SK |
| 77 | fprintf(stderr, "%s: data:\n", __func__); |
| 78 | /* sshbuf_dump_data(data, datalen, stderr); */ |
| 79 | fprintf(stderr, "%s: sigblob:\n", __func__); |
| 80 | sshbuf_dump_data(sigblob, len, stderr); |
| 81 | fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n", |
| 82 | __func__, sig_flags, sig_counter); |
| 83 | #endif |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 84 | if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) { |
| 85 | r = SSH_ERR_KEY_TYPE_MISMATCH; |
| 86 | goto out; |
| 87 | } |
| 88 | if (sshbuf_len(b) != 0) { |
| 89 | r = SSH_ERR_UNEXPECTED_TRAILING_DATA; |
| 90 | goto out; |
| 91 | } |
| 92 | if (len > crypto_sign_ed25519_BYTES) { |
| 93 | r = SSH_ERR_INVALID_FORMAT; |
| 94 | goto out; |
| 95 | } |
| 96 | if (ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application, |
| 97 | strlen(key->sk_application), apphash, sizeof(apphash)) != 0 || |
| 98 | ssh_digest_memory(SSH_DIGEST_SHA256, data, datalen, |
| 99 | msghash, sizeof(msghash)) != 0) { |
| 100 | r = SSH_ERR_INVALID_ARGUMENT; |
| 101 | goto out; |
| 102 | } |
djm@openbsd.org | e0d38ae | 2019-11-26 03:04:27 +0000 | [diff] [blame] | 103 | #ifdef DEBUG_SK |
| 104 | fprintf(stderr, "%s: hashed application:\n", __func__); |
| 105 | sshbuf_dump_data(apphash, sizeof(apphash), stderr); |
| 106 | fprintf(stderr, "%s: hashed message:\n", __func__); |
| 107 | sshbuf_dump_data(msghash, sizeof(msghash), stderr); |
| 108 | #endif |
djm@openbsd.org | b7e74ea | 2019-11-25 00:51:37 +0000 | [diff] [blame] | 109 | if ((details = calloc(1, sizeof(*details))) == NULL) { |
| 110 | r = SSH_ERR_ALLOC_FAIL; |
| 111 | goto out; |
| 112 | } |
| 113 | details->sk_counter = sig_counter; |
| 114 | details->sk_flags = sig_flags; |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 115 | if ((encoded = sshbuf_new()) == NULL) { |
| 116 | r = SSH_ERR_ALLOC_FAIL; |
| 117 | goto out; |
| 118 | } |
| 119 | if (sshbuf_put(encoded, sigblob, len) != 0 || |
| 120 | sshbuf_put(encoded, apphash, sizeof(apphash)) != 0 || |
| 121 | sshbuf_put_u8(encoded, sig_flags) != 0 || |
| 122 | sshbuf_put_u32(encoded, sig_counter) != 0 || |
| 123 | sshbuf_put(encoded, msghash, sizeof(msghash)) != 0) { |
| 124 | r = SSH_ERR_ALLOC_FAIL; |
| 125 | goto out; |
| 126 | } |
djm@openbsd.org | e0d38ae | 2019-11-26 03:04:27 +0000 | [diff] [blame] | 127 | #ifdef DEBUG_SK |
| 128 | fprintf(stderr, "%s: signed buf:\n", __func__); |
| 129 | sshbuf_dump(encoded, stderr); |
| 130 | #endif |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 131 | sm = sshbuf_ptr(encoded); |
| 132 | smlen = sshbuf_len(encoded); |
| 133 | mlen = smlen; |
| 134 | if ((m = malloc(smlen)) == NULL) { |
| 135 | r = SSH_ERR_ALLOC_FAIL; |
| 136 | goto out; |
| 137 | } |
| 138 | if ((ret = crypto_sign_ed25519_open(m, &mlen, sm, smlen, |
| 139 | key->ed25519_pk)) != 0) { |
| 140 | debug2("%s: crypto_sign_ed25519_open failed: %d", |
| 141 | __func__, ret); |
| 142 | } |
| 143 | if (ret != 0 || mlen != smlen - len) { |
| 144 | r = SSH_ERR_SIGNATURE_INVALID; |
| 145 | goto out; |
| 146 | } |
| 147 | /* XXX compare 'm' and 'sm + len' ? */ |
| 148 | /* success */ |
| 149 | r = 0; |
djm@openbsd.org | b7e74ea | 2019-11-25 00:51:37 +0000 | [diff] [blame] | 150 | if (detailsp != NULL) { |
| 151 | *detailsp = details; |
| 152 | details = NULL; |
| 153 | } |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 154 | out: |
| 155 | if (m != NULL) { |
| 156 | explicit_bzero(m, smlen); /* NB mlen may be invalid if r != 0 */ |
| 157 | free(m); |
| 158 | } |
djm@openbsd.org | b7e74ea | 2019-11-25 00:51:37 +0000 | [diff] [blame] | 159 | sshkey_sig_details_free(details); |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 160 | sshbuf_free(b); |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 161 | sshbuf_free(encoded); |
| 162 | free(ktype); |
| 163 | return r; |
| 164 | } |