3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
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".
12 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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.
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.
35 RCSID("$OpenBSD: key.c,v 1.52 2003/05/14 18:16:20 jakob Exp $");
37 #include <openssl/evp.h>
53 k = xmalloc(sizeof(*k));
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");
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");
85 fatal("key_new: bad key type %d", k->type);
92 key_new_private(int type)
94 Key *k = key_new(type);
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");
112 if ((k->dsa->priv_key = BN_new()) == NULL)
113 fatal("key_new_private: BN_new failed");
141 fatal("key_free: bad key type %d", k->type);
147 key_equal(Key *a, Key *b)
149 if (a == NULL || b == NULL || a->type != b->type)
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;
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;
166 fatal("key_equal: bad key type %d", a->type);
173 key_fingerprint_raw(Key *k, enum fp_type dgst_type, u_int *dgst_raw_length)
175 const EVP_MD *md = NULL;
178 u_char *retval = NULL;
182 *dgst_raw_length = 0;
192 fatal("key_fingerprint_raw: bad digest type %d",
197 nlen = BN_num_bytes(k->rsa->n);
198 elen = BN_num_bytes(k->rsa->e);
201 BN_bn2bin(k->rsa->n, blob);
202 BN_bn2bin(k->rsa->e, blob + nlen);
206 key_to_blob(k, &blob, &len);
212 fatal("key_fingerprint_raw: bad key type %d", k->type);
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);
223 fatal("key_fingerprint_raw: blob is null");
229 key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
234 retval = xmalloc(dgst_raw_len * 3 + 1);
236 for (i = 0; i < dgst_raw_len; i++) {
238 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
239 strlcat(retval, hex, dgst_raw_len * 3);
241 retval[(dgst_raw_len * 3) - 1] = '\0';
246 key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
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;
254 rounds = (dgst_raw_len / 2) + 1;
255 retval = xmalloc(sizeof(char) * (rounds*6));
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) +
262 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
263 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
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];
273 retval[j++] = consonants[idx4];
275 ((((u_int)(dgst_raw[2 * i])) * 7) +
276 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
282 retval[j++] = vowels[idx0];
283 retval[j++] = consonants[idx1];
284 retval[j++] = vowels[idx2];
293 key_fingerprint(Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
299 dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
301 fatal("key_fingerprint: null from key_fingerprint_raw()");
304 retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
306 case SSH_FP_BUBBLEBABBLE:
307 retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
310 fatal("key_fingerprint_ex: bad digest representation %d",
314 memset(dgst_raw, 0, dgst_raw_len);
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.
327 read_bignum(char **cpp, BIGNUM * value)
332 /* Skip any leading whitespace. */
333 for (; *cp == ' ' || *cp == '\t'; cp++)
336 /* Check that it begins with a decimal digit. */
337 if (*cp < '0' || *cp > '9')
340 /* Save starting position. */
343 /* Move forward until all decimal digits skipped. */
344 for (; *cp >= '0' && *cp <= '9'; cp++)
347 /* Save the old terminating character, and replace it by \0. */
351 /* Parse the number. */
352 if (BN_dec2bn(&value, *cpp) == 0)
355 /* Restore old terminating character. */
358 /* Move beyond the number and return success. */
364 write_bignum(FILE *f, BIGNUM *num)
366 char *buf = BN_bn2dec(num);
368 error("write_bignum: BN_bn2dec() failed");
371 fprintf(f, " %s", buf);
376 /* returns 1 ok, -1 error */
378 key_read(Key *ret, char **cpp)
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';
399 /* Get public exponent, public modulus. */
400 if (!read_bignum(cpp, ret->rsa->e))
402 if (!read_bignum(cpp, ret->rsa->n))
409 space = strchr(cp, ' ');
411 debug3("key_read: missing whitespace");
415 type = key_type_from_name(cp);
417 if (type == KEY_UNSPEC) {
418 debug3("key_read: missing keytype");
423 debug3("key_read: short string");
426 if (ret->type == KEY_UNSPEC) {
428 } else if (ret->type != type) {
429 /* is a key, but different type */
430 debug3("key_read: type mismatch");
435 n = uudecode(cp, blob, len);
437 error("key_read: uudecode %s failed", cp);
441 k = key_from_blob(blob, n);
444 error("key_read: key_from_blob %s failed", cp);
447 if (k->type != type) {
448 error("key_read: type mismatch: encoding error");
453 if (ret->type == KEY_RSA) {
454 if (ret->rsa != NULL)
460 RSA_print_fp(stderr, ret->rsa, 8);
463 if (ret->dsa != NULL)
469 DSA_print_fp(stderr, ret->dsa, 8);
476 /* advance cp: skip whitespace and data */
477 while (*cp == ' ' || *cp == '\t')
479 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
484 fatal("key_read: bad key type: %d", ret->type);
491 key_write(Key *key, FILE *f)
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)) {
506 error("key_write: failed for RSA key");
508 } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
509 (key->type == KEY_RSA && key->rsa != NULL)) {
510 key_to_blob(key, &blob, &len);
512 n = uuencode(blob, len, uu, 2*len);
514 fprintf(f, "%s %s", key_ssh_name(key), uu);
551 return "ssh-unknown";
560 return BN_num_bits(k->rsa->n);
563 return BN_num_bits(k->dsa->p);
570 rsa_generate_private_key(u_int bits)
573 private = RSA_generate_key(bits, 35, NULL, NULL);
575 fatal("rsa_generate_private_key: key generation failed.");
580 dsa_generate_private_key(u_int bits)
582 DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, 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.");
588 fatal("dsa_generate_private_key: NULL.");
593 key_generate(int type, u_int bits)
595 Key *k = key_new(KEY_UNSPEC);
598 k->dsa = dsa_generate_private_key(bits);
602 k->rsa = rsa_generate_private_key(bits);
605 fatal("key_generate: unknown type %d", type);
612 key_from_private(Key *k)
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);
625 n = key_new(k->type);
626 BN_copy(n->rsa->n, k->rsa->n);
627 BN_copy(n->rsa->e, k->rsa->e);
630 fatal("key_from_private: unknown type %d", k->type);
637 key_type_from_name(char *name)
639 if (strcmp(name, "rsa1") == 0) {
641 } else if (strcmp(name, "rsa") == 0) {
643 } else if (strcmp(name, "dsa") == 0) {
645 } else if (strcmp(name, "ssh-rsa") == 0) {
647 } else if (strcmp(name, "ssh-dss") == 0) {
650 debug2("key_type_from_name: unknown key type '%s'", name);
655 key_names_valid2(const char *names)
659 if (names == NULL || strcmp(names, "") == 0)
661 s = cp = xstrdup(names);
662 for ((p = strsep(&cp, ",")); p && *p != '\0';
663 (p = strsep(&cp, ","))) {
664 switch (key_type_from_name(p)) {
671 debug3("key names ok: [%s]", names);
677 key_from_blob(u_char *blob, int blen)
685 dump_base64(stderr, blob, blen);
688 buffer_append(&b, blob, blen);
689 ktype = buffer_get_string(&b, NULL);
690 type = key_type_from_name(ktype);
695 buffer_get_bignum2(&b, key->rsa->e);
696 buffer_get_bignum2(&b, key->rsa->n);
698 RSA_print_fp(stderr, key->rsa, 8);
703 buffer_get_bignum2(&b, key->dsa->p);
704 buffer_get_bignum2(&b, key->dsa->q);
705 buffer_get_bignum2(&b, key->dsa->g);
706 buffer_get_bignum2(&b, key->dsa->pub_key);
708 DSA_print_fp(stderr, key->dsa, 8);
715 error("key_from_blob: cannot handle type %s", ktype);
718 rlen = buffer_len(&b);
719 if (key != NULL && rlen != 0)
720 error("key_from_blob: remaining bytes in key blob %d", rlen);
727 key_to_blob(Key *key, u_char **blobp, u_int *lenp)
733 error("key_to_blob: key == NULL");
739 buffer_put_cstring(&b, key_ssh_name(key));
740 buffer_put_bignum2(&b, key->dsa->p);
741 buffer_put_bignum2(&b, key->dsa->q);
742 buffer_put_bignum2(&b, key->dsa->g);
743 buffer_put_bignum2(&b, key->dsa->pub_key);
746 buffer_put_cstring(&b, key_ssh_name(key));
747 buffer_put_bignum2(&b, key->rsa->e);
748 buffer_put_bignum2(&b, key->rsa->n);
751 error("key_to_blob: unsupported key type %d", key->type);
755 len = buffer_len(&b);
759 *blobp = xmalloc(len);
760 memcpy(*blobp, buffer_ptr(&b), len);
762 memset(buffer_ptr(&b), 0, len);
770 u_char **sigp, u_int *lenp,
771 u_char *data, u_int datalen)
775 return ssh_dss_sign(key, sigp, lenp, data, datalen);
778 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
781 error("key_sign: illegal key type %d", key->type);
788 * key_verify returns 1 for a correct signature, 0 for an incorrect signature
794 u_char *signature, u_int signaturelen,
795 u_char *data, u_int datalen)
797 if (signaturelen == 0)
802 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
805 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
808 error("key_verify: illegal key type %d", key->type);
814 /* Converts a private to a public key */
820 pk = xmalloc(sizeof(*pk));
822 pk->flags = k->flags;
829 if ((pk->rsa = RSA_new()) == NULL)
830 fatal("key_demote: RSA_new failed");
831 if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
832 fatal("key_demote: BN_dup failed");
833 if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
834 fatal("key_demote: BN_dup failed");
837 if ((pk->dsa = DSA_new()) == NULL)
838 fatal("key_demote: DSA_new failed");
839 if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
840 fatal("key_demote: BN_dup failed");
841 if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
842 fatal("key_demote: BN_dup failed");
843 if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
844 fatal("key_demote: BN_dup failed");
845 if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
846 fatal("key_demote: BN_dup failed");
849 fatal("key_free: bad key type %d", k->type);