Complete analysis of the magic ASN.1 stuff in RSASSA signatures
[u/mdw/putty] / sshrsa.c
index 5300127..aeafec3 100644 (file)
--- a/sshrsa.c
+++ b/sshrsa.c
@@ -310,8 +310,6 @@ static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
 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;
@@ -375,23 +373,34 @@ static char *rsa2_fingerprint(void *key) {
 /*
  * 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;
@@ -419,12 +428,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;
     }
@@ -452,9 +461,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];