2000-03-26 11:04:51 +08:00
|
|
|
/*
|
2000-09-16 10:29:08 +08:00
|
|
|
* read_bignum():
|
|
|
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
|
|
|
*
|
|
|
|
* As far as I am concerned, the code I have written for this software
|
|
|
|
* can be used freely for any purpose. Any derived versions of this
|
|
|
|
* software must be clearly marked as such, and if the derived work is
|
|
|
|
* incompatible with the protocol description in the RFC file, it must be
|
|
|
|
* called by a name other than "ssh" or "Secure Shell".
|
|
|
|
*
|
|
|
|
*
|
2001-07-04 11:32:30 +08:00
|
|
|
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
|
2000-03-26 11:04:51 +08:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
#include "includes.h"
|
2002-01-22 20:07:21 +08:00
|
|
|
RCSID("$OpenBSD: key.c,v 1.37 2001/12/25 18:49:56 markus Exp $");
|
2001-01-22 13:34:40 +08:00
|
|
|
|
2000-03-26 11:04:51 +08:00
|
|
|
#include <openssl/evp.h>
|
2001-01-22 13:34:40 +08:00
|
|
|
|
2000-03-26 11:04:51 +08:00
|
|
|
#include "xmalloc.h"
|
|
|
|
#include "key.h"
|
2000-11-13 19:57:25 +08:00
|
|
|
#include "rsa.h"
|
|
|
|
#include "ssh-dss.h"
|
|
|
|
#include "ssh-rsa.h"
|
2000-04-29 21:57:08 +08:00
|
|
|
#include "uuencode.h"
|
2000-11-13 19:57:25 +08:00
|
|
|
#include "buffer.h"
|
|
|
|
#include "bufaux.h"
|
2001-01-22 13:34:40 +08:00
|
|
|
#include "log.h"
|
2000-03-26 11:04:51 +08:00
|
|
|
|
|
|
|
Key *
|
|
|
|
key_new(int type)
|
|
|
|
{
|
|
|
|
Key *k;
|
|
|
|
RSA *rsa;
|
|
|
|
DSA *dsa;
|
|
|
|
k = xmalloc(sizeof(*k));
|
|
|
|
k->type = type;
|
2001-07-04 12:52:03 +08:00
|
|
|
k->flags = 0;
|
2000-04-29 21:57:08 +08:00
|
|
|
k->dsa = NULL;
|
|
|
|
k->rsa = NULL;
|
2000-03-26 11:04:51 +08:00
|
|
|
switch (k->type) {
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_RSA1:
|
2000-03-26 11:04:51 +08:00
|
|
|
case KEY_RSA:
|
|
|
|
rsa = RSA_new();
|
|
|
|
rsa->n = BN_new();
|
|
|
|
rsa->e = BN_new();
|
2002-01-22 20:07:21 +08:00
|
|
|
if (rsa == NULL || rsa->n == NULL || rsa->e == NULL)
|
|
|
|
fatal("key_new: malloc failure");
|
2000-03-26 11:04:51 +08:00
|
|
|
k->rsa = rsa;
|
|
|
|
break;
|
|
|
|
case KEY_DSA:
|
|
|
|
dsa = DSA_new();
|
|
|
|
dsa->p = BN_new();
|
|
|
|
dsa->q = BN_new();
|
|
|
|
dsa->g = BN_new();
|
|
|
|
dsa->pub_key = BN_new();
|
2002-01-22 20:07:21 +08:00
|
|
|
if (dsa == NULL || dsa->p == NULL || dsa->q == NULL ||
|
|
|
|
dsa->g == NULL || dsa->pub_key == NULL)
|
|
|
|
fatal("key_new: malloc failure");
|
2000-03-26 11:04:51 +08:00
|
|
|
k->dsa = dsa;
|
|
|
|
break;
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_UNSPEC:
|
2000-03-26 11:04:51 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("key_new: bad key type %d", k->type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return k;
|
|
|
|
}
|
2000-11-13 19:57:25 +08:00
|
|
|
Key *
|
|
|
|
key_new_private(int type)
|
|
|
|
{
|
|
|
|
Key *k = key_new(type);
|
|
|
|
switch (k->type) {
|
|
|
|
case KEY_RSA1:
|
|
|
|
case KEY_RSA:
|
|
|
|
k->rsa->d = BN_new();
|
|
|
|
k->rsa->iqmp = BN_new();
|
|
|
|
k->rsa->q = BN_new();
|
|
|
|
k->rsa->p = BN_new();
|
|
|
|
k->rsa->dmq1 = BN_new();
|
|
|
|
k->rsa->dmp1 = BN_new();
|
|
|
|
break;
|
|
|
|
case KEY_DSA:
|
|
|
|
k->dsa->priv_key = BN_new();
|
|
|
|
break;
|
|
|
|
case KEY_UNSPEC:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return k;
|
|
|
|
}
|
2000-03-26 11:04:51 +08:00
|
|
|
void
|
|
|
|
key_free(Key *k)
|
|
|
|
{
|
|
|
|
switch (k->type) {
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_RSA1:
|
2000-03-26 11:04:51 +08:00
|
|
|
case KEY_RSA:
|
|
|
|
if (k->rsa != NULL)
|
|
|
|
RSA_free(k->rsa);
|
|
|
|
k->rsa = NULL;
|
|
|
|
break;
|
|
|
|
case KEY_DSA:
|
|
|
|
if (k->dsa != NULL)
|
|
|
|
DSA_free(k->dsa);
|
|
|
|
k->dsa = NULL;
|
|
|
|
break;
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_UNSPEC:
|
|
|
|
break;
|
2000-03-26 11:04:51 +08:00
|
|
|
default:
|
|
|
|
fatal("key_free: bad key type %d", k->type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
xfree(k);
|
|
|
|
}
|
|
|
|
int
|
|
|
|
key_equal(Key *a, Key *b)
|
|
|
|
{
|
|
|
|
if (a == NULL || b == NULL || a->type != b->type)
|
|
|
|
return 0;
|
|
|
|
switch (a->type) {
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_RSA1:
|
2000-03-26 11:04:51 +08:00
|
|
|
case KEY_RSA:
|
|
|
|
return a->rsa != NULL && b->rsa != NULL &&
|
|
|
|
BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
|
|
|
|
BN_cmp(a->rsa->n, b->rsa->n) == 0;
|
|
|
|
break;
|
|
|
|
case KEY_DSA:
|
|
|
|
return a->dsa != NULL && b->dsa != NULL &&
|
|
|
|
BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
|
|
|
|
BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
|
|
|
|
BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
|
|
|
|
BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
|
|
|
|
break;
|
|
|
|
default:
|
2000-04-29 21:57:08 +08:00
|
|
|
fatal("key_equal: bad key type %d", a->type);
|
2000-03-26 11:04:51 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static u_char*
|
2001-03-12 04:03:44 +08:00
|
|
|
key_fingerprint_raw(Key *k, enum fp_type dgst_type, size_t *dgst_raw_length)
|
2000-03-26 11:04:51 +08:00
|
|
|
{
|
2001-03-12 10:59:31 +08:00
|
|
|
EVP_MD *md = NULL;
|
|
|
|
EVP_MD_CTX ctx;
|
2000-12-22 09:43:59 +08:00
|
|
|
u_char *blob = NULL;
|
2001-03-12 04:03:44 +08:00
|
|
|
u_char *retval = NULL;
|
2000-03-26 11:04:51 +08:00
|
|
|
int len = 0;
|
2000-04-29 21:57:08 +08:00
|
|
|
int nlen, elen;
|
2000-03-26 11:04:51 +08:00
|
|
|
|
2001-03-12 04:03:44 +08:00
|
|
|
*dgst_raw_length = 0;
|
|
|
|
|
2001-03-12 10:59:31 +08:00
|
|
|
switch (dgst_type) {
|
|
|
|
case SSH_FP_MD5:
|
|
|
|
md = EVP_md5();
|
|
|
|
break;
|
|
|
|
case SSH_FP_SHA1:
|
|
|
|
md = EVP_sha1();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("key_fingerprint_raw: bad digest type %d",
|
|
|
|
dgst_type);
|
|
|
|
}
|
2000-03-26 11:04:51 +08:00
|
|
|
switch (k->type) {
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_RSA1:
|
2000-03-26 11:04:51 +08:00
|
|
|
nlen = BN_num_bytes(k->rsa->n);
|
|
|
|
elen = BN_num_bytes(k->rsa->e);
|
|
|
|
len = nlen + elen;
|
2000-04-29 21:57:08 +08:00
|
|
|
blob = xmalloc(len);
|
|
|
|
BN_bn2bin(k->rsa->n, blob);
|
|
|
|
BN_bn2bin(k->rsa->e, blob + nlen);
|
2000-03-26 11:04:51 +08:00
|
|
|
break;
|
|
|
|
case KEY_DSA:
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_RSA:
|
|
|
|
key_to_blob(k, &blob, &len);
|
|
|
|
break;
|
|
|
|
case KEY_UNSPEC:
|
|
|
|
return retval;
|
2000-03-26 11:04:51 +08:00
|
|
|
break;
|
|
|
|
default:
|
2001-03-12 04:03:44 +08:00
|
|
|
fatal("key_fingerprint_raw: bad key type %d", k->type);
|
2000-03-26 11:04:51 +08:00
|
|
|
break;
|
|
|
|
}
|
2000-04-29 21:57:08 +08:00
|
|
|
if (blob != NULL) {
|
2001-03-12 04:03:44 +08:00
|
|
|
retval = xmalloc(EVP_MAX_MD_SIZE);
|
2000-06-22 19:32:31 +08:00
|
|
|
EVP_DigestInit(&ctx, md);
|
|
|
|
EVP_DigestUpdate(&ctx, blob, len);
|
2001-03-12 04:03:44 +08:00
|
|
|
EVP_DigestFinal(&ctx, retval, NULL);
|
|
|
|
*dgst_raw_length = md->md_size;
|
2000-04-29 21:57:08 +08:00
|
|
|
memset(blob, 0, len);
|
|
|
|
xfree(blob);
|
2001-03-12 04:03:44 +08:00
|
|
|
} else {
|
|
|
|
fatal("key_fingerprint_raw: blob is null");
|
2000-03-26 11:04:51 +08:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static char*
|
2001-04-06 07:26:32 +08:00
|
|
|
key_fingerprint_hex(u_char* dgst_raw, size_t dgst_raw_len)
|
2001-03-12 04:03:44 +08:00
|
|
|
{
|
|
|
|
char *retval;
|
|
|
|
int i;
|
|
|
|
|
2001-04-18 02:06:14 +08:00
|
|
|
retval = xmalloc(dgst_raw_len * 3 + 1);
|
2001-03-12 04:03:44 +08:00
|
|
|
retval[0] = '\0';
|
2001-12-21 11:45:46 +08:00
|
|
|
for (i = 0; i < dgst_raw_len; i++) {
|
2001-03-12 04:03:44 +08:00
|
|
|
char hex[4];
|
|
|
|
snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
|
|
|
|
strlcat(retval, hex, dgst_raw_len * 3);
|
|
|
|
}
|
|
|
|
retval[(dgst_raw_len * 3) - 1] = '\0';
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static char*
|
2001-04-06 07:26:32 +08:00
|
|
|
key_fingerprint_bubblebabble(u_char* dgst_raw, size_t dgst_raw_len)
|
2001-03-12 04:03:44 +08:00
|
|
|
{
|
|
|
|
char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
|
|
|
|
char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
|
|
|
|
'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
|
2001-03-12 04:06:59 +08:00
|
|
|
u_int i, j = 0, rounds, seed = 1;
|
2001-03-12 04:03:44 +08:00
|
|
|
char *retval;
|
|
|
|
|
|
|
|
rounds = (dgst_raw_len / 2) + 1;
|
|
|
|
retval = xmalloc(sizeof(char) * (rounds*6));
|
2001-03-12 04:06:59 +08:00
|
|
|
retval[j++] = 'x';
|
|
|
|
for (i = 0; i < rounds; i++) {
|
2001-03-12 04:03:44 +08:00
|
|
|
u_int idx0, idx1, idx2, idx3, idx4;
|
2001-03-12 04:06:59 +08:00
|
|
|
if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
|
|
|
|
idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
|
2001-03-12 04:03:44 +08:00
|
|
|
seed) % 6;
|
2001-03-12 04:06:59 +08:00
|
|
|
idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
|
|
|
|
idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
|
2001-03-12 04:03:44 +08:00
|
|
|
(seed / 6)) % 6;
|
2001-03-12 04:06:59 +08:00
|
|
|
retval[j++] = vowels[idx0];
|
|
|
|
retval[j++] = consonants[idx1];
|
|
|
|
retval[j++] = vowels[idx2];
|
|
|
|
if ((i + 1) < rounds) {
|
|
|
|
idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
|
|
|
|
idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
|
|
|
|
retval[j++] = consonants[idx3];
|
|
|
|
retval[j++] = '-';
|
|
|
|
retval[j++] = consonants[idx4];
|
2001-03-12 04:03:44 +08:00
|
|
|
seed = ((seed * 5) +
|
2001-03-12 04:06:59 +08:00
|
|
|
((((u_int)(dgst_raw[2 * i])) * 7) +
|
|
|
|
((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
|
2001-03-12 04:03:44 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
idx0 = seed % 6;
|
|
|
|
idx1 = 16;
|
|
|
|
idx2 = seed / 6;
|
2001-03-12 04:06:59 +08:00
|
|
|
retval[j++] = vowels[idx0];
|
|
|
|
retval[j++] = consonants[idx1];
|
|
|
|
retval[j++] = vowels[idx2];
|
2001-03-12 04:03:44 +08:00
|
|
|
}
|
|
|
|
}
|
2001-03-12 04:06:59 +08:00
|
|
|
retval[j++] = 'x';
|
|
|
|
retval[j++] = '\0';
|
2001-03-12 04:03:44 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2001-03-13 12:57:58 +08:00
|
|
|
key_fingerprint(Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
|
2001-03-12 04:03:44 +08:00
|
|
|
{
|
2001-04-06 07:26:32 +08:00
|
|
|
char *retval = NULL;
|
2001-03-12 04:03:44 +08:00
|
|
|
u_char *dgst_raw;
|
2001-04-06 07:26:32 +08:00
|
|
|
size_t dgst_raw_len;
|
2001-12-21 11:45:46 +08:00
|
|
|
|
2001-03-12 04:03:44 +08:00
|
|
|
dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
|
|
|
|
if (!dgst_raw)
|
2001-03-13 12:57:58 +08:00
|
|
|
fatal("key_fingerprint: null from key_fingerprint_raw()");
|
2001-12-07 02:00:18 +08:00
|
|
|
switch (dgst_rep) {
|
2001-03-12 04:03:44 +08:00
|
|
|
case SSH_FP_HEX:
|
|
|
|
retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
|
|
|
|
break;
|
|
|
|
case SSH_FP_BUBBLEBABBLE:
|
|
|
|
retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("key_fingerprint_ex: bad digest representation %d",
|
|
|
|
dgst_rep);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
memset(dgst_raw, 0, dgst_raw_len);
|
|
|
|
xfree(dgst_raw);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2000-03-26 11:04:51 +08:00
|
|
|
/*
|
|
|
|
* Reads a multiple-precision integer in decimal from the buffer, and advances
|
|
|
|
* the pointer. The integer must already be initialized. This function is
|
|
|
|
* permitted to modify the buffer. This leaves *cpp to point just beyond the
|
|
|
|
* last processed (and maybe modified) character. Note that this may modify
|
|
|
|
* the buffer containing the number.
|
|
|
|
*/
|
2001-06-25 13:01:22 +08:00
|
|
|
static int
|
2000-03-26 11:04:51 +08:00
|
|
|
read_bignum(char **cpp, BIGNUM * value)
|
|
|
|
{
|
|
|
|
char *cp = *cpp;
|
|
|
|
int old;
|
|
|
|
|
|
|
|
/* Skip any leading whitespace. */
|
|
|
|
for (; *cp == ' ' || *cp == '\t'; cp++)
|
|
|
|
;
|
|
|
|
|
|
|
|
/* Check that it begins with a decimal digit. */
|
|
|
|
if (*cp < '0' || *cp > '9')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Save starting position. */
|
|
|
|
*cpp = cp;
|
|
|
|
|
|
|
|
/* Move forward until all decimal digits skipped. */
|
|
|
|
for (; *cp >= '0' && *cp <= '9'; cp++)
|
|
|
|
;
|
|
|
|
|
|
|
|
/* Save the old terminating character, and replace it by \0. */
|
|
|
|
old = *cp;
|
|
|
|
*cp = 0;
|
|
|
|
|
|
|
|
/* Parse the number. */
|
|
|
|
if (BN_dec2bn(&value, *cpp) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Restore old terminating character. */
|
|
|
|
*cp = old;
|
|
|
|
|
|
|
|
/* Move beyond the number and return success. */
|
|
|
|
*cpp = cp;
|
|
|
|
return 1;
|
|
|
|
}
|
2001-06-25 13:01:22 +08:00
|
|
|
static int
|
2000-03-26 11:04:51 +08:00
|
|
|
write_bignum(FILE *f, BIGNUM *num)
|
|
|
|
{
|
|
|
|
char *buf = BN_bn2dec(num);
|
|
|
|
if (buf == NULL) {
|
|
|
|
error("write_bignum: BN_bn2dec() failed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fprintf(f, " %s", buf);
|
2001-10-10 13:00:49 +08:00
|
|
|
OPENSSL_free(buf);
|
2000-03-26 11:04:51 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2000-11-13 19:57:25 +08:00
|
|
|
|
2001-09-20 08:55:53 +08:00
|
|
|
/* returns 1 ok, -1 error */
|
2000-11-13 19:57:25 +08:00
|
|
|
int
|
2000-04-29 21:57:08 +08:00
|
|
|
key_read(Key *ret, char **cpp)
|
2000-03-26 11:04:51 +08:00
|
|
|
{
|
2000-04-29 21:57:08 +08:00
|
|
|
Key *k;
|
2000-11-13 19:57:25 +08:00
|
|
|
int success = -1;
|
|
|
|
char *cp, *space;
|
|
|
|
int len, n, type;
|
|
|
|
u_int bits;
|
2000-12-22 09:43:59 +08:00
|
|
|
u_char *blob;
|
2000-04-29 21:57:08 +08:00
|
|
|
|
|
|
|
cp = *cpp;
|
|
|
|
|
2001-12-07 02:00:18 +08:00
|
|
|
switch (ret->type) {
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_RSA1:
|
2000-04-29 21:57:08 +08:00
|
|
|
/* Get number of bits. */
|
|
|
|
if (*cp < '0' || *cp > '9')
|
2000-11-13 19:57:25 +08:00
|
|
|
return -1; /* Bad bit count... */
|
2000-04-29 21:57:08 +08:00
|
|
|
for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
|
|
|
|
bits = 10 * bits + *cp - '0';
|
2000-03-26 11:04:51 +08:00
|
|
|
if (bits == 0)
|
2000-11-13 19:57:25 +08:00
|
|
|
return -1;
|
2000-04-29 21:57:08 +08:00
|
|
|
*cpp = cp;
|
2000-03-26 11:04:51 +08:00
|
|
|
/* Get public exponent, public modulus. */
|
|
|
|
if (!read_bignum(cpp, ret->rsa->e))
|
2000-11-13 19:57:25 +08:00
|
|
|
return -1;
|
2000-03-26 11:04:51 +08:00
|
|
|
if (!read_bignum(cpp, ret->rsa->n))
|
2000-11-13 19:57:25 +08:00
|
|
|
return -1;
|
|
|
|
success = 1;
|
2000-03-26 11:04:51 +08:00
|
|
|
break;
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_UNSPEC:
|
|
|
|
case KEY_RSA:
|
2000-03-26 11:04:51 +08:00
|
|
|
case KEY_DSA:
|
2000-11-13 19:57:25 +08:00
|
|
|
space = strchr(cp, ' ');
|
|
|
|
if (space == NULL) {
|
|
|
|
debug3("key_read: no space");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*space = '\0';
|
|
|
|
type = key_type_from_name(cp);
|
|
|
|
*space = ' ';
|
|
|
|
if (type == KEY_UNSPEC) {
|
|
|
|
debug3("key_read: no key found");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cp = space+1;
|
|
|
|
if (*cp == '\0') {
|
|
|
|
debug3("key_read: short string");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ret->type == KEY_UNSPEC) {
|
|
|
|
ret->type = type;
|
|
|
|
} else if (ret->type != type) {
|
|
|
|
/* is a key, but different type */
|
|
|
|
debug3("key_read: type mismatch");
|
2001-09-20 08:55:53 +08:00
|
|
|
return -1;
|
2000-11-13 19:57:25 +08:00
|
|
|
}
|
2000-04-29 21:57:08 +08:00
|
|
|
len = 2*strlen(cp);
|
|
|
|
blob = xmalloc(len);
|
|
|
|
n = uudecode(cp, blob, len);
|
- Remove references to SSLeay.
- Big OpenBSD CVS update
- markus@cvs.openbsd.org
[clientloop.c]
- typo
[session.c]
- update proctitle on pty alloc/dealloc, e.g. w/ windows client
[session.c]
- update proctitle for proto 1, too
[channels.h nchan.c serverloop.c session.c sshd.c]
- use c-style comments
- deraadt@cvs.openbsd.org
[scp.c]
- more atomicio
- markus@cvs.openbsd.org
[channels.c]
- set O_NONBLOCK
[ssh.1]
- update AUTHOR
[readconf.c ssh-keygen.c ssh.h]
- default DSA key file ~/.ssh/id_dsa
[clientloop.c]
- typo, rm verbose debug
- deraadt@cvs.openbsd.org
[ssh-keygen.1]
- document DSA use of ssh-keygen
[sshd.8]
- a start at describing what i understand of the DSA side
[ssh-keygen.1]
- document -X and -x
[ssh-keygen.c]
- simplify usage
- markus@cvs.openbsd.org
[sshd.8]
- there is no rhosts_dsa
[ssh-keygen.1]
- document -y, update -X,-x
[nchan.c]
- fix close for non-open ssh1 channels
[servconf.c servconf.h ssh.h sshd.8 sshd.c ]
- s/DsaKey/HostDSAKey/, document option
[sshconnect2.c]
- respect number_of_password_prompts
[channels.c channels.h servconf.c servconf.h session.c sshd.8]
- GatewayPorts for sshd, ok deraadt@
[ssh-add.1 ssh-agent.1 ssh.1]
- more doc on: DSA, id_dsa, known_hosts2, authorized_keys2
[ssh.1]
- more info on proto 2
[sshd.8]
- sync AUTHOR w/ ssh.1
[key.c key.h sshconnect.c]
- print key type when talking about host keys
[packet.c]
- clear padding in ssh2
[dsa.c key.c radix.c ssh.h sshconnect1.c uuencode.c uuencode.h]
- replace broken uuencode w/ libc b64_ntop
[auth2.c]
- log failure before sending the reply
[key.c radix.c uuencode.c]
- remote trailing comments before calling __b64_pton
[auth2.c readconf.c readconf.h servconf.c servconf.h ssh.1]
[sshconnect2.c sshd.8]
- add DSAAuthetication option to ssh/sshd, document SSH2 in sshd.8
- Bring in b64_ntop and b64_pton from OpenBSD libc (bsd-base64.[ch])
2000-05-07 10:03:14 +08:00
|
|
|
if (n < 0) {
|
2000-05-30 11:44:51 +08:00
|
|
|
error("key_read: uudecode %s failed", cp);
|
2001-12-07 00:41:41 +08:00
|
|
|
xfree(blob);
|
2000-11-13 19:57:25 +08:00
|
|
|
return -1;
|
- Remove references to SSLeay.
- Big OpenBSD CVS update
- markus@cvs.openbsd.org
[clientloop.c]
- typo
[session.c]
- update proctitle on pty alloc/dealloc, e.g. w/ windows client
[session.c]
- update proctitle for proto 1, too
[channels.h nchan.c serverloop.c session.c sshd.c]
- use c-style comments
- deraadt@cvs.openbsd.org
[scp.c]
- more atomicio
- markus@cvs.openbsd.org
[channels.c]
- set O_NONBLOCK
[ssh.1]
- update AUTHOR
[readconf.c ssh-keygen.c ssh.h]
- default DSA key file ~/.ssh/id_dsa
[clientloop.c]
- typo, rm verbose debug
- deraadt@cvs.openbsd.org
[ssh-keygen.1]
- document DSA use of ssh-keygen
[sshd.8]
- a start at describing what i understand of the DSA side
[ssh-keygen.1]
- document -X and -x
[ssh-keygen.c]
- simplify usage
- markus@cvs.openbsd.org
[sshd.8]
- there is no rhosts_dsa
[ssh-keygen.1]
- document -y, update -X,-x
[nchan.c]
- fix close for non-open ssh1 channels
[servconf.c servconf.h ssh.h sshd.8 sshd.c ]
- s/DsaKey/HostDSAKey/, document option
[sshconnect2.c]
- respect number_of_password_prompts
[channels.c channels.h servconf.c servconf.h session.c sshd.8]
- GatewayPorts for sshd, ok deraadt@
[ssh-add.1 ssh-agent.1 ssh.1]
- more doc on: DSA, id_dsa, known_hosts2, authorized_keys2
[ssh.1]
- more info on proto 2
[sshd.8]
- sync AUTHOR w/ ssh.1
[key.c key.h sshconnect.c]
- print key type when talking about host keys
[packet.c]
- clear padding in ssh2
[dsa.c key.c radix.c ssh.h sshconnect1.c uuencode.c uuencode.h]
- replace broken uuencode w/ libc b64_ntop
[auth2.c]
- log failure before sending the reply
[key.c radix.c uuencode.c]
- remote trailing comments before calling __b64_pton
[auth2.c readconf.c readconf.h servconf.c servconf.h ssh.1]
[sshconnect2.c sshd.8]
- add DSAAuthetication option to ssh/sshd, document SSH2 in sshd.8
- Bring in b64_ntop and b64_pton from OpenBSD libc (bsd-base64.[ch])
2000-05-07 10:03:14 +08:00
|
|
|
}
|
2000-11-13 19:57:25 +08:00
|
|
|
k = key_from_blob(blob, n);
|
2001-12-07 00:41:41 +08:00
|
|
|
xfree(blob);
|
2000-05-30 11:44:51 +08:00
|
|
|
if (k == NULL) {
|
2000-11-13 19:57:25 +08:00
|
|
|
error("key_read: key_from_blob %s failed", cp);
|
|
|
|
return -1;
|
2000-05-30 11:44:51 +08:00
|
|
|
}
|
2000-11-13 19:57:25 +08:00
|
|
|
if (k->type != type) {
|
|
|
|
error("key_read: type mismatch: encoding error");
|
|
|
|
key_free(k);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/*XXXX*/
|
|
|
|
if (ret->type == KEY_RSA) {
|
|
|
|
if (ret->rsa != NULL)
|
|
|
|
RSA_free(ret->rsa);
|
|
|
|
ret->rsa = k->rsa;
|
|
|
|
k->rsa = NULL;
|
|
|
|
success = 1;
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
RSA_print_fp(stderr, ret->rsa, 8);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
if (ret->dsa != NULL)
|
|
|
|
DSA_free(ret->dsa);
|
|
|
|
ret->dsa = k->dsa;
|
|
|
|
k->dsa = NULL;
|
|
|
|
success = 1;
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
DSA_print_fp(stderr, ret->dsa, 8);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/*XXXX*/
|
2001-12-07 00:41:41 +08:00
|
|
|
key_free(k);
|
2000-11-13 19:57:25 +08:00
|
|
|
if (success != 1)
|
|
|
|
break;
|
2000-05-30 11:44:51 +08:00
|
|
|
/* advance cp: skip whitespace and data */
|
|
|
|
while (*cp == ' ' || *cp == '\t')
|
|
|
|
cp++;
|
|
|
|
while (*cp != '\0' && *cp != ' ' && *cp != '\t')
|
|
|
|
cp++;
|
|
|
|
*cpp = cp;
|
2000-03-26 11:04:51 +08:00
|
|
|
break;
|
|
|
|
default:
|
2000-04-29 21:57:08 +08:00
|
|
|
fatal("key_read: bad key type: %d", ret->type);
|
2000-03-26 11:04:51 +08:00
|
|
|
break;
|
|
|
|
}
|
2000-11-13 19:57:25 +08:00
|
|
|
return success;
|
2000-03-26 11:04:51 +08:00
|
|
|
}
|
|
|
|
int
|
|
|
|
key_write(Key *key, FILE *f)
|
|
|
|
{
|
|
|
|
int success = 0;
|
2000-12-22 09:43:59 +08:00
|
|
|
u_int bits = 0;
|
2000-03-26 11:04:51 +08:00
|
|
|
|
2000-11-13 19:57:25 +08:00
|
|
|
if (key->type == KEY_RSA1 && key->rsa != NULL) {
|
2000-03-26 11:04:51 +08:00
|
|
|
/* size of modulus 'n' */
|
|
|
|
bits = BN_num_bits(key->rsa->n);
|
|
|
|
fprintf(f, "%u", bits);
|
|
|
|
if (write_bignum(f, key->rsa->e) &&
|
|
|
|
write_bignum(f, key->rsa->n)) {
|
|
|
|
success = 1;
|
|
|
|
} else {
|
|
|
|
error("key_write: failed for RSA key");
|
|
|
|
}
|
2000-11-13 19:57:25 +08:00
|
|
|
} else if ((key->type == KEY_DSA && key->dsa != NULL) ||
|
|
|
|
(key->type == KEY_RSA && key->rsa != NULL)) {
|
2000-04-29 21:57:08 +08:00
|
|
|
int len, n;
|
2000-12-22 09:43:59 +08:00
|
|
|
u_char *blob, *uu;
|
2000-11-13 19:57:25 +08:00
|
|
|
key_to_blob(key, &blob, &len);
|
2000-04-29 21:57:08 +08:00
|
|
|
uu = xmalloc(2*len);
|
- Remove references to SSLeay.
- Big OpenBSD CVS update
- markus@cvs.openbsd.org
[clientloop.c]
- typo
[session.c]
- update proctitle on pty alloc/dealloc, e.g. w/ windows client
[session.c]
- update proctitle for proto 1, too
[channels.h nchan.c serverloop.c session.c sshd.c]
- use c-style comments
- deraadt@cvs.openbsd.org
[scp.c]
- more atomicio
- markus@cvs.openbsd.org
[channels.c]
- set O_NONBLOCK
[ssh.1]
- update AUTHOR
[readconf.c ssh-keygen.c ssh.h]
- default DSA key file ~/.ssh/id_dsa
[clientloop.c]
- typo, rm verbose debug
- deraadt@cvs.openbsd.org
[ssh-keygen.1]
- document DSA use of ssh-keygen
[sshd.8]
- a start at describing what i understand of the DSA side
[ssh-keygen.1]
- document -X and -x
[ssh-keygen.c]
- simplify usage
- markus@cvs.openbsd.org
[sshd.8]
- there is no rhosts_dsa
[ssh-keygen.1]
- document -y, update -X,-x
[nchan.c]
- fix close for non-open ssh1 channels
[servconf.c servconf.h ssh.h sshd.8 sshd.c ]
- s/DsaKey/HostDSAKey/, document option
[sshconnect2.c]
- respect number_of_password_prompts
[channels.c channels.h servconf.c servconf.h session.c sshd.8]
- GatewayPorts for sshd, ok deraadt@
[ssh-add.1 ssh-agent.1 ssh.1]
- more doc on: DSA, id_dsa, known_hosts2, authorized_keys2
[ssh.1]
- more info on proto 2
[sshd.8]
- sync AUTHOR w/ ssh.1
[key.c key.h sshconnect.c]
- print key type when talking about host keys
[packet.c]
- clear padding in ssh2
[dsa.c key.c radix.c ssh.h sshconnect1.c uuencode.c uuencode.h]
- replace broken uuencode w/ libc b64_ntop
[auth2.c]
- log failure before sending the reply
[key.c radix.c uuencode.c]
- remote trailing comments before calling __b64_pton
[auth2.c readconf.c readconf.h servconf.c servconf.h ssh.1]
[sshconnect2.c sshd.8]
- add DSAAuthetication option to ssh/sshd, document SSH2 in sshd.8
- Bring in b64_ntop and b64_pton from OpenBSD libc (bsd-base64.[ch])
2000-05-07 10:03:14 +08:00
|
|
|
n = uuencode(blob, len, uu, 2*len);
|
|
|
|
if (n > 0) {
|
2000-11-13 19:57:25 +08:00
|
|
|
fprintf(f, "%s %s", key_ssh_name(key), uu);
|
- Remove references to SSLeay.
- Big OpenBSD CVS update
- markus@cvs.openbsd.org
[clientloop.c]
- typo
[session.c]
- update proctitle on pty alloc/dealloc, e.g. w/ windows client
[session.c]
- update proctitle for proto 1, too
[channels.h nchan.c serverloop.c session.c sshd.c]
- use c-style comments
- deraadt@cvs.openbsd.org
[scp.c]
- more atomicio
- markus@cvs.openbsd.org
[channels.c]
- set O_NONBLOCK
[ssh.1]
- update AUTHOR
[readconf.c ssh-keygen.c ssh.h]
- default DSA key file ~/.ssh/id_dsa
[clientloop.c]
- typo, rm verbose debug
- deraadt@cvs.openbsd.org
[ssh-keygen.1]
- document DSA use of ssh-keygen
[sshd.8]
- a start at describing what i understand of the DSA side
[ssh-keygen.1]
- document -X and -x
[ssh-keygen.c]
- simplify usage
- markus@cvs.openbsd.org
[sshd.8]
- there is no rhosts_dsa
[ssh-keygen.1]
- document -y, update -X,-x
[nchan.c]
- fix close for non-open ssh1 channels
[servconf.c servconf.h ssh.h sshd.8 sshd.c ]
- s/DsaKey/HostDSAKey/, document option
[sshconnect2.c]
- respect number_of_password_prompts
[channels.c channels.h servconf.c servconf.h session.c sshd.8]
- GatewayPorts for sshd, ok deraadt@
[ssh-add.1 ssh-agent.1 ssh.1]
- more doc on: DSA, id_dsa, known_hosts2, authorized_keys2
[ssh.1]
- more info on proto 2
[sshd.8]
- sync AUTHOR w/ ssh.1
[key.c key.h sshconnect.c]
- print key type when talking about host keys
[packet.c]
- clear padding in ssh2
[dsa.c key.c radix.c ssh.h sshconnect1.c uuencode.c uuencode.h]
- replace broken uuencode w/ libc b64_ntop
[auth2.c]
- log failure before sending the reply
[key.c radix.c uuencode.c]
- remote trailing comments before calling __b64_pton
[auth2.c readconf.c readconf.h servconf.c servconf.h ssh.1]
[sshconnect2.c sshd.8]
- add DSAAuthetication option to ssh/sshd, document SSH2 in sshd.8
- Bring in b64_ntop and b64_pton from OpenBSD libc (bsd-base64.[ch])
2000-05-07 10:03:14 +08:00
|
|
|
success = 1;
|
|
|
|
}
|
2000-04-29 21:57:08 +08:00
|
|
|
xfree(blob);
|
|
|
|
xfree(uu);
|
2000-03-26 11:04:51 +08:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
- Remove references to SSLeay.
- Big OpenBSD CVS update
- markus@cvs.openbsd.org
[clientloop.c]
- typo
[session.c]
- update proctitle on pty alloc/dealloc, e.g. w/ windows client
[session.c]
- update proctitle for proto 1, too
[channels.h nchan.c serverloop.c session.c sshd.c]
- use c-style comments
- deraadt@cvs.openbsd.org
[scp.c]
- more atomicio
- markus@cvs.openbsd.org
[channels.c]
- set O_NONBLOCK
[ssh.1]
- update AUTHOR
[readconf.c ssh-keygen.c ssh.h]
- default DSA key file ~/.ssh/id_dsa
[clientloop.c]
- typo, rm verbose debug
- deraadt@cvs.openbsd.org
[ssh-keygen.1]
- document DSA use of ssh-keygen
[sshd.8]
- a start at describing what i understand of the DSA side
[ssh-keygen.1]
- document -X and -x
[ssh-keygen.c]
- simplify usage
- markus@cvs.openbsd.org
[sshd.8]
- there is no rhosts_dsa
[ssh-keygen.1]
- document -y, update -X,-x
[nchan.c]
- fix close for non-open ssh1 channels
[servconf.c servconf.h ssh.h sshd.8 sshd.c ]
- s/DsaKey/HostDSAKey/, document option
[sshconnect2.c]
- respect number_of_password_prompts
[channels.c channels.h servconf.c servconf.h session.c sshd.8]
- GatewayPorts for sshd, ok deraadt@
[ssh-add.1 ssh-agent.1 ssh.1]
- more doc on: DSA, id_dsa, known_hosts2, authorized_keys2
[ssh.1]
- more info on proto 2
[sshd.8]
- sync AUTHOR w/ ssh.1
[key.c key.h sshconnect.c]
- print key type when talking about host keys
[packet.c]
- clear padding in ssh2
[dsa.c key.c radix.c ssh.h sshconnect1.c uuencode.c uuencode.h]
- replace broken uuencode w/ libc b64_ntop
[auth2.c]
- log failure before sending the reply
[key.c radix.c uuencode.c]
- remote trailing comments before calling __b64_pton
[auth2.c readconf.c readconf.h servconf.c servconf.h ssh.1]
[sshconnect2.c sshd.8]
- add DSAAuthetication option to ssh/sshd, document SSH2 in sshd.8
- Bring in b64_ntop and b64_pton from OpenBSD libc (bsd-base64.[ch])
2000-05-07 10:03:14 +08:00
|
|
|
char *
|
|
|
|
key_type(Key *k)
|
|
|
|
{
|
|
|
|
switch (k->type) {
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_RSA1:
|
|
|
|
return "RSA1";
|
|
|
|
break;
|
- Remove references to SSLeay.
- Big OpenBSD CVS update
- markus@cvs.openbsd.org
[clientloop.c]
- typo
[session.c]
- update proctitle on pty alloc/dealloc, e.g. w/ windows client
[session.c]
- update proctitle for proto 1, too
[channels.h nchan.c serverloop.c session.c sshd.c]
- use c-style comments
- deraadt@cvs.openbsd.org
[scp.c]
- more atomicio
- markus@cvs.openbsd.org
[channels.c]
- set O_NONBLOCK
[ssh.1]
- update AUTHOR
[readconf.c ssh-keygen.c ssh.h]
- default DSA key file ~/.ssh/id_dsa
[clientloop.c]
- typo, rm verbose debug
- deraadt@cvs.openbsd.org
[ssh-keygen.1]
- document DSA use of ssh-keygen
[sshd.8]
- a start at describing what i understand of the DSA side
[ssh-keygen.1]
- document -X and -x
[ssh-keygen.c]
- simplify usage
- markus@cvs.openbsd.org
[sshd.8]
- there is no rhosts_dsa
[ssh-keygen.1]
- document -y, update -X,-x
[nchan.c]
- fix close for non-open ssh1 channels
[servconf.c servconf.h ssh.h sshd.8 sshd.c ]
- s/DsaKey/HostDSAKey/, document option
[sshconnect2.c]
- respect number_of_password_prompts
[channels.c channels.h servconf.c servconf.h session.c sshd.8]
- GatewayPorts for sshd, ok deraadt@
[ssh-add.1 ssh-agent.1 ssh.1]
- more doc on: DSA, id_dsa, known_hosts2, authorized_keys2
[ssh.1]
- more info on proto 2
[sshd.8]
- sync AUTHOR w/ ssh.1
[key.c key.h sshconnect.c]
- print key type when talking about host keys
[packet.c]
- clear padding in ssh2
[dsa.c key.c radix.c ssh.h sshconnect1.c uuencode.c uuencode.h]
- replace broken uuencode w/ libc b64_ntop
[auth2.c]
- log failure before sending the reply
[key.c radix.c uuencode.c]
- remote trailing comments before calling __b64_pton
[auth2.c readconf.c readconf.h servconf.c servconf.h ssh.1]
[sshconnect2.c sshd.8]
- add DSAAuthetication option to ssh/sshd, document SSH2 in sshd.8
- Bring in b64_ntop and b64_pton from OpenBSD libc (bsd-base64.[ch])
2000-05-07 10:03:14 +08:00
|
|
|
case KEY_RSA:
|
|
|
|
return "RSA";
|
|
|
|
break;
|
|
|
|
case KEY_DSA:
|
|
|
|
return "DSA";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return "unknown";
|
|
|
|
}
|
2000-11-13 19:57:25 +08:00
|
|
|
char *
|
|
|
|
key_ssh_name(Key *k)
|
|
|
|
{
|
|
|
|
switch (k->type) {
|
|
|
|
case KEY_RSA:
|
|
|
|
return "ssh-rsa";
|
|
|
|
break;
|
|
|
|
case KEY_DSA:
|
|
|
|
return "ssh-dss";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return "ssh-unknown";
|
|
|
|
}
|
|
|
|
u_int
|
2001-12-07 02:00:18 +08:00
|
|
|
key_size(Key *k)
|
|
|
|
{
|
2000-08-23 08:46:23 +08:00
|
|
|
switch (k->type) {
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_RSA1:
|
2000-08-23 08:46:23 +08:00
|
|
|
case KEY_RSA:
|
|
|
|
return BN_num_bits(k->rsa->n);
|
|
|
|
break;
|
|
|
|
case KEY_DSA:
|
|
|
|
return BN_num_bits(k->dsa->p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2000-11-13 19:57:25 +08:00
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static RSA *
|
2000-12-22 09:43:59 +08:00
|
|
|
rsa_generate_private_key(u_int bits)
|
2000-11-13 19:57:25 +08:00
|
|
|
{
|
2001-02-05 20:42:17 +08:00
|
|
|
RSA *private;
|
|
|
|
private = RSA_generate_key(bits, 35, NULL, NULL);
|
|
|
|
if (private == NULL)
|
|
|
|
fatal("rsa_generate_private_key: key generation failed.");
|
|
|
|
return private;
|
2000-11-13 19:57:25 +08:00
|
|
|
}
|
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static DSA*
|
2000-12-22 09:43:59 +08:00
|
|
|
dsa_generate_private_key(u_int bits)
|
2000-11-13 19:57:25 +08:00
|
|
|
{
|
|
|
|
DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
|
|
|
|
if (private == NULL)
|
|
|
|
fatal("dsa_generate_private_key: DSA_generate_parameters failed");
|
|
|
|
if (!DSA_generate_key(private))
|
2001-02-05 20:42:17 +08:00
|
|
|
fatal("dsa_generate_private_key: DSA_generate_key failed.");
|
|
|
|
if (private == NULL)
|
|
|
|
fatal("dsa_generate_private_key: NULL.");
|
2000-11-13 19:57:25 +08:00
|
|
|
return private;
|
|
|
|
}
|
|
|
|
|
|
|
|
Key *
|
2000-12-22 09:43:59 +08:00
|
|
|
key_generate(int type, u_int bits)
|
2000-11-13 19:57:25 +08:00
|
|
|
{
|
|
|
|
Key *k = key_new(KEY_UNSPEC);
|
|
|
|
switch (type) {
|
2001-02-05 20:42:17 +08:00
|
|
|
case KEY_DSA:
|
2000-11-13 19:57:25 +08:00
|
|
|
k->dsa = dsa_generate_private_key(bits);
|
|
|
|
break;
|
|
|
|
case KEY_RSA:
|
|
|
|
case KEY_RSA1:
|
|
|
|
k->rsa = rsa_generate_private_key(bits);
|
|
|
|
break;
|
|
|
|
default:
|
2001-02-05 20:42:17 +08:00
|
|
|
fatal("key_generate: unknown type %d", type);
|
2000-11-13 19:57:25 +08:00
|
|
|
}
|
2001-02-05 20:42:17 +08:00
|
|
|
k->type = type;
|
2000-11-13 19:57:25 +08:00
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
|
|
|
Key *
|
|
|
|
key_from_private(Key *k)
|
|
|
|
{
|
|
|
|
Key *n = NULL;
|
|
|
|
switch (k->type) {
|
2001-02-05 20:42:17 +08:00
|
|
|
case KEY_DSA:
|
2000-11-13 19:57:25 +08:00
|
|
|
n = key_new(k->type);
|
|
|
|
BN_copy(n->dsa->p, k->dsa->p);
|
|
|
|
BN_copy(n->dsa->q, k->dsa->q);
|
|
|
|
BN_copy(n->dsa->g, k->dsa->g);
|
|
|
|
BN_copy(n->dsa->pub_key, k->dsa->pub_key);
|
|
|
|
break;
|
|
|
|
case KEY_RSA:
|
|
|
|
case KEY_RSA1:
|
|
|
|
n = key_new(k->type);
|
|
|
|
BN_copy(n->rsa->n, k->rsa->n);
|
|
|
|
BN_copy(n->rsa->e, k->rsa->e);
|
|
|
|
break;
|
|
|
|
default:
|
2001-02-05 20:42:17 +08:00
|
|
|
fatal("key_from_private: unknown type %d", k->type);
|
2000-11-13 19:57:25 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
key_type_from_name(char *name)
|
|
|
|
{
|
2001-12-07 02:00:18 +08:00
|
|
|
if (strcmp(name, "rsa1") == 0) {
|
2000-11-13 19:57:25 +08:00
|
|
|
return KEY_RSA1;
|
2001-12-07 02:00:18 +08:00
|
|
|
} else if (strcmp(name, "rsa") == 0) {
|
2000-11-13 19:57:25 +08:00
|
|
|
return KEY_RSA;
|
2001-12-07 02:00:18 +08:00
|
|
|
} else if (strcmp(name, "dsa") == 0) {
|
2000-11-13 19:57:25 +08:00
|
|
|
return KEY_DSA;
|
2001-12-07 02:00:18 +08:00
|
|
|
} else if (strcmp(name, "ssh-rsa") == 0) {
|
2000-11-13 19:57:25 +08:00
|
|
|
return KEY_RSA;
|
2001-12-07 02:00:18 +08:00
|
|
|
} else if (strcmp(name, "ssh-dss") == 0) {
|
2000-11-13 19:57:25 +08:00
|
|
|
return KEY_DSA;
|
|
|
|
}
|
2001-03-12 04:01:55 +08:00
|
|
|
debug2("key_type_from_name: unknown key type '%s'", name);
|
2000-11-13 19:57:25 +08:00
|
|
|
return KEY_UNSPEC;
|
|
|
|
}
|
|
|
|
|
2001-04-18 02:11:36 +08:00
|
|
|
int
|
|
|
|
key_names_valid2(const char *names)
|
|
|
|
{
|
|
|
|
char *s, *cp, *p;
|
|
|
|
|
|
|
|
if (names == NULL || strcmp(names, "") == 0)
|
|
|
|
return 0;
|
|
|
|
s = cp = xstrdup(names);
|
|
|
|
for ((p = strsep(&cp, ",")); p && *p != '\0';
|
2001-12-21 11:45:46 +08:00
|
|
|
(p = strsep(&cp, ","))) {
|
2001-04-18 02:11:36 +08:00
|
|
|
switch (key_type_from_name(p)) {
|
|
|
|
case KEY_RSA1:
|
|
|
|
case KEY_UNSPEC:
|
|
|
|
xfree(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug3("key names ok: [%s]", names);
|
|
|
|
xfree(s);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-11-13 19:57:25 +08:00
|
|
|
Key *
|
2001-09-18 13:41:19 +08:00
|
|
|
key_from_blob(u_char *blob, int blen)
|
2000-11-13 19:57:25 +08:00
|
|
|
{
|
|
|
|
Buffer b;
|
|
|
|
char *ktype;
|
|
|
|
int rlen, type;
|
|
|
|
Key *key = NULL;
|
|
|
|
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
dump_base64(stderr, blob, blen);
|
|
|
|
#endif
|
|
|
|
buffer_init(&b);
|
|
|
|
buffer_append(&b, blob, blen);
|
|
|
|
ktype = buffer_get_string(&b, NULL);
|
|
|
|
type = key_type_from_name(ktype);
|
|
|
|
|
2001-12-07 02:00:18 +08:00
|
|
|
switch (type) {
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_RSA:
|
|
|
|
key = key_new(type);
|
|
|
|
buffer_get_bignum2(&b, key->rsa->e);
|
2001-01-18 10:04:35 +08:00
|
|
|
buffer_get_bignum2(&b, key->rsa->n);
|
2000-11-13 19:57:25 +08:00
|
|
|
#ifdef DEBUG_PK
|
|
|
|
RSA_print_fp(stderr, key->rsa, 8);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case KEY_DSA:
|
|
|
|
key = key_new(type);
|
|
|
|
buffer_get_bignum2(&b, key->dsa->p);
|
|
|
|
buffer_get_bignum2(&b, key->dsa->q);
|
|
|
|
buffer_get_bignum2(&b, key->dsa->g);
|
|
|
|
buffer_get_bignum2(&b, key->dsa->pub_key);
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
DSA_print_fp(stderr, key->dsa, 8);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case KEY_UNSPEC:
|
|
|
|
key = key_new(type);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("key_from_blob: cannot handle type %s", ktype);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rlen = buffer_len(&b);
|
|
|
|
if (key != NULL && rlen != 0)
|
|
|
|
error("key_from_blob: remaining bytes in key blob %d", rlen);
|
|
|
|
xfree(ktype);
|
|
|
|
buffer_free(&b);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-12-22 09:43:59 +08:00
|
|
|
key_to_blob(Key *key, u_char **blobp, u_int *lenp)
|
2000-11-13 19:57:25 +08:00
|
|
|
{
|
|
|
|
Buffer b;
|
|
|
|
int len;
|
2000-12-22 09:43:59 +08:00
|
|
|
u_char *buf;
|
2000-11-13 19:57:25 +08:00
|
|
|
|
|
|
|
if (key == NULL) {
|
|
|
|
error("key_to_blob: key == NULL");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
buffer_init(&b);
|
2001-12-07 02:00:18 +08:00
|
|
|
switch (key->type) {
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_DSA:
|
|
|
|
buffer_put_cstring(&b, key_ssh_name(key));
|
|
|
|
buffer_put_bignum2(&b, key->dsa->p);
|
|
|
|
buffer_put_bignum2(&b, key->dsa->q);
|
|
|
|
buffer_put_bignum2(&b, key->dsa->g);
|
|
|
|
buffer_put_bignum2(&b, key->dsa->pub_key);
|
|
|
|
break;
|
|
|
|
case KEY_RSA:
|
|
|
|
buffer_put_cstring(&b, key_ssh_name(key));
|
|
|
|
buffer_put_bignum2(&b, key->rsa->e);
|
2001-01-18 10:04:35 +08:00
|
|
|
buffer_put_bignum2(&b, key->rsa->n);
|
2000-11-13 19:57:25 +08:00
|
|
|
break;
|
|
|
|
default:
|
2001-09-18 13:49:14 +08:00
|
|
|
error("key_to_blob: unsupported key type %d", key->type);
|
|
|
|
buffer_free(&b);
|
|
|
|
return 0;
|
2000-11-13 19:57:25 +08:00
|
|
|
}
|
|
|
|
len = buffer_len(&b);
|
|
|
|
buf = xmalloc(len);
|
|
|
|
memcpy(buf, buffer_ptr(&b), len);
|
|
|
|
memset(buffer_ptr(&b), 0, len);
|
|
|
|
buffer_free(&b);
|
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = len;
|
|
|
|
if (blobp != NULL)
|
|
|
|
*blobp = buf;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
key_sign(
|
|
|
|
Key *key,
|
2000-12-22 09:43:59 +08:00
|
|
|
u_char **sigp, int *lenp,
|
|
|
|
u_char *data, int datalen)
|
2000-11-13 19:57:25 +08:00
|
|
|
{
|
2001-12-07 02:00:18 +08:00
|
|
|
switch (key->type) {
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_DSA:
|
|
|
|
return ssh_dss_sign(key, sigp, lenp, data, datalen);
|
|
|
|
break;
|
|
|
|
case KEY_RSA:
|
|
|
|
return ssh_rsa_sign(key, sigp, lenp, data, datalen);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("key_sign: illegal key type %d", key->type);
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
key_verify(
|
|
|
|
Key *key,
|
2000-12-22 09:43:59 +08:00
|
|
|
u_char *signature, int signaturelen,
|
|
|
|
u_char *data, int datalen)
|
2000-11-13 19:57:25 +08:00
|
|
|
{
|
2001-06-25 12:42:20 +08:00
|
|
|
if (signaturelen == 0)
|
|
|
|
return -1;
|
|
|
|
|
2001-12-07 02:00:18 +08:00
|
|
|
switch (key->type) {
|
2000-11-13 19:57:25 +08:00
|
|
|
case KEY_DSA:
|
|
|
|
return ssh_dss_verify(key, signature, signaturelen, data, datalen);
|
|
|
|
break;
|
|
|
|
case KEY_RSA:
|
|
|
|
return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("key_verify: illegal key type %d", key->type);
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|