OSDN Git Service

Enhance all settings encryption.
[ffftp/ffftp.git] / putty / SSHPUBK.C
index 7b5a690..f13e33b 100644 (file)
@@ -67,14 +67,15 @@ static int loadrsakey_main(FILE * fp, struct RSAKey *key, int pub_only,
     i += 4;\r
 \r
     /* Now the serious stuff. An ordinary SSH-1 public key. */\r
-    i += makekey(buf + i, len, key, NULL, 1);\r
-    if (i < 0)\r
+    = makekey(buf + i, len, key, NULL, 1);\r
+    if (j < 0)\r
        goto end;                      /* overran */\r
+    i += j;\r
 \r
     /* Next, the comment field. */\r
-    j = GET_32BIT(buf + i);\r
+    j = toint(GET_32BIT(buf + i));\r
     i += 4;\r
-    if (len - i < j)\r
+    if (j < 0 || len - i < j)\r
        goto end;\r
     comment = snewn(j + 1, char);\r
     if (comment) {\r
@@ -108,7 +109,7 @@ static int loadrsakey_main(FILE * fp, struct RSAKey *key, int pub_only,
        MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));\r
        MD5Final(keybuf, &md5c);\r
        des3_decrypt_pubkey(keybuf, buf + i, (len - i + 7) & ~7);\r
-       memset(keybuf, 0, sizeof(keybuf));      /* burn the evidence */\r
+       smemclr(keybuf, sizeof(keybuf));        /* burn the evidence */\r
     }\r
 \r
     /*\r
@@ -150,7 +151,7 @@ static int loadrsakey_main(FILE * fp, struct RSAKey *key, int pub_only,
        ret = 1;\r
 \r
   end:\r
-    memset(buf, 0, sizeof(buf));       /* burn the evidence */\r
+    smemclr(buf, sizeof(buf));       /* burn the evidence */\r
     return ret;\r
 }\r
 \r
@@ -162,7 +163,7 @@ int loadrsakey(const Filename *filename, struct RSAKey *key, char *passphrase,
     int ret = 0;\r
     const char *error = NULL;\r
 \r
-    fp = f_open(*filename, "rb", FALSE);\r
+    fp = f_open(filename, "rb", FALSE);\r
     if (!fp) {\r
        error = "can't open file";\r
        goto end;\r
@@ -203,7 +204,7 @@ int rsakey_encrypted(const Filename *filename, char **comment)
     FILE *fp;\r
     char buf[64];\r
 \r
-    fp = f_open(*filename, "rb", FALSE);\r
+    fp = f_open(filename, "rb", FALSE);\r
     if (!fp)\r
        return 0;                      /* doesn't even exist */\r
 \r
@@ -241,7 +242,7 @@ int rsakey_pubblob(const Filename *filename, void **blob, int *bloblen,
     *bloblen = 0;\r
     ret = 0;\r
 \r
-    fp = f_open(*filename, "rb", FALSE);\r
+    fp = f_open(filename, "rb", FALSE);\r
     if (!fp) {\r
        error = "can't open file";\r
        goto end;\r
@@ -257,8 +258,8 @@ int rsakey_pubblob(const Filename *filename, void **blob, int *bloblen,
            *blob = rsa_public_blob(&key, bloblen);\r
            freersakey(&key);\r
            ret = 1;\r
-           fp = NULL;\r
        }\r
+       fp = NULL; /* loadrsakey_main unconditionally closes fp */\r
     } else {\r
        error = "not an SSH-1 RSA file";\r
     }\r
@@ -358,13 +359,13 @@ int saversakey(const Filename *filename, struct RSAKey *key, char *passphrase)
        MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));\r
        MD5Final(keybuf, &md5c);\r
        des3_encrypt_pubkey(keybuf, estart, p - estart);\r
-       memset(keybuf, 0, sizeof(keybuf));      /* burn the evidence */\r
+       smemclr(keybuf, sizeof(keybuf));        /* burn the evidence */\r
     }\r
 \r
     /*\r
      * Done. Write the result to the file.\r
      */\r
-    fp = f_open(*filename, "wb", TRUE);\r
+    fp = f_open(filename, "wb", TRUE);\r
     if (fp) {\r
        int ret = (fwrite(buf, 1, p - buf, fp) == (size_t) (p - buf));\r
         if (fclose(fp))\r
@@ -462,7 +463,7 @@ static int read_header(FILE * fp, char *header)
     int len = 39;\r
     int c;\r
 \r
-    while (len > 0) {\r
+    while (1) {\r
        c = fgetc(fp);\r
        if (c == '\n' || c == '\r' || c == EOF)\r
            return 0;                  /* failure */\r
@@ -632,7 +633,7 @@ struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
     encryption = comment = mac = NULL;\r
     public_blob = private_blob = NULL;\r
 \r
-    fp = f_open(*filename, "rb", FALSE);\r
+    fp = f_open(filename, "rb", FALSE);\r
     if (!fp) {\r
        error = "can't open file";\r
        goto error;\r
@@ -647,6 +648,11 @@ struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
        /* this is an old key file; warn and then continue */\r
        old_keyfile_warning();\r
        old_fmt = 1;\r
+    } else if (0 == strncmp(header, "PuTTY-User-Key-File-", 20)) {\r
+       /* this is a key file FROM THE FUTURE; refuse it, but with a\r
+         * more specific error message than the generic one below */\r
+       error = "PuTTY key format too new";\r
+       goto error;\r
     } else {\r
        error = "not a PuTTY SSH-2 private key";\r
        goto error;\r
@@ -674,7 +680,6 @@ struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
        cipher = 0;\r
        cipherblk = 1;\r
     } else {\r
-       sfree(encryption);\r
        goto error;\r
     }\r
 \r
@@ -794,14 +799,14 @@ struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
 \r
            hmac_sha1_simple(mackey, 20, macdata, maclen, binary);\r
 \r
-           memset(mackey, 0, sizeof(mackey));\r
-           memset(&s, 0, sizeof(s));\r
+           smemclr(mackey, sizeof(mackey));\r
+           smemclr(&s, sizeof(s));\r
        } else {\r
            SHA_Simple(macdata, maclen, binary);\r
        }\r
 \r
        if (free_macdata) {\r
-           memset(macdata, 0, maclen);\r
+           smemclr(macdata, maclen);\r
            sfree(macdata);\r
        }\r
 \r
@@ -881,7 +886,7 @@ unsigned char *ssh2_userkey_loadpub(const Filename *filename, char **algorithm,
 \r
     public_blob = NULL;\r
 \r
-    fp = f_open(*filename, "rb", FALSE);\r
+    fp = f_open(filename, "rb", FALSE);\r
     if (!fp) {\r
        error = "can't open file";\r
        goto error;\r
@@ -891,7 +896,10 @@ unsigned char *ssh2_userkey_loadpub(const Filename *filename, char **algorithm,
     if (!read_header(fp, header)\r
        || (0 != strcmp(header, "PuTTY-User-Key-File-2") &&\r
            0 != strcmp(header, "PuTTY-User-Key-File-1"))) {\r
-       error = "not a PuTTY SSH-2 private key";\r
+        if (0 == strncmp(header, "PuTTY-User-Key-File-", 20))\r
+            error = "PuTTY key format too new";\r
+        else\r
+            error = "not a PuTTY SSH-2 private key";\r
        goto error;\r
     }\r
     error = "file format error";\r
@@ -962,7 +970,7 @@ int ssh2_userkey_encrypted(const Filename *filename, char **commentptr)
     if (commentptr)\r
        *commentptr = NULL;\r
 \r
-    fp = f_open(*filename, "rb", FALSE);\r
+    fp = f_open(filename, "rb", FALSE);\r
     if (!fp)\r
        return 0;\r
     if (!read_header(fp, header)\r
@@ -1000,6 +1008,8 @@ int ssh2_userkey_encrypted(const Filename *filename, char **commentptr)
 \r
     if (commentptr)\r
        *commentptr = comment;\r
+    else\r
+        sfree(comment);\r
 \r
     fclose(fp);\r
     if (!strcmp(b, "aes256-cbc"))\r
@@ -1116,10 +1126,10 @@ int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
            SHA_Bytes(&s, passphrase, strlen(passphrase));\r
        SHA_Final(&s, mackey);\r
        hmac_sha1_simple(mackey, 20, macdata, maclen, priv_mac);\r
-       memset(macdata, 0, maclen);\r
+       smemclr(macdata, maclen);\r
        sfree(macdata);\r
-       memset(mackey, 0, sizeof(mackey));\r
-       memset(&s, 0, sizeof(s));\r
+       smemclr(mackey, sizeof(mackey));\r
+       smemclr(&s, sizeof(s));\r
     }\r
 \r
     if (passphrase) {\r
@@ -1139,11 +1149,11 @@ int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
        aes256_encrypt_pubkey(key, priv_blob_encrypted,\r
                              priv_encrypted_len);\r
 \r
-       memset(key, 0, sizeof(key));\r
-       memset(&s, 0, sizeof(s));\r
+       smemclr(key, sizeof(key));\r
+       smemclr(&s, sizeof(s));\r
     }\r
 \r
-    fp = f_open(*filename, "w", TRUE);\r
+    fp = f_open(filename, "w", TRUE);\r
     if (!fp)\r
        return 0;\r
     fprintf(fp, "PuTTY-User-Key-File-2: %s\n", key->alg->name);\r
@@ -1160,7 +1170,7 @@ int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
     fclose(fp);\r
 \r
     sfree(pub_blob);\r
-    memset(priv_blob, 0, priv_blob_len);\r
+    smemclr(priv_blob, priv_blob_len);\r
     sfree(priv_blob);\r
     sfree(priv_blob_encrypted);\r
     return 1;\r
@@ -1179,7 +1189,7 @@ int key_type(const Filename *filename)
     const char openssh_sig[] = "-----BEGIN ";\r
     int i;\r
 \r
-    fp = f_open(*filename, "r", FALSE);\r
+    fp = f_open(filename, "r", FALSE);\r
     if (!fp)\r
        return SSH_KEYTYPE_UNOPENABLE;\r
     i = fread(buf, 1, sizeof(buf), fp);\r