Home | History | Annotate | Download | only in testprof
      1 #include <tomcrypt_test.h>
      2 #if defined(GMP_DESC) || defined(USE_GMP)
      3 #include <gmp.h>
      4 #endif
      5 
      6 #ifndef LTC_DER
      7 
      8 int der_tests(void)
      9 {
     10    fprintf(stderr, "NOP");
     11    return 0;
     12 }
     13 
     14 #else
     15 
     16 static void der_set_test(void)
     17 {
     18    ltc_asn1_list list[10];
     19    static const unsigned char oct_str[] = { 1, 2, 3, 4 };
     20    static const unsigned char bin_str[] = { 1, 0, 0, 1 };
     21    static const unsigned long int_val   = 12345678UL;
     22 
     23    unsigned char strs[10][10], outbuf[128];
     24    unsigned long x, val, outlen;
     25    int           err;
     26 
     27    /* make structure and encode it */
     28    LTC_SET_ASN1(list, 0, LTC_ASN1_OCTET_STRING,  oct_str, sizeof(oct_str));
     29    LTC_SET_ASN1(list, 1, LTC_ASN1_BIT_STRING,    bin_str, sizeof(bin_str));
     30    LTC_SET_ASN1(list, 2, LTC_ASN1_SHORT_INTEGER, &int_val, 1);
     31 
     32    /* encode it */
     33    outlen = sizeof(outbuf);
     34    if ((err = der_encode_set(list, 3, outbuf, &outlen)) != CRYPT_OK) {
     35       fprintf(stderr, "error encoding set: %s\n", error_to_string(err));
     36       exit(EXIT_FAILURE);
     37    }
     38 
     39 
     40    /* first let's test the set_decoder out of order to see what happens, we should get all the fields we expect even though they're in a diff order */
     41    LTC_SET_ASN1(list, 0, LTC_ASN1_BIT_STRING,    strs[1], sizeof(strs[1]));
     42    LTC_SET_ASN1(list, 1, LTC_ASN1_SHORT_INTEGER, &val, 1);
     43    LTC_SET_ASN1(list, 2, LTC_ASN1_OCTET_STRING,  strs[0], sizeof(strs[0]));
     44 
     45    if ((err = der_decode_set(outbuf, outlen, list, 3)) != CRYPT_OK) {
     46       fprintf(stderr, "error decoding set using der_decode_set: %s\n", error_to_string(err));
     47       exit(EXIT_FAILURE);
     48    }
     49 
     50    /* now compare the items */
     51    if (memcmp(strs[0], oct_str, sizeof(oct_str))) {
     52       fprintf(stderr, "error decoding set using der_decode_set (oct_str is wrong):\n");
     53       exit(EXIT_FAILURE);
     54    }
     55 
     56    if (memcmp(strs[1], bin_str, sizeof(bin_str))) {
     57       fprintf(stderr, "error decoding set using der_decode_set (bin_str is wrong):\n");
     58       exit(EXIT_FAILURE);
     59    }
     60 
     61    if (val != int_val) {
     62       fprintf(stderr, "error decoding set using der_decode_set (int_val is wrong):\n");
     63       exit(EXIT_FAILURE);
     64    }
     65 
     66    strcpy((char*)strs[0], "one");
     67    strcpy((char*)strs[1], "one2");
     68    strcpy((char*)strs[2], "two");
     69    strcpy((char*)strs[3], "aaa");
     70    strcpy((char*)strs[4], "aaaa");
     71    strcpy((char*)strs[5], "aab");
     72    strcpy((char*)strs[6], "aaab");
     73    strcpy((char*)strs[7], "bbb");
     74    strcpy((char*)strs[8], "bbba");
     75    strcpy((char*)strs[9], "bbbb");
     76 
     77    for (x = 0; x < 10; x++) {
     78        LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], strlen((char*)strs[x]));
     79    }
     80 
     81    outlen = sizeof(outbuf);
     82    if ((err = der_encode_setof(list, 10, outbuf, &outlen)) != CRYPT_OK) {
     83       fprintf(stderr, "error encoding SET OF: %s\n", error_to_string(err));
     84       exit(EXIT_FAILURE);
     85    }
     86 
     87    for (x = 0; x < 10; x++) {
     88        LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], sizeof(strs[x]) - 1);
     89    }
     90    XMEMSET(strs, 0, sizeof(strs));
     91 
     92    if ((err = der_decode_set(outbuf, outlen, list, 10)) != CRYPT_OK) {
     93       fprintf(stderr, "error decoding SET OF: %s\n", error_to_string(err));
     94       exit(EXIT_FAILURE);
     95    }
     96 
     97    /* now compare */
     98    for (x = 1; x < 10; x++) {
     99       if (!(strlen((char*)strs[x-1]) <= strlen((char*)strs[x])) && strcmp((char*)strs[x-1], (char*)strs[x]) >= 0) {
    100          fprintf(stderr, "error SET OF order at %lu is wrong\n", x);
    101          exit(EXIT_FAILURE);
    102       }
    103    }
    104 
    105 }
    106 
    107 
    108 /* we are encoding
    109 
    110   SEQUENCE {
    111      PRINTABLE "printable"
    112      IA5       "ia5"
    113      SEQUENCE {
    114         INTEGER 12345678
    115         UTCTIME { 91, 5, 6, 16, 45, 40, 1, 7, 0 }
    116         SEQUENCE {
    117            OCTET STRING { 1, 2, 3, 4 }
    118            BIT STRING   { 1, 0, 0, 1 }
    119            SEQUENCE {
    120               OID       { 1, 2, 840, 113549 }
    121               NULL
    122               SET OF {
    123                  PRINTABLE "333"  // WILL GET SORTED
    124                  PRINTABLE "222"
    125            }
    126         }
    127      }
    128   }
    129 
    130 */
    131 
    132 static void der_flexi_test(void)
    133 {
    134    static const char printable_str[]    = "printable";
    135    static const char set1_str[]         = "333";
    136    static const char set2_str[]         = "222";
    137    static const char ia5_str[]          = "ia5";
    138    static const unsigned long int_val   = 12345678UL;
    139    static const ltc_utctime   utctime   = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
    140    static const unsigned char oct_str[] = { 1, 2, 3, 4 };
    141    static const unsigned char bit_str[] = { 1, 0, 0, 1 };
    142    static const unsigned long oid_str[] = { 1, 2, 840, 113549 };
    143 
    144    unsigned char encode_buf[192];
    145    unsigned long encode_buf_len, decode_len;
    146    int           err;
    147 
    148    ltc_asn1_list static_list[5][3], *decoded_list, *l;
    149 
    150    /* build list */
    151    LTC_SET_ASN1(static_list[0], 0, LTC_ASN1_PRINTABLE_STRING, (void *)printable_str, strlen(printable_str));
    152    LTC_SET_ASN1(static_list[0], 1, LTC_ASN1_IA5_STRING,       (void *)ia5_str,       strlen(ia5_str));
    153    LTC_SET_ASN1(static_list[0], 2, LTC_ASN1_SEQUENCE,         static_list[1],   3);
    154 
    155    LTC_SET_ASN1(static_list[1], 0, LTC_ASN1_SHORT_INTEGER,    (void *)&int_val,         1);
    156    LTC_SET_ASN1(static_list[1], 1, LTC_ASN1_UTCTIME,          (void *)&utctime,         1);
    157    LTC_SET_ASN1(static_list[1], 2, LTC_ASN1_SEQUENCE,         static_list[2],   3);
    158 
    159    LTC_SET_ASN1(static_list[2], 0, LTC_ASN1_OCTET_STRING,     (void *)oct_str,          4);
    160    LTC_SET_ASN1(static_list[2], 1, LTC_ASN1_BIT_STRING,       (void *)bit_str,          4);
    161    LTC_SET_ASN1(static_list[2], 2, LTC_ASN1_SEQUENCE,         static_list[3],   3);
    162 
    163    LTC_SET_ASN1(static_list[3], 0, LTC_ASN1_OBJECT_IDENTIFIER,(void *)oid_str,          4);
    164    LTC_SET_ASN1(static_list[3], 1, LTC_ASN1_NULL,             NULL,             0);
    165    LTC_SET_ASN1(static_list[3], 2, LTC_ASN1_SETOF,            static_list[4],   2);
    166 
    167    LTC_SET_ASN1(static_list[4], 0, LTC_ASN1_PRINTABLE_STRING, set1_str, strlen(set1_str));
    168    LTC_SET_ASN1(static_list[4], 1, LTC_ASN1_PRINTABLE_STRING, set2_str, strlen(set2_str));
    169 
    170    /* encode it */
    171    encode_buf_len = sizeof(encode_buf);
    172    if ((err = der_encode_sequence(&static_list[0][0], 3, encode_buf, &encode_buf_len)) != CRYPT_OK) {
    173       fprintf(stderr, "Encoding static_list: %s\n", error_to_string(err));
    174       exit(EXIT_FAILURE);
    175    }
    176 
    177 #if 0
    178    {
    179      FILE *f;
    180      f = fopen("t.bin", "wb");
    181      fwrite(encode_buf, 1, encode_buf_len, f);
    182      fclose(f);
    183    }
    184 #endif
    185 
    186    /* decode with flexi */
    187    decode_len = encode_buf_len;
    188    if ((err = der_decode_sequence_flexi(encode_buf, &decode_len, &decoded_list)) != CRYPT_OK) {
    189       fprintf(stderr, "decoding static_list: %s\n", error_to_string(err));
    190       exit(EXIT_FAILURE);
    191    }
    192 
    193    if (decode_len != encode_buf_len) {
    194       fprintf(stderr, "Decode len of %lu does not match encode len of %lu \n", decode_len, encode_buf_len);
    195       exit(EXIT_FAILURE);
    196    }
    197 
    198    /* we expect l->next to be NULL and l->child to not be */
    199    l = decoded_list;
    200    if (l->next != NULL || l->child == NULL) {
    201       fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    202       exit(EXIT_FAILURE);
    203    }
    204 
    205    /* we expect a SEQUENCE */
    206       if (l->type != LTC_ASN1_SEQUENCE) {
    207          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    208          exit(EXIT_FAILURE);
    209       }
    210       l = l->child;
    211 
    212    /* PRINTABLE STRING */
    213       /* we expect printable_str */
    214       if (l->next == NULL || l->child != NULL) {
    215          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    216          exit(EXIT_FAILURE);
    217       }
    218 
    219       if (l->type != LTC_ASN1_PRINTABLE_STRING) {
    220          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    221          exit(EXIT_FAILURE);
    222       }
    223 
    224       if (l->size != strlen(printable_str) || memcmp(printable_str, l->data, l->size)) {
    225          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    226          exit(EXIT_FAILURE);
    227       }
    228 
    229       /* move to next */
    230       l = l->next;
    231 
    232    /* IA5 STRING */
    233       /* we expect ia5_str */
    234       if (l->next == NULL || l->child != NULL) {
    235          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    236          exit(EXIT_FAILURE);
    237       }
    238 
    239       if (l->type != LTC_ASN1_IA5_STRING) {
    240          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    241          exit(EXIT_FAILURE);
    242       }
    243 
    244       if (l->size != strlen(ia5_str) || memcmp(ia5_str, l->data, l->size)) {
    245          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    246          exit(EXIT_FAILURE);
    247       }
    248 
    249       /* move to next */
    250       l = l->next;
    251 
    252    /* expect child anve move down */
    253 
    254       if (l->next != NULL || l->child == NULL) {
    255          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    256          exit(EXIT_FAILURE);
    257       }
    258 
    259       if (l->type != LTC_ASN1_SEQUENCE) {
    260          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    261          exit(EXIT_FAILURE);
    262       }
    263       l = l->child;
    264 
    265 
    266    /* INTEGER */
    267 
    268       if (l->next == NULL || l->child != NULL) {
    269          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    270          exit(EXIT_FAILURE);
    271       }
    272 
    273       if (l->type != LTC_ASN1_INTEGER) {
    274          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    275          exit(EXIT_FAILURE);
    276       }
    277 
    278       if (mp_cmp_d(l->data, 12345678UL) != LTC_MP_EQ) {
    279          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    280          exit(EXIT_FAILURE);
    281       }
    282 
    283       /* move to next */
    284       l = l->next;
    285 
    286    /* UTCTIME */
    287 
    288       if (l->next == NULL || l->child != NULL) {
    289          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    290          exit(EXIT_FAILURE);
    291       }
    292 
    293       if (l->type != LTC_ASN1_UTCTIME) {
    294          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    295          exit(EXIT_FAILURE);
    296       }
    297 
    298       if (memcmp(l->data, &utctime, sizeof(utctime))) {
    299          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    300          exit(EXIT_FAILURE);
    301       }
    302 
    303       /* move to next */
    304       l = l->next;
    305 
    306    /* expect child anve move down */
    307 
    308       if (l->next != NULL || l->child == NULL) {
    309          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    310          exit(EXIT_FAILURE);
    311       }
    312 
    313       if (l->type != LTC_ASN1_SEQUENCE) {
    314          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    315          exit(EXIT_FAILURE);
    316       }
    317       l = l->child;
    318 
    319 
    320    /* OCTET STRING */
    321       /* we expect oct_str */
    322       if (l->next == NULL || l->child != NULL) {
    323          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    324          exit(EXIT_FAILURE);
    325       }
    326 
    327       if (l->type != LTC_ASN1_OCTET_STRING) {
    328          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    329          exit(EXIT_FAILURE);
    330       }
    331 
    332       if (l->size != sizeof(oct_str) || memcmp(oct_str, l->data, l->size)) {
    333          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    334          exit(EXIT_FAILURE);
    335       }
    336 
    337       /* move to next */
    338       l = l->next;
    339 
    340    /* BIT STRING */
    341       /* we expect oct_str */
    342       if (l->next == NULL || l->child != NULL) {
    343          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    344          exit(EXIT_FAILURE);
    345       }
    346 
    347       if (l->type != LTC_ASN1_BIT_STRING) {
    348          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    349          exit(EXIT_FAILURE);
    350       }
    351 
    352       if (l->size != sizeof(bit_str) || memcmp(bit_str, l->data, l->size)) {
    353          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    354          exit(EXIT_FAILURE);
    355       }
    356 
    357       /* move to next */
    358       l = l->next;
    359 
    360    /* expect child anve move down */
    361 
    362       if (l->next != NULL || l->child == NULL) {
    363          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    364          exit(EXIT_FAILURE);
    365       }
    366 
    367       if (l->type != LTC_ASN1_SEQUENCE) {
    368          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    369          exit(EXIT_FAILURE);
    370       }
    371       l = l->child;
    372 
    373 
    374    /* OID STRING */
    375       /* we expect oid_str */
    376       if (l->next == NULL || l->child != NULL) {
    377          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    378          exit(EXIT_FAILURE);
    379       }
    380 
    381       if (l->type != LTC_ASN1_OBJECT_IDENTIFIER) {
    382          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    383          exit(EXIT_FAILURE);
    384       }
    385 
    386       if (l->size != sizeof(oid_str)/sizeof(oid_str[0]) || memcmp(oid_str, l->data, l->size*sizeof(oid_str[0]))) {
    387          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    388          exit(EXIT_FAILURE);
    389       }
    390 
    391       /* move to next */
    392       l = l->next;
    393 
    394    /* NULL */
    395       if (l->type != LTC_ASN1_NULL) {
    396          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    397          exit(EXIT_FAILURE);
    398       }
    399 
    400       /* move to next */
    401       l = l->next;
    402 
    403    /* expect child anve move down */
    404       if (l->next != NULL || l->child == NULL) {
    405          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    406          exit(EXIT_FAILURE);
    407       }
    408 
    409       if (l->type != LTC_ASN1_SET) {
    410          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    411          exit(EXIT_FAILURE);
    412       }
    413       l = l->child;
    414 
    415    /* PRINTABLE STRING */
    416       /* we expect printable_str */
    417       if (l->next == NULL || l->child != NULL) {
    418          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    419          exit(EXIT_FAILURE);
    420       }
    421 
    422       if (l->type != LTC_ASN1_PRINTABLE_STRING) {
    423          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    424          exit(EXIT_FAILURE);
    425       }
    426 
    427 /* note we compare set2_str FIRST because the SET OF is sorted and "222" comes before "333" */
    428       if (l->size != strlen(set2_str) || memcmp(set2_str, l->data, l->size)) {
    429          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    430          exit(EXIT_FAILURE);
    431       }
    432 
    433       /* move to next */
    434       l = l->next;
    435 
    436    /* PRINTABLE STRING */
    437       /* we expect printable_str */
    438       if (l->type != LTC_ASN1_PRINTABLE_STRING) {
    439          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    440          exit(EXIT_FAILURE);
    441       }
    442 
    443       if (l->size != strlen(set1_str) || memcmp(set1_str, l->data, l->size)) {
    444          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
    445          exit(EXIT_FAILURE);
    446       }
    447 
    448 
    449    der_sequence_free(l);
    450 
    451 }
    452 
    453 static int der_choice_test(void)
    454 {
    455    ltc_asn1_list types[7], host[1];
    456    unsigned char bitbuf[10], octetbuf[10], ia5buf[10], printbuf[10], outbuf[256];
    457    unsigned long integer, oidbuf[10], outlen, inlen, x, y;
    458    void          *mpinteger;
    459    ltc_utctime   utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
    460 
    461    /* setup variables */
    462    for (x = 0; x < sizeof(bitbuf); x++)   { bitbuf[x]   = x & 1; }
    463    for (x = 0; x < sizeof(octetbuf); x++) { octetbuf[x] = x;     }
    464    for (x = 0; x < sizeof(ia5buf); x++)   { ia5buf[x]   = 'a';   }
    465    for (x = 0; x < sizeof(printbuf); x++) { printbuf[x] = 'a';   }
    466    integer = 1;
    467    for (x = 0; x < sizeof(oidbuf)/sizeof(oidbuf[0]); x++)   { oidbuf[x] = x + 1;   }
    468    DO(mp_init(&mpinteger));
    469 
    470    for (x = 0; x < 14; x++) {
    471        /* setup list */
    472        LTC_SET_ASN1(types, 0, LTC_ASN1_PRINTABLE_STRING, printbuf, sizeof(printbuf));
    473        LTC_SET_ASN1(types, 1, LTC_ASN1_BIT_STRING, bitbuf, sizeof(bitbuf));
    474        LTC_SET_ASN1(types, 2, LTC_ASN1_OCTET_STRING, octetbuf, sizeof(octetbuf));
    475        LTC_SET_ASN1(types, 3, LTC_ASN1_IA5_STRING, ia5buf, sizeof(ia5buf));
    476        if (x > 7) {
    477           LTC_SET_ASN1(types, 4, LTC_ASN1_SHORT_INTEGER, &integer, 1);
    478        } else {
    479           LTC_SET_ASN1(types, 4, LTC_ASN1_INTEGER, mpinteger, 1);
    480        }
    481        LTC_SET_ASN1(types, 5, LTC_ASN1_OBJECT_IDENTIFIER, oidbuf, sizeof(oidbuf)/sizeof(oidbuf[0]));
    482        LTC_SET_ASN1(types, 6, LTC_ASN1_UTCTIME, &utctime, 1);
    483 
    484        LTC_SET_ASN1(host, 0, LTC_ASN1_CHOICE, types, 7);
    485 
    486 
    487        /* encode */
    488        outlen = sizeof(outbuf);
    489        DO(der_encode_sequence(&types[x>6?x-7:x], 1, outbuf, &outlen));
    490 
    491        /* decode it */
    492        inlen = outlen;
    493        DO(der_decode_sequence(outbuf, inlen, &host[0], 1));
    494 
    495        for (y = 0; y < 7; y++) {
    496            if (types[y].used && y != (x>6?x-7:x)) {
    497                fprintf(stderr, "CHOICE, flag %lu in trial %lu was incorrectly set to one\n", y, x);
    498                return 1;
    499            }
    500            if (!types[y].used && y == (x>6?x-7:x)) {
    501                fprintf(stderr, "CHOICE, flag %lu in trial %lu was incorrectly set to zero\n", y, x);
    502                return 1;
    503            }
    504       }
    505   }
    506   mp_clear(mpinteger);
    507   return 0;
    508 }
    509 
    510 
    511 int der_tests(void)
    512 {
    513    unsigned long x, y, z, zz, oid[2][32];
    514    unsigned char buf[3][2048];
    515    void *a, *b, *c, *d, *e, *f, *g;
    516 
    517    static const unsigned char rsa_oid_der[] = { 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d };
    518    static const unsigned long rsa_oid[]     = { 1, 2, 840, 113549 };
    519 
    520    static const unsigned char rsa_ia5[]     = "test1 (at) rsa.com";
    521    static const unsigned char rsa_ia5_der[] = { 0x16, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x31,
    522                                                 0x40, 0x72, 0x73, 0x61, 0x2e, 0x63, 0x6f, 0x6d };
    523 
    524    static const unsigned char rsa_printable[] = "Test User 1";
    525    static const unsigned char rsa_printable_der[] = { 0x13, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x20, 0x55,
    526                                                       0x73, 0x65, 0x72, 0x20, 0x31 };
    527 
    528    static const ltc_utctime   rsa_time1 = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
    529    static const ltc_utctime   rsa_time2 = { 91, 5, 6, 23, 45, 40, 0, 0, 0 };
    530    ltc_utctime                tmp_time;
    531 
    532    static const unsigned char rsa_time1_der[] = { 0x17, 0x11, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x31, 0x36, 0x34, 0x35, 0x34, 0x30, 0x2D, 0x30, 0x37, 0x30, 0x30 };
    533    static const unsigned char rsa_time2_der[] = { 0x17, 0x0d, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x32, 0x33, 0x34, 0x35, 0x34, 0x30, 0x5a };
    534 
    535    static const wchar_t utf8_1[]           = { 0x0041, 0x2262, 0x0391, 0x002E };
    536    static const unsigned char utf8_1_der[] = { 0x0C, 0x07, 0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E };
    537    static const wchar_t utf8_2[]           = { 0xD55C, 0xAD6D, 0xC5B4 };
    538    static const unsigned char utf8_2_der[] = { 0x0C, 0x09, 0xED, 0x95, 0x9C, 0xEA, 0xB5, 0xAD, 0xEC, 0x96, 0xB4 };
    539 
    540    unsigned char utf8_buf[32];
    541    wchar_t utf8_out[32];
    542 
    543    DO(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL));
    544    for (zz = 0; zz < 16; zz++) {
    545 #ifdef USE_TFM
    546       for (z = 0; z < 256; z++) {
    547 #else
    548       for (z = 0; z < 1024; z++) {
    549 #endif
    550          if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
    551             fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
    552             return 1;
    553          }
    554          DO(mp_read_unsigned_bin(a, buf[0], z));
    555 /*          if (mp_iszero(a) == LTC_MP_NO) { a.sign = buf[0][0] & 1 ? LTC_MP_ZPOS : LTC_MP_NEG; } */
    556          x = sizeof(buf[0]);
    557          DO(der_encode_integer(a, buf[0], &x));
    558          DO(der_length_integer(a, &y));
    559          if (y != x) { fprintf(stderr, "DER INTEGER size mismatch\n"); return 1; }
    560          mp_set_int(b, 0);
    561          DO(der_decode_integer(buf[0], y, b));
    562          if (y != x || mp_cmp(a, b) != LTC_MP_EQ) {
    563             fprintf(stderr, "%lu: %lu vs %lu\n", z, x, y);
    564             mp_clear_multi(a, b, c, d, e, f, g, NULL);
    565             return 1;
    566          }
    567       }
    568    }
    569 
    570 /* test short integer */
    571    for (zz = 0; zz < 256; zz++) {
    572       for (z = 1; z < 4; z++) {
    573          if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
    574             fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
    575             return 1;
    576          }
    577          /* encode with normal */
    578          DO(mp_read_unsigned_bin(a, buf[0], z));
    579 
    580          x = sizeof(buf[0]);
    581          DO(der_encode_integer(a, buf[0], &x));
    582 
    583          /* encode with short */
    584          y = sizeof(buf[1]);
    585          DO(der_encode_short_integer(mp_get_int(a), buf[1], &y));
    586          if (x != y || memcmp(buf[0], buf[1], x)) {
    587             fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu\n", x, y);
    588             for (z = 0; z < x; z++) fprintf(stderr, "%02x ", buf[0][z]); fprintf(stderr, "\n");
    589             for (z = 0; z < y; z++) fprintf(stderr, "%02x ", buf[1][z]); fprintf(stderr, "\n");
    590             mp_clear_multi(a, b, c, d, e, f, g, NULL);
    591             return 1;
    592          }
    593 
    594          /* decode it */
    595          x = 0;
    596          DO(der_decode_short_integer(buf[1], y, &x));
    597          if (x != mp_get_int(a)) {
    598             fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, mp_get_int(a));
    599             mp_clear_multi(a, b, c, d, e, f, g, NULL);
    600             return 1;
    601          }
    602       }
    603    }
    604    mp_clear_multi(a, b, c, d, e, f, g, NULL);
    605 
    606 
    607 /* Test bit string */
    608    for (zz = 1; zz < 1536; zz++) {
    609        yarrow_read(buf[0], zz, &yarrow_prng);
    610        for (z = 0; z < zz; z++) {
    611            buf[0][z] &= 0x01;
    612        }
    613        x = sizeof(buf[1]);
    614        DO(der_encode_bit_string(buf[0], zz, buf[1], &x));
    615        DO(der_length_bit_string(zz, &y));
    616        if (y != x) {
    617           fprintf(stderr, "\nDER BIT STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
    618           return 1;
    619        }
    620 
    621        y = sizeof(buf[2]);
    622        DO(der_decode_bit_string(buf[1], x, buf[2], &y));
    623        if (y != zz || memcmp(buf[0], buf[2], zz)) {
    624           fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
    625           return 1;
    626        }
    627    }
    628 
    629 /* Test octet string */
    630    for (zz = 1; zz < 1536; zz++) {
    631        yarrow_read(buf[0], zz, &yarrow_prng);
    632        x = sizeof(buf[1]);
    633        DO(der_encode_octet_string(buf[0], zz, buf[1], &x));
    634        DO(der_length_octet_string(zz, &y));
    635        if (y != x) {
    636           fprintf(stderr, "\nDER OCTET STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
    637           return 1;
    638        }
    639        y = sizeof(buf[2]);
    640        DO(der_decode_octet_string(buf[1], x, buf[2], &y));
    641        if (y != zz || memcmp(buf[0], buf[2], zz)) {
    642           fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
    643           return 1;
    644        }
    645    }
    646 
    647 /* test OID */
    648    x = sizeof(buf[0]);
    649    DO(der_encode_object_identifier((unsigned long*)rsa_oid, sizeof(rsa_oid)/sizeof(rsa_oid[0]), buf[0], &x));
    650    if (x != sizeof(rsa_oid_der) || memcmp(rsa_oid_der, buf[0], x)) {
    651       fprintf(stderr, "rsa_oid_der encode failed to match, %lu, ", x);
    652       for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
    653       fprintf(stderr, "\n");
    654       return 1;
    655    }
    656 
    657    y = sizeof(oid[0])/sizeof(oid[0][0]);
    658    DO(der_decode_object_identifier(buf[0], x, oid[0], &y));
    659    if (y != sizeof(rsa_oid)/sizeof(rsa_oid[0]) || memcmp(rsa_oid, oid[0], sizeof(rsa_oid))) {
    660       fprintf(stderr, "rsa_oid_der decode failed to match, %lu, ", y);
    661       for (z = 0; z < y; z++) fprintf(stderr, "%lu ", oid[0][z]);
    662       fprintf(stderr, "\n");
    663       return 1;
    664    }
    665 
    666    /* do random strings */
    667    for (zz = 0; zz < 5000; zz++) {
    668        /* pick a random number of words */
    669        yarrow_read(buf[0], 4, &yarrow_prng);
    670        LOAD32L(z, buf[0]);
    671        z = 2 + (z % ((sizeof(oid[0])/sizeof(oid[0][0])) - 2));
    672 
    673        /* fill them in */
    674        oid[0][0] = buf[0][0] % 3;
    675        oid[0][1] = buf[0][1] % 40;
    676 
    677        for (y = 2; y < z; y++) {
    678           yarrow_read(buf[0], 4, &yarrow_prng);
    679           LOAD32L(oid[0][y], buf[0]);
    680        }
    681 
    682        /* encode it */
    683        x = sizeof(buf[0]);
    684        DO(der_encode_object_identifier(oid[0], z, buf[0], &x));
    685        DO(der_length_object_identifier(oid[0], z, &y));
    686        if (x != y) {
    687           fprintf(stderr, "Random OID %lu test failed, length mismatch: %lu, %lu\n", z, x, y);
    688           for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]);
    689           return 1;
    690        }
    691 
    692        /* decode it */
    693        y = sizeof(oid[0])/sizeof(oid[0][0]);
    694        DO(der_decode_object_identifier(buf[0], x, oid[1], &y));
    695        if (y != z) {
    696           fprintf(stderr, "Random OID %lu test failed, decode length mismatch: %lu, %lu\n", z, x, y);
    697           return 1;
    698        }
    699        if (memcmp(oid[0], oid[1], sizeof(oid[0][0]) * z)) {
    700           fprintf(stderr, "Random OID %lu test failed, decoded values wrong\n", z);
    701           for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]); fprintf(stderr, "\n\n Got \n\n");
    702           for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[1][x]);
    703           return 1;
    704        }
    705    }
    706 
    707 /* IA5 string */
    708    x = sizeof(buf[0]);
    709    DO(der_encode_ia5_string(rsa_ia5, strlen((char*)rsa_ia5), buf[0], &x));
    710    if (x != sizeof(rsa_ia5_der) || memcmp(buf[0], rsa_ia5_der, x)) {
    711       fprintf(stderr, "IA5 encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_ia5_der));
    712       return 1;
    713    }
    714    DO(der_length_ia5_string(rsa_ia5, strlen((char*)rsa_ia5), &y));
    715    if (y != x) {
    716       fprintf(stderr, "IA5 length failed to match: %lu, %lu\n", x, y);
    717       return 1;
    718    }
    719    y = sizeof(buf[1]);
    720    DO(der_decode_ia5_string(buf[0], x, buf[1], &y));
    721    if (y != strlen((char*)rsa_ia5) || memcmp(buf[1], rsa_ia5, strlen((char*)rsa_ia5))) {
    722        fprintf(stderr, "DER IA5 failed test vector\n");
    723        return 1;
    724    }
    725 
    726 /* Printable string */
    727    x = sizeof(buf[0]);
    728    DO(der_encode_printable_string(rsa_printable, strlen((char*)rsa_printable), buf[0], &x));
    729    if (x != sizeof(rsa_printable_der) || memcmp(buf[0], rsa_printable_der, x)) {
    730       fprintf(stderr, "PRINTABLE encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_printable_der));
    731       return 1;
    732    }
    733    DO(der_length_printable_string(rsa_printable, strlen((char*)rsa_printable), &y));
    734    if (y != x) {
    735       fprintf(stderr, "printable length failed to match: %lu, %lu\n", x, y);
    736       return 1;
    737    }
    738    y = sizeof(buf[1]);
    739    DO(der_decode_printable_string(buf[0], x, buf[1], &y));
    740    if (y != strlen((char*)rsa_printable) || memcmp(buf[1], rsa_printable, strlen((char*)rsa_printable))) {
    741        fprintf(stderr, "DER printable failed test vector\n");
    742        return 1;
    743    }
    744 
    745 /* Test UTC time */
    746    x = sizeof(buf[0]);
    747    DO(der_encode_utctime((ltc_utctime*)&rsa_time1, buf[0], &x));
    748    if (x != sizeof(rsa_time1_der) || memcmp(buf[0], rsa_time1_der, x)) {
    749       fprintf(stderr, "UTCTIME encode of rsa_time1 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
    750 fprintf(stderr, "\n\n");
    751 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n");
    752 
    753       return 1;
    754    }
    755    DO(der_length_utctime((ltc_utctime*)&rsa_time1, &y));
    756    if (y != x) {
    757       fprintf(stderr, "UTCTIME length failed to match for rsa_time1: %lu, %lu\n", x, y);
    758       return 1;
    759    }
    760    DO(der_decode_utctime(buf[0], &y, &tmp_time));
    761    if (y != x || memcmp(&rsa_time1, &tmp_time, sizeof(ltc_utctime))) {
    762       fprintf(stderr, "UTCTIME decode failed for rsa_time1: %lu %lu\n", x, y);
    763 fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
    764 tmp_time.YY,
    765 tmp_time.MM,
    766 tmp_time.DD,
    767 tmp_time.hh,
    768 tmp_time.mm,
    769 tmp_time.ss,
    770 tmp_time.off_dir,
    771 tmp_time.off_mm,
    772 tmp_time.off_hh);
    773       return 1;
    774    }
    775 
    776    x = sizeof(buf[0]);
    777    DO(der_encode_utctime((ltc_utctime*)&rsa_time2, buf[0], &x));
    778    if (x != sizeof(rsa_time2_der) || memcmp(buf[0], rsa_time2_der, x)) {
    779       fprintf(stderr, "UTCTIME encode of rsa_time2 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
    780 fprintf(stderr, "\n\n");
    781 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n");
    782 
    783       return 1;
    784    }
    785    DO(der_length_utctime((ltc_utctime*)&rsa_time2, &y));
    786    if (y != x) {
    787       fprintf(stderr, "UTCTIME length failed to match for rsa_time2: %lu, %lu\n", x, y);
    788       return 1;
    789    }
    790    DO(der_decode_utctime(buf[0], &y, &tmp_time));
    791    if (y != x || memcmp(&rsa_time2, &tmp_time, sizeof(ltc_utctime))) {
    792       fprintf(stderr, "UTCTIME decode failed for rsa_time2: %lu %lu\n", x, y);
    793 fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
    794 tmp_time.YY,
    795 tmp_time.MM,
    796 tmp_time.DD,
    797 tmp_time.hh,
    798 tmp_time.mm,
    799 tmp_time.ss,
    800 tmp_time.off_dir,
    801 tmp_time.off_mm,
    802 tmp_time.off_hh);
    803 
    804 
    805       return 1;
    806    }
    807 
    808    /* UTF 8 */
    809      /* encode it */
    810      x = sizeof(utf8_buf);
    811      DO(der_encode_utf8_string(utf8_1, sizeof(utf8_1) / sizeof(utf8_1[0]), utf8_buf, &x));
    812      if (x != sizeof(utf8_1_der) || memcmp(utf8_buf, utf8_1_der, x)) {
    813         fprintf(stderr, "DER UTF8_1 encoded to %lu bytes\n", x);
    814         for (y = 0; y < x; y++) fprintf(stderr, "%02x ", (unsigned)utf8_buf[y]); fprintf(stderr, "\n");
    815         return 1;
    816      }
    817      /* decode it */
    818      y = sizeof(utf8_out) / sizeof(utf8_out[0]);
    819      DO(der_decode_utf8_string(utf8_buf, x, utf8_out, &y));
    820      if (y != (sizeof(utf8_1) / sizeof(utf8_1[0])) || memcmp(utf8_1, utf8_out, y * sizeof(wchar_t))) {
    821         fprintf(stderr, "DER UTF8_1 decoded to %lu wchar_t\n", y);
    822         for (x = 0; x < y; x++) fprintf(stderr, "%04lx ", (unsigned long)utf8_out[x]); fprintf(stderr, "\n");
    823         return 1;
    824      }
    825 
    826      /* encode it */
    827      x = sizeof(utf8_buf);
    828      DO(der_encode_utf8_string(utf8_2, sizeof(utf8_2) / sizeof(utf8_2[0]), utf8_buf, &x));
    829      if (x != sizeof(utf8_2_der) || memcmp(utf8_buf, utf8_2_der, x)) {
    830         fprintf(stderr, "DER UTF8_2 encoded to %lu bytes\n", x);
    831         for (y = 0; y < x; y++) fprintf(stderr, "%02x ", (unsigned)utf8_buf[y]); fprintf(stderr, "\n");
    832         return 1;
    833      }
    834      /* decode it */
    835      y = sizeof(utf8_out) / sizeof(utf8_out[0]);
    836      DO(der_decode_utf8_string(utf8_buf, x, utf8_out, &y));
    837      if (y != (sizeof(utf8_2) / sizeof(utf8_2[0])) || memcmp(utf8_2, utf8_out, y * sizeof(wchar_t))) {
    838         fprintf(stderr, "DER UTF8_2 decoded to %lu wchar_t\n", y);
    839         for (x = 0; x < y; x++) fprintf(stderr, "%04lx ", (unsigned long)utf8_out[x]); fprintf(stderr, "\n");
    840         return 1;
    841      }
    842 
    843 
    844    der_set_test();
    845    der_flexi_test();
    846    return der_choice_test();
    847 }
    848 
    849 #endif
    850 
    851 /* $Source: /cvs/libtom/libtomcrypt/testprof/der_tests.c,v $ */
    852 /* $Revision: 1.49 $ */
    853 /* $Date: 2006/11/26 02:10:21 $ */
    854