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