projects
/
u
/
mdw
/
putty
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Add an extended version of ctrl_alloc which permits you to provide a
[u/mdw/putty]
/
import.c
diff --git
a/import.c
b/import.c
index
2ba2f9a
..
d4b87bc
100644
(file)
--- a/
import.c
+++ b/
import.c
@@
-289,8
+289,8
@@
static int ssh2_read_mpint(void *data, int len, struct mpint_pos *ret)
if (len < 4)
goto error;
if (len < 4)
goto error;
- bytes =
GET_32BIT(d
);
- if (
len < 4+
bytes)
+ bytes =
toint(GET_32BIT(d)
);
+ if (
bytes < 0 || len-4 <
bytes)
goto error;
ret->start = d + 4;
goto error;
ret->start = d + 4;
@@
-370,8
+370,10
@@
static struct openssh_key *load_openssh_key(const Filename *filename,
}
strip_crlf(line);
if (0 == strncmp(line, "-----END ", 9) &&
}
strip_crlf(line);
if (0 == strncmp(line, "-----END ", 9) &&
- 0 == strcmp(line+strlen(line)-16, "PRIVATE KEY-----"))
+ 0 == strcmp(line+strlen(line)-16, "PRIVATE KEY-----")) {
+ sfree(line);
break; /* done */
break; /* done */
+ }
if ((p = strchr(line, ':')) != NULL) {
if (headers_done) {
errmsg = "header found in body of key data";
if ((p = strchr(line, ':')) != NULL) {
if (headers_done) {
errmsg = "header found in body of key data";
@@
-592,12
+594,13
@@
struct ssh2_userkey *openssh_read(const Filename *filename, char *passphrase,
p = key->keyblob;
p = key->keyblob;
- /* Expect the SEQUENCE header. Take its absence as a failure to decrypt. */
+ /* Expect the SEQUENCE header. Take its absence as a failure to
+ * decrypt, if the key was encrypted. */
ret = ber_read_id_len(p, key->keyblob_len, &id, &len, &flags);
p += ret;
if (ret < 0 || id != 16) {
errmsg = "ASN.1 decoding failure";
ret = ber_read_id_len(p, key->keyblob_len, &id, &len, &flags);
p += ret;
if (ret < 0 || id != 16) {
errmsg = "ASN.1 decoding failure";
-
retval = SSH2_WRONG_PASSPHRASE
;
+
retval = key->encrypted ? SSH2_WRONG_PASSPHRASE : NULL
;
goto error;
}
goto error;
}
@@
-629,7
+632,7
@@
struct ssh2_userkey *openssh_read(const Filename *filename, char *passphrase,
if (ret < 0 || id != 2 ||
key->keyblob+key->keyblob_len-p < len) {
errmsg = "ASN.1 decoding failure";
if (ret < 0 || id != 2 ||
key->keyblob+key->keyblob_len-p < len) {
errmsg = "ASN.1 decoding failure";
- retval =
SSH2_WRONG_PASSPHRASE
;
+ retval =
key->encrypted ? SSH2_WRONG_PASSPHRASE : NULL
;
goto error;
}
goto error;
}
@@
-744,6
+747,10
@@
int openssh_write(const Filename *filename, struct ssh2_userkey *key,
struct mpint_pos n, e, d, p, q, iqmp, dmp1, dmq1;
Bignum bd, bp, bq, bdmp1, bdmq1;
struct mpint_pos n, e, d, p, q, iqmp, dmp1, dmq1;
Bignum bd, bp, bq, bdmp1, bdmq1;
+ /*
+ * These blobs were generated from inside PuTTY, so we needn't
+ * treat them as untrusted.
+ */
pos = 4 + GET_32BIT(pubblob);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &e);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &n);
pos = 4 + GET_32BIT(pubblob);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &e);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &n);
@@
-797,6
+804,10
@@
int openssh_write(const Filename *filename, struct ssh2_userkey *key,
int pos;
struct mpint_pos p, q, g, y, x;
int pos;
struct mpint_pos p, q, g, y, x;
+ /*
+ * These blobs were generated from inside PuTTY, so we needn't
+ * treat them as untrusted.
+ */
pos = 4 + GET_32BIT(pubblob);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &p);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &q);
pos = 4 + GET_32BIT(pubblob);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &p);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &q);
@@
-1082,8
+1093,10
@@
static struct sshcom_key *load_sshcom_key(const Filename *filename,
goto error;
}
strip_crlf(line);
goto error;
}
strip_crlf(line);
- if (!strcmp(line, "---- END SSH2 ENCRYPTED PRIVATE KEY ----"))
+ if (!strcmp(line, "---- END SSH2 ENCRYPTED PRIVATE KEY ----")) {
+ sfree(line);
break; /* done */
break; /* done */
+ }
if ((p = strchr(line, ':')) != NULL) {
if (headers_done) {
errmsg = "header found in body of key data";
if ((p = strchr(line, ':')) != NULL) {
if (headers_done) {
errmsg = "header found in body of key data";
@@
-1172,10
+1185,14
@@
static struct sshcom_key *load_sshcom_key(const Filename *filename,
goto error;
}
goto error;
}
+ fclose(fp);
if (errmsg_p) *errmsg_p = NULL;
return ret;
error:
if (errmsg_p) *errmsg_p = NULL;
return ret;
error:
+ if (fp)
+ fclose(fp);
+
if (line) {
smemclr(line, strlen(line));
sfree(line);
if (line) {
smemclr(line, strlen(line));
sfree(line);
@@
-1198,34
+1215,37
@@
int sshcom_encrypted(const Filename *filename, char **comment)
struct sshcom_key *key = load_sshcom_key(filename, NULL);
int pos, len, answer;
struct sshcom_key *key = load_sshcom_key(filename, NULL);
int pos, len, answer;
+ answer = 0;
+
*comment = NULL;
if (!key)
*comment = NULL;
if (!key)
-
return 0
;
+
goto done
;
/*
* Check magic number.
*/
/*
* Check magic number.
*/
- if (GET_32BIT(key->keyblob) != 0x3f6ff9eb)
- return 0; /* key is invalid */
+ if (GET_32BIT(key->keyblob) != 0x3f6ff9eb) {
+ goto done; /* key is invalid */
+ }
/*
* Find the cipher-type string.
*/
/*
* Find the cipher-type string.
*/
- answer = 0;
pos = 8;
if (key->keyblob_len < pos+4)
goto done; /* key is far too short */
pos = 8;
if (key->keyblob_len < pos+4)
goto done; /* key is far too short */
- pos += 4 + GET_32BIT(key->keyblob + pos); /* skip key type */
- if (
key->keyblob_len < pos+
4)
+ len = toint(GET_32BIT(key->keyblob + pos));
+ if (
len < 0 || len > key->keyblob_len - pos -
4)
goto done; /* key is far too short */
goto done; /* key is far too short */
- len = GET_32BIT(key->keyblob + pos); /* find cipher-type length */
- if (key->keyblob_len < pos+4+len)
+ pos += 4 + len; /* skip key type */
+ len = toint(GET_32BIT(key->keyblob + pos)); /* find cipher-type length */
+ if (len < 0 || len > key->keyblob_len - pos - 4)
goto done; /* cipher type string is incomplete */
if (len != 4 || 0 != memcmp(key->keyblob + pos + 4, "none", 4))
answer = 1;
done:
goto done; /* cipher type string is incomplete */
if (len != 4 || 0 != memcmp(key->keyblob + pos + 4, "none", 4))
answer = 1;
done:
- *comment = dupstr(key
->comment
);
+ *comment = dupstr(key
? key->comment : ""
);
smemclr(key->keyblob, key->keyblob_size);
sfree(key->keyblob);
smemclr(key, sizeof(*key));
smemclr(key->keyblob, key->keyblob_size);
sfree(key->keyblob);
smemclr(key, sizeof(*key));
@@
-1235,8
+1255,7
@@
int sshcom_encrypted(const Filename *filename, char **comment)
static int sshcom_read_mpint(void *data, int len, struct mpint_pos *ret)
{
static int sshcom_read_mpint(void *data, int len, struct mpint_pos *ret)
{
- int bits;
- int bytes;
+ unsigned bits, bytes;
unsigned char *d = (unsigned char *) data;
if (len < 4)
unsigned char *d = (unsigned char *) data;
if (len < 4)
@@
-1308,7
+1327,8
@@
struct ssh2_userkey *sshcom_read(const Filename *filename, char *passphrase,
*/
pos = 8;
if (key->keyblob_len < pos+4 ||
*/
pos = 8;
if (key->keyblob_len < pos+4 ||
- (len = GET_32BIT(key->keyblob + pos)) > key->keyblob_len - pos - 4) {
+ (len = toint(GET_32BIT(key->keyblob + pos))) < 0 ||
+ len > key->keyblob_len - pos - 4) {
errmsg = "key blob does not contain a key type string";
goto error;
}
errmsg = "key blob does not contain a key type string";
goto error;
}
@@
-1328,7
+1348,8
@@
struct ssh2_userkey *sshcom_read(const Filename *filename, char *passphrase,
* Determine the cipher type.
*/
if (key->keyblob_len < pos+4 ||
* Determine the cipher type.
*/
if (key->keyblob_len < pos+4 ||
- (len = GET_32BIT(key->keyblob + pos)) > key->keyblob_len - pos - 4) {
+ (len = toint(GET_32BIT(key->keyblob + pos))) < 0 ||
+ len > key->keyblob_len - pos - 4) {
errmsg = "key blob does not contain a cipher type string";
goto error;
}
errmsg = "key blob does not contain a cipher type string";
goto error;
}
@@
-1346,7
+1367,8
@@
struct ssh2_userkey *sshcom_read(const Filename *filename, char *passphrase,
* Get hold of the encrypted part of the key.
*/
if (key->keyblob_len < pos+4 ||
* Get hold of the encrypted part of the key.
*/
if (key->keyblob_len < pos+4 ||
- (len = GET_32BIT(key->keyblob + pos)) > key->keyblob_len - pos - 4) {
+ (len = toint(GET_32BIT(key->keyblob + pos))) < 0 ||
+ len > key->keyblob_len - pos - 4) {
errmsg = "key blob does not contain actual key data";
goto error;
}
errmsg = "key blob does not contain actual key data";
goto error;
}
@@
-1410,7
+1432,7
@@
struct ssh2_userkey *sshcom_read(const Filename *filename, char *passphrase,
/*
* Strip away the containing string to get to the real meat.
*/
/*
* Strip away the containing string to get to the real meat.
*/
- len =
GET_32BIT(ciphertext
);
+ len =
toint(GET_32BIT(ciphertext)
);
if (len < 0 || len > cipherlen-4) {
errmsg = "containing string was ill-formed";
goto error;
if (len < 0 || len > cipherlen-4) {
errmsg = "containing string was ill-formed";
goto error;
@@
-1539,6
+1561,10
@@
int sshcom_write(const Filename *filename, struct ssh2_userkey *key,
int pos;
struct mpint_pos n, e, d, p, q, iqmp;
int pos;
struct mpint_pos n, e, d, p, q, iqmp;
+ /*
+ * These blobs were generated from inside PuTTY, so we needn't
+ * treat them as untrusted.
+ */
pos = 4 + GET_32BIT(pubblob);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &e);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &n);
pos = 4 + GET_32BIT(pubblob);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &e);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &n);
@@
-1564,6
+1590,10
@@
int sshcom_write(const Filename *filename, struct ssh2_userkey *key,
int pos;
struct mpint_pos p, q, g, y, x;
int pos;
struct mpint_pos p, q, g, y, x;
+ /*
+ * These blobs were generated from inside PuTTY, so we needn't
+ * treat them as untrusted.
+ */
pos = 4 + GET_32BIT(pubblob);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &p);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &q);
pos = 4 + GET_32BIT(pubblob);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &p);
pos += ssh2_read_mpint(pubblob+pos, publen-pos, &q);