Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / libpkix / pkix / certsel / pkix_certselector.c
blob37e538132bf2bd6613013b6447e4025eb7de1e12
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Sun Microsystems
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 * pkix_certselector.c
40 * CertSelector Object Functions
44 #include "pkix_certselector.h"
46 /* --Private-Functions-------------------------------------------- */
49 * FUNCTION: pkix_CertSelector_Destroy
50 * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
52 static PKIX_Error *
53 pkix_CertSelector_Destroy(
54 PKIX_PL_Object *object,
55 void *plContext)
57 PKIX_CertSelector *selector = NULL;
59 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_Destroy");
60 PKIX_NULLCHECK_ONE(object);
62 /* Check that this object is a cert selector */
63 PKIX_CHECK(pkix_CheckType(object, PKIX_CERTSELECTOR_TYPE, plContext),
64 PKIX_OBJECTNOTCERTSELECTOR);
66 selector = (PKIX_CertSelector *)object;
67 PKIX_DECREF(selector->params);
68 PKIX_DECREF(selector->context);
70 cleanup:
72 PKIX_RETURN(CERTSELECTOR);
76 * FUNCTION: pkix_CertSelector_Duplicate
77 * (see comments for PKIX_PL_DuplicateCallback in pkix_pl_system.h)
79 static PKIX_Error *
80 pkix_CertSelector_Duplicate(
81 PKIX_PL_Object *object,
82 PKIX_PL_Object **pNewObject,
83 void *plContext)
85 PKIX_CertSelector *certSelector = NULL;
86 PKIX_CertSelector *certSelectorDuplicate = NULL;
88 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_Duplicate");
89 PKIX_NULLCHECK_TWO(object, pNewObject);
91 PKIX_CHECK(pkix_CheckType(object, PKIX_CERTSELECTOR_TYPE, plContext),
92 PKIX_OBJECTNOTCERTSELECTOR);
94 certSelector = (PKIX_CertSelector *)object;
96 PKIX_CHECK(PKIX_CertSelector_Create
97 (certSelector->matchCallback,
98 certSelector->context,
99 &certSelectorDuplicate,
100 plContext),
101 PKIX_CERTSELECTORCREATEFAILED);
103 PKIX_CHECK(PKIX_PL_Object_Duplicate
104 ((PKIX_PL_Object *)certSelector->params,
105 (PKIX_PL_Object **)&certSelectorDuplicate->params,
106 plContext),
107 PKIX_OBJECTDUPLICATEFAILED);
109 *pNewObject = (PKIX_PL_Object *)certSelectorDuplicate;
111 cleanup:
113 if (PKIX_ERROR_RECEIVED){
114 PKIX_DECREF(certSelectorDuplicate);
117 PKIX_RETURN(CERTSELECTOR);
121 * FUNCTION: pkix_CertSelector_Match_BasicConstraint
122 * DESCRIPTION:
124 * Determines whether the Cert pointed to by "cert" matches the basic
125 * constraints criterion using the basic constraints field of the
126 * ComCertSelParams pointed to by "params". If the basic constraints field is
127 * -1, no basic constraints check is done and the Cert is considered to match
128 * the basic constraints criterion. If the Cert does not match the basic
129 * constraints criterion, an Error pointer is returned.
131 * In order to match against this criterion, there are several possibilities.
133 * 1) If the criterion's minimum path length is greater than or equal to zero,
134 * a certificate must include a BasicConstraints extension with a pathLen of
135 * at least this value.
137 * 2) If the criterion's minimum path length is -2, a certificate must be an
138 * end-entity certificate.
140 * 3) If the criterion's minimum path length is -1, no basic constraints check
141 * is done and all certificates are considered to match this criterion.
143 * PARAMETERS:
144 * "params"
145 * Address of ComCertSelParams whose basic constraints field is used.
146 * Must be non-NULL.
147 * "cert"
148 * Address of Cert that is to be matched. Must be non-NULL.
149 * "pResult"
150 * Address of PKIX_Boolean that returns the match result.
151 * "plContext"
152 * Platform-specific context pointer.
153 * THREAD SAFETY:
154 * Conditionally Thread Safe
155 * (see Thread Safety Definitions in Programmer's Guide)
156 * RETURNS:
157 * Returns NULL if the function succeeds.
158 * Returns a CertSelector Error if the function fails in a non-fatal way.
159 * Returns a Fatal Error if the function fails in an unrecoverable way.
161 static PKIX_Error *
162 pkix_CertSelector_Match_BasicConstraint(
163 PKIX_ComCertSelParams *params,
164 PKIX_PL_Cert *cert,
165 PKIX_Boolean *pResult,
166 void *plContext)
168 PKIX_PL_CertBasicConstraints *basicConstraints = NULL;
169 PKIX_Boolean caFlag = PKIX_FALSE; /* EE Cert by default */
170 PKIX_Int32 pathLength = 0;
171 PKIX_Int32 minPathLength = 0;
173 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_Match_BasicConstraint");
174 PKIX_NULLCHECK_THREE(params, cert, pResult);
176 PKIX_CHECK(PKIX_ComCertSelParams_GetBasicConstraints
177 (params, &minPathLength, plContext),
178 PKIX_COMCERTSELPARAMSGETBASICCONSTRAINTSFAILED);
180 /* If the minPathLength is unlimited (-1), no checking */
181 if (minPathLength == PKIX_CERTSEL_ALL_MATCH_MIN_PATHLENGTH) {
182 goto cleanup;
185 PKIX_CHECK(PKIX_PL_Cert_GetBasicConstraints
186 (cert, &basicConstraints, plContext),
187 PKIX_CERTGETBASICCONSTRAINTSFAILED);
189 if (basicConstraints != NULL) {
190 PKIX_CHECK(PKIX_PL_BasicConstraints_GetCAFlag
191 (basicConstraints, &caFlag, plContext),
192 PKIX_BASICCONSTRAINTSGETCAFLAGFAILED);
194 PKIX_CHECK(PKIX_PL_BasicConstraints_GetPathLenConstraint
195 (basicConstraints, &pathLength, plContext),
196 PKIX_BASICCONSTRAINTSGETPATHLENCONSTRAINTFAILED);
200 * if minPathLength >= 0, the cert must have a BasicConstraints ext and
201 * the pathLength in this cert
202 * BasicConstraints needs to be >= minPathLength.
204 if (minPathLength >= 0){
205 if ((!basicConstraints) || (caFlag == PKIX_FALSE)){
206 PKIX_ERROR(PKIX_CERTNOTALLOWEDTOSIGNCERTIFICATES);
207 } else if ((pathLength != PKIX_UNLIMITED_PATH_CONSTRAINT) &&
208 (pathLength < minPathLength)){
209 PKIX_CERTSELECTOR_DEBUG
210 ("Basic Constraints path length match failed\n");
211 *pResult = PKIX_FALSE;
212 goto cleanup;
216 /* if the minPathLength is -2, this cert must be an end-entity cert. */
217 if (minPathLength == PKIX_CERTSEL_ENDENTITY_MIN_PATHLENGTH) {
218 if (caFlag == PKIX_TRUE) {
219 PKIX_CERTSELECTOR_DEBUG
220 ("Basic Constraints end-entity match failed\n");
221 *pResult = PKIX_FALSE;
222 goto cleanup;
226 cleanup:
228 PKIX_DECREF(basicConstraints);
229 PKIX_RETURN(CERTSELECTOR);
233 * FUNCTION: pkix_CertSelector_Match_Policies
234 * DESCRIPTION:
236 * Determines whether the Cert pointed to by "cert" matches the policy
237 * constraints specified in the ComCertsSelParams given by "params".
238 * If "params" specifies a policy constraint of NULL, all certificates
239 * match. If "params" specifies an empty list, "cert" must have at least
240 * some policy. Otherwise "cert" must include at least one of the
241 * policies in the list. See the description of PKIX_CertSelector in
242 * pkix_certsel.h for more.
244 * PARAMETERS:
245 * "params"
246 * Address of ComCertSelParams whose policy criterion (if any) is used.
247 * Must be non-NULL.
248 * "cert"
249 * Address of Cert that is to be matched. Must be non-NULL.
250 * "pResult"
251 * Address of PKIX_Boolean that returns the match result.
252 * "plContext"
253 * Platform-specific context pointer.
254 * THREAD SAFETY:
255 * Conditionally Thread Safe
256 * (see Thread Safety Definitions in Programmer's Guide)
257 * RETURNS:
258 * Returns NULL if the function succeeds.
259 * Returns a CertSelector Error if the function fails in a non-fatal way.
260 * Returns a Fatal Error if the function fails in an unrecoverable way.
262 static PKIX_Error *
263 pkix_CertSelector_Match_Policies(
264 PKIX_ComCertSelParams *params,
265 PKIX_PL_Cert *cert,
266 PKIX_Boolean *pResult,
267 void *plContext)
269 PKIX_UInt32 numConstraintPolicies = 0;
270 PKIX_UInt32 numCertPolicies = 0;
271 PKIX_UInt32 certPolicyIndex = 0;
272 PKIX_Boolean result = PKIX_FALSE;
273 PKIX_List *constraintPolicies = NULL; /* List of PKIX_PL_OID */
274 PKIX_List *certPolicyInfos = NULL; /* List of PKIX_PL_CertPolicyInfo */
275 PKIX_PL_CertPolicyInfo *policyInfo = NULL;
276 PKIX_PL_OID *polOID = NULL;
278 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_Match_Policies");
279 PKIX_NULLCHECK_THREE(params, cert, pResult);
281 PKIX_CHECK(PKIX_ComCertSelParams_GetPolicy
282 (params, &constraintPolicies, plContext),
283 PKIX_COMCERTSELPARAMSGETPOLICYFAILED);
285 /* If constraintPolicies is NULL, all certificates "match" */
286 if (constraintPolicies) {
287 PKIX_CHECK(PKIX_PL_Cert_GetPolicyInformation
288 (cert, &certPolicyInfos, plContext),
289 PKIX_CERTGETPOLICYINFORMATIONFAILED);
291 /* No hope of a match if cert has no policies */
292 if (!certPolicyInfos) {
293 PKIX_CERTSELECTOR_DEBUG("Certificate has no policies\n");
294 *pResult = PKIX_FALSE;
295 goto cleanup;
299 PKIX_CHECK(PKIX_List_GetLength
300 (constraintPolicies, &numConstraintPolicies, plContext),
301 PKIX_LISTGETLENGTHFAILED);
303 if (numConstraintPolicies > 0) {
305 PKIX_CHECK(PKIX_List_GetLength
306 (certPolicyInfos, &numCertPolicies, plContext),
307 PKIX_LISTGETLENGTHFAILED);
309 for (certPolicyIndex = 0;
310 ((!result) && (certPolicyIndex < numCertPolicies));
311 certPolicyIndex++) {
313 PKIX_CHECK(PKIX_List_GetItem
314 (certPolicyInfos,
315 certPolicyIndex,
316 (PKIX_PL_Object **)&policyInfo,
317 plContext),
318 PKIX_LISTGETELEMENTFAILED);
319 PKIX_CHECK(PKIX_PL_CertPolicyInfo_GetPolicyId
320 (policyInfo, &polOID, plContext),
321 PKIX_CERTPOLICYINFOGETPOLICYIDFAILED);
323 PKIX_CHECK(pkix_List_Contains
324 (constraintPolicies,
325 (PKIX_PL_Object *)polOID,
326 &result,
327 plContext),
328 PKIX_LISTCONTAINSFAILED);
329 PKIX_DECREF(policyInfo);
330 PKIX_DECREF(polOID);
332 if (!result) {
333 PKIX_CERTSELECTOR_DEBUG
334 ("Certificate has no acceptable policies\n");
335 *pResult = PKIX_FALSE;
336 goto cleanup;
341 cleanup:
343 PKIX_DECREF(constraintPolicies);
344 PKIX_DECREF(certPolicyInfos);
345 PKIX_DECREF(policyInfo);
346 PKIX_DECREF(polOID);
348 PKIX_RETURN(CERTSELECTOR);
353 * FUNCTION: pkix_CertSelector_Match_CertificateValid
354 * DESCRIPTION:
356 * Determines whether the Cert pointed to by "cert" matches the certificate
357 * validity criterion using the CertificateValid field of the
358 * ComCertSelParams pointed to by "params". If the CertificateValid field is
359 * NULL, no validity check is done and the Cert is considered to match
360 * the CertificateValid criterion. If the CertificateValid field specifies a
361 * Date prior to the notBefore field in the Cert, or greater than the notAfter
362 * field in the Cert, an Error is returned.
364 * PARAMETERS:
365 * "params"
366 * Address of ComCertSelParams whose certValid field is used.
367 * Must be non-NULL.
368 * "cert"
369 * Address of Cert that is to be matched. Must be non-NULL.
370 * "pResult"
371 * Address of PKIX_Boolean that returns the match result.
372 * "plContext"
373 * Platform-specific context pointer.
374 * THREAD SAFETY:
375 * Conditionally Thread Safe
376 * (see Thread Safety Definitions in Programmer's Guide)
377 * RETURNS:
378 * Returns NULL if the function succeeds.
379 * Returns a CertSelector Error if the function fails in a non-fatal way.
380 * Returns a Fatal Error if the function fails in an unrecoverable way.
382 static PKIX_Error *
383 pkix_CertSelector_Match_CertificateValid(
384 PKIX_ComCertSelParams *params,
385 PKIX_PL_Cert *cert,
386 PKIX_Boolean *pResult,
387 void *plContext)
389 PKIX_PL_Date *validityTime = NULL;
391 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_Match_CertificateValid");
392 PKIX_NULLCHECK_THREE(params, cert, pResult);
394 PKIX_CHECK(PKIX_ComCertSelParams_GetCertificateValid
395 (params, &validityTime, plContext),
396 PKIX_COMCERTSELPARAMSGETCERTIFICATEVALIDFAILED);
398 /* If the validityTime is not set, all certificates are acceptable */
399 if (validityTime) {
400 PKIX_CHECK(PKIX_PL_Cert_CheckValidity
401 (cert, validityTime, plContext),
402 PKIX_CERTCHECKVALIDITYFAILED);
405 cleanup:
407 PKIX_DECREF(validityTime);
409 PKIX_RETURN(CERTSELECTOR);
413 * FUNCTION: pkix_CertSelector_Match_NameConstraints
414 * DESCRIPTION:
416 * Determines whether the Cert pointed to by "cert" matches the name
417 * constraints criterion specified in the ComCertSelParams pointed to by
418 * "params". If the name constraints field is NULL, no name constraints check
419 * is done and the Cert is considered to match the name constraints criterion.
420 * If the Cert does not match the name constraints criterion, an Error pointer
421 * is returned.
423 * PARAMETERS:
424 * "params"
425 * Address of ComCertSelParams whose name constraints field is used.
426 * Must be non-NULL.
427 * "cert"
428 * Address of Cert that is to be matched. Must be non-NULL.
429 * "pResult"
430 * Address of PKIX_Boolean that returns the match result.
431 * "plContext"
432 * Platform-specific context pointer.
433 * THREAD SAFETY:
434 * Conditionally Thread Safe
435 * (see Thread Safety Definitions in Programmer's Guide)
436 * RETURNS:
437 * Returns NULL if the function succeeds.
438 * Returns a CertSelector Error if the function fails in a non-fatal way.
439 * Returns a Fatal Error if the function fails in an unrecoverable way.
441 static PKIX_Error *
442 pkix_CertSelector_Match_NameConstraints(
443 PKIX_ComCertSelParams *params,
444 PKIX_PL_Cert *cert,
445 PKIX_Boolean *pResult,
446 void *plContext)
448 PKIX_PL_CertNameConstraints *nameConstraints = NULL;
450 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_Match_NameConstraints");
451 PKIX_NULLCHECK_THREE(params, cert, pResult);
453 PKIX_CHECK(PKIX_ComCertSelParams_GetNameConstraints
454 (params, &nameConstraints, plContext),
455 PKIX_COMCERTSELPARAMSGETNAMECONSTRAINTSFAILED);
457 if (nameConstraints != NULL) {
459 PKIX_CHECK(PKIX_PL_Cert_CheckNameConstraints
460 (cert, nameConstraints, plContext),
461 PKIX_CERTCHECKNAMECONSTRAINTSFAILED);
464 cleanup:
466 PKIX_DECREF(nameConstraints);
467 PKIX_RETURN(CERTSELECTOR);
471 * FUNCTION: pkix_CertSelector_Match_PathToNames
472 * DESCRIPTION:
474 * Determines whether the names at pathToNames in "params" complies with the
475 * NameConstraints pointed to by "cert". If the pathToNames field is NULL
476 * or there is no name constraints for this "cert", no checking is done
477 * and the Cert is considered to match the name constraints criterion.
478 * If the Cert does not match the name constraints criterion, an Error
479 * pointer is returned.
481 * PARAMETERS:
482 * "params"
483 * Address of ComCertSelParams whose PathToNames field is used.
484 * Must be non-NULL.
485 * "cert"
486 * Address of Cert that is to be matched. Must be non-NULL.
487 * "pResult"
488 * Address of PKIX_Boolean that returns the match result.
489 * "plContext"
490 * Platform-specific context pointer.
491 * THREAD SAFETY:
492 * Conditionally Thread Safe
493 * (see Thread Safety Definitions in Programmer's Guide)
494 * RETURNS:
495 * Returns NULL if the function succeeds.
496 * Returns a CertSelector Error if the function fails in a non-fatal way.
497 * Returns a Fatal Error if the function fails in an unrecoverable way.
499 static PKIX_Error *
500 pkix_CertSelector_Match_PathToNames(
501 PKIX_ComCertSelParams *params,
502 PKIX_PL_Cert *cert,
503 PKIX_Boolean *pResult,
504 void *plContext)
506 PKIX_List *pathToNamesList = NULL;
507 PKIX_Boolean passed = PKIX_FALSE;
508 PKIX_PL_CertNameConstraints *nameConstraints = NULL;
510 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_Match_PathToNames");
511 PKIX_NULLCHECK_THREE(params, cert, pResult);
513 PKIX_CHECK(PKIX_ComCertSelParams_GetPathToNames
514 (params, &pathToNamesList, plContext),
515 PKIX_COMCERTSELPARAMSGETPATHTONAMESFAILED);
517 if (pathToNamesList != NULL) {
519 PKIX_CHECK(PKIX_PL_Cert_GetNameConstraints
520 (cert, &nameConstraints, plContext),
521 PKIX_CERTGETNAMECONSTRAINTSFAILED);
523 if (nameConstraints != NULL) {
525 PKIX_CHECK(PKIX_PL_CertNameConstraints_CheckNamesInNameSpace
526 (pathToNamesList, nameConstraints, &passed, plContext),
527 PKIX_CERTNAMECONSTRAINTSCHECKNAMESINNAMESPACEFAILED);
529 if (passed != PKIX_TRUE) {
530 PKIX_CERTSELECTOR_DEBUG("PathToName Match failed\n");
531 *pResult = PKIX_FALSE;
532 goto cleanup;
538 cleanup:
540 PKIX_DECREF(nameConstraints);
541 PKIX_DECREF(pathToNamesList);
543 PKIX_RETURN(CERTSELECTOR);
547 * FUNCTION: pkix_CertSelector_Match_SubjAltNames
548 * DESCRIPTION:
550 * Determines whether the names at subjAltNames in "params" match with the
551 * SubjAltNames pointed to by "cert". If the subjAltNames field is NULL,
552 * no name checking is done and the Cert is considered to match the
553 * criterion. If the Cert does not match the criterion, an Error pointer
554 * is returned.
556 * PARAMETERS:
557 * "params"
558 * Address of ComCertSelParams whose SubjAltNames field is used.
559 * Must be non-NULL.
560 * "cert"
561 * Address of Cert that is to be matched. Must be non-NULL.
562 * "pResult"
563 * Address of PKIX_Boolean that returns the match result.
564 * "plContext"
565 * Platform-specific context pointer.
566 * THREAD SAFETY:
567 * Conditionally Thread Safe
568 * (see Thread Safety Definitions in Programmer's Guide)
569 * RETURNS:
570 * Returns NULL if the function succeeds.
571 * Returns a CertSelector Error if the function fails in a non-fatal way.
572 * Returns a Fatal Error if the function fails in an unrecoverable way.
574 static PKIX_Error *
575 pkix_CertSelector_Match_SubjAltNames(
576 PKIX_ComCertSelParams *params,
577 PKIX_PL_Cert *cert,
578 PKIX_Boolean *pResult,
579 void *plContext)
581 PKIX_List *subjAltNamesList = NULL;
582 PKIX_List *certSubjAltNames = NULL;
583 PKIX_PL_GeneralName *name = NULL;
584 PKIX_Boolean checkPassed = PKIX_FALSE;
585 PKIX_Boolean matchAll = PKIX_TRUE;
586 PKIX_UInt32 i, numItems;
587 PKIX_UInt32 matchCount = 0;
589 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_Match_SubjAltNames");
590 PKIX_NULLCHECK_THREE(params, cert, pResult);
592 PKIX_CHECK(PKIX_ComCertSelParams_GetMatchAllSubjAltNames
593 (params, &matchAll, plContext),
594 PKIX_COMCERTSELPARAMSGETMATCHALLSUBJALTNAMESFAILED);
596 PKIX_CHECK(PKIX_ComCertSelParams_GetSubjAltNames
597 (params, &subjAltNamesList, plContext),
598 PKIX_COMCERTSELPARAMSGETSUBJALTNAMESFAILED);
600 if (subjAltNamesList != NULL) {
602 PKIX_CHECK(PKIX_PL_Cert_GetSubjectAltNames
603 (cert, &certSubjAltNames, plContext),
604 PKIX_CERTGETSUBJALTNAMESFAILED);
606 if (certSubjAltNames != NULL) {
608 PKIX_CHECK(PKIX_List_GetLength
609 (subjAltNamesList, &numItems, plContext),
610 PKIX_LISTGETLENGTHFAILED);
612 for (i = 0; i < numItems; i++) {
614 PKIX_CHECK(PKIX_List_GetItem
615 (subjAltNamesList,
617 (PKIX_PL_Object **) &name,
618 plContext),
619 PKIX_LISTGETITEMFAILED);
621 PKIX_CHECK(pkix_List_Contains
622 (certSubjAltNames,
623 (PKIX_PL_Object *) name,
624 &checkPassed,
625 plContext),
626 PKIX_LISTCONTAINSFAILED);
628 PKIX_DECREF(name);
630 if (checkPassed == PKIX_TRUE) {
632 if (matchAll == PKIX_FALSE) {
633 /* one match is good enough */
634 matchCount = numItems;
635 break;
636 } else {
637 /* else continue checking next */
638 matchCount++;
645 if (matchCount != numItems) {
646 PKIX_CERTSELECTOR_DEBUG("SubjAltName Match failed\n");
647 *pResult = PKIX_FALSE;
648 goto cleanup;
651 } else {
653 PKIX_CERTSELECTOR_DEBUG
654 ("SubjAltName Match failed: Cert has no SubjAltName\n");
655 *pResult = PKIX_FALSE;
656 goto cleanup;
661 cleanup:
663 PKIX_DECREF(name);
664 PKIX_DECREF(certSubjAltNames);
665 PKIX_DECREF(subjAltNamesList);
667 PKIX_RETURN(CERTSELECTOR);
671 * FUNCTION: pkix_CertSelector_Match_ExtendedKeyUsage
672 * DESCRIPTION:
674 * Determines whether the names at ExtKeyUsage in "params" matches with the
675 * ExtKeyUsage pointed to by "cert". If the ExtKeyUsage criterion or
676 * ExtKeyUsage in "cert" is NULL, no checking is done and the Cert is
677 * considered a match. If the Cert does not match, an Error pointer is
678 * returned.
680 * PARAMETERS:
681 * "params"
682 * Address of ComCertSelParams whose ExtKeyUsage field is used.
683 * Must be non-NULL.
684 * "cert"
685 * Address of Cert that is to be matched. Must be non-NULL.
686 * "pResult"
687 * Address of PKIX_Boolean that returns the match result.
688 * "plContext"
689 * Platform-specific context pointer.
690 * THREAD SAFETY:
691 * Conditionally Thread Safe
692 * (see Thread Safety Definitions in Programmer's Guide)
693 * RETURNS:
694 * Returns NULL if the function succeeds.
695 * Returns a CertSelector Error if the function fails in a non-fatal way.
696 * Returns a Fatal Error if the function fails in an unrecoverable way.
698 static PKIX_Error *
699 pkix_CertSelector_Match_ExtendedKeyUsage(
700 PKIX_ComCertSelParams *params,
701 PKIX_PL_Cert *cert,
702 PKIX_Boolean *pResult,
703 void *plContext)
705 PKIX_List *extKeyUsageList = NULL;
706 PKIX_List *certExtKeyUsageList = NULL;
707 PKIX_PL_OID *ekuOid = NULL;
708 PKIX_Boolean isContained = PKIX_FALSE;
709 PKIX_UInt32 numItems = 0;
710 PKIX_UInt32 i;
712 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_Match_ExtendedKeyUsage");
713 PKIX_NULLCHECK_THREE(params, cert, pResult);
715 PKIX_CHECK(PKIX_ComCertSelParams_GetExtendedKeyUsage
716 (params, &extKeyUsageList, plContext),
717 PKIX_COMCERTSELPARAMSGETEXTENDEDKEYUSAGEFAILED);
719 if (extKeyUsageList == NULL) {
720 goto cleanup;
723 PKIX_CHECK(PKIX_PL_Cert_GetExtendedKeyUsage
724 (cert, &certExtKeyUsageList, plContext),
725 PKIX_CERTGETEXTENDEDKEYUSAGEFAILED);
727 if (certExtKeyUsageList != NULL) {
729 PKIX_CHECK(PKIX_List_GetLength
730 (extKeyUsageList, &numItems, plContext),
731 PKIX_LISTGETLENGTHFAILED);
733 for (i = 0; i < numItems; i++) {
735 PKIX_CHECK(PKIX_List_GetItem
736 (extKeyUsageList, i, (PKIX_PL_Object **)&ekuOid, plContext),
737 PKIX_LISTGETITEMFAILED);
739 PKIX_CHECK(pkix_List_Contains
740 (certExtKeyUsageList,
741 (PKIX_PL_Object *)ekuOid,
742 &isContained,
743 plContext),
744 PKIX_LISTCONTAINSFAILED);
746 PKIX_DECREF(ekuOid);
748 if (isContained != PKIX_TRUE) {
749 PKIX_CERTSELECTOR_DEBUG
750 ("Extended Key Usage Match failed\n");
751 *pResult = PKIX_FALSE;
752 goto cleanup;
757 cleanup:
759 PKIX_DECREF(ekuOid);
760 PKIX_DECREF(extKeyUsageList);
761 PKIX_DECREF(certExtKeyUsageList);
763 PKIX_RETURN(CERTSELECTOR);
767 * FUNCTION: pkix_CertSelector_Match_KeyUsage
768 * DESCRIPTION:
770 * Determines whether the bits at KeyUsage in "params" matches with the
771 * KeyUsage pointed to by "cert". If the KeyUsage in params is 0
772 * no checking is done and the Cert is considered a match. If the Cert does
773 * not match, an Error pointer is returned.
775 * PARAMETERS:
776 * "params"
777 * Address of ComCertSelParams whose ExtKeyUsage field is used.
778 * Must be non-NULL.
779 * "cert"
780 * Address of Cert that is to be matched. Must be non-NULL.
781 * "pResult"
782 * Address of PKIX_Boolean that returns the match result.
783 * "plContext"
784 * Platform-specific context pointer.
785 * THREAD SAFETY:
786 * Conditionally Thread Safe
787 * (see Thread Safety Definitions in Programmer's Guide)
788 * RETURNS:
789 * Returns NULL if the function succeeds.
790 * Returns a CertSelector Error if the function fails in a non-fatal way.
791 * Returns a Fatal Error if the function fails in an unrecoverable way.
793 static PKIX_Error *
794 pkix_CertSelector_Match_KeyUsage(
795 PKIX_ComCertSelParams *params,
796 PKIX_PL_Cert *cert,
797 PKIX_Boolean *pResult,
798 void *plContext)
800 PKIX_UInt32 keyUsage = 0;
802 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_Match_KeyUsage");
803 PKIX_NULLCHECK_THREE(params, cert, pResult);
805 PKIX_CHECK(PKIX_ComCertSelParams_GetKeyUsage
806 (params, &keyUsage, plContext),
807 PKIX_COMCERTSELPARAMSGETKEYUSAGEFAILED);
809 if (keyUsage != 0) {
811 PKIX_CHECK(PKIX_PL_Cert_VerifyKeyUsage
812 (cert, keyUsage, plContext),
813 PKIX_CERTVERIFYKEYUSAGEFAILED);
817 cleanup:
819 PKIX_RETURN(CERTSELECTOR);
823 * FUNCTION: pkix_CertSelector_Match_SubjKeyId
824 * DESCRIPTION:
826 * Determines whether the bytes at subjKeyId in "params" matches with the
827 * Subject Key Identifier pointed to by "cert". If the subjKeyId in params is
828 * set to NULL, no checking is done and the Cert is considered a match. If
829 * the Cert does not match, an Error pointer is returned.
831 * PARAMETERS:
832 * "params"
833 * Address of ComCertSelParams whose subjKeyId field is used.
834 * Must be non-NULL.
835 * "cert"
836 * Address of Cert that is to be matched. Must be non-NULL.
837 * "pResult"
838 * Address of PKIX_Boolean that returns the match result.
839 * "plContext"
840 * Platform-specific context pointer.
841 * THREAD SAFETY:
842 * Conditionally Thread Safe
843 * (see Thread Safety Definitions in Programmer's Guide)
844 * RETURNS:
845 * Returns NULL if the function succeeds.
846 * Returns a CertSelector Error if the function fails in a non-fatal way.
847 * Returns a Fatal Error if the function fails in an unrecoverable way.
849 static PKIX_Error *
850 pkix_CertSelector_Match_SubjKeyId(
851 PKIX_ComCertSelParams *params,
852 PKIX_PL_Cert *cert,
853 PKIX_Boolean *pResult,
854 void *plContext)
856 PKIX_PL_ByteArray *selSubjKeyId = NULL;
857 PKIX_PL_ByteArray *certSubjKeyId = NULL;
858 PKIX_Boolean equals = PKIX_FALSE;
860 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_Match_SubjKeyId");
861 PKIX_NULLCHECK_THREE(params, cert, pResult);
863 PKIX_CHECK(PKIX_ComCertSelParams_GetSubjKeyIdentifier
864 (params, &selSubjKeyId, plContext),
865 PKIX_COMCERTSELPARAMSGETSUBJKEYIDENTIFIERFAILED);
867 if (selSubjKeyId != NULL) {
869 PKIX_CHECK(PKIX_PL_Cert_GetSubjectKeyIdentifier
870 (cert, &certSubjKeyId, plContext),
871 PKIX_CERTGETSUBJECTKEYIDENTIFIERFAILED);
873 if (certSubjKeyId != NULL) {
874 PKIX_CHECK(PKIX_PL_Object_Equals
875 ((PKIX_PL_Object *)selSubjKeyId,
876 (PKIX_PL_Object *)certSubjKeyId,
877 &equals,
878 plContext),
879 PKIX_OBJECTEQUALSFAILED);
881 if (equals != PKIX_TRUE) {
882 PKIX_CERTSELECTOR_DEBUG("SubjKeyId Match failed\n");
883 *pResult = PKIX_FALSE;
884 goto cleanup;
886 } else {
887 PKIX_CERTSELECTOR_DEBUG
888 ("SubjKeyId Match failed: Cert has no SubjKeyId\n");
889 *pResult = PKIX_FALSE;
890 goto cleanup;
894 cleanup:
896 PKIX_DECREF(selSubjKeyId);
897 PKIX_DECREF(certSubjKeyId);
899 PKIX_RETURN(CERTSELECTOR);
903 * FUNCTION: pkix_CertSelector_Match_AuthKeyId
904 * DESCRIPTION:
906 * Determines whether the bytes at authKeyId in "params" matches with the
907 * Authority Key Identifier pointed to by "cert". If the authKeyId in params
908 * is set to NULL, no checking is done and the Cert is considered a match. If
909 * the Cert does not match, an Error pointer is returned.
911 * PARAMETERS:
912 * "params"
913 * Address of ComCertSelParams whose authKeyId field is used.
914 * Must be non-NULL.
915 * "cert"
916 * Address of Cert that is to be matched. Must be non-NULL.
917 * "pResult"
918 * Address of PKIX_Boolean that returns the match result.
919 * "plContext"
920 * Platform-specific context pointer.
921 * THREAD SAFETY:
922 * Conditionally Thread Safe
923 * (see Thread Safety Definitions in Programmer's Guide)
924 * RETURNS:
925 * Returns NULL if the function succeeds.
926 * Returns a CertSelector Error if the function fails in a non-fatal way.
927 * Returns a Fatal Error if the function fails in an unrecoverable way.
929 static PKIX_Error *
930 pkix_CertSelector_Match_AuthKeyId(
931 PKIX_ComCertSelParams *params,
932 PKIX_PL_Cert *cert,
933 PKIX_Boolean *pResult,
934 void *plContext)
936 PKIX_PL_ByteArray *selAuthKeyId = NULL;
937 PKIX_PL_ByteArray *certAuthKeyId = NULL;
938 PKIX_Boolean equals = PKIX_FALSE;
940 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_Match_AuthKeyId");
941 PKIX_NULLCHECK_THREE(params, cert, pResult);
943 PKIX_CHECK(PKIX_ComCertSelParams_GetAuthorityKeyIdentifier
944 (params, &selAuthKeyId, plContext),
945 PKIX_COMCERTSELPARAMSGETAUTHORITYKEYIDENTIFIERFAILED);
947 if (selAuthKeyId != NULL) {
949 PKIX_CHECK(PKIX_PL_Cert_GetAuthorityKeyIdentifier
950 (cert, &certAuthKeyId, plContext),
951 PKIX_CERTGETAUTHORITYKEYIDENTIFIERFAILED);
953 if (certAuthKeyId != NULL) {
954 PKIX_CHECK(PKIX_PL_Object_Equals
955 ((PKIX_PL_Object *)selAuthKeyId,
956 (PKIX_PL_Object *)certAuthKeyId,
957 &equals,
958 plContext),
959 PKIX_OBJECTEQUALSFAILED);
961 if (equals != PKIX_TRUE) {
962 PKIX_CERTSELECTOR_DEBUG("AuthKeyId Match failed\n");
963 *pResult = PKIX_FALSE;
964 goto cleanup;
966 } else {
967 PKIX_CERTSELECTOR_DEBUG
968 ("AuthKeyId Match failed: Cert has no AuthKeyId\n");
969 *pResult = PKIX_FALSE;
970 goto cleanup;
974 cleanup:
976 PKIX_DECREF(selAuthKeyId);
977 PKIX_DECREF(certAuthKeyId);
979 PKIX_RETURN(CERTSELECTOR);
983 * FUNCTION: pkix_CertSelector_Match_SubjPKAlgId
984 * DESCRIPTION:
986 * Determines whether the OID at subjPKAlgId in "params" matches with the
987 * Subject Public Key Alg Id pointed to by "cert". If the subjPKAlgId in params
988 * is set to NULL, no checking is done and the Cert is considered a match. If
989 * the Cert does not match, an Error pointer is returned.
991 * PARAMETERS:
992 * "params"
993 * Address of ComCertSelParams whose subjPKAlgId field is used.
994 * Must be non-NULL.
995 * "cert"
996 * Address of Cert that is to be matched. Must be non-NULL.
997 * "pResult"
998 * Address of PKIX_Boolean that returns the match result.
999 * "plContext"
1000 * Platform-specific context pointer.
1001 * THREAD SAFETY:
1002 * Conditionally Thread Safe
1003 * (see Thread Safety Definitions in Programmer's Guide)
1004 * RETURNS:
1005 * Returns NULL if the function succeeds.
1006 * Returns a CertSelector Error if the function fails in a non-fatal way.
1007 * Returns a Fatal Error if the function fails in an unrecoverable way.
1009 static PKIX_Error *
1010 pkix_CertSelector_Match_SubjPKAlgId(
1011 PKIX_ComCertSelParams *params,
1012 PKIX_PL_Cert *cert,
1013 PKIX_Boolean *pResult,
1014 void *plContext)
1016 PKIX_PL_OID *selPKAlgId = NULL;
1017 PKIX_PL_OID *certPKAlgId = NULL;
1018 PKIX_Boolean equals = PKIX_FALSE;
1020 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_Match_SubjPKAlgId");
1021 PKIX_NULLCHECK_THREE(params, cert, pResult);
1023 PKIX_CHECK(PKIX_ComCertSelParams_GetSubjPKAlgId
1024 (params, &selPKAlgId, plContext),
1025 PKIX_COMCERTSELPARAMSGETSUBJPKALGIDFAILED);
1027 if (selPKAlgId != NULL) {
1029 PKIX_CHECK(PKIX_PL_Cert_GetSubjectPublicKeyAlgId
1030 (cert, &certPKAlgId, plContext),
1031 PKIX_CERTGETSUBJECTPUBLICKEYALGIDFAILED);
1033 if (certPKAlgId != NULL) {
1034 PKIX_CHECK(PKIX_PL_Object_Equals
1035 ((PKIX_PL_Object *)selPKAlgId,
1036 (PKIX_PL_Object *)certPKAlgId,
1037 &equals,
1038 plContext),
1039 PKIX_OBJECTEQUALSFAILED);
1041 if (equals != PKIX_TRUE) {
1042 PKIX_CERTSELECTOR_DEBUG
1043 ("SubjPKAlgId Match failed\n");
1044 *pResult = PKIX_FALSE;
1045 goto cleanup;
1047 } else {
1048 PKIX_CERTSELECTOR_DEBUG
1049 ("SubjPKAlgId Match failed: Cert has no SubjPKAlgId\n");
1050 *pResult = PKIX_FALSE;
1051 goto cleanup;
1055 cleanup:
1057 PKIX_DECREF(selPKAlgId);
1058 PKIX_DECREF(certPKAlgId);
1060 PKIX_RETURN(CERTSELECTOR);
1064 * FUNCTION: pkix_CertSelector_Match_SubjPubKey
1065 * DESCRIPTION:
1067 * Determines whether the key at subjPubKey in "params" matches with the
1068 * Subject Public Key pointed to by "cert". If the subjPubKey in params
1069 * is set to NULL, no checking is done and the Cert is considered a match. If
1070 * the Cert does not match, an Error pointer is returned.
1072 * PARAMETERS:
1073 * "params"
1074 * Address of ComCertSelParams whose subPubKey field is used.
1075 * Must be non-NULL.
1076 * "cert"
1077 * Address of Cert that is to be matched. Must be non-NULL.
1078 * "pResult"
1079 * Address of PKIX_Boolean that returns the match result.
1080 * "plContext"
1081 * Platform-specific context pointer.
1082 * THREAD SAFETY:
1083 * Conditionally Thread Safe
1084 * (see Thread Safety Definitions in Programmer's Guide)
1085 * RETURNS:
1086 * Returns NULL if the function succeeds.
1087 * Returns a CertSelector Error if the function fails in a non-fatal way.
1088 * Returns a Fatal Error if the function fails in an unrecoverable way.
1090 static PKIX_Error *
1091 pkix_CertSelector_Match_SubjPubKey(
1092 PKIX_ComCertSelParams *params,
1093 PKIX_PL_Cert *cert,
1094 PKIX_Boolean *pResult,
1095 void *plContext)
1097 PKIX_PL_PublicKey *selPK = NULL;
1098 PKIX_PL_PublicKey *certPK = NULL;
1099 PKIX_Boolean equals = PKIX_FALSE;
1101 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_Match_SubjPubKey");
1102 PKIX_NULLCHECK_THREE(params, cert, pResult);
1104 PKIX_CHECK(PKIX_ComCertSelParams_GetSubjPubKey
1105 (params, &selPK, plContext),
1106 PKIX_COMCERTSELPARAMSGETSUBJPUBKEYFAILED);
1108 if (selPK != NULL) {
1110 PKIX_CHECK(PKIX_PL_Cert_GetSubjectPublicKey
1111 (cert, &certPK, plContext),
1112 PKIX_CERTGETSUBJECTPUBLICKEYFAILED);
1114 if (certPK != NULL) {
1115 PKIX_CHECK(PKIX_PL_Object_Equals
1116 ((PKIX_PL_Object *)selPK,
1117 (PKIX_PL_Object *)certPK,
1118 &equals,
1119 plContext),
1120 PKIX_OBJECTEQUALSFAILED);
1122 if (equals != PKIX_TRUE) {
1123 PKIX_CERTSELECTOR_DEBUG
1124 ("Subject Public Key Match failed\n");
1125 *pResult = PKIX_FALSE;
1126 goto cleanup;
1129 } else {
1130 PKIX_CERTSELECTOR_DEBUG
1131 ("SubjPubKey Match failed: Cert has no SubjPubKey\n");
1132 *pResult = PKIX_FALSE;
1133 goto cleanup;
1137 cleanup:
1139 PKIX_DECREF(selPK);
1140 PKIX_DECREF(certPK);
1142 PKIX_RETURN(CERTSELECTOR);
1146 * FUNCTION: pkix_CertSelector_DefaultMatch
1147 * DESCRIPTION:
1149 * This default match function determines whether the specified Cert pointed
1150 * to by "cert" matches the criteria of the CertSelector pointed to by
1151 * "selector". If the Cert satisfies the CertSelector's criteria, PKIX_TRUE
1152 * is stored at "pResult". If the Cert does not match the CertSelector's
1153 * criteria, PKIX_FALSE is stored at "pResult".
1155 * This default match function understands how to process the most common
1156 * parameters. Any common parameter that is not set is assumed to be disabled,
1157 * which means this function will select all certificates without regard to
1158 * that particular disabled parameter. For example, if the SerialNumber
1159 * parameter is not set, this function will not filter out any certificate
1160 * based on its serial number. As such, if no parameters are set, all are
1161 * disabled and any certificate will match. If a parameter is disabled, its
1162 * associated PKIX_ComCertSelParams_Get* function returns a default value.
1163 * That value is -1 for PKIX_ComCertSelParams_GetBasicConstraints and
1164 * PKIX_ComCertSelParams_GetVersion, 0 for PKIX_ComCertSelParams_GetKeyUsage,
1165 * and NULL for all other Get functions.
1167 * PARAMETERS:
1168 * "selector"
1169 * Address of CertSelector whose MatchCallback logic and parameters are
1170 * to be used. Must be non-NULL.
1171 * "cert"
1172 * Address of Cert that is to be matched using "selector".
1173 * Must be non-NULL.
1174 * "pResult"
1175 * Address of PKIX_Boolean that returns the match result.
1176 * "plContext"
1177 * Platform-specific context pointer.
1178 * THREAD SAFETY:
1179 * Conditionally Thread Safe
1180 * (see Thread Safety Definitions in Programmer's Guide)
1181 * RETURNS:
1182 * Returns NULL if the function succeeds.
1183 * Returns a CertSelector Error if the function fails in a non-fatal way.
1184 * Returns a Fatal Error if the function fails in an unrecoverable way.
1186 static PKIX_Error *
1187 pkix_CertSelector_DefaultMatch(
1188 PKIX_CertSelector *selector,
1189 PKIX_PL_Cert *cert,
1190 PKIX_Boolean *pResult,
1191 void *plContext)
1193 PKIX_ComCertSelParams *params = NULL;
1194 PKIX_PL_X500Name *certSubject = NULL;
1195 PKIX_PL_X500Name *selSubject = NULL;
1196 PKIX_PL_X500Name *certIssuer = NULL;
1197 PKIX_PL_X500Name *selIssuer = NULL;
1198 PKIX_PL_BigInt *certSerialNumber = NULL;
1199 PKIX_PL_BigInt *selSerialNumber = NULL;
1200 PKIX_PL_Cert *selCert = NULL;
1201 PKIX_PL_Date *selDate = NULL;
1202 PKIX_UInt32 requiredKeyUsage = 0;
1203 PKIX_UInt32 selVersion = 0xFFFFFFFF;
1204 PKIX_UInt32 certVersion = 0;
1205 PKIX_Boolean result = PKIX_TRUE;
1207 #ifdef PKIX_BUILDDEBUG
1208 PKIX_PL_String *certString = NULL;
1209 void *certAscii = NULL;
1210 PKIX_UInt32 certAsciiLen;
1211 #endif
1213 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_DefaultMatch");
1214 PKIX_NULLCHECK_THREE(selector, cert, pResult);
1216 *pResult = PKIX_TRUE;
1217 params = selector->params;
1218 PKIX_INCREF(params);
1220 if (params == NULL){
1221 goto cleanup;
1224 PKIX_CHECK(PKIX_ComCertSelParams_GetVersion
1225 (params, &selVersion, plContext),
1226 PKIX_COMCERTSELPARAMSGETVERSIONFAILED);
1228 if (selVersion != 0xFFFFFFFF){
1229 PKIX_CHECK(PKIX_PL_Cert_GetVersion
1230 (cert, &certVersion, plContext),
1231 PKIX_CERTGETVERSIONFAILED);
1233 if (selVersion != certVersion) {
1234 PKIX_CERTSELECTOR_DEBUG("Version Match FAILED\n");
1235 *pResult = PKIX_FALSE;
1236 goto cleanup;
1240 PKIX_CHECK(PKIX_ComCertSelParams_GetSubject
1241 (params, &selSubject, plContext),
1242 PKIX_COMCERTSELPARAMSGETSUBJECTFAILED);
1244 if (selSubject){
1245 PKIX_CHECK(PKIX_PL_Cert_GetSubject
1246 (cert, &certSubject, plContext),
1247 PKIX_CERTGETSUBJECTFAILED);
1249 if (certSubject){
1250 PKIX_CHECK(PKIX_PL_X500Name_Match
1251 (selSubject, certSubject, &result, plContext),
1252 PKIX_X500NAMEMATCHFAILED);
1254 if (result == PKIX_FALSE){
1255 PKIX_CERTSELECTOR_DEBUG
1256 ("Subject Match FAILED\n");
1257 *pResult = PKIX_FALSE;
1258 goto cleanup;
1260 } else { /* cert has no subject */
1261 PKIX_CERTSELECTOR_DEBUG("Subject Match FAILED\n");
1262 *pResult = PKIX_FALSE;
1263 goto cleanup;
1268 PKIX_CHECK(PKIX_ComCertSelParams_GetIssuer
1269 (params, &selIssuer, plContext),
1270 PKIX_COMCERTSELPARAMSGETISSUERFAILED);
1272 if (selIssuer){
1273 PKIX_CHECK(PKIX_PL_Cert_GetIssuer
1274 (cert, &certIssuer, plContext),
1275 PKIX_CERTGETISSUERFAILED);
1277 PKIX_CHECK(PKIX_PL_X500Name_Match
1278 (selIssuer, certIssuer, &result, plContext),
1279 PKIX_X500NAMEMATCHFAILED);
1281 if (result == PKIX_FALSE){
1282 PKIX_CERTSELECTOR_DEBUG("Issuer Match FAILED\n");
1283 *pResult = PKIX_FALSE;
1284 goto cleanup;
1288 PKIX_CHECK(PKIX_ComCertSelParams_GetSerialNumber
1289 (params, &selSerialNumber, plContext),
1290 PKIX_COMCERTSELPARAMSGETSERIALNUMBERFAILED);
1292 if (selSerialNumber){
1293 PKIX_CHECK(PKIX_PL_Cert_GetSerialNumber
1294 (cert, &certSerialNumber, plContext),
1295 PKIX_CERTGETSERIALNUMBERFAILED);
1297 PKIX_CHECK(PKIX_PL_Object_Equals
1298 ((PKIX_PL_Object *)selSerialNumber,
1299 (PKIX_PL_Object *)certSerialNumber,
1300 &result,
1301 plContext),
1302 PKIX_OBJECTEQUALSFAILED);
1304 if (result == PKIX_FALSE){
1305 PKIX_CERTSELECTOR_DEBUG("Serial Number Match FAILED\n");
1306 *pResult = PKIX_FALSE;
1307 goto cleanup;
1311 PKIX_CHECK(PKIX_ComCertSelParams_GetCertificate
1312 (params, &selCert, plContext),
1313 PKIX_COMCERTSELPARAMSGETCERTIFICATEFAILED);
1315 if (selCert){
1316 PKIX_CHECK(PKIX_PL_Object_Equals
1317 ((PKIX_PL_Object *) selCert,
1318 (PKIX_PL_Object *) cert,
1319 &result,
1320 plContext),
1321 PKIX_OBJECTEQUALSFAILED);
1323 if (result == PKIX_FALSE){
1324 PKIX_CERTSELECTOR_DEBUG("Certificate Match FAILED\n");
1325 *pResult = PKIX_FALSE;
1326 goto cleanup;
1331 PKIX_CHECK(PKIX_ComCertSelParams_GetCertificateValid
1332 (params, &selDate, plContext),
1333 PKIX_COMCERTSELPARAMSGETCERTIFICATEVALIDFAILED);
1335 if (selDate){
1336 PKIX_CHECK(PKIX_PL_Cert_CheckValidity
1337 (cert, selDate, plContext),
1338 PKIX_CERTCHECKVALIDITYFAILED);
1341 PKIX_CHECK(PKIX_ComCertSelParams_GetKeyUsage
1342 (params, &requiredKeyUsage, plContext),
1343 PKIX_COMCERTSELPARAMSGETKEYUSAGEFAILED);
1345 if (requiredKeyUsage != 0) {
1346 PKIX_CHECK(PKIX_PL_Cert_VerifyKeyUsage
1347 (cert, requiredKeyUsage, plContext),
1348 PKIX_CERTVERIFYKEYUSAGEFAILED);
1351 PKIX_CHECK(pkix_CertSelector_Match_BasicConstraint
1352 (params, cert, &result, plContext),
1353 PKIX_CERTSELECTORMATCHBASICCONSTRAINTFAILED);
1355 if (result == PKIX_FALSE){
1356 PKIX_CERTSELECTOR_DEBUG("BasicConstraint Match FAILED\n");
1357 *pResult = PKIX_FALSE;
1358 goto cleanup;
1361 PKIX_CHECK(pkix_CertSelector_Match_Policies
1362 (params, cert, &result, plContext),
1363 PKIX_CERTSELECTORMATCHPOLICIESFAILED);
1365 if (result == PKIX_FALSE){
1366 PKIX_CERTSELECTOR_DEBUG("Policies Match FAILED\n");
1367 *pResult = PKIX_FALSE;
1368 goto cleanup;
1371 PKIX_CHECK(pkix_CertSelector_Match_CertificateValid
1372 (params, cert, &result, plContext),
1373 PKIX_CERTSELECTORMATCHCERTIFICATEVALIDFAILED);
1375 if (result == PKIX_FALSE){
1376 PKIX_CERTSELECTOR_DEBUG("CertificateValid Match FAILED\n");
1377 *pResult = PKIX_FALSE;
1378 goto cleanup;
1381 PKIX_CHECK(pkix_CertSelector_Match_NameConstraints
1382 (params, cert, &result, plContext),
1383 PKIX_CERTSELECTORMATCHNAMECONSTRAINTSFAILED);
1385 if (result == PKIX_FALSE){
1386 PKIX_CERTSELECTOR_DEBUG("NameConstraints Match FAILED\n");
1387 *pResult = PKIX_FALSE;
1388 goto cleanup;
1391 PKIX_CHECK(pkix_CertSelector_Match_PathToNames
1392 (params, cert, &result, plContext),
1393 PKIX_CERTSELECTORMATCHPATHTONAMESFAILED);
1395 if (result == PKIX_FALSE){
1396 PKIX_CERTSELECTOR_DEBUG("PathToNames Match FAILED\n");
1397 *pResult = PKIX_FALSE;
1398 goto cleanup;
1401 PKIX_CHECK(pkix_CertSelector_Match_SubjAltNames
1402 (params, cert, &result, plContext),
1403 PKIX_CERTSELECTORMATCHSUBJALTNAMESFAILED);
1405 if (result == PKIX_FALSE){
1406 PKIX_CERTSELECTOR_DEBUG("SubjAltNames Match FAILED\n");
1407 *pResult = PKIX_FALSE;
1408 goto cleanup;
1411 PKIX_CHECK(pkix_CertSelector_Match_ExtendedKeyUsage
1412 (params, cert, &result, plContext),
1413 PKIX_CERTSELECTORMATCHEXTENDEDKEYUSAGEFAILED);
1415 if (result == PKIX_FALSE){
1416 PKIX_CERTSELECTOR_DEBUG("ExtendedKeyUsage Match FAILED\n");
1417 *pResult = PKIX_FALSE;
1418 goto cleanup;
1421 PKIX_CHECK(pkix_CertSelector_Match_KeyUsage
1422 (params, cert, &result, plContext),
1423 PKIX_CERTSELECTORMATCHKEYUSAGEFAILED);
1425 if (result == PKIX_FALSE){
1426 PKIX_CERTSELECTOR_DEBUG("KeyUsage Match FAILED\n");
1427 *pResult = PKIX_FALSE;
1428 goto cleanup;
1431 PKIX_CHECK(pkix_CertSelector_Match_SubjKeyId
1432 (params, cert, &result, plContext),
1433 PKIX_CERTSELECTORMATCHSUBJKEYIDFAILED);
1435 if (result == PKIX_FALSE){
1436 PKIX_CERTSELECTOR_DEBUG("SubjKeyId Match FAILED\n");
1437 *pResult = PKIX_FALSE;
1438 goto cleanup;
1441 PKIX_CHECK(pkix_CertSelector_Match_AuthKeyId
1442 (params, cert, &result, plContext),
1443 PKIX_CERTSELECTORMATCHAUTHKEYIDFAILED);
1445 if (result == PKIX_FALSE){
1446 PKIX_CERTSELECTOR_DEBUG("AuthKeyId Match FAILED\n");
1447 *pResult = PKIX_FALSE;
1448 goto cleanup;
1451 PKIX_CHECK(pkix_CertSelector_Match_SubjPKAlgId
1452 (params, cert, &result, plContext),
1453 PKIX_CERTSELECTORMATCHSUBJPKALGIDFAILED);
1455 if (result == PKIX_FALSE){
1456 PKIX_CERTSELECTOR_DEBUG("SubjPKAlgId Match FAILED\n");
1457 *pResult = PKIX_FALSE;
1458 goto cleanup;
1461 PKIX_CHECK(pkix_CertSelector_Match_SubjPubKey
1462 (params, cert, &result, plContext),
1463 PKIX_CERTSELECTORMATCHSUBJPUBKEYFAILED);
1465 if (result == PKIX_FALSE){
1466 PKIX_CERTSELECTOR_DEBUG("SubjPubKey Match FAILED\n");
1467 *pResult = PKIX_FALSE;
1468 goto cleanup;
1471 /* if we reach here, the cert has successfully matched criteria */
1474 #ifdef PKIX_BUILDDEBUG
1476 PKIX_CHECK(pkix_pl_Cert_ToString_Helper
1477 (cert, PKIX_TRUE, &certString, plContext),
1478 PKIX_CERTTOSTRINGHELPERFAILED);
1480 PKIX_CHECK(PKIX_PL_String_GetEncoded
1481 (certString,
1482 PKIX_ESCASCII,
1483 &certAscii,
1484 &certAsciiLen,
1485 plContext),
1486 PKIX_STRINGGETENCODEDFAILED);
1488 PKIX_CERTSELECTOR_DEBUG_ARG("Cert Selected:\n%s\n", certAscii);
1490 #endif
1492 cleanup:
1494 #ifdef PKIX_BUILDDEBUG
1495 PKIX_DECREF(certString);
1496 PKIX_FREE(certAscii);
1497 #endif
1499 PKIX_DECREF(certSubject);
1500 PKIX_DECREF(selSubject);
1501 PKIX_DECREF(certIssuer);
1502 PKIX_DECREF(selIssuer);
1503 PKIX_DECREF(certSerialNumber);
1504 PKIX_DECREF(selSerialNumber);
1505 PKIX_DECREF(selCert);
1506 PKIX_DECREF(selDate);
1507 PKIX_DECREF(params);
1508 PKIX_RETURN(CERTSELECTOR);
1512 * FUNCTION: pkix_CertSelector_RegisterSelf
1513 * DESCRIPTION:
1514 * Registers PKIX_CERTSELECTOR_TYPE and its related functions with
1515 * systemClasses[]
1516 * THREAD SAFETY:
1517 * Not Thread Safe - for performance and complexity reasons
1519 * Since this function is only called by PKIX_PL_Initialize, which should
1520 * only be called once, it is acceptable that this function is not
1521 * thread-safe.
1523 PKIX_Error *
1524 pkix_CertSelector_RegisterSelf(void *plContext)
1526 extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
1527 pkix_ClassTable_Entry entry;
1529 PKIX_ENTER(CERTSELECTOR, "pkix_CertSelector_RegisterSelf");
1531 entry.description = "CertSelector";
1532 entry.destructor = pkix_CertSelector_Destroy;
1533 entry.equalsFunction = NULL;
1534 entry.hashcodeFunction = NULL;
1535 entry.toStringFunction = NULL;
1536 entry.comparator = NULL;
1537 entry.duplicateFunction = pkix_CertSelector_Duplicate;
1539 systemClasses[PKIX_CERTSELECTOR_TYPE] = entry;
1541 PKIX_RETURN(CERTSELECTOR);
1544 /* --Public-Functions--------------------------------------------- */
1548 * FUNCTION: PKIX_CertSelector_Create (see comments in pkix_certsel.h)
1550 PKIX_Error *
1551 PKIX_CertSelector_Create(
1552 PKIX_CertSelector_MatchCallback callback,
1553 PKIX_PL_Object *certSelectorContext,
1554 PKIX_CertSelector **pSelector,
1555 void *plContext)
1557 PKIX_CertSelector *selector = NULL;
1559 PKIX_ENTER(CERTSELECTOR, "PKIX_CertSelector_Create");
1560 PKIX_NULLCHECK_ONE(pSelector);
1562 PKIX_CHECK(PKIX_PL_Object_Alloc
1563 (PKIX_CERTSELECTOR_TYPE,
1564 sizeof (PKIX_CertSelector),
1565 (PKIX_PL_Object **)&selector,
1566 plContext),
1567 PKIX_COULDNOTCREATECERTSELECTOROBJECT);
1570 * if user specified a particular match callback, we use that one.
1571 * otherwise, we use the default match implementation which
1572 * understands how to process PKIX_ComCertSelParams
1575 if (callback){
1576 selector->matchCallback = callback;
1577 } else {
1578 selector->matchCallback = pkix_CertSelector_DefaultMatch;
1581 /* initialize other fields */
1582 selector->params = NULL;
1584 PKIX_INCREF(certSelectorContext);
1585 selector->context = certSelectorContext;
1587 *pSelector = selector;
1589 cleanup:
1591 PKIX_RETURN(CERTSELECTOR);
1596 * FUNCTION: PKIX_CertSelector_GetMatchCallback
1597 * (see comments in pkix_certsel.h)
1599 PKIX_Error *
1600 PKIX_CertSelector_GetMatchCallback(
1601 PKIX_CertSelector *selector,
1602 PKIX_CertSelector_MatchCallback *pCallback,
1603 void *plContext)
1605 PKIX_ENTER(CERTSELECTOR, "PKIX_CertSelector_GetMatchCallback");
1606 PKIX_NULLCHECK_TWO(selector, pCallback);
1608 *pCallback = selector->matchCallback;
1610 PKIX_RETURN(CERTSELECTOR);
1614 * FUNCTION: PKIX_CertSelector_GetCertSelectorContext
1615 * (see comments in pkix_certsel.h)
1617 PKIX_Error *
1618 PKIX_CertSelector_GetCertSelectorContext(
1619 PKIX_CertSelector *selector,
1620 PKIX_PL_Object **pCertSelectorContext,
1621 void *plContext)
1623 PKIX_ENTER(CERTSELECTOR, "PKIX_CertSelector_GetCertSelectorContext");
1624 PKIX_NULLCHECK_TWO(selector, pCertSelectorContext);
1626 PKIX_INCREF(selector->context);
1628 *pCertSelectorContext = selector->context;
1630 PKIX_RETURN(CERTSELECTOR);
1634 * FUNCTION: PKIX_CertSelector_GetCommonCertSelectorParams
1635 * (see comments in pkix_certsel.h)
1637 PKIX_Error *
1638 PKIX_CertSelector_GetCommonCertSelectorParams(
1639 PKIX_CertSelector *selector,
1640 PKIX_ComCertSelParams **pParams,
1641 void *plContext)
1643 PKIX_ENTER(CERTSELECTOR,
1644 "PKIX_CertSelector_GetCommonCertSelectorParams");
1646 PKIX_NULLCHECK_TWO(selector, pParams);
1648 PKIX_INCREF(selector->params);
1649 *pParams = selector->params;
1651 PKIX_RETURN(CERTSELECTOR);
1656 * FUNCTION: PKIX_CertSelector_SetCommonCertSelectorParams
1657 * (see comments in pkix_certsel.h)
1659 PKIX_Error *
1660 PKIX_CertSelector_SetCommonCertSelectorParams(
1661 PKIX_CertSelector *selector,
1662 PKIX_ComCertSelParams *params,
1663 void *plContext)
1665 PKIX_ENTER(CERTSELECTOR,
1666 "PKIX_CertSelector_SetCommonCertSelectorParams");
1668 PKIX_NULLCHECK_ONE(selector);
1670 PKIX_DECREF(selector->params);
1671 PKIX_INCREF(params);
1672 selector->params = params;
1674 PKIX_CHECK(PKIX_PL_Object_InvalidateCache
1675 ((PKIX_PL_Object *)selector, plContext),
1676 PKIX_OBJECTINVALIDATECACHEFAILED);
1678 cleanup:
1680 PKIX_RETURN(CERTSELECTOR);
1685 * FUNCTION: pkix_CertSelector_Select
1686 * DESCRIPTION:
1688 * This function applies the selector pointed to by "selector" to each Cert,
1689 * in turn, in the List pointed to by "before", and creates a List containing
1690 * all the Certs that matched, or passed the selection process, storing that
1691 * List at "pAfter". If no Certs match, an empty List is stored at "pAfter".
1693 * The List returned in "pAfter" is immutable.
1695 * PARAMETERS:
1696 * "selector"
1697 * Address of CertSelelector to be applied to the List. Must be non-NULL.
1698 * "before"
1699 * Address of List that is to be filtered. Must be non-NULL.
1700 * "pAfter"
1701 * Address at which resulting List, possibly empty, is stored. Must be
1702 * non-NULL.
1703 * "plContext"
1704 * Platform-specific context pointer.
1705 * THREAD SAFETY:
1706 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
1707 * RETURNS:
1708 * Returns NULL if the function succeeds.
1709 * Returns a CertSelector Error if the function fails in a non-fatal way.
1710 * Returns a Fatal Error if the function fails in an unrecoverable way.
1712 PKIX_Error *
1713 pkix_CertSelector_Select(
1714 PKIX_CertSelector *selector,
1715 PKIX_List *before,
1716 PKIX_List **pAfter,
1717 void *plContext)
1719 PKIX_Boolean match = PKIX_FALSE;
1720 PKIX_UInt32 numBefore = 0;
1721 PKIX_UInt32 i = 0;
1722 PKIX_List *filtered = NULL;
1723 PKIX_PL_Cert *candidate = NULL;
1725 PKIX_ENTER(CERTSELECTOR, "PKIX_CertSelector_Select");
1726 PKIX_NULLCHECK_THREE(selector, before, pAfter);
1728 PKIX_CHECK(PKIX_List_Create(&filtered, plContext),
1729 PKIX_LISTCREATEFAILED);
1731 PKIX_CHECK(PKIX_List_GetLength(before, &numBefore, plContext),
1732 PKIX_LISTGETLENGTHFAILED);
1734 for (i = 0; i < numBefore; i++) {
1736 PKIX_CHECK(PKIX_List_GetItem
1737 (before, i, (PKIX_PL_Object **)&candidate, plContext),
1738 PKIX_LISTGETITEMFAILED);
1740 PKIX_CHECK_ONLY_FATAL(selector->matchCallback
1741 (selector, candidate, &match, plContext),
1742 PKIX_CERTSELECTORMATCHCALLBACKFAILED);
1744 if ((!(PKIX_ERROR_RECEIVED)) && (match == PKIX_TRUE)) {
1746 PKIX_CHECK_ONLY_FATAL(PKIX_List_AppendItem
1747 (filtered,
1748 (PKIX_PL_Object *)candidate,
1749 plContext),
1750 PKIX_LISTAPPENDITEMFAILED);
1753 pkixTempErrorReceived = PKIX_FALSE;
1754 PKIX_DECREF(candidate);
1757 PKIX_CHECK(PKIX_List_SetImmutable(filtered, plContext),
1758 PKIX_LISTSETIMMUTABLEFAILED);
1760 /* Don't throw away the list if one Cert was bad! */
1761 pkixTempErrorReceived = PKIX_FALSE;
1763 *pAfter = filtered;
1765 cleanup:
1767 PKIX_DECREF(candidate);
1769 PKIX_RETURN(CERTSELECTOR);