2 * clutils.c - common CL app-level routines, X version
7 #include <Security/cssm.h>
9 #include <Security/cssmapple.h> /* apple, not intel */
10 #include <utilLib/common.h>
12 static CSSM_API_MEMORY_FUNCS memFuncs
= {
20 static CSSM_VERSION vers
= {2, 0};
23 * Init CSSM and establish a session with the Apple CL.
25 CSSM_CL_HANDLE
clStartup()
27 CSSM_CL_HANDLE clHand
;
30 if(cssmStartup() == CSSM_FALSE
) {
33 crtn
= CSSM_ModuleLoad(&gGuidAppleX509CL
,
34 CSSM_KEY_HIERARCHY_NONE
,
36 NULL
); // AppNotifyCallbackCtx
38 printError("CSSM_ModuleLoad(AppleCL)", crtn
);
41 crtn
= CSSM_ModuleAttach (&gGuidAppleX509CL
,
43 &memFuncs
, // memFuncs
45 CSSM_SERVICE_CL
, // SubserviceFlags - Where is this used?
47 CSSM_KEY_HIERARCHY_NONE
,
48 NULL
, // FunctionTable
53 printError("CSSM_ModuleAttach(AppleCL)", crtn
);
62 CSSM_CL_HANDLE clHand
)
66 crtn
= CSSM_ModuleDetach(clHand
);
68 printf("Error detaching from AppleCL\n");
69 printError("CSSM_ModuleDetach", crtn
);
72 crtn
= CSSM_ModuleUnload(&gGuidAppleX509CL
, NULL
, NULL
);
74 printf("Error unloading AppleCL\n");
75 printError("CSSM_ModuleUnload", crtn
);
80 * Init CSSM and establish a session with the Apple TP.
82 CSSM_TP_HANDLE
tpStartup()
84 CSSM_TP_HANDLE tpHand
;
87 if(cssmStartup() == CSSM_FALSE
) {
90 crtn
= CSSM_ModuleLoad(&gGuidAppleX509TP
,
91 CSSM_KEY_HIERARCHY_NONE
,
93 NULL
); // AppNotifyCallbackCtx
95 printError("CSSM_ModuleLoad(AppleTP)", crtn
);
98 crtn
= CSSM_ModuleAttach (&gGuidAppleX509TP
,
100 &memFuncs
, // memFuncs
102 CSSM_SERVICE_TP
, // SubserviceFlags
104 CSSM_KEY_HIERARCHY_NONE
,
105 NULL
, // FunctionTable
110 printError("CSSM_ModuleAttach(AppleTP)", crtn
);
119 CSSM_TP_HANDLE tpHand
)
123 crtn
= CSSM_ModuleDetach(tpHand
);
125 printf("Error detaching from AppleTP\n");
126 printError("CSSM_ModuleDetach", crtn
);
129 crtn
= CSSM_ModuleUnload(&gGuidAppleX509TP
, NULL
, NULL
);
131 printf("Error unloading AppleTP\n");
132 printError("CSSM_ModuleUnload", crtn
);
138 * Cook up a CSSM_DATA with specified integer, DER style (minimum number of
139 * bytes, big-endian).
141 CSSM_DATA_PTR
intToDER(unsigned theInt
)
143 CSSM_DATA_PTR DER_Data
= (CSSM_DATA_PTR
)CSSM_MALLOC(sizeof(CSSM_DATA
));
146 DER_Data
->Length
= 1;
147 DER_Data
->Data
= (uint8
*)CSSM_MALLOC(1);
148 DER_Data
->Data
[0] = (unsigned char)(theInt
);
150 else if(theInt
< 0x10000) {
151 DER_Data
->Length
= 2;
152 DER_Data
->Data
= (uint8
*)CSSM_MALLOC(2);
153 DER_Data
->Data
[0] = (unsigned char)(theInt
>> 8);
154 DER_Data
->Data
[1] = (unsigned char)(theInt
);
156 else if(theInt
< 0x1000000) {
157 DER_Data
->Length
= 3;
158 DER_Data
->Data
= (uint8
*)CSSM_MALLOC(3);
159 DER_Data
->Data
[0] = (unsigned char)(theInt
>> 16);
160 DER_Data
->Data
[1] = (unsigned char)(theInt
>> 8);
161 DER_Data
->Data
[2] = (unsigned char)(theInt
);
164 DER_Data
->Length
= 4;
165 DER_Data
->Data
= (uint8
*)CSSM_MALLOC(4);
166 DER_Data
->Data
[0] = (unsigned char)(theInt
>> 24);
167 DER_Data
->Data
[1] = (unsigned char)(theInt
>> 16);
168 DER_Data
->Data
[2] = (unsigned char)(theInt
>> 8);
169 DER_Data
->Data
[3] = (unsigned char)(theInt
);
175 * Convert a CSSM_DATA_PTR, referring to a DER-encoded int, to a
178 uint32
DER_ToInt(const CSSM_DATA
*DER_Data
)
183 while(i
< DER_Data
->Length
) {
184 rtn
|= DER_Data
->Data
[i
];
185 if(++i
== DER_Data
->Length
) {