blob: 0b03e991454677446316556c38355e177ae102f1 [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"
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000035RCSID("$OpenBSD: key.c,v 1.48 2002/07/04 10:41:47 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}
Ben Lindstrom836f0e92002-06-23 21:21:30 +000092
Damien Miller0bc1bd82000-11-13 22:57:25 +110093Key *
94key_new_private(int type)
95{
96 Key *k = key_new(type);
97 switch (k->type) {
98 case KEY_RSA1:
99 case KEY_RSA:
Damien Millerda755162002-01-22 23:09:22 +1100100 if ((k->rsa->d = BN_new()) == NULL)
101 fatal("key_new_private: BN_new failed");
102 if ((k->rsa->iqmp = BN_new()) == NULL)
103 fatal("key_new_private: BN_new failed");
104 if ((k->rsa->q = BN_new()) == NULL)
105 fatal("key_new_private: BN_new failed");
106 if ((k->rsa->p = BN_new()) == NULL)
107 fatal("key_new_private: BN_new failed");
108 if ((k->rsa->dmq1 = BN_new()) == NULL)
109 fatal("key_new_private: BN_new failed");
110 if ((k->rsa->dmp1 = BN_new()) == NULL)
111 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100112 break;
113 case KEY_DSA:
Damien Millerda755162002-01-22 23:09:22 +1100114 if ((k->dsa->priv_key = BN_new()) == NULL)
115 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100116 break;
117 case KEY_UNSPEC:
118 break;
119 default:
120 break;
121 }
122 return k;
123}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000124
Damien Miller450a7a12000-03-26 13:04:51 +1000125void
126key_free(Key *k)
127{
128 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100129 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000130 case KEY_RSA:
131 if (k->rsa != NULL)
132 RSA_free(k->rsa);
133 k->rsa = NULL;
134 break;
135 case KEY_DSA:
136 if (k->dsa != NULL)
137 DSA_free(k->dsa);
138 k->dsa = NULL;
139 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100140 case KEY_UNSPEC:
141 break;
Damien Miller450a7a12000-03-26 13:04:51 +1000142 default:
143 fatal("key_free: bad key type %d", k->type);
144 break;
145 }
146 xfree(k);
147}
148int
149key_equal(Key *a, Key *b)
150{
151 if (a == NULL || b == NULL || a->type != b->type)
152 return 0;
153 switch (a->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100154 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000155 case KEY_RSA:
156 return a->rsa != NULL && b->rsa != NULL &&
157 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
158 BN_cmp(a->rsa->n, b->rsa->n) == 0;
159 break;
160 case KEY_DSA:
161 return a->dsa != NULL && b->dsa != NULL &&
162 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
163 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
164 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
165 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
166 break;
167 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000168 fatal("key_equal: bad key type %d", a->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000169 break;
170 }
171 return 0;
172}
173
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000174static u_char *
Damien Miller3672e4b2002-02-05 11:54:07 +1100175key_fingerprint_raw(Key *k, enum fp_type dgst_type, u_int *dgst_raw_length)
Damien Miller450a7a12000-03-26 13:04:51 +1000176{
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000177 const EVP_MD *md = NULL;
Ben Lindstromf0b48532001-03-12 02:59:31 +0000178 EVP_MD_CTX ctx;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000179 u_char *blob = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000180 u_char *retval = NULL;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000181 u_int len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000182 int nlen, elen;
Damien Miller450a7a12000-03-26 13:04:51 +1000183
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000184 *dgst_raw_length = 0;
185
Ben Lindstromf0b48532001-03-12 02:59:31 +0000186 switch (dgst_type) {
187 case SSH_FP_MD5:
188 md = EVP_md5();
189 break;
190 case SSH_FP_SHA1:
191 md = EVP_sha1();
192 break;
193 default:
194 fatal("key_fingerprint_raw: bad digest type %d",
195 dgst_type);
196 }
Damien Miller450a7a12000-03-26 13:04:51 +1000197 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100198 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000199 nlen = BN_num_bytes(k->rsa->n);
200 elen = BN_num_bytes(k->rsa->e);
201 len = nlen + elen;
Damien Millereba71ba2000-04-29 23:57:08 +1000202 blob = xmalloc(len);
203 BN_bn2bin(k->rsa->n, blob);
204 BN_bn2bin(k->rsa->e, blob + nlen);
Damien Miller450a7a12000-03-26 13:04:51 +1000205 break;
206 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100207 case KEY_RSA:
208 key_to_blob(k, &blob, &len);
209 break;
210 case KEY_UNSPEC:
211 return retval;
Damien Miller450a7a12000-03-26 13:04:51 +1000212 break;
213 default:
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000214 fatal("key_fingerprint_raw: bad key type %d", k->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000215 break;
216 }
Damien Millereba71ba2000-04-29 23:57:08 +1000217 if (blob != NULL) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000218 retval = xmalloc(EVP_MAX_MD_SIZE);
Damien Miller6536c7d2000-06-22 21:32:31 +1000219 EVP_DigestInit(&ctx, md);
220 EVP_DigestUpdate(&ctx, blob, len);
Damien Miller3672e4b2002-02-05 11:54:07 +1100221 EVP_DigestFinal(&ctx, retval, dgst_raw_length);
Damien Millereba71ba2000-04-29 23:57:08 +1000222 memset(blob, 0, len);
223 xfree(blob);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000224 } else {
225 fatal("key_fingerprint_raw: blob is null");
Damien Miller450a7a12000-03-26 13:04:51 +1000226 }
227 return retval;
228}
229
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000230static char *
231key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000232{
233 char *retval;
234 int i;
235
Ben Lindstrom459d1912001-04-17 18:06:14 +0000236 retval = xmalloc(dgst_raw_len * 3 + 1);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000237 retval[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100238 for (i = 0; i < dgst_raw_len; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000239 char hex[4];
240 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
241 strlcat(retval, hex, dgst_raw_len * 3);
242 }
243 retval[(dgst_raw_len * 3) - 1] = '\0';
244 return retval;
245}
246
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000247static char *
248key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000249{
250 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
251 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
252 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000253 u_int i, j = 0, rounds, seed = 1;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000254 char *retval;
255
256 rounds = (dgst_raw_len / 2) + 1;
257 retval = xmalloc(sizeof(char) * (rounds*6));
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000258 retval[j++] = 'x';
259 for (i = 0; i < rounds; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000260 u_int idx0, idx1, idx2, idx3, idx4;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000261 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
262 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000263 seed) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000264 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
265 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000266 (seed / 6)) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000267 retval[j++] = vowels[idx0];
268 retval[j++] = consonants[idx1];
269 retval[j++] = vowels[idx2];
270 if ((i + 1) < rounds) {
271 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
272 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
273 retval[j++] = consonants[idx3];
274 retval[j++] = '-';
275 retval[j++] = consonants[idx4];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000276 seed = ((seed * 5) +
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000277 ((((u_int)(dgst_raw[2 * i])) * 7) +
278 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000279 }
280 } else {
281 idx0 = seed % 6;
282 idx1 = 16;
283 idx2 = seed / 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000284 retval[j++] = vowels[idx0];
285 retval[j++] = consonants[idx1];
286 retval[j++] = vowels[idx2];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000287 }
288 }
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000289 retval[j++] = 'x';
290 retval[j++] = '\0';
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000291 return retval;
292}
293
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000294char *
Ben Lindstromcfccef92001-03-13 04:57:58 +0000295key_fingerprint(Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000296{
Ben Lindstroma3700052001-04-05 23:26:32 +0000297 char *retval = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000298 u_char *dgst_raw;
Damien Miller3672e4b2002-02-05 11:54:07 +1100299 u_int dgst_raw_len;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100300
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000301 dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
302 if (!dgst_raw)
Ben Lindstromcfccef92001-03-13 04:57:58 +0000303 fatal("key_fingerprint: null from key_fingerprint_raw()");
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000304 switch (dgst_rep) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000305 case SSH_FP_HEX:
306 retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
307 break;
308 case SSH_FP_BUBBLEBABBLE:
309 retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
310 break;
311 default:
312 fatal("key_fingerprint_ex: bad digest representation %d",
313 dgst_rep);
314 break;
315 }
316 memset(dgst_raw, 0, dgst_raw_len);
317 xfree(dgst_raw);
318 return retval;
319}
320
Damien Miller450a7a12000-03-26 13:04:51 +1000321/*
322 * Reads a multiple-precision integer in decimal from the buffer, and advances
323 * the pointer. The integer must already be initialized. This function is
324 * permitted to modify the buffer. This leaves *cpp to point just beyond the
325 * last processed (and maybe modified) character. Note that this may modify
326 * the buffer containing the number.
327 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000328static int
Damien Miller450a7a12000-03-26 13:04:51 +1000329read_bignum(char **cpp, BIGNUM * value)
330{
331 char *cp = *cpp;
332 int old;
333
334 /* Skip any leading whitespace. */
335 for (; *cp == ' ' || *cp == '\t'; cp++)
336 ;
337
338 /* Check that it begins with a decimal digit. */
339 if (*cp < '0' || *cp > '9')
340 return 0;
341
342 /* Save starting position. */
343 *cpp = cp;
344
345 /* Move forward until all decimal digits skipped. */
346 for (; *cp >= '0' && *cp <= '9'; cp++)
347 ;
348
349 /* Save the old terminating character, and replace it by \0. */
350 old = *cp;
351 *cp = 0;
352
353 /* Parse the number. */
354 if (BN_dec2bn(&value, *cpp) == 0)
355 return 0;
356
357 /* Restore old terminating character. */
358 *cp = old;
359
360 /* Move beyond the number and return success. */
361 *cpp = cp;
362 return 1;
363}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000364
Ben Lindstrombba81212001-06-25 05:01:22 +0000365static int
Damien Miller450a7a12000-03-26 13:04:51 +1000366write_bignum(FILE *f, BIGNUM *num)
367{
368 char *buf = BN_bn2dec(num);
369 if (buf == NULL) {
370 error("write_bignum: BN_bn2dec() failed");
371 return 0;
372 }
373 fprintf(f, " %s", buf);
Damien Milleraf3030f2001-10-10 15:00:49 +1000374 OPENSSL_free(buf);
Damien Miller450a7a12000-03-26 13:04:51 +1000375 return 1;
376}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100377
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000378/* returns 1 ok, -1 error */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100379int
Damien Millereba71ba2000-04-29 23:57:08 +1000380key_read(Key *ret, char **cpp)
Damien Miller450a7a12000-03-26 13:04:51 +1000381{
Damien Millereba71ba2000-04-29 23:57:08 +1000382 Key *k;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100383 int success = -1;
384 char *cp, *space;
385 int len, n, type;
386 u_int bits;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000387 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000388
389 cp = *cpp;
390
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000391 switch (ret->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100392 case KEY_RSA1:
Damien Millereba71ba2000-04-29 23:57:08 +1000393 /* Get number of bits. */
394 if (*cp < '0' || *cp > '9')
Damien Miller0bc1bd82000-11-13 22:57:25 +1100395 return -1; /* Bad bit count... */
Damien Millereba71ba2000-04-29 23:57:08 +1000396 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
397 bits = 10 * bits + *cp - '0';
Damien Miller450a7a12000-03-26 13:04:51 +1000398 if (bits == 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100399 return -1;
Damien Millereba71ba2000-04-29 23:57:08 +1000400 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000401 /* Get public exponent, public modulus. */
402 if (!read_bignum(cpp, ret->rsa->e))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100403 return -1;
Damien Miller450a7a12000-03-26 13:04:51 +1000404 if (!read_bignum(cpp, ret->rsa->n))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100405 return -1;
406 success = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000407 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100408 case KEY_UNSPEC:
409 case KEY_RSA:
Damien Miller450a7a12000-03-26 13:04:51 +1000410 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100411 space = strchr(cp, ' ');
412 if (space == NULL) {
413 debug3("key_read: no space");
414 return -1;
415 }
416 *space = '\0';
417 type = key_type_from_name(cp);
418 *space = ' ';
419 if (type == KEY_UNSPEC) {
420 debug3("key_read: no key found");
421 return -1;
422 }
423 cp = space+1;
424 if (*cp == '\0') {
425 debug3("key_read: short string");
426 return -1;
427 }
428 if (ret->type == KEY_UNSPEC) {
429 ret->type = type;
430 } else if (ret->type != type) {
431 /* is a key, but different type */
432 debug3("key_read: type mismatch");
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000433 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100434 }
Damien Millereba71ba2000-04-29 23:57:08 +1000435 len = 2*strlen(cp);
436 blob = xmalloc(len);
437 n = uudecode(cp, blob, len);
Damien Millere247cc42000-05-07 12:03:14 +1000438 if (n < 0) {
Damien Millerb1715dc2000-05-30 13:44:51 +1000439 error("key_read: uudecode %s failed", cp);
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000440 xfree(blob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100441 return -1;
Damien Millere247cc42000-05-07 12:03:14 +1000442 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100443 k = key_from_blob(blob, n);
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000444 xfree(blob);
Damien Millerb1715dc2000-05-30 13:44:51 +1000445 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100446 error("key_read: key_from_blob %s failed", cp);
447 return -1;
Damien Millerb1715dc2000-05-30 13:44:51 +1000448 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100449 if (k->type != type) {
450 error("key_read: type mismatch: encoding error");
451 key_free(k);
452 return -1;
453 }
454/*XXXX*/
455 if (ret->type == KEY_RSA) {
456 if (ret->rsa != NULL)
457 RSA_free(ret->rsa);
458 ret->rsa = k->rsa;
459 k->rsa = NULL;
460 success = 1;
461#ifdef DEBUG_PK
462 RSA_print_fp(stderr, ret->rsa, 8);
463#endif
464 } else {
465 if (ret->dsa != NULL)
466 DSA_free(ret->dsa);
467 ret->dsa = k->dsa;
468 k->dsa = NULL;
469 success = 1;
470#ifdef DEBUG_PK
471 DSA_print_fp(stderr, ret->dsa, 8);
472#endif
473 }
474/*XXXX*/
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000475 key_free(k);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100476 if (success != 1)
477 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000478 /* advance cp: skip whitespace and data */
479 while (*cp == ' ' || *cp == '\t')
480 cp++;
481 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
482 cp++;
483 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000484 break;
485 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000486 fatal("key_read: bad key type: %d", ret->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000487 break;
488 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100489 return success;
Damien Miller450a7a12000-03-26 13:04:51 +1000490}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000491
Damien Miller450a7a12000-03-26 13:04:51 +1000492int
493key_write(Key *key, FILE *f)
494{
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000495 int n, success = 0;
496 u_int len, bits = 0;
497 u_char *blob, *uu;
Damien Miller450a7a12000-03-26 13:04:51 +1000498
Damien Miller0bc1bd82000-11-13 22:57:25 +1100499 if (key->type == KEY_RSA1 && key->rsa != NULL) {
Damien Miller450a7a12000-03-26 13:04:51 +1000500 /* size of modulus 'n' */
501 bits = BN_num_bits(key->rsa->n);
502 fprintf(f, "%u", bits);
503 if (write_bignum(f, key->rsa->e) &&
504 write_bignum(f, key->rsa->n)) {
505 success = 1;
506 } else {
507 error("key_write: failed for RSA key");
508 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100509 } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
510 (key->type == KEY_RSA && key->rsa != NULL)) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100511 key_to_blob(key, &blob, &len);
Damien Millereba71ba2000-04-29 23:57:08 +1000512 uu = xmalloc(2*len);
Damien Millere247cc42000-05-07 12:03:14 +1000513 n = uuencode(blob, len, uu, 2*len);
514 if (n > 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100515 fprintf(f, "%s %s", key_ssh_name(key), uu);
Damien Millere247cc42000-05-07 12:03:14 +1000516 success = 1;
517 }
Damien Millereba71ba2000-04-29 23:57:08 +1000518 xfree(blob);
519 xfree(uu);
Damien Miller450a7a12000-03-26 13:04:51 +1000520 }
521 return success;
522}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000523
Damien Millere247cc42000-05-07 12:03:14 +1000524char *
525key_type(Key *k)
526{
527 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100528 case KEY_RSA1:
529 return "RSA1";
530 break;
Damien Millere247cc42000-05-07 12:03:14 +1000531 case KEY_RSA:
532 return "RSA";
533 break;
534 case KEY_DSA:
535 return "DSA";
536 break;
537 }
538 return "unknown";
539}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000540
Damien Miller0bc1bd82000-11-13 22:57:25 +1100541char *
542key_ssh_name(Key *k)
543{
544 switch (k->type) {
545 case KEY_RSA:
546 return "ssh-rsa";
547 break;
548 case KEY_DSA:
549 return "ssh-dss";
550 break;
551 }
552 return "ssh-unknown";
553}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000554
Damien Miller0bc1bd82000-11-13 22:57:25 +1100555u_int
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000556key_size(Key *k)
557{
Damien Millerad833b32000-08-23 10:46:23 +1000558 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100559 case KEY_RSA1:
Damien Millerad833b32000-08-23 10:46:23 +1000560 case KEY_RSA:
561 return BN_num_bits(k->rsa->n);
562 break;
563 case KEY_DSA:
564 return BN_num_bits(k->dsa->p);
565 break;
566 }
567 return 0;
568}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100569
Ben Lindstrombba81212001-06-25 05:01:22 +0000570static RSA *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000571rsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100572{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000573 RSA *private;
574 private = RSA_generate_key(bits, 35, NULL, NULL);
575 if (private == NULL)
576 fatal("rsa_generate_private_key: key generation failed.");
577 return private;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100578}
579
Ben Lindstrombba81212001-06-25 05:01:22 +0000580static DSA*
Ben Lindstrom46c16222000-12-22 01:43:59 +0000581dsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100582{
583 DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
584 if (private == NULL)
585 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
586 if (!DSA_generate_key(private))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000587 fatal("dsa_generate_private_key: DSA_generate_key failed.");
588 if (private == NULL)
589 fatal("dsa_generate_private_key: NULL.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100590 return private;
591}
592
593Key *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000594key_generate(int type, u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100595{
596 Key *k = key_new(KEY_UNSPEC);
597 switch (type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000598 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100599 k->dsa = dsa_generate_private_key(bits);
600 break;
601 case KEY_RSA:
602 case KEY_RSA1:
603 k->rsa = rsa_generate_private_key(bits);
604 break;
605 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000606 fatal("key_generate: unknown type %d", type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100607 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000608 k->type = type;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100609 return k;
610}
611
612Key *
613key_from_private(Key *k)
614{
615 Key *n = NULL;
616 switch (k->type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000617 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100618 n = key_new(k->type);
619 BN_copy(n->dsa->p, k->dsa->p);
620 BN_copy(n->dsa->q, k->dsa->q);
621 BN_copy(n->dsa->g, k->dsa->g);
622 BN_copy(n->dsa->pub_key, k->dsa->pub_key);
623 break;
624 case KEY_RSA:
625 case KEY_RSA1:
626 n = key_new(k->type);
627 BN_copy(n->rsa->n, k->rsa->n);
628 BN_copy(n->rsa->e, k->rsa->e);
629 break;
630 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000631 fatal("key_from_private: unknown type %d", k->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100632 break;
633 }
634 return n;
635}
636
637int
638key_type_from_name(char *name)
639{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000640 if (strcmp(name, "rsa1") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100641 return KEY_RSA1;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000642 } else if (strcmp(name, "rsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100643 return KEY_RSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000644 } else if (strcmp(name, "dsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100645 return KEY_DSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000646 } else if (strcmp(name, "ssh-rsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100647 return KEY_RSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000648 } else if (strcmp(name, "ssh-dss") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100649 return KEY_DSA;
650 }
Ben Lindstromb54873a2001-03-11 20:01:55 +0000651 debug2("key_type_from_name: unknown key type '%s'", name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100652 return KEY_UNSPEC;
653}
654
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000655int
656key_names_valid2(const char *names)
657{
658 char *s, *cp, *p;
659
660 if (names == NULL || strcmp(names, "") == 0)
661 return 0;
662 s = cp = xstrdup(names);
663 for ((p = strsep(&cp, ",")); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100664 (p = strsep(&cp, ","))) {
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000665 switch (key_type_from_name(p)) {
666 case KEY_RSA1:
667 case KEY_UNSPEC:
668 xfree(s);
669 return 0;
670 }
671 }
672 debug3("key names ok: [%s]", names);
673 xfree(s);
674 return 1;
675}
676
Damien Miller0bc1bd82000-11-13 22:57:25 +1100677Key *
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000678key_from_blob(u_char *blob, int blen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100679{
680 Buffer b;
681 char *ktype;
682 int rlen, type;
683 Key *key = NULL;
684
685#ifdef DEBUG_PK
686 dump_base64(stderr, blob, blen);
687#endif
688 buffer_init(&b);
689 buffer_append(&b, blob, blen);
690 ktype = buffer_get_string(&b, NULL);
691 type = key_type_from_name(ktype);
692
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000693 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100694 case KEY_RSA:
695 key = key_new(type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100696 buffer_get_bignum2(&b, key->rsa->e);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000697 buffer_get_bignum2(&b, key->rsa->n);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100698#ifdef DEBUG_PK
699 RSA_print_fp(stderr, key->rsa, 8);
700#endif
701 break;
702 case KEY_DSA:
703 key = key_new(type);
704 buffer_get_bignum2(&b, key->dsa->p);
705 buffer_get_bignum2(&b, key->dsa->q);
706 buffer_get_bignum2(&b, key->dsa->g);
707 buffer_get_bignum2(&b, key->dsa->pub_key);
708#ifdef DEBUG_PK
709 DSA_print_fp(stderr, key->dsa, 8);
710#endif
711 break;
712 case KEY_UNSPEC:
713 key = key_new(type);
714 break;
715 default:
716 error("key_from_blob: cannot handle type %s", ktype);
717 break;
718 }
719 rlen = buffer_len(&b);
720 if (key != NULL && rlen != 0)
721 error("key_from_blob: remaining bytes in key blob %d", rlen);
722 xfree(ktype);
723 buffer_free(&b);
724 return key;
725}
726
727int
Ben Lindstrom46c16222000-12-22 01:43:59 +0000728key_to_blob(Key *key, u_char **blobp, u_int *lenp)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100729{
730 Buffer b;
731 int len;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100732
733 if (key == NULL) {
734 error("key_to_blob: key == NULL");
735 return 0;
736 }
737 buffer_init(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000738 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100739 case KEY_DSA:
740 buffer_put_cstring(&b, key_ssh_name(key));
741 buffer_put_bignum2(&b, key->dsa->p);
742 buffer_put_bignum2(&b, key->dsa->q);
743 buffer_put_bignum2(&b, key->dsa->g);
744 buffer_put_bignum2(&b, key->dsa->pub_key);
745 break;
746 case KEY_RSA:
747 buffer_put_cstring(&b, key_ssh_name(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100748 buffer_put_bignum2(&b, key->rsa->e);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000749 buffer_put_bignum2(&b, key->rsa->n);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100750 break;
751 default:
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000752 error("key_to_blob: unsupported key type %d", key->type);
753 buffer_free(&b);
754 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100755 }
756 len = buffer_len(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100757 if (lenp != NULL)
758 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000759 if (blobp != NULL) {
760 *blobp = xmalloc(len);
761 memcpy(*blobp, buffer_ptr(&b), len);
762 }
763 memset(buffer_ptr(&b), 0, len);
764 buffer_free(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100765 return len;
766}
767
768int
769key_sign(
770 Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000771 u_char **sigp, u_int *lenp,
772 u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100773{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000774 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100775 case KEY_DSA:
776 return ssh_dss_sign(key, sigp, lenp, data, datalen);
777 break;
778 case KEY_RSA:
779 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
780 break;
781 default:
782 error("key_sign: illegal key type %d", key->type);
783 return -1;
784 break;
785 }
786}
787
Ben Lindstrom01fff0c2002-06-06 20:54:07 +0000788/*
789 * key_verify returns 1 for a correct signature, 0 for an incorrect signature
790 * and -1 on error.
791 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100792int
793key_verify(
794 Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000795 u_char *signature, u_int signaturelen,
796 u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100797{
Ben Lindstrom5363aee2001-06-25 04:42:20 +0000798 if (signaturelen == 0)
799 return -1;
800
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000801 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100802 case KEY_DSA:
803 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
804 break;
805 case KEY_RSA:
806 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
807 break;
808 default:
809 error("key_verify: illegal key type %d", key->type);
810 return -1;
811 break;
812 }
813}
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000814
815/* Converts a private to a public key */
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000816Key *
817key_demote(Key *k)
818{
819 Key *pk;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000820
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000821 pk = xmalloc(sizeof(*pk));
822 pk->type = k->type;
823 pk->flags = k->flags;
824 pk->dsa = NULL;
825 pk->rsa = NULL;
826
827 switch (k->type) {
828 case KEY_RSA1:
829 case KEY_RSA:
830 if ((pk->rsa = RSA_new()) == NULL)
831 fatal("key_demote: RSA_new failed");
832 if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
833 fatal("key_demote: BN_dup failed");
834 if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
835 fatal("key_demote: BN_dup failed");
836 break;
837 case KEY_DSA:
838 if ((pk->dsa = DSA_new()) == NULL)
839 fatal("key_demote: DSA_new failed");
840 if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
841 fatal("key_demote: BN_dup failed");
842 if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
843 fatal("key_demote: BN_dup failed");
844 if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
845 fatal("key_demote: BN_dup failed");
846 if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
847 fatal("key_demote: BN_dup failed");
848 break;
849 default:
850 fatal("key_free: bad key type %d", k->type);
851 break;
852 }
853
854 return (pk);
855}