security/nss/lib/libpkix/pkix/checker/pkix_signaturechecker.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
-rwxr-xr-x

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 /*
michael@0 5 * pkix_signaturechecker.c
michael@0 6 *
michael@0 7 * Functions for signature validation
michael@0 8 *
michael@0 9 */
michael@0 10
michael@0 11 #include "pkix_signaturechecker.h"
michael@0 12
michael@0 13 /*
michael@0 14 * FUNCTION: pkix_SignatureCheckerstate_Destroy
michael@0 15 * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
michael@0 16 */
michael@0 17 static PKIX_Error *
michael@0 18 pkix_SignatureCheckerState_Destroy(
michael@0 19 PKIX_PL_Object *object,
michael@0 20 void *plContext)
michael@0 21 {
michael@0 22 pkix_SignatureCheckerState *state = NULL;
michael@0 23
michael@0 24 PKIX_ENTER(SIGNATURECHECKERSTATE,
michael@0 25 "pkix_SignatureCheckerState_Destroy");
michael@0 26 PKIX_NULLCHECK_ONE(object);
michael@0 27
michael@0 28 /* Check that this object is a signature checker state */
michael@0 29 PKIX_CHECK(pkix_CheckType
michael@0 30 (object, PKIX_SIGNATURECHECKERSTATE_TYPE, plContext),
michael@0 31 PKIX_OBJECTNOTSIGNATURECHECKERSTATE);
michael@0 32
michael@0 33 state = (pkix_SignatureCheckerState *) object;
michael@0 34
michael@0 35 state->prevCertCertSign = PKIX_FALSE;
michael@0 36
michael@0 37 PKIX_DECREF(state->prevPublicKey);
michael@0 38 PKIX_DECREF(state->prevPublicKeyList);
michael@0 39 PKIX_DECREF(state->keyUsageOID);
michael@0 40
michael@0 41 cleanup:
michael@0 42
michael@0 43 PKIX_RETURN(SIGNATURECHECKERSTATE);
michael@0 44 }
michael@0 45
michael@0 46 /*
michael@0 47 * FUNCTION: pkix_SignatureCheckerState_RegisterSelf
michael@0 48 *
michael@0 49 * DESCRIPTION:
michael@0 50 * Registers PKIX_SIGNATURECHECKERSTATE_TYPE and its related functions
michael@0 51 * with systemClasses[]
michael@0 52 *
michael@0 53 * THREAD SAFETY:
michael@0 54 * Not Thread Safe (see Thread Safety Definitions in Programmer's Guide)
michael@0 55 *
michael@0 56 * Since this function is only called by PKIX_PL_Initialize, which should
michael@0 57 * only be called once, it is acceptable that this function is not
michael@0 58 * thread-safe.
michael@0 59 */
michael@0 60 PKIX_Error *
michael@0 61 pkix_SignatureCheckerState_RegisterSelf(void *plContext)
michael@0 62 {
michael@0 63 extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
michael@0 64 pkix_ClassTable_Entry entry;
michael@0 65
michael@0 66 PKIX_ENTER(SIGNATURECHECKERSTATE,
michael@0 67 "pkix_SignatureCheckerState_RegisterSelf");
michael@0 68
michael@0 69 entry.description = "SignatureCheckerState";
michael@0 70 entry.objCounter = 0;
michael@0 71 entry.typeObjectSize = sizeof(pkix_SignatureCheckerState);
michael@0 72 entry.destructor = pkix_SignatureCheckerState_Destroy;
michael@0 73 entry.equalsFunction = NULL;
michael@0 74 entry.hashcodeFunction = NULL;
michael@0 75 entry.toStringFunction = NULL;
michael@0 76 entry.comparator = NULL;
michael@0 77 entry.duplicateFunction = NULL;
michael@0 78
michael@0 79 systemClasses[PKIX_SIGNATURECHECKERSTATE_TYPE] = entry;
michael@0 80
michael@0 81 PKIX_RETURN(SIGNATURECHECKERSTATE);
michael@0 82 }
michael@0 83
michael@0 84 /*
michael@0 85 * FUNCTION: pkix_SignatureCheckerState_Create
michael@0 86 *
michael@0 87 * DESCRIPTION:
michael@0 88 * Allocate and initialize SignatureChecker state data.
michael@0 89 *
michael@0 90 * PARAMETERS
michael@0 91 * "trustedPubKey"
michael@0 92 * Address of trusted Anchor Public Key for verifying first Cert in the
michael@0 93 * chain. Must be non-NULL.
michael@0 94 * "certsRemaining"
michael@0 95 * Number of certificates remaining in the chain.
michael@0 96 * "pCheckerState"
michael@0 97 * Address where SignatureCheckerState will be stored. Must be non-NULL.
michael@0 98 * "plContext"
michael@0 99 * Platform-specific context pointer.
michael@0 100 *
michael@0 101 * THREAD SAFETY:
michael@0 102 * Not Thread Safe (see Thread Safety Definitions in Programmer's Guide)
michael@0 103 *
michael@0 104 * RETURNS:
michael@0 105 * Returns NULL if the function succeeds.
michael@0 106 * Returns a SignatureCheckerState Error if the function fails in a
michael@0 107 * non-fatal way.
michael@0 108 * Returns a Fatal Error if the function fails in an unrecoverable way.
michael@0 109 */
michael@0 110 static PKIX_Error *
michael@0 111 pkix_SignatureCheckerState_Create(
michael@0 112 PKIX_PL_PublicKey *trustedPubKey,
michael@0 113 PKIX_UInt32 certsRemaining,
michael@0 114 pkix_SignatureCheckerState **pCheckerState,
michael@0 115 void *plContext)
michael@0 116 {
michael@0 117 pkix_SignatureCheckerState *state = NULL;
michael@0 118 PKIX_PL_OID *keyUsageOID = NULL;
michael@0 119
michael@0 120 PKIX_ENTER(SIGNATURECHECKERSTATE, "pkix_SignatureCheckerState_Create");
michael@0 121 PKIX_NULLCHECK_TWO(trustedPubKey, pCheckerState);
michael@0 122
michael@0 123 PKIX_CHECK(PKIX_PL_Object_Alloc
michael@0 124 (PKIX_SIGNATURECHECKERSTATE_TYPE,
michael@0 125 sizeof (pkix_SignatureCheckerState),
michael@0 126 (PKIX_PL_Object **)&state,
michael@0 127 plContext),
michael@0 128 PKIX_COULDNOTCREATESIGNATURECHECKERSTATEOBJECT);
michael@0 129
michael@0 130 /* Initialize fields */
michael@0 131
michael@0 132 state->prevCertCertSign = PKIX_TRUE;
michael@0 133 state->prevPublicKeyList = NULL;
michael@0 134 state->certsRemaining = certsRemaining;
michael@0 135
michael@0 136 PKIX_INCREF(trustedPubKey);
michael@0 137 state->prevPublicKey = trustedPubKey;
michael@0 138
michael@0 139 PKIX_CHECK(PKIX_PL_OID_Create
michael@0 140 (PKIX_CERTKEYUSAGE_OID,
michael@0 141 &keyUsageOID,
michael@0 142 plContext),
michael@0 143 PKIX_OIDCREATEFAILED);
michael@0 144
michael@0 145 state->keyUsageOID = keyUsageOID;
michael@0 146 keyUsageOID = NULL;
michael@0 147
michael@0 148 *pCheckerState = state;
michael@0 149 state = NULL;
michael@0 150
michael@0 151 cleanup:
michael@0 152
michael@0 153 PKIX_DECREF(keyUsageOID);
michael@0 154 PKIX_DECREF(state);
michael@0 155
michael@0 156 PKIX_RETURN(SIGNATURECHECKERSTATE);
michael@0 157 }
michael@0 158
michael@0 159 /* --Private-Functions-------------------------------------------- */
michael@0 160
michael@0 161 /*
michael@0 162 * FUNCTION: pkix_SignatureChecker_Check
michael@0 163 * (see comments for PKIX_CertChainChecker_CheckCallback in pkix_checker.h)
michael@0 164 */
michael@0 165 PKIX_Error *
michael@0 166 pkix_SignatureChecker_Check(
michael@0 167 PKIX_CertChainChecker *checker,
michael@0 168 PKIX_PL_Cert *cert,
michael@0 169 PKIX_List *unresolvedCriticalExtensions,
michael@0 170 void **pNBIOContext,
michael@0 171 void *plContext)
michael@0 172 {
michael@0 173 pkix_SignatureCheckerState *state = NULL;
michael@0 174 PKIX_PL_PublicKey *prevPubKey = NULL;
michael@0 175 PKIX_PL_PublicKey *currPubKey = NULL;
michael@0 176 PKIX_PL_PublicKey *newPubKey = NULL;
michael@0 177 PKIX_PL_PublicKey *pKey = NULL;
michael@0 178 PKIX_PL_CertBasicConstraints *basicConstraints = NULL;
michael@0 179 PKIX_Error *checkKeyUsageFail = NULL;
michael@0 180 PKIX_Error *verifyFail = NULL;
michael@0 181 PKIX_Boolean certVerified = PKIX_FALSE;
michael@0 182
michael@0 183 PKIX_ENTER(CERTCHAINCHECKER, "pkix_SignatureChecker_Check");
michael@0 184 PKIX_NULLCHECK_THREE(checker, cert, pNBIOContext);
michael@0 185
michael@0 186 *pNBIOContext = NULL; /* we never block on pending I/O */
michael@0 187
michael@0 188 PKIX_CHECK(PKIX_CertChainChecker_GetCertChainCheckerState
michael@0 189 (checker, (PKIX_PL_Object **)&state, plContext),
michael@0 190 PKIX_CERTCHAINCHECKERGETCERTCHAINCHECKERSTATEFAILED);
michael@0 191
michael@0 192 (state->certsRemaining)--;
michael@0 193
michael@0 194 PKIX_INCREF(state->prevPublicKey);
michael@0 195 prevPubKey = state->prevPublicKey;
michael@0 196
michael@0 197 /*
michael@0 198 * Previous Cert doesn't have CertSign bit on for signature
michael@0 199 * verification and it is not a self-issued Cert so there is no
michael@0 200 * old key saved. This is considered error.
michael@0 201 */
michael@0 202 if (state->prevCertCertSign == PKIX_FALSE &&
michael@0 203 state->prevPublicKeyList == NULL) {
michael@0 204 PKIX_ERROR(PKIX_KEYUSAGEKEYCERTSIGNBITNOTON);
michael@0 205 }
michael@0 206
michael@0 207 /* Previous Cert is valid for signature verification, try it first */
michael@0 208 if (state->prevCertCertSign == PKIX_TRUE) {
michael@0 209 verifyFail = PKIX_PL_Cert_VerifySignature
michael@0 210 (cert, prevPubKey, plContext);
michael@0 211 if (verifyFail == NULL) {
michael@0 212 certVerified = PKIX_TRUE;
michael@0 213 } else {
michael@0 214 certVerified = PKIX_FALSE;
michael@0 215 }
michael@0 216 }
michael@0 217
michael@0 218 #ifdef NIST_TEST_4_5_4_AND_4_5_6
michael@0 219
michael@0 220 /*
michael@0 221 * Following codes under this compiler flag is implemented for
michael@0 222 * special cases of NIST tests 4.5.4 and 4.5.6. We are not sure
michael@0 223 * we should handle these two tests as what is implemented so the
michael@0 224 * codes are commented out, and the tests fails (for now).
michael@0 225 * For Cert chain validation, our assumption is all the Certs on
michael@0 226 * the chain are using its previous Cert's public key to decode
michael@0 227 * its current key. But for thses two tests, keys are used not
michael@0 228 * in this precedent order, we can either
michael@0 229 * 1) Use what is implemented here: take in what Cert order NIST
michael@0 230 * specified and for continuous self-issued Certs, stacking up
michael@0 231 * their keys and tries all of them in FILO order.
michael@0 232 * But this method breaks the idea of chain key presdency.
michael@0 233 * 2) Use Build Chain facility: we will specify the valid Certs
michael@0 234 * order (means key precedency is kept) and count on Build Chain
michael@0 235 * to get the Certs that can fill for the needed keys. This may have
michael@0 236 * performance impact.
michael@0 237 * 3) Fetch Certs from CertStore: we will specifiy the valid Certs
michael@0 238 * order and use CertSelector on SubjectName to get a list of
michael@0 239 * candidates Certs to fill in for the needed keys.
michael@0 240 * Anyhow, the codes are kept around just in case we want to use
michael@0 241 * solution one...
michael@0 242 */
michael@0 243
michael@0 244 /* If failed and previous key is self-issued, try its old key(s) */
michael@0 245 if (certVerified == PKIX_FALSE && state->prevPublicKeyList != NULL) {
michael@0 246
michael@0 247 /* Verify from keys on the list */
michael@0 248 PKIX_CHECK(PKIX_List_GetLength
michael@0 249 (state->prevPublicKeyList, &numKeys, plContext),
michael@0 250 PKIX_LISTGETLENGTHFAILED);
michael@0 251
michael@0 252 for (i = numKeys - 1; i >= 0; i--) {
michael@0 253
michael@0 254 PKIX_CHECK(PKIX_List_GetItem
michael@0 255 (state->prevPublicKeyList,
michael@0 256 i,
michael@0 257 (PKIX_PL_Object **) &pKey,
michael@0 258 plContext),
michael@0 259 PKIX_LISTGETITEMFAILED);
michael@0 260
michael@0 261 PKIX_DECREF(verifyFail);
michael@0 262 verifyFail = PKIX_PL_Cert_VerifySignature
michael@0 263 (cert, pKey, plContext);
michael@0 264
michael@0 265 if (verifyFail == NULL) {
michael@0 266 certVerified = PKIX_TRUE;
michael@0 267 break;
michael@0 268 } else {
michael@0 269 certVerified = PKIX_FALSE;
michael@0 270 }
michael@0 271
michael@0 272 PKIX_DECREF(pKey);
michael@0 273 }
michael@0 274 }
michael@0 275 #endif
michael@0 276
michael@0 277 if (certVerified == PKIX_FALSE) {
michael@0 278 pkixErrorResult = verifyFail;
michael@0 279 verifyFail = NULL;
michael@0 280 PKIX_ERROR(PKIX_VALIDATIONFAILEDCERTSIGNATURECHECKING);
michael@0 281 }
michael@0 282
michael@0 283 #ifdef NIST_TEST_4_5_4_AND_4_5_6
michael@0 284 /*
michael@0 285 * Check if Cert is self-issued. If so, the old key(s) is saved, in
michael@0 286 * conjunction to the new key, for verifying CERT validity later.
michael@0 287 */
michael@0 288 PKIX_CHECK(pkix_IsCertSelfIssued(cert, &selfIssued, plContext),
michael@0 289 PKIX_ISCERTSELFISSUEFAILED);
michael@0 290
michael@0 291 /*
michael@0 292 * Check if Cert is self-issued. If so, the public key of the Cert
michael@0 293 * that issues this Cert (old key) can be used together with this
michael@0 294 * current key (new key) for key verification. If there are multiple
michael@0 295 * self-issued certs, keys of those Certs (old keys) can also be used
michael@0 296 * for key verification. Old key(s) is saved in a list (PrevPublickKey-
michael@0 297 * List) and cleared when a Cert is no longer self-issued. PrevPublic-
michael@0 298 * Key keep key of the previous Cert.
michael@0 299 */
michael@0 300 if (selfIssued == PKIX_TRUE) {
michael@0 301
michael@0 302 /* Make sure previous Cert is valid for signature verification */
michael@0 303 if (state->prevCertCertSign == PKIX_TRUE) {
michael@0 304
michael@0 305 if (state->prevPublicKeyList == NULL) {
michael@0 306
michael@0 307 PKIX_CHECK(PKIX_List_Create
michael@0 308 (&state->prevPublicKeyList, plContext),
michael@0 309 PKIX_LISTCREATEFALIED);
michael@0 310
michael@0 311 }
michael@0 312
michael@0 313 PKIX_CHECK(PKIX_List_AppendItem
michael@0 314 (state->prevPublicKeyList,
michael@0 315 (PKIX_PL_Object *) state->prevPublicKey,
michael@0 316 plContext),
michael@0 317 PKIX_LISTAPPENDITEMFAILED);
michael@0 318 }
michael@0 319
michael@0 320 } else {
michael@0 321 /* Not self-issued Cert any more, clear old key(s) saved */
michael@0 322 PKIX_DECREF(state->prevPublicKeyList);
michael@0 323 }
michael@0 324 #endif
michael@0 325
michael@0 326 /* Save current key as prevPublicKey */
michael@0 327 PKIX_CHECK(PKIX_PL_Cert_GetSubjectPublicKey
michael@0 328 (cert, &currPubKey, plContext),
michael@0 329 PKIX_CERTGETSUBJECTPUBLICKEYFAILED);
michael@0 330
michael@0 331 PKIX_CHECK(PKIX_PL_PublicKey_MakeInheritedDSAPublicKey
michael@0 332 (currPubKey, prevPubKey, &newPubKey, plContext),
michael@0 333 PKIX_PUBLICKEYMAKEINHERITEDDSAPUBLICKEYFAILED);
michael@0 334
michael@0 335 if (newPubKey == NULL){
michael@0 336 PKIX_INCREF(currPubKey);
michael@0 337 newPubKey = currPubKey;
michael@0 338 }
michael@0 339
michael@0 340 PKIX_INCREF(newPubKey);
michael@0 341 PKIX_DECREF(state->prevPublicKey);
michael@0 342
michael@0 343 state->prevPublicKey = newPubKey;
michael@0 344
michael@0 345 /* Save this Cert key usage CertSign bit */
michael@0 346 if (state->certsRemaining != 0) {
michael@0 347 checkKeyUsageFail = PKIX_PL_Cert_VerifyKeyUsage
michael@0 348 (cert, PKIX_KEY_CERT_SIGN, plContext);
michael@0 349
michael@0 350 state->prevCertCertSign = (checkKeyUsageFail == NULL)?
michael@0 351 PKIX_TRUE:PKIX_FALSE;
michael@0 352
michael@0 353 PKIX_DECREF(checkKeyUsageFail);
michael@0 354 }
michael@0 355
michael@0 356 /* Remove Key Usage Extension OID from list */
michael@0 357 if (unresolvedCriticalExtensions != NULL) {
michael@0 358
michael@0 359 PKIX_CHECK(pkix_List_Remove
michael@0 360 (unresolvedCriticalExtensions,
michael@0 361 (PKIX_PL_Object *) state->keyUsageOID,
michael@0 362 plContext),
michael@0 363 PKIX_LISTREMOVEFAILED);
michael@0 364 }
michael@0 365
michael@0 366 PKIX_CHECK(PKIX_CertChainChecker_SetCertChainCheckerState
michael@0 367 (checker, (PKIX_PL_Object *)state, plContext),
michael@0 368 PKIX_CERTCHAINCHECKERSETCERTCHAINCHECKERSTATEFAILED);
michael@0 369
michael@0 370 cleanup:
michael@0 371
michael@0 372 PKIX_DECREF(state);
michael@0 373 PKIX_DECREF(pKey);
michael@0 374 PKIX_DECREF(prevPubKey);
michael@0 375 PKIX_DECREF(currPubKey);
michael@0 376 PKIX_DECREF(newPubKey);
michael@0 377 PKIX_DECREF(basicConstraints);
michael@0 378 PKIX_DECREF(verifyFail);
michael@0 379 PKIX_DECREF(checkKeyUsageFail);
michael@0 380
michael@0 381 PKIX_RETURN(CERTCHAINCHECKER);
michael@0 382
michael@0 383 }
michael@0 384
michael@0 385 /*
michael@0 386 * FUNCTION: pkix_SignatureChecker_Initialize
michael@0 387 * DESCRIPTION:
michael@0 388 *
michael@0 389 * Creates a new CertChainChecker and stores it at "pChecker", where it will
michael@0 390 * be used by pkix_SignatureChecker_Check to check that the public key in
michael@0 391 * the checker's state is able to successfully validate the certificate's
michael@0 392 * signature. The PublicKey pointed to by "trustedPubKey" is used to
michael@0 393 * initialize the checker's state.
michael@0 394 *
michael@0 395 * PARAMETERS:
michael@0 396 * "trustedPubKey"
michael@0 397 * Address of PublicKey representing the trusted public key used to
michael@0 398 * initialize the state of this checker. Must be non-NULL.
michael@0 399 * "certsRemaining"
michael@0 400 * Number of certificates remaining in the chain.
michael@0 401 * "pChecker"
michael@0 402 * Address where object pointer will be stored. Must be non-NULL.
michael@0 403 * "plContext"
michael@0 404 * Platform-specific context pointer.
michael@0 405 * THREAD SAFETY:
michael@0 406 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
michael@0 407 * RETURNS:
michael@0 408 * Returns NULL if the function succeeds.
michael@0 409 * Returns a CertChainChecker Error if the function fails in a non-fatal way.
michael@0 410 * Returns a Fatal Error if the function fails in an unrecoverable way.
michael@0 411 */
michael@0 412 PKIX_Error *
michael@0 413 pkix_SignatureChecker_Initialize(
michael@0 414 PKIX_PL_PublicKey *trustedPubKey,
michael@0 415 PKIX_UInt32 certsRemaining,
michael@0 416 PKIX_CertChainChecker **pChecker,
michael@0 417 void *plContext)
michael@0 418 {
michael@0 419 pkix_SignatureCheckerState* state = NULL;
michael@0 420 PKIX_ENTER(CERTCHAINCHECKER, "PKIX_SignatureChecker_Initialize");
michael@0 421 PKIX_NULLCHECK_TWO(pChecker, trustedPubKey);
michael@0 422
michael@0 423 PKIX_CHECK(pkix_SignatureCheckerState_Create
michael@0 424 (trustedPubKey, certsRemaining, &state, plContext),
michael@0 425 PKIX_SIGNATURECHECKERSTATECREATEFAILED);
michael@0 426
michael@0 427 PKIX_CHECK(PKIX_CertChainChecker_Create
michael@0 428 (pkix_SignatureChecker_Check,
michael@0 429 PKIX_FALSE,
michael@0 430 PKIX_FALSE,
michael@0 431 NULL,
michael@0 432 (PKIX_PL_Object *) state,
michael@0 433 pChecker,
michael@0 434 plContext),
michael@0 435 PKIX_CERTCHAINCHECKERCREATEFAILED);
michael@0 436
michael@0 437 cleanup:
michael@0 438
michael@0 439 PKIX_DECREF(state);
michael@0 440
michael@0 441 PKIX_RETURN(CERTCHAINCHECKER);
michael@0 442
michael@0 443 }

mercurial