]> andersk Git - gssapi-openssh.git/blame - openssh/key.c
Import of OpenSSH 4.4p1
[gssapi-openssh.git] / openssh / key.c
CommitLineData
9108f8d9 1/* $OpenBSD: key.c,v 1.67 2006/08/03 03:34:42 deraadt Exp $ */
3c0ef626 2/*
3 * read_bignum():
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 *
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 *
12 *
13 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
9108f8d9 35
3c0ef626 36#include "includes.h"
9108f8d9 37
38#include <sys/types.h>
3c0ef626 39
40#include <openssl/evp.h>
41
9108f8d9 42#include <stdarg.h>
43#include <stdio.h>
44#include <string.h>
45
3c0ef626 46#include "xmalloc.h"
47#include "key.h"
48#include "rsa.h"
3c0ef626 49#include "uuencode.h"
50#include "buffer.h"
3c0ef626 51#include "log.h"
52
53Key *
54key_new(int type)
55{
56 Key *k;
57 RSA *rsa;
58 DSA *dsa;
9108f8d9 59 k = xcalloc(1, sizeof(*k));
3c0ef626 60 k->type = type;
3c0ef626 61 k->dsa = NULL;
62 k->rsa = NULL;
63 switch (k->type) {
64 case KEY_RSA1:
65 case KEY_RSA:
e9a17296 66 if ((rsa = RSA_new()) == NULL)
67 fatal("key_new: RSA_new failed");
68 if ((rsa->n = BN_new()) == NULL)
69 fatal("key_new: BN_new failed");
70 if ((rsa->e = BN_new()) == NULL)
71 fatal("key_new: BN_new failed");
3c0ef626 72 k->rsa = rsa;
73 break;
74 case KEY_DSA:
e9a17296 75 if ((dsa = DSA_new()) == NULL)
76 fatal("key_new: DSA_new failed");
77 if ((dsa->p = BN_new()) == NULL)
78 fatal("key_new: BN_new failed");
79 if ((dsa->q = BN_new()) == NULL)
80 fatal("key_new: BN_new failed");
81 if ((dsa->g = BN_new()) == NULL)
82 fatal("key_new: BN_new failed");
83 if ((dsa->pub_key = BN_new()) == NULL)
84 fatal("key_new: BN_new failed");
3c0ef626 85 k->dsa = dsa;
86 break;
87 case KEY_UNSPEC:
88 break;
89 default:
90 fatal("key_new: bad key type %d", k->type);
91 break;
92 }
93 return k;
94}
680cee3b 95
3c0ef626 96Key *
97key_new_private(int type)
98{
99 Key *k = key_new(type);
100 switch (k->type) {
101 case KEY_RSA1:
102 case KEY_RSA:
e9a17296 103 if ((k->rsa->d = BN_new()) == NULL)
104 fatal("key_new_private: BN_new failed");
105 if ((k->rsa->iqmp = BN_new()) == NULL)
106 fatal("key_new_private: BN_new failed");
107 if ((k->rsa->q = BN_new()) == NULL)
108 fatal("key_new_private: BN_new failed");
109 if ((k->rsa->p = BN_new()) == NULL)
110 fatal("key_new_private: BN_new failed");
111 if ((k->rsa->dmq1 = BN_new()) == NULL)
112 fatal("key_new_private: BN_new failed");
113 if ((k->rsa->dmp1 = BN_new()) == NULL)
114 fatal("key_new_private: BN_new failed");
3c0ef626 115 break;
116 case KEY_DSA:
e9a17296 117 if ((k->dsa->priv_key = BN_new()) == NULL)
118 fatal("key_new_private: BN_new failed");
3c0ef626 119 break;
120 case KEY_UNSPEC:
121 break;
122 default:
123 break;
124 }
125 return k;
126}
680cee3b 127
3c0ef626 128void
129key_free(Key *k)
130{
9108f8d9 131 if (k == NULL)
132 fatal("key_free: key is NULL");
3c0ef626 133 switch (k->type) {
134 case KEY_RSA1:
135 case KEY_RSA:
136 if (k->rsa != NULL)
137 RSA_free(k->rsa);
138 k->rsa = NULL;
139 break;
140 case KEY_DSA:
141 if (k->dsa != NULL)
142 DSA_free(k->dsa);
143 k->dsa = NULL;
144 break;
145 case KEY_UNSPEC:
146 break;
147 default:
148 fatal("key_free: bad key type %d", k->type);
149 break;
150 }
151 xfree(k);
152}
cdd66111 153
3c0ef626 154int
cdd66111 155key_equal(const Key *a, const Key *b)
3c0ef626 156{
157 if (a == NULL || b == NULL || a->type != b->type)
158 return 0;
159 switch (a->type) {
160 case KEY_RSA1:
161 case KEY_RSA:
162 return a->rsa != NULL && b->rsa != NULL &&
163 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
164 BN_cmp(a->rsa->n, b->rsa->n) == 0;
3c0ef626 165 case KEY_DSA:
166 return a->dsa != NULL && b->dsa != NULL &&
167 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
168 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
169 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
170 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
3c0ef626 171 default:
172 fatal("key_equal: bad key type %d", a->type);
173 break;
174 }
175 return 0;
176}
177
0fff78ff 178u_char*
cdd66111 179key_fingerprint_raw(const Key *k, enum fp_type dgst_type,
180 u_int *dgst_raw_length)
3c0ef626 181{
e9a17296 182 const EVP_MD *md = NULL;
3c0ef626 183 EVP_MD_CTX ctx;
184 u_char *blob = NULL;
185 u_char *retval = NULL;
e9a17296 186 u_int len = 0;
3c0ef626 187 int nlen, elen;
188
189 *dgst_raw_length = 0;
190
191 switch (dgst_type) {
192 case SSH_FP_MD5:
193 md = EVP_md5();
194 break;
195 case SSH_FP_SHA1:
196 md = EVP_sha1();
197 break;
198 default:
199 fatal("key_fingerprint_raw: bad digest type %d",
200 dgst_type);
201 }
202 switch (k->type) {
203 case KEY_RSA1:
204 nlen = BN_num_bytes(k->rsa->n);
205 elen = BN_num_bytes(k->rsa->e);
206 len = nlen + elen;
207 blob = xmalloc(len);
208 BN_bn2bin(k->rsa->n, blob);
209 BN_bn2bin(k->rsa->e, blob + nlen);
210 break;
211 case KEY_DSA:
212 case KEY_RSA:
213 key_to_blob(k, &blob, &len);
214 break;
215 case KEY_UNSPEC:
216 return retval;
3c0ef626 217 default:
218 fatal("key_fingerprint_raw: bad key type %d", k->type);
219 break;
220 }
221 if (blob != NULL) {
222 retval = xmalloc(EVP_MAX_MD_SIZE);
223 EVP_DigestInit(&ctx, md);
224 EVP_DigestUpdate(&ctx, blob, len);
e9a17296 225 EVP_DigestFinal(&ctx, retval, dgst_raw_length);
3c0ef626 226 memset(blob, 0, len);
227 xfree(blob);
228 } else {
229 fatal("key_fingerprint_raw: blob is null");
230 }
231 return retval;
232}
233
41b2f314 234static char *
235key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
3c0ef626 236{
237 char *retval;
665a873d 238 u_int i;
3c0ef626 239
9108f8d9 240 retval = xcalloc(1, dgst_raw_len * 3 + 1);
e9a17296 241 for (i = 0; i < dgst_raw_len; i++) {
3c0ef626 242 char hex[4];
243 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
0fff78ff 244 strlcat(retval, hex, dgst_raw_len * 3 + 1);
3c0ef626 245 }
0fff78ff 246
247 /* Remove the trailing ':' character */
3c0ef626 248 retval[(dgst_raw_len * 3) - 1] = '\0';
249 return retval;
250}
251
41b2f314 252static char *
253key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
3c0ef626 254{
255 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
256 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
257 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
258 u_int i, j = 0, rounds, seed = 1;
259 char *retval;
260
261 rounds = (dgst_raw_len / 2) + 1;
9108f8d9 262 retval = xcalloc((rounds * 6), sizeof(char));
3c0ef626 263 retval[j++] = 'x';
264 for (i = 0; i < rounds; i++) {
265 u_int idx0, idx1, idx2, idx3, idx4;
266 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
267 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
268 seed) % 6;
269 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
270 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
271 (seed / 6)) % 6;
272 retval[j++] = vowels[idx0];
273 retval[j++] = consonants[idx1];
274 retval[j++] = vowels[idx2];
275 if ((i + 1) < rounds) {
276 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
277 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
278 retval[j++] = consonants[idx3];
279 retval[j++] = '-';
280 retval[j++] = consonants[idx4];
281 seed = ((seed * 5) +
282 ((((u_int)(dgst_raw[2 * i])) * 7) +
283 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
284 }
285 } else {
286 idx0 = seed % 6;
287 idx1 = 16;
288 idx2 = seed / 6;
289 retval[j++] = vowels[idx0];
290 retval[j++] = consonants[idx1];
291 retval[j++] = vowels[idx2];
292 }
293 }
294 retval[j++] = 'x';
295 retval[j++] = '\0';
296 return retval;
297}
298
41b2f314 299char *
cdd66111 300key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
3c0ef626 301{
302 char *retval = NULL;
303 u_char *dgst_raw;
e9a17296 304 u_int dgst_raw_len;
305
3c0ef626 306 dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
307 if (!dgst_raw)
308 fatal("key_fingerprint: null from key_fingerprint_raw()");
e9a17296 309 switch (dgst_rep) {
3c0ef626 310 case SSH_FP_HEX:
311 retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
312 break;
313 case SSH_FP_BUBBLEBABBLE:
314 retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
315 break;
316 default:
317 fatal("key_fingerprint_ex: bad digest representation %d",
318 dgst_rep);
319 break;
320 }
321 memset(dgst_raw, 0, dgst_raw_len);
322 xfree(dgst_raw);
323 return retval;
324}
325
326/*
327 * Reads a multiple-precision integer in decimal from the buffer, and advances
328 * the pointer. The integer must already be initialized. This function is
329 * permitted to modify the buffer. This leaves *cpp to point just beyond the
330 * last processed (and maybe modified) character. Note that this may modify
331 * the buffer containing the number.
332 */
333static int
334read_bignum(char **cpp, BIGNUM * value)
335{
336 char *cp = *cpp;
337 int old;
338
339 /* Skip any leading whitespace. */
340 for (; *cp == ' ' || *cp == '\t'; cp++)
341 ;
342
343 /* Check that it begins with a decimal digit. */
344 if (*cp < '0' || *cp > '9')
345 return 0;
346
347 /* Save starting position. */
348 *cpp = cp;
349
350 /* Move forward until all decimal digits skipped. */
351 for (; *cp >= '0' && *cp <= '9'; cp++)
352 ;
353
354 /* Save the old terminating character, and replace it by \0. */
355 old = *cp;
356 *cp = 0;
357
358 /* Parse the number. */
359 if (BN_dec2bn(&value, *cpp) == 0)
360 return 0;
361
362 /* Restore old terminating character. */
363 *cp = old;
364
365 /* Move beyond the number and return success. */
366 *cpp = cp;
367 return 1;
368}
680cee3b 369
3c0ef626 370static int
371write_bignum(FILE *f, BIGNUM *num)
372{
373 char *buf = BN_bn2dec(num);
374 if (buf == NULL) {
375 error("write_bignum: BN_bn2dec() failed");
376 return 0;
377 }
378 fprintf(f, " %s", buf);
379 OPENSSL_free(buf);
380 return 1;
381}
382
383/* returns 1 ok, -1 error */
384int
385key_read(Key *ret, char **cpp)
386{
387 Key *k;
388 int success = -1;
389 char *cp, *space;
390 int len, n, type;
391 u_int bits;
392 u_char *blob;
393
394 cp = *cpp;
395
e9a17296 396 switch (ret->type) {
3c0ef626 397 case KEY_RSA1:
398 /* Get number of bits. */
399 if (*cp < '0' || *cp > '9')
400 return -1; /* Bad bit count... */
401 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
402 bits = 10 * bits + *cp - '0';
403 if (bits == 0)
404 return -1;
405 *cpp = cp;
406 /* Get public exponent, public modulus. */
407 if (!read_bignum(cpp, ret->rsa->e))
408 return -1;
409 if (!read_bignum(cpp, ret->rsa->n))
410 return -1;
411 success = 1;
412 break;
413 case KEY_UNSPEC:
414 case KEY_RSA:
415 case KEY_DSA:
416 space = strchr(cp, ' ');
417 if (space == NULL) {
6a9b3198 418 debug3("key_read: missing whitespace");
3c0ef626 419 return -1;
420 }
421 *space = '\0';
422 type = key_type_from_name(cp);
423 *space = ' ';
424 if (type == KEY_UNSPEC) {
6a9b3198 425 debug3("key_read: missing keytype");
3c0ef626 426 return -1;
427 }
428 cp = space+1;
429 if (*cp == '\0') {
430 debug3("key_read: short string");
431 return -1;
432 }
433 if (ret->type == KEY_UNSPEC) {
434 ret->type = type;
435 } else if (ret->type != type) {
436 /* is a key, but different type */
437 debug3("key_read: type mismatch");
438 return -1;
439 }
440 len = 2*strlen(cp);
441 blob = xmalloc(len);
442 n = uudecode(cp, blob, len);
443 if (n < 0) {
444 error("key_read: uudecode %s failed", cp);
e9a17296 445 xfree(blob);
3c0ef626 446 return -1;
447 }
0fff78ff 448 k = key_from_blob(blob, (u_int)n);
e9a17296 449 xfree(blob);
3c0ef626 450 if (k == NULL) {
451 error("key_read: key_from_blob %s failed", cp);
452 return -1;
453 }
3c0ef626 454 if (k->type != type) {
455 error("key_read: type mismatch: encoding error");
456 key_free(k);
457 return -1;
458 }
459/*XXXX*/
460 if (ret->type == KEY_RSA) {
461 if (ret->rsa != NULL)
462 RSA_free(ret->rsa);
463 ret->rsa = k->rsa;
464 k->rsa = NULL;
465 success = 1;
466#ifdef DEBUG_PK
467 RSA_print_fp(stderr, ret->rsa, 8);
468#endif
469 } else {
470 if (ret->dsa != NULL)
471 DSA_free(ret->dsa);
472 ret->dsa = k->dsa;
473 k->dsa = NULL;
474 success = 1;
475#ifdef DEBUG_PK
476 DSA_print_fp(stderr, ret->dsa, 8);
477#endif
478 }
479/*XXXX*/
e9a17296 480 key_free(k);
3c0ef626 481 if (success != 1)
482 break;
3c0ef626 483 /* advance cp: skip whitespace and data */
484 while (*cp == ' ' || *cp == '\t')
485 cp++;
486 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
487 cp++;
488 *cpp = cp;
489 break;
490 default:
491 fatal("key_read: bad key type: %d", ret->type);
492 break;
493 }
494 return success;
495}
680cee3b 496
3c0ef626 497int
cdd66111 498key_write(const Key *key, FILE *f)
3c0ef626 499{
e9a17296 500 int n, success = 0;
501 u_int len, bits = 0;
41b2f314 502 u_char *blob;
503 char *uu;
3c0ef626 504
505 if (key->type == KEY_RSA1 && key->rsa != NULL) {
506 /* size of modulus 'n' */
507 bits = BN_num_bits(key->rsa->n);
508 fprintf(f, "%u", bits);
509 if (write_bignum(f, key->rsa->e) &&
510 write_bignum(f, key->rsa->n)) {
511 success = 1;
512 } else {
513 error("key_write: failed for RSA key");
514 }
515 } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
516 (key->type == KEY_RSA && key->rsa != NULL)) {
3c0ef626 517 key_to_blob(key, &blob, &len);
518 uu = xmalloc(2*len);
519 n = uuencode(blob, len, uu, 2*len);
520 if (n > 0) {
521 fprintf(f, "%s %s", key_ssh_name(key), uu);
522 success = 1;
523 }
524 xfree(blob);
525 xfree(uu);
526 }
527 return success;
528}
680cee3b 529
cdd66111 530const char *
531key_type(const Key *k)
3c0ef626 532{
533 switch (k->type) {
534 case KEY_RSA1:
535 return "RSA1";
3c0ef626 536 case KEY_RSA:
537 return "RSA";
3c0ef626 538 case KEY_DSA:
539 return "DSA";
3c0ef626 540 }
541 return "unknown";
542}
680cee3b 543
cdd66111 544const char *
545key_ssh_name(const Key *k)
3c0ef626 546{
547 switch (k->type) {
548 case KEY_RSA:
549 return "ssh-rsa";
3c0ef626 550 case KEY_DSA:
551 return "ssh-dss";
3c0ef626 552 }
553 return "ssh-unknown";
554}
680cee3b 555
3c0ef626 556u_int
cdd66111 557key_size(const Key *k)
e9a17296 558{
3c0ef626 559 switch (k->type) {
560 case KEY_RSA1:
561 case KEY_RSA:
562 return BN_num_bits(k->rsa->n);
3c0ef626 563 case KEY_DSA:
564 return BN_num_bits(k->dsa->p);
3c0ef626 565 }
566 return 0;
567}
568
569static RSA *
570rsa_generate_private_key(u_int bits)
571{
572 RSA *private;
9108f8d9 573
3c0ef626 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;
578}
579
580static DSA*
581dsa_generate_private_key(u_int bits)
582{
583 DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
9108f8d9 584
3c0ef626 585 if (private == NULL)
586 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
587 if (!DSA_generate_key(private))
588 fatal("dsa_generate_private_key: DSA_generate_key failed.");
589 if (private == NULL)
590 fatal("dsa_generate_private_key: NULL.");
591 return private;
592}
593
594Key *
595key_generate(int type, u_int bits)
596{
597 Key *k = key_new(KEY_UNSPEC);
598 switch (type) {
599 case KEY_DSA:
600 k->dsa = dsa_generate_private_key(bits);
601 break;
602 case KEY_RSA:
603 case KEY_RSA1:
604 k->rsa = rsa_generate_private_key(bits);
605 break;
606 default:
607 fatal("key_generate: unknown type %d", type);
608 }
609 k->type = type;
610 return k;
611}
612
613Key *
cdd66111 614key_from_private(const Key *k)
3c0ef626 615{
616 Key *n = NULL;
617 switch (k->type) {
618 case KEY_DSA:
619 n = key_new(k->type);
620 BN_copy(n->dsa->p, k->dsa->p);
621 BN_copy(n->dsa->q, k->dsa->q);
622 BN_copy(n->dsa->g, k->dsa->g);
623 BN_copy(n->dsa->pub_key, k->dsa->pub_key);
624 break;
625 case KEY_RSA:
626 case KEY_RSA1:
627 n = key_new(k->type);
628 BN_copy(n->rsa->n, k->rsa->n);
629 BN_copy(n->rsa->e, k->rsa->e);
630 break;
631 default:
632 fatal("key_from_private: unknown type %d", k->type);
633 break;
634 }
635 return n;
636}
637
638int
639key_type_from_name(char *name)
640{
e9a17296 641 if (strcmp(name, "rsa1") == 0) {
3c0ef626 642 return KEY_RSA1;
e9a17296 643 } else if (strcmp(name, "rsa") == 0) {
3c0ef626 644 return KEY_RSA;
e9a17296 645 } else if (strcmp(name, "dsa") == 0) {
3c0ef626 646 return KEY_DSA;
e9a17296 647 } else if (strcmp(name, "ssh-rsa") == 0) {
3c0ef626 648 return KEY_RSA;
e9a17296 649 } else if (strcmp(name, "ssh-dss") == 0) {
3c0ef626 650 return KEY_DSA;
651 }
652 debug2("key_type_from_name: unknown key type '%s'", name);
653 return KEY_UNSPEC;
654}
655
656int
657key_names_valid2(const char *names)
658{
659 char *s, *cp, *p;
660
661 if (names == NULL || strcmp(names, "") == 0)
662 return 0;
663 s = cp = xstrdup(names);
664 for ((p = strsep(&cp, ",")); p && *p != '\0';
e9a17296 665 (p = strsep(&cp, ","))) {
3c0ef626 666 switch (key_type_from_name(p)) {
667 case KEY_RSA1:
668 case KEY_UNSPEC:
669 xfree(s);
670 return 0;
671 }
672 }
673 debug3("key names ok: [%s]", names);
674 xfree(s);
675 return 1;
676}
677
678Key *
cdd66111 679key_from_blob(const u_char *blob, u_int blen)
3c0ef626 680{
681 Buffer b;
3c0ef626 682 int rlen, type;
996d5e62 683 char *ktype = NULL;
3c0ef626 684 Key *key = NULL;
685
686#ifdef DEBUG_PK
687 dump_base64(stderr, blob, blen);
688#endif
689 buffer_init(&b);
690 buffer_append(&b, blob, blen);
996d5e62 691 if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
692 error("key_from_blob: can't read key type");
693 goto out;
694 }
695
3c0ef626 696 type = key_type_from_name(ktype);
697
e9a17296 698 switch (type) {
3c0ef626 699 case KEY_RSA:
700 key = key_new(type);
996d5e62 701 if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
702 buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
703 error("key_from_blob: can't read rsa key");
704 key_free(key);
705 key = NULL;
706 goto out;
707 }
3c0ef626 708#ifdef DEBUG_PK
709 RSA_print_fp(stderr, key->rsa, 8);
710#endif
711 break;
712 case KEY_DSA:
713 key = key_new(type);
996d5e62 714 if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
715 buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
716 buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
717 buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
718 error("key_from_blob: can't read dsa key");
719 key_free(key);
720 key = NULL;
721 goto out;
722 }
3c0ef626 723#ifdef DEBUG_PK
724 DSA_print_fp(stderr, key->dsa, 8);
725#endif
726 break;
727 case KEY_UNSPEC:
728 key = key_new(type);
729 break;
730 default:
731 error("key_from_blob: cannot handle type %s", ktype);
996d5e62 732 goto out;
3c0ef626 733 }
734 rlen = buffer_len(&b);
735 if (key != NULL && rlen != 0)
736 error("key_from_blob: remaining bytes in key blob %d", rlen);
996d5e62 737 out:
738 if (ktype != NULL)
739 xfree(ktype);
3c0ef626 740 buffer_free(&b);
741 return key;
742}
743
744int
cdd66111 745key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
3c0ef626 746{
747 Buffer b;
748 int len;
3c0ef626 749
750 if (key == NULL) {
751 error("key_to_blob: key == NULL");
752 return 0;
753 }
754 buffer_init(&b);
e9a17296 755 switch (key->type) {
3c0ef626 756 case KEY_DSA:
757 buffer_put_cstring(&b, key_ssh_name(key));
758 buffer_put_bignum2(&b, key->dsa->p);
759 buffer_put_bignum2(&b, key->dsa->q);
760 buffer_put_bignum2(&b, key->dsa->g);
761 buffer_put_bignum2(&b, key->dsa->pub_key);
762 break;
763 case KEY_RSA:
764 buffer_put_cstring(&b, key_ssh_name(key));
765 buffer_put_bignum2(&b, key->rsa->e);
766 buffer_put_bignum2(&b, key->rsa->n);
767 break;
768 default:
769 error("key_to_blob: unsupported key type %d", key->type);
770 buffer_free(&b);
771 return 0;
772 }
773 len = buffer_len(&b);
3c0ef626 774 if (lenp != NULL)
775 *lenp = len;
41b2f314 776 if (blobp != NULL) {
777 *blobp = xmalloc(len);
778 memcpy(*blobp, buffer_ptr(&b), len);
779 }
780 memset(buffer_ptr(&b), 0, len);
781 buffer_free(&b);
3c0ef626 782 return len;
783}
784
785int
786key_sign(
cdd66111 787 const Key *key,
e9a17296 788 u_char **sigp, u_int *lenp,
cdd66111 789 const u_char *data, u_int datalen)
3c0ef626 790{
e9a17296 791 switch (key->type) {
3c0ef626 792 case KEY_DSA:
793 return ssh_dss_sign(key, sigp, lenp, data, datalen);
3c0ef626 794 case KEY_RSA:
795 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
3c0ef626 796 default:
c9f39d2c 797 error("key_sign: invalid key type %d", key->type);
3c0ef626 798 return -1;
3c0ef626 799 }
800}
801
f5799ae1 802/*
803 * key_verify returns 1 for a correct signature, 0 for an incorrect signature
804 * and -1 on error.
805 */
3c0ef626 806int
807key_verify(
cdd66111 808 const Key *key,
809 const u_char *signature, u_int signaturelen,
810 const u_char *data, u_int datalen)
3c0ef626 811{
812 if (signaturelen == 0)
813 return -1;
814
e9a17296 815 switch (key->type) {
3c0ef626 816 case KEY_DSA:
817 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
3c0ef626 818 case KEY_RSA:
819 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
3c0ef626 820 default:
c9f39d2c 821 error("key_verify: invalid key type %d", key->type);
3c0ef626 822 return -1;
3c0ef626 823 }
824}
700318f3 825
826/* Converts a private to a public key */
700318f3 827Key *
cdd66111 828key_demote(const Key *k)
700318f3 829{
830 Key *pk;
831
9108f8d9 832 pk = xcalloc(1, sizeof(*pk));
700318f3 833 pk->type = k->type;
834 pk->flags = k->flags;
835 pk->dsa = NULL;
836 pk->rsa = NULL;
837
838 switch (k->type) {
839 case KEY_RSA1:
840 case KEY_RSA:
841 if ((pk->rsa = RSA_new()) == NULL)
842 fatal("key_demote: RSA_new failed");
843 if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
844 fatal("key_demote: BN_dup failed");
845 if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
846 fatal("key_demote: BN_dup failed");
847 break;
848 case KEY_DSA:
849 if ((pk->dsa = DSA_new()) == NULL)
850 fatal("key_demote: DSA_new failed");
851 if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
852 fatal("key_demote: BN_dup failed");
853 if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
854 fatal("key_demote: BN_dup failed");
855 if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
856 fatal("key_demote: BN_dup failed");
857 if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
858 fatal("key_demote: BN_dup failed");
859 break;
860 default:
861 fatal("key_free: bad key type %d", k->type);
862 break;
863 }
864
865 return (pk);
866}
This page took 1.637092 seconds and 5 git commands to generate.