17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * read_bignum():
37c478bd9Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software
67c478bd9Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this
77c478bd9Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is
87c478bd9Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be
97c478bd9Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell".
107c478bd9Sstevel@tonic-gate *
117c478bd9Sstevel@tonic-gate *
127c478bd9Sstevel@tonic-gate * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
157c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
167c478bd9Sstevel@tonic-gate * are met:
177c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
187c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
197c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
207c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
217c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
227c478bd9Sstevel@tonic-gate *
237c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
247c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
257c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
267c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
277c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
287c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
297c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
307c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
317c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
327c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate #include "includes.h"
357c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: key.c,v 1.49 2002/09/09 14:54:14 markus Exp $");
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #include <openssl/evp.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include "xmalloc.h"
427c478bd9Sstevel@tonic-gate #include "key.h"
437c478bd9Sstevel@tonic-gate #include "rsa.h"
447c478bd9Sstevel@tonic-gate #include "ssh-dss.h"
457c478bd9Sstevel@tonic-gate #include "ssh-rsa.h"
467c478bd9Sstevel@tonic-gate #include "uuencode.h"
477c478bd9Sstevel@tonic-gate #include "buffer.h"
487c478bd9Sstevel@tonic-gate #include "bufaux.h"
497c478bd9Sstevel@tonic-gate #include "log.h"
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate Key *
key_new(int type)527c478bd9Sstevel@tonic-gate key_new(int type)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate Key *k;
557c478bd9Sstevel@tonic-gate RSA *rsa;
567c478bd9Sstevel@tonic-gate DSA *dsa;
577c478bd9Sstevel@tonic-gate k = xmalloc(sizeof(*k));
587c478bd9Sstevel@tonic-gate k->type = type;
597c478bd9Sstevel@tonic-gate k->flags = 0;
607c478bd9Sstevel@tonic-gate k->dsa = NULL;
617c478bd9Sstevel@tonic-gate k->rsa = NULL;
627c478bd9Sstevel@tonic-gate switch (k->type) {
637c478bd9Sstevel@tonic-gate case KEY_RSA1:
647c478bd9Sstevel@tonic-gate case KEY_RSA:
657c478bd9Sstevel@tonic-gate if ((rsa = RSA_new()) == NULL)
667c478bd9Sstevel@tonic-gate fatal("key_new: RSA_new failed");
677c478bd9Sstevel@tonic-gate if ((rsa->n = BN_new()) == NULL)
687c478bd9Sstevel@tonic-gate fatal("key_new: BN_new failed");
697c478bd9Sstevel@tonic-gate if ((rsa->e = BN_new()) == NULL)
707c478bd9Sstevel@tonic-gate fatal("key_new: BN_new failed");
717c478bd9Sstevel@tonic-gate k->rsa = rsa;
727c478bd9Sstevel@tonic-gate break;
737c478bd9Sstevel@tonic-gate case KEY_DSA:
747c478bd9Sstevel@tonic-gate if ((dsa = DSA_new()) == NULL)
757c478bd9Sstevel@tonic-gate fatal("key_new: DSA_new failed");
767c478bd9Sstevel@tonic-gate if ((dsa->p = BN_new()) == NULL)
777c478bd9Sstevel@tonic-gate fatal("key_new: BN_new failed");
787c478bd9Sstevel@tonic-gate if ((dsa->q = BN_new()) == NULL)
797c478bd9Sstevel@tonic-gate fatal("key_new: BN_new failed");
807c478bd9Sstevel@tonic-gate if ((dsa->g = BN_new()) == NULL)
817c478bd9Sstevel@tonic-gate fatal("key_new: BN_new failed");
827c478bd9Sstevel@tonic-gate if ((dsa->pub_key = BN_new()) == NULL)
837c478bd9Sstevel@tonic-gate fatal("key_new: BN_new failed");
847c478bd9Sstevel@tonic-gate k->dsa = dsa;
857c478bd9Sstevel@tonic-gate break;
867c478bd9Sstevel@tonic-gate case KEY_UNSPEC:
877c478bd9Sstevel@tonic-gate break;
887c478bd9Sstevel@tonic-gate default:
897c478bd9Sstevel@tonic-gate fatal("key_new: bad key type %d", k->type);
907c478bd9Sstevel@tonic-gate break;
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate return k;
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate Key *
key_new_private(int type)967c478bd9Sstevel@tonic-gate key_new_private(int type)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate Key *k = key_new(type);
997c478bd9Sstevel@tonic-gate switch (k->type) {
1007c478bd9Sstevel@tonic-gate case KEY_RSA1:
1017c478bd9Sstevel@tonic-gate case KEY_RSA:
1027c478bd9Sstevel@tonic-gate if ((k->rsa->d = BN_new()) == NULL)
1037c478bd9Sstevel@tonic-gate fatal("key_new_private: BN_new failed");
1047c478bd9Sstevel@tonic-gate if ((k->rsa->iqmp = BN_new()) == NULL)
1057c478bd9Sstevel@tonic-gate fatal("key_new_private: BN_new failed");
1067c478bd9Sstevel@tonic-gate if ((k->rsa->q = BN_new()) == NULL)
1077c478bd9Sstevel@tonic-gate fatal("key_new_private: BN_new failed");
1087c478bd9Sstevel@tonic-gate if ((k->rsa->p = BN_new()) == NULL)
1097c478bd9Sstevel@tonic-gate fatal("key_new_private: BN_new failed");
1107c478bd9Sstevel@tonic-gate if ((k->rsa->dmq1 = BN_new()) == NULL)
1117c478bd9Sstevel@tonic-gate fatal("key_new_private: BN_new failed");
1127c478bd9Sstevel@tonic-gate if ((k->rsa->dmp1 = BN_new()) == NULL)
1137c478bd9Sstevel@tonic-gate fatal("key_new_private: BN_new failed");
1147c478bd9Sstevel@tonic-gate break;
1157c478bd9Sstevel@tonic-gate case KEY_DSA:
1167c478bd9Sstevel@tonic-gate if ((k->dsa->priv_key = BN_new()) == NULL)
1177c478bd9Sstevel@tonic-gate fatal("key_new_private: BN_new failed");
1187c478bd9Sstevel@tonic-gate break;
1197c478bd9Sstevel@tonic-gate case KEY_UNSPEC:
1207c478bd9Sstevel@tonic-gate break;
1217c478bd9Sstevel@tonic-gate default:
1227c478bd9Sstevel@tonic-gate break;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate return k;
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate void
key_free(Key * k)1287c478bd9Sstevel@tonic-gate key_free(Key *k)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate switch (k->type) {
1317c478bd9Sstevel@tonic-gate case KEY_RSA1:
1327c478bd9Sstevel@tonic-gate case KEY_RSA:
1337c478bd9Sstevel@tonic-gate if (k->rsa != NULL)
1347c478bd9Sstevel@tonic-gate RSA_free(k->rsa);
1357c478bd9Sstevel@tonic-gate k->rsa = NULL;
1367c478bd9Sstevel@tonic-gate break;
1377c478bd9Sstevel@tonic-gate case KEY_DSA:
1387c478bd9Sstevel@tonic-gate if (k->dsa != NULL)
1397c478bd9Sstevel@tonic-gate DSA_free(k->dsa);
1407c478bd9Sstevel@tonic-gate k->dsa = NULL;
1417c478bd9Sstevel@tonic-gate break;
1427c478bd9Sstevel@tonic-gate case KEY_UNSPEC:
1437c478bd9Sstevel@tonic-gate break;
1447c478bd9Sstevel@tonic-gate default:
1457c478bd9Sstevel@tonic-gate fatal("key_free: bad key type %d", k->type);
1467c478bd9Sstevel@tonic-gate break;
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate xfree(k);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate int
key_equal(const Key * a,const Key * b)151*442d23f4Sjp161948 key_equal(const Key *a, const Key *b)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate if (a == NULL || b == NULL || a->type != b->type)
1547c478bd9Sstevel@tonic-gate return 0;
1557c478bd9Sstevel@tonic-gate switch (a->type) {
1567c478bd9Sstevel@tonic-gate case KEY_RSA1:
1577c478bd9Sstevel@tonic-gate case KEY_RSA:
1587c478bd9Sstevel@tonic-gate return a->rsa != NULL && b->rsa != NULL &&
1597c478bd9Sstevel@tonic-gate BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
1607c478bd9Sstevel@tonic-gate BN_cmp(a->rsa->n, b->rsa->n) == 0;
1617c478bd9Sstevel@tonic-gate break;
1627c478bd9Sstevel@tonic-gate case KEY_DSA:
1637c478bd9Sstevel@tonic-gate return a->dsa != NULL && b->dsa != NULL &&
1647c478bd9Sstevel@tonic-gate BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
1657c478bd9Sstevel@tonic-gate BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
1667c478bd9Sstevel@tonic-gate BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
1677c478bd9Sstevel@tonic-gate BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
1687c478bd9Sstevel@tonic-gate break;
1697c478bd9Sstevel@tonic-gate default:
1707c478bd9Sstevel@tonic-gate fatal("key_equal: bad key type %d", a->type);
1717c478bd9Sstevel@tonic-gate break;
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate return 0;
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate static u_char *
key_fingerprint_raw(Key * k,enum fp_type dgst_type,u_int * dgst_raw_length)1777c478bd9Sstevel@tonic-gate key_fingerprint_raw(Key *k, enum fp_type dgst_type, u_int *dgst_raw_length)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate const EVP_MD *md = NULL;
1807c478bd9Sstevel@tonic-gate EVP_MD_CTX ctx;
1817c478bd9Sstevel@tonic-gate u_char *blob = NULL;
1827c478bd9Sstevel@tonic-gate u_char *retval = NULL;
1837c478bd9Sstevel@tonic-gate u_int len = 0;
1847c478bd9Sstevel@tonic-gate int nlen, elen;
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate *dgst_raw_length = 0;
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate switch (dgst_type) {
1897c478bd9Sstevel@tonic-gate case SSH_FP_MD5:
1907c478bd9Sstevel@tonic-gate md = EVP_md5();
1917c478bd9Sstevel@tonic-gate break;
1927c478bd9Sstevel@tonic-gate case SSH_FP_SHA1:
1937c478bd9Sstevel@tonic-gate md = EVP_sha1();
1947c478bd9Sstevel@tonic-gate break;
1957c478bd9Sstevel@tonic-gate default:
1967c478bd9Sstevel@tonic-gate fatal("key_fingerprint_raw: bad digest type %d",
1977c478bd9Sstevel@tonic-gate dgst_type);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate switch (k->type) {
2007c478bd9Sstevel@tonic-gate case KEY_RSA1:
2017c478bd9Sstevel@tonic-gate nlen = BN_num_bytes(k->rsa->n);
2027c478bd9Sstevel@tonic-gate elen = BN_num_bytes(k->rsa->e);
2037c478bd9Sstevel@tonic-gate len = nlen + elen;
2047c478bd9Sstevel@tonic-gate blob = xmalloc(len);
2057c478bd9Sstevel@tonic-gate BN_bn2bin(k->rsa->n, blob);
2067c478bd9Sstevel@tonic-gate BN_bn2bin(k->rsa->e, blob + nlen);
2077c478bd9Sstevel@tonic-gate break;
2087c478bd9Sstevel@tonic-gate case KEY_DSA:
2097c478bd9Sstevel@tonic-gate case KEY_RSA:
2107c478bd9Sstevel@tonic-gate key_to_blob(k, &blob, &len);
2117c478bd9Sstevel@tonic-gate break;
2127c478bd9Sstevel@tonic-gate case KEY_UNSPEC:
2137c478bd9Sstevel@tonic-gate return retval;
2147c478bd9Sstevel@tonic-gate break;
2157c478bd9Sstevel@tonic-gate default:
2167c478bd9Sstevel@tonic-gate fatal("key_fingerprint_raw: bad key type %d", k->type);
2177c478bd9Sstevel@tonic-gate break;
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate if (blob != NULL) {
2207c478bd9Sstevel@tonic-gate retval = xmalloc(EVP_MAX_MD_SIZE);
2217c478bd9Sstevel@tonic-gate EVP_DigestInit(&ctx, md);
2227c478bd9Sstevel@tonic-gate EVP_DigestUpdate(&ctx, blob, len);
2237c478bd9Sstevel@tonic-gate EVP_DigestFinal(&ctx, retval, dgst_raw_length);
2247c478bd9Sstevel@tonic-gate memset(blob, 0, len);
2257c478bd9Sstevel@tonic-gate xfree(blob);
2267c478bd9Sstevel@tonic-gate } else {
2277c478bd9Sstevel@tonic-gate fatal("key_fingerprint_raw: blob is null");
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate return retval;
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate static char *
key_fingerprint_hex(u_char * dgst_raw,u_int dgst_raw_len)2337c478bd9Sstevel@tonic-gate key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate char *retval;
2367c478bd9Sstevel@tonic-gate int i;
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate retval = xmalloc(dgst_raw_len * 3 + 1);
2397c478bd9Sstevel@tonic-gate retval[0] = '\0';
2407c478bd9Sstevel@tonic-gate for (i = 0; i < dgst_raw_len; i++) {
2417c478bd9Sstevel@tonic-gate char hex[4];
2427c478bd9Sstevel@tonic-gate snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
2437c478bd9Sstevel@tonic-gate strlcat(retval, hex, dgst_raw_len * 3);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate retval[(dgst_raw_len * 3) - 1] = '\0';
2467c478bd9Sstevel@tonic-gate return retval;
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate static char *
key_fingerprint_bubblebabble(u_char * dgst_raw,u_int dgst_raw_len)2507c478bd9Sstevel@tonic-gate key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
2537c478bd9Sstevel@tonic-gate char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
2547c478bd9Sstevel@tonic-gate 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
2557c478bd9Sstevel@tonic-gate u_int i, j = 0, rounds, seed = 1;
2567c478bd9Sstevel@tonic-gate char *retval;
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate rounds = (dgst_raw_len / 2) + 1;
2597c478bd9Sstevel@tonic-gate retval = xmalloc(sizeof(char) * (rounds*6));
2607c478bd9Sstevel@tonic-gate retval[j++] = 'x';
2617c478bd9Sstevel@tonic-gate for (i = 0; i < rounds; i++) {
2627c478bd9Sstevel@tonic-gate u_int idx0, idx1, idx2, idx3, idx4;
2637c478bd9Sstevel@tonic-gate if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
2647c478bd9Sstevel@tonic-gate idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
2657c478bd9Sstevel@tonic-gate seed) % 6;
2667c478bd9Sstevel@tonic-gate idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
2677c478bd9Sstevel@tonic-gate idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
2687c478bd9Sstevel@tonic-gate (seed / 6)) % 6;
2697c478bd9Sstevel@tonic-gate retval[j++] = vowels[idx0];
2707c478bd9Sstevel@tonic-gate retval[j++] = consonants[idx1];
2717c478bd9Sstevel@tonic-gate retval[j++] = vowels[idx2];
2727c478bd9Sstevel@tonic-gate if ((i + 1) < rounds) {
2737c478bd9Sstevel@tonic-gate idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
2747c478bd9Sstevel@tonic-gate idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
2757c478bd9Sstevel@tonic-gate retval[j++] = consonants[idx3];
2767c478bd9Sstevel@tonic-gate retval[j++] = '-';
2777c478bd9Sstevel@tonic-gate retval[j++] = consonants[idx4];
2787c478bd9Sstevel@tonic-gate seed = ((seed * 5) +
2797c478bd9Sstevel@tonic-gate ((((u_int)(dgst_raw[2 * i])) * 7) +
2807c478bd9Sstevel@tonic-gate ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate } else {
2837c478bd9Sstevel@tonic-gate idx0 = seed % 6;
2847c478bd9Sstevel@tonic-gate idx1 = 16;
2857c478bd9Sstevel@tonic-gate idx2 = seed / 6;
2867c478bd9Sstevel@tonic-gate retval[j++] = vowels[idx0];
2877c478bd9Sstevel@tonic-gate retval[j++] = consonants[idx1];
2887c478bd9Sstevel@tonic-gate retval[j++] = vowels[idx2];
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate retval[j++] = 'x';
2927c478bd9Sstevel@tonic-gate retval[j++] = '\0';
2937c478bd9Sstevel@tonic-gate return retval;
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate char *
key_fingerprint(Key * k,enum fp_type dgst_type,enum fp_rep dgst_rep)2977c478bd9Sstevel@tonic-gate key_fingerprint(Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate char *retval = NULL;
3007c478bd9Sstevel@tonic-gate u_char *dgst_raw;
3017c478bd9Sstevel@tonic-gate u_int dgst_raw_len;
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
3047c478bd9Sstevel@tonic-gate if (!dgst_raw)
3057c478bd9Sstevel@tonic-gate fatal("key_fingerprint: null from key_fingerprint_raw()");
3067c478bd9Sstevel@tonic-gate switch (dgst_rep) {
3077c478bd9Sstevel@tonic-gate case SSH_FP_HEX:
3087c478bd9Sstevel@tonic-gate retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
3097c478bd9Sstevel@tonic-gate break;
3107c478bd9Sstevel@tonic-gate case SSH_FP_BUBBLEBABBLE:
3117c478bd9Sstevel@tonic-gate retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
3127c478bd9Sstevel@tonic-gate break;
3137c478bd9Sstevel@tonic-gate default:
3147c478bd9Sstevel@tonic-gate fatal("key_fingerprint_ex: bad digest representation %d",
3157c478bd9Sstevel@tonic-gate dgst_rep);
3167c478bd9Sstevel@tonic-gate break;
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate memset(dgst_raw, 0, dgst_raw_len);
3197c478bd9Sstevel@tonic-gate xfree(dgst_raw);
3207c478bd9Sstevel@tonic-gate return retval;
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate /*
3247c478bd9Sstevel@tonic-gate * Reads a multiple-precision integer in decimal from the buffer, and advances
3257c478bd9Sstevel@tonic-gate * the pointer. The integer must already be initialized. This function is
3267c478bd9Sstevel@tonic-gate * permitted to modify the buffer. This leaves *cpp to point just beyond the
3277c478bd9Sstevel@tonic-gate * last processed (and maybe modified) character. Note that this may modify
3287c478bd9Sstevel@tonic-gate * the buffer containing the number.
3297c478bd9Sstevel@tonic-gate */
3307c478bd9Sstevel@tonic-gate static int
read_bignum(char ** cpp,BIGNUM * value)3317c478bd9Sstevel@tonic-gate read_bignum(char **cpp, BIGNUM * value)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate char *cp = *cpp;
3347c478bd9Sstevel@tonic-gate int old;
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate /* Skip any leading whitespace. */
3377c478bd9Sstevel@tonic-gate for (; *cp == ' ' || *cp == '\t'; cp++)
3387c478bd9Sstevel@tonic-gate ;
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate /* Check that it begins with a decimal digit. */
3417c478bd9Sstevel@tonic-gate if (*cp < '0' || *cp > '9')
3427c478bd9Sstevel@tonic-gate return 0;
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate /* Save starting position. */
3457c478bd9Sstevel@tonic-gate *cpp = cp;
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate /* Move forward until all decimal digits skipped. */
3487c478bd9Sstevel@tonic-gate for (; *cp >= '0' && *cp <= '9'; cp++)
3497c478bd9Sstevel@tonic-gate ;
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate /* Save the old terminating character, and replace it by \0. */
3527c478bd9Sstevel@tonic-gate old = *cp;
3537c478bd9Sstevel@tonic-gate *cp = 0;
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate /* Parse the number. */
3567c478bd9Sstevel@tonic-gate if (BN_dec2bn(&value, *cpp) == 0)
3577c478bd9Sstevel@tonic-gate return 0;
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate /* Restore old terminating character. */
3607c478bd9Sstevel@tonic-gate *cp = old;
3617c478bd9Sstevel@tonic-gate
3627c478bd9Sstevel@tonic-gate /* Move beyond the number and return success. */
3637c478bd9Sstevel@tonic-gate *cpp = cp;
3647c478bd9Sstevel@tonic-gate return 1;
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate static int
write_bignum(FILE * f,BIGNUM * num)3687c478bd9Sstevel@tonic-gate write_bignum(FILE *f, BIGNUM *num)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate char *buf = BN_bn2dec(num);
3717c478bd9Sstevel@tonic-gate if (buf == NULL) {
3727c478bd9Sstevel@tonic-gate error("write_bignum: BN_bn2dec() failed");
3737c478bd9Sstevel@tonic-gate return 0;
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate fprintf(f, " %s", buf);
3767c478bd9Sstevel@tonic-gate OPENSSL_free(buf);
3777c478bd9Sstevel@tonic-gate return 1;
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate /* returns 1 ok, -1 error */
3817c478bd9Sstevel@tonic-gate int
key_read(Key * ret,char ** cpp)3827c478bd9Sstevel@tonic-gate key_read(Key *ret, char **cpp)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate Key *k;
3857c478bd9Sstevel@tonic-gate int success = -1;
3867c478bd9Sstevel@tonic-gate char *cp, *space;
3877c478bd9Sstevel@tonic-gate int len, n, type;
3887c478bd9Sstevel@tonic-gate u_int bits;
3897c478bd9Sstevel@tonic-gate u_char *blob;
3907c478bd9Sstevel@tonic-gate
3917c478bd9Sstevel@tonic-gate cp = *cpp;
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate switch (ret->type) {
3947c478bd9Sstevel@tonic-gate case KEY_RSA1:
3957c478bd9Sstevel@tonic-gate /* Get number of bits. */
3967c478bd9Sstevel@tonic-gate if (*cp < '0' || *cp > '9')
3977c478bd9Sstevel@tonic-gate return -1; /* Bad bit count... */
3987c478bd9Sstevel@tonic-gate for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
3997c478bd9Sstevel@tonic-gate bits = 10 * bits + *cp - '0';
4007c478bd9Sstevel@tonic-gate if (bits == 0)
4017c478bd9Sstevel@tonic-gate return -1;
4027c478bd9Sstevel@tonic-gate *cpp = cp;
4037c478bd9Sstevel@tonic-gate /* Get public exponent, public modulus. */
4047c478bd9Sstevel@tonic-gate if (!read_bignum(cpp, ret->rsa->e))
4057c478bd9Sstevel@tonic-gate return -1;
4067c478bd9Sstevel@tonic-gate if (!read_bignum(cpp, ret->rsa->n))
4077c478bd9Sstevel@tonic-gate return -1;
4087c478bd9Sstevel@tonic-gate success = 1;
4097c478bd9Sstevel@tonic-gate break;
4107c478bd9Sstevel@tonic-gate case KEY_UNSPEC:
4117c478bd9Sstevel@tonic-gate case KEY_RSA:
4127c478bd9Sstevel@tonic-gate case KEY_DSA:
4137c478bd9Sstevel@tonic-gate space = strchr(cp, ' ');
4147c478bd9Sstevel@tonic-gate if (space == NULL) {
4157c478bd9Sstevel@tonic-gate debug3("key_read: no space");
4167c478bd9Sstevel@tonic-gate return -1;
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate *space = '\0';
4197c478bd9Sstevel@tonic-gate type = key_type_from_name(cp);
4207c478bd9Sstevel@tonic-gate *space = ' ';
4217c478bd9Sstevel@tonic-gate if (type == KEY_UNSPEC) {
4227c478bd9Sstevel@tonic-gate debug3("key_read: no key found");
4237c478bd9Sstevel@tonic-gate return -1;
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate cp = space+1;
4267c478bd9Sstevel@tonic-gate if (*cp == '\0') {
4277c478bd9Sstevel@tonic-gate debug3("key_read: short string");
4287c478bd9Sstevel@tonic-gate return -1;
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate if (ret->type == KEY_UNSPEC) {
4317c478bd9Sstevel@tonic-gate ret->type = type;
4327c478bd9Sstevel@tonic-gate } else if (ret->type != type) {
4337c478bd9Sstevel@tonic-gate /* is a key, but different type */
4347c478bd9Sstevel@tonic-gate debug3("key_read: type mismatch");
4357c478bd9Sstevel@tonic-gate return -1;
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate len = 2*strlen(cp);
4387c478bd9Sstevel@tonic-gate blob = xmalloc(len);
4397c478bd9Sstevel@tonic-gate n = uudecode(cp, blob, len);
4407c478bd9Sstevel@tonic-gate if (n < 0) {
4417c478bd9Sstevel@tonic-gate error("key_read: uudecode %s failed", cp);
4427c478bd9Sstevel@tonic-gate xfree(blob);
4437c478bd9Sstevel@tonic-gate return -1;
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate k = key_from_blob(blob, n);
4467c478bd9Sstevel@tonic-gate xfree(blob);
4477c478bd9Sstevel@tonic-gate if (k == NULL) {
4487c478bd9Sstevel@tonic-gate error("key_read: key_from_blob %s failed", cp);
4497c478bd9Sstevel@tonic-gate return -1;
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate if (k->type != type) {
4527c478bd9Sstevel@tonic-gate error("key_read: type mismatch: encoding error");
4537c478bd9Sstevel@tonic-gate key_free(k);
4547c478bd9Sstevel@tonic-gate return -1;
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate /*XXXX*/
4577c478bd9Sstevel@tonic-gate if (ret->type == KEY_RSA) {
4587c478bd9Sstevel@tonic-gate if (ret->rsa != NULL)
4597c478bd9Sstevel@tonic-gate RSA_free(ret->rsa);
4607c478bd9Sstevel@tonic-gate ret->rsa = k->rsa;
4617c478bd9Sstevel@tonic-gate k->rsa = NULL;
4627c478bd9Sstevel@tonic-gate success = 1;
4637c478bd9Sstevel@tonic-gate #ifdef DEBUG_PK
4647c478bd9Sstevel@tonic-gate RSA_print_fp(stderr, ret->rsa, 8);
4657c478bd9Sstevel@tonic-gate #endif
4667c478bd9Sstevel@tonic-gate } else {
4677c478bd9Sstevel@tonic-gate if (ret->dsa != NULL)
4687c478bd9Sstevel@tonic-gate DSA_free(ret->dsa);
4697c478bd9Sstevel@tonic-gate ret->dsa = k->dsa;
4707c478bd9Sstevel@tonic-gate k->dsa = NULL;
4717c478bd9Sstevel@tonic-gate success = 1;
4727c478bd9Sstevel@tonic-gate #ifdef DEBUG_PK
4737c478bd9Sstevel@tonic-gate DSA_print_fp(stderr, ret->dsa, 8);
4747c478bd9Sstevel@tonic-gate #endif
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate /*XXXX*/
4777c478bd9Sstevel@tonic-gate key_free(k);
4787c478bd9Sstevel@tonic-gate if (success != 1)
4797c478bd9Sstevel@tonic-gate break;
4807c478bd9Sstevel@tonic-gate /* advance cp: skip whitespace and data */
4817c478bd9Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t')
4827c478bd9Sstevel@tonic-gate cp++;
4837c478bd9Sstevel@tonic-gate while (*cp != '\0' && *cp != ' ' && *cp != '\t')
4847c478bd9Sstevel@tonic-gate cp++;
4857c478bd9Sstevel@tonic-gate *cpp = cp;
4867c478bd9Sstevel@tonic-gate break;
4877c478bd9Sstevel@tonic-gate default:
4887c478bd9Sstevel@tonic-gate fatal("key_read: bad key type: %d", ret->type);
4897c478bd9Sstevel@tonic-gate break;
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate return success;
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate int
key_write(const Key * key,FILE * f)495*442d23f4Sjp161948 key_write(const Key *key, FILE *f)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate int n, success = 0;
4987c478bd9Sstevel@tonic-gate u_int len, bits = 0;
4997c478bd9Sstevel@tonic-gate u_char *blob;
5007c478bd9Sstevel@tonic-gate char *uu;
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate if (key->type == KEY_RSA1 && key->rsa != NULL) {
5037c478bd9Sstevel@tonic-gate /* size of modulus 'n' */
5047c478bd9Sstevel@tonic-gate bits = BN_num_bits(key->rsa->n);
5057c478bd9Sstevel@tonic-gate fprintf(f, "%u", bits);
5067c478bd9Sstevel@tonic-gate if (write_bignum(f, key->rsa->e) &&
5077c478bd9Sstevel@tonic-gate write_bignum(f, key->rsa->n)) {
5087c478bd9Sstevel@tonic-gate success = 1;
5097c478bd9Sstevel@tonic-gate } else {
5107c478bd9Sstevel@tonic-gate error("key_write: failed for RSA key");
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
5137c478bd9Sstevel@tonic-gate (key->type == KEY_RSA && key->rsa != NULL)) {
5147c478bd9Sstevel@tonic-gate key_to_blob(key, &blob, &len);
5157c478bd9Sstevel@tonic-gate uu = xmalloc(2*len);
5167c478bd9Sstevel@tonic-gate n = uuencode(blob, len, uu, 2*len);
5177c478bd9Sstevel@tonic-gate if (n > 0) {
5187c478bd9Sstevel@tonic-gate fprintf(f, "%s %s", key_ssh_name(key), uu);
5197c478bd9Sstevel@tonic-gate success = 1;
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate xfree(blob);
5227c478bd9Sstevel@tonic-gate xfree(uu);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate return success;
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate char *
key_type(Key * k)5287c478bd9Sstevel@tonic-gate key_type(Key *k)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate switch (k->type) {
5317c478bd9Sstevel@tonic-gate case KEY_RSA1:
5327c478bd9Sstevel@tonic-gate return "RSA1";
5337c478bd9Sstevel@tonic-gate break;
5347c478bd9Sstevel@tonic-gate case KEY_RSA:
5357c478bd9Sstevel@tonic-gate return "RSA";
5367c478bd9Sstevel@tonic-gate break;
5377c478bd9Sstevel@tonic-gate case KEY_DSA:
5387c478bd9Sstevel@tonic-gate return "DSA";
5397c478bd9Sstevel@tonic-gate break;
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate return "unknown";
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate
5447c478bd9Sstevel@tonic-gate char *
key_ssh_name(const Key * k)545*442d23f4Sjp161948 key_ssh_name(const Key *k)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate switch (k->type) {
5487c478bd9Sstevel@tonic-gate case KEY_RSA:
5497c478bd9Sstevel@tonic-gate return "ssh-rsa";
5507c478bd9Sstevel@tonic-gate break;
5517c478bd9Sstevel@tonic-gate case KEY_DSA:
5527c478bd9Sstevel@tonic-gate return "ssh-dss";
5537c478bd9Sstevel@tonic-gate break;
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate return "ssh-unknown";
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate
5587c478bd9Sstevel@tonic-gate u_int
key_size(Key * k)5597c478bd9Sstevel@tonic-gate key_size(Key *k)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate switch (k->type) {
5627c478bd9Sstevel@tonic-gate case KEY_RSA1:
5637c478bd9Sstevel@tonic-gate case KEY_RSA:
5647c478bd9Sstevel@tonic-gate return BN_num_bits(k->rsa->n);
5657c478bd9Sstevel@tonic-gate break;
5667c478bd9Sstevel@tonic-gate case KEY_DSA:
5677c478bd9Sstevel@tonic-gate return BN_num_bits(k->dsa->p);
5687c478bd9Sstevel@tonic-gate break;
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate return 0;
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate static RSA *
rsa_generate_private_key(u_int bits)5747c478bd9Sstevel@tonic-gate rsa_generate_private_key(u_int bits)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate RSA *private;
5777c478bd9Sstevel@tonic-gate private = RSA_generate_key(bits, 35, NULL, NULL);
5787c478bd9Sstevel@tonic-gate if (private == NULL)
5797c478bd9Sstevel@tonic-gate fatal("rsa_generate_private_key: key generation failed.");
5807c478bd9Sstevel@tonic-gate return private;
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate static DSA*
dsa_generate_private_key(u_int bits)5847c478bd9Sstevel@tonic-gate dsa_generate_private_key(u_int bits)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
5877c478bd9Sstevel@tonic-gate if (private == NULL)
5887c478bd9Sstevel@tonic-gate fatal("dsa_generate_private_key: DSA_generate_parameters failed");
5897c478bd9Sstevel@tonic-gate if (!DSA_generate_key(private))
5907c478bd9Sstevel@tonic-gate fatal("dsa_generate_private_key: DSA_generate_key failed.");
5917c478bd9Sstevel@tonic-gate if (private == NULL)
5927c478bd9Sstevel@tonic-gate fatal("dsa_generate_private_key: NULL.");
5937c478bd9Sstevel@tonic-gate return private;
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate
5967c478bd9Sstevel@tonic-gate Key *
key_generate(int type,u_int bits)5977c478bd9Sstevel@tonic-gate key_generate(int type, u_int bits)
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate Key *k = key_new(KEY_UNSPEC);
6007c478bd9Sstevel@tonic-gate switch (type) {
6017c478bd9Sstevel@tonic-gate case KEY_DSA:
6027c478bd9Sstevel@tonic-gate k->dsa = dsa_generate_private_key(bits);
6037c478bd9Sstevel@tonic-gate break;
6047c478bd9Sstevel@tonic-gate case KEY_RSA:
6057c478bd9Sstevel@tonic-gate case KEY_RSA1:
6067c478bd9Sstevel@tonic-gate k->rsa = rsa_generate_private_key(bits);
6077c478bd9Sstevel@tonic-gate break;
6087c478bd9Sstevel@tonic-gate default:
6097c478bd9Sstevel@tonic-gate fatal("key_generate: unknown type %d", type);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate k->type = type;
6127c478bd9Sstevel@tonic-gate return k;
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate
6157c478bd9Sstevel@tonic-gate Key *
key_from_private(Key * k)6167c478bd9Sstevel@tonic-gate key_from_private(Key *k)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate Key *n = NULL;
6197c478bd9Sstevel@tonic-gate switch (k->type) {
6207c478bd9Sstevel@tonic-gate case KEY_DSA:
6217c478bd9Sstevel@tonic-gate n = key_new(k->type);
6227c478bd9Sstevel@tonic-gate BN_copy(n->dsa->p, k->dsa->p);
6237c478bd9Sstevel@tonic-gate BN_copy(n->dsa->q, k->dsa->q);
6247c478bd9Sstevel@tonic-gate BN_copy(n->dsa->g, k->dsa->g);
6257c478bd9Sstevel@tonic-gate BN_copy(n->dsa->pub_key, k->dsa->pub_key);
6267c478bd9Sstevel@tonic-gate break;
6277c478bd9Sstevel@tonic-gate case KEY_RSA:
6287c478bd9Sstevel@tonic-gate case KEY_RSA1:
6297c478bd9Sstevel@tonic-gate n = key_new(k->type);
6307c478bd9Sstevel@tonic-gate BN_copy(n->rsa->n, k->rsa->n);
6317c478bd9Sstevel@tonic-gate BN_copy(n->rsa->e, k->rsa->e);
6327c478bd9Sstevel@tonic-gate break;
6337c478bd9Sstevel@tonic-gate default:
6347c478bd9Sstevel@tonic-gate fatal("key_from_private: unknown type %d", k->type);
6357c478bd9Sstevel@tonic-gate break;
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate return n;
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate
6407c478bd9Sstevel@tonic-gate int
key_type_from_name(char * name)6417c478bd9Sstevel@tonic-gate key_type_from_name(char *name)
6427c478bd9Sstevel@tonic-gate {
6437c478bd9Sstevel@tonic-gate if (strcmp(name, "rsa1") == 0) {
6447c478bd9Sstevel@tonic-gate return KEY_RSA1;
6457c478bd9Sstevel@tonic-gate } else if (strcmp(name, "rsa") == 0) {
6467c478bd9Sstevel@tonic-gate return KEY_RSA;
6477c478bd9Sstevel@tonic-gate } else if (strcmp(name, "dsa") == 0) {
6487c478bd9Sstevel@tonic-gate return KEY_DSA;
6497c478bd9Sstevel@tonic-gate } else if (strcmp(name, "ssh-rsa") == 0) {
6507c478bd9Sstevel@tonic-gate return KEY_RSA;
6517c478bd9Sstevel@tonic-gate } else if (strcmp(name, "ssh-dss") == 0) {
6527c478bd9Sstevel@tonic-gate return KEY_DSA;
6537c478bd9Sstevel@tonic-gate } else if (strcmp(name, "null") == 0){
6547c478bd9Sstevel@tonic-gate return KEY_NULL;
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate debug2("key_type_from_name: unknown key type '%s'", name);
6577c478bd9Sstevel@tonic-gate return KEY_UNSPEC;
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate int
key_names_valid2(const char * names)6617c478bd9Sstevel@tonic-gate key_names_valid2(const char *names)
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate char *s, *cp, *p;
6647c478bd9Sstevel@tonic-gate
6657c478bd9Sstevel@tonic-gate if (names == NULL || strcmp(names, "") == 0)
6667c478bd9Sstevel@tonic-gate return 0;
6677c478bd9Sstevel@tonic-gate s = cp = xstrdup(names);
6687c478bd9Sstevel@tonic-gate for ((p = strsep(&cp, ",")); p && *p != '\0';
6697c478bd9Sstevel@tonic-gate (p = strsep(&cp, ","))) {
6707c478bd9Sstevel@tonic-gate switch (key_type_from_name(p)) {
6717c478bd9Sstevel@tonic-gate case KEY_RSA1:
6727c478bd9Sstevel@tonic-gate case KEY_UNSPEC:
6737c478bd9Sstevel@tonic-gate xfree(s);
6747c478bd9Sstevel@tonic-gate return 0;
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate debug3("key names ok: [%s]", names);
6787c478bd9Sstevel@tonic-gate xfree(s);
6797c478bd9Sstevel@tonic-gate return 1;
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate
6827c478bd9Sstevel@tonic-gate Key *
key_from_blob(u_char * blob,int blen)6837c478bd9Sstevel@tonic-gate key_from_blob(u_char *blob, int blen)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate Buffer b;
6867c478bd9Sstevel@tonic-gate char *ktype;
6877c478bd9Sstevel@tonic-gate int rlen, type;
6887c478bd9Sstevel@tonic-gate Key *key = NULL;
6897c478bd9Sstevel@tonic-gate
6907c478bd9Sstevel@tonic-gate #ifdef DEBUG_PK
6917c478bd9Sstevel@tonic-gate dump_base64(stderr, blob, blen);
6927c478bd9Sstevel@tonic-gate #endif
6937c478bd9Sstevel@tonic-gate buffer_init(&b);
6947c478bd9Sstevel@tonic-gate buffer_append(&b, blob, blen);
69526ba1984Sjp161948 if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
69626ba1984Sjp161948 error("key_from_blob: can't read key type");
69726ba1984Sjp161948 goto out;
69826ba1984Sjp161948 }
69926ba1984Sjp161948
7007c478bd9Sstevel@tonic-gate type = key_type_from_name(ktype);
7017c478bd9Sstevel@tonic-gate
7027c478bd9Sstevel@tonic-gate switch (type) {
7037c478bd9Sstevel@tonic-gate case KEY_RSA:
7047c478bd9Sstevel@tonic-gate key = key_new(type);
70526ba1984Sjp161948 if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
70626ba1984Sjp161948 buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
70726ba1984Sjp161948 error("key_from_blob: can't read rsa key");
70826ba1984Sjp161948 key_free(key);
70926ba1984Sjp161948 key = NULL;
71026ba1984Sjp161948 goto out;
71126ba1984Sjp161948 }
7127c478bd9Sstevel@tonic-gate #ifdef DEBUG_PK
7137c478bd9Sstevel@tonic-gate RSA_print_fp(stderr, key->rsa, 8);
7147c478bd9Sstevel@tonic-gate #endif
7157c478bd9Sstevel@tonic-gate break;
7167c478bd9Sstevel@tonic-gate case KEY_DSA:
7177c478bd9Sstevel@tonic-gate key = key_new(type);
71826ba1984Sjp161948 if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
71926ba1984Sjp161948 buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
72026ba1984Sjp161948 buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
72126ba1984Sjp161948 buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
72226ba1984Sjp161948 error("key_from_blob: can't read dsa key");
72326ba1984Sjp161948 key_free(key);
72426ba1984Sjp161948 key = NULL;
72526ba1984Sjp161948 goto out;
72626ba1984Sjp161948 }
7277c478bd9Sstevel@tonic-gate #ifdef DEBUG_PK
7287c478bd9Sstevel@tonic-gate DSA_print_fp(stderr, key->dsa, 8);
7297c478bd9Sstevel@tonic-gate #endif
7307c478bd9Sstevel@tonic-gate break;
7317c478bd9Sstevel@tonic-gate case KEY_UNSPEC:
7327c478bd9Sstevel@tonic-gate key = key_new(type);
7337c478bd9Sstevel@tonic-gate break;
7347c478bd9Sstevel@tonic-gate default:
7357c478bd9Sstevel@tonic-gate error("key_from_blob: cannot handle type %s", ktype);
73626ba1984Sjp161948 goto out;
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate rlen = buffer_len(&b);
7397c478bd9Sstevel@tonic-gate if (key != NULL && rlen != 0)
7407c478bd9Sstevel@tonic-gate error("key_from_blob: remaining bytes in key blob %d", rlen);
74126ba1984Sjp161948 out:
74226ba1984Sjp161948 if (ktype != NULL)
7437c478bd9Sstevel@tonic-gate xfree(ktype);
7447c478bd9Sstevel@tonic-gate buffer_free(&b);
7457c478bd9Sstevel@tonic-gate return key;
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate
7487c478bd9Sstevel@tonic-gate int
key_to_blob(const Key * key,u_char ** blobp,u_int * lenp)749*442d23f4Sjp161948 key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate Buffer b;
7527c478bd9Sstevel@tonic-gate int len;
7537c478bd9Sstevel@tonic-gate
7547c478bd9Sstevel@tonic-gate if (key == NULL) {
7557c478bd9Sstevel@tonic-gate error("key_to_blob: key == NULL");
7567c478bd9Sstevel@tonic-gate return 0;
7577c478bd9Sstevel@tonic-gate }
7587c478bd9Sstevel@tonic-gate buffer_init(&b);
7597c478bd9Sstevel@tonic-gate switch (key->type) {
7607c478bd9Sstevel@tonic-gate case KEY_DSA:
7617c478bd9Sstevel@tonic-gate buffer_put_cstring(&b, key_ssh_name(key));
7627c478bd9Sstevel@tonic-gate buffer_put_bignum2(&b, key->dsa->p);
7637c478bd9Sstevel@tonic-gate buffer_put_bignum2(&b, key->dsa->q);
7647c478bd9Sstevel@tonic-gate buffer_put_bignum2(&b, key->dsa->g);
7657c478bd9Sstevel@tonic-gate buffer_put_bignum2(&b, key->dsa->pub_key);
7667c478bd9Sstevel@tonic-gate break;
7677c478bd9Sstevel@tonic-gate case KEY_RSA:
7687c478bd9Sstevel@tonic-gate buffer_put_cstring(&b, key_ssh_name(key));
7697c478bd9Sstevel@tonic-gate buffer_put_bignum2(&b, key->rsa->e);
7707c478bd9Sstevel@tonic-gate buffer_put_bignum2(&b, key->rsa->n);
7717c478bd9Sstevel@tonic-gate break;
7727c478bd9Sstevel@tonic-gate default:
7737c478bd9Sstevel@tonic-gate error("key_to_blob: unsupported key type %d", key->type);
7747c478bd9Sstevel@tonic-gate buffer_free(&b);
7757c478bd9Sstevel@tonic-gate return 0;
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate len = buffer_len(&b);
7787c478bd9Sstevel@tonic-gate if (lenp != NULL)
7797c478bd9Sstevel@tonic-gate *lenp = len;
7807c478bd9Sstevel@tonic-gate if (blobp != NULL) {
7817c478bd9Sstevel@tonic-gate *blobp = xmalloc(len);
7827c478bd9Sstevel@tonic-gate memcpy(*blobp, buffer_ptr(&b), len);
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate memset(buffer_ptr(&b), 0, len);
7857c478bd9Sstevel@tonic-gate buffer_free(&b);
7867c478bd9Sstevel@tonic-gate return len;
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate
7897c478bd9Sstevel@tonic-gate int
key_sign(Key * key,u_char ** sigp,u_int * lenp,u_char * data,u_int datalen)7907c478bd9Sstevel@tonic-gate key_sign(
7917c478bd9Sstevel@tonic-gate Key *key,
7927c478bd9Sstevel@tonic-gate u_char **sigp, u_int *lenp,
7937c478bd9Sstevel@tonic-gate u_char *data, u_int datalen)
7947c478bd9Sstevel@tonic-gate {
7957c478bd9Sstevel@tonic-gate switch (key->type) {
7967c478bd9Sstevel@tonic-gate case KEY_DSA:
7977c478bd9Sstevel@tonic-gate return ssh_dss_sign(key, sigp, lenp, data, datalen);
7987c478bd9Sstevel@tonic-gate break;
7997c478bd9Sstevel@tonic-gate case KEY_RSA:
8007c478bd9Sstevel@tonic-gate return ssh_rsa_sign(key, sigp, lenp, data, datalen);
8017c478bd9Sstevel@tonic-gate break;
8027c478bd9Sstevel@tonic-gate default:
8037c478bd9Sstevel@tonic-gate error("key_sign: illegal key type %d", key->type);
8047c478bd9Sstevel@tonic-gate return -1;
8057c478bd9Sstevel@tonic-gate break;
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate
8097c478bd9Sstevel@tonic-gate /*
8107c478bd9Sstevel@tonic-gate * key_verify returns 1 for a correct signature, 0 for an incorrect signature
8117c478bd9Sstevel@tonic-gate * and -1 on error.
8127c478bd9Sstevel@tonic-gate */
8137c478bd9Sstevel@tonic-gate int
key_verify(Key * key,u_char * signature,u_int signaturelen,u_char * data,u_int datalen)8147c478bd9Sstevel@tonic-gate key_verify(
8157c478bd9Sstevel@tonic-gate Key *key,
8167c478bd9Sstevel@tonic-gate u_char *signature, u_int signaturelen,
8177c478bd9Sstevel@tonic-gate u_char *data, u_int datalen)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate if (signaturelen == 0)
8207c478bd9Sstevel@tonic-gate return -1;
8217c478bd9Sstevel@tonic-gate
8227c478bd9Sstevel@tonic-gate switch (key->type) {
8237c478bd9Sstevel@tonic-gate case KEY_DSA:
8247c478bd9Sstevel@tonic-gate return ssh_dss_verify(key, signature, signaturelen, data, datalen);
8257c478bd9Sstevel@tonic-gate break;
8267c478bd9Sstevel@tonic-gate case KEY_RSA:
8277c478bd9Sstevel@tonic-gate return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
8287c478bd9Sstevel@tonic-gate break;
8297c478bd9Sstevel@tonic-gate default:
8307c478bd9Sstevel@tonic-gate error("key_verify: illegal key type %d", key->type);
8317c478bd9Sstevel@tonic-gate return -1;
8327c478bd9Sstevel@tonic-gate break;
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate }
8357c478bd9Sstevel@tonic-gate
8367c478bd9Sstevel@tonic-gate /* Converts a private to a public key */
8377c478bd9Sstevel@tonic-gate Key *
key_demote(Key * k)8387c478bd9Sstevel@tonic-gate key_demote(Key *k)
8397c478bd9Sstevel@tonic-gate {
8407c478bd9Sstevel@tonic-gate Key *pk;
8417c478bd9Sstevel@tonic-gate
8427c478bd9Sstevel@tonic-gate pk = xmalloc(sizeof(*pk));
8437c478bd9Sstevel@tonic-gate pk->type = k->type;
8447c478bd9Sstevel@tonic-gate pk->flags = k->flags;
8457c478bd9Sstevel@tonic-gate pk->dsa = NULL;
8467c478bd9Sstevel@tonic-gate pk->rsa = NULL;
8477c478bd9Sstevel@tonic-gate
8487c478bd9Sstevel@tonic-gate switch (k->type) {
8497c478bd9Sstevel@tonic-gate case KEY_RSA1:
8507c478bd9Sstevel@tonic-gate case KEY_RSA:
8517c478bd9Sstevel@tonic-gate if ((pk->rsa = RSA_new()) == NULL)
8527c478bd9Sstevel@tonic-gate fatal("key_demote: RSA_new failed");
8537c478bd9Sstevel@tonic-gate if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
8547c478bd9Sstevel@tonic-gate fatal("key_demote: BN_dup failed");
8557c478bd9Sstevel@tonic-gate if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
8567c478bd9Sstevel@tonic-gate fatal("key_demote: BN_dup failed");
8577c478bd9Sstevel@tonic-gate break;
8587c478bd9Sstevel@tonic-gate case KEY_DSA:
8597c478bd9Sstevel@tonic-gate if ((pk->dsa = DSA_new()) == NULL)
8607c478bd9Sstevel@tonic-gate fatal("key_demote: DSA_new failed");
8617c478bd9Sstevel@tonic-gate if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
8627c478bd9Sstevel@tonic-gate fatal("key_demote: BN_dup failed");
8637c478bd9Sstevel@tonic-gate if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
8647c478bd9Sstevel@tonic-gate fatal("key_demote: BN_dup failed");
8657c478bd9Sstevel@tonic-gate if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
8667c478bd9Sstevel@tonic-gate fatal("key_demote: BN_dup failed");
8677c478bd9Sstevel@tonic-gate if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
8687c478bd9Sstevel@tonic-gate fatal("key_demote: BN_dup failed");
8697c478bd9Sstevel@tonic-gate break;
8707c478bd9Sstevel@tonic-gate default:
8717c478bd9Sstevel@tonic-gate fatal("key_free: bad key type %d", k->type);
8727c478bd9Sstevel@tonic-gate break;
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate
8757c478bd9Sstevel@tonic-gate return (pk);
8767c478bd9Sstevel@tonic-gate }
877