Damien Miller | 87dd5f2 | 2008-07-11 17:35:09 +1000 | [diff] [blame] | 1 | /* $OpenBSD: key.c,v 1.78 2008/07/07 23:32:51 stevesk 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. |
Darren Tucker | 0f0ef0a | 2008-06-13 08:58:05 +1000 | [diff] [blame] | 14 | * Copyright (c) 2008 Alexander von Gernler. All rights reserved. |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 15 | * |
| 16 | * Redistribution and use in source and binary forms, with or without |
| 17 | * modification, are permitted provided that the following conditions |
| 18 | * are met: |
| 19 | * 1. Redistributions of source code must retain the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer. |
| 21 | * 2. Redistributions in binary form must reproduce the above copyright |
| 22 | * notice, this list of conditions and the following disclaimer in the |
| 23 | * documentation and/or other materials provided with the distribution. |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 26 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 27 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 28 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 31 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 32 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 34 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 36 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 37 | #include "includes.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 38 | |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 39 | #include <sys/param.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 40 | #include <sys/types.h> |
| 41 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 42 | #include <openssl/evp.h> |
Darren Tucker | 3d295a6 | 2008-02-28 19:22:04 +1100 | [diff] [blame] | 43 | #include <openbsd-compat/openssl-compat.h> |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 44 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 45 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 46 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 47 | #include <string.h> |
| 48 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 49 | #include "xmalloc.h" |
| 50 | #include "key.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 51 | #include "rsa.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 52 | #include "uuencode.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 53 | #include "buffer.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 54 | #include "log.h" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 55 | |
| 56 | Key * |
| 57 | key_new(int type) |
| 58 | { |
| 59 | Key *k; |
| 60 | RSA *rsa; |
| 61 | DSA *dsa; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 62 | k = xcalloc(1, sizeof(*k)); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 63 | k->type = type; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 64 | k->dsa = NULL; |
| 65 | k->rsa = NULL; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 66 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 67 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 68 | case KEY_RSA: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 69 | if ((rsa = RSA_new()) == NULL) |
| 70 | fatal("key_new: RSA_new failed"); |
| 71 | if ((rsa->n = BN_new()) == NULL) |
| 72 | fatal("key_new: BN_new failed"); |
| 73 | if ((rsa->e = BN_new()) == NULL) |
| 74 | fatal("key_new: BN_new failed"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 75 | k->rsa = rsa; |
| 76 | break; |
| 77 | case KEY_DSA: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 78 | if ((dsa = DSA_new()) == NULL) |
| 79 | fatal("key_new: DSA_new failed"); |
| 80 | if ((dsa->p = BN_new()) == NULL) |
| 81 | fatal("key_new: BN_new failed"); |
| 82 | if ((dsa->q = BN_new()) == NULL) |
| 83 | fatal("key_new: BN_new failed"); |
| 84 | if ((dsa->g = BN_new()) == NULL) |
| 85 | fatal("key_new: BN_new failed"); |
| 86 | if ((dsa->pub_key = BN_new()) == NULL) |
| 87 | fatal("key_new: BN_new failed"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 88 | k->dsa = dsa; |
| 89 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 90 | case KEY_UNSPEC: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 91 | break; |
| 92 | default: |
| 93 | fatal("key_new: bad key type %d", k->type); |
| 94 | break; |
| 95 | } |
| 96 | return k; |
| 97 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 98 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 99 | Key * |
| 100 | key_new_private(int type) |
| 101 | { |
| 102 | Key *k = key_new(type); |
| 103 | switch (k->type) { |
| 104 | case KEY_RSA1: |
| 105 | case KEY_RSA: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 106 | if ((k->rsa->d = BN_new()) == NULL) |
| 107 | fatal("key_new_private: BN_new failed"); |
| 108 | if ((k->rsa->iqmp = BN_new()) == NULL) |
| 109 | fatal("key_new_private: BN_new failed"); |
| 110 | if ((k->rsa->q = BN_new()) == NULL) |
| 111 | fatal("key_new_private: BN_new failed"); |
| 112 | if ((k->rsa->p = BN_new()) == NULL) |
| 113 | fatal("key_new_private: BN_new failed"); |
| 114 | if ((k->rsa->dmq1 = BN_new()) == NULL) |
| 115 | fatal("key_new_private: BN_new failed"); |
| 116 | if ((k->rsa->dmp1 = BN_new()) == NULL) |
| 117 | fatal("key_new_private: BN_new failed"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 118 | break; |
| 119 | case KEY_DSA: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 120 | if ((k->dsa->priv_key = BN_new()) == NULL) |
| 121 | fatal("key_new_private: BN_new failed"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 122 | break; |
| 123 | case KEY_UNSPEC: |
| 124 | break; |
| 125 | default: |
| 126 | break; |
| 127 | } |
| 128 | return k; |
| 129 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 130 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 131 | void |
| 132 | key_free(Key *k) |
| 133 | { |
Damien Miller | 429fcc2 | 2006-03-26 14:02:16 +1100 | [diff] [blame] | 134 | if (k == NULL) |
Damien Miller | bbaad77 | 2006-03-26 14:03:03 +1100 | [diff] [blame] | 135 | fatal("key_free: key is NULL"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 136 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 137 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 138 | case KEY_RSA: |
| 139 | if (k->rsa != NULL) |
| 140 | RSA_free(k->rsa); |
| 141 | k->rsa = NULL; |
| 142 | break; |
| 143 | case KEY_DSA: |
| 144 | if (k->dsa != NULL) |
| 145 | DSA_free(k->dsa); |
| 146 | k->dsa = NULL; |
| 147 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 148 | case KEY_UNSPEC: |
| 149 | break; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 150 | default: |
| 151 | fatal("key_free: bad key type %d", k->type); |
| 152 | break; |
| 153 | } |
| 154 | xfree(k); |
| 155 | } |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 156 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 157 | int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 158 | key_equal(const Key *a, const Key *b) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 159 | { |
| 160 | if (a == NULL || b == NULL || a->type != b->type) |
| 161 | return 0; |
| 162 | switch (a->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 163 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 164 | case KEY_RSA: |
| 165 | return a->rsa != NULL && b->rsa != NULL && |
| 166 | BN_cmp(a->rsa->e, b->rsa->e) == 0 && |
| 167 | BN_cmp(a->rsa->n, b->rsa->n) == 0; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 168 | case KEY_DSA: |
| 169 | return a->dsa != NULL && b->dsa != NULL && |
| 170 | BN_cmp(a->dsa->p, b->dsa->p) == 0 && |
| 171 | BN_cmp(a->dsa->q, b->dsa->q) == 0 && |
| 172 | BN_cmp(a->dsa->g, b->dsa->g) == 0 && |
| 173 | BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 174 | default: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 175 | fatal("key_equal: bad key type %d", a->type); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 176 | } |
Damien Miller | 87dd5f2 | 2008-07-11 17:35:09 +1000 | [diff] [blame] | 177 | /* NOTREACHED */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 178 | } |
| 179 | |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 180 | u_char* |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 181 | key_fingerprint_raw(const Key *k, enum fp_type dgst_type, |
| 182 | u_int *dgst_raw_length) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 183 | { |
Ben Lindstrom | 80cb27d | 2002-03-05 01:33:36 +0000 | [diff] [blame] | 184 | const EVP_MD *md = NULL; |
Ben Lindstrom | f0b4853 | 2001-03-12 02:59:31 +0000 | [diff] [blame] | 185 | EVP_MD_CTX ctx; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 186 | u_char *blob = NULL; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 187 | u_char *retval = NULL; |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 188 | u_int len = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 189 | int nlen, elen; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 190 | |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 191 | *dgst_raw_length = 0; |
| 192 | |
Ben Lindstrom | f0b4853 | 2001-03-12 02:59:31 +0000 | [diff] [blame] | 193 | switch (dgst_type) { |
| 194 | case SSH_FP_MD5: |
| 195 | md = EVP_md5(); |
| 196 | break; |
| 197 | case SSH_FP_SHA1: |
| 198 | md = EVP_sha1(); |
| 199 | break; |
| 200 | default: |
| 201 | fatal("key_fingerprint_raw: bad digest type %d", |
| 202 | dgst_type); |
| 203 | } |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 204 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 205 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 206 | nlen = BN_num_bytes(k->rsa->n); |
| 207 | elen = BN_num_bytes(k->rsa->e); |
| 208 | len = nlen + elen; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 209 | blob = xmalloc(len); |
| 210 | BN_bn2bin(k->rsa->n, blob); |
| 211 | BN_bn2bin(k->rsa->e, blob + nlen); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 212 | break; |
| 213 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 214 | case KEY_RSA: |
| 215 | key_to_blob(k, &blob, &len); |
| 216 | break; |
| 217 | case KEY_UNSPEC: |
| 218 | return retval; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 219 | default: |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 220 | fatal("key_fingerprint_raw: bad key type %d", k->type); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 221 | break; |
| 222 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 223 | if (blob != NULL) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 224 | retval = xmalloc(EVP_MAX_MD_SIZE); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 225 | EVP_DigestInit(&ctx, md); |
| 226 | EVP_DigestUpdate(&ctx, blob, len); |
Damien Miller | 3672e4b | 2002-02-05 11:54:07 +1100 | [diff] [blame] | 227 | EVP_DigestFinal(&ctx, retval, dgst_raw_length); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 228 | memset(blob, 0, len); |
| 229 | xfree(blob); |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 230 | } else { |
| 231 | fatal("key_fingerprint_raw: blob is null"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 232 | } |
| 233 | return retval; |
| 234 | } |
| 235 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 236 | static char * |
| 237 | key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len) |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 238 | { |
| 239 | char *retval; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 240 | u_int i; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 241 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 242 | retval = xcalloc(1, dgst_raw_len * 3 + 1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 243 | for (i = 0; i < dgst_raw_len; i++) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 244 | char hex[4]; |
| 245 | snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]); |
Darren Tucker | 2958861 | 2003-07-14 17:28:34 +1000 | [diff] [blame] | 246 | strlcat(retval, hex, dgst_raw_len * 3 + 1); |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 247 | } |
Darren Tucker | 2958861 | 2003-07-14 17:28:34 +1000 | [diff] [blame] | 248 | |
| 249 | /* Remove the trailing ':' character */ |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 250 | retval[(dgst_raw_len * 3) - 1] = '\0'; |
| 251 | return retval; |
| 252 | } |
| 253 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 254 | static char * |
| 255 | key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len) |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 256 | { |
| 257 | char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' }; |
| 258 | char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm', |
| 259 | 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' }; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 260 | u_int i, j = 0, rounds, seed = 1; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 261 | char *retval; |
| 262 | |
| 263 | rounds = (dgst_raw_len / 2) + 1; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 264 | retval = xcalloc((rounds * 6), sizeof(char)); |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 265 | retval[j++] = 'x'; |
| 266 | for (i = 0; i < rounds; i++) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 267 | u_int idx0, idx1, idx2, idx3, idx4; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 268 | if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) { |
| 269 | idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) + |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 270 | seed) % 6; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 271 | idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15; |
| 272 | idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) + |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 273 | (seed / 6)) % 6; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 274 | retval[j++] = vowels[idx0]; |
| 275 | retval[j++] = consonants[idx1]; |
| 276 | retval[j++] = vowels[idx2]; |
| 277 | if ((i + 1) < rounds) { |
| 278 | idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15; |
| 279 | idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15; |
| 280 | retval[j++] = consonants[idx3]; |
| 281 | retval[j++] = '-'; |
| 282 | retval[j++] = consonants[idx4]; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 283 | seed = ((seed * 5) + |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 284 | ((((u_int)(dgst_raw[2 * i])) * 7) + |
| 285 | ((u_int)(dgst_raw[(2 * i) + 1])))) % 36; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 286 | } |
| 287 | } else { |
| 288 | idx0 = seed % 6; |
| 289 | idx1 = 16; |
| 290 | idx2 = seed / 6; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 291 | retval[j++] = vowels[idx0]; |
| 292 | retval[j++] = consonants[idx1]; |
| 293 | retval[j++] = vowels[idx2]; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 296 | retval[j++] = 'x'; |
| 297 | retval[j++] = '\0'; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 298 | return retval; |
| 299 | } |
| 300 | |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 301 | /* |
| 302 | * Draw an ASCII-Art representing the fingerprint so human brain can |
| 303 | * profit from its built-in pattern recognition ability. |
| 304 | * This technique is called "random art" and can be found in some |
| 305 | * scientific publications like this original paper: |
| 306 | * |
| 307 | * "Hash Visualization: a New Technique to improve Real-World Security", |
| 308 | * Perrig A. and Song D., 1999, International Workshop on Cryptographic |
| 309 | * Techniques and E-Commerce (CrypTEC '99) |
| 310 | * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf |
| 311 | * |
| 312 | * The subject came up in a talk by Dan Kaminsky, too. |
| 313 | * |
| 314 | * If you see the picture is different, the key is different. |
| 315 | * If the picture looks the same, you still know nothing. |
| 316 | * |
| 317 | * The algorithm used here is a worm crawling over a discrete plane, |
| 318 | * leaving a trace (augmenting the field) everywhere it goes. |
| 319 | * Movement is taken from dgst_raw 2bit-wise. Bumping into walls |
| 320 | * makes the respective movement vector be ignored for this turn. |
| 321 | * Graphs are not unambiguous, because circles in graphs can be |
| 322 | * walked in either direction. |
| 323 | */ |
Darren Tucker | 987ac84 | 2008-06-13 04:54:40 +1000 | [diff] [blame] | 324 | |
| 325 | /* |
| 326 | * Field sizes for the random art. Have to be odd, so the starting point |
| 327 | * can be in the exact middle of the picture, and FLDBASE should be >=8 . |
| 328 | * Else pictures would be too dense, and drawing the frame would |
| 329 | * fail, too, because the key type would not fit in anymore. |
| 330 | */ |
| 331 | #define FLDBASE 8 |
| 332 | #define FLDSIZE_Y (FLDBASE + 1) |
| 333 | #define FLDSIZE_X (FLDBASE * 2 + 1) |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 334 | static char * |
Darren Tucker | 987ac84 | 2008-06-13 04:54:40 +1000 | [diff] [blame] | 335 | key_fingerprint_randomart(u_char *dgst_raw, u_int dgst_raw_len, const Key *k) |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 336 | { |
| 337 | /* |
| 338 | * Chars to be used after each other every time the worm |
| 339 | * intersects with itself. Matter of taste. |
| 340 | */ |
Darren Tucker | 4b3b977 | 2008-06-13 04:55:10 +1000 | [diff] [blame] | 341 | char *augmentation_string = " .o+=*BOX@%&#/^SE"; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 342 | char *retval, *p; |
Darren Tucker | 014d76f | 2008-06-13 04:43:51 +1000 | [diff] [blame] | 343 | u_char field[FLDSIZE_X][FLDSIZE_Y]; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 344 | u_int i, b; |
| 345 | int x, y; |
Darren Tucker | d32b28a | 2008-06-13 04:45:50 +1000 | [diff] [blame] | 346 | size_t len = strlen(augmentation_string) - 1; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 347 | |
| 348 | retval = xcalloc(1, (FLDSIZE_X + 3) * (FLDSIZE_Y + 2)); |
| 349 | |
| 350 | /* initialize field */ |
Darren Tucker | 014d76f | 2008-06-13 04:43:51 +1000 | [diff] [blame] | 351 | memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char)); |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 352 | x = FLDSIZE_X / 2; |
| 353 | y = FLDSIZE_Y / 2; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 354 | |
| 355 | /* process raw key */ |
| 356 | for (i = 0; i < dgst_raw_len; i++) { |
| 357 | int input; |
| 358 | /* each byte conveys four 2-bit move commands */ |
| 359 | input = dgst_raw[i]; |
| 360 | for (b = 0; b < 4; b++) { |
| 361 | /* evaluate 2 bit, rest is shifted later */ |
| 362 | x += (input & 0x1) ? 1 : -1; |
| 363 | y += (input & 0x2) ? 1 : -1; |
| 364 | |
| 365 | /* assure we are still in bounds */ |
| 366 | x = MAX(x, 0); |
| 367 | y = MAX(y, 0); |
| 368 | x = MIN(x, FLDSIZE_X - 1); |
| 369 | y = MIN(y, FLDSIZE_Y - 1); |
| 370 | |
| 371 | /* augment the field */ |
Darren Tucker | 014d76f | 2008-06-13 04:43:51 +1000 | [diff] [blame] | 372 | field[x][y]++; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 373 | input = input >> 2; |
| 374 | } |
| 375 | } |
Darren Tucker | 4b3b977 | 2008-06-13 04:55:10 +1000 | [diff] [blame] | 376 | |
| 377 | /* mark starting point and end point*/ |
| 378 | field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1; |
| 379 | field[x][y] = len; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 380 | |
| 381 | /* fill in retval */ |
Damien Miller | 007132a | 2008-06-29 22:45:37 +1000 | [diff] [blame] | 382 | snprintf(retval, FLDSIZE_X, "+--[%4s %4u]", key_type(k), key_size(k)); |
Darren Tucker | 987ac84 | 2008-06-13 04:54:40 +1000 | [diff] [blame] | 383 | p = strchr(retval, '\0'); |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 384 | |
| 385 | /* output upper border */ |
Damien Miller | 007132a | 2008-06-29 22:45:37 +1000 | [diff] [blame] | 386 | for (i = p - retval - 1; i < FLDSIZE_X; i++) |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 387 | *p++ = '-'; |
| 388 | *p++ = '+'; |
| 389 | *p++ = '\n'; |
| 390 | |
| 391 | /* output content */ |
| 392 | for (y = 0; y < FLDSIZE_Y; y++) { |
| 393 | *p++ = '|'; |
| 394 | for (x = 0; x < FLDSIZE_X; x++) |
Darren Tucker | d32b28a | 2008-06-13 04:45:50 +1000 | [diff] [blame] | 395 | *p++ = augmentation_string[MIN(field[x][y], len)]; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 396 | *p++ = '|'; |
| 397 | *p++ = '\n'; |
| 398 | } |
| 399 | |
| 400 | /* output lower border */ |
| 401 | *p++ = '+'; |
| 402 | for (i = 0; i < FLDSIZE_X; i++) |
| 403 | *p++ = '-'; |
| 404 | *p++ = '+'; |
| 405 | |
| 406 | return retval; |
| 407 | } |
| 408 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 409 | char * |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 410 | 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] | 411 | { |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 412 | char *retval = NULL; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 413 | u_char *dgst_raw; |
Damien Miller | 3672e4b | 2002-02-05 11:54:07 +1100 | [diff] [blame] | 414 | u_int dgst_raw_len; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 415 | |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 416 | dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len); |
| 417 | if (!dgst_raw) |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 418 | fatal("key_fingerprint: null from key_fingerprint_raw()"); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 419 | switch (dgst_rep) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 420 | case SSH_FP_HEX: |
| 421 | retval = key_fingerprint_hex(dgst_raw, dgst_raw_len); |
| 422 | break; |
| 423 | case SSH_FP_BUBBLEBABBLE: |
| 424 | retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len); |
| 425 | break; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 426 | case SSH_FP_RANDOMART: |
Darren Tucker | 987ac84 | 2008-06-13 04:54:40 +1000 | [diff] [blame] | 427 | retval = key_fingerprint_randomart(dgst_raw, dgst_raw_len, k); |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 428 | break; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 429 | default: |
| 430 | fatal("key_fingerprint_ex: bad digest representation %d", |
| 431 | dgst_rep); |
| 432 | break; |
| 433 | } |
| 434 | memset(dgst_raw, 0, dgst_raw_len); |
| 435 | xfree(dgst_raw); |
| 436 | return retval; |
| 437 | } |
| 438 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 439 | /* |
| 440 | * Reads a multiple-precision integer in decimal from the buffer, and advances |
| 441 | * the pointer. The integer must already be initialized. This function is |
| 442 | * permitted to modify the buffer. This leaves *cpp to point just beyond the |
| 443 | * last processed (and maybe modified) character. Note that this may modify |
| 444 | * the buffer containing the number. |
| 445 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 446 | static int |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 447 | read_bignum(char **cpp, BIGNUM * value) |
| 448 | { |
| 449 | char *cp = *cpp; |
| 450 | int old; |
| 451 | |
| 452 | /* Skip any leading whitespace. */ |
| 453 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 454 | ; |
| 455 | |
| 456 | /* Check that it begins with a decimal digit. */ |
| 457 | if (*cp < '0' || *cp > '9') |
| 458 | return 0; |
| 459 | |
| 460 | /* Save starting position. */ |
| 461 | *cpp = cp; |
| 462 | |
| 463 | /* Move forward until all decimal digits skipped. */ |
| 464 | for (; *cp >= '0' && *cp <= '9'; cp++) |
| 465 | ; |
| 466 | |
| 467 | /* Save the old terminating character, and replace it by \0. */ |
| 468 | old = *cp; |
| 469 | *cp = 0; |
| 470 | |
| 471 | /* Parse the number. */ |
| 472 | if (BN_dec2bn(&value, *cpp) == 0) |
| 473 | return 0; |
| 474 | |
| 475 | /* Restore old terminating character. */ |
| 476 | *cp = old; |
| 477 | |
| 478 | /* Move beyond the number and return success. */ |
| 479 | *cpp = cp; |
| 480 | return 1; |
| 481 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 482 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 483 | static int |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 484 | write_bignum(FILE *f, BIGNUM *num) |
| 485 | { |
| 486 | char *buf = BN_bn2dec(num); |
| 487 | if (buf == NULL) { |
| 488 | error("write_bignum: BN_bn2dec() failed"); |
| 489 | return 0; |
| 490 | } |
| 491 | fprintf(f, " %s", buf); |
Damien Miller | af3030f | 2001-10-10 15:00:49 +1000 | [diff] [blame] | 492 | OPENSSL_free(buf); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 493 | return 1; |
| 494 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 495 | |
Ben Lindstrom | 309f3d1 | 2001-09-20 00:55:53 +0000 | [diff] [blame] | 496 | /* returns 1 ok, -1 error */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 497 | int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 498 | key_read(Key *ret, char **cpp) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 499 | { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 500 | Key *k; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 501 | int success = -1; |
| 502 | char *cp, *space; |
| 503 | int len, n, type; |
| 504 | u_int bits; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 505 | u_char *blob; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 506 | |
| 507 | cp = *cpp; |
| 508 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 509 | switch (ret->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 510 | case KEY_RSA1: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 511 | /* Get number of bits. */ |
| 512 | if (*cp < '0' || *cp > '9') |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 513 | return -1; /* Bad bit count... */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 514 | for (bits = 0; *cp >= '0' && *cp <= '9'; cp++) |
| 515 | bits = 10 * bits + *cp - '0'; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 516 | if (bits == 0) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 517 | return -1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 518 | *cpp = cp; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 519 | /* Get public exponent, public modulus. */ |
| 520 | if (!read_bignum(cpp, ret->rsa->e)) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 521 | return -1; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 522 | if (!read_bignum(cpp, ret->rsa->n)) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 523 | return -1; |
| 524 | success = 1; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 525 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 526 | case KEY_UNSPEC: |
| 527 | case KEY_RSA: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 528 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 529 | space = strchr(cp, ' '); |
| 530 | if (space == NULL) { |
Damien Miller | 386f1f3 | 2003-02-24 11:54:57 +1100 | [diff] [blame] | 531 | debug3("key_read: missing whitespace"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 532 | return -1; |
| 533 | } |
| 534 | *space = '\0'; |
| 535 | type = key_type_from_name(cp); |
| 536 | *space = ' '; |
| 537 | if (type == KEY_UNSPEC) { |
Damien Miller | 386f1f3 | 2003-02-24 11:54:57 +1100 | [diff] [blame] | 538 | debug3("key_read: missing keytype"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 539 | return -1; |
| 540 | } |
| 541 | cp = space+1; |
| 542 | if (*cp == '\0') { |
| 543 | debug3("key_read: short string"); |
| 544 | return -1; |
| 545 | } |
| 546 | if (ret->type == KEY_UNSPEC) { |
| 547 | ret->type = type; |
| 548 | } else if (ret->type != type) { |
| 549 | /* is a key, but different type */ |
| 550 | debug3("key_read: type mismatch"); |
Ben Lindstrom | 309f3d1 | 2001-09-20 00:55:53 +0000 | [diff] [blame] | 551 | return -1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 552 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 553 | len = 2*strlen(cp); |
| 554 | blob = xmalloc(len); |
| 555 | n = uudecode(cp, blob, len); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 556 | if (n < 0) { |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 557 | error("key_read: uudecode %s failed", cp); |
Ben Lindstrom | 4cbc181 | 2001-12-06 16:41:41 +0000 | [diff] [blame] | 558 | xfree(blob); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 559 | return -1; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 560 | } |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 561 | k = key_from_blob(blob, (u_int)n); |
Ben Lindstrom | 4cbc181 | 2001-12-06 16:41:41 +0000 | [diff] [blame] | 562 | xfree(blob); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 563 | if (k == NULL) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 564 | error("key_read: key_from_blob %s failed", cp); |
| 565 | return -1; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 566 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 567 | if (k->type != type) { |
| 568 | error("key_read: type mismatch: encoding error"); |
| 569 | key_free(k); |
| 570 | return -1; |
| 571 | } |
| 572 | /*XXXX*/ |
| 573 | if (ret->type == KEY_RSA) { |
| 574 | if (ret->rsa != NULL) |
| 575 | RSA_free(ret->rsa); |
| 576 | ret->rsa = k->rsa; |
| 577 | k->rsa = NULL; |
| 578 | success = 1; |
| 579 | #ifdef DEBUG_PK |
| 580 | RSA_print_fp(stderr, ret->rsa, 8); |
| 581 | #endif |
| 582 | } else { |
| 583 | if (ret->dsa != NULL) |
| 584 | DSA_free(ret->dsa); |
| 585 | ret->dsa = k->dsa; |
| 586 | k->dsa = NULL; |
| 587 | success = 1; |
| 588 | #ifdef DEBUG_PK |
| 589 | DSA_print_fp(stderr, ret->dsa, 8); |
| 590 | #endif |
| 591 | } |
| 592 | /*XXXX*/ |
Ben Lindstrom | 4cbc181 | 2001-12-06 16:41:41 +0000 | [diff] [blame] | 593 | key_free(k); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 594 | if (success != 1) |
| 595 | break; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 596 | /* advance cp: skip whitespace and data */ |
| 597 | while (*cp == ' ' || *cp == '\t') |
| 598 | cp++; |
| 599 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') |
| 600 | cp++; |
| 601 | *cpp = cp; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 602 | break; |
| 603 | default: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 604 | fatal("key_read: bad key type: %d", ret->type); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 605 | break; |
| 606 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 607 | return success; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 608 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 609 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 610 | int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 611 | key_write(const Key *key, FILE *f) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 612 | { |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 613 | int n, success = 0; |
| 614 | u_int len, bits = 0; |
Damien Miller | a10f561 | 2002-09-12 09:49:15 +1000 | [diff] [blame] | 615 | u_char *blob; |
| 616 | char *uu; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 617 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 618 | if (key->type == KEY_RSA1 && key->rsa != NULL) { |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 619 | /* size of modulus 'n' */ |
| 620 | bits = BN_num_bits(key->rsa->n); |
| 621 | fprintf(f, "%u", bits); |
| 622 | if (write_bignum(f, key->rsa->e) && |
| 623 | write_bignum(f, key->rsa->n)) { |
| 624 | success = 1; |
| 625 | } else { |
| 626 | error("key_write: failed for RSA key"); |
| 627 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 628 | } else if ((key->type == KEY_DSA && key->dsa != NULL) || |
| 629 | (key->type == KEY_RSA && key->rsa != NULL)) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 630 | key_to_blob(key, &blob, &len); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 631 | uu = xmalloc(2*len); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 632 | n = uuencode(blob, len, uu, 2*len); |
| 633 | if (n > 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 634 | fprintf(f, "%s %s", key_ssh_name(key), uu); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 635 | success = 1; |
| 636 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 637 | xfree(blob); |
| 638 | xfree(uu); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 639 | } |
| 640 | return success; |
| 641 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 642 | |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 643 | const char * |
| 644 | key_type(const Key *k) |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 645 | { |
| 646 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 647 | case KEY_RSA1: |
| 648 | return "RSA1"; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 649 | case KEY_RSA: |
| 650 | return "RSA"; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 651 | case KEY_DSA: |
| 652 | return "DSA"; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 653 | } |
| 654 | return "unknown"; |
| 655 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 656 | |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 657 | const char * |
| 658 | key_ssh_name(const Key *k) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 659 | { |
| 660 | switch (k->type) { |
| 661 | case KEY_RSA: |
| 662 | return "ssh-rsa"; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 663 | case KEY_DSA: |
| 664 | return "ssh-dss"; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 665 | } |
| 666 | return "ssh-unknown"; |
| 667 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 668 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 669 | u_int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 670 | key_size(const Key *k) |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 671 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 672 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 673 | case KEY_RSA1: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 674 | case KEY_RSA: |
| 675 | return BN_num_bits(k->rsa->n); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 676 | case KEY_DSA: |
| 677 | return BN_num_bits(k->dsa->p); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 678 | } |
| 679 | return 0; |
| 680 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 681 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 682 | static RSA * |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 683 | rsa_generate_private_key(u_int bits) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 684 | { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 685 | RSA *private; |
Damien Miller | 69b7203 | 2006-03-26 14:02:35 +1100 | [diff] [blame] | 686 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 687 | private = RSA_generate_key(bits, 35, NULL, NULL); |
| 688 | if (private == NULL) |
| 689 | fatal("rsa_generate_private_key: key generation failed."); |
| 690 | return private; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 691 | } |
| 692 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 693 | static DSA* |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 694 | dsa_generate_private_key(u_int bits) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 695 | { |
| 696 | DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL); |
Damien Miller | 69b7203 | 2006-03-26 14:02:35 +1100 | [diff] [blame] | 697 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 698 | if (private == NULL) |
| 699 | fatal("dsa_generate_private_key: DSA_generate_parameters failed"); |
| 700 | if (!DSA_generate_key(private)) |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 701 | fatal("dsa_generate_private_key: DSA_generate_key failed."); |
| 702 | if (private == NULL) |
| 703 | fatal("dsa_generate_private_key: NULL."); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 704 | return private; |
| 705 | } |
| 706 | |
| 707 | Key * |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 708 | key_generate(int type, u_int bits) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 709 | { |
| 710 | Key *k = key_new(KEY_UNSPEC); |
| 711 | switch (type) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 712 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 713 | k->dsa = dsa_generate_private_key(bits); |
| 714 | break; |
| 715 | case KEY_RSA: |
| 716 | case KEY_RSA1: |
| 717 | k->rsa = rsa_generate_private_key(bits); |
| 718 | break; |
| 719 | default: |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 720 | fatal("key_generate: unknown type %d", type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 721 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 722 | k->type = type; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 723 | return k; |
| 724 | } |
| 725 | |
| 726 | Key * |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 727 | key_from_private(const Key *k) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 728 | { |
| 729 | Key *n = NULL; |
| 730 | switch (k->type) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 731 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 732 | n = key_new(k->type); |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 733 | if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) || |
| 734 | (BN_copy(n->dsa->q, k->dsa->q) == NULL) || |
| 735 | (BN_copy(n->dsa->g, k->dsa->g) == NULL) || |
| 736 | (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL)) |
| 737 | fatal("key_from_private: BN_copy failed"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 738 | break; |
| 739 | case KEY_RSA: |
| 740 | case KEY_RSA1: |
| 741 | n = key_new(k->type); |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 742 | if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) || |
| 743 | (BN_copy(n->rsa->e, k->rsa->e) == NULL)) |
| 744 | fatal("key_from_private: BN_copy failed"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 745 | break; |
| 746 | default: |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 747 | fatal("key_from_private: unknown type %d", k->type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 748 | break; |
| 749 | } |
| 750 | return n; |
| 751 | } |
| 752 | |
| 753 | int |
| 754 | key_type_from_name(char *name) |
| 755 | { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 756 | if (strcmp(name, "rsa1") == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 757 | return KEY_RSA1; |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 758 | } else if (strcmp(name, "rsa") == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 759 | return KEY_RSA; |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 760 | } else if (strcmp(name, "dsa") == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 761 | return KEY_DSA; |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 762 | } else if (strcmp(name, "ssh-rsa") == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 763 | return KEY_RSA; |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 764 | } else if (strcmp(name, "ssh-dss") == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 765 | return KEY_DSA; |
| 766 | } |
Ben Lindstrom | b54873a | 2001-03-11 20:01:55 +0000 | [diff] [blame] | 767 | debug2("key_type_from_name: unknown key type '%s'", name); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 768 | return KEY_UNSPEC; |
| 769 | } |
| 770 | |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 771 | int |
| 772 | key_names_valid2(const char *names) |
| 773 | { |
| 774 | char *s, *cp, *p; |
| 775 | |
| 776 | if (names == NULL || strcmp(names, "") == 0) |
| 777 | return 0; |
| 778 | s = cp = xstrdup(names); |
| 779 | for ((p = strsep(&cp, ",")); p && *p != '\0'; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 780 | (p = strsep(&cp, ","))) { |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 781 | switch (key_type_from_name(p)) { |
| 782 | case KEY_RSA1: |
| 783 | case KEY_UNSPEC: |
| 784 | xfree(s); |
| 785 | return 0; |
| 786 | } |
| 787 | } |
| 788 | debug3("key names ok: [%s]", names); |
| 789 | xfree(s); |
| 790 | return 1; |
| 791 | } |
| 792 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 793 | Key * |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 794 | key_from_blob(const u_char *blob, u_int blen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 795 | { |
| 796 | Buffer b; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 797 | int rlen, type; |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 798 | char *ktype = NULL; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 799 | Key *key = NULL; |
| 800 | |
| 801 | #ifdef DEBUG_PK |
| 802 | dump_base64(stderr, blob, blen); |
| 803 | #endif |
| 804 | buffer_init(&b); |
| 805 | buffer_append(&b, blob, blen); |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 806 | if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) { |
| 807 | error("key_from_blob: can't read key type"); |
| 808 | goto out; |
| 809 | } |
| 810 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 811 | type = key_type_from_name(ktype); |
| 812 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 813 | switch (type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 814 | case KEY_RSA: |
| 815 | key = key_new(type); |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 816 | if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 || |
| 817 | buffer_get_bignum2_ret(&b, key->rsa->n) == -1) { |
| 818 | error("key_from_blob: can't read rsa key"); |
| 819 | key_free(key); |
| 820 | key = NULL; |
| 821 | goto out; |
| 822 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 823 | #ifdef DEBUG_PK |
| 824 | RSA_print_fp(stderr, key->rsa, 8); |
| 825 | #endif |
| 826 | break; |
| 827 | case KEY_DSA: |
| 828 | key = key_new(type); |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 829 | if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 || |
| 830 | buffer_get_bignum2_ret(&b, key->dsa->q) == -1 || |
| 831 | buffer_get_bignum2_ret(&b, key->dsa->g) == -1 || |
| 832 | buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) { |
| 833 | error("key_from_blob: can't read dsa key"); |
| 834 | key_free(key); |
| 835 | key = NULL; |
| 836 | goto out; |
| 837 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 838 | #ifdef DEBUG_PK |
| 839 | DSA_print_fp(stderr, key->dsa, 8); |
| 840 | #endif |
| 841 | break; |
| 842 | case KEY_UNSPEC: |
| 843 | key = key_new(type); |
| 844 | break; |
| 845 | default: |
| 846 | error("key_from_blob: cannot handle type %s", ktype); |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 847 | goto out; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 848 | } |
| 849 | rlen = buffer_len(&b); |
| 850 | if (key != NULL && rlen != 0) |
| 851 | error("key_from_blob: remaining bytes in key blob %d", rlen); |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 852 | out: |
| 853 | if (ktype != NULL) |
| 854 | xfree(ktype); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 855 | buffer_free(&b); |
| 856 | return key; |
| 857 | } |
| 858 | |
| 859 | int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 860 | key_to_blob(const Key *key, u_char **blobp, u_int *lenp) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 861 | { |
| 862 | Buffer b; |
| 863 | int len; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 864 | |
| 865 | if (key == NULL) { |
| 866 | error("key_to_blob: key == NULL"); |
| 867 | return 0; |
| 868 | } |
| 869 | buffer_init(&b); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 870 | switch (key->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 871 | case KEY_DSA: |
| 872 | buffer_put_cstring(&b, key_ssh_name(key)); |
| 873 | buffer_put_bignum2(&b, key->dsa->p); |
| 874 | buffer_put_bignum2(&b, key->dsa->q); |
| 875 | buffer_put_bignum2(&b, key->dsa->g); |
| 876 | buffer_put_bignum2(&b, key->dsa->pub_key); |
| 877 | break; |
| 878 | case KEY_RSA: |
| 879 | buffer_put_cstring(&b, key_ssh_name(key)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 880 | buffer_put_bignum2(&b, key->rsa->e); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 881 | buffer_put_bignum2(&b, key->rsa->n); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 882 | break; |
| 883 | default: |
Ben Lindstrom | 99a30f1 | 2001-09-18 05:49:14 +0000 | [diff] [blame] | 884 | error("key_to_blob: unsupported key type %d", key->type); |
| 885 | buffer_free(&b); |
| 886 | return 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 887 | } |
| 888 | len = buffer_len(&b); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 889 | if (lenp != NULL) |
| 890 | *lenp = len; |
Ben Lindstrom | 2bf759c | 2002-07-07 22:13:31 +0000 | [diff] [blame] | 891 | if (blobp != NULL) { |
| 892 | *blobp = xmalloc(len); |
| 893 | memcpy(*blobp, buffer_ptr(&b), len); |
| 894 | } |
| 895 | memset(buffer_ptr(&b), 0, len); |
| 896 | buffer_free(&b); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 897 | return len; |
| 898 | } |
| 899 | |
| 900 | int |
| 901 | key_sign( |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 902 | const Key *key, |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 903 | u_char **sigp, u_int *lenp, |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 904 | const u_char *data, u_int datalen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 905 | { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 906 | switch (key->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 907 | case KEY_DSA: |
| 908 | return ssh_dss_sign(key, sigp, lenp, data, datalen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 909 | case KEY_RSA: |
| 910 | return ssh_rsa_sign(key, sigp, lenp, data, datalen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 911 | default: |
Darren Tucker | 5cb30ad | 2004-08-12 22:40:24 +1000 | [diff] [blame] | 912 | error("key_sign: invalid key type %d", key->type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 913 | return -1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 914 | } |
| 915 | } |
| 916 | |
Ben Lindstrom | 01fff0c | 2002-06-06 20:54:07 +0000 | [diff] [blame] | 917 | /* |
| 918 | * key_verify returns 1 for a correct signature, 0 for an incorrect signature |
| 919 | * and -1 on error. |
| 920 | */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 921 | int |
| 922 | key_verify( |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 923 | const Key *key, |
| 924 | const u_char *signature, u_int signaturelen, |
| 925 | const u_char *data, u_int datalen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 926 | { |
Ben Lindstrom | 5363aee | 2001-06-25 04:42:20 +0000 | [diff] [blame] | 927 | if (signaturelen == 0) |
| 928 | return -1; |
| 929 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 930 | switch (key->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 931 | case KEY_DSA: |
| 932 | return ssh_dss_verify(key, signature, signaturelen, data, datalen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 933 | case KEY_RSA: |
| 934 | return ssh_rsa_verify(key, signature, signaturelen, data, datalen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 935 | default: |
Darren Tucker | 5cb30ad | 2004-08-12 22:40:24 +1000 | [diff] [blame] | 936 | error("key_verify: invalid key type %d", key->type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 937 | return -1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 938 | } |
| 939 | } |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 940 | |
| 941 | /* Converts a private to a public key */ |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 942 | Key * |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 943 | key_demote(const Key *k) |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 944 | { |
| 945 | Key *pk; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 946 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 947 | pk = xcalloc(1, sizeof(*pk)); |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 948 | pk->type = k->type; |
| 949 | pk->flags = k->flags; |
| 950 | pk->dsa = NULL; |
| 951 | pk->rsa = NULL; |
| 952 | |
| 953 | switch (k->type) { |
| 954 | case KEY_RSA1: |
| 955 | case KEY_RSA: |
| 956 | if ((pk->rsa = RSA_new()) == NULL) |
| 957 | fatal("key_demote: RSA_new failed"); |
| 958 | if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL) |
| 959 | fatal("key_demote: BN_dup failed"); |
| 960 | if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL) |
| 961 | fatal("key_demote: BN_dup failed"); |
| 962 | break; |
| 963 | case KEY_DSA: |
| 964 | if ((pk->dsa = DSA_new()) == NULL) |
| 965 | fatal("key_demote: DSA_new failed"); |
| 966 | if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL) |
| 967 | fatal("key_demote: BN_dup failed"); |
| 968 | if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL) |
| 969 | fatal("key_demote: BN_dup failed"); |
| 970 | if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL) |
| 971 | fatal("key_demote: BN_dup failed"); |
| 972 | if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL) |
| 973 | fatal("key_demote: BN_dup failed"); |
| 974 | break; |
| 975 | default: |
| 976 | fatal("key_free: bad key type %d", k->type); |
| 977 | break; |
| 978 | } |
| 979 | |
| 980 | return (pk); |
| 981 | } |