jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-dss.c,v 1.37 2018/02/07 02:06:51 jsing 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 | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 28 | #ifdef WITH_OPENSSL |
| 29 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 32 | #include <openssl/bn.h> |
Damien Miller | 3f022b5 | 2014-08-19 11:32:34 +1000 | [diff] [blame] | 33 | #include <openssl/dsa.h> |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 34 | #include <openssl/evp.h> |
| 35 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 36 | #include <stdarg.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 37 | #include <string.h> |
| 38 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 39 | #include "sshbuf.h" |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 40 | #include "compat.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 41 | #include "ssherr.h" |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 42 | #include "digest.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 43 | #define SSHKEY_INTERNAL |
| 44 | #include "sshkey.h" |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 45 | |
Damien Miller | 48f54b9 | 2018-09-13 12:13:50 +1000 | [diff] [blame] | 46 | #include "openbsd-compat/openssl-compat.h" |
| 47 | |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 48 | #define INTBLOB_LEN 20 |
| 49 | #define SIGBLOB_LEN (2*INTBLOB_LEN) |
| 50 | |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 51 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 52 | ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp, |
| 53 | const u_char *data, size_t datalen, u_int compat) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 54 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 55 | DSA_SIG *sig = NULL; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 56 | const BIGNUM *sig_r, *sig_s; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 57 | u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN]; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 58 | size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1); |
| 59 | struct sshbuf *b = NULL; |
| 60 | int ret = SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 61 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 62 | if (lenp != NULL) |
| 63 | *lenp = 0; |
| 64 | if (sigp != NULL) |
| 65 | *sigp = NULL; |
Damien Miller | 3e19295 | 2013-12-29 17:47:50 +1100 | [diff] [blame] | 66 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 67 | if (key == NULL || key->dsa == NULL || |
| 68 | sshkey_type_plain(key->type) != KEY_DSA) |
| 69 | return SSH_ERR_INVALID_ARGUMENT; |
| 70 | if (dlen == 0) |
| 71 | return SSH_ERR_INTERNAL_ERROR; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 72 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 73 | if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen, |
| 74 | digest, sizeof(digest))) != 0) |
| 75 | goto out; |
Ben Lindstrom | c66d436 | 2001-06-09 01:36:21 +0000 | [diff] [blame] | 76 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 77 | if ((sig = DSA_do_sign(digest, dlen, key->dsa)) == NULL) { |
| 78 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 79 | goto out; |
Ben Lindstrom | c66d436 | 2001-06-09 01:36:21 +0000 | [diff] [blame] | 80 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 81 | |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 82 | DSA_SIG_get0(sig, &sig_r, &sig_s); |
| 83 | rlen = BN_num_bytes(sig_r); |
| 84 | slen = BN_num_bytes(sig_s); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 85 | if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 86 | ret = SSH_ERR_INTERNAL_ERROR; |
| 87 | goto out; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 88 | } |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 89 | explicit_bzero(sigblob, SIGBLOB_LEN); |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 90 | BN_bn2bin(sig_r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen); |
| 91 | BN_bn2bin(sig_s, sigblob + SIGBLOB_LEN - slen); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 92 | |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 93 | if ((b = sshbuf_new()) == NULL) { |
| 94 | ret = SSH_ERR_ALLOC_FAIL; |
| 95 | goto out; |
| 96 | } |
| 97 | if ((ret = sshbuf_put_cstring(b, "ssh-dss")) != 0 || |
| 98 | (ret = sshbuf_put_string(b, sigblob, SIGBLOB_LEN)) != 0) |
| 99 | goto out; |
| 100 | |
| 101 | len = sshbuf_len(b); |
| 102 | if (sigp != NULL) { |
| 103 | if ((*sigp = malloc(len)) == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 104 | ret = SSH_ERR_ALLOC_FAIL; |
| 105 | goto out; |
| 106 | } |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 107 | memcpy(*sigp, sshbuf_ptr(b), len); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 108 | } |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 109 | if (lenp != NULL) |
| 110 | *lenp = len; |
| 111 | ret = 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 112 | out: |
| 113 | explicit_bzero(digest, sizeof(digest)); |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 114 | DSA_SIG_free(sig); |
mmcc@openbsd.org | 52d7078 | 2015-12-11 04:21:11 +0000 | [diff] [blame] | 115 | sshbuf_free(b); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 116 | return ret; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 117 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 118 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 119 | int |
| 120 | ssh_dss_verify(const struct sshkey *key, |
| 121 | const u_char *signature, size_t signaturelen, |
| 122 | const u_char *data, size_t datalen, u_int compat) |
| 123 | { |
| 124 | DSA_SIG *sig = NULL; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 125 | BIGNUM *sig_r = NULL, *sig_s = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 126 | u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL; |
| 127 | size_t len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1); |
| 128 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 129 | struct sshbuf *b = NULL; |
| 130 | char *ktype = NULL; |
| 131 | |
| 132 | if (key == NULL || key->dsa == NULL || |
djm@openbsd.org | b6e0140 | 2016-04-21 06:08:02 +0000 | [diff] [blame] | 133 | sshkey_type_plain(key->type) != KEY_DSA || |
| 134 | signature == NULL || signaturelen == 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 135 | return SSH_ERR_INVALID_ARGUMENT; |
| 136 | if (dlen == 0) |
| 137 | return SSH_ERR_INTERNAL_ERROR; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 138 | |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 139 | /* fetch signature */ |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 140 | if ((b = sshbuf_from(signature, signaturelen)) == NULL) |
| 141 | return SSH_ERR_ALLOC_FAIL; |
| 142 | if (sshbuf_get_cstring(b, &ktype, NULL) != 0 || |
| 143 | sshbuf_get_string(b, &sigblob, &len) != 0) { |
| 144 | ret = SSH_ERR_INVALID_FORMAT; |
| 145 | goto out; |
| 146 | } |
| 147 | if (strcmp("ssh-dss", ktype) != 0) { |
| 148 | ret = SSH_ERR_KEY_TYPE_MISMATCH; |
| 149 | goto out; |
| 150 | } |
| 151 | if (sshbuf_len(b) != 0) { |
| 152 | ret = SSH_ERR_UNEXPECTED_TRAILING_DATA; |
| 153 | goto out; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | if (len != SIGBLOB_LEN) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 157 | ret = SSH_ERR_INVALID_FORMAT; |
| 158 | goto out; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* parse signature */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 162 | if ((sig = DSA_SIG_new()) == NULL || |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 163 | (sig_r = BN_new()) == NULL || |
| 164 | (sig_s = BN_new()) == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 165 | ret = SSH_ERR_ALLOC_FAIL; |
| 166 | goto out; |
| 167 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 168 | if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig_r) == NULL) || |
| 169 | (BN_bin2bn(sigblob + INTBLOB_LEN, INTBLOB_LEN, sig_s) == NULL)) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 170 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 171 | goto out; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 172 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 173 | if (!DSA_SIG_set0(sig, sig_r, sig_s)) { |
| 174 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 175 | goto out; |
| 176 | } |
| 177 | sig_r = sig_s = NULL; /* transferred */ |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 178 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 179 | /* sha1 the data */ |
| 180 | if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen, |
| 181 | digest, sizeof(digest))) != 0) |
| 182 | goto out; |
| 183 | |
| 184 | switch (DSA_do_verify(digest, dlen, sig, key->dsa)) { |
| 185 | case 1: |
| 186 | ret = 0; |
| 187 | break; |
| 188 | case 0: |
| 189 | ret = SSH_ERR_SIGNATURE_INVALID; |
| 190 | goto out; |
| 191 | default: |
| 192 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 193 | goto out; |
| 194 | } |
| 195 | |
| 196 | out: |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 197 | explicit_bzero(digest, sizeof(digest)); |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 198 | DSA_SIG_free(sig); |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 199 | BN_clear_free(sig_r); |
| 200 | BN_clear_free(sig_s); |
mmcc@openbsd.org | 52d7078 | 2015-12-11 04:21:11 +0000 | [diff] [blame] | 201 | sshbuf_free(b); |
mmcc@openbsd.org | d59ce08 | 2015-12-10 17:08:40 +0000 | [diff] [blame] | 202 | free(ktype); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 203 | if (sigblob != NULL) { |
| 204 | explicit_bzero(sigblob, len); |
| 205 | free(sigblob); |
| 206 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 207 | return ret; |
| 208 | } |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 209 | #endif /* WITH_OPENSSL */ |