blob: 27910ff0efcf58a2076ad11ddce200dd9e095828 [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"
Kevin Steves2b725a02001-02-05 18:16:28 +000035RCSID("$OpenBSD: key.c,v 1.17 2001/02/04 15:32:24 stevesk 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
Damien Miller450a7a12000-03-26 13:04:51 +1000156/*
157 * Generate key fingerprint in ascii format.
158 * Based on ideas and code from Bjoern Groenvall <bg@sics.se>
159 */
160char *
161key_fingerprint(Key *k)
162{
Damien Miller6536c7d2000-06-22 21:32:31 +1000163 static char retval[(EVP_MAX_MD_SIZE+1)*3];
Ben Lindstrom46c16222000-12-22 01:43:59 +0000164 u_char *blob = NULL;
Damien Miller450a7a12000-03-26 13:04:51 +1000165 int len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000166 int nlen, elen;
Damien Miller450a7a12000-03-26 13:04:51 +1000167
Damien Miller0bc1bd82000-11-13 22:57:25 +1100168 retval[0] = '\0';
Damien Miller450a7a12000-03-26 13:04:51 +1000169 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100170 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000171 nlen = BN_num_bytes(k->rsa->n);
172 elen = BN_num_bytes(k->rsa->e);
173 len = nlen + elen;
Damien Millereba71ba2000-04-29 23:57:08 +1000174 blob = xmalloc(len);
175 BN_bn2bin(k->rsa->n, blob);
176 BN_bn2bin(k->rsa->e, blob + nlen);
Damien Miller450a7a12000-03-26 13:04:51 +1000177 break;
178 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100179 case KEY_RSA:
180 key_to_blob(k, &blob, &len);
181 break;
182 case KEY_UNSPEC:
183 return retval;
Damien Miller450a7a12000-03-26 13:04:51 +1000184 break;
185 default:
186 fatal("key_fingerprint: bad key type %d", k->type);
187 break;
188 }
Damien Millereba71ba2000-04-29 23:57:08 +1000189 if (blob != NULL) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000190 int i;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000191 u_char digest[EVP_MAX_MD_SIZE];
Damien Miller6536c7d2000-06-22 21:32:31 +1000192 EVP_MD *md = EVP_md5();
193 EVP_MD_CTX ctx;
194 EVP_DigestInit(&ctx, md);
195 EVP_DigestUpdate(&ctx, blob, len);
196 EVP_DigestFinal(&ctx, digest, NULL);
197 for(i = 0; i < md->md_size; i++) {
198 char hex[4];
199 snprintf(hex, sizeof(hex), "%02x:", digest[i]);
200 strlcat(retval, hex, sizeof(retval));
201 }
202 retval[strlen(retval) - 1] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000203 memset(blob, 0, len);
204 xfree(blob);
Damien Miller450a7a12000-03-26 13:04:51 +1000205 }
206 return retval;
207}
208
209/*
210 * Reads a multiple-precision integer in decimal from the buffer, and advances
211 * the pointer. The integer must already be initialized. This function is
212 * permitted to modify the buffer. This leaves *cpp to point just beyond the
213 * last processed (and maybe modified) character. Note that this may modify
214 * the buffer containing the number.
215 */
216int
217read_bignum(char **cpp, BIGNUM * value)
218{
219 char *cp = *cpp;
220 int old;
221
222 /* Skip any leading whitespace. */
223 for (; *cp == ' ' || *cp == '\t'; cp++)
224 ;
225
226 /* Check that it begins with a decimal digit. */
227 if (*cp < '0' || *cp > '9')
228 return 0;
229
230 /* Save starting position. */
231 *cpp = cp;
232
233 /* Move forward until all decimal digits skipped. */
234 for (; *cp >= '0' && *cp <= '9'; cp++)
235 ;
236
237 /* Save the old terminating character, and replace it by \0. */
238 old = *cp;
239 *cp = 0;
240
241 /* Parse the number. */
242 if (BN_dec2bn(&value, *cpp) == 0)
243 return 0;
244
245 /* Restore old terminating character. */
246 *cp = old;
247
248 /* Move beyond the number and return success. */
249 *cpp = cp;
250 return 1;
251}
252int
253write_bignum(FILE *f, BIGNUM *num)
254{
255 char *buf = BN_bn2dec(num);
256 if (buf == NULL) {
257 error("write_bignum: BN_bn2dec() failed");
258 return 0;
259 }
260 fprintf(f, " %s", buf);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000261 xfree(buf);
Damien Miller450a7a12000-03-26 13:04:51 +1000262 return 1;
263}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100264
265/* returns 1 ok, -1 error, 0 type mismatch */
266int
Damien Millereba71ba2000-04-29 23:57:08 +1000267key_read(Key *ret, char **cpp)
Damien Miller450a7a12000-03-26 13:04:51 +1000268{
Damien Millereba71ba2000-04-29 23:57:08 +1000269 Key *k;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100270 int success = -1;
271 char *cp, *space;
272 int len, n, type;
273 u_int bits;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000274 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000275
276 cp = *cpp;
277
Damien Miller450a7a12000-03-26 13:04:51 +1000278 switch(ret->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100279 case KEY_RSA1:
Damien Millereba71ba2000-04-29 23:57:08 +1000280 /* Get number of bits. */
281 if (*cp < '0' || *cp > '9')
Damien Miller0bc1bd82000-11-13 22:57:25 +1100282 return -1; /* Bad bit count... */
Damien Millereba71ba2000-04-29 23:57:08 +1000283 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
284 bits = 10 * bits + *cp - '0';
Damien Miller450a7a12000-03-26 13:04:51 +1000285 if (bits == 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100286 return -1;
Damien Millereba71ba2000-04-29 23:57:08 +1000287 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000288 /* Get public exponent, public modulus. */
289 if (!read_bignum(cpp, ret->rsa->e))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100290 return -1;
Damien Miller450a7a12000-03-26 13:04:51 +1000291 if (!read_bignum(cpp, ret->rsa->n))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100292 return -1;
293 success = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000294 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100295 case KEY_UNSPEC:
296 case KEY_RSA:
Damien Miller450a7a12000-03-26 13:04:51 +1000297 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100298 space = strchr(cp, ' ');
299 if (space == NULL) {
300 debug3("key_read: no space");
301 return -1;
302 }
303 *space = '\0';
304 type = key_type_from_name(cp);
305 *space = ' ';
306 if (type == KEY_UNSPEC) {
307 debug3("key_read: no key found");
308 return -1;
309 }
310 cp = space+1;
311 if (*cp == '\0') {
312 debug3("key_read: short string");
313 return -1;
314 }
315 if (ret->type == KEY_UNSPEC) {
316 ret->type = type;
317 } else if (ret->type != type) {
318 /* is a key, but different type */
319 debug3("key_read: type mismatch");
Damien Miller450a7a12000-03-26 13:04:51 +1000320 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100321 }
Damien Millereba71ba2000-04-29 23:57:08 +1000322 len = 2*strlen(cp);
323 blob = xmalloc(len);
324 n = uudecode(cp, blob, len);
Damien Millere247cc42000-05-07 12:03:14 +1000325 if (n < 0) {
Damien Millerb1715dc2000-05-30 13:44:51 +1000326 error("key_read: uudecode %s failed", cp);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100327 return -1;
Damien Millere247cc42000-05-07 12:03:14 +1000328 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100329 k = key_from_blob(blob, n);
Damien Millerb1715dc2000-05-30 13:44:51 +1000330 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100331 error("key_read: key_from_blob %s failed", cp);
332 return -1;
Damien Millerb1715dc2000-05-30 13:44:51 +1000333 }
Damien Millereba71ba2000-04-29 23:57:08 +1000334 xfree(blob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100335 if (k->type != type) {
336 error("key_read: type mismatch: encoding error");
337 key_free(k);
338 return -1;
339 }
340/*XXXX*/
341 if (ret->type == KEY_RSA) {
342 if (ret->rsa != NULL)
343 RSA_free(ret->rsa);
344 ret->rsa = k->rsa;
345 k->rsa = NULL;
346 success = 1;
347#ifdef DEBUG_PK
348 RSA_print_fp(stderr, ret->rsa, 8);
349#endif
350 } else {
351 if (ret->dsa != NULL)
352 DSA_free(ret->dsa);
353 ret->dsa = k->dsa;
354 k->dsa = NULL;
355 success = 1;
356#ifdef DEBUG_PK
357 DSA_print_fp(stderr, ret->dsa, 8);
358#endif
359 }
360/*XXXX*/
361 if (success != 1)
362 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000363 key_free(k);
Damien Millerb1715dc2000-05-30 13:44:51 +1000364 /* advance cp: skip whitespace and data */
365 while (*cp == ' ' || *cp == '\t')
366 cp++;
367 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
368 cp++;
369 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000370 break;
371 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000372 fatal("key_read: bad key type: %d", ret->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000373 break;
374 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100375 return success;
Damien Miller450a7a12000-03-26 13:04:51 +1000376}
377int
378key_write(Key *key, FILE *f)
379{
380 int success = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000381 u_int bits = 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000382
Damien Miller0bc1bd82000-11-13 22:57:25 +1100383 if (key->type == KEY_RSA1 && key->rsa != NULL) {
Damien Miller450a7a12000-03-26 13:04:51 +1000384 /* size of modulus 'n' */
385 bits = BN_num_bits(key->rsa->n);
386 fprintf(f, "%u", bits);
387 if (write_bignum(f, key->rsa->e) &&
388 write_bignum(f, key->rsa->n)) {
389 success = 1;
390 } else {
391 error("key_write: failed for RSA key");
392 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100393 } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
394 (key->type == KEY_RSA && key->rsa != NULL)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000395 int len, n;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000396 u_char *blob, *uu;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100397 key_to_blob(key, &blob, &len);
Damien Millereba71ba2000-04-29 23:57:08 +1000398 uu = xmalloc(2*len);
Damien Millere247cc42000-05-07 12:03:14 +1000399 n = uuencode(blob, len, uu, 2*len);
400 if (n > 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100401 fprintf(f, "%s %s", key_ssh_name(key), uu);
Damien Millere247cc42000-05-07 12:03:14 +1000402 success = 1;
403 }
Damien Millereba71ba2000-04-29 23:57:08 +1000404 xfree(blob);
405 xfree(uu);
Damien Miller450a7a12000-03-26 13:04:51 +1000406 }
407 return success;
408}
Damien Millere247cc42000-05-07 12:03:14 +1000409char *
410key_type(Key *k)
411{
412 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100413 case KEY_RSA1:
414 return "RSA1";
415 break;
Damien Millere247cc42000-05-07 12:03:14 +1000416 case KEY_RSA:
417 return "RSA";
418 break;
419 case KEY_DSA:
420 return "DSA";
421 break;
422 }
423 return "unknown";
424}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100425char *
426key_ssh_name(Key *k)
427{
428 switch (k->type) {
429 case KEY_RSA:
430 return "ssh-rsa";
431 break;
432 case KEY_DSA:
433 return "ssh-dss";
434 break;
435 }
436 return "ssh-unknown";
437}
438u_int
Damien Millerad833b32000-08-23 10:46:23 +1000439key_size(Key *k){
440 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100441 case KEY_RSA1:
Damien Millerad833b32000-08-23 10:46:23 +1000442 case KEY_RSA:
443 return BN_num_bits(k->rsa->n);
444 break;
445 case KEY_DSA:
446 return BN_num_bits(k->dsa->p);
447 break;
448 }
449 return 0;
450}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100451
452RSA *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000453rsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100454{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000455 RSA *private;
456 private = RSA_generate_key(bits, 35, NULL, NULL);
457 if (private == NULL)
458 fatal("rsa_generate_private_key: key generation failed.");
459 return private;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100460}
461
462DSA*
Ben Lindstrom46c16222000-12-22 01:43:59 +0000463dsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100464{
465 DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
466 if (private == NULL)
467 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
468 if (!DSA_generate_key(private))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000469 fatal("dsa_generate_private_key: DSA_generate_key failed.");
470 if (private == NULL)
471 fatal("dsa_generate_private_key: NULL.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100472 return private;
473}
474
475Key *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000476key_generate(int type, u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100477{
478 Key *k = key_new(KEY_UNSPEC);
479 switch (type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000480 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100481 k->dsa = dsa_generate_private_key(bits);
482 break;
483 case KEY_RSA:
484 case KEY_RSA1:
485 k->rsa = rsa_generate_private_key(bits);
486 break;
487 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000488 fatal("key_generate: unknown type %d", type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100489 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000490 k->type = type;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100491 return k;
492}
493
494Key *
495key_from_private(Key *k)
496{
497 Key *n = NULL;
498 switch (k->type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000499 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100500 n = key_new(k->type);
501 BN_copy(n->dsa->p, k->dsa->p);
502 BN_copy(n->dsa->q, k->dsa->q);
503 BN_copy(n->dsa->g, k->dsa->g);
504 BN_copy(n->dsa->pub_key, k->dsa->pub_key);
505 break;
506 case KEY_RSA:
507 case KEY_RSA1:
508 n = key_new(k->type);
509 BN_copy(n->rsa->n, k->rsa->n);
510 BN_copy(n->rsa->e, k->rsa->e);
511 break;
512 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000513 fatal("key_from_private: unknown type %d", k->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100514 break;
515 }
516 return n;
517}
518
519int
520key_type_from_name(char *name)
521{
522 if (strcmp(name, "rsa1") == 0){
523 return KEY_RSA1;
524 } else if (strcmp(name, "rsa") == 0){
525 return KEY_RSA;
526 } else if (strcmp(name, "dsa") == 0){
527 return KEY_DSA;
528 } else if (strcmp(name, "ssh-rsa") == 0){
529 return KEY_RSA;
530 } else if (strcmp(name, "ssh-dss") == 0){
531 return KEY_DSA;
532 }
533 debug("key_type_from_name: unknown key type '%s'", name);
534 return KEY_UNSPEC;
535}
536
537Key *
538key_from_blob(char *blob, int blen)
539{
540 Buffer b;
541 char *ktype;
542 int rlen, type;
543 Key *key = NULL;
544
545#ifdef DEBUG_PK
546 dump_base64(stderr, blob, blen);
547#endif
548 buffer_init(&b);
549 buffer_append(&b, blob, blen);
550 ktype = buffer_get_string(&b, NULL);
551 type = key_type_from_name(ktype);
552
553 switch(type){
554 case KEY_RSA:
555 key = key_new(type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100556 buffer_get_bignum2(&b, key->rsa->e);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000557 buffer_get_bignum2(&b, key->rsa->n);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100558#ifdef DEBUG_PK
559 RSA_print_fp(stderr, key->rsa, 8);
560#endif
561 break;
562 case KEY_DSA:
563 key = key_new(type);
564 buffer_get_bignum2(&b, key->dsa->p);
565 buffer_get_bignum2(&b, key->dsa->q);
566 buffer_get_bignum2(&b, key->dsa->g);
567 buffer_get_bignum2(&b, key->dsa->pub_key);
568#ifdef DEBUG_PK
569 DSA_print_fp(stderr, key->dsa, 8);
570#endif
571 break;
572 case KEY_UNSPEC:
573 key = key_new(type);
574 break;
575 default:
576 error("key_from_blob: cannot handle type %s", ktype);
577 break;
578 }
579 rlen = buffer_len(&b);
580 if (key != NULL && rlen != 0)
581 error("key_from_blob: remaining bytes in key blob %d", rlen);
582 xfree(ktype);
583 buffer_free(&b);
584 return key;
585}
586
587int
Ben Lindstrom46c16222000-12-22 01:43:59 +0000588key_to_blob(Key *key, u_char **blobp, u_int *lenp)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100589{
590 Buffer b;
591 int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000592 u_char *buf;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100593
594 if (key == NULL) {
595 error("key_to_blob: key == NULL");
596 return 0;
597 }
598 buffer_init(&b);
599 switch(key->type){
600 case KEY_DSA:
601 buffer_put_cstring(&b, key_ssh_name(key));
602 buffer_put_bignum2(&b, key->dsa->p);
603 buffer_put_bignum2(&b, key->dsa->q);
604 buffer_put_bignum2(&b, key->dsa->g);
605 buffer_put_bignum2(&b, key->dsa->pub_key);
606 break;
607 case KEY_RSA:
608 buffer_put_cstring(&b, key_ssh_name(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100609 buffer_put_bignum2(&b, key->rsa->e);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000610 buffer_put_bignum2(&b, key->rsa->n);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100611 break;
612 default:
613 error("key_to_blob: illegal key type %d", key->type);
614 break;
615 }
616 len = buffer_len(&b);
617 buf = xmalloc(len);
618 memcpy(buf, buffer_ptr(&b), len);
619 memset(buffer_ptr(&b), 0, len);
620 buffer_free(&b);
621 if (lenp != NULL)
622 *lenp = len;
623 if (blobp != NULL)
624 *blobp = buf;
625 return len;
626}
627
628int
629key_sign(
630 Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000631 u_char **sigp, int *lenp,
632 u_char *data, int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100633{
634 switch(key->type){
635 case KEY_DSA:
636 return ssh_dss_sign(key, sigp, lenp, data, datalen);
637 break;
638 case KEY_RSA:
639 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
640 break;
641 default:
642 error("key_sign: illegal key type %d", key->type);
643 return -1;
644 break;
645 }
646}
647
648int
649key_verify(
650 Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000651 u_char *signature, int signaturelen,
652 u_char *data, int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100653{
654 switch(key->type){
655 case KEY_DSA:
656 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
657 break;
658 case KEY_RSA:
659 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
660 break;
661 default:
662 error("key_verify: illegal key type %d", key->type);
663 return -1;
664 break;
665 }
666}