Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-dss.c,v 1.32 2014/06/24 01:13:21 djm Exp $ */ |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "includes.h" |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 27 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 30 | #include <openssl/bn.h> |
| 31 | #include <openssl/evp.h> |
| 32 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 33 | #include <stdarg.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 34 | #include <string.h> |
| 35 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 36 | #include "sshbuf.h" |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 37 | #include "compat.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 38 | #include "ssherr.h" |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 39 | #include "digest.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 40 | #define SSHKEY_INTERNAL |
| 41 | #include "sshkey.h" |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 42 | |
| 43 | #define INTBLOB_LEN 20 |
| 44 | #define SIGBLOB_LEN (2*INTBLOB_LEN) |
| 45 | |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 46 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 47 | ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp, |
| 48 | const u_char *data, size_t datalen, u_int compat) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 49 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 50 | DSA_SIG *sig = NULL; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 51 | u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN]; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 52 | size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1); |
| 53 | struct sshbuf *b = NULL; |
| 54 | int ret = SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 55 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 56 | if (lenp != NULL) |
| 57 | *lenp = 0; |
| 58 | if (sigp != NULL) |
| 59 | *sigp = NULL; |
Damien Miller | 3e19295 | 2013-12-29 17:47:50 +1100 | [diff] [blame] | 60 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 61 | if (key == NULL || key->dsa == NULL || |
| 62 | sshkey_type_plain(key->type) != KEY_DSA) |
| 63 | return SSH_ERR_INVALID_ARGUMENT; |
| 64 | if (dlen == 0) |
| 65 | return SSH_ERR_INTERNAL_ERROR; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 66 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 67 | if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen, |
| 68 | digest, sizeof(digest))) != 0) |
| 69 | goto out; |
Ben Lindstrom | c66d436 | 2001-06-09 01:36:21 +0000 | [diff] [blame] | 70 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 71 | if ((sig = DSA_do_sign(digest, dlen, key->dsa)) == NULL) { |
| 72 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 73 | goto out; |
Ben Lindstrom | c66d436 | 2001-06-09 01:36:21 +0000 | [diff] [blame] | 74 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 75 | |
| 76 | rlen = BN_num_bytes(sig->r); |
| 77 | slen = BN_num_bytes(sig->s); |
| 78 | if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 79 | ret = SSH_ERR_INTERNAL_ERROR; |
| 80 | goto out; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 81 | } |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 82 | explicit_bzero(sigblob, SIGBLOB_LEN); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 83 | BN_bn2bin(sig->r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen); |
| 84 | BN_bn2bin(sig->s, sigblob + SIGBLOB_LEN - slen); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 85 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 86 | if (compat & SSH_BUG_SIGBLOB) { |
Ben Lindstrom | 2bf759c | 2002-07-07 22:13:31 +0000 | [diff] [blame] | 87 | if (sigp != NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 88 | if ((*sigp = malloc(SIGBLOB_LEN)) == NULL) { |
| 89 | ret = SSH_ERR_ALLOC_FAIL; |
| 90 | goto out; |
| 91 | } |
Ben Lindstrom | 2bf759c | 2002-07-07 22:13:31 +0000 | [diff] [blame] | 92 | memcpy(*sigp, sigblob, SIGBLOB_LEN); |
| 93 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 94 | if (lenp != NULL) |
| 95 | *lenp = SIGBLOB_LEN; |
| 96 | ret = 0; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 97 | } else { |
| 98 | /* ietf-drafts */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 99 | if ((b = sshbuf_new()) == NULL) { |
| 100 | ret = SSH_ERR_ALLOC_FAIL; |
| 101 | goto out; |
| 102 | } |
| 103 | if ((ret = sshbuf_put_cstring(b, "ssh-dss")) != 0 || |
| 104 | (ret = sshbuf_put_string(b, sigblob, SIGBLOB_LEN)) != 0) |
| 105 | goto out; |
| 106 | len = sshbuf_len(b); |
| 107 | if (sigp != NULL) { |
| 108 | if ((*sigp = malloc(len)) == NULL) { |
| 109 | ret = SSH_ERR_ALLOC_FAIL; |
| 110 | goto out; |
| 111 | } |
| 112 | memcpy(*sigp, sshbuf_ptr(b), len); |
| 113 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 114 | if (lenp != NULL) |
| 115 | *lenp = len; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 116 | ret = 0; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 117 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 118 | out: |
| 119 | explicit_bzero(digest, sizeof(digest)); |
| 120 | if (sig != NULL) |
| 121 | DSA_SIG_free(sig); |
| 122 | if (b != NULL) |
| 123 | sshbuf_free(b); |
| 124 | return ret; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 125 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 126 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 127 | int |
| 128 | ssh_dss_verify(const struct sshkey *key, |
| 129 | const u_char *signature, size_t signaturelen, |
| 130 | const u_char *data, size_t datalen, u_int compat) |
| 131 | { |
| 132 | DSA_SIG *sig = NULL; |
| 133 | u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL; |
| 134 | size_t len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1); |
| 135 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 136 | struct sshbuf *b = NULL; |
| 137 | char *ktype = NULL; |
| 138 | |
| 139 | if (key == NULL || key->dsa == NULL || |
| 140 | sshkey_type_plain(key->type) != KEY_DSA) |
| 141 | return SSH_ERR_INVALID_ARGUMENT; |
| 142 | if (dlen == 0) |
| 143 | return SSH_ERR_INTERNAL_ERROR; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 144 | |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 145 | /* fetch signature */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 146 | if (compat & SSH_BUG_SIGBLOB) { |
| 147 | if ((sigblob = malloc(signaturelen)) == NULL) |
| 148 | return SSH_ERR_ALLOC_FAIL; |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 149 | memcpy(sigblob, signature, signaturelen); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 150 | len = signaturelen; |
| 151 | } else { |
| 152 | /* ietf-drafts */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 153 | if ((b = sshbuf_from(signature, signaturelen)) == NULL) |
| 154 | return SSH_ERR_ALLOC_FAIL; |
| 155 | if (sshbuf_get_cstring(b, &ktype, NULL) != 0 || |
| 156 | sshbuf_get_string(b, &sigblob, &len) != 0) { |
| 157 | ret = SSH_ERR_INVALID_FORMAT; |
| 158 | goto out; |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 159 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 160 | if (strcmp("ssh-dss", ktype) != 0) { |
| 161 | ret = SSH_ERR_KEY_TYPE_MISMATCH; |
| 162 | goto out; |
| 163 | } |
| 164 | if (sshbuf_len(b) != 0) { |
| 165 | ret = SSH_ERR_UNEXPECTED_TRAILING_DATA; |
| 166 | goto out; |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 167 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | if (len != SIGBLOB_LEN) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 171 | ret = SSH_ERR_INVALID_FORMAT; |
| 172 | goto out; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* parse signature */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 176 | if ((sig = DSA_SIG_new()) == NULL || |
| 177 | (sig->r = BN_new()) == NULL || |
| 178 | (sig->s = BN_new()) == NULL) { |
| 179 | ret = SSH_ERR_ALLOC_FAIL; |
| 180 | goto out; |
| 181 | } |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 182 | if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 183 | (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL)) { |
| 184 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 185 | goto out; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 186 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 187 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 188 | /* sha1 the data */ |
| 189 | if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen, |
| 190 | digest, sizeof(digest))) != 0) |
| 191 | goto out; |
| 192 | |
| 193 | switch (DSA_do_verify(digest, dlen, sig, key->dsa)) { |
| 194 | case 1: |
| 195 | ret = 0; |
| 196 | break; |
| 197 | case 0: |
| 198 | ret = SSH_ERR_SIGNATURE_INVALID; |
| 199 | goto out; |
| 200 | default: |
| 201 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 202 | goto out; |
| 203 | } |
| 204 | |
| 205 | out: |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 206 | explicit_bzero(digest, sizeof(digest)); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 207 | if (sig != NULL) |
| 208 | DSA_SIG_free(sig); |
| 209 | if (b != NULL) |
| 210 | sshbuf_free(b); |
| 211 | if (ktype != NULL) |
| 212 | free(ktype); |
| 213 | if (sigblob != NULL) { |
| 214 | explicit_bzero(sigblob, len); |
| 215 | free(sigblob); |
| 216 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 217 | return ret; |
| 218 | } |