]> git.saurik.com Git - apple/security.git/blob - SecurityTests/clxutils/threadTest/derDecode.cpp
Security-57336.1.9.tar.gz
[apple/security.git] / SecurityTests / clxutils / threadTest / derDecode.cpp
1 /*
2 * DER decode test
3 */
4 #include "testParams.h"
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <time.h>
8 #include <string.h>
9 #include <utilLib/common.h>
10 #include <utilLib/cspwrap.h>
11 #include <Security/SecAsn1Coder.h>
12 #include <Security/SecAsn1Templates.h>
13 #include <stddef.h>
14 #include <unistd.h>
15
16 #define DO_BAD_DECODE 0
17 #define DO_PAUSE 0 /* for malloc debug */
18
19 /* good DER, bad DER */
20 static CSSM_DATA goodDer;
21 static CSSM_DATA badDer;
22
23 typedef struct {
24 CSSM_DATA int1;
25 CSSM_DATA int2;
26 } twoInts;
27
28 static const SecAsn1Template twoIntsTemp[] = {
29 { SEC_ASN1_SEQUENCE,
30 0, NULL, sizeof(twoInts) },
31 { SEC_ASN1_INTEGER, offsetof(twoInts, int1) },
32 { SEC_ASN1_INTEGER, offsetof(twoInts, int2) },
33 {0}
34 };
35
36 static uint8 int1[] = {1,2,3};
37 static uint8 int2[] = {3,4,5,6,7};
38
39 #define DECODES_PER_LOOP 1
40
41 int derDecodeInit(
42 TestParams *testParams)
43 {
44 /*
45 * DER encode a sequence of two integers
46 */
47 twoInts ti;
48 ti.int1.Data = int1;
49 ti.int1.Length = sizeof(int1);
50 ti.int2.Data = int2;
51 ti.int2.Length = sizeof(int2);
52
53 /* encode --> tempDer */
54 SecAsn1CoderRef coder;
55 SecAsn1CoderCreate(&coder);
56
57 CSSM_DATA tmpDer = {0, NULL};
58 if(SecAsn1EncodeItem(coder, &ti, twoIntsTemp, &tmpDer)) {
59 printf("***derDecodeInit: Error on encodeItem()\n");
60 return -1;
61 }
62
63 /* copy to goodDer and badDer */
64 appCopyCssmData(&tmpDer, &goodDer);
65 appCopyCssmData(&tmpDer, &badDer);
66
67 /* increment the length of the outer sequence to force error */
68 badDer.Data[1]++;
69 SecAsn1CoderRelease(coder);
70 return 0;
71 }
72
73 int derDecodeTest(TestParams *testParams)
74 {
75 /* which flavor - good or bad? */
76 const CSSM_DATA *derSrc;
77 bool expectErr = false;
78
79 if((testParams->threadNum & 1) || !DO_BAD_DECODE) {
80 derSrc = &goodDer;
81 }
82 else {
83 derSrc = &badDer;
84 expectErr = true;
85 }
86 for(unsigned loop=0; loop<testParams->numLoops; loop++) {
87 if(testParams->verbose) {
88 printf("derDecode thread %d: loop %d\n",
89 testParams->threadNum, loop);
90 }
91 else if(!testParams->quiet) {
92 printChar(testParams->progressChar);
93 }
94
95 for(unsigned dex=0; dex<DECODES_PER_LOOP; dex++) {
96 SecAsn1CoderRef coder;
97 SecAsn1CoderCreate(&coder);
98 twoInts ti;
99 OSStatus perr1, perr2;
100 memset(&ti, 0, sizeof(ti));
101 perr1 = SecAsn1DecodeData(coder, derSrc, twoIntsTemp, &ti);
102 usleep(100);
103 perr2 = SecAsn1DecodeData(coder, derSrc, twoIntsTemp, &ti);
104 if(perr1 != perr2) {
105 printf("***derDecodeTest: different errors (%d, %d)\n",
106 (int)perr1, (int)perr2);
107 return 1;
108 }
109 if(expectErr) {
110 if(!perr1) {
111 printf("derDecodeTest: expect failure, got success\n");
112 return 1;
113 }
114 }
115 else {
116 if(perr1) {
117 printf("derDecodeTest: expect success, got %d\n", (int)perr1);
118 return 1;
119 }
120 }
121 SecAsn1CoderRelease(coder);
122 }
123 #if DO_PAUSE
124 fflush(stdin);
125 printf("End of loop, CR to continue: ");
126 getchar();
127 #endif
128 }
129 return 0;
130 }
131