4 * track down multithread SecureTransport memory smasher - this test
7 #include "testParams.h"
8 #include <Security/cssm.h>
9 #include <utilLib/common.h>
10 #include <utilLib/cspwrap.h>
11 #include <clAppUtils/clutils.h>
12 #include <clAppUtils/tpUtils.h>
13 #include <security_cdsa_utils/cuFileIo.h>
18 #include <Security/SecureTransport.h>
22 #define NUM_INNER_LOOPS 10
26 * Derive a symmetric CSSM_KEY from the specified raw key material.
28 static CSSM_RETURN
cdsaDeriveKey(
29 CSSM_CSP_HANDLE cspHandle
,
32 CSSM_ALGORITHMS keyAlg
, // e.g., CSSM_ALGID_AES
37 CSSM_CC_HANDLE ccHand
;
38 CSSM_DATA dummyLabel
= {8, (uint8
*)"tempKey"};
39 CSSM_DATA saltData
= {8, (uint8
*)"someSalt"};
40 CSSM_PKCS5_PBKDF2_PARAMS pbeParams
;
42 CSSM_ACCESS_CREDENTIALS creds
;
44 memset(key
, 0, sizeof(CSSM_KEY
));
45 memset(&creds
, 0, sizeof(CSSM_ACCESS_CREDENTIALS
));
46 crtn
= CSSM_CSP_CreateDeriveKeyContext(cspHandle
,
47 CSSM_ALGID_PKCS5_PBKDF2
,
52 1000, // iterationCount, 1000 is the minimum
57 cssmPerror("CSSM_CSP_CreateDeriveKeyContext", crtn
);
61 /* this is the caller's raw key bits, typically ASCII (though it
62 * could be anything) */
63 pbeParams
.Passphrase
.Data
= (uint8
*)rawKey
;
64 pbeParams
.Passphrase
.Length
= rawKeyLen
;
65 /* The only PRF supported by the CSP is HMACSHA1 */
66 pbeParams
.PseudoRandomFunction
= CSSM_PKCS5_PBKDF2_PRF_HMAC_SHA1
;
67 pbeData
.Data
= (uint8
*)&pbeParams
;
68 pbeData
.Length
= sizeof(pbeParams
);
69 crtn
= CSSM_DeriveKey(ccHand
,
72 CSSM_KEYATTR_RETURN_DATA
| CSSM_KEYATTR_EXTRACTABLE
,
76 CSSM_DeleteContext(ccHand
); // ignore error here
78 cssmPerror("CSSM_DeriveKey", crtn
);
83 static CSSM_API_MEMORY_FUNCS memFuncs
= {
90 static CSSM_VERSION vers
= {2, 0};
93 * Initialize CDSA and attach to the CSP.
95 static CSSM_RETURN
cdsaCspAttach(
96 CSSM_CSP_HANDLE
*cspHandle
)
98 CSSM_CSP_HANDLE cspHand
;
101 /* Load the CSP bundle into this app's memory space */
102 crtn
= CSSM_ModuleLoad(&gGuidAppleCSP
,
103 CSSM_KEY_HIERARCHY_NONE
,
104 NULL
, // eventHandler
105 NULL
); // AppNotifyCallbackCtx
110 /* obtain a handle which will be used to refer to the CSP */
111 crtn
= CSSM_ModuleAttach (&gGuidAppleCSP
,
113 &memFuncs
, // memFuncs
117 CSSM_KEY_HIERARCHY_NONE
,
118 NULL
, // FunctionTable
125 *cspHandle
= cspHand
;
129 static bool thrashInit
= false;
130 static bool doSsl
= true;
131 static bool doKey
= true;
132 static bool doAttach
= true;
134 int sslThrashInit(TestParams
*testParams
)
140 char *opts
= testParams
->testOpts
;
145 while(*opts
!= '\0') {
149 printf("...sslThrash: doKey disabled\n");
153 printf("...sslThrash: doSsl disabled\n");
157 printf("...sslThrash: doAttach disabled\n");
160 /* for other tests */
170 #define SSL_CTX_SIZE 600
171 #define FAKE_DISPOSE 0
173 int sslThrash(TestParams
*testParams
)
177 SSLContextRef sslCtx
;
182 for(loopNum
=0; loopNum
<testParams
->numLoops
; loopNum
++) {
183 if(testParams
->verbose
) {
184 printf("sslThrash loop %d\n", loopNum
);
186 else if(!testParams
->quiet
) {
187 printChar(testParams
->progressChar
);
190 for(dex
=0; dex
<NUM_INNER_LOOPS
; dex
++) {
192 crtn
= cdsaCspAttach(&cspHand
);
194 printf("cdsaCspAttach error\n");
200 sslCtx
= (SSLContext
*)malloc(SSL_CTX_SIZE
);
202 OSStatus ortn
= SSLNewContext(false, &sslCtx
);
204 cssmPerror("SSLNewContext", ortn
);
205 printf("SSLNewContext error %d\n", (int)ortn
);
211 crtn
= cdsaDeriveKey(testParams
->cspHand
,
218 printf("cdsaDeriveKey error\n");
223 #if FAKE_SSL || FAKE_DISPOSE
226 OSStatus ortn
= SSLDisposeContext(sslCtx
);
228 cssmPerror("SSLDisposeContext", ortn
);
229 printf("SSLDisposeContext error %d\n", (int)ortn
);
235 crtn
= CSSM_FreeKey(testParams
->cspHand
,
240 cssmPerror("CSSM_FreeKey", crtn
);
241 printf("CSSM_FreeKey error\n");
246 crtn
= CSSM_ModuleDetach(cspHand
);
248 cssmPerror("CSSM_ModuleDetach", crtn
);
249 printf("CSSM_ModuleDetach error\n");
258 printf("Hit CR to continue: ");