]> andersk Git - gssapi-openssh.git/blobdiff - openssh/key.c
Put kexgss.co on its own line to make merging easier.
[gssapi-openssh.git] / openssh / key.c
index 2cf75ce2867339b6a52c6b235c83d73bc35f9773..06b15d65c1617a74f33b32881ef587e91889031e 100644 (file)
@@ -1,3 +1,4 @@
+/* $OpenBSD: key.c,v 1.69 2007/07/12 05:48:05 ray Exp $ */
 /*
  * read_bignum():
  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  * (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"
-RCSID("$OpenBSD: key.c,v 1.56 2004/07/28 09:40:29 markus Exp $");
+
+#include <sys/types.h>
 
 #include <openssl/evp.h>
 
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
 #include "xmalloc.h"
 #include "key.h"
 #include "rsa.h"
 #include "uuencode.h"
 #include "buffer.h"
-#include "bufaux.h"
 #include "log.h"
 
 Key *
@@ -50,9 +56,8 @@ key_new(int type)
        Key *k;
        RSA *rsa;
        DSA *dsa;
-       k = xmalloc(sizeof(*k));
+       k = xcalloc(1, sizeof(*k));
        k->type = type;
-       k->flags = 0;
        k->dsa = NULL;
        k->rsa = NULL;
        switch (k->type) {
@@ -123,6 +128,8 @@ key_new_private(int type)
 void
 key_free(Key *k)
 {
+       if (k == NULL)
+               fatal("key_free: key is NULL");
        switch (k->type) {
        case KEY_RSA1:
        case KEY_RSA:
@@ -155,19 +162,15 @@ key_equal(const Key *a, const Key *b)
                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:
                fatal("key_equal: bad key type %d", a->type);
-               break;
        }
-       return 0;
 }
 
 u_char*
@@ -209,7 +212,6 @@ key_fingerprint_raw(const Key *k, enum fp_type dgst_type,
                break;
        case KEY_UNSPEC:
                return retval;
-               break;
        default:
                fatal("key_fingerprint_raw: bad key type %d", k->type);
                break;
@@ -231,10 +233,9 @@ static char *
 key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
 {
        char *retval;
-       int i;
+       u_int i;
 
-       retval = xmalloc(dgst_raw_len * 3 + 1);
-       retval[0] = '\0';
+       retval = xcalloc(1, dgst_raw_len * 3 + 1);
        for (i = 0; i < dgst_raw_len; i++) {
                char hex[4];
                snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
@@ -256,7 +257,7 @@ key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
        char *retval;
 
        rounds = (dgst_raw_len / 2) + 1;
-       retval = xmalloc(sizeof(char) * (rounds*6));
+       retval = xcalloc((rounds * 6), sizeof(char));
        retval[j++] = 'x';
        for (i = 0; i < rounds; i++) {
                u_int idx0, idx1, idx2, idx3, idx4;
@@ -530,13 +531,10 @@ key_type(const Key *k)
        switch (k->type) {
        case KEY_RSA1:
                return "RSA1";
-               break;
        case KEY_RSA:
                return "RSA";
-               break;
        case KEY_DSA:
                return "DSA";
-               break;
        }
        return "unknown";
 }
@@ -547,10 +545,8 @@ key_ssh_name(const Key *k)
        switch (k->type) {
        case KEY_RSA:
                return "ssh-rsa";
-               break;
        case KEY_DSA:
                return "ssh-dss";
-               break;
        }
        return "ssh-unknown";
 }
@@ -562,10 +558,8 @@ key_size(const Key *k)
        case KEY_RSA1:
        case KEY_RSA:
                return BN_num_bits(k->rsa->n);
-               break;
        case KEY_DSA:
                return BN_num_bits(k->dsa->p);
-               break;
        }
        return 0;
 }
@@ -574,6 +568,7 @@ static RSA *
 rsa_generate_private_key(u_int bits)
 {
        RSA *private;
+
        private = RSA_generate_key(bits, 35, NULL, NULL);
        if (private == NULL)
                fatal("rsa_generate_private_key: key generation failed.");
@@ -584,6 +579,7 @@ static DSA*
 dsa_generate_private_key(u_int bits)
 {
        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))
@@ -619,16 +615,18 @@ key_from_private(const Key *k)
        switch (k->type) {
        case KEY_DSA:
                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);
+               if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
+                   (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
+                   (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
+                   (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL))
+                       fatal("key_from_private: BN_copy failed");
                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);
+               if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
+                   (BN_copy(n->rsa->e, k->rsa->e) == NULL))
+                       fatal("key_from_private: BN_copy failed");
                break;
        default:
                fatal("key_from_private: unknown type %d", k->type);
@@ -650,7 +648,7 @@ key_type_from_name(char *name)
                return KEY_RSA;
        } else if (strcmp(name, "ssh-dss") == 0) {
                return KEY_DSA;
-       } else if (strcmp(name, "null") == 0){
+       } else if (strcmp(name, "null") == 0) {
                return KEY_NULL;
        }
        debug2("key_type_from_name: unknown key type '%s'", name);
@@ -683,8 +681,8 @@ Key *
 key_from_blob(const u_char *blob, u_int blen)
 {
        Buffer b;
-       char *ktype;
        int rlen, type;
+       char *ktype = NULL;
        Key *key = NULL;
 
 #ifdef DEBUG_PK
@@ -692,24 +690,38 @@ key_from_blob(const u_char *blob, u_int blen)
 #endif
        buffer_init(&b);
        buffer_append(&b, blob, blen);
-       ktype = buffer_get_string(&b, NULL);
+       if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
+               error("key_from_blob: can't read key type");
+               goto out;
+       }
+
        type = key_type_from_name(ktype);
 
        switch (type) {
        case KEY_RSA:
                key = key_new(type);
-               buffer_get_bignum2(&b, key->rsa->e);
-               buffer_get_bignum2(&b, key->rsa->n);
+               if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
+                   buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
+                       error("key_from_blob: can't read rsa key");
+                       key_free(key);
+                       key = NULL;
+                       goto out;
+               }
 #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);
+               if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
+                   buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
+                   buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
+                   buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
+                       error("key_from_blob: can't read dsa key");
+                       key_free(key);
+                       key = NULL;
+                       goto out;
+               }
 #ifdef DEBUG_PK
                DSA_print_fp(stderr, key->dsa, 8);
 #endif
@@ -719,12 +731,14 @@ key_from_blob(const u_char *blob, u_int blen)
                break;
        default:
                error("key_from_blob: cannot handle type %s", ktype);
-               break;
+               goto out;
        }
        rlen = buffer_len(&b);
        if (key != NULL && rlen != 0)
                error("key_from_blob: remaining bytes in key blob %d", rlen);
-       xfree(ktype);
+ out:
+       if (ktype != NULL)
+               xfree(ktype);
        buffer_free(&b);
        return key;
 }
@@ -779,14 +793,11 @@ key_sign(
        switch (key->type) {
        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: invalid key type %d", key->type);
                return -1;
-               break;
        }
 }
 
@@ -806,14 +817,11 @@ key_verify(
        switch (key->type) {
        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: invalid key type %d", key->type);
                return -1;
-               break;
        }
 }
 
@@ -823,7 +831,7 @@ key_demote(const Key *k)
 {
        Key *pk;
 
-       pk = xmalloc(sizeof(*pk));
+       pk = xcalloc(1, sizeof(*pk));
        pk->type = k->type;
        pk->flags = k->flags;
        pk->dsa = NULL;
This page took 0.046822 seconds and 4 git commands to generate.