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" |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame^] | 35 | RCSID("$OpenBSD: key.c,v 1.38 2001/12/27 18:22:16 markus Exp $"); |
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" |
| 42 | #include "ssh-dss.h" |
| 43 | #include "ssh-rsa.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 44 | #include "uuencode.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 45 | #include "buffer.h" |
| 46 | #include "bufaux.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 47 | #include "log.h" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 48 | |
| 49 | Key * |
| 50 | key_new(int type) |
| 51 | { |
| 52 | Key *k; |
| 53 | RSA *rsa; |
| 54 | DSA *dsa; |
| 55 | k = xmalloc(sizeof(*k)); |
| 56 | k->type = type; |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 57 | k->flags = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 58 | k->dsa = NULL; |
| 59 | k->rsa = NULL; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 60 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 61 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 62 | case KEY_RSA: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame^] | 63 | if ((rsa = RSA_new()) == NULL) |
| 64 | fatal("key_new: RSA_new failed"); |
| 65 | if ((rsa->n = BN_new()) == NULL) |
| 66 | fatal("key_new: BN_new failed"); |
| 67 | if ((rsa->e = BN_new()) == NULL) |
| 68 | fatal("key_new: BN_new failed"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 69 | k->rsa = rsa; |
| 70 | break; |
| 71 | case KEY_DSA: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame^] | 72 | if ((dsa = DSA_new()) == NULL) |
| 73 | fatal("key_new: DSA_new failed"); |
| 74 | if ((dsa->p = BN_new()) == NULL) |
| 75 | fatal("key_new: BN_new failed"); |
| 76 | if ((dsa->q = BN_new()) == NULL) |
| 77 | fatal("key_new: BN_new failed"); |
| 78 | if ((dsa->g = BN_new()) == NULL) |
| 79 | fatal("key_new: BN_new failed"); |
| 80 | if ((dsa->pub_key = BN_new()) == NULL) |
| 81 | fatal("key_new: BN_new failed"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 82 | k->dsa = dsa; |
| 83 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 84 | case KEY_UNSPEC: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 85 | break; |
| 86 | default: |
| 87 | fatal("key_new: bad key type %d", k->type); |
| 88 | break; |
| 89 | } |
| 90 | return k; |
| 91 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 92 | Key * |
| 93 | key_new_private(int type) |
| 94 | { |
| 95 | Key *k = key_new(type); |
| 96 | switch (k->type) { |
| 97 | case KEY_RSA1: |
| 98 | case KEY_RSA: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame^] | 99 | if ((k->rsa->d = BN_new()) == NULL) |
| 100 | fatal("key_new_private: BN_new failed"); |
| 101 | if ((k->rsa->iqmp = BN_new()) == NULL) |
| 102 | fatal("key_new_private: BN_new failed"); |
| 103 | if ((k->rsa->q = BN_new()) == NULL) |
| 104 | fatal("key_new_private: BN_new failed"); |
| 105 | if ((k->rsa->p = BN_new()) == NULL) |
| 106 | fatal("key_new_private: BN_new failed"); |
| 107 | if ((k->rsa->dmq1 = BN_new()) == NULL) |
| 108 | fatal("key_new_private: BN_new failed"); |
| 109 | if ((k->rsa->dmp1 = BN_new()) == NULL) |
| 110 | fatal("key_new_private: BN_new failed"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 111 | break; |
| 112 | case KEY_DSA: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame^] | 113 | if ((k->dsa->priv_key = BN_new()) == NULL) |
| 114 | fatal("key_new_private: BN_new failed"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 115 | break; |
| 116 | case KEY_UNSPEC: |
| 117 | break; |
| 118 | default: |
| 119 | break; |
| 120 | } |
| 121 | return k; |
| 122 | } |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 123 | void |
| 124 | key_free(Key *k) |
| 125 | { |
| 126 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 127 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 128 | case KEY_RSA: |
| 129 | if (k->rsa != NULL) |
| 130 | RSA_free(k->rsa); |
| 131 | k->rsa = NULL; |
| 132 | break; |
| 133 | case KEY_DSA: |
| 134 | if (k->dsa != NULL) |
| 135 | DSA_free(k->dsa); |
| 136 | k->dsa = NULL; |
| 137 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 138 | case KEY_UNSPEC: |
| 139 | break; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 140 | default: |
| 141 | fatal("key_free: bad key type %d", k->type); |
| 142 | break; |
| 143 | } |
| 144 | xfree(k); |
| 145 | } |
| 146 | int |
| 147 | key_equal(Key *a, Key *b) |
| 148 | { |
| 149 | if (a == NULL || b == NULL || a->type != b->type) |
| 150 | return 0; |
| 151 | switch (a->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 152 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 153 | case KEY_RSA: |
| 154 | return a->rsa != NULL && b->rsa != NULL && |
| 155 | BN_cmp(a->rsa->e, b->rsa->e) == 0 && |
| 156 | BN_cmp(a->rsa->n, b->rsa->n) == 0; |
| 157 | break; |
| 158 | case KEY_DSA: |
| 159 | return a->dsa != NULL && b->dsa != NULL && |
| 160 | BN_cmp(a->dsa->p, b->dsa->p) == 0 && |
| 161 | BN_cmp(a->dsa->q, b->dsa->q) == 0 && |
| 162 | BN_cmp(a->dsa->g, b->dsa->g) == 0 && |
| 163 | BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0; |
| 164 | break; |
| 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 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 172 | static u_char* |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 173 | key_fingerprint_raw(Key *k, enum fp_type dgst_type, size_t *dgst_raw_length) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 174 | { |
Ben Lindstrom | f0b4853 | 2001-03-12 02:59:31 +0000 | [diff] [blame] | 175 | EVP_MD *md = NULL; |
| 176 | EVP_MD_CTX ctx; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 177 | u_char *blob = NULL; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 178 | u_char *retval = NULL; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 179 | int len = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 180 | int nlen, elen; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 181 | |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 182 | *dgst_raw_length = 0; |
| 183 | |
Ben Lindstrom | f0b4853 | 2001-03-12 02:59:31 +0000 | [diff] [blame] | 184 | switch (dgst_type) { |
| 185 | case SSH_FP_MD5: |
| 186 | md = EVP_md5(); |
| 187 | break; |
| 188 | case SSH_FP_SHA1: |
| 189 | md = EVP_sha1(); |
| 190 | break; |
| 191 | default: |
| 192 | fatal("key_fingerprint_raw: bad digest type %d", |
| 193 | dgst_type); |
| 194 | } |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 195 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 196 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 197 | nlen = BN_num_bytes(k->rsa->n); |
| 198 | elen = BN_num_bytes(k->rsa->e); |
| 199 | len = nlen + elen; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 200 | blob = xmalloc(len); |
| 201 | BN_bn2bin(k->rsa->n, blob); |
| 202 | BN_bn2bin(k->rsa->e, blob + nlen); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 203 | break; |
| 204 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 205 | case KEY_RSA: |
| 206 | key_to_blob(k, &blob, &len); |
| 207 | break; |
| 208 | case KEY_UNSPEC: |
| 209 | return retval; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 210 | break; |
| 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); |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 219 | EVP_DigestFinal(&ctx, retval, NULL); |
| 220 | *dgst_raw_length = md->md_size; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 221 | memset(blob, 0, len); |
| 222 | xfree(blob); |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 223 | } else { |
| 224 | fatal("key_fingerprint_raw: blob is null"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 225 | } |
| 226 | return retval; |
| 227 | } |
| 228 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 229 | static char* |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 230 | key_fingerprint_hex(u_char* dgst_raw, size_t dgst_raw_len) |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 231 | { |
| 232 | char *retval; |
| 233 | int i; |
| 234 | |
Ben Lindstrom | 459d191 | 2001-04-17 18:06:14 +0000 | [diff] [blame] | 235 | retval = xmalloc(dgst_raw_len * 3 + 1); |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 236 | retval[0] = '\0'; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 237 | for (i = 0; i < dgst_raw_len; i++) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 238 | char hex[4]; |
| 239 | snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]); |
| 240 | strlcat(retval, hex, dgst_raw_len * 3); |
| 241 | } |
| 242 | retval[(dgst_raw_len * 3) - 1] = '\0'; |
| 243 | return retval; |
| 244 | } |
| 245 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 246 | static char* |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 247 | key_fingerprint_bubblebabble(u_char* dgst_raw, size_t 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; |
| 256 | retval = xmalloc(sizeof(char) * (rounds*6)); |
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 | |
| 293 | char* |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 294 | key_fingerprint(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; |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 298 | size_t 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 | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 363 | static int |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 364 | write_bignum(FILE *f, BIGNUM *num) |
| 365 | { |
| 366 | char *buf = BN_bn2dec(num); |
| 367 | if (buf == NULL) { |
| 368 | error("write_bignum: BN_bn2dec() failed"); |
| 369 | return 0; |
| 370 | } |
| 371 | fprintf(f, " %s", buf); |
Damien Miller | af3030f | 2001-10-10 15:00:49 +1000 | [diff] [blame] | 372 | OPENSSL_free(buf); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 373 | return 1; |
| 374 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 375 | |
Ben Lindstrom | 309f3d1 | 2001-09-20 00:55:53 +0000 | [diff] [blame] | 376 | /* returns 1 ok, -1 error */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 377 | int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 378 | key_read(Key *ret, char **cpp) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 379 | { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 380 | Key *k; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 381 | int success = -1; |
| 382 | char *cp, *space; |
| 383 | int len, n, type; |
| 384 | u_int bits; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 385 | u_char *blob; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 386 | |
| 387 | cp = *cpp; |
| 388 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 389 | switch (ret->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 390 | case KEY_RSA1: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 391 | /* Get number of bits. */ |
| 392 | if (*cp < '0' || *cp > '9') |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 393 | return -1; /* Bad bit count... */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 394 | for (bits = 0; *cp >= '0' && *cp <= '9'; cp++) |
| 395 | bits = 10 * bits + *cp - '0'; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 396 | if (bits == 0) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 397 | return -1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 398 | *cpp = cp; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 399 | /* Get public exponent, public modulus. */ |
| 400 | if (!read_bignum(cpp, ret->rsa->e)) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 401 | return -1; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 402 | if (!read_bignum(cpp, ret->rsa->n)) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 403 | return -1; |
| 404 | success = 1; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 405 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 406 | case KEY_UNSPEC: |
| 407 | case KEY_RSA: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 408 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 409 | space = strchr(cp, ' '); |
| 410 | if (space == NULL) { |
| 411 | debug3("key_read: no space"); |
| 412 | return -1; |
| 413 | } |
| 414 | *space = '\0'; |
| 415 | type = key_type_from_name(cp); |
| 416 | *space = ' '; |
| 417 | if (type == KEY_UNSPEC) { |
| 418 | debug3("key_read: no key found"); |
| 419 | return -1; |
| 420 | } |
| 421 | cp = space+1; |
| 422 | if (*cp == '\0') { |
| 423 | debug3("key_read: short string"); |
| 424 | return -1; |
| 425 | } |
| 426 | if (ret->type == KEY_UNSPEC) { |
| 427 | ret->type = type; |
| 428 | } else if (ret->type != type) { |
| 429 | /* is a key, but different type */ |
| 430 | debug3("key_read: type mismatch"); |
Ben Lindstrom | 309f3d1 | 2001-09-20 00:55:53 +0000 | [diff] [blame] | 431 | return -1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 432 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 433 | len = 2*strlen(cp); |
| 434 | blob = xmalloc(len); |
| 435 | n = uudecode(cp, blob, len); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 436 | if (n < 0) { |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 437 | error("key_read: uudecode %s failed", cp); |
Ben Lindstrom | 4cbc181 | 2001-12-06 16:41:41 +0000 | [diff] [blame] | 438 | xfree(blob); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 439 | return -1; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 440 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 441 | k = key_from_blob(blob, n); |
Ben Lindstrom | 4cbc181 | 2001-12-06 16:41:41 +0000 | [diff] [blame] | 442 | xfree(blob); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 443 | if (k == NULL) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 444 | error("key_read: key_from_blob %s failed", cp); |
| 445 | return -1; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 446 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 447 | if (k->type != type) { |
| 448 | error("key_read: type mismatch: encoding error"); |
| 449 | key_free(k); |
| 450 | return -1; |
| 451 | } |
| 452 | /*XXXX*/ |
| 453 | if (ret->type == KEY_RSA) { |
| 454 | if (ret->rsa != NULL) |
| 455 | RSA_free(ret->rsa); |
| 456 | ret->rsa = k->rsa; |
| 457 | k->rsa = NULL; |
| 458 | success = 1; |
| 459 | #ifdef DEBUG_PK |
| 460 | RSA_print_fp(stderr, ret->rsa, 8); |
| 461 | #endif |
| 462 | } else { |
| 463 | if (ret->dsa != NULL) |
| 464 | DSA_free(ret->dsa); |
| 465 | ret->dsa = k->dsa; |
| 466 | k->dsa = NULL; |
| 467 | success = 1; |
| 468 | #ifdef DEBUG_PK |
| 469 | DSA_print_fp(stderr, ret->dsa, 8); |
| 470 | #endif |
| 471 | } |
| 472 | /*XXXX*/ |
Ben Lindstrom | 4cbc181 | 2001-12-06 16:41:41 +0000 | [diff] [blame] | 473 | key_free(k); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 474 | if (success != 1) |
| 475 | break; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 476 | /* advance cp: skip whitespace and data */ |
| 477 | while (*cp == ' ' || *cp == '\t') |
| 478 | cp++; |
| 479 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') |
| 480 | cp++; |
| 481 | *cpp = cp; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 482 | break; |
| 483 | default: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 484 | fatal("key_read: bad key type: %d", ret->type); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 485 | break; |
| 486 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 487 | return success; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 488 | } |
| 489 | int |
| 490 | key_write(Key *key, FILE *f) |
| 491 | { |
| 492 | int success = 0; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 493 | u_int bits = 0; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 494 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 495 | if (key->type == KEY_RSA1 && key->rsa != NULL) { |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 496 | /* size of modulus 'n' */ |
| 497 | bits = BN_num_bits(key->rsa->n); |
| 498 | fprintf(f, "%u", bits); |
| 499 | if (write_bignum(f, key->rsa->e) && |
| 500 | write_bignum(f, key->rsa->n)) { |
| 501 | success = 1; |
| 502 | } else { |
| 503 | error("key_write: failed for RSA key"); |
| 504 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 505 | } else if ((key->type == KEY_DSA && key->dsa != NULL) || |
| 506 | (key->type == KEY_RSA && key->rsa != NULL)) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 507 | int len, n; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 508 | u_char *blob, *uu; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 509 | key_to_blob(key, &blob, &len); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 510 | uu = xmalloc(2*len); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 511 | n = uuencode(blob, len, uu, 2*len); |
| 512 | if (n > 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 513 | fprintf(f, "%s %s", key_ssh_name(key), uu); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 514 | success = 1; |
| 515 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 516 | xfree(blob); |
| 517 | xfree(uu); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 518 | } |
| 519 | return success; |
| 520 | } |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 521 | char * |
| 522 | key_type(Key *k) |
| 523 | { |
| 524 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 525 | case KEY_RSA1: |
| 526 | return "RSA1"; |
| 527 | break; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 528 | case KEY_RSA: |
| 529 | return "RSA"; |
| 530 | break; |
| 531 | case KEY_DSA: |
| 532 | return "DSA"; |
| 533 | break; |
| 534 | } |
| 535 | return "unknown"; |
| 536 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 537 | char * |
| 538 | key_ssh_name(Key *k) |
| 539 | { |
| 540 | switch (k->type) { |
| 541 | case KEY_RSA: |
| 542 | return "ssh-rsa"; |
| 543 | break; |
| 544 | case KEY_DSA: |
| 545 | return "ssh-dss"; |
| 546 | break; |
| 547 | } |
| 548 | return "ssh-unknown"; |
| 549 | } |
| 550 | u_int |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 551 | key_size(Key *k) |
| 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); |
| 557 | break; |
| 558 | case KEY_DSA: |
| 559 | return BN_num_bits(k->dsa->p); |
| 560 | break; |
| 561 | } |
| 562 | return 0; |
| 563 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 564 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 565 | static RSA * |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 566 | rsa_generate_private_key(u_int bits) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 567 | { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 568 | RSA *private; |
| 569 | private = RSA_generate_key(bits, 35, NULL, NULL); |
| 570 | if (private == NULL) |
| 571 | fatal("rsa_generate_private_key: key generation failed."); |
| 572 | return private; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 573 | } |
| 574 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 575 | static DSA* |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 576 | dsa_generate_private_key(u_int bits) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 577 | { |
| 578 | DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL); |
| 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 * |
| 608 | key_from_private(Key *k) |
| 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 * |
Ben Lindstrom | 9e0ddd4 | 2001-09-18 05:41:19 +0000 | [diff] [blame] | 673 | key_from_blob(u_char *blob, int blen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 674 | { |
| 675 | Buffer b; |
| 676 | char *ktype; |
| 677 | int rlen, type; |
| 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); |
| 685 | ktype = buffer_get_string(&b, NULL); |
| 686 | type = key_type_from_name(ktype); |
| 687 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 688 | switch (type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 689 | case KEY_RSA: |
| 690 | key = key_new(type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 691 | buffer_get_bignum2(&b, key->rsa->e); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 692 | buffer_get_bignum2(&b, key->rsa->n); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 693 | #ifdef DEBUG_PK |
| 694 | RSA_print_fp(stderr, key->rsa, 8); |
| 695 | #endif |
| 696 | break; |
| 697 | case KEY_DSA: |
| 698 | key = key_new(type); |
| 699 | buffer_get_bignum2(&b, key->dsa->p); |
| 700 | buffer_get_bignum2(&b, key->dsa->q); |
| 701 | buffer_get_bignum2(&b, key->dsa->g); |
| 702 | buffer_get_bignum2(&b, key->dsa->pub_key); |
| 703 | #ifdef DEBUG_PK |
| 704 | DSA_print_fp(stderr, key->dsa, 8); |
| 705 | #endif |
| 706 | break; |
| 707 | case KEY_UNSPEC: |
| 708 | key = key_new(type); |
| 709 | break; |
| 710 | default: |
| 711 | error("key_from_blob: cannot handle type %s", ktype); |
| 712 | break; |
| 713 | } |
| 714 | rlen = buffer_len(&b); |
| 715 | if (key != NULL && rlen != 0) |
| 716 | error("key_from_blob: remaining bytes in key blob %d", rlen); |
| 717 | xfree(ktype); |
| 718 | buffer_free(&b); |
| 719 | return key; |
| 720 | } |
| 721 | |
| 722 | int |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 723 | key_to_blob(Key *key, u_char **blobp, u_int *lenp) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 724 | { |
| 725 | Buffer b; |
| 726 | int len; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 727 | u_char *buf; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 728 | |
| 729 | if (key == NULL) { |
| 730 | error("key_to_blob: key == NULL"); |
| 731 | return 0; |
| 732 | } |
| 733 | buffer_init(&b); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 734 | switch (key->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 735 | case KEY_DSA: |
| 736 | buffer_put_cstring(&b, key_ssh_name(key)); |
| 737 | buffer_put_bignum2(&b, key->dsa->p); |
| 738 | buffer_put_bignum2(&b, key->dsa->q); |
| 739 | buffer_put_bignum2(&b, key->dsa->g); |
| 740 | buffer_put_bignum2(&b, key->dsa->pub_key); |
| 741 | break; |
| 742 | case KEY_RSA: |
| 743 | buffer_put_cstring(&b, key_ssh_name(key)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 744 | buffer_put_bignum2(&b, key->rsa->e); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 745 | buffer_put_bignum2(&b, key->rsa->n); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 746 | break; |
| 747 | default: |
Ben Lindstrom | 99a30f1 | 2001-09-18 05:49:14 +0000 | [diff] [blame] | 748 | error("key_to_blob: unsupported key type %d", key->type); |
| 749 | buffer_free(&b); |
| 750 | return 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 751 | } |
| 752 | len = buffer_len(&b); |
| 753 | buf = xmalloc(len); |
| 754 | memcpy(buf, buffer_ptr(&b), len); |
| 755 | memset(buffer_ptr(&b), 0, len); |
| 756 | buffer_free(&b); |
| 757 | if (lenp != NULL) |
| 758 | *lenp = len; |
| 759 | if (blobp != NULL) |
| 760 | *blobp = buf; |
| 761 | return len; |
| 762 | } |
| 763 | |
| 764 | int |
| 765 | key_sign( |
| 766 | Key *key, |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 767 | u_char **sigp, int *lenp, |
| 768 | u_char *data, int datalen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 769 | { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 770 | switch (key->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 771 | case KEY_DSA: |
| 772 | return ssh_dss_sign(key, sigp, lenp, data, datalen); |
| 773 | break; |
| 774 | case KEY_RSA: |
| 775 | return ssh_rsa_sign(key, sigp, lenp, data, datalen); |
| 776 | break; |
| 777 | default: |
| 778 | error("key_sign: illegal key type %d", key->type); |
| 779 | return -1; |
| 780 | break; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | int |
| 785 | key_verify( |
| 786 | Key *key, |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 787 | u_char *signature, int signaturelen, |
| 788 | u_char *data, int datalen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 789 | { |
Ben Lindstrom | 5363aee | 2001-06-25 04:42:20 +0000 | [diff] [blame] | 790 | if (signaturelen == 0) |
| 791 | return -1; |
| 792 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 793 | switch (key->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 794 | case KEY_DSA: |
| 795 | return ssh_dss_verify(key, signature, signaturelen, data, datalen); |
| 796 | break; |
| 797 | case KEY_RSA: |
| 798 | return ssh_rsa_verify(key, signature, signaturelen, data, datalen); |
| 799 | break; |
| 800 | default: |
| 801 | error("key_verify: illegal key type %d", key->type); |
| 802 | return -1; |
| 803 | break; |
| 804 | } |
| 805 | } |