summaryrefslogtreecommitdiff
path: root/crypto/openssh/key.c
diff options
context:
space:
mode:
authorDag-Erling Smørgrav <des@FreeBSD.org>2002-03-18 09:55:03 +0000
committerDag-Erling Smørgrav <des@FreeBSD.org>2002-03-18 09:55:03 +0000
commitae1f160d56b2d59d406f1af34cbdcc88a9e1b914 (patch)
tree7ac239d263df7247abaf6488a321dac17f2ebce9 /crypto/openssh/key.c
parent1e8db6e2f63ea90b361b3bbc9ebe9990660cb596 (diff)
downloadsrc-test2-ae1f160d56b2d59d406f1af34cbdcc88a9e1b914.tar.gz
src-test2-ae1f160d56b2d59d406f1af34cbdcc88a9e1b914.zip
Notes
Diffstat (limited to 'crypto/openssh/key.c')
-rw-r--r--crypto/openssh/key.c146
1 files changed, 83 insertions, 63 deletions
diff --git a/crypto/openssh/key.c b/crypto/openssh/key.c
index 3b9f9f786214..cda91571affd 100644
--- a/crypto/openssh/key.c
+++ b/crypto/openssh/key.c
@@ -9,7 +9,7 @@
* called by a name other than "ssh" or "Secure Shell".
*
*
- * Copyright (c) 2000 Markus Friedl. All rights reserved.
+ * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -32,7 +32,7 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "includes.h"
-RCSID("$OpenBSD: key.c,v 1.25 2001/04/17 10:53:24 markus Exp $");
+RCSID("$OpenBSD: key.c,v 1.41 2002/02/28 15:46:33 markus Exp $");
#include <openssl/evp.h>
@@ -54,22 +54,31 @@ key_new(int type)
DSA *dsa;
k = xmalloc(sizeof(*k));
k->type = type;
+ k->flags = 0;
k->dsa = NULL;
k->rsa = NULL;
switch (k->type) {
case KEY_RSA1:
case KEY_RSA:
- rsa = RSA_new();
- rsa->n = BN_new();
- rsa->e = BN_new();
+ if ((rsa = RSA_new()) == NULL)
+ fatal("key_new: RSA_new failed");
+ if ((rsa->n = BN_new()) == NULL)
+ fatal("key_new: BN_new failed");
+ if ((rsa->e = BN_new()) == NULL)
+ fatal("key_new: BN_new failed");
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();
+ if ((dsa = DSA_new()) == NULL)
+ fatal("key_new: DSA_new failed");
+ if ((dsa->p = BN_new()) == NULL)
+ fatal("key_new: BN_new failed");
+ if ((dsa->q = BN_new()) == NULL)
+ fatal("key_new: BN_new failed");
+ if ((dsa->g = BN_new()) == NULL)
+ fatal("key_new: BN_new failed");
+ if ((dsa->pub_key = BN_new()) == NULL)
+ fatal("key_new: BN_new failed");
k->dsa = dsa;
break;
case KEY_UNSPEC:
@@ -87,15 +96,22 @@ key_new_private(int 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();
+ if ((k->rsa->d = BN_new()) == NULL)
+ fatal("key_new_private: BN_new failed");
+ if ((k->rsa->iqmp = BN_new()) == NULL)
+ fatal("key_new_private: BN_new failed");
+ if ((k->rsa->q = BN_new()) == NULL)
+ fatal("key_new_private: BN_new failed");
+ if ((k->rsa->p = BN_new()) == NULL)
+ fatal("key_new_private: BN_new failed");
+ if ((k->rsa->dmq1 = BN_new()) == NULL)
+ fatal("key_new_private: BN_new failed");
+ if ((k->rsa->dmp1 = BN_new()) == NULL)
+ fatal("key_new_private: BN_new failed");
break;
case KEY_DSA:
- k->dsa->priv_key = BN_new();
+ if ((k->dsa->priv_key = BN_new()) == NULL)
+ fatal("key_new_private: BN_new failed");
break;
case KEY_UNSPEC:
break;
@@ -153,14 +169,14 @@ key_equal(Key *a, Key *b)
return 0;
}
-u_char*
-key_fingerprint_raw(Key *k, enum fp_type dgst_type, size_t *dgst_raw_length)
+static u_char*
+key_fingerprint_raw(Key *k, enum fp_type dgst_type, u_int *dgst_raw_length)
{
- EVP_MD *md = NULL;
+ const EVP_MD *md = NULL;
EVP_MD_CTX ctx;
u_char *blob = NULL;
u_char *retval = NULL;
- int len = 0;
+ u_int len = 0;
int nlen, elen;
*dgst_raw_length = 0;
@@ -200,8 +216,7 @@ key_fingerprint_raw(Key *k, enum fp_type dgst_type, size_t *dgst_raw_length)
retval = xmalloc(EVP_MAX_MD_SIZE);
EVP_DigestInit(&ctx, md);
EVP_DigestUpdate(&ctx, blob, len);
- EVP_DigestFinal(&ctx, retval, NULL);
- *dgst_raw_length = md->md_size;
+ EVP_DigestFinal(&ctx, retval, dgst_raw_length);
memset(blob, 0, len);
xfree(blob);
} else {
@@ -210,15 +225,15 @@ key_fingerprint_raw(Key *k, enum fp_type dgst_type, size_t *dgst_raw_length)
return retval;
}
-char*
-key_fingerprint_hex(u_char* dgst_raw, size_t dgst_raw_len)
+static char*
+key_fingerprint_hex(u_char* dgst_raw, u_int dgst_raw_len)
{
char *retval;
int i;
retval = xmalloc(dgst_raw_len * 3 + 1);
retval[0] = '\0';
- for(i = 0; i < dgst_raw_len; i++) {
+ for (i = 0; i < dgst_raw_len; i++) {
char hex[4];
snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
strlcat(retval, hex, dgst_raw_len * 3);
@@ -227,8 +242,8 @@ key_fingerprint_hex(u_char* dgst_raw, size_t dgst_raw_len)
return retval;
}
-char*
-key_fingerprint_bubblebabble(u_char* dgst_raw, size_t dgst_raw_len)
+static char*
+key_fingerprint_bubblebabble(u_char* dgst_raw, u_int dgst_raw_len)
{
char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
@@ -279,12 +294,12 @@ key_fingerprint(Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
{
char *retval = NULL;
u_char *dgst_raw;
- size_t dgst_raw_len;
-
+ u_int dgst_raw_len;
+
dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
if (!dgst_raw)
fatal("key_fingerprint: null from key_fingerprint_raw()");
- switch(dgst_rep) {
+ switch (dgst_rep) {
case SSH_FP_HEX:
retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
break;
@@ -308,7 +323,7 @@ key_fingerprint(Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
* last processed (and maybe modified) character. Note that this may modify
* the buffer containing the number.
*/
-int
+static int
read_bignum(char **cpp, BIGNUM * value)
{
char *cp = *cpp;
@@ -344,7 +359,7 @@ read_bignum(char **cpp, BIGNUM * value)
*cpp = cp;
return 1;
}
-int
+static int
write_bignum(FILE *f, BIGNUM *num)
{
char *buf = BN_bn2dec(num);
@@ -353,11 +368,11 @@ write_bignum(FILE *f, BIGNUM *num)
return 0;
}
fprintf(f, " %s", buf);
- xfree(buf);
+ OPENSSL_free(buf);
return 1;
}
-/* returns 1 ok, -1 error, 0 type mismatch */
+/* returns 1 ok, -1 error */
int
key_read(Key *ret, char **cpp)
{
@@ -370,7 +385,7 @@ key_read(Key *ret, char **cpp)
cp = *cpp;
- switch(ret->type) {
+ switch (ret->type) {
case KEY_RSA1:
/* Get number of bits. */
if (*cp < '0' || *cp > '9')
@@ -412,21 +427,22 @@ key_read(Key *ret, char **cpp)
} else if (ret->type != type) {
/* is a key, but different type */
debug3("key_read: type mismatch");
- return 0;
+ return -1;
}
len = 2*strlen(cp);
blob = xmalloc(len);
n = uudecode(cp, blob, len);
if (n < 0) {
error("key_read: uudecode %s failed", cp);
+ xfree(blob);
return -1;
}
k = key_from_blob(blob, n);
+ xfree(blob);
if (k == NULL) {
error("key_read: key_from_blob %s failed", cp);
return -1;
}
- xfree(blob);
if (k->type != type) {
error("key_read: type mismatch: encoding error");
key_free(k);
@@ -453,9 +469,9 @@ key_read(Key *ret, char **cpp)
#endif
}
/*XXXX*/
+ key_free(k);
if (success != 1)
break;
- key_free(k);
/* advance cp: skip whitespace and data */
while (*cp == ' ' || *cp == '\t')
cp++;
@@ -472,8 +488,9 @@ key_read(Key *ret, char **cpp)
int
key_write(Key *key, FILE *f)
{
- int success = 0;
- u_int bits = 0;
+ int n, success = 0;
+ u_int len, bits = 0;
+ u_char *blob, *uu;
if (key->type == KEY_RSA1 && key->rsa != NULL) {
/* size of modulus 'n' */
@@ -487,8 +504,6 @@ key_write(Key *key, FILE *f)
}
} else if ((key->type == KEY_DSA && key->dsa != NULL) ||
(key->type == KEY_RSA && key->rsa != NULL)) {
- int len, n;
- u_char *blob, *uu;
key_to_blob(key, &blob, &len);
uu = xmalloc(2*len);
n = uuencode(blob, len, uu, 2*len);
@@ -531,7 +546,8 @@ key_ssh_name(Key *k)
return "ssh-unknown";
}
u_int
-key_size(Key *k){
+key_size(Key *k)
+{
switch (k->type) {
case KEY_RSA1:
case KEY_RSA:
@@ -544,7 +560,7 @@ key_size(Key *k){
return 0;
}
-RSA *
+static RSA *
rsa_generate_private_key(u_int bits)
{
RSA *private;
@@ -554,7 +570,7 @@ rsa_generate_private_key(u_int bits)
return private;
}
-DSA*
+static DSA*
dsa_generate_private_key(u_int bits)
{
DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
@@ -614,15 +630,15 @@ key_from_private(Key *k)
int
key_type_from_name(char *name)
{
- if (strcmp(name, "rsa1") == 0){
+ if (strcmp(name, "rsa1") == 0) {
return KEY_RSA1;
- } else if (strcmp(name, "rsa") == 0){
+ } else if (strcmp(name, "rsa") == 0) {
return KEY_RSA;
- } else if (strcmp(name, "dsa") == 0){
+ } else if (strcmp(name, "dsa") == 0) {
return KEY_DSA;
- } else if (strcmp(name, "ssh-rsa") == 0){
+ } else if (strcmp(name, "ssh-rsa") == 0) {
return KEY_RSA;
- } else if (strcmp(name, "ssh-dss") == 0){
+ } else if (strcmp(name, "ssh-dss") == 0) {
return KEY_DSA;
}
debug2("key_type_from_name: unknown key type '%s'", name);
@@ -638,7 +654,7 @@ key_names_valid2(const char *names)
return 0;
s = cp = xstrdup(names);
for ((p = strsep(&cp, ",")); p && *p != '\0';
- (p = strsep(&cp, ","))) {
+ (p = strsep(&cp, ","))) {
switch (key_type_from_name(p)) {
case KEY_RSA1:
case KEY_UNSPEC:
@@ -652,7 +668,7 @@ key_names_valid2(const char *names)
}
Key *
-key_from_blob(char *blob, int blen)
+key_from_blob(u_char *blob, int blen)
{
Buffer b;
char *ktype;
@@ -667,7 +683,7 @@ key_from_blob(char *blob, int blen)
ktype = buffer_get_string(&b, NULL);
type = key_type_from_name(ktype);
- switch(type){
+ switch (type) {
case KEY_RSA:
key = key_new(type);
buffer_get_bignum2(&b, key->rsa->e);
@@ -713,7 +729,7 @@ key_to_blob(Key *key, u_char **blobp, u_int *lenp)
return 0;
}
buffer_init(&b);
- switch(key->type){
+ switch (key->type) {
case KEY_DSA:
buffer_put_cstring(&b, key_ssh_name(key));
buffer_put_bignum2(&b, key->dsa->p);
@@ -727,8 +743,9 @@ key_to_blob(Key *key, u_char **blobp, u_int *lenp)
buffer_put_bignum2(&b, key->rsa->n);
break;
default:
- error("key_to_blob: illegal key type %d", key->type);
- break;
+ error("key_to_blob: unsupported key type %d", key->type);
+ buffer_free(&b);
+ return 0;
}
len = buffer_len(&b);
buf = xmalloc(len);
@@ -745,10 +762,10 @@ key_to_blob(Key *key, u_char **blobp, u_int *lenp)
int
key_sign(
Key *key,
- u_char **sigp, int *lenp,
- u_char *data, int datalen)
+ u_char **sigp, u_int *lenp,
+ u_char *data, u_int datalen)
{
- switch(key->type){
+ switch (key->type) {
case KEY_DSA:
return ssh_dss_sign(key, sigp, lenp, data, datalen);
break;
@@ -765,10 +782,13 @@ key_sign(
int
key_verify(
Key *key,
- u_char *signature, int signaturelen,
- u_char *data, int datalen)
+ u_char *signature, u_int signaturelen,
+ u_char *data, u_int datalen)
{
- switch(key->type){
+ if (signaturelen == 0)
+ return -1;
+
+ switch (key->type) {
case KEY_DSA:
return ssh_dss_verify(key, signature, signaturelen, data, datalen);
break;