Damien Miller | 57c3011 | 2006-03-26 14:24:48 +1100 | [diff] [blame] | 1 | /* $OpenBSD: key.c,v 1.64 2006/03/25 13:17:02 djm Exp $ */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 3 | * read_bignum(): |
| 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * |
| 6 | * As far as I am concerned, the code I have written for this software |
| 7 | * can be used freely for any purpose. Any derived versions of this |
| 8 | * software must be clearly marked as such, and if the derived work is |
| 9 | * incompatible with the protocol description in the RFC file, it must be |
| 10 | * called by a name other than "ssh" or "Secure Shell". |
| 11 | * |
| 12 | * |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 13 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 14 | * |
| 15 | * Redistribution and use in source and binary forms, with or without |
| 16 | * modification, are permitted provided that the following conditions |
| 17 | * are met: |
| 18 | * 1. Redistributions of source code must retain the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer. |
| 20 | * 2. Redistributions in binary form must reproduce the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer in the |
| 22 | * documentation and/or other materials provided with the distribution. |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 25 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 26 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 27 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 29 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 33 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 35 | #include "includes.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 36 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 37 | #include <openssl/evp.h> |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 38 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 39 | #include "xmalloc.h" |
| 40 | #include "key.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 41 | #include "rsa.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 42 | #include "uuencode.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 43 | #include "buffer.h" |
| 44 | #include "bufaux.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 45 | #include "log.h" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 46 | |
| 47 | Key * |
| 48 | key_new(int type) |
| 49 | { |
| 50 | Key *k; |
| 51 | RSA *rsa; |
| 52 | DSA *dsa; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 53 | k = xcalloc(1, sizeof(*k)); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 54 | k->type = type; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 55 | k->dsa = NULL; |
| 56 | k->rsa = NULL; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 57 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 58 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 59 | case KEY_RSA: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 60 | if ((rsa = RSA_new()) == NULL) |
| 61 | fatal("key_new: RSA_new failed"); |
| 62 | if ((rsa->n = BN_new()) == NULL) |
| 63 | fatal("key_new: BN_new failed"); |
| 64 | if ((rsa->e = BN_new()) == NULL) |
| 65 | fatal("key_new: BN_new failed"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 66 | k->rsa = rsa; |
| 67 | break; |
| 68 | case KEY_DSA: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 69 | if ((dsa = DSA_new()) == NULL) |
| 70 | fatal("key_new: DSA_new failed"); |
| 71 | if ((dsa->p = BN_new()) == NULL) |
| 72 | fatal("key_new: BN_new failed"); |
| 73 | if ((dsa->q = BN_new()) == NULL) |
| 74 | fatal("key_new: BN_new failed"); |
| 75 | if ((dsa->g = BN_new()) == NULL) |
| 76 | fatal("key_new: BN_new failed"); |
| 77 | if ((dsa->pub_key = BN_new()) == NULL) |
| 78 | fatal("key_new: BN_new failed"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 79 | k->dsa = dsa; |
| 80 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 81 | case KEY_UNSPEC: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 82 | break; |
| 83 | default: |
| 84 | fatal("key_new: bad key type %d", k->type); |
| 85 | break; |
| 86 | } |
| 87 | return k; |
| 88 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 89 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 90 | Key * |
| 91 | key_new_private(int type) |
| 92 | { |
| 93 | Key *k = key_new(type); |
| 94 | switch (k->type) { |
| 95 | case KEY_RSA1: |
| 96 | case KEY_RSA: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 97 | if ((k->rsa->d = BN_new()) == NULL) |
| 98 | fatal("key_new_private: BN_new failed"); |
| 99 | if ((k->rsa->iqmp = BN_new()) == NULL) |
| 100 | fatal("key_new_private: BN_new failed"); |
| 101 | if ((k->rsa->q = BN_new()) == NULL) |
| 102 | fatal("key_new_private: BN_new failed"); |
| 103 | if ((k->rsa->p = BN_new()) == NULL) |
| 104 | fatal("key_new_private: BN_new failed"); |
| 105 | if ((k->rsa->dmq1 = BN_new()) == NULL) |
| 106 | fatal("key_new_private: BN_new failed"); |
| 107 | if ((k->rsa->dmp1 = BN_new()) == NULL) |
| 108 | fatal("key_new_private: BN_new failed"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 109 | break; |
| 110 | case KEY_DSA: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 111 | if ((k->dsa->priv_key = BN_new()) == NULL) |
| 112 | fatal("key_new_private: BN_new failed"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 113 | break; |
| 114 | case KEY_UNSPEC: |
| 115 | break; |
| 116 | default: |
| 117 | break; |
| 118 | } |
| 119 | return k; |
| 120 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 121 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 122 | void |
| 123 | key_free(Key *k) |
| 124 | { |
Damien Miller | 429fcc2 | 2006-03-26 14:02:16 +1100 | [diff] [blame] | 125 | if (k == NULL) |
Damien Miller | bbaad77 | 2006-03-26 14:03:03 +1100 | [diff] [blame] | 126 | fatal("key_free: key is NULL"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 127 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 128 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 129 | case KEY_RSA: |
| 130 | if (k->rsa != NULL) |
| 131 | RSA_free(k->rsa); |
| 132 | k->rsa = NULL; |
| 133 | break; |
| 134 | case KEY_DSA: |
| 135 | if (k->dsa != NULL) |
| 136 | DSA_free(k->dsa); |
| 137 | k->dsa = NULL; |
| 138 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 139 | case KEY_UNSPEC: |
| 140 | break; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 141 | default: |
| 142 | fatal("key_free: bad key type %d", k->type); |
| 143 | break; |
| 144 | } |
| 145 | xfree(k); |
| 146 | } |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 147 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 148 | int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 149 | key_equal(const Key *a, const Key *b) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 150 | { |
| 151 | if (a == NULL || b == NULL || a->type != b->type) |
| 152 | return 0; |
| 153 | switch (a->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 154 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 155 | case KEY_RSA: |
| 156 | return a->rsa != NULL && b->rsa != NULL && |
| 157 | BN_cmp(a->rsa->e, b->rsa->e) == 0 && |
| 158 | BN_cmp(a->rsa->n, b->rsa->n) == 0; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 159 | case KEY_DSA: |
| 160 | return a->dsa != NULL && b->dsa != NULL && |
| 161 | BN_cmp(a->dsa->p, b->dsa->p) == 0 && |
| 162 | BN_cmp(a->dsa->q, b->dsa->q) == 0 && |
| 163 | BN_cmp(a->dsa->g, b->dsa->g) == 0 && |
| 164 | BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 165 | default: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 166 | fatal("key_equal: bad key type %d", a->type); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 167 | break; |
| 168 | } |
| 169 | return 0; |
| 170 | } |
| 171 | |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 172 | u_char* |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 173 | key_fingerprint_raw(const Key *k, enum fp_type dgst_type, |
| 174 | u_int *dgst_raw_length) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 175 | { |
Ben Lindstrom | 80cb27d | 2002-03-05 01:33:36 +0000 | [diff] [blame] | 176 | const EVP_MD *md = NULL; |
Ben Lindstrom | f0b4853 | 2001-03-12 02:59:31 +0000 | [diff] [blame] | 177 | EVP_MD_CTX ctx; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 178 | u_char *blob = NULL; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 179 | u_char *retval = NULL; |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 180 | u_int len = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 181 | int nlen, elen; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 182 | |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 183 | *dgst_raw_length = 0; |
| 184 | |
Ben Lindstrom | f0b4853 | 2001-03-12 02:59:31 +0000 | [diff] [blame] | 185 | switch (dgst_type) { |
| 186 | case SSH_FP_MD5: |
| 187 | md = EVP_md5(); |
| 188 | break; |
| 189 | case SSH_FP_SHA1: |
| 190 | md = EVP_sha1(); |
| 191 | break; |
| 192 | default: |
| 193 | fatal("key_fingerprint_raw: bad digest type %d", |
| 194 | dgst_type); |
| 195 | } |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 196 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 197 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 198 | nlen = BN_num_bytes(k->rsa->n); |
| 199 | elen = BN_num_bytes(k->rsa->e); |
| 200 | len = nlen + elen; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 201 | blob = xmalloc(len); |
| 202 | BN_bn2bin(k->rsa->n, blob); |
| 203 | BN_bn2bin(k->rsa->e, blob + nlen); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 204 | break; |
| 205 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 206 | case KEY_RSA: |
| 207 | key_to_blob(k, &blob, &len); |
| 208 | break; |
| 209 | case KEY_UNSPEC: |
| 210 | return retval; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 211 | default: |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 212 | fatal("key_fingerprint_raw: bad key type %d", k->type); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 213 | break; |
| 214 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 215 | if (blob != NULL) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 216 | retval = xmalloc(EVP_MAX_MD_SIZE); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 217 | EVP_DigestInit(&ctx, md); |
| 218 | EVP_DigestUpdate(&ctx, blob, len); |
Damien Miller | 3672e4b | 2002-02-05 11:54:07 +1100 | [diff] [blame] | 219 | EVP_DigestFinal(&ctx, retval, dgst_raw_length); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 220 | memset(blob, 0, len); |
| 221 | xfree(blob); |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 222 | } else { |
| 223 | fatal("key_fingerprint_raw: blob is null"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 224 | } |
| 225 | return retval; |
| 226 | } |
| 227 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 228 | static char * |
| 229 | key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len) |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 230 | { |
| 231 | char *retval; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 232 | u_int i; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 233 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 234 | retval = xcalloc(1, dgst_raw_len * 3 + 1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 235 | for (i = 0; i < dgst_raw_len; i++) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 236 | char hex[4]; |
| 237 | snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]); |
Darren Tucker | 2958861 | 2003-07-14 17:28:34 +1000 | [diff] [blame] | 238 | strlcat(retval, hex, dgst_raw_len * 3 + 1); |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 239 | } |
Darren Tucker | 2958861 | 2003-07-14 17:28:34 +1000 | [diff] [blame] | 240 | |
| 241 | /* Remove the trailing ':' character */ |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 242 | retval[(dgst_raw_len * 3) - 1] = '\0'; |
| 243 | return retval; |
| 244 | } |
| 245 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 246 | static char * |
| 247 | key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len) |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 248 | { |
| 249 | char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' }; |
| 250 | char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm', |
| 251 | 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' }; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 252 | u_int i, j = 0, rounds, seed = 1; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 253 | char *retval; |
| 254 | |
| 255 | rounds = (dgst_raw_len / 2) + 1; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 256 | retval = xcalloc((rounds * 6), sizeof(char)); |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 257 | retval[j++] = 'x'; |
| 258 | for (i = 0; i < rounds; i++) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 259 | u_int idx0, idx1, idx2, idx3, idx4; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 260 | if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) { |
| 261 | idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) + |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 262 | seed) % 6; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 263 | idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15; |
| 264 | idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) + |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 265 | (seed / 6)) % 6; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 266 | retval[j++] = vowels[idx0]; |
| 267 | retval[j++] = consonants[idx1]; |
| 268 | retval[j++] = vowels[idx2]; |
| 269 | if ((i + 1) < rounds) { |
| 270 | idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15; |
| 271 | idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15; |
| 272 | retval[j++] = consonants[idx3]; |
| 273 | retval[j++] = '-'; |
| 274 | retval[j++] = consonants[idx4]; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 275 | seed = ((seed * 5) + |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 276 | ((((u_int)(dgst_raw[2 * i])) * 7) + |
| 277 | ((u_int)(dgst_raw[(2 * i) + 1])))) % 36; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 278 | } |
| 279 | } else { |
| 280 | idx0 = seed % 6; |
| 281 | idx1 = 16; |
| 282 | idx2 = seed / 6; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 283 | retval[j++] = vowels[idx0]; |
| 284 | retval[j++] = consonants[idx1]; |
| 285 | retval[j++] = vowels[idx2]; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 288 | retval[j++] = 'x'; |
| 289 | retval[j++] = '\0'; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 290 | return retval; |
| 291 | } |
| 292 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 293 | char * |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 294 | key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep) |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 295 | { |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 296 | char *retval = NULL; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 297 | u_char *dgst_raw; |
Damien Miller | 3672e4b | 2002-02-05 11:54:07 +1100 | [diff] [blame] | 298 | u_int dgst_raw_len; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 299 | |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 300 | dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len); |
| 301 | if (!dgst_raw) |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 302 | fatal("key_fingerprint: null from key_fingerprint_raw()"); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 303 | switch (dgst_rep) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 304 | case SSH_FP_HEX: |
| 305 | retval = key_fingerprint_hex(dgst_raw, dgst_raw_len); |
| 306 | break; |
| 307 | case SSH_FP_BUBBLEBABBLE: |
| 308 | retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len); |
| 309 | break; |
| 310 | default: |
| 311 | fatal("key_fingerprint_ex: bad digest representation %d", |
| 312 | dgst_rep); |
| 313 | break; |
| 314 | } |
| 315 | memset(dgst_raw, 0, dgst_raw_len); |
| 316 | xfree(dgst_raw); |
| 317 | return retval; |
| 318 | } |
| 319 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 320 | /* |
| 321 | * Reads a multiple-precision integer in decimal from the buffer, and advances |
| 322 | * the pointer. The integer must already be initialized. This function is |
| 323 | * permitted to modify the buffer. This leaves *cpp to point just beyond the |
| 324 | * last processed (and maybe modified) character. Note that this may modify |
| 325 | * the buffer containing the number. |
| 326 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 327 | static int |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 328 | read_bignum(char **cpp, BIGNUM * value) |
| 329 | { |
| 330 | char *cp = *cpp; |
| 331 | int old; |
| 332 | |
| 333 | /* Skip any leading whitespace. */ |
| 334 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 335 | ; |
| 336 | |
| 337 | /* Check that it begins with a decimal digit. */ |
| 338 | if (*cp < '0' || *cp > '9') |
| 339 | return 0; |
| 340 | |
| 341 | /* Save starting position. */ |
| 342 | *cpp = cp; |
| 343 | |
| 344 | /* Move forward until all decimal digits skipped. */ |
| 345 | for (; *cp >= '0' && *cp <= '9'; cp++) |
| 346 | ; |
| 347 | |
| 348 | /* Save the old terminating character, and replace it by \0. */ |
| 349 | old = *cp; |
| 350 | *cp = 0; |
| 351 | |
| 352 | /* Parse the number. */ |
| 353 | if (BN_dec2bn(&value, *cpp) == 0) |
| 354 | return 0; |
| 355 | |
| 356 | /* Restore old terminating character. */ |
| 357 | *cp = old; |
| 358 | |
| 359 | /* Move beyond the number and return success. */ |
| 360 | *cpp = cp; |
| 361 | return 1; |
| 362 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 363 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 364 | static int |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 365 | write_bignum(FILE *f, BIGNUM *num) |
| 366 | { |
| 367 | char *buf = BN_bn2dec(num); |
| 368 | if (buf == NULL) { |
| 369 | error("write_bignum: BN_bn2dec() failed"); |
| 370 | return 0; |
| 371 | } |
| 372 | fprintf(f, " %s", buf); |
Damien Miller | af3030f | 2001-10-10 15:00:49 +1000 | [diff] [blame] | 373 | OPENSSL_free(buf); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 374 | return 1; |
| 375 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 376 | |
Ben Lindstrom | 309f3d1 | 2001-09-20 00:55:53 +0000 | [diff] [blame] | 377 | /* returns 1 ok, -1 error */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 378 | int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 379 | key_read(Key *ret, char **cpp) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 380 | { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 381 | Key *k; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 382 | int success = -1; |
| 383 | char *cp, *space; |
| 384 | int len, n, type; |
| 385 | u_int bits; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 386 | u_char *blob; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 387 | |
| 388 | cp = *cpp; |
| 389 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 390 | switch (ret->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 391 | case KEY_RSA1: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 392 | /* Get number of bits. */ |
| 393 | if (*cp < '0' || *cp > '9') |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 394 | return -1; /* Bad bit count... */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 395 | for (bits = 0; *cp >= '0' && *cp <= '9'; cp++) |
| 396 | bits = 10 * bits + *cp - '0'; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 397 | if (bits == 0) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 398 | return -1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 399 | *cpp = cp; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 400 | /* Get public exponent, public modulus. */ |
| 401 | if (!read_bignum(cpp, ret->rsa->e)) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 402 | return -1; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 403 | if (!read_bignum(cpp, ret->rsa->n)) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 404 | return -1; |
| 405 | success = 1; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 406 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 407 | case KEY_UNSPEC: |
| 408 | case KEY_RSA: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 409 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 410 | space = strchr(cp, ' '); |
| 411 | if (space == NULL) { |
Damien Miller | 386f1f3 | 2003-02-24 11:54:57 +1100 | [diff] [blame] | 412 | debug3("key_read: missing whitespace"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 413 | return -1; |
| 414 | } |
| 415 | *space = '\0'; |
| 416 | type = key_type_from_name(cp); |
| 417 | *space = ' '; |
| 418 | if (type == KEY_UNSPEC) { |
Damien Miller | 386f1f3 | 2003-02-24 11:54:57 +1100 | [diff] [blame] | 419 | debug3("key_read: missing keytype"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 420 | return -1; |
| 421 | } |
| 422 | cp = space+1; |
| 423 | if (*cp == '\0') { |
| 424 | debug3("key_read: short string"); |
| 425 | return -1; |
| 426 | } |
| 427 | if (ret->type == KEY_UNSPEC) { |
| 428 | ret->type = type; |
| 429 | } else if (ret->type != type) { |
| 430 | /* is a key, but different type */ |
| 431 | debug3("key_read: type mismatch"); |
Ben Lindstrom | 309f3d1 | 2001-09-20 00:55:53 +0000 | [diff] [blame] | 432 | return -1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 433 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 434 | len = 2*strlen(cp); |
| 435 | blob = xmalloc(len); |
| 436 | n = uudecode(cp, blob, len); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 437 | if (n < 0) { |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 438 | error("key_read: uudecode %s failed", cp); |
Ben Lindstrom | 4cbc181 | 2001-12-06 16:41:41 +0000 | [diff] [blame] | 439 | xfree(blob); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 440 | return -1; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 441 | } |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 442 | k = key_from_blob(blob, (u_int)n); |
Ben Lindstrom | 4cbc181 | 2001-12-06 16:41:41 +0000 | [diff] [blame] | 443 | xfree(blob); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 444 | if (k == NULL) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 445 | error("key_read: key_from_blob %s failed", cp); |
| 446 | return -1; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 447 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 448 | if (k->type != type) { |
| 449 | error("key_read: type mismatch: encoding error"); |
| 450 | key_free(k); |
| 451 | return -1; |
| 452 | } |
| 453 | /*XXXX*/ |
| 454 | if (ret->type == KEY_RSA) { |
| 455 | if (ret->rsa != NULL) |
| 456 | RSA_free(ret->rsa); |
| 457 | ret->rsa = k->rsa; |
| 458 | k->rsa = NULL; |
| 459 | success = 1; |
| 460 | #ifdef DEBUG_PK |
| 461 | RSA_print_fp(stderr, ret->rsa, 8); |
| 462 | #endif |
| 463 | } else { |
| 464 | if (ret->dsa != NULL) |
| 465 | DSA_free(ret->dsa); |
| 466 | ret->dsa = k->dsa; |
| 467 | k->dsa = NULL; |
| 468 | success = 1; |
| 469 | #ifdef DEBUG_PK |
| 470 | DSA_print_fp(stderr, ret->dsa, 8); |
| 471 | #endif |
| 472 | } |
| 473 | /*XXXX*/ |
Ben Lindstrom | 4cbc181 | 2001-12-06 16:41:41 +0000 | [diff] [blame] | 474 | key_free(k); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 475 | if (success != 1) |
| 476 | break; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 477 | /* advance cp: skip whitespace and data */ |
| 478 | while (*cp == ' ' || *cp == '\t') |
| 479 | cp++; |
| 480 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') |
| 481 | cp++; |
| 482 | *cpp = cp; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 483 | break; |
| 484 | default: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 485 | fatal("key_read: bad key type: %d", ret->type); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 486 | break; |
| 487 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 488 | return success; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 489 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 490 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 491 | int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 492 | key_write(const Key *key, FILE *f) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 493 | { |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 494 | int n, success = 0; |
| 495 | u_int len, bits = 0; |
Damien Miller | a10f561 | 2002-09-12 09:49:15 +1000 | [diff] [blame] | 496 | u_char *blob; |
| 497 | char *uu; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 498 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 499 | if (key->type == KEY_RSA1 && key->rsa != NULL) { |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 500 | /* size of modulus 'n' */ |
| 501 | bits = BN_num_bits(key->rsa->n); |
| 502 | fprintf(f, "%u", bits); |
| 503 | if (write_bignum(f, key->rsa->e) && |
| 504 | write_bignum(f, key->rsa->n)) { |
| 505 | success = 1; |
| 506 | } else { |
| 507 | error("key_write: failed for RSA key"); |
| 508 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 509 | } else if ((key->type == KEY_DSA && key->dsa != NULL) || |
| 510 | (key->type == KEY_RSA && key->rsa != NULL)) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 511 | key_to_blob(key, &blob, &len); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 512 | uu = xmalloc(2*len); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 513 | n = uuencode(blob, len, uu, 2*len); |
| 514 | if (n > 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 515 | fprintf(f, "%s %s", key_ssh_name(key), uu); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 516 | success = 1; |
| 517 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 518 | xfree(blob); |
| 519 | xfree(uu); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 520 | } |
| 521 | return success; |
| 522 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 523 | |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 524 | const char * |
| 525 | key_type(const Key *k) |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 526 | { |
| 527 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 528 | case KEY_RSA1: |
| 529 | return "RSA1"; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 530 | case KEY_RSA: |
| 531 | return "RSA"; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 532 | case KEY_DSA: |
| 533 | return "DSA"; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 534 | } |
| 535 | return "unknown"; |
| 536 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 537 | |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 538 | const char * |
| 539 | key_ssh_name(const Key *k) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 540 | { |
| 541 | switch (k->type) { |
| 542 | case KEY_RSA: |
| 543 | return "ssh-rsa"; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 544 | case KEY_DSA: |
| 545 | return "ssh-dss"; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 546 | } |
| 547 | return "ssh-unknown"; |
| 548 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 549 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 550 | u_int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 551 | key_size(const Key *k) |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 552 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 553 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 554 | case KEY_RSA1: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 555 | case KEY_RSA: |
| 556 | return BN_num_bits(k->rsa->n); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 557 | case KEY_DSA: |
| 558 | return BN_num_bits(k->dsa->p); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 559 | } |
| 560 | return 0; |
| 561 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 562 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 563 | static RSA * |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 564 | rsa_generate_private_key(u_int bits) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 565 | { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 566 | RSA *private; |
Damien Miller | 69b7203 | 2006-03-26 14:02:35 +1100 | [diff] [blame] | 567 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 568 | private = RSA_generate_key(bits, 35, NULL, NULL); |
| 569 | if (private == NULL) |
| 570 | fatal("rsa_generate_private_key: key generation failed."); |
| 571 | return private; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 572 | } |
| 573 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 574 | static DSA* |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 575 | dsa_generate_private_key(u_int bits) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 576 | { |
| 577 | DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL); |
Damien Miller | 69b7203 | 2006-03-26 14:02:35 +1100 | [diff] [blame] | 578 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 579 | if (private == NULL) |
| 580 | fatal("dsa_generate_private_key: DSA_generate_parameters failed"); |
| 581 | if (!DSA_generate_key(private)) |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 582 | fatal("dsa_generate_private_key: DSA_generate_key failed."); |
| 583 | if (private == NULL) |
| 584 | fatal("dsa_generate_private_key: NULL."); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 585 | return private; |
| 586 | } |
| 587 | |
| 588 | Key * |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 589 | key_generate(int type, u_int bits) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 590 | { |
| 591 | Key *k = key_new(KEY_UNSPEC); |
| 592 | switch (type) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 593 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 594 | k->dsa = dsa_generate_private_key(bits); |
| 595 | break; |
| 596 | case KEY_RSA: |
| 597 | case KEY_RSA1: |
| 598 | k->rsa = rsa_generate_private_key(bits); |
| 599 | break; |
| 600 | default: |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 601 | fatal("key_generate: unknown type %d", type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 602 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 603 | k->type = type; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 604 | return k; |
| 605 | } |
| 606 | |
| 607 | Key * |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 608 | key_from_private(const Key *k) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 609 | { |
| 610 | Key *n = NULL; |
| 611 | switch (k->type) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 612 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 613 | n = key_new(k->type); |
| 614 | BN_copy(n->dsa->p, k->dsa->p); |
| 615 | BN_copy(n->dsa->q, k->dsa->q); |
| 616 | BN_copy(n->dsa->g, k->dsa->g); |
| 617 | BN_copy(n->dsa->pub_key, k->dsa->pub_key); |
| 618 | break; |
| 619 | case KEY_RSA: |
| 620 | case KEY_RSA1: |
| 621 | n = key_new(k->type); |
| 622 | BN_copy(n->rsa->n, k->rsa->n); |
| 623 | BN_copy(n->rsa->e, k->rsa->e); |
| 624 | break; |
| 625 | default: |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 626 | fatal("key_from_private: unknown type %d", k->type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 627 | break; |
| 628 | } |
| 629 | return n; |
| 630 | } |
| 631 | |
| 632 | int |
| 633 | key_type_from_name(char *name) |
| 634 | { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 635 | if (strcmp(name, "rsa1") == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 636 | return KEY_RSA1; |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 637 | } else if (strcmp(name, "rsa") == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 638 | return KEY_RSA; |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 639 | } else if (strcmp(name, "dsa") == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 640 | return KEY_DSA; |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 641 | } else if (strcmp(name, "ssh-rsa") == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 642 | return KEY_RSA; |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 643 | } else if (strcmp(name, "ssh-dss") == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 644 | return KEY_DSA; |
| 645 | } |
Ben Lindstrom | b54873a | 2001-03-11 20:01:55 +0000 | [diff] [blame] | 646 | debug2("key_type_from_name: unknown key type '%s'", name); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 647 | return KEY_UNSPEC; |
| 648 | } |
| 649 | |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 650 | int |
| 651 | key_names_valid2(const char *names) |
| 652 | { |
| 653 | char *s, *cp, *p; |
| 654 | |
| 655 | if (names == NULL || strcmp(names, "") == 0) |
| 656 | return 0; |
| 657 | s = cp = xstrdup(names); |
| 658 | for ((p = strsep(&cp, ",")); p && *p != '\0'; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 659 | (p = strsep(&cp, ","))) { |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 660 | switch (key_type_from_name(p)) { |
| 661 | case KEY_RSA1: |
| 662 | case KEY_UNSPEC: |
| 663 | xfree(s); |
| 664 | return 0; |
| 665 | } |
| 666 | } |
| 667 | debug3("key names ok: [%s]", names); |
| 668 | xfree(s); |
| 669 | return 1; |
| 670 | } |
| 671 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 672 | Key * |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 673 | key_from_blob(const u_char *blob, u_int blen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 674 | { |
| 675 | Buffer b; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 676 | int rlen, type; |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 677 | char *ktype = NULL; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 678 | Key *key = NULL; |
| 679 | |
| 680 | #ifdef DEBUG_PK |
| 681 | dump_base64(stderr, blob, blen); |
| 682 | #endif |
| 683 | buffer_init(&b); |
| 684 | buffer_append(&b, blob, blen); |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 685 | if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) { |
| 686 | error("key_from_blob: can't read key type"); |
| 687 | goto out; |
| 688 | } |
| 689 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 690 | type = key_type_from_name(ktype); |
| 691 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 692 | switch (type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 693 | case KEY_RSA: |
| 694 | key = key_new(type); |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 695 | if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 || |
| 696 | buffer_get_bignum2_ret(&b, key->rsa->n) == -1) { |
| 697 | error("key_from_blob: can't read rsa key"); |
| 698 | key_free(key); |
| 699 | key = NULL; |
| 700 | goto out; |
| 701 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 702 | #ifdef DEBUG_PK |
| 703 | RSA_print_fp(stderr, key->rsa, 8); |
| 704 | #endif |
| 705 | break; |
| 706 | case KEY_DSA: |
| 707 | key = key_new(type); |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 708 | if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 || |
| 709 | buffer_get_bignum2_ret(&b, key->dsa->q) == -1 || |
| 710 | buffer_get_bignum2_ret(&b, key->dsa->g) == -1 || |
| 711 | buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) { |
| 712 | error("key_from_blob: can't read dsa key"); |
| 713 | key_free(key); |
| 714 | key = NULL; |
| 715 | goto out; |
| 716 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 717 | #ifdef DEBUG_PK |
| 718 | DSA_print_fp(stderr, key->dsa, 8); |
| 719 | #endif |
| 720 | break; |
| 721 | case KEY_UNSPEC: |
| 722 | key = key_new(type); |
| 723 | break; |
| 724 | default: |
| 725 | error("key_from_blob: cannot handle type %s", ktype); |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 726 | goto out; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 727 | } |
| 728 | rlen = buffer_len(&b); |
| 729 | if (key != NULL && rlen != 0) |
| 730 | error("key_from_blob: remaining bytes in key blob %d", rlen); |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 731 | out: |
| 732 | if (ktype != NULL) |
| 733 | xfree(ktype); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 734 | buffer_free(&b); |
| 735 | return key; |
| 736 | } |
| 737 | |
| 738 | int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 739 | key_to_blob(const Key *key, u_char **blobp, u_int *lenp) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 740 | { |
| 741 | Buffer b; |
| 742 | int len; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 743 | |
| 744 | if (key == NULL) { |
| 745 | error("key_to_blob: key == NULL"); |
| 746 | return 0; |
| 747 | } |
| 748 | buffer_init(&b); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 749 | switch (key->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 750 | case KEY_DSA: |
| 751 | buffer_put_cstring(&b, key_ssh_name(key)); |
| 752 | buffer_put_bignum2(&b, key->dsa->p); |
| 753 | buffer_put_bignum2(&b, key->dsa->q); |
| 754 | buffer_put_bignum2(&b, key->dsa->g); |
| 755 | buffer_put_bignum2(&b, key->dsa->pub_key); |
| 756 | break; |
| 757 | case KEY_RSA: |
| 758 | buffer_put_cstring(&b, key_ssh_name(key)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 759 | buffer_put_bignum2(&b, key->rsa->e); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 760 | buffer_put_bignum2(&b, key->rsa->n); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 761 | break; |
| 762 | default: |
Ben Lindstrom | 99a30f1 | 2001-09-18 05:49:14 +0000 | [diff] [blame] | 763 | error("key_to_blob: unsupported key type %d", key->type); |
| 764 | buffer_free(&b); |
| 765 | return 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 766 | } |
| 767 | len = buffer_len(&b); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 768 | if (lenp != NULL) |
| 769 | *lenp = len; |
Ben Lindstrom | 2bf759c | 2002-07-07 22:13:31 +0000 | [diff] [blame] | 770 | if (blobp != NULL) { |
| 771 | *blobp = xmalloc(len); |
| 772 | memcpy(*blobp, buffer_ptr(&b), len); |
| 773 | } |
| 774 | memset(buffer_ptr(&b), 0, len); |
| 775 | buffer_free(&b); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 776 | return len; |
| 777 | } |
| 778 | |
| 779 | int |
| 780 | key_sign( |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 781 | const Key *key, |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 782 | u_char **sigp, u_int *lenp, |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 783 | const u_char *data, u_int datalen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 784 | { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 785 | switch (key->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 786 | case KEY_DSA: |
| 787 | return ssh_dss_sign(key, sigp, lenp, data, datalen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 788 | case KEY_RSA: |
| 789 | return ssh_rsa_sign(key, sigp, lenp, data, datalen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 790 | default: |
Darren Tucker | 5cb30ad | 2004-08-12 22:40:24 +1000 | [diff] [blame] | 791 | error("key_sign: invalid key type %d", key->type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 792 | return -1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
Ben Lindstrom | 01fff0c | 2002-06-06 20:54:07 +0000 | [diff] [blame] | 796 | /* |
| 797 | * key_verify returns 1 for a correct signature, 0 for an incorrect signature |
| 798 | * and -1 on error. |
| 799 | */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 800 | int |
| 801 | key_verify( |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 802 | const Key *key, |
| 803 | const u_char *signature, u_int signaturelen, |
| 804 | const u_char *data, u_int datalen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 805 | { |
Ben Lindstrom | 5363aee | 2001-06-25 04:42:20 +0000 | [diff] [blame] | 806 | if (signaturelen == 0) |
| 807 | return -1; |
| 808 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 809 | switch (key->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 810 | case KEY_DSA: |
| 811 | return ssh_dss_verify(key, signature, signaturelen, data, datalen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 812 | case KEY_RSA: |
| 813 | return ssh_rsa_verify(key, signature, signaturelen, data, datalen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 814 | default: |
Darren Tucker | 5cb30ad | 2004-08-12 22:40:24 +1000 | [diff] [blame] | 815 | error("key_verify: invalid key type %d", key->type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 816 | return -1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 817 | } |
| 818 | } |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 819 | |
| 820 | /* Converts a private to a public key */ |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 821 | Key * |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 822 | key_demote(const Key *k) |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 823 | { |
| 824 | Key *pk; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 825 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 826 | pk = xcalloc(1, sizeof(*pk)); |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 827 | pk->type = k->type; |
| 828 | pk->flags = k->flags; |
| 829 | pk->dsa = NULL; |
| 830 | pk->rsa = NULL; |
| 831 | |
| 832 | switch (k->type) { |
| 833 | case KEY_RSA1: |
| 834 | case KEY_RSA: |
| 835 | if ((pk->rsa = RSA_new()) == NULL) |
| 836 | fatal("key_demote: RSA_new failed"); |
| 837 | if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL) |
| 838 | fatal("key_demote: BN_dup failed"); |
| 839 | if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL) |
| 840 | fatal("key_demote: BN_dup failed"); |
| 841 | break; |
| 842 | case KEY_DSA: |
| 843 | if ((pk->dsa = DSA_new()) == NULL) |
| 844 | fatal("key_demote: DSA_new failed"); |
| 845 | if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL) |
| 846 | fatal("key_demote: BN_dup failed"); |
| 847 | if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL) |
| 848 | fatal("key_demote: BN_dup failed"); |
| 849 | if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL) |
| 850 | fatal("key_demote: BN_dup failed"); |
| 851 | if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL) |
| 852 | fatal("key_demote: BN_dup failed"); |
| 853 | break; |
| 854 | default: |
| 855 | fatal("key_free: bad key type %d", k->type); |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | return (pk); |
| 860 | } |