security/nss/lib/certhigh/certvfy.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 #include "nspr.h"
michael@0 5 #include "secerr.h"
michael@0 6 #include "secport.h"
michael@0 7 #include "seccomon.h"
michael@0 8 #include "secoid.h"
michael@0 9 #include "sslerr.h"
michael@0 10 #include "genname.h"
michael@0 11 #include "keyhi.h"
michael@0 12 #include "cert.h"
michael@0 13 #include "certdb.h"
michael@0 14 #include "certi.h"
michael@0 15 #include "cryptohi.h"
michael@0 16 #include "pkix.h"
michael@0 17 /*#include "pkix_sample_modules.h" */
michael@0 18 #include "pkix_pl_cert.h"
michael@0 19
michael@0 20
michael@0 21 #include "nsspki.h"
michael@0 22 #include "pkitm.h"
michael@0 23 #include "pkim.h"
michael@0 24 #include "pki3hack.h"
michael@0 25 #include "base.h"
michael@0 26
michael@0 27 /*
michael@0 28 * Check the validity times of a certificate
michael@0 29 */
michael@0 30 SECStatus
michael@0 31 CERT_CertTimesValid(CERTCertificate *c)
michael@0 32 {
michael@0 33 SECCertTimeValidity valid = CERT_CheckCertValidTimes(c, PR_Now(), PR_TRUE);
michael@0 34 return (valid == secCertTimeValid) ? SECSuccess : SECFailure;
michael@0 35 }
michael@0 36
michael@0 37 /*
michael@0 38 * verify the signature of a signed data object with the given DER publickey
michael@0 39 */
michael@0 40 SECStatus
michael@0 41 CERT_VerifySignedDataWithPublicKey(const CERTSignedData *sd,
michael@0 42 SECKEYPublicKey *pubKey,
michael@0 43 void *wincx)
michael@0 44 {
michael@0 45 SECStatus rv;
michael@0 46 SECItem sig;
michael@0 47 SECOidTag hashAlg = SEC_OID_UNKNOWN;
michael@0 48
michael@0 49 if ( !pubKey || !sd ) {
michael@0 50 PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
michael@0 51 return SECFailure;
michael@0 52 }
michael@0 53
michael@0 54 /* check the signature */
michael@0 55 sig = sd->signature;
michael@0 56 /* convert sig->len from bit counts to byte count. */
michael@0 57 DER_ConvertBitString(&sig);
michael@0 58
michael@0 59 rv = VFY_VerifyDataWithAlgorithmID(sd->data.data, sd->data.len, pubKey,
michael@0 60 &sig, &sd->signatureAlgorithm, &hashAlg, wincx);
michael@0 61 if (rv == SECSuccess) {
michael@0 62 /* Are we honoring signatures for this algorithm? */
michael@0 63 PRUint32 policyFlags = 0;
michael@0 64 rv = NSS_GetAlgorithmPolicy(hashAlg, &policyFlags);
michael@0 65 if (rv == SECSuccess &&
michael@0 66 !(policyFlags & NSS_USE_ALG_IN_CERT_SIGNATURE)) {
michael@0 67 PORT_SetError(SEC_ERROR_CERT_SIGNATURE_ALGORITHM_DISABLED);
michael@0 68 rv = SECFailure;
michael@0 69 }
michael@0 70 }
michael@0 71 return rv;
michael@0 72 }
michael@0 73
michael@0 74 /*
michael@0 75 * verify the signature of a signed data object with the given DER publickey
michael@0 76 */
michael@0 77 SECStatus
michael@0 78 CERT_VerifySignedDataWithPublicKeyInfo(CERTSignedData *sd,
michael@0 79 CERTSubjectPublicKeyInfo *pubKeyInfo,
michael@0 80 void *wincx)
michael@0 81 {
michael@0 82 SECKEYPublicKey *pubKey;
michael@0 83 SECStatus rv = SECFailure;
michael@0 84
michael@0 85 /* get cert's public key */
michael@0 86 pubKey = SECKEY_ExtractPublicKey(pubKeyInfo);
michael@0 87 if (pubKey) {
michael@0 88 rv = CERT_VerifySignedDataWithPublicKey(sd, pubKey, wincx);
michael@0 89 SECKEY_DestroyPublicKey(pubKey);
michael@0 90 }
michael@0 91 return rv;
michael@0 92 }
michael@0 93
michael@0 94 /*
michael@0 95 * verify the signature of a signed data object with the given certificate
michael@0 96 */
michael@0 97 SECStatus
michael@0 98 CERT_VerifySignedData(CERTSignedData *sd, CERTCertificate *cert,
michael@0 99 PRTime t, void *wincx)
michael@0 100 {
michael@0 101 SECKEYPublicKey *pubKey = 0;
michael@0 102 SECStatus rv = SECFailure;
michael@0 103 SECCertTimeValidity validity;
michael@0 104
michael@0 105 /* check the certificate's validity */
michael@0 106 validity = CERT_CheckCertValidTimes(cert, t, PR_FALSE);
michael@0 107 if ( validity != secCertTimeValid ) {
michael@0 108 return rv;
michael@0 109 }
michael@0 110
michael@0 111 /* get cert's public key */
michael@0 112 pubKey = CERT_ExtractPublicKey(cert);
michael@0 113 if (pubKey) {
michael@0 114 rv = CERT_VerifySignedDataWithPublicKey(sd, pubKey, wincx);
michael@0 115 SECKEY_DestroyPublicKey(pubKey);
michael@0 116 }
michael@0 117 return rv;
michael@0 118 }
michael@0 119
michael@0 120
michael@0 121 SECStatus
michael@0 122 SEC_CheckCRL(CERTCertDBHandle *handle,CERTCertificate *cert,
michael@0 123 CERTCertificate *caCert, PRTime t, void * wincx)
michael@0 124 {
michael@0 125 return CERT_CheckCRL(cert, caCert, NULL, t, wincx);
michael@0 126 }
michael@0 127
michael@0 128 /*
michael@0 129 * Find the issuer of a cert. Use the authorityKeyID if it exists.
michael@0 130 */
michael@0 131 CERTCertificate *
michael@0 132 CERT_FindCertIssuer(CERTCertificate *cert, PRTime validTime, SECCertUsage usage)
michael@0 133 {
michael@0 134 NSSCertificate *me;
michael@0 135 NSSTime *nssTime;
michael@0 136 NSSTrustDomain *td;
michael@0 137 NSSCryptoContext *cc;
michael@0 138 NSSCertificate *chain[3];
michael@0 139 NSSUsage nssUsage;
michael@0 140 PRStatus status;
michael@0 141
michael@0 142 me = STAN_GetNSSCertificate(cert);
michael@0 143 if (!me) {
michael@0 144 PORT_SetError(SEC_ERROR_NO_MEMORY);
michael@0 145 return NULL;
michael@0 146 }
michael@0 147 nssTime = NSSTime_SetPRTime(NULL, validTime);
michael@0 148 nssUsage.anyUsage = PR_FALSE;
michael@0 149 nssUsage.nss3usage = usage;
michael@0 150 nssUsage.nss3lookingForCA = PR_TRUE;
michael@0 151 memset(chain, 0, 3*sizeof(NSSCertificate *));
michael@0 152 td = STAN_GetDefaultTrustDomain();
michael@0 153 cc = STAN_GetDefaultCryptoContext();
michael@0 154 (void)NSSCertificate_BuildChain(me, nssTime, &nssUsage, NULL,
michael@0 155 chain, 2, NULL, &status, td, cc);
michael@0 156 nss_ZFreeIf(nssTime);
michael@0 157 if (status == PR_SUCCESS) {
michael@0 158 PORT_Assert(me == chain[0]);
michael@0 159 /* if it's a root, the chain will only have one cert */
michael@0 160 if (!chain[1]) {
michael@0 161 /* already has a reference from the call to BuildChain */
michael@0 162 return cert;
michael@0 163 }
michael@0 164 NSSCertificate_Destroy(chain[0]); /* the first cert in the chain */
michael@0 165 return STAN_GetCERTCertificate(chain[1]); /* return the 2nd */
michael@0 166 }
michael@0 167 if (chain[0]) {
michael@0 168 PORT_Assert(me == chain[0]);
michael@0 169 NSSCertificate_Destroy(chain[0]); /* the first cert in the chain */
michael@0 170 }
michael@0 171 PORT_SetError (SEC_ERROR_UNKNOWN_ISSUER);
michael@0 172 return NULL;
michael@0 173 }
michael@0 174
michael@0 175 /*
michael@0 176 * return required trust flags for various cert usages for CAs
michael@0 177 */
michael@0 178 SECStatus
michael@0 179 CERT_TrustFlagsForCACertUsage(SECCertUsage usage,
michael@0 180 unsigned int *retFlags,
michael@0 181 SECTrustType *retTrustType)
michael@0 182 {
michael@0 183 unsigned int requiredFlags;
michael@0 184 SECTrustType trustType;
michael@0 185
michael@0 186 switch ( usage ) {
michael@0 187 case certUsageSSLClient:
michael@0 188 requiredFlags = CERTDB_TRUSTED_CLIENT_CA;
michael@0 189 trustType = trustSSL;
michael@0 190 break;
michael@0 191 case certUsageSSLServer:
michael@0 192 case certUsageSSLCA:
michael@0 193 requiredFlags = CERTDB_TRUSTED_CA;
michael@0 194 trustType = trustSSL;
michael@0 195 break;
michael@0 196 case certUsageSSLServerWithStepUp:
michael@0 197 requiredFlags = CERTDB_TRUSTED_CA | CERTDB_GOVT_APPROVED_CA;
michael@0 198 trustType = trustSSL;
michael@0 199 break;
michael@0 200 case certUsageEmailSigner:
michael@0 201 case certUsageEmailRecipient:
michael@0 202 requiredFlags = CERTDB_TRUSTED_CA;
michael@0 203 trustType = trustEmail;
michael@0 204 break;
michael@0 205 case certUsageObjectSigner:
michael@0 206 requiredFlags = CERTDB_TRUSTED_CA;
michael@0 207 trustType = trustObjectSigning;
michael@0 208 break;
michael@0 209 case certUsageVerifyCA:
michael@0 210 case certUsageAnyCA:
michael@0 211 case certUsageStatusResponder:
michael@0 212 requiredFlags = CERTDB_TRUSTED_CA;
michael@0 213 trustType = trustTypeNone;
michael@0 214 break;
michael@0 215 default:
michael@0 216 PORT_Assert(0);
michael@0 217 goto loser;
michael@0 218 }
michael@0 219 if ( retFlags != NULL ) {
michael@0 220 *retFlags = requiredFlags;
michael@0 221 }
michael@0 222 if ( retTrustType != NULL ) {
michael@0 223 *retTrustType = trustType;
michael@0 224 }
michael@0 225
michael@0 226 return(SECSuccess);
michael@0 227 loser:
michael@0 228 return(SECFailure);
michael@0 229 }
michael@0 230
michael@0 231 void
michael@0 232 cert_AddToVerifyLog(CERTVerifyLog *log, CERTCertificate *cert, long error,
michael@0 233 unsigned int depth, void *arg)
michael@0 234 {
michael@0 235 CERTVerifyLogNode *node, *tnode;
michael@0 236
michael@0 237 PORT_Assert(log != NULL);
michael@0 238
michael@0 239 node = (CERTVerifyLogNode *)PORT_ArenaAlloc(log->arena,
michael@0 240 sizeof(CERTVerifyLogNode));
michael@0 241 if ( node != NULL ) {
michael@0 242 node->cert = CERT_DupCertificate(cert);
michael@0 243 node->error = error;
michael@0 244 node->depth = depth;
michael@0 245 node->arg = arg;
michael@0 246
michael@0 247 if ( log->tail == NULL ) {
michael@0 248 /* empty list */
michael@0 249 log->head = log->tail = node;
michael@0 250 node->prev = NULL;
michael@0 251 node->next = NULL;
michael@0 252 } else if ( depth >= log->tail->depth ) {
michael@0 253 /* add to tail */
michael@0 254 node->prev = log->tail;
michael@0 255 log->tail->next = node;
michael@0 256 log->tail = node;
michael@0 257 node->next = NULL;
michael@0 258 } else if ( depth < log->head->depth ) {
michael@0 259 /* add at head */
michael@0 260 node->prev = NULL;
michael@0 261 node->next = log->head;
michael@0 262 log->head->prev = node;
michael@0 263 log->head = node;
michael@0 264 } else {
michael@0 265 /* add in middle */
michael@0 266 tnode = log->tail;
michael@0 267 while ( tnode != NULL ) {
michael@0 268 if ( depth >= tnode->depth ) {
michael@0 269 /* insert after tnode */
michael@0 270 node->prev = tnode;
michael@0 271 node->next = tnode->next;
michael@0 272 tnode->next->prev = node;
michael@0 273 tnode->next = node;
michael@0 274 break;
michael@0 275 }
michael@0 276
michael@0 277 tnode = tnode->prev;
michael@0 278 }
michael@0 279 }
michael@0 280
michael@0 281 log->count++;
michael@0 282 }
michael@0 283 return;
michael@0 284 }
michael@0 285
michael@0 286 #define EXIT_IF_NOT_LOGGING(log) \
michael@0 287 if ( log == NULL ) { \
michael@0 288 goto loser; \
michael@0 289 }
michael@0 290
michael@0 291 #define LOG_ERROR_OR_EXIT(log,cert,depth,arg) \
michael@0 292 if ( log != NULL ) { \
michael@0 293 cert_AddToVerifyLog(log, cert, PORT_GetError(), depth, \
michael@0 294 (void *)(PRWord)arg); \
michael@0 295 } else { \
michael@0 296 goto loser; \
michael@0 297 }
michael@0 298
michael@0 299 #define LOG_ERROR(log,cert,depth,arg) \
michael@0 300 if ( log != NULL ) { \
michael@0 301 cert_AddToVerifyLog(log, cert, PORT_GetError(), depth, \
michael@0 302 (void *)(PRWord)arg); \
michael@0 303 }
michael@0 304
michael@0 305 static SECStatus
michael@0 306 cert_VerifyCertChainOld(CERTCertDBHandle *handle, CERTCertificate *cert,
michael@0 307 PRBool checkSig, PRBool* sigerror,
michael@0 308 SECCertUsage certUsage, PRTime t, void *wincx,
michael@0 309 CERTVerifyLog *log, PRBool* revoked)
michael@0 310 {
michael@0 311 SECTrustType trustType;
michael@0 312 CERTBasicConstraints basicConstraint;
michael@0 313 CERTCertificate *issuerCert = NULL;
michael@0 314 CERTCertificate *subjectCert = NULL;
michael@0 315 CERTCertificate *badCert = NULL;
michael@0 316 PRBool isca;
michael@0 317 SECStatus rv;
michael@0 318 SECStatus rvFinal = SECSuccess;
michael@0 319 int count;
michael@0 320 int currentPathLen = 0;
michael@0 321 int pathLengthLimit = CERT_UNLIMITED_PATH_CONSTRAINT;
michael@0 322 unsigned int caCertType;
michael@0 323 unsigned int requiredCAKeyUsage;
michael@0 324 unsigned int requiredFlags;
michael@0 325 PLArenaPool *arena = NULL;
michael@0 326 CERTGeneralName *namesList = NULL;
michael@0 327 CERTCertificate **certsList = NULL;
michael@0 328 int certsListLen = 16;
michael@0 329 int namesCount = 0;
michael@0 330 PRBool subjectCertIsSelfIssued;
michael@0 331 CERTCertTrust issuerTrust;
michael@0 332
michael@0 333 if (revoked) {
michael@0 334 *revoked = PR_FALSE;
michael@0 335 }
michael@0 336
michael@0 337 if (CERT_KeyUsageAndTypeForCertUsage(certUsage, PR_TRUE,
michael@0 338 &requiredCAKeyUsage,
michael@0 339 &caCertType)
michael@0 340 != SECSuccess ) {
michael@0 341 PORT_Assert(0);
michael@0 342 EXIT_IF_NOT_LOGGING(log);
michael@0 343 requiredCAKeyUsage = 0;
michael@0 344 caCertType = 0;
michael@0 345 }
michael@0 346
michael@0 347 switch ( certUsage ) {
michael@0 348 case certUsageSSLClient:
michael@0 349 case certUsageSSLServer:
michael@0 350 case certUsageSSLCA:
michael@0 351 case certUsageSSLServerWithStepUp:
michael@0 352 case certUsageEmailSigner:
michael@0 353 case certUsageEmailRecipient:
michael@0 354 case certUsageObjectSigner:
michael@0 355 case certUsageVerifyCA:
michael@0 356 case certUsageAnyCA:
michael@0 357 case certUsageStatusResponder:
michael@0 358 if ( CERT_TrustFlagsForCACertUsage(certUsage, &requiredFlags,
michael@0 359 &trustType) != SECSuccess ) {
michael@0 360 PORT_Assert(0);
michael@0 361 EXIT_IF_NOT_LOGGING(log);
michael@0 362 /* XXX continuing with requiredFlags = 0 seems wrong. It'll
michael@0 363 * cause the following test to be true incorrectly:
michael@0 364 * flags = SEC_GET_TRUST_FLAGS(issuerCert->trust, trustType);
michael@0 365 * if (( flags & requiredFlags ) == requiredFlags) {
michael@0 366 * rv = rvFinal;
michael@0 367 * goto done;
michael@0 368 * }
michael@0 369 * There are three other instances of this problem.
michael@0 370 */
michael@0 371 requiredFlags = 0;
michael@0 372 trustType = trustSSL;
michael@0 373 }
michael@0 374 break;
michael@0 375 default:
michael@0 376 PORT_Assert(0);
michael@0 377 EXIT_IF_NOT_LOGGING(log);
michael@0 378 requiredFlags = 0;
michael@0 379 trustType = trustSSL;/* This used to be 0, but we need something
michael@0 380 * that matches the enumeration type.
michael@0 381 */
michael@0 382 caCertType = 0;
michael@0 383 }
michael@0 384
michael@0 385 subjectCert = CERT_DupCertificate(cert);
michael@0 386 if ( subjectCert == NULL ) {
michael@0 387 goto loser;
michael@0 388 }
michael@0 389
michael@0 390 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
michael@0 391 if (arena == NULL) {
michael@0 392 goto loser;
michael@0 393 }
michael@0 394
michael@0 395 certsList = PORT_ZNewArray(CERTCertificate *, certsListLen);
michael@0 396 if (certsList == NULL)
michael@0 397 goto loser;
michael@0 398
michael@0 399 /* RFC 3280 says that the name constraints will apply to the names
michael@0 400 ** in the leaf (EE) cert, whether it is self issued or not, so
michael@0 401 ** we pretend that it is not.
michael@0 402 */
michael@0 403 subjectCertIsSelfIssued = PR_FALSE;
michael@0 404 for ( count = 0; count < CERT_MAX_CERT_CHAIN; count++ ) {
michael@0 405 PRBool validCAOverride = PR_FALSE;
michael@0 406
michael@0 407 /* Construct a list of names for the current and all previous
michael@0 408 * certifcates (except leaf (EE) certs, root CAs, and self-issued
michael@0 409 * intermediate CAs) to be verified against the name constraints
michael@0 410 * extension of the issuer certificate.
michael@0 411 */
michael@0 412 if (subjectCertIsSelfIssued == PR_FALSE) {
michael@0 413 CERTGeneralName *subjectNameList;
michael@0 414 int subjectNameListLen;
michael@0 415 int i;
michael@0 416 PRBool getSubjectCN = (!count && certUsage == certUsageSSLServer);
michael@0 417 subjectNameList =
michael@0 418 CERT_GetConstrainedCertificateNames(subjectCert, arena,
michael@0 419 getSubjectCN);
michael@0 420 if (!subjectNameList)
michael@0 421 goto loser;
michael@0 422 subjectNameListLen = CERT_GetNamesLength(subjectNameList);
michael@0 423 if (!subjectNameListLen)
michael@0 424 goto loser;
michael@0 425 if (certsListLen <= namesCount + subjectNameListLen) {
michael@0 426 CERTCertificate **tmpCertsList;
michael@0 427 certsListLen = (namesCount + subjectNameListLen) * 2;
michael@0 428 tmpCertsList =
michael@0 429 (CERTCertificate **)PORT_Realloc(certsList,
michael@0 430 certsListLen * sizeof(CERTCertificate *));
michael@0 431 if (tmpCertsList == NULL) {
michael@0 432 goto loser;
michael@0 433 }
michael@0 434 certsList = tmpCertsList;
michael@0 435 }
michael@0 436 for (i = 0; i < subjectNameListLen; i++) {
michael@0 437 certsList[namesCount + i] = subjectCert;
michael@0 438 }
michael@0 439 namesCount += subjectNameListLen;
michael@0 440 namesList = cert_CombineNamesLists(namesList, subjectNameList);
michael@0 441 }
michael@0 442
michael@0 443 /* check if the cert has an unsupported critical extension */
michael@0 444 if ( subjectCert->options.bits.hasUnsupportedCriticalExt ) {
michael@0 445 PORT_SetError(SEC_ERROR_UNKNOWN_CRITICAL_EXTENSION);
michael@0 446 LOG_ERROR_OR_EXIT(log,subjectCert,count,0);
michael@0 447 }
michael@0 448
michael@0 449 /* find the certificate of the issuer */
michael@0 450 issuerCert = CERT_FindCertIssuer(subjectCert, t, certUsage);
michael@0 451 if ( ! issuerCert ) {
michael@0 452 PORT_SetError(SEC_ERROR_UNKNOWN_ISSUER);
michael@0 453 LOG_ERROR(log,subjectCert,count,0);
michael@0 454 goto loser;
michael@0 455 }
michael@0 456
michael@0 457 /* verify the signature on the cert */
michael@0 458 if ( checkSig ) {
michael@0 459 rv = CERT_VerifySignedData(&subjectCert->signatureWrap,
michael@0 460 issuerCert, t, wincx);
michael@0 461
michael@0 462 if ( rv != SECSuccess ) {
michael@0 463 if (sigerror) {
michael@0 464 *sigerror = PR_TRUE;
michael@0 465 }
michael@0 466 if ( PORT_GetError() == SEC_ERROR_EXPIRED_CERTIFICATE ) {
michael@0 467 PORT_SetError(SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE);
michael@0 468 LOG_ERROR_OR_EXIT(log,issuerCert,count+1,0);
michael@0 469 } else {
michael@0 470 if (PORT_GetError() !=
michael@0 471 SEC_ERROR_CERT_SIGNATURE_ALGORITHM_DISABLED) {
michael@0 472 PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
michael@0 473 }
michael@0 474 LOG_ERROR_OR_EXIT(log,subjectCert,count,0);
michael@0 475 }
michael@0 476 }
michael@0 477 }
michael@0 478
michael@0 479 /* If the basicConstraint extension is included in an immediate CA
michael@0 480 * certificate, make sure that the isCA flag is on. If the
michael@0 481 * pathLenConstraint component exists, it must be greater than the
michael@0 482 * number of CA certificates we have seen so far. If the extension
michael@0 483 * is omitted, we will assume that this is a CA certificate with
michael@0 484 * an unlimited pathLenConstraint (since it already passes the
michael@0 485 * netscape-cert-type extension checking).
michael@0 486 */
michael@0 487
michael@0 488 rv = CERT_FindBasicConstraintExten(issuerCert, &basicConstraint);
michael@0 489 if ( rv != SECSuccess ) {
michael@0 490 if (PORT_GetError() != SEC_ERROR_EXTENSION_NOT_FOUND) {
michael@0 491 LOG_ERROR_OR_EXIT(log,issuerCert,count+1,0);
michael@0 492 }
michael@0 493 pathLengthLimit = CERT_UNLIMITED_PATH_CONSTRAINT;
michael@0 494 /* no basic constraints found, we aren't (yet) a CA. */
michael@0 495 isca = PR_FALSE;
michael@0 496 } else {
michael@0 497 if ( basicConstraint.isCA == PR_FALSE ) {
michael@0 498 PORT_SetError (SEC_ERROR_CA_CERT_INVALID);
michael@0 499 LOG_ERROR_OR_EXIT(log,issuerCert,count+1,0);
michael@0 500 }
michael@0 501 pathLengthLimit = basicConstraint.pathLenConstraint;
michael@0 502 isca = PR_TRUE;
michael@0 503 }
michael@0 504 /* make sure that the path len constraint is properly set.*/
michael@0 505 if (pathLengthLimit >= 0 && currentPathLen > pathLengthLimit) {
michael@0 506 PORT_SetError (SEC_ERROR_PATH_LEN_CONSTRAINT_INVALID);
michael@0 507 LOG_ERROR_OR_EXIT(log, issuerCert, count+1, pathLengthLimit);
michael@0 508 }
michael@0 509
michael@0 510 /* make sure that the entire chain is within the name space of the
michael@0 511 * current issuer certificate.
michael@0 512 */
michael@0 513 rv = CERT_CompareNameSpace(issuerCert, namesList, certsList,
michael@0 514 arena, &badCert);
michael@0 515 if (rv != SECSuccess || badCert != NULL) {
michael@0 516 PORT_SetError(SEC_ERROR_CERT_NOT_IN_NAME_SPACE);
michael@0 517 LOG_ERROR_OR_EXIT(log, badCert, count + 1, 0);
michael@0 518 goto loser;
michael@0 519 }
michael@0 520
michael@0 521 /* XXX - the error logging may need to go down into CRL stuff at some
michael@0 522 * point
michael@0 523 */
michael@0 524 /* check revoked list (issuer) */
michael@0 525 rv = SEC_CheckCRL(handle, subjectCert, issuerCert, t, wincx);
michael@0 526 if (rv == SECFailure) {
michael@0 527 if (revoked) {
michael@0 528 *revoked = PR_TRUE;
michael@0 529 }
michael@0 530 LOG_ERROR_OR_EXIT(log,subjectCert,count,0);
michael@0 531 } else if (rv == SECWouldBlock) {
michael@0 532 /* We found something fishy, so we intend to issue an
michael@0 533 * error to the user, but the user may wish to continue
michael@0 534 * processing, in which case we better make sure nothing
michael@0 535 * worse has happened... so keep cranking the loop */
michael@0 536 rvFinal = SECFailure;
michael@0 537 if (revoked) {
michael@0 538 *revoked = PR_TRUE;
michael@0 539 }
michael@0 540 LOG_ERROR(log,subjectCert,count,0);
michael@0 541 }
michael@0 542
michael@0 543 if ( CERT_GetCertTrust(issuerCert, &issuerTrust) == SECSuccess) {
michael@0 544 /* we have some trust info, but this does NOT imply that this
michael@0 545 * cert is actually trusted for any purpose. The cert may be
michael@0 546 * explicitly UNtrusted. We won't know until we examine the
michael@0 547 * trust bits.
michael@0 548 */
michael@0 549 unsigned int flags;
michael@0 550
michael@0 551 if (certUsage != certUsageAnyCA &&
michael@0 552 certUsage != certUsageStatusResponder) {
michael@0 553
michael@0 554 /*
michael@0 555 * XXX This choice of trustType seems arbitrary.
michael@0 556 */
michael@0 557 if ( certUsage == certUsageVerifyCA ) {
michael@0 558 if ( subjectCert->nsCertType & NS_CERT_TYPE_EMAIL_CA ) {
michael@0 559 trustType = trustEmail;
michael@0 560 } else if ( subjectCert->nsCertType & NS_CERT_TYPE_SSL_CA ) {
michael@0 561 trustType = trustSSL;
michael@0 562 } else {
michael@0 563 trustType = trustObjectSigning;
michael@0 564 }
michael@0 565 }
michael@0 566
michael@0 567 flags = SEC_GET_TRUST_FLAGS(&issuerTrust, trustType);
michael@0 568 if (( flags & requiredFlags ) == requiredFlags) {
michael@0 569 /* we found a trusted one, so return */
michael@0 570 rv = rvFinal;
michael@0 571 goto done;
michael@0 572 }
michael@0 573 if (flags & CERTDB_VALID_CA) {
michael@0 574 validCAOverride = PR_TRUE;
michael@0 575 }
michael@0 576 /* is it explicitly distrusted? */
michael@0 577 if ((flags & CERTDB_TERMINAL_RECORD) &&
michael@0 578 ((flags & (CERTDB_TRUSTED|CERTDB_TRUSTED_CA)) == 0)) {
michael@0 579 /* untrusted -- the cert is explicitly untrusted, not
michael@0 580 * just that it doesn't chain to a trusted cert */
michael@0 581 PORT_SetError(SEC_ERROR_UNTRUSTED_ISSUER);
michael@0 582 LOG_ERROR_OR_EXIT(log,issuerCert,count+1,flags);
michael@0 583 }
michael@0 584 } else {
michael@0 585 /* Check if we have any valid trust when cheching for
michael@0 586 * certUsageAnyCA or certUsageStatusResponder. */
michael@0 587 for (trustType = trustSSL; trustType < trustTypeNone;
michael@0 588 trustType++) {
michael@0 589 flags = SEC_GET_TRUST_FLAGS(&issuerTrust, trustType);
michael@0 590 if ((flags & requiredFlags) == requiredFlags) {
michael@0 591 rv = rvFinal;
michael@0 592 goto done;
michael@0 593 }
michael@0 594 if (flags & CERTDB_VALID_CA)
michael@0 595 validCAOverride = PR_TRUE;
michael@0 596 }
michael@0 597 /* We have 2 separate loops because we want any single trust
michael@0 598 * bit to allow this usage to return trusted. Only if none of
michael@0 599 * the trust bits are on do we check to see if the cert is
michael@0 600 * untrusted */
michael@0 601 for (trustType = trustSSL; trustType < trustTypeNone;
michael@0 602 trustType++) {
michael@0 603 flags = SEC_GET_TRUST_FLAGS(&issuerTrust, trustType);
michael@0 604 /* is it explicitly distrusted? */
michael@0 605 if ((flags & CERTDB_TERMINAL_RECORD) &&
michael@0 606 ((flags & (CERTDB_TRUSTED|CERTDB_TRUSTED_CA)) == 0)) {
michael@0 607 /* untrusted -- the cert is explicitly untrusted, not
michael@0 608 * just that it doesn't chain to a trusted cert */
michael@0 609 PORT_SetError(SEC_ERROR_UNTRUSTED_ISSUER);
michael@0 610 LOG_ERROR_OR_EXIT(log,issuerCert,count+1,flags);
michael@0 611 }
michael@0 612 }
michael@0 613 }
michael@0 614 }
michael@0 615
michael@0 616 if (!validCAOverride) {
michael@0 617 /*
michael@0 618 * Make sure that if this is an intermediate CA in the chain that
michael@0 619 * it was given permission by its signer to be a CA.
michael@0 620 */
michael@0 621 /*
michael@0 622 * if basicConstraints says it is a ca, then we check the
michael@0 623 * nsCertType. If the nsCertType has any CA bits set, then
michael@0 624 * it must have the right one.
michael@0 625 */
michael@0 626 if (!isca || (issuerCert->nsCertType & NS_CERT_TYPE_CA)) {
michael@0 627 isca = (issuerCert->nsCertType & caCertType) ? PR_TRUE : PR_FALSE;
michael@0 628 }
michael@0 629
michael@0 630 if ( !isca ) {
michael@0 631 PORT_SetError(SEC_ERROR_CA_CERT_INVALID);
michael@0 632 LOG_ERROR_OR_EXIT(log,issuerCert,count+1,0);
michael@0 633 }
michael@0 634
michael@0 635 /* make sure key usage allows cert signing */
michael@0 636 if (CERT_CheckKeyUsage(issuerCert, requiredCAKeyUsage) != SECSuccess) {
michael@0 637 PORT_SetError(SEC_ERROR_INADEQUATE_KEY_USAGE);
michael@0 638 LOG_ERROR_OR_EXIT(log,issuerCert,count+1,requiredCAKeyUsage);
michael@0 639 }
michael@0 640 }
michael@0 641
michael@0 642 /* make sure that the issuer is not self signed. If it is, then
michael@0 643 * stop here to prevent looping.
michael@0 644 */
michael@0 645 if (issuerCert->isRoot) {
michael@0 646 PORT_SetError(SEC_ERROR_UNTRUSTED_ISSUER);
michael@0 647 LOG_ERROR(log, issuerCert, count+1, 0);
michael@0 648 goto loser;
michael@0 649 }
michael@0 650 /* The issuer cert will be the subject cert in the next loop.
michael@0 651 * A cert is self-issued if its subject and issuer are equal and
michael@0 652 * both are of non-zero length.
michael@0 653 */
michael@0 654 subjectCertIsSelfIssued = (PRBool)
michael@0 655 SECITEM_ItemsAreEqual(&issuerCert->derIssuer,
michael@0 656 &issuerCert->derSubject) &&
michael@0 657 issuerCert->derSubject.len > 0;
michael@0 658 if (subjectCertIsSelfIssued == PR_FALSE) {
michael@0 659 /* RFC 3280 says only non-self-issued intermediate CA certs
michael@0 660 * count in path length.
michael@0 661 */
michael@0 662 ++currentPathLen;
michael@0 663 }
michael@0 664
michael@0 665 CERT_DestroyCertificate(subjectCert);
michael@0 666 subjectCert = issuerCert;
michael@0 667 issuerCert = NULL;
michael@0 668 }
michael@0 669
michael@0 670 PORT_SetError(SEC_ERROR_UNKNOWN_ISSUER);
michael@0 671 LOG_ERROR(log,subjectCert,count,0);
michael@0 672 loser:
michael@0 673 rv = SECFailure;
michael@0 674 done:
michael@0 675 if (certsList != NULL) {
michael@0 676 PORT_Free(certsList);
michael@0 677 }
michael@0 678 if ( issuerCert ) {
michael@0 679 CERT_DestroyCertificate(issuerCert);
michael@0 680 }
michael@0 681
michael@0 682 if ( subjectCert ) {
michael@0 683 CERT_DestroyCertificate(subjectCert);
michael@0 684 }
michael@0 685
michael@0 686 if ( arena != NULL ) {
michael@0 687 PORT_FreeArena(arena, PR_FALSE);
michael@0 688 }
michael@0 689 return rv;
michael@0 690 }
michael@0 691
michael@0 692 SECStatus
michael@0 693 cert_VerifyCertChain(CERTCertDBHandle *handle, CERTCertificate *cert,
michael@0 694 PRBool checkSig, PRBool* sigerror,
michael@0 695 SECCertUsage certUsage, PRTime t, void *wincx,
michael@0 696 CERTVerifyLog *log, PRBool* revoked)
michael@0 697 {
michael@0 698 if (CERT_GetUsePKIXForValidation()) {
michael@0 699 return cert_VerifyCertChainPkix(cert, checkSig, certUsage, t,
michael@0 700 wincx, log, sigerror, revoked);
michael@0 701 }
michael@0 702 return cert_VerifyCertChainOld(handle, cert, checkSig, sigerror,
michael@0 703 certUsage, t, wincx, log, revoked);
michael@0 704 }
michael@0 705
michael@0 706 SECStatus
michael@0 707 CERT_VerifyCertChain(CERTCertDBHandle *handle, CERTCertificate *cert,
michael@0 708 PRBool checkSig, SECCertUsage certUsage, PRTime t,
michael@0 709 void *wincx, CERTVerifyLog *log)
michael@0 710 {
michael@0 711 return cert_VerifyCertChain(handle, cert, checkSig, NULL, certUsage, t,
michael@0 712 wincx, log, NULL);
michael@0 713 }
michael@0 714
michael@0 715 /*
michael@0 716 * verify that a CA can sign a certificate with the requested usage.
michael@0 717 */
michael@0 718 SECStatus
michael@0 719 CERT_VerifyCACertForUsage(CERTCertDBHandle *handle, CERTCertificate *cert,
michael@0 720 PRBool checkSig, SECCertUsage certUsage, PRTime t,
michael@0 721 void *wincx, CERTVerifyLog *log)
michael@0 722 {
michael@0 723 SECTrustType trustType;
michael@0 724 CERTBasicConstraints basicConstraint;
michael@0 725 PRBool isca;
michael@0 726 PRBool validCAOverride = PR_FALSE;
michael@0 727 SECStatus rv;
michael@0 728 SECStatus rvFinal = SECSuccess;
michael@0 729 unsigned int flags;
michael@0 730 unsigned int caCertType;
michael@0 731 unsigned int requiredCAKeyUsage;
michael@0 732 unsigned int requiredFlags;
michael@0 733 CERTCertificate *issuerCert;
michael@0 734 CERTCertTrust certTrust;
michael@0 735
michael@0 736
michael@0 737 if (CERT_KeyUsageAndTypeForCertUsage(certUsage, PR_TRUE,
michael@0 738 &requiredCAKeyUsage,
michael@0 739 &caCertType) != SECSuccess ) {
michael@0 740 PORT_Assert(0);
michael@0 741 EXIT_IF_NOT_LOGGING(log);
michael@0 742 requiredCAKeyUsage = 0;
michael@0 743 caCertType = 0;
michael@0 744 }
michael@0 745
michael@0 746 switch ( certUsage ) {
michael@0 747 case certUsageSSLClient:
michael@0 748 case certUsageSSLServer:
michael@0 749 case certUsageSSLCA:
michael@0 750 case certUsageSSLServerWithStepUp:
michael@0 751 case certUsageEmailSigner:
michael@0 752 case certUsageEmailRecipient:
michael@0 753 case certUsageObjectSigner:
michael@0 754 case certUsageVerifyCA:
michael@0 755 case certUsageStatusResponder:
michael@0 756 if ( CERT_TrustFlagsForCACertUsage(certUsage, &requiredFlags,
michael@0 757 &trustType) != SECSuccess ) {
michael@0 758 PORT_Assert(0);
michael@0 759 EXIT_IF_NOT_LOGGING(log);
michael@0 760 requiredFlags = 0;
michael@0 761 trustType = trustSSL;
michael@0 762 }
michael@0 763 break;
michael@0 764 default:
michael@0 765 PORT_Assert(0);
michael@0 766 EXIT_IF_NOT_LOGGING(log);
michael@0 767 requiredFlags = 0;
michael@0 768 trustType = trustSSL;/* This used to be 0, but we need something
michael@0 769 * that matches the enumeration type.
michael@0 770 */
michael@0 771 caCertType = 0;
michael@0 772 }
michael@0 773
michael@0 774 /* If the basicConstraint extension is included in an intermmediate CA
michael@0 775 * certificate, make sure that the isCA flag is on. If the
michael@0 776 * pathLenConstraint component exists, it must be greater than the
michael@0 777 * number of CA certificates we have seen so far. If the extension
michael@0 778 * is omitted, we will assume that this is a CA certificate with
michael@0 779 * an unlimited pathLenConstraint (since it already passes the
michael@0 780 * netscape-cert-type extension checking).
michael@0 781 */
michael@0 782
michael@0 783 rv = CERT_FindBasicConstraintExten(cert, &basicConstraint);
michael@0 784 if ( rv != SECSuccess ) {
michael@0 785 if (PORT_GetError() != SEC_ERROR_EXTENSION_NOT_FOUND) {
michael@0 786 LOG_ERROR_OR_EXIT(log,cert,0,0);
michael@0 787 }
michael@0 788 /* no basic constraints found, we aren't (yet) a CA. */
michael@0 789 isca = PR_FALSE;
michael@0 790 } else {
michael@0 791 if ( basicConstraint.isCA == PR_FALSE ) {
michael@0 792 PORT_SetError (SEC_ERROR_CA_CERT_INVALID);
michael@0 793 LOG_ERROR_OR_EXIT(log,cert,0,0);
michael@0 794 }
michael@0 795
michael@0 796 /* can't check path length if we don't know the previous path */
michael@0 797 isca = PR_TRUE;
michael@0 798 }
michael@0 799
michael@0 800 if ( CERT_GetCertTrust(cert, &certTrust) == SECSuccess ) {
michael@0 801 /* we have some trust info, but this does NOT imply that this
michael@0 802 * cert is actually trusted for any purpose. The cert may be
michael@0 803 * explicitly UNtrusted. We won't know until we examine the
michael@0 804 * trust bits.
michael@0 805 */
michael@0 806 if (certUsage == certUsageStatusResponder) {
michael@0 807 /* Check the special case of certUsageStatusResponder */
michael@0 808 issuerCert = CERT_FindCertIssuer(cert, t, certUsage);
michael@0 809 if (issuerCert) {
michael@0 810 if (SEC_CheckCRL(handle, cert, issuerCert, t, wincx)
michael@0 811 != SECSuccess) {
michael@0 812 PORT_SetError(SEC_ERROR_REVOKED_CERTIFICATE);
michael@0 813 CERT_DestroyCertificate(issuerCert);
michael@0 814 goto loser;
michael@0 815 }
michael@0 816 CERT_DestroyCertificate(issuerCert);
michael@0 817 }
michael@0 818 /* XXX We have NOT determined that this cert is trusted.
michael@0 819 * For years, NSS has treated this as trusted,
michael@0 820 * but it seems incorrect.
michael@0 821 */
michael@0 822 rv = rvFinal;
michael@0 823 goto done;
michael@0 824 }
michael@0 825
michael@0 826 /*
michael@0 827 * check the trust params of the issuer
michael@0 828 */
michael@0 829 flags = SEC_GET_TRUST_FLAGS(&certTrust, trustType);
michael@0 830 if ( ( flags & requiredFlags ) == requiredFlags) {
michael@0 831 /* we found a trusted one, so return */
michael@0 832 rv = rvFinal;
michael@0 833 goto done;
michael@0 834 }
michael@0 835 if (flags & CERTDB_VALID_CA) {
michael@0 836 validCAOverride = PR_TRUE;
michael@0 837 }
michael@0 838 /* is it explicitly distrusted? */
michael@0 839 if ((flags & CERTDB_TERMINAL_RECORD) &&
michael@0 840 ((flags & (CERTDB_TRUSTED|CERTDB_TRUSTED_CA)) == 0)) {
michael@0 841 /* untrusted -- the cert is explicitly untrusted, not
michael@0 842 * just that it doesn't chain to a trusted cert */
michael@0 843 PORT_SetError(SEC_ERROR_UNTRUSTED_CERT);
michael@0 844 LOG_ERROR_OR_EXIT(log,cert,0,flags);
michael@0 845 }
michael@0 846 }
michael@0 847 if (!validCAOverride) {
michael@0 848 /*
michael@0 849 * Make sure that if this is an intermediate CA in the chain that
michael@0 850 * it was given permission by its signer to be a CA.
michael@0 851 */
michael@0 852 /*
michael@0 853 * if basicConstraints says it is a ca, then we check the
michael@0 854 * nsCertType. If the nsCertType has any CA bits set, then
michael@0 855 * it must have the right one.
michael@0 856 */
michael@0 857 if (!isca || (cert->nsCertType & NS_CERT_TYPE_CA)) {
michael@0 858 isca = (cert->nsCertType & caCertType) ? PR_TRUE : PR_FALSE;
michael@0 859 }
michael@0 860
michael@0 861 if (!isca) {
michael@0 862 PORT_SetError(SEC_ERROR_CA_CERT_INVALID);
michael@0 863 LOG_ERROR_OR_EXIT(log,cert,0,0);
michael@0 864 }
michael@0 865
michael@0 866 /* make sure key usage allows cert signing */
michael@0 867 if (CERT_CheckKeyUsage(cert, requiredCAKeyUsage) != SECSuccess) {
michael@0 868 PORT_SetError(SEC_ERROR_INADEQUATE_KEY_USAGE);
michael@0 869 LOG_ERROR_OR_EXIT(log,cert,0,requiredCAKeyUsage);
michael@0 870 }
michael@0 871 }
michael@0 872 /* make sure that the issuer is not self signed. If it is, then
michael@0 873 * stop here to prevent looping.
michael@0 874 */
michael@0 875 if (cert->isRoot) {
michael@0 876 PORT_SetError(SEC_ERROR_UNTRUSTED_ISSUER);
michael@0 877 LOG_ERROR(log, cert, 0, 0);
michael@0 878 goto loser;
michael@0 879 }
michael@0 880
michael@0 881 return CERT_VerifyCertChain(handle, cert, checkSig, certUsage, t,
michael@0 882 wincx, log);
michael@0 883 loser:
michael@0 884 rv = SECFailure;
michael@0 885 done:
michael@0 886 return rv;
michael@0 887 }
michael@0 888
michael@0 889 #define NEXT_USAGE() { \
michael@0 890 i*=2; \
michael@0 891 certUsage++; \
michael@0 892 continue; \
michael@0 893 }
michael@0 894
michael@0 895 #define VALID_USAGE() { \
michael@0 896 NEXT_USAGE(); \
michael@0 897 }
michael@0 898
michael@0 899 #define INVALID_USAGE() { \
michael@0 900 if (returnedUsages) { \
michael@0 901 *returnedUsages &= (~i); \
michael@0 902 } \
michael@0 903 if (PR_TRUE == requiredUsage) { \
michael@0 904 valid = SECFailure; \
michael@0 905 } \
michael@0 906 NEXT_USAGE(); \
michael@0 907 }
michael@0 908
michael@0 909 /*
michael@0 910 * check the leaf cert against trust and usage.
michael@0 911 * returns success if the cert is not distrusted. If the cert is
michael@0 912 * trusted, then the trusted bool will be true.
michael@0 913 * returns failure if the cert is distrusted. If failure, flags
michael@0 914 * will return the flag bits that indicated distrust.
michael@0 915 */
michael@0 916 SECStatus
michael@0 917 cert_CheckLeafTrust(CERTCertificate *cert, SECCertUsage certUsage,
michael@0 918 unsigned int *failedFlags, PRBool *trusted)
michael@0 919 {
michael@0 920 unsigned int flags;
michael@0 921 CERTCertTrust trust;
michael@0 922
michael@0 923 *failedFlags = 0;
michael@0 924 *trusted = PR_FALSE;
michael@0 925
michael@0 926 /* check trust flags to see if this cert is directly trusted */
michael@0 927 if ( CERT_GetCertTrust(cert, &trust) == SECSuccess ) {
michael@0 928 switch ( certUsage ) {
michael@0 929 case certUsageSSLClient:
michael@0 930 case certUsageSSLServer:
michael@0 931 flags = trust.sslFlags;
michael@0 932
michael@0 933 /* is the cert directly trusted or not trusted ? */
michael@0 934 if ( flags & CERTDB_TERMINAL_RECORD) { /* the trust record is
michael@0 935 * authoritative */
michael@0 936 if ( flags & CERTDB_TRUSTED ) { /* trust this cert */
michael@0 937 *trusted = PR_TRUE;
michael@0 938 return SECSuccess;
michael@0 939 } else { /* don't trust this cert */
michael@0 940 *failedFlags = flags;
michael@0 941 return SECFailure;
michael@0 942 }
michael@0 943 }
michael@0 944 break;
michael@0 945 case certUsageSSLServerWithStepUp:
michael@0 946 /* XXX - step up certs can't be directly trusted, only distrust */
michael@0 947 flags = trust.sslFlags;
michael@0 948 if ( flags & CERTDB_TERMINAL_RECORD) { /* the trust record is
michael@0 949 * authoritative */
michael@0 950 if (( flags & CERTDB_TRUSTED ) == 0) {
michael@0 951 /* don't trust this cert */
michael@0 952 *failedFlags = flags;
michael@0 953 return SECFailure;
michael@0 954 }
michael@0 955 }
michael@0 956 break;
michael@0 957 case certUsageSSLCA:
michael@0 958 flags = trust.sslFlags;
michael@0 959 if ( flags & CERTDB_TERMINAL_RECORD) { /* the trust record is
michael@0 960 * authoritative */
michael@0 961 if (( flags & (CERTDB_TRUSTED|CERTDB_TRUSTED_CA) ) == 0) {
michael@0 962 /* don't trust this cert */
michael@0 963 *failedFlags = flags;
michael@0 964 return SECFailure;
michael@0 965 }
michael@0 966 }
michael@0 967 break;
michael@0 968 case certUsageEmailSigner:
michael@0 969 case certUsageEmailRecipient:
michael@0 970 flags = trust.emailFlags;
michael@0 971 if ( flags & CERTDB_TERMINAL_RECORD) { /* the trust record is
michael@0 972 * authoritative */
michael@0 973 if ( flags & CERTDB_TRUSTED ) { /* trust this cert */
michael@0 974 *trusted = PR_TRUE;
michael@0 975 return SECSuccess;
michael@0 976 }
michael@0 977 else { /* don't trust this cert */
michael@0 978 *failedFlags = flags;
michael@0 979 return SECFailure;
michael@0 980 }
michael@0 981 }
michael@0 982
michael@0 983 break;
michael@0 984 case certUsageObjectSigner:
michael@0 985 flags = trust.objectSigningFlags;
michael@0 986
michael@0 987 if ( flags & CERTDB_TERMINAL_RECORD) { /* the trust record is
michael@0 988 * authoritative */
michael@0 989 if ( flags & CERTDB_TRUSTED ) { /* trust this cert */
michael@0 990 *trusted = PR_TRUE;
michael@0 991 return SECSuccess;
michael@0 992 } else { /* don't trust this cert */
michael@0 993 *failedFlags = flags;
michael@0 994 return SECFailure;
michael@0 995 }
michael@0 996 }
michael@0 997 break;
michael@0 998 case certUsageVerifyCA:
michael@0 999 case certUsageStatusResponder:
michael@0 1000 flags = trust.sslFlags;
michael@0 1001 /* is the cert directly trusted or not trusted ? */
michael@0 1002 if ( ( flags & ( CERTDB_VALID_CA | CERTDB_TRUSTED_CA ) ) ==
michael@0 1003 ( CERTDB_VALID_CA | CERTDB_TRUSTED_CA ) ) {
michael@0 1004 *trusted = PR_TRUE;
michael@0 1005 return SECSuccess;
michael@0 1006 }
michael@0 1007 flags = trust.emailFlags;
michael@0 1008 /* is the cert directly trusted or not trusted ? */
michael@0 1009 if ( ( flags & ( CERTDB_VALID_CA | CERTDB_TRUSTED_CA ) ) ==
michael@0 1010 ( CERTDB_VALID_CA | CERTDB_TRUSTED_CA ) ) {
michael@0 1011 *trusted = PR_TRUE;
michael@0 1012 return SECSuccess;
michael@0 1013 }
michael@0 1014 flags = trust.objectSigningFlags;
michael@0 1015 /* is the cert directly trusted or not trusted ? */
michael@0 1016 if ( ( flags & ( CERTDB_VALID_CA | CERTDB_TRUSTED_CA ) ) ==
michael@0 1017 ( CERTDB_VALID_CA | CERTDB_TRUSTED_CA ) ) {
michael@0 1018 *trusted = PR_TRUE;
michael@0 1019 return SECSuccess;
michael@0 1020 }
michael@0 1021 /* fall through to test distrust */
michael@0 1022 case certUsageAnyCA:
michael@0 1023 case certUsageUserCertImport:
michael@0 1024 /* do we distrust these certs explicitly */
michael@0 1025 flags = trust.sslFlags;
michael@0 1026 if ( flags & CERTDB_TERMINAL_RECORD) { /* the trust record is
michael@0 1027 * authoritative */
michael@0 1028 if ((flags & (CERTDB_TRUSTED|CERTDB_TRUSTED_CA)) == 0) {
michael@0 1029 *failedFlags = flags;
michael@0 1030 return SECFailure;
michael@0 1031 }
michael@0 1032 }
michael@0 1033 flags = trust.emailFlags;
michael@0 1034 if ( flags & CERTDB_TERMINAL_RECORD) { /* the trust record is
michael@0 1035 * authoritative */
michael@0 1036 if ((flags & (CERTDB_TRUSTED|CERTDB_TRUSTED_CA)) == 0) {
michael@0 1037 *failedFlags = flags;
michael@0 1038 return SECFailure;
michael@0 1039 }
michael@0 1040 }
michael@0 1041 /* fall through */
michael@0 1042 case certUsageProtectedObjectSigner:
michael@0 1043 flags = trust.objectSigningFlags;
michael@0 1044 if ( flags & CERTDB_TERMINAL_RECORD) { /* the trust record is
michael@0 1045 * authoritative */
michael@0 1046 if ((flags & (CERTDB_TRUSTED|CERTDB_TRUSTED_CA)) == 0) {
michael@0 1047 *failedFlags = flags;
michael@0 1048 return SECFailure;
michael@0 1049 }
michael@0 1050 }
michael@0 1051 break;
michael@0 1052 }
michael@0 1053 }
michael@0 1054 return SECSuccess;
michael@0 1055 }
michael@0 1056
michael@0 1057 /*
michael@0 1058 * verify a certificate by checking if it's valid and that we
michael@0 1059 * trust the issuer.
michael@0 1060 *
michael@0 1061 * certificateUsage contains a bitfield of all cert usages that are
michael@0 1062 * required for verification to succeed
michael@0 1063 *
michael@0 1064 * a bitfield of cert usages is returned in *returnedUsages
michael@0 1065 * if requiredUsages is non-zero, the returned bitmap is only
michael@0 1066 * for those required usages, otherwise it is for all usages
michael@0 1067 *
michael@0 1068 */
michael@0 1069 SECStatus
michael@0 1070 CERT_VerifyCertificate(CERTCertDBHandle *handle, CERTCertificate *cert,
michael@0 1071 PRBool checkSig, SECCertificateUsage requiredUsages, PRTime t,
michael@0 1072 void *wincx, CERTVerifyLog *log, SECCertificateUsage* returnedUsages)
michael@0 1073 {
michael@0 1074 SECStatus rv;
michael@0 1075 SECStatus valid;
michael@0 1076 unsigned int requiredKeyUsage;
michael@0 1077 unsigned int requiredCertType;
michael@0 1078 unsigned int flags;
michael@0 1079 unsigned int certType;
michael@0 1080 PRBool allowOverride;
michael@0 1081 SECCertTimeValidity validity;
michael@0 1082 CERTStatusConfig *statusConfig;
michael@0 1083 PRInt32 i;
michael@0 1084 SECCertUsage certUsage = 0;
michael@0 1085 PRBool checkedOCSP = PR_FALSE;
michael@0 1086 PRBool checkAllUsages = PR_FALSE;
michael@0 1087 PRBool revoked = PR_FALSE;
michael@0 1088 PRBool sigerror = PR_FALSE;
michael@0 1089 PRBool trusted = PR_FALSE;
michael@0 1090
michael@0 1091 if (!requiredUsages) {
michael@0 1092 /* there are no required usages, so the user probably wants to
michael@0 1093 get status for all usages */
michael@0 1094 checkAllUsages = PR_TRUE;
michael@0 1095 }
michael@0 1096
michael@0 1097 if (returnedUsages) {
michael@0 1098 *returnedUsages = 0;
michael@0 1099 } else {
michael@0 1100 /* we don't have a place to return status for all usages,
michael@0 1101 so we can skip checks for usages that aren't required */
michael@0 1102 checkAllUsages = PR_FALSE;
michael@0 1103 }
michael@0 1104 valid = SECSuccess ; /* start off assuming cert is valid */
michael@0 1105
michael@0 1106 /* make sure that the cert is valid at time t */
michael@0 1107 allowOverride = (PRBool)((requiredUsages & certificateUsageSSLServer) ||
michael@0 1108 (requiredUsages & certificateUsageSSLServerWithStepUp));
michael@0 1109 validity = CERT_CheckCertValidTimes(cert, t, allowOverride);
michael@0 1110 if ( validity != secCertTimeValid ) {
michael@0 1111 valid = SECFailure;
michael@0 1112 LOG_ERROR_OR_EXIT(log,cert,0,validity);
michael@0 1113 }
michael@0 1114
michael@0 1115 /* check key usage and netscape cert type */
michael@0 1116 cert_GetCertType(cert);
michael@0 1117 certType = cert->nsCertType;
michael@0 1118
michael@0 1119 for (i=1; i<=certificateUsageHighest &&
michael@0 1120 (SECSuccess == valid || returnedUsages || log) ; ) {
michael@0 1121 PRBool requiredUsage = (i & requiredUsages) ? PR_TRUE : PR_FALSE;
michael@0 1122 if (PR_FALSE == requiredUsage && PR_FALSE == checkAllUsages) {
michael@0 1123 NEXT_USAGE();
michael@0 1124 }
michael@0 1125 if (returnedUsages) {
michael@0 1126 *returnedUsages |= i; /* start off assuming this usage is valid */
michael@0 1127 }
michael@0 1128 switch ( certUsage ) {
michael@0 1129 case certUsageSSLClient:
michael@0 1130 case certUsageSSLServer:
michael@0 1131 case certUsageSSLServerWithStepUp:
michael@0 1132 case certUsageSSLCA:
michael@0 1133 case certUsageEmailSigner:
michael@0 1134 case certUsageEmailRecipient:
michael@0 1135 case certUsageObjectSigner:
michael@0 1136 case certUsageStatusResponder:
michael@0 1137 rv = CERT_KeyUsageAndTypeForCertUsage(certUsage, PR_FALSE,
michael@0 1138 &requiredKeyUsage,
michael@0 1139 &requiredCertType);
michael@0 1140 if ( rv != SECSuccess ) {
michael@0 1141 PORT_Assert(0);
michael@0 1142 /* EXIT_IF_NOT_LOGGING(log); XXX ??? */
michael@0 1143 requiredKeyUsage = 0;
michael@0 1144 requiredCertType = 0;
michael@0 1145 INVALID_USAGE();
michael@0 1146 }
michael@0 1147 break;
michael@0 1148
michael@0 1149 case certUsageAnyCA:
michael@0 1150 case certUsageProtectedObjectSigner:
michael@0 1151 case certUsageUserCertImport:
michael@0 1152 case certUsageVerifyCA:
michael@0 1153 /* these usages cannot be verified */
michael@0 1154 NEXT_USAGE();
michael@0 1155
michael@0 1156 default:
michael@0 1157 PORT_Assert(0);
michael@0 1158 requiredKeyUsage = 0;
michael@0 1159 requiredCertType = 0;
michael@0 1160 INVALID_USAGE();
michael@0 1161 }
michael@0 1162 if ( CERT_CheckKeyUsage(cert, requiredKeyUsage) != SECSuccess ) {
michael@0 1163 if (PR_TRUE == requiredUsage) {
michael@0 1164 PORT_SetError(SEC_ERROR_INADEQUATE_KEY_USAGE);
michael@0 1165 }
michael@0 1166 LOG_ERROR(log,cert,0,requiredKeyUsage);
michael@0 1167 INVALID_USAGE();
michael@0 1168 }
michael@0 1169 if ( !( certType & requiredCertType ) ) {
michael@0 1170 if (PR_TRUE == requiredUsage) {
michael@0 1171 PORT_SetError(SEC_ERROR_INADEQUATE_CERT_TYPE);
michael@0 1172 }
michael@0 1173 LOG_ERROR(log,cert,0,requiredCertType);
michael@0 1174 INVALID_USAGE();
michael@0 1175 }
michael@0 1176
michael@0 1177 rv = cert_CheckLeafTrust(cert, certUsage, &flags, &trusted);
michael@0 1178 if (rv == SECFailure) {
michael@0 1179 if (PR_TRUE == requiredUsage) {
michael@0 1180 PORT_SetError(SEC_ERROR_UNTRUSTED_CERT);
michael@0 1181 }
michael@0 1182 LOG_ERROR(log, cert, 0, flags);
michael@0 1183 INVALID_USAGE();
michael@0 1184 } else if (trusted) {
michael@0 1185 VALID_USAGE();
michael@0 1186 }
michael@0 1187
michael@0 1188 if (PR_TRUE == revoked || PR_TRUE == sigerror) {
michael@0 1189 INVALID_USAGE();
michael@0 1190 }
michael@0 1191
michael@0 1192 rv = cert_VerifyCertChain(handle, cert,
michael@0 1193 checkSig, &sigerror,
michael@0 1194 certUsage, t, wincx, log,
michael@0 1195 &revoked);
michael@0 1196
michael@0 1197 if (rv != SECSuccess) {
michael@0 1198 /* EXIT_IF_NOT_LOGGING(log); XXX ???? */
michael@0 1199 INVALID_USAGE();
michael@0 1200 }
michael@0 1201
michael@0 1202 /*
michael@0 1203 * Check OCSP revocation status, but only if the cert we are checking
michael@0 1204 * is not a status responder itself. We only do this in the case
michael@0 1205 * where we checked the cert chain (above); explicit trust "wins"
michael@0 1206 * (avoids status checking, just as it avoids CRL checking) by
michael@0 1207 * bypassing this code.
michael@0 1208 */
michael@0 1209
michael@0 1210 if (PR_FALSE == checkedOCSP) {
michael@0 1211 checkedOCSP = PR_TRUE; /* only check OCSP once */
michael@0 1212 statusConfig = CERT_GetStatusConfig(handle);
michael@0 1213 if (requiredUsages != certificateUsageStatusResponder &&
michael@0 1214 statusConfig != NULL) {
michael@0 1215 if (statusConfig->statusChecker != NULL) {
michael@0 1216 rv = (* statusConfig->statusChecker)(handle, cert,
michael@0 1217 t, wincx);
michael@0 1218 if (rv != SECSuccess) {
michael@0 1219 LOG_ERROR(log,cert,0,0);
michael@0 1220 revoked = PR_TRUE;
michael@0 1221 INVALID_USAGE();
michael@0 1222 }
michael@0 1223 }
michael@0 1224 }
michael@0 1225 }
michael@0 1226
michael@0 1227 NEXT_USAGE();
michael@0 1228 }
michael@0 1229
michael@0 1230 loser:
michael@0 1231 return(valid);
michael@0 1232 }
michael@0 1233
michael@0 1234 SECStatus
michael@0 1235 CERT_VerifyCert(CERTCertDBHandle *handle, CERTCertificate *cert,
michael@0 1236 PRBool checkSig, SECCertUsage certUsage, PRTime t,
michael@0 1237 void *wincx, CERTVerifyLog *log)
michael@0 1238 {
michael@0 1239 return cert_VerifyCertWithFlags(handle, cert, checkSig, certUsage, t,
michael@0 1240 CERT_VERIFYCERT_USE_DEFAULTS, wincx, log);
michael@0 1241 }
michael@0 1242
michael@0 1243 SECStatus
michael@0 1244 cert_VerifyCertWithFlags(CERTCertDBHandle *handle, CERTCertificate *cert,
michael@0 1245 PRBool checkSig, SECCertUsage certUsage, PRTime t,
michael@0 1246 PRUint32 flags, void *wincx, CERTVerifyLog *log)
michael@0 1247 {
michael@0 1248 SECStatus rv;
michael@0 1249 unsigned int requiredKeyUsage;
michael@0 1250 unsigned int requiredCertType;
michael@0 1251 unsigned int failedFlags;
michael@0 1252 unsigned int certType;
michael@0 1253 PRBool trusted;
michael@0 1254 PRBool allowOverride;
michael@0 1255 SECCertTimeValidity validity;
michael@0 1256 CERTStatusConfig *statusConfig;
michael@0 1257
michael@0 1258 #ifdef notdef
michael@0 1259 /* check if this cert is in the Evil list */
michael@0 1260 rv = CERT_CheckForEvilCert(cert);
michael@0 1261 if ( rv != SECSuccess ) {
michael@0 1262 PORT_SetError(SEC_ERROR_REVOKED_CERTIFICATE);
michael@0 1263 LOG_ERROR_OR_EXIT(log,cert,0,0);
michael@0 1264 }
michael@0 1265 #endif
michael@0 1266
michael@0 1267 /* make sure that the cert is valid at time t */
michael@0 1268 allowOverride = (PRBool)((certUsage == certUsageSSLServer) ||
michael@0 1269 (certUsage == certUsageSSLServerWithStepUp));
michael@0 1270 validity = CERT_CheckCertValidTimes(cert, t, allowOverride);
michael@0 1271 if ( validity != secCertTimeValid ) {
michael@0 1272 LOG_ERROR_OR_EXIT(log,cert,0,validity);
michael@0 1273 }
michael@0 1274
michael@0 1275 /* check key usage and netscape cert type */
michael@0 1276 cert_GetCertType(cert);
michael@0 1277 certType = cert->nsCertType;
michael@0 1278 switch ( certUsage ) {
michael@0 1279 case certUsageSSLClient:
michael@0 1280 case certUsageSSLServer:
michael@0 1281 case certUsageSSLServerWithStepUp:
michael@0 1282 case certUsageSSLCA:
michael@0 1283 case certUsageEmailSigner:
michael@0 1284 case certUsageEmailRecipient:
michael@0 1285 case certUsageObjectSigner:
michael@0 1286 case certUsageStatusResponder:
michael@0 1287 rv = CERT_KeyUsageAndTypeForCertUsage(certUsage, PR_FALSE,
michael@0 1288 &requiredKeyUsage,
michael@0 1289 &requiredCertType);
michael@0 1290 if ( rv != SECSuccess ) {
michael@0 1291 PORT_Assert(0);
michael@0 1292 EXIT_IF_NOT_LOGGING(log);
michael@0 1293 requiredKeyUsage = 0;
michael@0 1294 requiredCertType = 0;
michael@0 1295 }
michael@0 1296 break;
michael@0 1297 case certUsageVerifyCA:
michael@0 1298 case certUsageAnyCA:
michael@0 1299 requiredKeyUsage = KU_KEY_CERT_SIGN;
michael@0 1300 requiredCertType = NS_CERT_TYPE_CA;
michael@0 1301 if ( ! ( certType & NS_CERT_TYPE_CA ) ) {
michael@0 1302 certType |= NS_CERT_TYPE_CA;
michael@0 1303 }
michael@0 1304 break;
michael@0 1305 default:
michael@0 1306 PORT_Assert(0);
michael@0 1307 EXIT_IF_NOT_LOGGING(log);
michael@0 1308 requiredKeyUsage = 0;
michael@0 1309 requiredCertType = 0;
michael@0 1310 }
michael@0 1311 if ( CERT_CheckKeyUsage(cert, requiredKeyUsage) != SECSuccess ) {
michael@0 1312 PORT_SetError(SEC_ERROR_INADEQUATE_KEY_USAGE);
michael@0 1313 LOG_ERROR_OR_EXIT(log,cert,0,requiredKeyUsage);
michael@0 1314 }
michael@0 1315 if ( !( certType & requiredCertType ) ) {
michael@0 1316 PORT_SetError(SEC_ERROR_INADEQUATE_CERT_TYPE);
michael@0 1317 LOG_ERROR_OR_EXIT(log,cert,0,requiredCertType);
michael@0 1318 }
michael@0 1319
michael@0 1320 rv = cert_CheckLeafTrust(cert, certUsage, &failedFlags, &trusted);
michael@0 1321 if (rv == SECFailure) {
michael@0 1322 PORT_SetError(SEC_ERROR_UNTRUSTED_CERT);
michael@0 1323 LOG_ERROR_OR_EXIT(log, cert, 0, failedFlags);
michael@0 1324 } else if (trusted) {
michael@0 1325 goto done;
michael@0 1326 }
michael@0 1327
michael@0 1328
michael@0 1329 rv = CERT_VerifyCertChain(handle, cert, checkSig, certUsage,
michael@0 1330 t, wincx, log);
michael@0 1331 if (rv != SECSuccess) {
michael@0 1332 EXIT_IF_NOT_LOGGING(log);
michael@0 1333 }
michael@0 1334
michael@0 1335 /*
michael@0 1336 * Check revocation status, but only if the cert we are checking is not a
michael@0 1337 * status responder itself and the caller did not ask us to skip the check.
michael@0 1338 * We only do this in the case where we checked the cert chain (above);
michael@0 1339 * explicit trust "wins" (avoids status checking, just as it avoids CRL
michael@0 1340 * checking, which is all done inside VerifyCertChain) by bypassing this
michael@0 1341 * code.
michael@0 1342 */
michael@0 1343 if (!(flags & CERT_VERIFYCERT_SKIP_OCSP) &&
michael@0 1344 certUsage != certUsageStatusResponder) {
michael@0 1345 statusConfig = CERT_GetStatusConfig(handle);
michael@0 1346 if (statusConfig && statusConfig->statusChecker) {
michael@0 1347 rv = (* statusConfig->statusChecker)(handle, cert,
michael@0 1348 t, wincx);
michael@0 1349 if (rv != SECSuccess) {
michael@0 1350 LOG_ERROR_OR_EXIT(log,cert,0,0);
michael@0 1351 }
michael@0 1352 }
michael@0 1353 }
michael@0 1354
michael@0 1355 done:
michael@0 1356 if (log && log->head) {
michael@0 1357 return SECFailure;
michael@0 1358 }
michael@0 1359 return(SECSuccess);
michael@0 1360
michael@0 1361 loser:
michael@0 1362 rv = SECFailure;
michael@0 1363
michael@0 1364 return(rv);
michael@0 1365 }
michael@0 1366
michael@0 1367 /*
michael@0 1368 * verify a certificate by checking if its valid and that we
michael@0 1369 * trust the issuer. Verify time against now.
michael@0 1370 */
michael@0 1371 SECStatus
michael@0 1372 CERT_VerifyCertificateNow(CERTCertDBHandle *handle, CERTCertificate *cert,
michael@0 1373 PRBool checkSig, SECCertificateUsage requiredUsages,
michael@0 1374 void *wincx, SECCertificateUsage* returnedUsages)
michael@0 1375 {
michael@0 1376 return(CERT_VerifyCertificate(handle, cert, checkSig,
michael@0 1377 requiredUsages, PR_Now(), wincx, NULL, returnedUsages));
michael@0 1378 }
michael@0 1379
michael@0 1380 /* obsolete, do not use for new code */
michael@0 1381 SECStatus
michael@0 1382 CERT_VerifyCertNow(CERTCertDBHandle *handle, CERTCertificate *cert,
michael@0 1383 PRBool checkSig, SECCertUsage certUsage, void *wincx)
michael@0 1384 {
michael@0 1385 return(CERT_VerifyCert(handle, cert, checkSig,
michael@0 1386 certUsage, PR_Now(), wincx, NULL));
michael@0 1387 }
michael@0 1388
michael@0 1389
michael@0 1390 /* [ FROM pcertdb.c ] */
michael@0 1391 /*
michael@0 1392 * Supported usage values and types:
michael@0 1393 * certUsageSSLClient
michael@0 1394 * certUsageSSLServer
michael@0 1395 * certUsageSSLServerWithStepUp
michael@0 1396 * certUsageEmailSigner
michael@0 1397 * certUsageEmailRecipient
michael@0 1398 * certUsageObjectSigner
michael@0 1399 */
michael@0 1400
michael@0 1401 CERTCertificate *
michael@0 1402 CERT_FindMatchingCert(CERTCertDBHandle *handle, SECItem *derName,
michael@0 1403 CERTCertOwner owner, SECCertUsage usage,
michael@0 1404 PRBool preferTrusted, PRTime validTime, PRBool validOnly)
michael@0 1405 {
michael@0 1406 CERTCertList *certList = NULL;
michael@0 1407 CERTCertificate *cert = NULL;
michael@0 1408 CERTCertTrust certTrust;
michael@0 1409 unsigned int requiredTrustFlags;
michael@0 1410 SECTrustType requiredTrustType;
michael@0 1411 unsigned int flags;
michael@0 1412
michael@0 1413 PRBool lookingForCA = PR_FALSE;
michael@0 1414 SECStatus rv;
michael@0 1415 CERTCertListNode *node;
michael@0 1416 CERTCertificate *saveUntrustedCA = NULL;
michael@0 1417
michael@0 1418 /* if preferTrusted is set, must be a CA cert */
michael@0 1419 PORT_Assert( ! ( preferTrusted && ( owner != certOwnerCA ) ) );
michael@0 1420
michael@0 1421 if ( owner == certOwnerCA ) {
michael@0 1422 lookingForCA = PR_TRUE;
michael@0 1423 if ( preferTrusted ) {
michael@0 1424 rv = CERT_TrustFlagsForCACertUsage(usage, &requiredTrustFlags,
michael@0 1425 &requiredTrustType);
michael@0 1426 if ( rv != SECSuccess ) {
michael@0 1427 goto loser;
michael@0 1428 }
michael@0 1429 requiredTrustFlags |= CERTDB_VALID_CA;
michael@0 1430 }
michael@0 1431 }
michael@0 1432
michael@0 1433 certList = CERT_CreateSubjectCertList(NULL, handle, derName, validTime,
michael@0 1434 validOnly);
michael@0 1435 if ( certList != NULL ) {
michael@0 1436 rv = CERT_FilterCertListByUsage(certList, usage, lookingForCA);
michael@0 1437 if ( rv != SECSuccess ) {
michael@0 1438 goto loser;
michael@0 1439 }
michael@0 1440
michael@0 1441 node = CERT_LIST_HEAD(certList);
michael@0 1442
michael@0 1443 while ( !CERT_LIST_END(node, certList) ) {
michael@0 1444 cert = node->cert;
michael@0 1445
michael@0 1446 /* looking for a trusted CA cert */
michael@0 1447 if ( ( owner == certOwnerCA ) && preferTrusted &&
michael@0 1448 ( requiredTrustType != trustTypeNone ) ) {
michael@0 1449
michael@0 1450 if ( CERT_GetCertTrust(cert, &certTrust) != SECSuccess ) {
michael@0 1451 flags = 0;
michael@0 1452 } else {
michael@0 1453 flags = SEC_GET_TRUST_FLAGS(&certTrust, requiredTrustType);
michael@0 1454 }
michael@0 1455
michael@0 1456 if ( ( flags & requiredTrustFlags ) != requiredTrustFlags ) {
michael@0 1457 /* cert is not trusted */
michael@0 1458 /* if this is the first cert to get this far, then save
michael@0 1459 * it, so we can use it if we can't find a trusted one
michael@0 1460 */
michael@0 1461 if ( saveUntrustedCA == NULL ) {
michael@0 1462 saveUntrustedCA = cert;
michael@0 1463 }
michael@0 1464 goto endloop;
michael@0 1465 }
michael@0 1466 }
michael@0 1467 /* if we got this far, then this cert meets all criteria */
michael@0 1468 break;
michael@0 1469
michael@0 1470 endloop:
michael@0 1471 node = CERT_LIST_NEXT(node);
michael@0 1472 cert = NULL;
michael@0 1473 }
michael@0 1474
michael@0 1475 /* use the saved one if we have it */
michael@0 1476 if ( cert == NULL ) {
michael@0 1477 cert = saveUntrustedCA;
michael@0 1478 }
michael@0 1479
michael@0 1480 /* if we found one then bump the ref count before freeing the list */
michael@0 1481 if ( cert != NULL ) {
michael@0 1482 /* bump the ref count */
michael@0 1483 cert = CERT_DupCertificate(cert);
michael@0 1484 }
michael@0 1485
michael@0 1486 CERT_DestroyCertList(certList);
michael@0 1487 }
michael@0 1488
michael@0 1489 return(cert);
michael@0 1490
michael@0 1491 loser:
michael@0 1492 if ( certList != NULL ) {
michael@0 1493 CERT_DestroyCertList(certList);
michael@0 1494 }
michael@0 1495
michael@0 1496 return(NULL);
michael@0 1497 }
michael@0 1498
michael@0 1499
michael@0 1500 /* [ From certdb.c ] */
michael@0 1501 /*
michael@0 1502 * Filter a list of certificates, removing those certs that do not have
michael@0 1503 * one of the named CA certs somewhere in their cert chain.
michael@0 1504 *
michael@0 1505 * "certList" - the list of certificates to filter
michael@0 1506 * "nCANames" - number of CA names
michael@0 1507 * "caNames" - array of CA names in string(rfc 1485) form
michael@0 1508 * "usage" - what use the certs are for, this is used when
michael@0 1509 * selecting CA certs
michael@0 1510 */
michael@0 1511 SECStatus
michael@0 1512 CERT_FilterCertListByCANames(CERTCertList *certList, int nCANames,
michael@0 1513 char **caNames, SECCertUsage usage)
michael@0 1514 {
michael@0 1515 CERTCertificate *issuerCert = NULL;
michael@0 1516 CERTCertificate *subjectCert;
michael@0 1517 CERTCertListNode *node, *freenode;
michael@0 1518 CERTCertificate *cert;
michael@0 1519 int n;
michael@0 1520 char **names;
michael@0 1521 PRBool found;
michael@0 1522 PRTime time;
michael@0 1523
michael@0 1524 if ( nCANames <= 0 ) {
michael@0 1525 return(SECSuccess);
michael@0 1526 }
michael@0 1527
michael@0 1528 time = PR_Now();
michael@0 1529
michael@0 1530 node = CERT_LIST_HEAD(certList);
michael@0 1531
michael@0 1532 while ( ! CERT_LIST_END(node, certList) ) {
michael@0 1533 cert = node->cert;
michael@0 1534
michael@0 1535 subjectCert = CERT_DupCertificate(cert);
michael@0 1536
michael@0 1537 /* traverse the CA certs for this cert */
michael@0 1538 found = PR_FALSE;
michael@0 1539 while ( subjectCert != NULL ) {
michael@0 1540 n = nCANames;
michael@0 1541 names = caNames;
michael@0 1542
michael@0 1543 if (subjectCert->issuerName != NULL) {
michael@0 1544 while ( n > 0 ) {
michael@0 1545 if ( PORT_Strcmp(*names, subjectCert->issuerName) == 0 ) {
michael@0 1546 found = PR_TRUE;
michael@0 1547 break;
michael@0 1548 }
michael@0 1549
michael@0 1550 n--;
michael@0 1551 names++;
michael@0 1552 }
michael@0 1553 }
michael@0 1554
michael@0 1555 if ( found ) {
michael@0 1556 break;
michael@0 1557 }
michael@0 1558
michael@0 1559 issuerCert = CERT_FindCertIssuer(subjectCert, time, usage);
michael@0 1560 if ( issuerCert == subjectCert ) {
michael@0 1561 CERT_DestroyCertificate(issuerCert);
michael@0 1562 issuerCert = NULL;
michael@0 1563 break;
michael@0 1564 }
michael@0 1565 CERT_DestroyCertificate(subjectCert);
michael@0 1566 subjectCert = issuerCert;
michael@0 1567
michael@0 1568 }
michael@0 1569 CERT_DestroyCertificate(subjectCert);
michael@0 1570 if ( !found ) {
michael@0 1571 /* CA was not found, so remove this cert from the list */
michael@0 1572 freenode = node;
michael@0 1573 node = CERT_LIST_NEXT(node);
michael@0 1574 CERT_RemoveCertListNode(freenode);
michael@0 1575 } else {
michael@0 1576 /* CA was found, so leave it in the list */
michael@0 1577 node = CERT_LIST_NEXT(node);
michael@0 1578 }
michael@0 1579 }
michael@0 1580
michael@0 1581 return(SECSuccess);
michael@0 1582 }
michael@0 1583
michael@0 1584 /*
michael@0 1585 * Given a certificate, return a string containing the nickname, and possibly
michael@0 1586 * one of the validity strings, based on the current validity state of the
michael@0 1587 * certificate.
michael@0 1588 *
michael@0 1589 * "arena" - arena to allocate returned string from. If NULL, then heap
michael@0 1590 * is used.
michael@0 1591 * "cert" - the cert to get nickname from
michael@0 1592 * "expiredString" - the string to append to the nickname if the cert is
michael@0 1593 * expired.
michael@0 1594 * "notYetGoodString" - the string to append to the nickname if the cert is
michael@0 1595 * not yet good.
michael@0 1596 */
michael@0 1597 char *
michael@0 1598 CERT_GetCertNicknameWithValidity(PLArenaPool *arena, CERTCertificate *cert,
michael@0 1599 char *expiredString, char *notYetGoodString)
michael@0 1600 {
michael@0 1601 SECCertTimeValidity validity;
michael@0 1602 char *nickname = NULL, *tmpstr = NULL;
michael@0 1603
michael@0 1604 validity = CERT_CheckCertValidTimes(cert, PR_Now(), PR_FALSE);
michael@0 1605
michael@0 1606 /* if the cert is good, then just use the nickname directly */
michael@0 1607 if ( validity == secCertTimeValid ) {
michael@0 1608 if ( arena == NULL ) {
michael@0 1609 nickname = PORT_Strdup(cert->nickname);
michael@0 1610 } else {
michael@0 1611 nickname = PORT_ArenaStrdup(arena, cert->nickname);
michael@0 1612 }
michael@0 1613
michael@0 1614 if ( nickname == NULL ) {
michael@0 1615 goto loser;
michael@0 1616 }
michael@0 1617 } else {
michael@0 1618
michael@0 1619 /* if the cert is not valid, then tack one of the strings on the
michael@0 1620 * end
michael@0 1621 */
michael@0 1622 if ( validity == secCertTimeExpired ) {
michael@0 1623 tmpstr = PR_smprintf("%s%s", cert->nickname,
michael@0 1624 expiredString);
michael@0 1625 } else if ( validity == secCertTimeNotValidYet ) {
michael@0 1626 /* not yet valid */
michael@0 1627 tmpstr = PR_smprintf("%s%s", cert->nickname,
michael@0 1628 notYetGoodString);
michael@0 1629 } else {
michael@0 1630 /* undetermined */
michael@0 1631 tmpstr = PR_smprintf("%s",
michael@0 1632 "(NULL) (Validity Unknown)");
michael@0 1633 }
michael@0 1634
michael@0 1635 if ( tmpstr == NULL ) {
michael@0 1636 goto loser;
michael@0 1637 }
michael@0 1638
michael@0 1639 if ( arena ) {
michael@0 1640 /* copy the string into the arena and free the malloc'd one */
michael@0 1641 nickname = PORT_ArenaStrdup(arena, tmpstr);
michael@0 1642 PORT_Free(tmpstr);
michael@0 1643 } else {
michael@0 1644 nickname = tmpstr;
michael@0 1645 }
michael@0 1646 if ( nickname == NULL ) {
michael@0 1647 goto loser;
michael@0 1648 }
michael@0 1649 }
michael@0 1650 return(nickname);
michael@0 1651
michael@0 1652 loser:
michael@0 1653 return(NULL);
michael@0 1654 }
michael@0 1655
michael@0 1656 /*
michael@0 1657 * Collect the nicknames from all certs in a CertList. If the cert is not
michael@0 1658 * valid, append a string to that nickname.
michael@0 1659 *
michael@0 1660 * "certList" - the list of certificates
michael@0 1661 * "expiredString" - the string to append to the nickname of any expired cert
michael@0 1662 * "notYetGoodString" - the string to append to the nickname of any cert
michael@0 1663 * that is not yet valid
michael@0 1664 */
michael@0 1665 CERTCertNicknames *
michael@0 1666 CERT_NicknameStringsFromCertList(CERTCertList *certList, char *expiredString,
michael@0 1667 char *notYetGoodString)
michael@0 1668 {
michael@0 1669 CERTCertNicknames *names;
michael@0 1670 PLArenaPool *arena;
michael@0 1671 CERTCertListNode *node;
michael@0 1672 char **nn;
michael@0 1673
michael@0 1674 /* allocate an arena */
michael@0 1675 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
michael@0 1676 if ( arena == NULL ) {
michael@0 1677 return(NULL);
michael@0 1678 }
michael@0 1679
michael@0 1680 /* allocate the structure */
michael@0 1681 names = PORT_ArenaAlloc(arena, sizeof(CERTCertNicknames));
michael@0 1682 if ( names == NULL ) {
michael@0 1683 goto loser;
michael@0 1684 }
michael@0 1685
michael@0 1686 /* init the structure */
michael@0 1687 names->arena = arena;
michael@0 1688 names->head = NULL;
michael@0 1689 names->numnicknames = 0;
michael@0 1690 names->nicknames = NULL;
michael@0 1691 names->totallen = 0;
michael@0 1692
michael@0 1693 /* count the certs in the list */
michael@0 1694 node = CERT_LIST_HEAD(certList);
michael@0 1695 while ( ! CERT_LIST_END(node, certList) ) {
michael@0 1696 names->numnicknames++;
michael@0 1697 node = CERT_LIST_NEXT(node);
michael@0 1698 }
michael@0 1699
michael@0 1700 /* allocate nicknames array */
michael@0 1701 names->nicknames = PORT_ArenaAlloc(arena,
michael@0 1702 sizeof(char *) * names->numnicknames);
michael@0 1703 if ( names->nicknames == NULL ) {
michael@0 1704 goto loser;
michael@0 1705 }
michael@0 1706
michael@0 1707 /* just in case printf can't deal with null strings */
michael@0 1708 if (expiredString == NULL ) {
michael@0 1709 expiredString = "";
michael@0 1710 }
michael@0 1711
michael@0 1712 if ( notYetGoodString == NULL ) {
michael@0 1713 notYetGoodString = "";
michael@0 1714 }
michael@0 1715
michael@0 1716 /* traverse the list of certs and collect the nicknames */
michael@0 1717 nn = names->nicknames;
michael@0 1718 node = CERT_LIST_HEAD(certList);
michael@0 1719 while ( ! CERT_LIST_END(node, certList) ) {
michael@0 1720 *nn = CERT_GetCertNicknameWithValidity(arena, node->cert,
michael@0 1721 expiredString,
michael@0 1722 notYetGoodString);
michael@0 1723 if ( *nn == NULL ) {
michael@0 1724 goto loser;
michael@0 1725 }
michael@0 1726
michael@0 1727 names->totallen += PORT_Strlen(*nn);
michael@0 1728
michael@0 1729 nn++;
michael@0 1730 node = CERT_LIST_NEXT(node);
michael@0 1731 }
michael@0 1732
michael@0 1733 return(names);
michael@0 1734
michael@0 1735 loser:
michael@0 1736 PORT_FreeArena(arena, PR_FALSE);
michael@0 1737 return(NULL);
michael@0 1738 }
michael@0 1739
michael@0 1740 /*
michael@0 1741 * Extract the nickname from a nickmake string that may have either
michael@0 1742 * expiredString or notYetGoodString appended.
michael@0 1743 *
michael@0 1744 * Args:
michael@0 1745 * "namestring" - the string containing the nickname, and possibly
michael@0 1746 * one of the validity label strings
michael@0 1747 * "expiredString" - the expired validity label string
michael@0 1748 * "notYetGoodString" - the not yet good validity label string
michael@0 1749 *
michael@0 1750 * Returns the raw nickname
michael@0 1751 */
michael@0 1752 char *
michael@0 1753 CERT_ExtractNicknameString(char *namestring, char *expiredString,
michael@0 1754 char *notYetGoodString)
michael@0 1755 {
michael@0 1756 int explen, nyglen, namelen;
michael@0 1757 int retlen;
michael@0 1758 char *retstr;
michael@0 1759
michael@0 1760 namelen = PORT_Strlen(namestring);
michael@0 1761 explen = PORT_Strlen(expiredString);
michael@0 1762 nyglen = PORT_Strlen(notYetGoodString);
michael@0 1763
michael@0 1764 if ( namelen > explen ) {
michael@0 1765 if ( PORT_Strcmp(expiredString, &namestring[namelen-explen]) == 0 ) {
michael@0 1766 retlen = namelen - explen;
michael@0 1767 retstr = (char *)PORT_Alloc(retlen+1);
michael@0 1768 if ( retstr == NULL ) {
michael@0 1769 goto loser;
michael@0 1770 }
michael@0 1771
michael@0 1772 PORT_Memcpy(retstr, namestring, retlen);
michael@0 1773 retstr[retlen] = '\0';
michael@0 1774 goto done;
michael@0 1775 }
michael@0 1776 }
michael@0 1777
michael@0 1778 if ( namelen > nyglen ) {
michael@0 1779 if ( PORT_Strcmp(notYetGoodString, &namestring[namelen-nyglen]) == 0) {
michael@0 1780 retlen = namelen - nyglen;
michael@0 1781 retstr = (char *)PORT_Alloc(retlen+1);
michael@0 1782 if ( retstr == NULL ) {
michael@0 1783 goto loser;
michael@0 1784 }
michael@0 1785
michael@0 1786 PORT_Memcpy(retstr, namestring, retlen);
michael@0 1787 retstr[retlen] = '\0';
michael@0 1788 goto done;
michael@0 1789 }
michael@0 1790 }
michael@0 1791
michael@0 1792 /* if name string is shorter than either invalid string, then it must
michael@0 1793 * be a raw nickname
michael@0 1794 */
michael@0 1795 retstr = PORT_Strdup(namestring);
michael@0 1796
michael@0 1797 done:
michael@0 1798 return(retstr);
michael@0 1799
michael@0 1800 loser:
michael@0 1801 return(NULL);
michael@0 1802 }
michael@0 1803
michael@0 1804 CERTCertList *
michael@0 1805 CERT_GetCertChainFromCert(CERTCertificate *cert, PRTime time, SECCertUsage usage)
michael@0 1806 {
michael@0 1807 CERTCertList *chain = NULL;
michael@0 1808 int count = 0;
michael@0 1809
michael@0 1810 if (NULL == cert) {
michael@0 1811 return NULL;
michael@0 1812 }
michael@0 1813
michael@0 1814 cert = CERT_DupCertificate(cert);
michael@0 1815 if (NULL == cert) {
michael@0 1816 PORT_SetError(SEC_ERROR_NO_MEMORY);
michael@0 1817 return NULL;
michael@0 1818 }
michael@0 1819
michael@0 1820 chain = CERT_NewCertList();
michael@0 1821 if (NULL == chain) {
michael@0 1822 PORT_SetError(SEC_ERROR_NO_MEMORY);
michael@0 1823 return NULL;
michael@0 1824 }
michael@0 1825
michael@0 1826 while (cert != NULL && ++count <= CERT_MAX_CERT_CHAIN) {
michael@0 1827 if (SECSuccess != CERT_AddCertToListTail(chain, cert)) {
michael@0 1828 /* return partial chain */
michael@0 1829 PORT_SetError(SEC_ERROR_NO_MEMORY);
michael@0 1830 return chain;
michael@0 1831 }
michael@0 1832
michael@0 1833 if (cert->isRoot) {
michael@0 1834 /* return complete chain */
michael@0 1835 return chain;
michael@0 1836 }
michael@0 1837
michael@0 1838 cert = CERT_FindCertIssuer(cert, time, usage);
michael@0 1839 }
michael@0 1840
michael@0 1841 /* return partial chain */
michael@0 1842 PORT_SetError(SEC_ERROR_UNKNOWN_ISSUER);
michael@0 1843 return chain;
michael@0 1844 }

mercurial