~ubuntu-branches/ubuntu/precise/dropbear/precise

« back to all changes in this revision

Viewing changes to libtomcrypt/testprof/der_tests.c

  • Committer: Bazaar Package Importer
  • Author(s): Gerrit Pape
  • Date: 2007-03-02 20:48:18 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070302204818-ozmbou2sbyj7dus5
Tags: 0.49-1
* new upstream release, fixes
  * CVE-2007-1099: dropbear dbclient insufficient warning on hostkey
    mismatch (closes: #412899).
  * dbclient uses static "Password:" prompt instead of using the server's
    prompt (closes: #394996).
* debian/control: Suggests: openssh-client, not ssh (closes: #405686);
  Standards-Version: 3.7.2.2.
* debian/README.Debian: ssh -> openssh-server, openssh-client; remove
  'Replacing OpenSSH "sshd" with Dropbear' part, this is simply done by not
  installing the openssh-server package.
* debian/README.runit: runsvstat -> sv status.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include <tomcrypt_test.h>
 
2
#if defined(GMP_DESC) || defined(USE_GMP)
 
3
#include <gmp.h>
 
4
#endif
2
5
 
3
6
#ifndef LTC_DER
4
7
 
10
13
 
11
14
#else
12
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
 
13
453
static int der_choice_test(void)
14
454
{
15
455
   ltc_asn1_list types[7], host[1];
16
456
   unsigned char bitbuf[10], octetbuf[10], ia5buf[10], printbuf[10], outbuf[256];
17
457
   unsigned long integer, oidbuf[10], outlen, inlen, x, y;
18
 
   mp_int        mpinteger;
 
458
   void          *mpinteger;
19
459
   ltc_utctime   utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
20
460
 
21
461
   /* setup variables */
25
465
   for (x = 0; x < sizeof(printbuf); x++) { printbuf[x] = 'a';   }
26
466
   integer = 1;
27
467
   for (x = 0; x < sizeof(oidbuf)/sizeof(oidbuf[0]); x++)   { oidbuf[x] = x + 1;   }
28
 
   DO(mpi_to_ltc_error(mp_init(&mpinteger)));
 
468
   DO(mp_init(&mpinteger));
29
469
 
30
470
   for (x = 0; x < 14; x++) {
31
471
       /* setup list */
36
476
       if (x > 7) {
37
477
          LTC_SET_ASN1(types, 4, LTC_ASN1_SHORT_INTEGER, &integer, 1);
38
478
       } else {
39
 
          LTC_SET_ASN1(types, 4, LTC_ASN1_INTEGER, &mpinteger, 1);
 
479
          LTC_SET_ASN1(types, 4, LTC_ASN1_INTEGER, mpinteger, 1);
40
480
       }
41
481
       LTC_SET_ASN1(types, 5, LTC_ASN1_OBJECT_IDENTIFIER, oidbuf, sizeof(oidbuf)/sizeof(oidbuf[0]));
42
482
       LTC_SET_ASN1(types, 6, LTC_ASN1_UTCTIME, &utctime, 1);
50
490
 
51
491
       /* decode it */
52
492
       inlen = outlen;
53
 
       DO(der_decode_sequence(outbuf, inlen, &host, 1));
 
493
       DO(der_decode_sequence(outbuf, inlen, &host[0], 1));
54
494
 
55
495
       for (y = 0; y < 7; y++) {
56
496
           if (types[y].used && y != (x>6?x-7:x)) {
63
503
           }
64
504
      }
65
505
  }
66
 
  mp_clear(&mpinteger);
 
506
  mp_clear(mpinteger);
67
507
  return 0;
68
508
}
69
509
   
72
512
{
73
513
   unsigned long x, y, z, zz, oid[2][32];
74
514
   unsigned char buf[3][2048];
75
 
   mp_int a, b, c, d, e, f, g;
 
515
   void *a, *b, *c, *d, *e, *f, *g;
76
516
 
77
517
   static const unsigned char rsa_oid_der[] = { 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d };
78
518
   static const unsigned long rsa_oid[]     = { 1, 2, 840, 113549 };
92
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 };
93
533
   static const unsigned char rsa_time2_der[] = { 0x17, 0x0d, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x32, 0x33, 0x34, 0x35, 0x34, 0x30, 0x5a };
94
534
 
95
 
   DO(mpi_to_ltc_error(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL)));
 
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));
96
544
   for (zz = 0; zz < 16; zz++) {
 
545
#ifdef USE_TFM
 
546
      for (z = 0; z < 256; z++) {
 
547
#else
97
548
      for (z = 0; z < 1024; z++) {
 
549
#endif
98
550
         if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
99
551
            fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
100
552
            return 1;
101
553
         }
102
 
         DO(mpi_to_ltc_error(mp_read_unsigned_bin(&a, buf[0], z)));
103
 
         if (mp_iszero(&a) == MP_NO) { a.sign = buf[0][0] & 1 ? MP_ZPOS : MP_NEG; }
 
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; } */
104
556
         x = sizeof(buf[0]);
105
 
         DO(der_encode_integer(&a, buf[0], &x));
106
 
         DO(der_length_integer(&a, &y));
 
557
         DO(der_encode_integer(a, buf[0], &x));
 
558
         DO(der_length_integer(a, &y));
107
559
         if (y != x) { fprintf(stderr, "DER INTEGER size mismatch\n"); return 1; }
108
 
         mp_zero(&b);
109
 
         DO(der_decode_integer(buf[0], y, &b));
110
 
         if (y != x || mp_cmp(&a, &b) != MP_EQ) {
 
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) {
111
563
            fprintf(stderr, "%lu: %lu vs %lu\n", z, x, y);
112
 
#ifdef BN_MP_TORADIX_C
113
 
            mp_todecimal(&a, buf[0]);
114
 
            mp_todecimal(&b, buf[1]);
115
 
            fprintf(stderr, "a == %s\nb == %s\n", buf[0], buf[1]);
116
 
#endif
117
 
            mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
 
564
            mp_clear_multi(a, b, c, d, e, f, g, NULL);
118
565
            return 1;
119
566
         }
120
567
      }
128
575
            return 1;
129
576
         }
130
577
         /* encode with normal */
131
 
         DO(mpi_to_ltc_error(mp_read_unsigned_bin(&a, buf[0], z)));
 
578
         DO(mp_read_unsigned_bin(a, buf[0], z));
132
579
 
133
580
         x = sizeof(buf[0]);
134
 
         DO(der_encode_integer(&a, buf[0], &x));
 
581
         DO(der_encode_integer(a, buf[0], &x));
135
582
 
136
583
         /* encode with short */
137
584
         y = sizeof(buf[1]);
138
 
         DO(der_encode_short_integer(mp_get_int(&a), buf[1], &y));
 
585
         DO(der_encode_short_integer(mp_get_int(a), buf[1], &y));
139
586
         if (x != y || memcmp(buf[0], buf[1], x)) {
140
587
            fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu\n", x, y);
141
588
            for (z = 0; z < x; z++) fprintf(stderr, "%02x ", buf[0][z]); fprintf(stderr, "\n");
142
589
            for (z = 0; z < y; z++) fprintf(stderr, "%02x ", buf[1][z]); fprintf(stderr, "\n");
143
 
            mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
 
590
            mp_clear_multi(a, b, c, d, e, f, g, NULL);
144
591
            return 1;
145
592
         }
146
593
 
147
594
         /* decode it */
148
595
         x = 0;
149
596
         DO(der_decode_short_integer(buf[1], y, &x));
150
 
         if (x != mp_get_int(&a)) {
151
 
            fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, mp_get_int(&a));
152
 
            mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
 
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);
153
600
            return 1;
154
601
         }
155
602
      }
156
603
   } 
157
 
   mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
 
604
   mp_clear_multi(a, b, c, d, e, f, g, NULL);
158
605
 
159
606
   
160
607
/* Test bit string */
199
646
 
200
647
/* test OID */
201
648
   x = sizeof(buf[0]);
202
 
   DO(der_encode_object_identifier(rsa_oid, sizeof(rsa_oid)/sizeof(rsa_oid[0]), buf[0], &x));
 
649
   DO(der_encode_object_identifier((unsigned long*)rsa_oid, sizeof(rsa_oid)/sizeof(rsa_oid[0]), buf[0], &x));
203
650
   if (x != sizeof(rsa_oid_der) || memcmp(rsa_oid_der, buf[0], x)) {
204
651
      fprintf(stderr, "rsa_oid_der encode failed to match, %lu, ", x);
205
652
      for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
259
706
 
260
707
/* IA5 string */
261
708
   x = sizeof(buf[0]);
262
 
   DO(der_encode_ia5_string(rsa_ia5, strlen(rsa_ia5), buf[0], &x));
 
709
   DO(der_encode_ia5_string(rsa_ia5, strlen((char*)rsa_ia5), buf[0], &x));
263
710
   if (x != sizeof(rsa_ia5_der) || memcmp(buf[0], rsa_ia5_der, x)) {
264
711
      fprintf(stderr, "IA5 encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_ia5_der));
265
712
      return 1;
266
713
   }
267
 
   DO(der_length_ia5_string(rsa_ia5, strlen(rsa_ia5), &y));
 
714
   DO(der_length_ia5_string(rsa_ia5, strlen((char*)rsa_ia5), &y));
268
715
   if (y != x) {
269
716
      fprintf(stderr, "IA5 length failed to match: %lu, %lu\n", x, y);
270
717
      return 1;
271
718
   }
272
719
   y = sizeof(buf[1]);
273
720
   DO(der_decode_ia5_string(buf[0], x, buf[1], &y));
274
 
   if (y != strlen(rsa_ia5) || memcmp(buf[1], rsa_ia5, strlen(rsa_ia5))) {
 
721
   if (y != strlen((char*)rsa_ia5) || memcmp(buf[1], rsa_ia5, strlen((char*)rsa_ia5))) {
275
722
       fprintf(stderr, "DER IA5 failed test vector\n");
276
723
       return 1;
277
724
   }
278
725
 
279
726
/* Printable string */
280
727
   x = sizeof(buf[0]);
281
 
   DO(der_encode_printable_string(rsa_printable, strlen(rsa_printable), buf[0], &x));
 
728
   DO(der_encode_printable_string(rsa_printable, strlen((char*)rsa_printable), buf[0], &x));
282
729
   if (x != sizeof(rsa_printable_der) || memcmp(buf[0], rsa_printable_der, x)) {
283
730
      fprintf(stderr, "PRINTABLE encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_printable_der));
284
731
      return 1;
285
732
   }
286
 
   DO(der_length_printable_string(rsa_printable, strlen(rsa_printable), &y));
 
733
   DO(der_length_printable_string(rsa_printable, strlen((char*)rsa_printable), &y));
287
734
   if (y != x) {
288
735
      fprintf(stderr, "printable length failed to match: %lu, %lu\n", x, y);
289
736
      return 1;
290
737
   }
291
738
   y = sizeof(buf[1]);
292
739
   DO(der_decode_printable_string(buf[0], x, buf[1], &y));
293
 
   if (y != strlen(rsa_printable) || memcmp(buf[1], rsa_printable, strlen(rsa_printable))) {
 
740
   if (y != strlen((char*)rsa_printable) || memcmp(buf[1], rsa_printable, strlen((char*)rsa_printable))) {
294
741
       fprintf(stderr, "DER printable failed test vector\n");
295
742
       return 1;
296
743
   }
297
744
 
298
745
/* Test UTC time */
299
746
   x = sizeof(buf[0]);
300
 
   DO(der_encode_utctime(&rsa_time1, buf[0], &x));
 
747
   DO(der_encode_utctime((ltc_utctime*)&rsa_time1, buf[0], &x));
301
748
   if (x != sizeof(rsa_time1_der) || memcmp(buf[0], rsa_time1_der, x)) {
302
749
      fprintf(stderr, "UTCTIME encode of rsa_time1 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
303
750
fprintf(stderr, "\n\n");
305
752
 
306
753
      return 1;
307
754
   }
308
 
   DO(der_length_utctime(&rsa_time1, &y));
 
755
   DO(der_length_utctime((ltc_utctime*)&rsa_time1, &y));
309
756
   if (y != x) {
310
757
      fprintf(stderr, "UTCTIME length failed to match for rsa_time1: %lu, %lu\n", x, y);
311
758
      return 1;
327
774
   }
328
775
 
329
776
   x = sizeof(buf[0]);
330
 
   DO(der_encode_utctime(&rsa_time2, buf[0], &x));
 
777
   DO(der_encode_utctime((ltc_utctime*)&rsa_time2, buf[0], &x));
331
778
   if (x != sizeof(rsa_time2_der) || memcmp(buf[0], rsa_time2_der, x)) {
332
779
      fprintf(stderr, "UTCTIME encode of rsa_time2 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
333
780
fprintf(stderr, "\n\n");
335
782
 
336
783
      return 1;
337
784
   }
338
 
   DO(der_length_utctime(&rsa_time2, &y));
 
785
   DO(der_length_utctime((ltc_utctime*)&rsa_time2, &y));
339
786
   if (y != x) {
340
787
      fprintf(stderr, "UTCTIME length failed to match for rsa_time2: %lu, %lu\n", x, y);
341
788
      return 1;
358
805
      return 1;
359
806
   }
360
807
 
361
 
 
362
 
 
 
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();
363
846
   return der_choice_test();
364
847
}
365
848
 
366
849
#endif
367
850
 
368
851
/* $Source: /cvs/libtom/libtomcrypt/testprof/der_tests.c,v $ */
369
 
/* $Revision: 1.25 $ */
370
 
/* $Date: 2005/06/20 20:37:45 $ */
 
852
/* $Revision: 1.49 $ */
 
853
/* $Date: 2006/11/26 02:10:21 $ */