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