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. |
| 12 | * 3. All advertising materials mentioning features or use of this software |
| 13 | * must display the following acknowledgement: |
| 14 | * This product includes software developed by Markus Friedl. |
| 15 | * 4. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include "includes.h" |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 31 | RCSID("$Id: dsa.c,v 1.7 2000/05/08 17:42:24 markus Exp $"); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 32 | |
| 33 | #include "ssh.h" |
| 34 | #include "xmalloc.h" |
| 35 | #include "buffer.h" |
| 36 | #include "bufaux.h" |
| 37 | #include "compat.h" |
| 38 | |
| 39 | #include <openssl/bn.h> |
| 40 | #include <openssl/dh.h> |
| 41 | #include <openssl/rsa.h> |
| 42 | #include <openssl/dsa.h> |
| 43 | #include <openssl/evp.h> |
| 44 | #include <openssl/bio.h> |
| 45 | #include <openssl/pem.h> |
| 46 | |
| 47 | #include <openssl/hmac.h> |
| 48 | #include "kex.h" |
| 49 | #include "key.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 50 | #include "uuencode.h" |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 51 | |
| 52 | #define INTBLOB_LEN 20 |
| 53 | #define SIGBLOB_LEN (2*INTBLOB_LEN) |
| 54 | |
| 55 | Key * |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 56 | dsa_key_from_blob( |
| 57 | char *blob, int blen) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 58 | { |
| 59 | Buffer b; |
| 60 | char *ktype; |
| 61 | int rlen; |
| 62 | DSA *dsa; |
| 63 | Key *key; |
| 64 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 65 | #ifdef DEBUG_DSS |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 66 | dump_base64(stderr, blob, blen); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 67 | #endif |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 68 | /* fetch & parse DSA/DSS pubkey */ |
| 69 | key = key_new(KEY_DSA); |
| 70 | dsa = key->dsa; |
| 71 | buffer_init(&b); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 72 | buffer_append(&b, blob, blen); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 73 | ktype = buffer_get_string(&b, NULL); |
| 74 | if (strcmp(KEX_DSS, ktype) != 0) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 75 | error("dsa_key_from_blob: cannot handle type %s", ktype); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 76 | key_free(key); |
| 77 | return NULL; |
| 78 | } |
| 79 | buffer_get_bignum2(&b, dsa->p); |
| 80 | buffer_get_bignum2(&b, dsa->q); |
| 81 | buffer_get_bignum2(&b, dsa->g); |
| 82 | buffer_get_bignum2(&b, dsa->pub_key); |
| 83 | rlen = buffer_len(&b); |
| 84 | if(rlen != 0) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 85 | error("dsa_key_from_blob: remaining bytes in key blob %d", rlen); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 86 | buffer_free(&b); |
| 87 | |
| 88 | debug("keytype %s", ktype); |
| 89 | #ifdef DEBUG_DSS |
| 90 | DSA_print_fp(stderr, dsa, 8); |
| 91 | #endif |
| 92 | return key; |
| 93 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 94 | int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 95 | dsa_make_key_blob(Key *key, unsigned char **blobp, unsigned int *lenp) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 96 | { |
| 97 | Buffer b; |
| 98 | int len; |
| 99 | unsigned char *buf; |
| 100 | |
| 101 | if (key == NULL || key->type != KEY_DSA) |
| 102 | return 0; |
| 103 | buffer_init(&b); |
| 104 | buffer_put_cstring(&b, KEX_DSS); |
| 105 | buffer_put_bignum2(&b, key->dsa->p); |
| 106 | buffer_put_bignum2(&b, key->dsa->q); |
| 107 | buffer_put_bignum2(&b, key->dsa->g); |
| 108 | buffer_put_bignum2(&b, key->dsa->pub_key); |
| 109 | len = buffer_len(&b); |
| 110 | buf = xmalloc(len); |
| 111 | memcpy(buf, buffer_ptr(&b), len); |
| 112 | memset(buffer_ptr(&b), 0, len); |
| 113 | buffer_free(&b); |
| 114 | if (lenp != NULL) |
| 115 | *lenp = len; |
| 116 | if (blobp != NULL) |
| 117 | *blobp = buf; |
| 118 | return len; |
| 119 | } |
| 120 | int |
| 121 | dsa_sign( |
| 122 | Key *key, |
| 123 | unsigned char **sigp, int *lenp, |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 124 | unsigned char *data, int datalen) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 125 | { |
| 126 | unsigned char *digest; |
| 127 | unsigned char *ret; |
| 128 | DSA_SIG *sig; |
| 129 | EVP_MD *evp_md = EVP_sha1(); |
| 130 | EVP_MD_CTX md; |
| 131 | unsigned int rlen; |
| 132 | unsigned int slen; |
| 133 | unsigned int len; |
| 134 | unsigned char sigblob[SIGBLOB_LEN]; |
| 135 | Buffer b; |
| 136 | |
| 137 | if (key == NULL || key->type != KEY_DSA || key->dsa == NULL) { |
| 138 | error("dsa_sign: no DSA key"); |
| 139 | return -1; |
| 140 | } |
| 141 | digest = xmalloc(evp_md->md_size); |
| 142 | EVP_DigestInit(&md, evp_md); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 143 | EVP_DigestUpdate(&md, data, datalen); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 144 | EVP_DigestFinal(&md, digest, NULL); |
| 145 | |
| 146 | sig = DSA_do_sign(digest, evp_md->md_size, key->dsa); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 147 | if (sig == NULL) { |
| 148 | fatal("dsa_sign: cannot sign"); |
| 149 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 150 | |
| 151 | rlen = BN_num_bytes(sig->r); |
| 152 | slen = BN_num_bytes(sig->s); |
| 153 | if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) { |
| 154 | error("bad sig size %d %d", rlen, slen); |
| 155 | DSA_SIG_free(sig); |
| 156 | return -1; |
| 157 | } |
| 158 | debug("sig size %d %d", rlen, slen); |
| 159 | |
| 160 | memset(sigblob, 0, SIGBLOB_LEN); |
| 161 | BN_bn2bin(sig->r, sigblob+ SIGBLOB_LEN - INTBLOB_LEN - rlen); |
| 162 | BN_bn2bin(sig->s, sigblob+ SIGBLOB_LEN - slen); |
| 163 | DSA_SIG_free(sig); |
| 164 | |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 165 | if (datafellows & SSH_BUG_SIGBLOB) { |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 166 | debug("datafellows"); |
| 167 | ret = xmalloc(SIGBLOB_LEN); |
| 168 | memcpy(ret, sigblob, SIGBLOB_LEN); |
| 169 | if (lenp != NULL) |
| 170 | *lenp = SIGBLOB_LEN; |
| 171 | if (sigp != NULL) |
| 172 | *sigp = ret; |
| 173 | } else { |
| 174 | /* ietf-drafts */ |
| 175 | buffer_init(&b); |
| 176 | buffer_put_cstring(&b, KEX_DSS); |
| 177 | buffer_put_string(&b, sigblob, SIGBLOB_LEN); |
| 178 | len = buffer_len(&b); |
| 179 | ret = xmalloc(len); |
| 180 | memcpy(ret, buffer_ptr(&b), len); |
| 181 | buffer_free(&b); |
| 182 | if (lenp != NULL) |
| 183 | *lenp = len; |
| 184 | if (sigp != NULL) |
| 185 | *sigp = ret; |
| 186 | } |
| 187 | return 0; |
| 188 | } |
| 189 | int |
| 190 | dsa_verify( |
| 191 | Key *key, |
| 192 | unsigned char *signature, int signaturelen, |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 193 | unsigned char *data, int datalen) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 194 | { |
| 195 | Buffer b; |
| 196 | unsigned char *digest; |
| 197 | DSA_SIG *sig; |
| 198 | EVP_MD *evp_md = EVP_sha1(); |
| 199 | EVP_MD_CTX md; |
| 200 | char *ktype; |
| 201 | unsigned char *sigblob; |
| 202 | char *txt; |
| 203 | unsigned int len; |
| 204 | int rlen; |
| 205 | int ret; |
| 206 | |
| 207 | if (key == NULL || key->type != KEY_DSA || key->dsa == NULL) { |
| 208 | error("dsa_verify: no DSA key"); |
| 209 | return -1; |
| 210 | } |
| 211 | |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 212 | if (!(datafellows & SSH_BUG_SIGBLOB) && |
| 213 | signaturelen == SIGBLOB_LEN) { |
| 214 | datafellows |= ~SSH_BUG_SIGBLOB; |
| 215 | log("autodetect SSH_BUG_SIGBLOB"); |
| 216 | } else if ((datafellows & SSH_BUG_SIGBLOB) && |
| 217 | signaturelen != SIGBLOB_LEN) { |
| 218 | log("autoremove SSH_BUG_SIGBLOB"); |
| 219 | datafellows &= ~SSH_BUG_SIGBLOB; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | debug("len %d datafellows %d", signaturelen, datafellows); |
| 223 | |
| 224 | /* fetch signature */ |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 225 | if (datafellows & SSH_BUG_SIGBLOB) { |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 226 | sigblob = signature; |
| 227 | len = signaturelen; |
| 228 | } else { |
| 229 | /* ietf-drafts */ |
| 230 | buffer_init(&b); |
| 231 | buffer_append(&b, (char *) signature, signaturelen); |
| 232 | ktype = buffer_get_string(&b, NULL); |
| 233 | sigblob = (unsigned char *)buffer_get_string(&b, &len); |
| 234 | rlen = buffer_len(&b); |
| 235 | if(rlen != 0) |
| 236 | error("remaining bytes in signature %d", rlen); |
| 237 | buffer_free(&b); |
| 238 | } |
| 239 | |
| 240 | if (len != SIGBLOB_LEN) { |
| 241 | fatal("bad sigbloblen %d != SIGBLOB_LEN", len); |
| 242 | } |
| 243 | |
| 244 | /* parse signature */ |
| 245 | sig = DSA_SIG_new(); |
| 246 | sig->r = BN_new(); |
| 247 | sig->s = BN_new(); |
| 248 | BN_bin2bn(sigblob, INTBLOB_LEN, sig->r); |
| 249 | BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 250 | |
| 251 | if (!(datafellows & SSH_BUG_SIGBLOB)) { |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 252 | memset(sigblob, 0, len); |
| 253 | xfree(sigblob); |
| 254 | } |
| 255 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 256 | /* sha1 the data */ |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 257 | digest = xmalloc(evp_md->md_size); |
| 258 | EVP_DigestInit(&md, evp_md); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 259 | EVP_DigestUpdate(&md, data, datalen); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 260 | EVP_DigestFinal(&md, digest, NULL); |
| 261 | |
| 262 | ret = DSA_do_verify(digest, evp_md->md_size, sig, key->dsa); |
| 263 | |
| 264 | memset(digest, 0, evp_md->md_size); |
| 265 | xfree(digest); |
| 266 | DSA_SIG_free(sig); |
| 267 | |
| 268 | switch (ret) { |
| 269 | case 1: |
| 270 | txt = "correct"; |
| 271 | break; |
| 272 | case 0: |
| 273 | txt = "incorrect"; |
| 274 | break; |
| 275 | case -1: |
| 276 | default: |
| 277 | txt = "error"; |
| 278 | break; |
| 279 | } |
| 280 | debug("dsa_verify: signature %s", txt); |
| 281 | return ret; |
| 282 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 283 | |
| 284 | Key * |
| 285 | dsa_generate_key(unsigned int bits) |
| 286 | { |
| 287 | DSA *dsa = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL); |
| 288 | Key *k; |
| 289 | if (dsa == NULL) { |
| 290 | fatal("DSA_generate_parameters failed"); |
| 291 | } |
| 292 | if (!DSA_generate_key(dsa)) { |
| 293 | fatal("DSA_generate_keys failed"); |
| 294 | } |
| 295 | |
| 296 | k = key_new(KEY_EMPTY); |
| 297 | k->type = KEY_DSA; |
| 298 | k->dsa = dsa; |
| 299 | return k; |
| 300 | } |