Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
| 25 | #include "includes.h" |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 26 | RCSID("$OpenBSD: ssh-dss.c,v 1.2 2000/12/19 23:17:58 markus Exp $"); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 27 | |
| 28 | #include "ssh.h" |
| 29 | #include "xmalloc.h" |
| 30 | #include "buffer.h" |
| 31 | #include "bufaux.h" |
| 32 | #include "compat.h" |
| 33 | |
| 34 | #include <openssl/bn.h> |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 35 | #include <openssl/rsa.h> |
| 36 | #include <openssl/dsa.h> |
| 37 | #include <openssl/evp.h> |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 38 | |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 39 | #include "key.h" |
| 40 | |
| 41 | #define INTBLOB_LEN 20 |
| 42 | #define SIGBLOB_LEN (2*INTBLOB_LEN) |
| 43 | |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 44 | int |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 45 | ssh_dss_sign( |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 46 | Key *key, |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 47 | u_char **sigp, int *lenp, |
| 48 | u_char *data, int datalen) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 49 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 50 | u_char *digest; |
| 51 | u_char *ret; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 52 | DSA_SIG *sig; |
| 53 | EVP_MD *evp_md = EVP_sha1(); |
| 54 | EVP_MD_CTX md; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 55 | u_int rlen; |
| 56 | u_int slen; |
| 57 | u_int len; |
| 58 | u_char sigblob[SIGBLOB_LEN]; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 59 | Buffer b; |
| 60 | |
| 61 | if (key == NULL || key->type != KEY_DSA || key->dsa == NULL) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 62 | error("ssh_dss_sign: no DSA key"); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 63 | return -1; |
| 64 | } |
| 65 | digest = xmalloc(evp_md->md_size); |
| 66 | EVP_DigestInit(&md, evp_md); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 67 | EVP_DigestUpdate(&md, data, datalen); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 68 | EVP_DigestFinal(&md, digest, NULL); |
| 69 | |
| 70 | sig = DSA_do_sign(digest, evp_md->md_size, key->dsa); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 71 | if (sig == NULL) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 72 | fatal("ssh_dss_sign: cannot sign"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 73 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 74 | |
| 75 | rlen = BN_num_bytes(sig->r); |
| 76 | slen = BN_num_bytes(sig->s); |
| 77 | if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) { |
| 78 | error("bad sig size %d %d", rlen, slen); |
| 79 | DSA_SIG_free(sig); |
| 80 | return -1; |
| 81 | } |
| 82 | debug("sig size %d %d", rlen, slen); |
| 83 | |
| 84 | memset(sigblob, 0, SIGBLOB_LEN); |
| 85 | BN_bn2bin(sig->r, sigblob+ SIGBLOB_LEN - INTBLOB_LEN - rlen); |
| 86 | BN_bn2bin(sig->s, sigblob+ SIGBLOB_LEN - slen); |
| 87 | DSA_SIG_free(sig); |
| 88 | |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 89 | if (datafellows & SSH_BUG_SIGBLOB) { |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 90 | debug("datafellows"); |
| 91 | ret = xmalloc(SIGBLOB_LEN); |
| 92 | memcpy(ret, sigblob, SIGBLOB_LEN); |
| 93 | if (lenp != NULL) |
| 94 | *lenp = SIGBLOB_LEN; |
| 95 | if (sigp != NULL) |
| 96 | *sigp = ret; |
| 97 | } else { |
| 98 | /* ietf-drafts */ |
| 99 | buffer_init(&b); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 100 | buffer_put_cstring(&b, "ssh-dss"); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 101 | buffer_put_string(&b, sigblob, SIGBLOB_LEN); |
| 102 | len = buffer_len(&b); |
| 103 | ret = xmalloc(len); |
| 104 | memcpy(ret, buffer_ptr(&b), len); |
| 105 | buffer_free(&b); |
| 106 | if (lenp != NULL) |
| 107 | *lenp = len; |
| 108 | if (sigp != NULL) |
| 109 | *sigp = ret; |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | int |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 114 | ssh_dss_verify( |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 115 | Key *key, |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 116 | u_char *signature, int signaturelen, |
| 117 | u_char *data, int datalen) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 118 | { |
| 119 | Buffer b; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 120 | u_char *digest; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 121 | DSA_SIG *sig; |
| 122 | EVP_MD *evp_md = EVP_sha1(); |
| 123 | EVP_MD_CTX md; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 124 | u_char *sigblob; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 125 | char *txt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 126 | u_int len, dlen; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 127 | int rlen; |
| 128 | int ret; |
| 129 | |
| 130 | if (key == NULL || key->type != KEY_DSA || key->dsa == NULL) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 131 | error("ssh_dss_verify: no DSA key"); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 132 | return -1; |
| 133 | } |
| 134 | |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 135 | if (!(datafellows & SSH_BUG_SIGBLOB) && |
| 136 | signaturelen == SIGBLOB_LEN) { |
| 137 | datafellows |= ~SSH_BUG_SIGBLOB; |
| 138 | log("autodetect SSH_BUG_SIGBLOB"); |
| 139 | } else if ((datafellows & SSH_BUG_SIGBLOB) && |
| 140 | signaturelen != SIGBLOB_LEN) { |
| 141 | log("autoremove SSH_BUG_SIGBLOB"); |
| 142 | datafellows &= ~SSH_BUG_SIGBLOB; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | debug("len %d datafellows %d", signaturelen, datafellows); |
| 146 | |
| 147 | /* fetch signature */ |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 148 | if (datafellows & SSH_BUG_SIGBLOB) { |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 149 | sigblob = signature; |
| 150 | len = signaturelen; |
| 151 | } else { |
| 152 | /* ietf-drafts */ |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 153 | char *ktype; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 154 | buffer_init(&b); |
| 155 | buffer_append(&b, (char *) signature, signaturelen); |
| 156 | ktype = buffer_get_string(&b, NULL); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 157 | if (strcmp("ssh-dss", ktype) != 0) { |
| 158 | error("ssh_dss_verify: cannot handle type %s", ktype); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 159 | buffer_free(&b); |
| 160 | return -1; |
| 161 | } |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 162 | sigblob = (u_char *)buffer_get_string(&b, &len); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 163 | rlen = buffer_len(&b); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 164 | if(rlen != 0) { |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 165 | error("remaining bytes in signature %d", rlen); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 166 | buffer_free(&b); |
| 167 | return -1; |
| 168 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 169 | buffer_free(&b); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 170 | xfree(ktype); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | if (len != SIGBLOB_LEN) { |
| 174 | fatal("bad sigbloblen %d != SIGBLOB_LEN", len); |
| 175 | } |
| 176 | |
| 177 | /* parse signature */ |
| 178 | sig = DSA_SIG_new(); |
| 179 | sig->r = BN_new(); |
| 180 | sig->s = BN_new(); |
| 181 | BN_bin2bn(sigblob, INTBLOB_LEN, sig->r); |
| 182 | BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 183 | |
| 184 | if (!(datafellows & SSH_BUG_SIGBLOB)) { |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 185 | memset(sigblob, 0, len); |
| 186 | xfree(sigblob); |
| 187 | } |
| 188 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 189 | /* sha1 the data */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 190 | dlen = evp_md->md_size; |
| 191 | digest = xmalloc(dlen); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 192 | EVP_DigestInit(&md, evp_md); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 193 | EVP_DigestUpdate(&md, data, datalen); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 194 | EVP_DigestFinal(&md, digest, NULL); |
| 195 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 196 | ret = DSA_do_verify(digest, dlen, sig, key->dsa); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 197 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 198 | memset(digest, 0, dlen); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 199 | xfree(digest); |
| 200 | DSA_SIG_free(sig); |
| 201 | |
| 202 | switch (ret) { |
| 203 | case 1: |
| 204 | txt = "correct"; |
| 205 | break; |
| 206 | case 0: |
| 207 | txt = "incorrect"; |
| 208 | break; |
| 209 | case -1: |
| 210 | default: |
| 211 | txt = "error"; |
| 212 | break; |
| 213 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 214 | debug("ssh_dss_verify: signature %s", txt); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 215 | return ret; |
| 216 | } |