2 * tpVerifyParsed.cpp - wrapper for CSSM_TP_CertGroupVerify using parsd anchors.
5 #include <Security/Security.h>
6 #include "tpVerifyParsed.h"
7 #include <Security/SecRootCertStorePriv.h>
8 #include <Security/RootCertCachePriv.h>
12 #include <clAppUtils/CertParser.h>
13 #include <utilLib/common.h>
16 * The main task is converting a set of CSSM_DATA-style anchors into a
17 * SecParsedRootCertArrayRef.
20 /* raw cert --> SecParsedRootCert */
21 static int parseRootCert(
22 CSSM_CL_HANDLE clHand
,
23 const CSSM_DATA
&certData
,
24 SecParsedRootCert
&parsedRoot
)
27 CertParser
cert(clHand
, certData
);
29 const void *p
= cert
.fieldForOid(CSSMOID_X509V1SubjectName
, len
);
30 appCopyData(p
, len
, &parsedRoot
.subject
);
32 /* skip key and times, I think they are going away */
33 appCopyCssmData(&certData
, &parsedRoot
.certData
);
37 printf("CertParser threw!\n");
42 static void freeParsedRoot(
43 SecParsedRootCert
&parsedRoot
)
45 if(parsedRoot
.subject
.Data
) {
46 CSSM_FREE(parsedRoot
.subject
.Data
);
48 if(parsedRoot
.certData
.Data
) {
49 CSSM_FREE(parsedRoot
.certData
.Data
);
53 static int createParsedCertArray(
54 CSSM_CL_HANDLE clHand
,
55 unsigned numAnchorCerts
,
56 CSSM_DATA_PTR anchorCerts
,
57 SecParsedRootCertArrayRef
*arrayRef
) // RETURNED
59 SecParsedRootCertArray
*outArray
= (SecParsedRootCertArray
*)malloc(sizeof(*outArray
));
60 memset(outArray
, 0, sizeof(*outArray
));
61 unsigned len
= sizeof(SecParsedRootCert
) * numAnchorCerts
;
62 outArray
->roots
= (SecParsedRootCert
*)malloc(len
);
63 memset(outArray
->roots
, 0, len
);
64 for(unsigned dex
=0; dex
<numAnchorCerts
; dex
++) {
65 if(parseRootCert(clHand
, anchorCerts
[dex
], outArray
->roots
[dex
])) {
69 outArray
->numRoots
= numAnchorCerts
;
74 static void freeParsedCertArray(
75 SecParsedRootCertArrayRef arrayRef
)
77 for(unsigned dex
=0; dex
<arrayRef
->numRoots
; dex
++) {
78 freeParsedRoot(arrayRef
->roots
[dex
]);
80 free(arrayRef
->roots
);
81 free((void *)arrayRef
);
84 CSSM_RETURN
tpCertGroupVerifyParsed(
85 CSSM_TP_HANDLE tpHand
,
86 CSSM_CL_HANDLE clHand
,
87 CSSM_CSP_HANDLE cspHand
,
88 CSSM_DL_DB_LIST_PTR dbListPtr
,
89 const CSSM_OID
*policy
, // optional
90 const CSSM_DATA
*fieldOpts
, // optional
91 const CSSM_DATA
*actionData
, // optional
93 const CSSM_CERTGROUP
*certGroup
,
94 CSSM_DATA_PTR anchorCerts
,
95 unsigned numAnchorCerts
,
96 CSSM_TP_STOP_ON stopOn
, // CSSM_TP_STOP_ON_POLICY, etc.
97 CSSM_TIMESTRING cssmTimeStr
,// optional
98 CSSM_TP_VERIFY_CONTEXT_RESULT_PTR result
) // optional, RETURNED
100 /* main job is building a CSSM_TP_VERIFY_CONTEXT and its components */
101 CSSM_TP_VERIFY_CONTEXT vfyCtx
;
102 CSSM_TP_CALLERAUTH_CONTEXT authCtx
;
104 memset(&vfyCtx
, 0, sizeof(CSSM_TP_VERIFY_CONTEXT
));
105 vfyCtx
.Action
= CSSM_TP_ACTION_DEFAULT
;
107 vfyCtx
.ActionData
= *actionData
;
110 vfyCtx
.ActionData
.Data
= NULL
;
111 vfyCtx
.ActionData
.Length
= 0;
113 vfyCtx
.Cred
= &authCtx
;
115 /* CSSM_TP_CALLERAUTH_CONTEXT components */
117 typedef struct cssm_tp_callerauth_context {
118 CSSM_TP_POLICYINFO Policy;
119 CSSM_TIMESTRING VerifyTime;
120 CSSM_TP_STOP_ON VerificationAbortOn;
121 CSSM_TP_VERIFICATION_RESULTS_CALLBACK CallbackWithVerifiedCert;
122 uint32 NumberOfAnchorCerts;
123 CSSM_DATA_PTR AnchorCerts;
124 CSSM_DL_DB_LIST_PTR DBList;
125 CSSM_ACCESS_CREDENTIALS_PTR CallerCredentials;
126 } CSSM_TP_CALLERAUTH_CONTEXT, *CSSM_TP_CALLERAUTH_CONTEXT_PTR;
128 /* zero or one policy here */
131 policyId
.FieldOid
= (CSSM_OID
)*policy
;
132 authCtx
.Policy
.NumberOfPolicyIds
= 1;
133 authCtx
.Policy
.PolicyIds
= &policyId
;
134 if(fieldOpts
!= NULL
) {
135 policyId
.FieldValue
= *fieldOpts
;
138 policyId
.FieldValue
.Data
= NULL
;
139 policyId
.FieldValue
.Length
= 0;
143 authCtx
.Policy
.NumberOfPolicyIds
= 0;
144 authCtx
.Policy
.PolicyIds
= NULL
;
146 authCtx
.Policy
.PolicyControl
= policyOpts
;
147 authCtx
.VerifyTime
= cssmTimeStr
; // may be NULL
148 authCtx
.VerificationAbortOn
= stopOn
;
149 authCtx
.CallbackWithVerifiedCert
= NULL
;
151 /* here's the difference between this and tpCertGroupVerify */
152 SecParsedRootCertArrayRef arrayRef
= NULL
;
154 if(createParsedCertArray(clHand
, numAnchorCerts
, anchorCerts
, &arrayRef
)) {
157 authCtx
.NumberOfAnchorCerts
= APPLE_TP_PARSED_ANCHOR_INDICATOR
;
158 authCtx
.AnchorCerts
= (CSSM_DATA_PTR
)arrayRef
;
161 authCtx
.NumberOfAnchorCerts
= 0;
162 authCtx
.AnchorCerts
= NULL
;
164 authCtx
.DBList
= dbListPtr
;
165 authCtx
.CallerCredentials
= NULL
;
167 CSSM_RETURN crtn
= CSSM_TP_CertGroupVerify(tpHand
,
175 freeParsedCertArray(arrayRef
);