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