]> andersk Git - openssh.git/blame_incremental - key.c
- deraadt@cvs.openbsd.org 2006/03/20 17:10:19
[openssh.git] / key.c
... / ...
CommitLineData
1/*
2 * 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 *
12 * Copyright (c) 2000, 2001 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.
22 *
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 */
34#include "includes.h"
35
36#include <openssl/evp.h>
37
38#include "xmalloc.h"
39#include "key.h"
40#include "rsa.h"
41#include "uuencode.h"
42#include "buffer.h"
43#include "bufaux.h"
44#include "log.h"
45
46Key *
47key_new(int type)
48{
49 Key *k;
50 RSA *rsa;
51 DSA *dsa;
52 k = xmalloc(sizeof(*k));
53 k->type = type;
54 k->flags = 0;
55 k->dsa = NULL;
56 k->rsa = NULL;
57 switch (k->type) {
58 case KEY_RSA1:
59 case KEY_RSA:
60 if ((rsa = RSA_new()) == NULL)
61 fatal("key_new: RSA_new failed");
62 if ((rsa->n = BN_new()) == NULL)
63 fatal("key_new: BN_new failed");
64 if ((rsa->e = BN_new()) == NULL)
65 fatal("key_new: BN_new failed");
66 k->rsa = rsa;
67 break;
68 case KEY_DSA:
69 if ((dsa = DSA_new()) == NULL)
70 fatal("key_new: DSA_new failed");
71 if ((dsa->p = BN_new()) == NULL)
72 fatal("key_new: BN_new failed");
73 if ((dsa->q = BN_new()) == NULL)
74 fatal("key_new: BN_new failed");
75 if ((dsa->g = BN_new()) == NULL)
76 fatal("key_new: BN_new failed");
77 if ((dsa->pub_key = BN_new()) == NULL)
78 fatal("key_new: BN_new failed");
79 k->dsa = dsa;
80 break;
81 case KEY_UNSPEC:
82 break;
83 default:
84 fatal("key_new: bad key type %d", k->type);
85 break;
86 }
87 return k;
88}
89
90Key *
91key_new_private(int type)
92{
93 Key *k = key_new(type);
94 switch (k->type) {
95 case KEY_RSA1:
96 case KEY_RSA:
97 if ((k->rsa->d = BN_new()) == NULL)
98 fatal("key_new_private: BN_new failed");
99 if ((k->rsa->iqmp = BN_new()) == NULL)
100 fatal("key_new_private: BN_new failed");
101 if ((k->rsa->q = BN_new()) == NULL)
102 fatal("key_new_private: BN_new failed");
103 if ((k->rsa->p = BN_new()) == NULL)
104 fatal("key_new_private: BN_new failed");
105 if ((k->rsa->dmq1 = BN_new()) == NULL)
106 fatal("key_new_private: BN_new failed");
107 if ((k->rsa->dmp1 = BN_new()) == NULL)
108 fatal("key_new_private: BN_new failed");
109 break;
110 case KEY_DSA:
111 if ((k->dsa->priv_key = BN_new()) == NULL)
112 fatal("key_new_private: BN_new failed");
113 break;
114 case KEY_UNSPEC:
115 break;
116 default:
117 break;
118 }
119 return k;
120}
121
122void
123key_free(Key *k)
124{
125 if (k == NULL)
126 fatal("key_free: key is NULL);
127 switch (k->type) {
128 case KEY_RSA1:
129 case KEY_RSA:
130 if (k->rsa != NULL)
131 RSA_free(k->rsa);
132 k->rsa = NULL;
133 break;
134 case KEY_DSA:
135 if (k->dsa != NULL)
136 DSA_free(k->dsa);
137 k->dsa = NULL;
138 break;
139 case KEY_UNSPEC:
140 break;
141 default:
142 fatal("key_free: bad key type %d", k->type);
143 break;
144 }
145 xfree(k);
146}
147
148int
149key_equal(const Key *a, const Key *b)
150{
151 if (a == NULL || b == NULL || a->type != b->type)
152 return 0;
153 switch (a->type) {
154 case KEY_RSA1:
155 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 case KEY_DSA:
160 return a->dsa != NULL && b->dsa != NULL &&
161 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
162 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
163 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
164 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
165 default:
166 fatal("key_equal: bad key type %d", a->type);
167 break;
168 }
169 return 0;
170}
171
172u_char*
173key_fingerprint_raw(const Key *k, enum fp_type dgst_type,
174 u_int *dgst_raw_length)
175{
176 const EVP_MD *md = NULL;
177 EVP_MD_CTX ctx;
178 u_char *blob = NULL;
179 u_char *retval = NULL;
180 u_int len = 0;
181 int nlen, elen;
182
183 *dgst_raw_length = 0;
184
185 switch (dgst_type) {
186 case SSH_FP_MD5:
187 md = EVP_md5();
188 break;
189 case SSH_FP_SHA1:
190 md = EVP_sha1();
191 break;
192 default:
193 fatal("key_fingerprint_raw: bad digest type %d",
194 dgst_type);
195 }
196 switch (k->type) {
197 case KEY_RSA1:
198 nlen = BN_num_bytes(k->rsa->n);
199 elen = BN_num_bytes(k->rsa->e);
200 len = nlen + elen;
201 blob = xmalloc(len);
202 BN_bn2bin(k->rsa->n, blob);
203 BN_bn2bin(k->rsa->e, blob + nlen);
204 break;
205 case KEY_DSA:
206 case KEY_RSA:
207 key_to_blob(k, &blob, &len);
208 break;
209 case KEY_UNSPEC:
210 return retval;
211 default:
212 fatal("key_fingerprint_raw: bad key type %d", k->type);
213 break;
214 }
215 if (blob != NULL) {
216 retval = xmalloc(EVP_MAX_MD_SIZE);
217 EVP_DigestInit(&ctx, md);
218 EVP_DigestUpdate(&ctx, blob, len);
219 EVP_DigestFinal(&ctx, retval, dgst_raw_length);
220 memset(blob, 0, len);
221 xfree(blob);
222 } else {
223 fatal("key_fingerprint_raw: blob is null");
224 }
225 return retval;
226}
227
228static char *
229key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
230{
231 char *retval;
232 u_int i;
233
234 retval = xmalloc(dgst_raw_len * 3 + 1);
235 retval[0] = '\0';
236 for (i = 0; i < dgst_raw_len; i++) {
237 char hex[4];
238 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
239 strlcat(retval, hex, dgst_raw_len * 3 + 1);
240 }
241
242 /* Remove the trailing ':' character */
243 retval[(dgst_raw_len * 3) - 1] = '\0';
244 return retval;
245}
246
247static char *
248key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
249{
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' };
253 u_int i, j = 0, rounds, seed = 1;
254 char *retval;
255
256 rounds = (dgst_raw_len / 2) + 1;
257 retval = xmalloc(sizeof(char) * (rounds*6));
258 retval[j++] = 'x';
259 for (i = 0; i < rounds; i++) {
260 u_int idx0, idx1, idx2, idx3, idx4;
261 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
262 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
263 seed) % 6;
264 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
265 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
266 (seed / 6)) % 6;
267 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];
276 seed = ((seed * 5) +
277 ((((u_int)(dgst_raw[2 * i])) * 7) +
278 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
279 }
280 } else {
281 idx0 = seed % 6;
282 idx1 = 16;
283 idx2 = seed / 6;
284 retval[j++] = vowels[idx0];
285 retval[j++] = consonants[idx1];
286 retval[j++] = vowels[idx2];
287 }
288 }
289 retval[j++] = 'x';
290 retval[j++] = '\0';
291 return retval;
292}
293
294char *
295key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
296{
297 char *retval = NULL;
298 u_char *dgst_raw;
299 u_int dgst_raw_len;
300
301 dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
302 if (!dgst_raw)
303 fatal("key_fingerprint: null from key_fingerprint_raw()");
304 switch (dgst_rep) {
305 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
321/*
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 */
328static int
329read_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}
364
365static int
366write_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);
374 OPENSSL_free(buf);
375 return 1;
376}
377
378/* returns 1 ok, -1 error */
379int
380key_read(Key *ret, char **cpp)
381{
382 Key *k;
383 int success = -1;
384 char *cp, *space;
385 int len, n, type;
386 u_int bits;
387 u_char *blob;
388
389 cp = *cpp;
390
391 switch (ret->type) {
392 case KEY_RSA1:
393 /* Get number of bits. */
394 if (*cp < '0' || *cp > '9')
395 return -1; /* Bad bit count... */
396 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
397 bits = 10 * bits + *cp - '0';
398 if (bits == 0)
399 return -1;
400 *cpp = cp;
401 /* Get public exponent, public modulus. */
402 if (!read_bignum(cpp, ret->rsa->e))
403 return -1;
404 if (!read_bignum(cpp, ret->rsa->n))
405 return -1;
406 success = 1;
407 break;
408 case KEY_UNSPEC:
409 case KEY_RSA:
410 case KEY_DSA:
411 space = strchr(cp, ' ');
412 if (space == NULL) {
413 debug3("key_read: missing whitespace");
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: missing keytype");
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");
433 return -1;
434 }
435 len = 2*strlen(cp);
436 blob = xmalloc(len);
437 n = uudecode(cp, blob, len);
438 if (n < 0) {
439 error("key_read: uudecode %s failed", cp);
440 xfree(blob);
441 return -1;
442 }
443 k = key_from_blob(blob, (u_int)n);
444 xfree(blob);
445 if (k == NULL) {
446 error("key_read: key_from_blob %s failed", cp);
447 return -1;
448 }
449 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*/
475 key_free(k);
476 if (success != 1)
477 break;
478 /* 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;
484 break;
485 default:
486 fatal("key_read: bad key type: %d", ret->type);
487 break;
488 }
489 return success;
490}
491
492int
493key_write(const Key *key, FILE *f)
494{
495 int n, success = 0;
496 u_int len, bits = 0;
497 u_char *blob;
498 char *uu;
499
500 if (key->type == KEY_RSA1 && key->rsa != NULL) {
501 /* size of modulus 'n' */
502 bits = BN_num_bits(key->rsa->n);
503 fprintf(f, "%u", bits);
504 if (write_bignum(f, key->rsa->e) &&
505 write_bignum(f, key->rsa->n)) {
506 success = 1;
507 } else {
508 error("key_write: failed for RSA key");
509 }
510 } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
511 (key->type == KEY_RSA && key->rsa != NULL)) {
512 key_to_blob(key, &blob, &len);
513 uu = xmalloc(2*len);
514 n = uuencode(blob, len, uu, 2*len);
515 if (n > 0) {
516 fprintf(f, "%s %s", key_ssh_name(key), uu);
517 success = 1;
518 }
519 xfree(blob);
520 xfree(uu);
521 }
522 return success;
523}
524
525const char *
526key_type(const Key *k)
527{
528 switch (k->type) {
529 case KEY_RSA1:
530 return "RSA1";
531 case KEY_RSA:
532 return "RSA";
533 case KEY_DSA:
534 return "DSA";
535 }
536 return "unknown";
537}
538
539const char *
540key_ssh_name(const Key *k)
541{
542 switch (k->type) {
543 case KEY_RSA:
544 return "ssh-rsa";
545 case KEY_DSA:
546 return "ssh-dss";
547 }
548 return "ssh-unknown";
549}
550
551u_int
552key_size(const Key *k)
553{
554 switch (k->type) {
555 case KEY_RSA1:
556 case KEY_RSA:
557 return BN_num_bits(k->rsa->n);
558 case KEY_DSA:
559 return BN_num_bits(k->dsa->p);
560 }
561 return 0;
562}
563
564static RSA *
565rsa_generate_private_key(u_int bits)
566{
567 RSA *private;
568
569 private = RSA_generate_key(bits, 35, NULL, NULL);
570 if (private == NULL)
571 fatal("rsa_generate_private_key: key generation failed.");
572 return private;
573}
574
575static DSA*
576dsa_generate_private_key(u_int bits)
577{
578 DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
579
580 if (private == NULL)
581 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
582 if (!DSA_generate_key(private))
583 fatal("dsa_generate_private_key: DSA_generate_key failed.");
584 if (private == NULL)
585 fatal("dsa_generate_private_key: NULL.");
586 return private;
587}
588
589Key *
590key_generate(int type, u_int bits)
591{
592 Key *k = key_new(KEY_UNSPEC);
593 switch (type) {
594 case KEY_DSA:
595 k->dsa = dsa_generate_private_key(bits);
596 break;
597 case KEY_RSA:
598 case KEY_RSA1:
599 k->rsa = rsa_generate_private_key(bits);
600 break;
601 default:
602 fatal("key_generate: unknown type %d", type);
603 }
604 k->type = type;
605 return k;
606}
607
608Key *
609key_from_private(const Key *k)
610{
611 Key *n = NULL;
612 switch (k->type) {
613 case KEY_DSA:
614 n = key_new(k->type);
615 BN_copy(n->dsa->p, k->dsa->p);
616 BN_copy(n->dsa->q, k->dsa->q);
617 BN_copy(n->dsa->g, k->dsa->g);
618 BN_copy(n->dsa->pub_key, k->dsa->pub_key);
619 break;
620 case KEY_RSA:
621 case KEY_RSA1:
622 n = key_new(k->type);
623 BN_copy(n->rsa->n, k->rsa->n);
624 BN_copy(n->rsa->e, k->rsa->e);
625 break;
626 default:
627 fatal("key_from_private: unknown type %d", k->type);
628 break;
629 }
630 return n;
631}
632
633int
634key_type_from_name(char *name)
635{
636 if (strcmp(name, "rsa1") == 0) {
637 return KEY_RSA1;
638 } else if (strcmp(name, "rsa") == 0) {
639 return KEY_RSA;
640 } else if (strcmp(name, "dsa") == 0) {
641 return KEY_DSA;
642 } else if (strcmp(name, "ssh-rsa") == 0) {
643 return KEY_RSA;
644 } else if (strcmp(name, "ssh-dss") == 0) {
645 return KEY_DSA;
646 }
647 debug2("key_type_from_name: unknown key type '%s'", name);
648 return KEY_UNSPEC;
649}
650
651int
652key_names_valid2(const char *names)
653{
654 char *s, *cp, *p;
655
656 if (names == NULL || strcmp(names, "") == 0)
657 return 0;
658 s = cp = xstrdup(names);
659 for ((p = strsep(&cp, ",")); p && *p != '\0';
660 (p = strsep(&cp, ","))) {
661 switch (key_type_from_name(p)) {
662 case KEY_RSA1:
663 case KEY_UNSPEC:
664 xfree(s);
665 return 0;
666 }
667 }
668 debug3("key names ok: [%s]", names);
669 xfree(s);
670 return 1;
671}
672
673Key *
674key_from_blob(const u_char *blob, u_int blen)
675{
676 Buffer b;
677 int rlen, type;
678 char *ktype = NULL;
679 Key *key = NULL;
680
681#ifdef DEBUG_PK
682 dump_base64(stderr, blob, blen);
683#endif
684 buffer_init(&b);
685 buffer_append(&b, blob, blen);
686 if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
687 error("key_from_blob: can't read key type");
688 goto out;
689 }
690
691 type = key_type_from_name(ktype);
692
693 switch (type) {
694 case KEY_RSA:
695 key = key_new(type);
696 if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
697 buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
698 error("key_from_blob: can't read rsa key");
699 key_free(key);
700 key = NULL;
701 goto out;
702 }
703#ifdef DEBUG_PK
704 RSA_print_fp(stderr, key->rsa, 8);
705#endif
706 break;
707 case KEY_DSA:
708 key = key_new(type);
709 if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
710 buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
711 buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
712 buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
713 error("key_from_blob: can't read dsa key");
714 key_free(key);
715 key = NULL;
716 goto out;
717 }
718#ifdef DEBUG_PK
719 DSA_print_fp(stderr, key->dsa, 8);
720#endif
721 break;
722 case KEY_UNSPEC:
723 key = key_new(type);
724 break;
725 default:
726 error("key_from_blob: cannot handle type %s", ktype);
727 goto out;
728 }
729 rlen = buffer_len(&b);
730 if (key != NULL && rlen != 0)
731 error("key_from_blob: remaining bytes in key blob %d", rlen);
732 out:
733 if (ktype != NULL)
734 xfree(ktype);
735 buffer_free(&b);
736 return key;
737}
738
739int
740key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
741{
742 Buffer b;
743 int len;
744
745 if (key == NULL) {
746 error("key_to_blob: key == NULL");
747 return 0;
748 }
749 buffer_init(&b);
750 switch (key->type) {
751 case KEY_DSA:
752 buffer_put_cstring(&b, key_ssh_name(key));
753 buffer_put_bignum2(&b, key->dsa->p);
754 buffer_put_bignum2(&b, key->dsa->q);
755 buffer_put_bignum2(&b, key->dsa->g);
756 buffer_put_bignum2(&b, key->dsa->pub_key);
757 break;
758 case KEY_RSA:
759 buffer_put_cstring(&b, key_ssh_name(key));
760 buffer_put_bignum2(&b, key->rsa->e);
761 buffer_put_bignum2(&b, key->rsa->n);
762 break;
763 default:
764 error("key_to_blob: unsupported key type %d", key->type);
765 buffer_free(&b);
766 return 0;
767 }
768 len = buffer_len(&b);
769 if (lenp != NULL)
770 *lenp = len;
771 if (blobp != NULL) {
772 *blobp = xmalloc(len);
773 memcpy(*blobp, buffer_ptr(&b), len);
774 }
775 memset(buffer_ptr(&b), 0, len);
776 buffer_free(&b);
777 return len;
778}
779
780int
781key_sign(
782 const Key *key,
783 u_char **sigp, u_int *lenp,
784 const u_char *data, u_int datalen)
785{
786 switch (key->type) {
787 case KEY_DSA:
788 return ssh_dss_sign(key, sigp, lenp, data, datalen);
789 case KEY_RSA:
790 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
791 default:
792 error("key_sign: invalid key type %d", key->type);
793 return -1;
794 }
795}
796
797/*
798 * key_verify returns 1 for a correct signature, 0 for an incorrect signature
799 * and -1 on error.
800 */
801int
802key_verify(
803 const Key *key,
804 const u_char *signature, u_int signaturelen,
805 const u_char *data, u_int datalen)
806{
807 if (signaturelen == 0)
808 return -1;
809
810 switch (key->type) {
811 case KEY_DSA:
812 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
813 case KEY_RSA:
814 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
815 default:
816 error("key_verify: invalid key type %d", key->type);
817 return -1;
818 }
819}
820
821/* Converts a private to a public key */
822Key *
823key_demote(const Key *k)
824{
825 Key *pk;
826
827 pk = xmalloc(sizeof(*pk));
828 pk->type = k->type;
829 pk->flags = k->flags;
830 pk->dsa = NULL;
831 pk->rsa = NULL;
832
833 switch (k->type) {
834 case KEY_RSA1:
835 case KEY_RSA:
836 if ((pk->rsa = RSA_new()) == NULL)
837 fatal("key_demote: RSA_new failed");
838 if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
839 fatal("key_demote: BN_dup failed");
840 if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
841 fatal("key_demote: BN_dup failed");
842 break;
843 case KEY_DSA:
844 if ((pk->dsa = DSA_new()) == NULL)
845 fatal("key_demote: DSA_new failed");
846 if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
847 fatal("key_demote: BN_dup failed");
848 if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
849 fatal("key_demote: BN_dup failed");
850 if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
851 fatal("key_demote: BN_dup failed");
852 if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
853 fatal("key_demote: BN_dup failed");
854 break;
855 default:
856 fatal("key_free: bad key type %d", k->type);
857 break;
858 }
859
860 return (pk);
861}
This page took 0.044715 seconds and 5 git commands to generate.