Fix various trivial compiler warnings
[u/mdw/putty] / sshrsa.c
index c933862..36069ad 100644 (file)
--- a/sshrsa.c
+++ b/sshrsa.c
@@ -9,8 +9,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
-#include <windows.h> // FIXME
-#include "putty.h" // FIXME
 
 #include "ssh.h"
 
@@ -208,7 +206,7 @@ static void *rsa2_newkey(char *data, int len) {
     if (!rsa) return NULL;
     getstring(&data, &len, &p, &slen);
 
-    if (!p || memcmp(p, "ssh-rsa", 7)) {
+    if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
        sfree(rsa);
        return NULL;
     }
@@ -309,6 +307,36 @@ static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
     return rsa;
 }
 
+static void *rsa2_openssh_createkey(unsigned char **blob, int *len) {
+    char **b = (char **)blob;
+    struct RSAKey *rsa;
+
+    rsa = smalloc(sizeof(struct RSAKey));
+    if (!rsa) return NULL;
+    rsa->comment = NULL;
+
+    rsa->modulus = getmp(b, len);
+    rsa->exponent = getmp(b, len);
+    rsa->private_exponent = getmp(b, len);
+    rsa->iqmp = getmp(b, len);
+    rsa->p = getmp(b, len);
+    rsa->q = getmp(b, len);
+
+    if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent ||
+       !rsa->iqmp || !rsa->p || !rsa->q) {
+       sfree(rsa->modulus);
+       sfree(rsa->exponent);
+       sfree(rsa->private_exponent);
+       sfree(rsa->iqmp);
+       sfree(rsa->p);
+       sfree(rsa->q);
+       sfree(rsa);
+       return NULL;
+    }
+
+    return rsa;
+}
+
 static char *rsa2_fingerprint(void *key) {
     struct RSAKey *rsa = (struct RSAKey *)key;
     struct MD5Context md5c;
@@ -362,6 +390,8 @@ static unsigned char asn1_weird_stuff[] = {
     0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14,
 };
 
+#define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
+
 static int rsa2_verifysig(void *key, char *sig, int siglen,
                         char *data, int datalen) {
     struct RSAKey *rsa = (struct RSAKey *)key;
@@ -389,12 +419,12 @@ static int rsa2_verifysig(void *key, char *sig, int siglen,
     if (bignum_byte(out, bytes-2) != 1)
         ret = 0;
     /* Most of the rest should be FF. */
-    for (i = bytes-3; i >= 20 + sizeof(asn1_weird_stuff); i--) {
+    for (i = bytes-3; i >= 20 + ASN1_LEN; i--) {
         if (bignum_byte(out, i) != 0xFF)
             ret = 0;
     }
     /* Then we expect to see the asn1_weird_stuff. */
-    for (i = 20 + sizeof(asn1_weird_stuff) - 1, j=0; i >= 20; i--,j++) {
+    for (i = 20 + ASN1_LEN - 1, j=0; i >= 20; i--,j++) {
         if (bignum_byte(out, i) != asn1_weird_stuff[j])
             ret = 0;
     }
@@ -422,9 +452,9 @@ unsigned char *rsa2_sign(void *key, char *data, int datalen, int *siglen) {
     bytes = smalloc(nbytes);
 
     bytes[0] = 1;
-    for (i = 1; i < nbytes-20-sizeof(asn1_weird_stuff); i++)
+    for (i = 1; i < nbytes-20-ASN1_LEN; i++)
        bytes[i] = 0xFF;
-    for (i = nbytes-20-sizeof(asn1_weird_stuff), j=0; i < nbytes-20; i++,j++)
+    for (i = nbytes-20-ASN1_LEN, j=0; i < nbytes-20; i++,j++)
        bytes[i] = asn1_weird_stuff[j];
     for (i = nbytes-20, j=0; i < nbytes; i++,j++)
        bytes[i] = hash[j];
@@ -455,6 +485,7 @@ const struct ssh_signkey ssh_rsa = {
     rsa2_public_blob,
     rsa2_private_blob,
     rsa2_createkey,
+    rsa2_openssh_createkey,
     rsa2_fingerprint,
     rsa2_verifysig,
     rsa2_sign,