]> andersk Git - gssapi-openssh.git/blobdiff - openssh/key.c
Import of OpenSSH 4.9p1
[gssapi-openssh.git] / openssh / key.c
index 08c158b59c124b2305bbb2863823f5faa902c2d5..62bf8361d6791caea9bae2be052eb3ae125cc269 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.58 2005/06/17 02:44:32 djm Exp $");
+
+#include <sys/types.h>
 
 #include <openssl/evp.h>
+#include <openbsd-compat/openssl-compat.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 +57,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 +129,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 +163,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 +213,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;
@@ -233,8 +236,7 @@ key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
        char *retval;
        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 +258,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 +532,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 +546,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 +559,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 +569,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 +580,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 +616,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);
@@ -793,14 +792,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;
        }
 }
 
@@ -820,14 +816,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;
        }
 }
 
@@ -837,7 +830,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.044242 seconds and 4 git commands to generate.