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