]> andersk Git - gssapi-openssh.git/blame - openssh/key.c
Put kexgss.co on its own line to make merging easier.
[gssapi-openssh.git] / openssh / key.c
CommitLineData
fa0f0f45 1/* $OpenBSD: key.c,v 1.69 2007/07/12 05:48:05 ray 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 */
30460aeb 35
3c0ef626 36#include "includes.h"
30460aeb 37
38#include <sys/types.h>
3c0ef626 39
40#include <openssl/evp.h>
41
30460aeb 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;
30460aeb 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:
e9702f7d 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:
e9702f7d 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}
276b07a3 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:
e9702f7d 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:
e9702f7d 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}
276b07a3 127
3c0ef626 128void
129key_free(Key *k)
130{
30460aeb 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}
540d72c3 153
3c0ef626 154int
540d72c3 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);
3c0ef626 173 }
3c0ef626 174}
175
7cac2b65 176u_char*
540d72c3 177key_fingerprint_raw(const Key *k, enum fp_type dgst_type,
178 u_int *dgst_raw_length)
3c0ef626 179{
e9702f7d 180 const EVP_MD *md = NULL;
3c0ef626 181 EVP_MD_CTX ctx;
182 u_char *blob = NULL;
183 u_char *retval = NULL;
e9702f7d 184 u_int len = 0;
3c0ef626 185 int nlen, elen;
186
187 *dgst_raw_length = 0;
188
189 switch (dgst_type) {
190 case SSH_FP_MD5:
191 md = EVP_md5();
192 break;
193 case SSH_FP_SHA1:
194 md = EVP_sha1();
195 break;
196 default:
197 fatal("key_fingerprint_raw: bad digest type %d",
198 dgst_type);
199 }
200 switch (k->type) {
201 case KEY_RSA1:
202 nlen = BN_num_bytes(k->rsa->n);
203 elen = BN_num_bytes(k->rsa->e);
204 len = nlen + elen;
205 blob = xmalloc(len);
206 BN_bn2bin(k->rsa->n, blob);
207 BN_bn2bin(k->rsa->e, blob + nlen);
208 break;
209 case KEY_DSA:
210 case KEY_RSA:
211 key_to_blob(k, &blob, &len);
212 break;
213 case KEY_UNSPEC:
214 return retval;
3c0ef626 215 default:
216 fatal("key_fingerprint_raw: bad key type %d", k->type);
217 break;
218 }
219 if (blob != NULL) {
220 retval = xmalloc(EVP_MAX_MD_SIZE);
221 EVP_DigestInit(&ctx, md);
222 EVP_DigestUpdate(&ctx, blob, len);
e9702f7d 223 EVP_DigestFinal(&ctx, retval, dgst_raw_length);
3c0ef626 224 memset(blob, 0, len);
225 xfree(blob);
226 } else {
227 fatal("key_fingerprint_raw: blob is null");
228 }
229 return retval;
230}
231
d03f4262 232static char *
233key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
3c0ef626 234{
235 char *retval;
2ce0bfe4 236 u_int i;
3c0ef626 237
30460aeb 238 retval = xcalloc(1, dgst_raw_len * 3 + 1);
e9702f7d 239 for (i = 0; i < dgst_raw_len; i++) {
3c0ef626 240 char hex[4];
241 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
7cac2b65 242 strlcat(retval, hex, dgst_raw_len * 3 + 1);
3c0ef626 243 }
7cac2b65 244
245 /* Remove the trailing ':' character */
3c0ef626 246 retval[(dgst_raw_len * 3) - 1] = '\0';
247 return retval;
248}
249
d03f4262 250static char *
251key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
3c0ef626 252{
253 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
254 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
255 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
256 u_int i, j = 0, rounds, seed = 1;
257 char *retval;
258
259 rounds = (dgst_raw_len / 2) + 1;
30460aeb 260 retval = xcalloc((rounds * 6), sizeof(char));
3c0ef626 261 retval[j++] = 'x';
262 for (i = 0; i < rounds; i++) {
263 u_int idx0, idx1, idx2, idx3, idx4;
264 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
265 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
266 seed) % 6;
267 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
268 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
269 (seed / 6)) % 6;
270 retval[j++] = vowels[idx0];
271 retval[j++] = consonants[idx1];
272 retval[j++] = vowels[idx2];
273 if ((i + 1) < rounds) {
274 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
275 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
276 retval[j++] = consonants[idx3];
277 retval[j++] = '-';
278 retval[j++] = consonants[idx4];
279 seed = ((seed * 5) +
280 ((((u_int)(dgst_raw[2 * i])) * 7) +
281 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
282 }
283 } else {
284 idx0 = seed % 6;
285 idx1 = 16;
286 idx2 = seed / 6;
287 retval[j++] = vowels[idx0];
288 retval[j++] = consonants[idx1];
289 retval[j++] = vowels[idx2];
290 }
291 }
292 retval[j++] = 'x';
293 retval[j++] = '\0';
294 return retval;
295}
296
d03f4262 297char *
540d72c3 298key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
3c0ef626 299{
300 char *retval = NULL;
301 u_char *dgst_raw;
e9702f7d 302 u_int dgst_raw_len;
303
3c0ef626 304 dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
305 if (!dgst_raw)
306 fatal("key_fingerprint: null from key_fingerprint_raw()");
e9702f7d 307 switch (dgst_rep) {
3c0ef626 308 case SSH_FP_HEX:
309 retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
310 break;
311 case SSH_FP_BUBBLEBABBLE:
312 retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
313 break;
314 default:
315 fatal("key_fingerprint_ex: bad digest representation %d",
316 dgst_rep);
317 break;
318 }
319 memset(dgst_raw, 0, dgst_raw_len);
320 xfree(dgst_raw);
321 return retval;
322}
323
324/*
325 * Reads a multiple-precision integer in decimal from the buffer, and advances
326 * the pointer. The integer must already be initialized. This function is
327 * permitted to modify the buffer. This leaves *cpp to point just beyond the
328 * last processed (and maybe modified) character. Note that this may modify
329 * the buffer containing the number.
330 */
331static int
332read_bignum(char **cpp, BIGNUM * value)
333{
334 char *cp = *cpp;
335 int old;
336
337 /* Skip any leading whitespace. */
338 for (; *cp == ' ' || *cp == '\t'; cp++)
339 ;
340
341 /* Check that it begins with a decimal digit. */
342 if (*cp < '0' || *cp > '9')
343 return 0;
344
345 /* Save starting position. */
346 *cpp = cp;
347
348 /* Move forward until all decimal digits skipped. */
349 for (; *cp >= '0' && *cp <= '9'; cp++)
350 ;
351
352 /* Save the old terminating character, and replace it by \0. */
353 old = *cp;
354 *cp = 0;
355
356 /* Parse the number. */
357 if (BN_dec2bn(&value, *cpp) == 0)
358 return 0;
359
360 /* Restore old terminating character. */
361 *cp = old;
362
363 /* Move beyond the number and return success. */
364 *cpp = cp;
365 return 1;
366}
276b07a3 367
3c0ef626 368static int
369write_bignum(FILE *f, BIGNUM *num)
370{
371 char *buf = BN_bn2dec(num);
372 if (buf == NULL) {
373 error("write_bignum: BN_bn2dec() failed");
374 return 0;
375 }
376 fprintf(f, " %s", buf);
377 OPENSSL_free(buf);
378 return 1;
379}
380
381/* returns 1 ok, -1 error */
382int
383key_read(Key *ret, char **cpp)
384{
385 Key *k;
386 int success = -1;
387 char *cp, *space;
388 int len, n, type;
389 u_int bits;
390 u_char *blob;
391
392 cp = *cpp;
393
e9702f7d 394 switch (ret->type) {
3c0ef626 395 case KEY_RSA1:
396 /* Get number of bits. */
397 if (*cp < '0' || *cp > '9')
398 return -1; /* Bad bit count... */
399 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
400 bits = 10 * bits + *cp - '0';
401 if (bits == 0)
402 return -1;
403 *cpp = cp;
404 /* Get public exponent, public modulus. */
405 if (!read_bignum(cpp, ret->rsa->e))
406 return -1;
407 if (!read_bignum(cpp, ret->rsa->n))
408 return -1;
409 success = 1;
410 break;
411 case KEY_UNSPEC:
412 case KEY_RSA:
413 case KEY_DSA:
414 space = strchr(cp, ' ');
415 if (space == NULL) {
bfe49944 416 debug3("key_read: missing whitespace");
3c0ef626 417 return -1;
418 }
419 *space = '\0';
420 type = key_type_from_name(cp);
421 *space = ' ';
422 if (type == KEY_UNSPEC) {
bfe49944 423 debug3("key_read: missing keytype");
3c0ef626 424 return -1;
425 }
426 cp = space+1;
427 if (*cp == '\0') {
428 debug3("key_read: short string");
429 return -1;
430 }
431 if (ret->type == KEY_UNSPEC) {
432 ret->type = type;
433 } else if (ret->type != type) {
434 /* is a key, but different type */
435 debug3("key_read: type mismatch");
436 return -1;
437 }
438 len = 2*strlen(cp);
439 blob = xmalloc(len);
440 n = uudecode(cp, blob, len);
441 if (n < 0) {
442 error("key_read: uudecode %s failed", cp);
e9702f7d 443 xfree(blob);
3c0ef626 444 return -1;
445 }
7cac2b65 446 k = key_from_blob(blob, (u_int)n);
e9702f7d 447 xfree(blob);
3c0ef626 448 if (k == NULL) {
449 error("key_read: key_from_blob %s failed", cp);
450 return -1;
451 }
3c0ef626 452 if (k->type != type) {
453 error("key_read: type mismatch: encoding error");
454 key_free(k);
455 return -1;
456 }
457/*XXXX*/
458 if (ret->type == KEY_RSA) {
459 if (ret->rsa != NULL)
460 RSA_free(ret->rsa);
461 ret->rsa = k->rsa;
462 k->rsa = NULL;
463 success = 1;
464#ifdef DEBUG_PK
465 RSA_print_fp(stderr, ret->rsa, 8);
466#endif
467 } else {
468 if (ret->dsa != NULL)
469 DSA_free(ret->dsa);
470 ret->dsa = k->dsa;
471 k->dsa = NULL;
472 success = 1;
473#ifdef DEBUG_PK
474 DSA_print_fp(stderr, ret->dsa, 8);
475#endif
476 }
477/*XXXX*/
e9702f7d 478 key_free(k);
3c0ef626 479 if (success != 1)
480 break;
3c0ef626 481 /* advance cp: skip whitespace and data */
482 while (*cp == ' ' || *cp == '\t')
483 cp++;
484 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
485 cp++;
486 *cpp = cp;
487 break;
488 default:
489 fatal("key_read: bad key type: %d", ret->type);
490 break;
491 }
492 return success;
493}
276b07a3 494
3c0ef626 495int
540d72c3 496key_write(const Key *key, FILE *f)
3c0ef626 497{
e9702f7d 498 int n, success = 0;
499 u_int len, bits = 0;
d03f4262 500 u_char *blob;
501 char *uu;
3c0ef626 502
503 if (key->type == KEY_RSA1 && key->rsa != NULL) {
504 /* size of modulus 'n' */
505 bits = BN_num_bits(key->rsa->n);
506 fprintf(f, "%u", bits);
507 if (write_bignum(f, key->rsa->e) &&
508 write_bignum(f, key->rsa->n)) {
509 success = 1;
510 } else {
511 error("key_write: failed for RSA key");
512 }
513 } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
514 (key->type == KEY_RSA && key->rsa != NULL)) {
3c0ef626 515 key_to_blob(key, &blob, &len);
516 uu = xmalloc(2*len);
517 n = uuencode(blob, len, uu, 2*len);
518 if (n > 0) {
519 fprintf(f, "%s %s", key_ssh_name(key), uu);
520 success = 1;
521 }
522 xfree(blob);
523 xfree(uu);
524 }
525 return success;
526}
276b07a3 527
540d72c3 528const char *
529key_type(const Key *k)
3c0ef626 530{
531 switch (k->type) {
532 case KEY_RSA1:
533 return "RSA1";
3c0ef626 534 case KEY_RSA:
535 return "RSA";
3c0ef626 536 case KEY_DSA:
537 return "DSA";
3c0ef626 538 }
539 return "unknown";
540}
276b07a3 541
540d72c3 542const char *
543key_ssh_name(const Key *k)
3c0ef626 544{
545 switch (k->type) {
546 case KEY_RSA:
547 return "ssh-rsa";
3c0ef626 548 case KEY_DSA:
549 return "ssh-dss";
3c0ef626 550 }
551 return "ssh-unknown";
552}
276b07a3 553
3c0ef626 554u_int
540d72c3 555key_size(const Key *k)
e9702f7d 556{
3c0ef626 557 switch (k->type) {
558 case KEY_RSA1:
559 case KEY_RSA:
560 return BN_num_bits(k->rsa->n);
3c0ef626 561 case KEY_DSA:
562 return BN_num_bits(k->dsa->p);
3c0ef626 563 }
564 return 0;
565}
566
567static RSA *
568rsa_generate_private_key(u_int bits)
569{
570 RSA *private;
30460aeb 571
3c0ef626 572 private = RSA_generate_key(bits, 35, NULL, NULL);
573 if (private == NULL)
574 fatal("rsa_generate_private_key: key generation failed.");
575 return private;
576}
577
578static DSA*
579dsa_generate_private_key(u_int bits)
580{
581 DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
30460aeb 582
3c0ef626 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 *
540d72c3 612key_from_private(const Key *k)
3c0ef626 613{
614 Key *n = NULL;
615 switch (k->type) {
616 case KEY_DSA:
617 n = key_new(k->type);
240debe0 618 if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
619 (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
620 (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
621 (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL))
622 fatal("key_from_private: BN_copy failed");
3c0ef626 623 break;
624 case KEY_RSA:
625 case KEY_RSA1:
626 n = key_new(k->type);
240debe0 627 if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
628 (BN_copy(n->rsa->e, k->rsa->e) == NULL))
629 fatal("key_from_private: BN_copy failed");
3c0ef626 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{
e9702f7d 641 if (strcmp(name, "rsa1") == 0) {
3c0ef626 642 return KEY_RSA1;
e9702f7d 643 } else if (strcmp(name, "rsa") == 0) {
3c0ef626 644 return KEY_RSA;
e9702f7d 645 } else if (strcmp(name, "dsa") == 0) {
3c0ef626 646 return KEY_DSA;
e9702f7d 647 } else if (strcmp(name, "ssh-rsa") == 0) {
3c0ef626 648 return KEY_RSA;
e9702f7d 649 } else if (strcmp(name, "ssh-dss") == 0) {
3c0ef626 650 return KEY_DSA;
fe4ad273 651 } else if (strcmp(name, "null") == 0) {
5598e598 652 return KEY_NULL;
3c0ef626 653 }
654 debug2("key_type_from_name: unknown key type '%s'", name);
655 return KEY_UNSPEC;
656}
657
658int
659key_names_valid2(const char *names)
660{
661 char *s, *cp, *p;
662
663 if (names == NULL || strcmp(names, "") == 0)
664 return 0;
665 s = cp = xstrdup(names);
666 for ((p = strsep(&cp, ",")); p && *p != '\0';
e9702f7d 667 (p = strsep(&cp, ","))) {
3c0ef626 668 switch (key_type_from_name(p)) {
669 case KEY_RSA1:
670 case KEY_UNSPEC:
671 xfree(s);
672 return 0;
673 }
674 }
675 debug3("key names ok: [%s]", names);
676 xfree(s);
677 return 1;
678}
679
680Key *
540d72c3 681key_from_blob(const u_char *blob, u_int blen)
3c0ef626 682{
683 Buffer b;
3c0ef626 684 int rlen, type;
dfddba3d 685 char *ktype = NULL;
3c0ef626 686 Key *key = NULL;
687
688#ifdef DEBUG_PK
689 dump_base64(stderr, blob, blen);
690#endif
691 buffer_init(&b);
692 buffer_append(&b, blob, blen);
dfddba3d 693 if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
694 error("key_from_blob: can't read key type");
695 goto out;
696 }
697
3c0ef626 698 type = key_type_from_name(ktype);
699
e9702f7d 700 switch (type) {
3c0ef626 701 case KEY_RSA:
702 key = key_new(type);
dfddba3d 703 if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
704 buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
705 error("key_from_blob: can't read rsa key");
706 key_free(key);
707 key = NULL;
708 goto out;
709 }
3c0ef626 710#ifdef DEBUG_PK
711 RSA_print_fp(stderr, key->rsa, 8);
712#endif
713 break;
714 case KEY_DSA:
715 key = key_new(type);
dfddba3d 716 if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
717 buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
718 buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
719 buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
720 error("key_from_blob: can't read dsa key");
721 key_free(key);
722 key = NULL;
723 goto out;
724 }
3c0ef626 725#ifdef DEBUG_PK
726 DSA_print_fp(stderr, key->dsa, 8);
727#endif
728 break;
729 case KEY_UNSPEC:
730 key = key_new(type);
731 break;
732 default:
733 error("key_from_blob: cannot handle type %s", ktype);
dfddba3d 734 goto out;
3c0ef626 735 }
736 rlen = buffer_len(&b);
737 if (key != NULL && rlen != 0)
738 error("key_from_blob: remaining bytes in key blob %d", rlen);
dfddba3d 739 out:
740 if (ktype != NULL)
741 xfree(ktype);
3c0ef626 742 buffer_free(&b);
743 return key;
744}
745
746int
540d72c3 747key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
3c0ef626 748{
749 Buffer b;
750 int len;
3c0ef626 751
752 if (key == NULL) {
753 error("key_to_blob: key == NULL");
754 return 0;
755 }
756 buffer_init(&b);
e9702f7d 757 switch (key->type) {
3c0ef626 758 case KEY_DSA:
759 buffer_put_cstring(&b, key_ssh_name(key));
760 buffer_put_bignum2(&b, key->dsa->p);
761 buffer_put_bignum2(&b, key->dsa->q);
762 buffer_put_bignum2(&b, key->dsa->g);
763 buffer_put_bignum2(&b, key->dsa->pub_key);
764 break;
765 case KEY_RSA:
766 buffer_put_cstring(&b, key_ssh_name(key));
767 buffer_put_bignum2(&b, key->rsa->e);
768 buffer_put_bignum2(&b, key->rsa->n);
769 break;
770 default:
771 error("key_to_blob: unsupported key type %d", key->type);
772 buffer_free(&b);
773 return 0;
774 }
775 len = buffer_len(&b);
3c0ef626 776 if (lenp != NULL)
777 *lenp = len;
d03f4262 778 if (blobp != NULL) {
779 *blobp = xmalloc(len);
780 memcpy(*blobp, buffer_ptr(&b), len);
781 }
782 memset(buffer_ptr(&b), 0, len);
783 buffer_free(&b);
3c0ef626 784 return len;
785}
786
787int
788key_sign(
540d72c3 789 const Key *key,
e9702f7d 790 u_char **sigp, u_int *lenp,
540d72c3 791 const u_char *data, u_int datalen)
3c0ef626 792{
e9702f7d 793 switch (key->type) {
3c0ef626 794 case KEY_DSA:
795 return ssh_dss_sign(key, sigp, lenp, data, datalen);
3c0ef626 796 case KEY_RSA:
797 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
3c0ef626 798 default:
7e82606e 799 error("key_sign: invalid key type %d", key->type);
3c0ef626 800 return -1;
3c0ef626 801 }
802}
803
44a053a3 804/*
805 * key_verify returns 1 for a correct signature, 0 for an incorrect signature
806 * and -1 on error.
807 */
3c0ef626 808int
809key_verify(
540d72c3 810 const Key *key,
811 const u_char *signature, u_int signaturelen,
812 const u_char *data, u_int datalen)
3c0ef626 813{
814 if (signaturelen == 0)
815 return -1;
816
e9702f7d 817 switch (key->type) {
3c0ef626 818 case KEY_DSA:
819 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
3c0ef626 820 case KEY_RSA:
821 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
3c0ef626 822 default:
7e82606e 823 error("key_verify: invalid key type %d", key->type);
3c0ef626 824 return -1;
3c0ef626 825 }
826}
350391c5 827
828/* Converts a private to a public key */
350391c5 829Key *
540d72c3 830key_demote(const Key *k)
350391c5 831{
832 Key *pk;
833
30460aeb 834 pk = xcalloc(1, sizeof(*pk));
350391c5 835 pk->type = k->type;
836 pk->flags = k->flags;
837 pk->dsa = NULL;
838 pk->rsa = NULL;
839
840 switch (k->type) {
841 case KEY_RSA1:
842 case KEY_RSA:
843 if ((pk->rsa = RSA_new()) == NULL)
844 fatal("key_demote: RSA_new failed");
845 if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
846 fatal("key_demote: BN_dup failed");
847 if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
848 fatal("key_demote: BN_dup failed");
849 break;
850 case KEY_DSA:
851 if ((pk->dsa = DSA_new()) == NULL)
852 fatal("key_demote: DSA_new failed");
853 if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
854 fatal("key_demote: BN_dup failed");
855 if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
856 fatal("key_demote: BN_dup failed");
857 if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
858 fatal("key_demote: BN_dup failed");
859 if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
860 fatal("key_demote: BN_dup failed");
861 break;
862 default:
863 fatal("key_free: bad key type %d", k->type);
864 break;
865 }
866
867 return (pk);
868}
This page took 0.210931 seconds and 5 git commands to generate.