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