blob: 8b1057e8ece774cf9d5b68b18530a66ea056b0a7 [file] [log] [blame]
Damien Miller450a7a12000-03-26 13:04:51 +10001/*
Damien Millere4340be2000-09-16 13:29:08 +11002 * read_bignum():
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 *
5 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
10 *
11 *
Ben Lindstrom44697232001-07-04 03:32:30 +000012 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Miller450a7a12000-03-26 13:04:51 +100013 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
Damien Miller450a7a12000-03-26 13:04:51 +100022 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
Damien Miller450a7a12000-03-26 13:04:51 +100034#include "includes.h"
Damien Miller3672e4b2002-02-05 11:54:07 +110035RCSID("$OpenBSD: key.c,v 1.39 2002/01/25 22:07:40 markus Exp $");
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036
Damien Miller450a7a12000-03-26 13:04:51 +100037#include <openssl/evp.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000038
Damien Miller450a7a12000-03-26 13:04:51 +100039#include "xmalloc.h"
40#include "key.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110041#include "rsa.h"
42#include "ssh-dss.h"
43#include "ssh-rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100044#include "uuencode.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110045#include "buffer.h"
46#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "log.h"
Damien Miller450a7a12000-03-26 13:04:51 +100048
49Key *
50key_new(int type)
51{
52 Key *k;
53 RSA *rsa;
54 DSA *dsa;
55 k = xmalloc(sizeof(*k));
56 k->type = type;
Ben Lindstromc5b68002001-07-04 04:52:03 +000057 k->flags = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100058 k->dsa = NULL;
59 k->rsa = NULL;
Damien Miller450a7a12000-03-26 13:04:51 +100060 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +110061 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +100062 case KEY_RSA:
Damien Millerda755162002-01-22 23:09:22 +110063 if ((rsa = RSA_new()) == NULL)
64 fatal("key_new: RSA_new failed");
65 if ((rsa->n = BN_new()) == NULL)
66 fatal("key_new: BN_new failed");
67 if ((rsa->e = BN_new()) == NULL)
68 fatal("key_new: BN_new failed");
Damien Miller450a7a12000-03-26 13:04:51 +100069 k->rsa = rsa;
70 break;
71 case KEY_DSA:
Damien Millerda755162002-01-22 23:09:22 +110072 if ((dsa = DSA_new()) == NULL)
73 fatal("key_new: DSA_new failed");
74 if ((dsa->p = BN_new()) == NULL)
75 fatal("key_new: BN_new failed");
76 if ((dsa->q = BN_new()) == NULL)
77 fatal("key_new: BN_new failed");
78 if ((dsa->g = BN_new()) == NULL)
79 fatal("key_new: BN_new failed");
80 if ((dsa->pub_key = BN_new()) == NULL)
81 fatal("key_new: BN_new failed");
Damien Miller450a7a12000-03-26 13:04:51 +100082 k->dsa = dsa;
83 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +110084 case KEY_UNSPEC:
Damien Miller450a7a12000-03-26 13:04:51 +100085 break;
86 default:
87 fatal("key_new: bad key type %d", k->type);
88 break;
89 }
90 return k;
91}
Damien Miller0bc1bd82000-11-13 22:57:25 +110092Key *
93key_new_private(int type)
94{
95 Key *k = key_new(type);
96 switch (k->type) {
97 case KEY_RSA1:
98 case KEY_RSA:
Damien Millerda755162002-01-22 23:09:22 +110099 if ((k->rsa->d = BN_new()) == NULL)
100 fatal("key_new_private: BN_new failed");
101 if ((k->rsa->iqmp = BN_new()) == NULL)
102 fatal("key_new_private: BN_new failed");
103 if ((k->rsa->q = BN_new()) == NULL)
104 fatal("key_new_private: BN_new failed");
105 if ((k->rsa->p = BN_new()) == NULL)
106 fatal("key_new_private: BN_new failed");
107 if ((k->rsa->dmq1 = BN_new()) == NULL)
108 fatal("key_new_private: BN_new failed");
109 if ((k->rsa->dmp1 = BN_new()) == NULL)
110 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100111 break;
112 case KEY_DSA:
Damien Millerda755162002-01-22 23:09:22 +1100113 if ((k->dsa->priv_key = BN_new()) == NULL)
114 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100115 break;
116 case KEY_UNSPEC:
117 break;
118 default:
119 break;
120 }
121 return k;
122}
Damien Miller450a7a12000-03-26 13:04:51 +1000123void
124key_free(Key *k)
125{
126 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100127 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000128 case KEY_RSA:
129 if (k->rsa != NULL)
130 RSA_free(k->rsa);
131 k->rsa = NULL;
132 break;
133 case KEY_DSA:
134 if (k->dsa != NULL)
135 DSA_free(k->dsa);
136 k->dsa = NULL;
137 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100138 case KEY_UNSPEC:
139 break;
Damien Miller450a7a12000-03-26 13:04:51 +1000140 default:
141 fatal("key_free: bad key type %d", k->type);
142 break;
143 }
144 xfree(k);
145}
146int
147key_equal(Key *a, Key *b)
148{
149 if (a == NULL || b == NULL || a->type != b->type)
150 return 0;
151 switch (a->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100152 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000153 case KEY_RSA:
154 return a->rsa != NULL && b->rsa != NULL &&
155 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
156 BN_cmp(a->rsa->n, b->rsa->n) == 0;
157 break;
158 case KEY_DSA:
159 return a->dsa != NULL && b->dsa != NULL &&
160 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
161 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
162 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
163 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
164 break;
165 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000166 fatal("key_equal: bad key type %d", a->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000167 break;
168 }
169 return 0;
170}
171
Ben Lindstrombba81212001-06-25 05:01:22 +0000172static u_char*
Damien Miller3672e4b2002-02-05 11:54:07 +1100173key_fingerprint_raw(Key *k, enum fp_type dgst_type, u_int *dgst_raw_length)
Damien Miller450a7a12000-03-26 13:04:51 +1000174{
Ben Lindstromf0b48532001-03-12 02:59:31 +0000175 EVP_MD *md = NULL;
176 EVP_MD_CTX ctx;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000177 u_char *blob = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000178 u_char *retval = NULL;
Damien Miller450a7a12000-03-26 13:04:51 +1000179 int len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000180 int nlen, elen;
Damien Miller450a7a12000-03-26 13:04:51 +1000181
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000182 *dgst_raw_length = 0;
183
Ben Lindstromf0b48532001-03-12 02:59:31 +0000184 switch (dgst_type) {
185 case SSH_FP_MD5:
186 md = EVP_md5();
187 break;
188 case SSH_FP_SHA1:
189 md = EVP_sha1();
190 break;
191 default:
192 fatal("key_fingerprint_raw: bad digest type %d",
193 dgst_type);
194 }
Damien Miller450a7a12000-03-26 13:04:51 +1000195 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100196 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000197 nlen = BN_num_bytes(k->rsa->n);
198 elen = BN_num_bytes(k->rsa->e);
199 len = nlen + elen;
Damien Millereba71ba2000-04-29 23:57:08 +1000200 blob = xmalloc(len);
201 BN_bn2bin(k->rsa->n, blob);
202 BN_bn2bin(k->rsa->e, blob + nlen);
Damien Miller450a7a12000-03-26 13:04:51 +1000203 break;
204 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100205 case KEY_RSA:
206 key_to_blob(k, &blob, &len);
207 break;
208 case KEY_UNSPEC:
209 return retval;
Damien Miller450a7a12000-03-26 13:04:51 +1000210 break;
211 default:
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000212 fatal("key_fingerprint_raw: bad key type %d", k->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000213 break;
214 }
Damien Millereba71ba2000-04-29 23:57:08 +1000215 if (blob != NULL) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000216 retval = xmalloc(EVP_MAX_MD_SIZE);
Damien Miller6536c7d2000-06-22 21:32:31 +1000217 EVP_DigestInit(&ctx, md);
218 EVP_DigestUpdate(&ctx, blob, len);
Damien Miller3672e4b2002-02-05 11:54:07 +1100219 EVP_DigestFinal(&ctx, retval, dgst_raw_length);
Damien Millereba71ba2000-04-29 23:57:08 +1000220 memset(blob, 0, len);
221 xfree(blob);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000222 } else {
223 fatal("key_fingerprint_raw: blob is null");
Damien Miller450a7a12000-03-26 13:04:51 +1000224 }
225 return retval;
226}
227
Ben Lindstrombba81212001-06-25 05:01:22 +0000228static char*
Damien Miller3672e4b2002-02-05 11:54:07 +1100229key_fingerprint_hex(u_char* dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000230{
231 char *retval;
232 int i;
233
Ben Lindstrom459d1912001-04-17 18:06:14 +0000234 retval = xmalloc(dgst_raw_len * 3 + 1);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000235 retval[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100236 for (i = 0; i < dgst_raw_len; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000237 char hex[4];
238 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
239 strlcat(retval, hex, dgst_raw_len * 3);
240 }
241 retval[(dgst_raw_len * 3) - 1] = '\0';
242 return retval;
243}
244
Ben Lindstrombba81212001-06-25 05:01:22 +0000245static char*
Damien Miller3672e4b2002-02-05 11:54:07 +1100246key_fingerprint_bubblebabble(u_char* dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000247{
248 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
249 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
250 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000251 u_int i, j = 0, rounds, seed = 1;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000252 char *retval;
253
254 rounds = (dgst_raw_len / 2) + 1;
255 retval = xmalloc(sizeof(char) * (rounds*6));
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000256 retval[j++] = 'x';
257 for (i = 0; i < rounds; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000258 u_int idx0, idx1, idx2, idx3, idx4;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000259 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
260 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000261 seed) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000262 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
263 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000264 (seed / 6)) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000265 retval[j++] = vowels[idx0];
266 retval[j++] = consonants[idx1];
267 retval[j++] = vowels[idx2];
268 if ((i + 1) < rounds) {
269 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
270 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
271 retval[j++] = consonants[idx3];
272 retval[j++] = '-';
273 retval[j++] = consonants[idx4];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000274 seed = ((seed * 5) +
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000275 ((((u_int)(dgst_raw[2 * i])) * 7) +
276 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000277 }
278 } else {
279 idx0 = seed % 6;
280 idx1 = 16;
281 idx2 = seed / 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000282 retval[j++] = vowels[idx0];
283 retval[j++] = consonants[idx1];
284 retval[j++] = vowels[idx2];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000285 }
286 }
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000287 retval[j++] = 'x';
288 retval[j++] = '\0';
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000289 return retval;
290}
291
292char*
Ben Lindstromcfccef92001-03-13 04:57:58 +0000293key_fingerprint(Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000294{
Ben Lindstroma3700052001-04-05 23:26:32 +0000295 char *retval = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000296 u_char *dgst_raw;
Damien Miller3672e4b2002-02-05 11:54:07 +1100297 u_int dgst_raw_len;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100298
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000299 dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
300 if (!dgst_raw)
Ben Lindstromcfccef92001-03-13 04:57:58 +0000301 fatal("key_fingerprint: null from key_fingerprint_raw()");
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000302 switch (dgst_rep) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000303 case SSH_FP_HEX:
304 retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
305 break;
306 case SSH_FP_BUBBLEBABBLE:
307 retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
308 break;
309 default:
310 fatal("key_fingerprint_ex: bad digest representation %d",
311 dgst_rep);
312 break;
313 }
314 memset(dgst_raw, 0, dgst_raw_len);
315 xfree(dgst_raw);
316 return retval;
317}
318
Damien Miller450a7a12000-03-26 13:04:51 +1000319/*
320 * Reads a multiple-precision integer in decimal from the buffer, and advances
321 * the pointer. The integer must already be initialized. This function is
322 * permitted to modify the buffer. This leaves *cpp to point just beyond the
323 * last processed (and maybe modified) character. Note that this may modify
324 * the buffer containing the number.
325 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000326static int
Damien Miller450a7a12000-03-26 13:04:51 +1000327read_bignum(char **cpp, BIGNUM * value)
328{
329 char *cp = *cpp;
330 int old;
331
332 /* Skip any leading whitespace. */
333 for (; *cp == ' ' || *cp == '\t'; cp++)
334 ;
335
336 /* Check that it begins with a decimal digit. */
337 if (*cp < '0' || *cp > '9')
338 return 0;
339
340 /* Save starting position. */
341 *cpp = cp;
342
343 /* Move forward until all decimal digits skipped. */
344 for (; *cp >= '0' && *cp <= '9'; cp++)
345 ;
346
347 /* Save the old terminating character, and replace it by \0. */
348 old = *cp;
349 *cp = 0;
350
351 /* Parse the number. */
352 if (BN_dec2bn(&value, *cpp) == 0)
353 return 0;
354
355 /* Restore old terminating character. */
356 *cp = old;
357
358 /* Move beyond the number and return success. */
359 *cpp = cp;
360 return 1;
361}
Ben Lindstrombba81212001-06-25 05:01:22 +0000362static int
Damien Miller450a7a12000-03-26 13:04:51 +1000363write_bignum(FILE *f, BIGNUM *num)
364{
365 char *buf = BN_bn2dec(num);
366 if (buf == NULL) {
367 error("write_bignum: BN_bn2dec() failed");
368 return 0;
369 }
370 fprintf(f, " %s", buf);
Damien Milleraf3030f2001-10-10 15:00:49 +1000371 OPENSSL_free(buf);
Damien Miller450a7a12000-03-26 13:04:51 +1000372 return 1;
373}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100374
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000375/* returns 1 ok, -1 error */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100376int
Damien Millereba71ba2000-04-29 23:57:08 +1000377key_read(Key *ret, char **cpp)
Damien Miller450a7a12000-03-26 13:04:51 +1000378{
Damien Millereba71ba2000-04-29 23:57:08 +1000379 Key *k;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100380 int success = -1;
381 char *cp, *space;
382 int len, n, type;
383 u_int bits;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000384 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000385
386 cp = *cpp;
387
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000388 switch (ret->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100389 case KEY_RSA1:
Damien Millereba71ba2000-04-29 23:57:08 +1000390 /* Get number of bits. */
391 if (*cp < '0' || *cp > '9')
Damien Miller0bc1bd82000-11-13 22:57:25 +1100392 return -1; /* Bad bit count... */
Damien Millereba71ba2000-04-29 23:57:08 +1000393 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
394 bits = 10 * bits + *cp - '0';
Damien Miller450a7a12000-03-26 13:04:51 +1000395 if (bits == 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100396 return -1;
Damien Millereba71ba2000-04-29 23:57:08 +1000397 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000398 /* Get public exponent, public modulus. */
399 if (!read_bignum(cpp, ret->rsa->e))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100400 return -1;
Damien Miller450a7a12000-03-26 13:04:51 +1000401 if (!read_bignum(cpp, ret->rsa->n))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100402 return -1;
403 success = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000404 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100405 case KEY_UNSPEC:
406 case KEY_RSA:
Damien Miller450a7a12000-03-26 13:04:51 +1000407 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100408 space = strchr(cp, ' ');
409 if (space == NULL) {
410 debug3("key_read: no space");
411 return -1;
412 }
413 *space = '\0';
414 type = key_type_from_name(cp);
415 *space = ' ';
416 if (type == KEY_UNSPEC) {
417 debug3("key_read: no key found");
418 return -1;
419 }
420 cp = space+1;
421 if (*cp == '\0') {
422 debug3("key_read: short string");
423 return -1;
424 }
425 if (ret->type == KEY_UNSPEC) {
426 ret->type = type;
427 } else if (ret->type != type) {
428 /* is a key, but different type */
429 debug3("key_read: type mismatch");
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000430 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100431 }
Damien Millereba71ba2000-04-29 23:57:08 +1000432 len = 2*strlen(cp);
433 blob = xmalloc(len);
434 n = uudecode(cp, blob, len);
Damien Millere247cc42000-05-07 12:03:14 +1000435 if (n < 0) {
Damien Millerb1715dc2000-05-30 13:44:51 +1000436 error("key_read: uudecode %s failed", cp);
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000437 xfree(blob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100438 return -1;
Damien Millere247cc42000-05-07 12:03:14 +1000439 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100440 k = key_from_blob(blob, n);
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000441 xfree(blob);
Damien Millerb1715dc2000-05-30 13:44:51 +1000442 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100443 error("key_read: key_from_blob %s failed", cp);
444 return -1;
Damien Millerb1715dc2000-05-30 13:44:51 +1000445 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100446 if (k->type != type) {
447 error("key_read: type mismatch: encoding error");
448 key_free(k);
449 return -1;
450 }
451/*XXXX*/
452 if (ret->type == KEY_RSA) {
453 if (ret->rsa != NULL)
454 RSA_free(ret->rsa);
455 ret->rsa = k->rsa;
456 k->rsa = NULL;
457 success = 1;
458#ifdef DEBUG_PK
459 RSA_print_fp(stderr, ret->rsa, 8);
460#endif
461 } else {
462 if (ret->dsa != NULL)
463 DSA_free(ret->dsa);
464 ret->dsa = k->dsa;
465 k->dsa = NULL;
466 success = 1;
467#ifdef DEBUG_PK
468 DSA_print_fp(stderr, ret->dsa, 8);
469#endif
470 }
471/*XXXX*/
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000472 key_free(k);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100473 if (success != 1)
474 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000475 /* advance cp: skip whitespace and data */
476 while (*cp == ' ' || *cp == '\t')
477 cp++;
478 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
479 cp++;
480 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000481 break;
482 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000483 fatal("key_read: bad key type: %d", ret->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000484 break;
485 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100486 return success;
Damien Miller450a7a12000-03-26 13:04:51 +1000487}
488int
489key_write(Key *key, FILE *f)
490{
491 int success = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000492 u_int bits = 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000493
Damien Miller0bc1bd82000-11-13 22:57:25 +1100494 if (key->type == KEY_RSA1 && key->rsa != NULL) {
Damien Miller450a7a12000-03-26 13:04:51 +1000495 /* size of modulus 'n' */
496 bits = BN_num_bits(key->rsa->n);
497 fprintf(f, "%u", bits);
498 if (write_bignum(f, key->rsa->e) &&
499 write_bignum(f, key->rsa->n)) {
500 success = 1;
501 } else {
502 error("key_write: failed for RSA key");
503 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100504 } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
505 (key->type == KEY_RSA && key->rsa != NULL)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000506 int len, n;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000507 u_char *blob, *uu;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100508 key_to_blob(key, &blob, &len);
Damien Millereba71ba2000-04-29 23:57:08 +1000509 uu = xmalloc(2*len);
Damien Millere247cc42000-05-07 12:03:14 +1000510 n = uuencode(blob, len, uu, 2*len);
511 if (n > 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100512 fprintf(f, "%s %s", key_ssh_name(key), uu);
Damien Millere247cc42000-05-07 12:03:14 +1000513 success = 1;
514 }
Damien Millereba71ba2000-04-29 23:57:08 +1000515 xfree(blob);
516 xfree(uu);
Damien Miller450a7a12000-03-26 13:04:51 +1000517 }
518 return success;
519}
Damien Millere247cc42000-05-07 12:03:14 +1000520char *
521key_type(Key *k)
522{
523 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100524 case KEY_RSA1:
525 return "RSA1";
526 break;
Damien Millere247cc42000-05-07 12:03:14 +1000527 case KEY_RSA:
528 return "RSA";
529 break;
530 case KEY_DSA:
531 return "DSA";
532 break;
533 }
534 return "unknown";
535}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100536char *
537key_ssh_name(Key *k)
538{
539 switch (k->type) {
540 case KEY_RSA:
541 return "ssh-rsa";
542 break;
543 case KEY_DSA:
544 return "ssh-dss";
545 break;
546 }
547 return "ssh-unknown";
548}
549u_int
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000550key_size(Key *k)
551{
Damien Millerad833b32000-08-23 10:46:23 +1000552 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100553 case KEY_RSA1:
Damien Millerad833b32000-08-23 10:46:23 +1000554 case KEY_RSA:
555 return BN_num_bits(k->rsa->n);
556 break;
557 case KEY_DSA:
558 return BN_num_bits(k->dsa->p);
559 break;
560 }
561 return 0;
562}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100563
Ben Lindstrombba81212001-06-25 05:01:22 +0000564static RSA *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000565rsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100566{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000567 RSA *private;
568 private = RSA_generate_key(bits, 35, NULL, NULL);
569 if (private == NULL)
570 fatal("rsa_generate_private_key: key generation failed.");
571 return private;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100572}
573
Ben Lindstrombba81212001-06-25 05:01:22 +0000574static DSA*
Ben Lindstrom46c16222000-12-22 01:43:59 +0000575dsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100576{
577 DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
578 if (private == NULL)
579 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
580 if (!DSA_generate_key(private))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000581 fatal("dsa_generate_private_key: DSA_generate_key failed.");
582 if (private == NULL)
583 fatal("dsa_generate_private_key: NULL.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100584 return private;
585}
586
587Key *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000588key_generate(int type, u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100589{
590 Key *k = key_new(KEY_UNSPEC);
591 switch (type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000592 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100593 k->dsa = dsa_generate_private_key(bits);
594 break;
595 case KEY_RSA:
596 case KEY_RSA1:
597 k->rsa = rsa_generate_private_key(bits);
598 break;
599 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000600 fatal("key_generate: unknown type %d", type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100601 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000602 k->type = type;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100603 return k;
604}
605
606Key *
607key_from_private(Key *k)
608{
609 Key *n = NULL;
610 switch (k->type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000611 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100612 n = key_new(k->type);
613 BN_copy(n->dsa->p, k->dsa->p);
614 BN_copy(n->dsa->q, k->dsa->q);
615 BN_copy(n->dsa->g, k->dsa->g);
616 BN_copy(n->dsa->pub_key, k->dsa->pub_key);
617 break;
618 case KEY_RSA:
619 case KEY_RSA1:
620 n = key_new(k->type);
621 BN_copy(n->rsa->n, k->rsa->n);
622 BN_copy(n->rsa->e, k->rsa->e);
623 break;
624 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000625 fatal("key_from_private: unknown type %d", k->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100626 break;
627 }
628 return n;
629}
630
631int
632key_type_from_name(char *name)
633{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000634 if (strcmp(name, "rsa1") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100635 return KEY_RSA1;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000636 } else if (strcmp(name, "rsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100637 return KEY_RSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000638 } else if (strcmp(name, "dsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100639 return KEY_DSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000640 } else if (strcmp(name, "ssh-rsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100641 return KEY_RSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000642 } else if (strcmp(name, "ssh-dss") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100643 return KEY_DSA;
644 }
Ben Lindstromb54873a2001-03-11 20:01:55 +0000645 debug2("key_type_from_name: unknown key type '%s'", name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100646 return KEY_UNSPEC;
647}
648
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000649int
650key_names_valid2(const char *names)
651{
652 char *s, *cp, *p;
653
654 if (names == NULL || strcmp(names, "") == 0)
655 return 0;
656 s = cp = xstrdup(names);
657 for ((p = strsep(&cp, ",")); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100658 (p = strsep(&cp, ","))) {
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000659 switch (key_type_from_name(p)) {
660 case KEY_RSA1:
661 case KEY_UNSPEC:
662 xfree(s);
663 return 0;
664 }
665 }
666 debug3("key names ok: [%s]", names);
667 xfree(s);
668 return 1;
669}
670
Damien Miller0bc1bd82000-11-13 22:57:25 +1100671Key *
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000672key_from_blob(u_char *blob, int blen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100673{
674 Buffer b;
675 char *ktype;
676 int rlen, type;
677 Key *key = NULL;
678
679#ifdef DEBUG_PK
680 dump_base64(stderr, blob, blen);
681#endif
682 buffer_init(&b);
683 buffer_append(&b, blob, blen);
684 ktype = buffer_get_string(&b, NULL);
685 type = key_type_from_name(ktype);
686
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000687 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100688 case KEY_RSA:
689 key = key_new(type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100690 buffer_get_bignum2(&b, key->rsa->e);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000691 buffer_get_bignum2(&b, key->rsa->n);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100692#ifdef DEBUG_PK
693 RSA_print_fp(stderr, key->rsa, 8);
694#endif
695 break;
696 case KEY_DSA:
697 key = key_new(type);
698 buffer_get_bignum2(&b, key->dsa->p);
699 buffer_get_bignum2(&b, key->dsa->q);
700 buffer_get_bignum2(&b, key->dsa->g);
701 buffer_get_bignum2(&b, key->dsa->pub_key);
702#ifdef DEBUG_PK
703 DSA_print_fp(stderr, key->dsa, 8);
704#endif
705 break;
706 case KEY_UNSPEC:
707 key = key_new(type);
708 break;
709 default:
710 error("key_from_blob: cannot handle type %s", ktype);
711 break;
712 }
713 rlen = buffer_len(&b);
714 if (key != NULL && rlen != 0)
715 error("key_from_blob: remaining bytes in key blob %d", rlen);
716 xfree(ktype);
717 buffer_free(&b);
718 return key;
719}
720
721int
Ben Lindstrom46c16222000-12-22 01:43:59 +0000722key_to_blob(Key *key, u_char **blobp, u_int *lenp)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100723{
724 Buffer b;
725 int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000726 u_char *buf;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100727
728 if (key == NULL) {
729 error("key_to_blob: key == NULL");
730 return 0;
731 }
732 buffer_init(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000733 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100734 case KEY_DSA:
735 buffer_put_cstring(&b, key_ssh_name(key));
736 buffer_put_bignum2(&b, key->dsa->p);
737 buffer_put_bignum2(&b, key->dsa->q);
738 buffer_put_bignum2(&b, key->dsa->g);
739 buffer_put_bignum2(&b, key->dsa->pub_key);
740 break;
741 case KEY_RSA:
742 buffer_put_cstring(&b, key_ssh_name(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100743 buffer_put_bignum2(&b, key->rsa->e);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000744 buffer_put_bignum2(&b, key->rsa->n);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100745 break;
746 default:
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000747 error("key_to_blob: unsupported key type %d", key->type);
748 buffer_free(&b);
749 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100750 }
751 len = buffer_len(&b);
752 buf = xmalloc(len);
753 memcpy(buf, buffer_ptr(&b), len);
754 memset(buffer_ptr(&b), 0, len);
755 buffer_free(&b);
756 if (lenp != NULL)
757 *lenp = len;
758 if (blobp != NULL)
759 *blobp = buf;
760 return len;
761}
762
763int
764key_sign(
765 Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000766 u_char **sigp, int *lenp,
767 u_char *data, int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100768{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000769 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100770 case KEY_DSA:
771 return ssh_dss_sign(key, sigp, lenp, data, datalen);
772 break;
773 case KEY_RSA:
774 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
775 break;
776 default:
777 error("key_sign: illegal key type %d", key->type);
778 return -1;
779 break;
780 }
781}
782
783int
784key_verify(
785 Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000786 u_char *signature, int signaturelen,
787 u_char *data, int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100788{
Ben Lindstrom5363aee2001-06-25 04:42:20 +0000789 if (signaturelen == 0)
790 return -1;
791
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000792 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100793 case KEY_DSA:
794 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
795 break;
796 case KEY_RSA:
797 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
798 break;
799 default:
800 error("key_verify: illegal key type %d", key->type);
801 return -1;
802 break;
803 }
804}