md = key->modulus;
ex = key->exponent;
- mdlen = (ssh1_bignum_bitcount(md)+15) / 16;
- exlen = (ssh1_bignum_bitcount(ex)+15) / 16;
+ mdlen = (bignum_bitcount(md)+15) / 16;
+ exlen = (bignum_bitcount(ex)+15) / 16;
return 4 * (mdlen+exlen) + 20;
}
len += sprintf(str+len, "0x");
- nibbles = (3 + ssh1_bignum_bitcount(ex))/4; if (nibbles<1) nibbles=1;
+ nibbles = (3 + bignum_bitcount(ex))/4; if (nibbles<1) nibbles=1;
for (i=nibbles; i-- ;)
str[len++] = hex[(bignum_byte(ex, i/2) >> (4*(i%2))) & 0xF];
len += sprintf(str+len, ",0x");
- nibbles = (3 + ssh1_bignum_bitcount(md))/4; if (nibbles<1) nibbles=1;
+ nibbles = (3 + bignum_bitcount(md))/4; if (nibbles<1) nibbles=1;
for (i=nibbles; i-- ;)
str[len++] = hex[(bignum_byte(md, i/2) >> (4*(i%2))) & 0xF];
}
MD5Final(digest, &md5c);
- sprintf(buffer, "%d ", ssh1_bignum_bitcount(key->modulus));
+ sprintf(buffer, "%d ", bignum_bitcount(key->modulus));
for (i = 0; i < 16; i++)
sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]);
strncpy(str, buffer, len); str[len-1] = '\0';
}
}
+/*
+ * Verify that the public data in an RSA key matches the private
+ * data. We also check the private data itself: we ensure that p >
+ * q and that iqmp really is the inverse of q mod p.
+ */
+int rsa_verify(struct RSAKey *key) {
+ Bignum n, ed, pm1, qm1;
+ int cmp;
+
+ /* n must equal pq. */
+ n = bigmul(key->p, key->q);
+ cmp = bignum_cmp(n, key->modulus);
+ freebn(n);
+ if (cmp != 0)
+ return 0;
+
+ /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
+ pm1 = copybn(key->p);
+ decbn(pm1);
+ ed = modmul(key->exponent, key->private_exponent, pm1);
+ cmp = bignum_cmp(ed, One);
+ sfree(ed);
+ if (cmp != 0)
+ return 0;
+
+ qm1 = copybn(key->q);
+ decbn(qm1);
+ ed = modmul(key->exponent, key->private_exponent, qm1);
+ cmp = bignum_cmp(ed, One);
+ sfree(ed);
+ if (cmp != 0)
+ return 0;
+
+ /*
+ * Ensure p > q.
+ */
+ if (bignum_cmp(key->p, key->q) <= 0)
+ return 0;
+
+ /*
+ * Ensure iqmp * q is congruent to 1, modulo p.
+ */
+ n = modmul(key->iqmp, key->q, key->p);
+ cmp = bignum_cmp(n, One);
+ sfree(n);
+ if (cmp != 0)
+ return 0;
+
+ return 1;
+}
+
void freersakey(struct RSAKey *key) {
if (key->modulus) freebn(key->modulus);
if (key->exponent) freebn(key->exponent);
len = rsastr_len(rsa);
p = smalloc(len);
- rsastr_fmt(p, rsa);
+ rsastr_fmt(p, rsa);
return p;
}
int i;
unsigned char *blob, *p;
- elen = (ssh1_bignum_bitcount(rsa->exponent)+8)/8;
- mlen = (ssh1_bignum_bitcount(rsa->modulus)+8)/8;
+ elen = (bignum_bitcount(rsa->exponent)+8)/8;
+ mlen = (bignum_bitcount(rsa->modulus)+8)/8;
/*
* string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
int i;
unsigned char *blob, *p;
- dlen = (ssh1_bignum_bitcount(rsa->private_exponent)+8)/8;
- plen = (ssh1_bignum_bitcount(rsa->p)+8)/8;
- qlen = (ssh1_bignum_bitcount(rsa->q)+8)/8;
- ulen = (ssh1_bignum_bitcount(rsa->iqmp)+8)/8;
+ dlen = (bignum_bitcount(rsa->private_exponent)+8)/8;
+ plen = (bignum_bitcount(rsa->p)+8)/8;
+ qlen = (bignum_bitcount(rsa->q)+8)/8;
+ ulen = (bignum_bitcount(rsa->iqmp)+8)/8;
/*
* mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
rsa->q = getmp(&pb, &priv_len);
rsa->iqmp = getmp(&pb, &priv_len);
+ if (!rsa_verify(rsa)) {
+ rsa2_freekey(rsa);
+ return NULL;
+ }
+
return rsa;
}
static void *rsa2_openssh_createkey(unsigned char **blob, int *len) {
char **b = (char **)blob;
struct RSAKey *rsa;
- char *p;
- int slen;
rsa = smalloc(sizeof(struct RSAKey));
if (!rsa) return NULL;
return rsa;
}
+static int *rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len) {
+ struct RSAKey *rsa = (struct RSAKey *)key;
+ int bloblen, i;
+
+ bloblen =
+ ssh2_bignum_length(rsa->modulus) +
+ ssh2_bignum_length(rsa->exponent) +
+ ssh2_bignum_length(rsa->private_exponent) +
+ ssh2_bignum_length(rsa->iqmp) +
+ ssh2_bignum_length(rsa->p) +
+ ssh2_bignum_length(rsa->q);
+
+ if (bloblen > len)
+ return bloblen;
+
+ bloblen = 0;
+#define ENC(x) \
+ PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
+ for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
+ ENC(rsa->modulus);
+ ENC(rsa->exponent);
+ ENC(rsa->private_exponent);
+ ENC(rsa->iqmp);
+ ENC(rsa->p);
+ ENC(rsa->q);
+
+ return bloblen;
+}
+
static char *rsa2_fingerprint(void *key) {
struct RSAKey *rsa = (struct RSAKey *)key;
struct MD5Context md5c;
MD5Update(&md5c, "\0\0\0\7ssh-rsa", 11);
#define ADD_BIGNUM(bignum) \
- numlen = (ssh1_bignum_bitcount(bignum)+8)/8; \
+ numlen = (bignum_bitcount(bignum)+8)/8; \
PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
for (i = numlen; i-- ;) { \
unsigned char c = bignum_byte(bignum, i); \
MD5Final(digest, &md5c);
- sprintf(buffer, "ssh-rsa %d ", ssh1_bignum_bitcount(rsa->modulus));
+ sprintf(buffer, "ssh-rsa %d ", bignum_bitcount(rsa->modulus));
for (i = 0; i < 16; i++)
sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]);
ret = smalloc(strlen(buffer)+1);
/*
* This is the magic ASN.1/DER prefix that goes in the decoded
* signature, between the string of FFs and the actual SHA hash
- * value. As closely as I can tell, the meaning of it is:
+ * value. The meaning of it is:
*
* 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
*
* 30 21 -- a constructed SEQUENCE of length 0x21
* 30 09 -- a constructed sub-SEQUENCE of length 9
* 06 05 -- an object identifier, length 5
- * 2B 0E 03 02 1A --
+ * 2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 }
+ * (the 1,3 comes from 0x2B = 43 = 40*1+3)
* 05 00 -- NULL
* 04 14 -- a primitive OCTET STRING of length 0x14
* [0x14 bytes of hash data follows]
+ *
+ * The object id in the middle there is listed as `id-sha1' in
+ * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn (the
+ * ASN module for PKCS #1) and its expanded form is as follows:
+ *
+ * id-sha1 OBJECT IDENTIFIER ::= {
+ * iso(1) identified-organization(3) oiw(14) secsig(3)
+ * algorithms(2) 26 }
*/
static unsigned char asn1_weird_stuff[] = {
0x00,0x30,0x21,0x30,0x09,0x06,0x05,0x2B,
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;
ret = 1;
- bytes = ssh1_bignum_bitcount(rsa->modulus) / 8;
+ bytes = bignum_bitcount(rsa->modulus) / 8;
/* Top (partial) byte should be zero. */
if (bignum_byte(out, bytes-1) != 0)
ret = 0;
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;
}
SHA_Simple(data, datalen, hash);
- nbytes = (ssh1_bignum_bitcount(rsa->modulus)-1) / 8;
+ nbytes = (bignum_bitcount(rsa->modulus)-1) / 8;
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];
out = modpow(in, rsa->private_exponent, rsa->modulus);
freebn(in);
- nbytes = (ssh1_bignum_bitcount(out)+7)/8;
+ nbytes = (bignum_bitcount(out)+7)/8;
bytes = smalloc(4+7+4+nbytes);
PUT_32BIT(bytes, 7);
memcpy(bytes+4, "ssh-rsa", 7);
rsa2_private_blob,
rsa2_createkey,
rsa2_openssh_createkey,
+ rsa2_openssh_fmtkey,
rsa2_fingerprint,
rsa2_verifysig,
rsa2_sign,