blob: d05c86ae7f245781743ccbaec3005dbf880b8712 [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 *
Damien Miller450a7a12000-03-26 13:04:51 +100012 * Copyright (c) 2000 Markus Friedl. All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
Damien 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"
Ben Lindstromf0b48532001-03-12 02:59:31 +000035RCSID("$OpenBSD: key.c,v 1.21 2001/03/11 18:29:51 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;
Damien Millereba71ba2000-04-29 23:57:08 +100057 k->dsa = NULL;
58 k->rsa = NULL;
Damien Miller450a7a12000-03-26 13:04:51 +100059 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +110060 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +100061 case KEY_RSA:
62 rsa = RSA_new();
63 rsa->n = BN_new();
64 rsa->e = BN_new();
65 k->rsa = rsa;
66 break;
67 case KEY_DSA:
68 dsa = DSA_new();
69 dsa->p = BN_new();
70 dsa->q = BN_new();
71 dsa->g = BN_new();
72 dsa->pub_key = BN_new();
73 k->dsa = dsa;
74 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +110075 case KEY_UNSPEC:
Damien Miller450a7a12000-03-26 13:04:51 +100076 break;
77 default:
78 fatal("key_new: bad key type %d", k->type);
79 break;
80 }
81 return k;
82}
Damien Miller0bc1bd82000-11-13 22:57:25 +110083Key *
84key_new_private(int type)
85{
86 Key *k = key_new(type);
87 switch (k->type) {
88 case KEY_RSA1:
89 case KEY_RSA:
90 k->rsa->d = BN_new();
91 k->rsa->iqmp = BN_new();
92 k->rsa->q = BN_new();
93 k->rsa->p = BN_new();
94 k->rsa->dmq1 = BN_new();
95 k->rsa->dmp1 = BN_new();
96 break;
97 case KEY_DSA:
98 k->dsa->priv_key = BN_new();
99 break;
100 case KEY_UNSPEC:
101 break;
102 default:
103 break;
104 }
105 return k;
106}
Damien Miller450a7a12000-03-26 13:04:51 +1000107void
108key_free(Key *k)
109{
110 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100111 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000112 case KEY_RSA:
113 if (k->rsa != NULL)
114 RSA_free(k->rsa);
115 k->rsa = NULL;
116 break;
117 case KEY_DSA:
118 if (k->dsa != NULL)
119 DSA_free(k->dsa);
120 k->dsa = NULL;
121 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100122 case KEY_UNSPEC:
123 break;
Damien Miller450a7a12000-03-26 13:04:51 +1000124 default:
125 fatal("key_free: bad key type %d", k->type);
126 break;
127 }
128 xfree(k);
129}
130int
131key_equal(Key *a, Key *b)
132{
133 if (a == NULL || b == NULL || a->type != b->type)
134 return 0;
135 switch (a->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100136 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000137 case KEY_RSA:
138 return a->rsa != NULL && b->rsa != NULL &&
139 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
140 BN_cmp(a->rsa->n, b->rsa->n) == 0;
141 break;
142 case KEY_DSA:
143 return a->dsa != NULL && b->dsa != NULL &&
144 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
145 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
146 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
147 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
148 break;
149 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000150 fatal("key_equal: bad key type %d", a->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000151 break;
152 }
153 return 0;
154}
155
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000156u_char*
157key_fingerprint_raw(Key *k, enum fp_type dgst_type, size_t *dgst_raw_length)
Damien Miller450a7a12000-03-26 13:04:51 +1000158{
Ben Lindstromf0b48532001-03-12 02:59:31 +0000159 EVP_MD *md = NULL;
160 EVP_MD_CTX ctx;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000161 u_char *blob = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000162 u_char *retval = NULL;
Damien Miller450a7a12000-03-26 13:04:51 +1000163 int len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000164 int nlen, elen;
Damien Miller450a7a12000-03-26 13:04:51 +1000165
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000166 *dgst_raw_length = 0;
167
Ben Lindstromf0b48532001-03-12 02:59:31 +0000168 switch (dgst_type) {
169 case SSH_FP_MD5:
170 md = EVP_md5();
171 break;
172 case SSH_FP_SHA1:
173 md = EVP_sha1();
174 break;
175 default:
176 fatal("key_fingerprint_raw: bad digest type %d",
177 dgst_type);
178 }
Damien Miller450a7a12000-03-26 13:04:51 +1000179 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100180 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000181 nlen = BN_num_bytes(k->rsa->n);
182 elen = BN_num_bytes(k->rsa->e);
183 len = nlen + elen;
Damien Millereba71ba2000-04-29 23:57:08 +1000184 blob = xmalloc(len);
185 BN_bn2bin(k->rsa->n, blob);
186 BN_bn2bin(k->rsa->e, blob + nlen);
Damien Miller450a7a12000-03-26 13:04:51 +1000187 break;
188 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100189 case KEY_RSA:
190 key_to_blob(k, &blob, &len);
191 break;
192 case KEY_UNSPEC:
193 return retval;
Damien Miller450a7a12000-03-26 13:04:51 +1000194 break;
195 default:
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000196 fatal("key_fingerprint_raw: bad key type %d", k->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000197 break;
198 }
Damien Millereba71ba2000-04-29 23:57:08 +1000199 if (blob != NULL) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000200 retval = xmalloc(EVP_MAX_MD_SIZE);
Damien Miller6536c7d2000-06-22 21:32:31 +1000201 EVP_DigestInit(&ctx, md);
202 EVP_DigestUpdate(&ctx, blob, len);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000203 EVP_DigestFinal(&ctx, retval, NULL);
204 *dgst_raw_length = md->md_size;
Damien Millereba71ba2000-04-29 23:57:08 +1000205 memset(blob, 0, len);
206 xfree(blob);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000207 } else {
208 fatal("key_fingerprint_raw: blob is null");
Damien Miller450a7a12000-03-26 13:04:51 +1000209 }
210 return retval;
211}
212
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000213char*
214key_fingerprint_hex(u_char* dgst_raw, size_t dgst_raw_len)
215{
216 char *retval;
217 int i;
218
219 retval = xmalloc(dgst_raw_len * 3);
220 retval[0] = '\0';
221 for(i = 0; i < dgst_raw_len; i++) {
222 char hex[4];
223 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
224 strlcat(retval, hex, dgst_raw_len * 3);
225 }
226 retval[(dgst_raw_len * 3) - 1] = '\0';
227 return retval;
228}
229
230char*
231key_fingerprint_bubblebabble(u_char* dgst_raw, size_t dgst_raw_len)
232{
233 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
234 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
235 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000236 u_int i, j = 0, rounds, seed = 1;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000237 char *retval;
238
239 rounds = (dgst_raw_len / 2) + 1;
240 retval = xmalloc(sizeof(char) * (rounds*6));
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000241 retval[j++] = 'x';
242 for (i = 0; i < rounds; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000243 u_int idx0, idx1, idx2, idx3, idx4;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000244 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
245 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000246 seed) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000247 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
248 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000249 (seed / 6)) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000250 retval[j++] = vowels[idx0];
251 retval[j++] = consonants[idx1];
252 retval[j++] = vowels[idx2];
253 if ((i + 1) < rounds) {
254 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
255 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
256 retval[j++] = consonants[idx3];
257 retval[j++] = '-';
258 retval[j++] = consonants[idx4];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000259 seed = ((seed * 5) +
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000260 ((((u_int)(dgst_raw[2 * i])) * 7) +
261 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000262 }
263 } else {
264 idx0 = seed % 6;
265 idx1 = 16;
266 idx2 = seed / 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000267 retval[j++] = vowels[idx0];
268 retval[j++] = consonants[idx1];
269 retval[j++] = vowels[idx2];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000270 }
271 }
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000272 retval[j++] = 'x';
273 retval[j++] = '\0';
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000274 return retval;
275}
276
277char*
278key_fingerprint_ex(Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
279{
280 char *retval = NULL;
281 u_char *dgst_raw;
282 size_t dgst_raw_len;
283
284 dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
285 if (!dgst_raw)
286 fatal("key_fingerprint_ex: null value returned from key_fingerprint_raw()");
287 switch(dgst_rep) {
288 case SSH_FP_HEX:
289 retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
290 break;
291 case SSH_FP_BUBBLEBABBLE:
292 retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
293 break;
294 default:
295 fatal("key_fingerprint_ex: bad digest representation %d",
296 dgst_rep);
297 break;
298 }
299 memset(dgst_raw, 0, dgst_raw_len);
300 xfree(dgst_raw);
301 return retval;
302}
303
304char *
305key_fingerprint(Key *k)
306{
307 static char retval[(EVP_MAX_MD_SIZE + 1) * 3];
308 char *digest;
309
310 digest = key_fingerprint_ex(k, SSH_FP_MD5, SSH_FP_HEX);
311 strlcpy(retval, digest, sizeof(retval));
312 xfree(digest);
313 return retval;
314}
315
Damien Miller450a7a12000-03-26 13:04:51 +1000316/*
317 * Reads a multiple-precision integer in decimal from the buffer, and advances
318 * the pointer. The integer must already be initialized. This function is
319 * permitted to modify the buffer. This leaves *cpp to point just beyond the
320 * last processed (and maybe modified) character. Note that this may modify
321 * the buffer containing the number.
322 */
323int
324read_bignum(char **cpp, BIGNUM * value)
325{
326 char *cp = *cpp;
327 int old;
328
329 /* Skip any leading whitespace. */
330 for (; *cp == ' ' || *cp == '\t'; cp++)
331 ;
332
333 /* Check that it begins with a decimal digit. */
334 if (*cp < '0' || *cp > '9')
335 return 0;
336
337 /* Save starting position. */
338 *cpp = cp;
339
340 /* Move forward until all decimal digits skipped. */
341 for (; *cp >= '0' && *cp <= '9'; cp++)
342 ;
343
344 /* Save the old terminating character, and replace it by \0. */
345 old = *cp;
346 *cp = 0;
347
348 /* Parse the number. */
349 if (BN_dec2bn(&value, *cpp) == 0)
350 return 0;
351
352 /* Restore old terminating character. */
353 *cp = old;
354
355 /* Move beyond the number and return success. */
356 *cpp = cp;
357 return 1;
358}
359int
360write_bignum(FILE *f, BIGNUM *num)
361{
362 char *buf = BN_bn2dec(num);
363 if (buf == NULL) {
364 error("write_bignum: BN_bn2dec() failed");
365 return 0;
366 }
367 fprintf(f, " %s", buf);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000368 xfree(buf);
Damien Miller450a7a12000-03-26 13:04:51 +1000369 return 1;
370}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100371
372/* returns 1 ok, -1 error, 0 type mismatch */
373int
Damien Millereba71ba2000-04-29 23:57:08 +1000374key_read(Key *ret, char **cpp)
Damien Miller450a7a12000-03-26 13:04:51 +1000375{
Damien Millereba71ba2000-04-29 23:57:08 +1000376 Key *k;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100377 int success = -1;
378 char *cp, *space;
379 int len, n, type;
380 u_int bits;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000381 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000382
383 cp = *cpp;
384
Damien Miller450a7a12000-03-26 13:04:51 +1000385 switch(ret->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100386 case KEY_RSA1:
Damien Millereba71ba2000-04-29 23:57:08 +1000387 /* Get number of bits. */
388 if (*cp < '0' || *cp > '9')
Damien Miller0bc1bd82000-11-13 22:57:25 +1100389 return -1; /* Bad bit count... */
Damien Millereba71ba2000-04-29 23:57:08 +1000390 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
391 bits = 10 * bits + *cp - '0';
Damien Miller450a7a12000-03-26 13:04:51 +1000392 if (bits == 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100393 return -1;
Damien Millereba71ba2000-04-29 23:57:08 +1000394 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000395 /* Get public exponent, public modulus. */
396 if (!read_bignum(cpp, ret->rsa->e))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100397 return -1;
Damien Miller450a7a12000-03-26 13:04:51 +1000398 if (!read_bignum(cpp, ret->rsa->n))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100399 return -1;
400 success = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000401 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100402 case KEY_UNSPEC:
403 case KEY_RSA:
Damien Miller450a7a12000-03-26 13:04:51 +1000404 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100405 space = strchr(cp, ' ');
406 if (space == NULL) {
407 debug3("key_read: no space");
408 return -1;
409 }
410 *space = '\0';
411 type = key_type_from_name(cp);
412 *space = ' ';
413 if (type == KEY_UNSPEC) {
414 debug3("key_read: no key found");
415 return -1;
416 }
417 cp = space+1;
418 if (*cp == '\0') {
419 debug3("key_read: short string");
420 return -1;
421 }
422 if (ret->type == KEY_UNSPEC) {
423 ret->type = type;
424 } else if (ret->type != type) {
425 /* is a key, but different type */
426 debug3("key_read: type mismatch");
Damien Miller450a7a12000-03-26 13:04:51 +1000427 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100428 }
Damien Millereba71ba2000-04-29 23:57:08 +1000429 len = 2*strlen(cp);
430 blob = xmalloc(len);
431 n = uudecode(cp, blob, len);
Damien Millere247cc42000-05-07 12:03:14 +1000432 if (n < 0) {
Damien Millerb1715dc2000-05-30 13:44:51 +1000433 error("key_read: uudecode %s failed", cp);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100434 return -1;
Damien Millere247cc42000-05-07 12:03:14 +1000435 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100436 k = key_from_blob(blob, n);
Damien Millerb1715dc2000-05-30 13:44:51 +1000437 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100438 error("key_read: key_from_blob %s failed", cp);
439 return -1;
Damien Millerb1715dc2000-05-30 13:44:51 +1000440 }
Damien Millereba71ba2000-04-29 23:57:08 +1000441 xfree(blob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100442 if (k->type != type) {
443 error("key_read: type mismatch: encoding error");
444 key_free(k);
445 return -1;
446 }
447/*XXXX*/
448 if (ret->type == KEY_RSA) {
449 if (ret->rsa != NULL)
450 RSA_free(ret->rsa);
451 ret->rsa = k->rsa;
452 k->rsa = NULL;
453 success = 1;
454#ifdef DEBUG_PK
455 RSA_print_fp(stderr, ret->rsa, 8);
456#endif
457 } else {
458 if (ret->dsa != NULL)
459 DSA_free(ret->dsa);
460 ret->dsa = k->dsa;
461 k->dsa = NULL;
462 success = 1;
463#ifdef DEBUG_PK
464 DSA_print_fp(stderr, ret->dsa, 8);
465#endif
466 }
467/*XXXX*/
468 if (success != 1)
469 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000470 key_free(k);
Damien Millerb1715dc2000-05-30 13:44:51 +1000471 /* advance cp: skip whitespace and data */
472 while (*cp == ' ' || *cp == '\t')
473 cp++;
474 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
475 cp++;
476 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000477 break;
478 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000479 fatal("key_read: bad key type: %d", ret->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000480 break;
481 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100482 return success;
Damien Miller450a7a12000-03-26 13:04:51 +1000483}
484int
485key_write(Key *key, FILE *f)
486{
487 int success = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000488 u_int bits = 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000489
Damien Miller0bc1bd82000-11-13 22:57:25 +1100490 if (key->type == KEY_RSA1 && key->rsa != NULL) {
Damien Miller450a7a12000-03-26 13:04:51 +1000491 /* size of modulus 'n' */
492 bits = BN_num_bits(key->rsa->n);
493 fprintf(f, "%u", bits);
494 if (write_bignum(f, key->rsa->e) &&
495 write_bignum(f, key->rsa->n)) {
496 success = 1;
497 } else {
498 error("key_write: failed for RSA key");
499 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100500 } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
501 (key->type == KEY_RSA && key->rsa != NULL)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000502 int len, n;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000503 u_char *blob, *uu;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100504 key_to_blob(key, &blob, &len);
Damien Millereba71ba2000-04-29 23:57:08 +1000505 uu = xmalloc(2*len);
Damien Millere247cc42000-05-07 12:03:14 +1000506 n = uuencode(blob, len, uu, 2*len);
507 if (n > 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100508 fprintf(f, "%s %s", key_ssh_name(key), uu);
Damien Millere247cc42000-05-07 12:03:14 +1000509 success = 1;
510 }
Damien Millereba71ba2000-04-29 23:57:08 +1000511 xfree(blob);
512 xfree(uu);
Damien Miller450a7a12000-03-26 13:04:51 +1000513 }
514 return success;
515}
Damien Millere247cc42000-05-07 12:03:14 +1000516char *
517key_type(Key *k)
518{
519 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100520 case KEY_RSA1:
521 return "RSA1";
522 break;
Damien Millere247cc42000-05-07 12:03:14 +1000523 case KEY_RSA:
524 return "RSA";
525 break;
526 case KEY_DSA:
527 return "DSA";
528 break;
529 }
530 return "unknown";
531}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100532char *
533key_ssh_name(Key *k)
534{
535 switch (k->type) {
536 case KEY_RSA:
537 return "ssh-rsa";
538 break;
539 case KEY_DSA:
540 return "ssh-dss";
541 break;
542 }
543 return "ssh-unknown";
544}
545u_int
Damien Millerad833b32000-08-23 10:46:23 +1000546key_size(Key *k){
547 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100548 case KEY_RSA1:
Damien Millerad833b32000-08-23 10:46:23 +1000549 case KEY_RSA:
550 return BN_num_bits(k->rsa->n);
551 break;
552 case KEY_DSA:
553 return BN_num_bits(k->dsa->p);
554 break;
555 }
556 return 0;
557}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100558
559RSA *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000560rsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100561{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000562 RSA *private;
563 private = RSA_generate_key(bits, 35, NULL, NULL);
564 if (private == NULL)
565 fatal("rsa_generate_private_key: key generation failed.");
566 return private;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100567}
568
569DSA*
Ben Lindstrom46c16222000-12-22 01:43:59 +0000570dsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100571{
572 DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
573 if (private == NULL)
574 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
575 if (!DSA_generate_key(private))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000576 fatal("dsa_generate_private_key: DSA_generate_key failed.");
577 if (private == NULL)
578 fatal("dsa_generate_private_key: NULL.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100579 return private;
580}
581
582Key *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000583key_generate(int type, u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100584{
585 Key *k = key_new(KEY_UNSPEC);
586 switch (type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000587 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100588 k->dsa = dsa_generate_private_key(bits);
589 break;
590 case KEY_RSA:
591 case KEY_RSA1:
592 k->rsa = rsa_generate_private_key(bits);
593 break;
594 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000595 fatal("key_generate: unknown type %d", type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100596 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000597 k->type = type;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100598 return k;
599}
600
601Key *
602key_from_private(Key *k)
603{
604 Key *n = NULL;
605 switch (k->type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000606 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100607 n = key_new(k->type);
608 BN_copy(n->dsa->p, k->dsa->p);
609 BN_copy(n->dsa->q, k->dsa->q);
610 BN_copy(n->dsa->g, k->dsa->g);
611 BN_copy(n->dsa->pub_key, k->dsa->pub_key);
612 break;
613 case KEY_RSA:
614 case KEY_RSA1:
615 n = key_new(k->type);
616 BN_copy(n->rsa->n, k->rsa->n);
617 BN_copy(n->rsa->e, k->rsa->e);
618 break;
619 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000620 fatal("key_from_private: unknown type %d", k->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100621 break;
622 }
623 return n;
624}
625
626int
627key_type_from_name(char *name)
628{
629 if (strcmp(name, "rsa1") == 0){
630 return KEY_RSA1;
631 } else if (strcmp(name, "rsa") == 0){
632 return KEY_RSA;
633 } else if (strcmp(name, "dsa") == 0){
634 return KEY_DSA;
635 } else if (strcmp(name, "ssh-rsa") == 0){
636 return KEY_RSA;
637 } else if (strcmp(name, "ssh-dss") == 0){
638 return KEY_DSA;
639 }
Ben Lindstromb54873a2001-03-11 20:01:55 +0000640 debug2("key_type_from_name: unknown key type '%s'", name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100641 return KEY_UNSPEC;
642}
643
644Key *
645key_from_blob(char *blob, int blen)
646{
647 Buffer b;
648 char *ktype;
649 int rlen, type;
650 Key *key = NULL;
651
652#ifdef DEBUG_PK
653 dump_base64(stderr, blob, blen);
654#endif
655 buffer_init(&b);
656 buffer_append(&b, blob, blen);
657 ktype = buffer_get_string(&b, NULL);
658 type = key_type_from_name(ktype);
659
660 switch(type){
661 case KEY_RSA:
662 key = key_new(type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100663 buffer_get_bignum2(&b, key->rsa->e);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000664 buffer_get_bignum2(&b, key->rsa->n);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100665#ifdef DEBUG_PK
666 RSA_print_fp(stderr, key->rsa, 8);
667#endif
668 break;
669 case KEY_DSA:
670 key = key_new(type);
671 buffer_get_bignum2(&b, key->dsa->p);
672 buffer_get_bignum2(&b, key->dsa->q);
673 buffer_get_bignum2(&b, key->dsa->g);
674 buffer_get_bignum2(&b, key->dsa->pub_key);
675#ifdef DEBUG_PK
676 DSA_print_fp(stderr, key->dsa, 8);
677#endif
678 break;
679 case KEY_UNSPEC:
680 key = key_new(type);
681 break;
682 default:
683 error("key_from_blob: cannot handle type %s", ktype);
684 break;
685 }
686 rlen = buffer_len(&b);
687 if (key != NULL && rlen != 0)
688 error("key_from_blob: remaining bytes in key blob %d", rlen);
689 xfree(ktype);
690 buffer_free(&b);
691 return key;
692}
693
694int
Ben Lindstrom46c16222000-12-22 01:43:59 +0000695key_to_blob(Key *key, u_char **blobp, u_int *lenp)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100696{
697 Buffer b;
698 int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000699 u_char *buf;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100700
701 if (key == NULL) {
702 error("key_to_blob: key == NULL");
703 return 0;
704 }
705 buffer_init(&b);
706 switch(key->type){
707 case KEY_DSA:
708 buffer_put_cstring(&b, key_ssh_name(key));
709 buffer_put_bignum2(&b, key->dsa->p);
710 buffer_put_bignum2(&b, key->dsa->q);
711 buffer_put_bignum2(&b, key->dsa->g);
712 buffer_put_bignum2(&b, key->dsa->pub_key);
713 break;
714 case KEY_RSA:
715 buffer_put_cstring(&b, key_ssh_name(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100716 buffer_put_bignum2(&b, key->rsa->e);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000717 buffer_put_bignum2(&b, key->rsa->n);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100718 break;
719 default:
720 error("key_to_blob: illegal key type %d", key->type);
721 break;
722 }
723 len = buffer_len(&b);
724 buf = xmalloc(len);
725 memcpy(buf, buffer_ptr(&b), len);
726 memset(buffer_ptr(&b), 0, len);
727 buffer_free(&b);
728 if (lenp != NULL)
729 *lenp = len;
730 if (blobp != NULL)
731 *blobp = buf;
732 return len;
733}
734
735int
736key_sign(
737 Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000738 u_char **sigp, int *lenp,
739 u_char *data, int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100740{
741 switch(key->type){
742 case KEY_DSA:
743 return ssh_dss_sign(key, sigp, lenp, data, datalen);
744 break;
745 case KEY_RSA:
746 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
747 break;
748 default:
749 error("key_sign: illegal key type %d", key->type);
750 return -1;
751 break;
752 }
753}
754
755int
756key_verify(
757 Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000758 u_char *signature, int signaturelen,
759 u_char *data, int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100760{
761 switch(key->type){
762 case KEY_DSA:
763 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
764 break;
765 case KEY_RSA:
766 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
767 break;
768 default:
769 error("key_verify: illegal key type %d", key->type);
770 return -1;
771 break;
772 }
773}