Blame nss/lib/libpkix/pkix/checker/pkix_ocspchecker.c

Packit 40b132
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit 40b132
/*
Packit 40b132
 * pkix_ocspchecker.c
Packit 40b132
 *
Packit 40b132
 * OcspChecker Object Functions
Packit 40b132
 *
Packit 40b132
 */
Packit 40b132
Packit 40b132
#include "pkix_ocspchecker.h"
Packit 40b132
#include "pkix_pl_ocspcertid.h"
Packit 40b132
#include "pkix_error.h"
Packit 40b132
Packit 40b132
Packit 40b132
/* --Private-Data-and-Types--------------------------------------- */
Packit 40b132
Packit 40b132
typedef struct pkix_OcspCheckerStruct {
Packit 40b132
    /* RevocationMethod is the super class of OcspChecker. */
Packit 40b132
    pkix_RevocationMethod method;
Packit 40b132
    PKIX_PL_VerifyCallback certVerifyFcn;
Packit 40b132
} pkix_OcspChecker;
Packit 40b132
Packit 40b132
/* --Private-Functions-------------------------------------------- */
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * FUNCTION: pkix_OcspChecker_Destroy
Packit 40b132
 *      (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
Packit 40b132
 */
Packit 40b132
static PKIX_Error *
Packit 40b132
pkix_OcspChecker_Destroy(
Packit 40b132
        PKIX_PL_Object *object,
Packit 40b132
        void *plContext)
Packit 40b132
{
Packit 40b132
    return NULL;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * FUNCTION: pkix_OcspChecker_RegisterSelf
Packit 40b132
 * DESCRIPTION:
Packit 40b132
 *  Registers PKIX_OCSPCHECKER_TYPE and its related functions with
Packit 40b132
 *  systemClasses[]
Packit 40b132
 * THREAD SAFETY:
Packit 40b132
 *  Not Thread Safe - for performance and complexity reasons
Packit 40b132
 *
Packit 40b132
 *  Since this function is only called by PKIX_PL_Initialize, which should
Packit 40b132
 *  only be called once, it is acceptable that this function is not
Packit 40b132
 *  thread-safe.
Packit 40b132
 */
Packit 40b132
PKIX_Error *
Packit 40b132
pkix_OcspChecker_RegisterSelf(void *plContext)
Packit 40b132
{
Packit 40b132
        extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
Packit 40b132
        pkix_ClassTable_Entry* entry = &systemClasses[PKIX_OCSPCHECKER_TYPE];
Packit 40b132
Packit 40b132
        PKIX_ENTER(OCSPCHECKER, "pkix_OcspChecker_RegisterSelf");
Packit 40b132
Packit 40b132
        entry->description = "OcspChecker";
Packit 40b132
        entry->typeObjectSize = sizeof(pkix_OcspChecker);
Packit 40b132
        entry->destructor = pkix_OcspChecker_Destroy;
Packit 40b132
Packit 40b132
        PKIX_RETURN(OCSPCHECKER);
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * FUNCTION: pkix_OcspChecker_Create
Packit 40b132
 */
Packit 40b132
PKIX_Error *
Packit 40b132
pkix_OcspChecker_Create(PKIX_RevocationMethodType methodType,
Packit 40b132
                        PKIX_UInt32 flags,
Packit 40b132
                        PKIX_UInt32 priority,
Packit 40b132
                        pkix_LocalRevocationCheckFn localRevChecker,
Packit 40b132
                        pkix_ExternalRevocationCheckFn externalRevChecker,
Packit 40b132
                        PKIX_PL_VerifyCallback verifyFn,
Packit 40b132
                        pkix_RevocationMethod **pChecker,
Packit 40b132
                        void *plContext)
Packit 40b132
{
Packit 40b132
        pkix_OcspChecker *method = NULL;
Packit 40b132
Packit 40b132
        PKIX_ENTER(OCSPCHECKER, "pkix_OcspChecker_Create");
Packit 40b132
        PKIX_NULLCHECK_ONE(pChecker);
Packit 40b132
Packit 40b132
        PKIX_CHECK(PKIX_PL_Object_Alloc
Packit 40b132
                    (PKIX_OCSPCHECKER_TYPE,
Packit 40b132
                    sizeof (pkix_OcspChecker),
Packit 40b132
                    (PKIX_PL_Object **)&method,
Packit 40b132
                    plContext),
Packit 40b132
                    PKIX_COULDNOTCREATECERTCHAINCHECKEROBJECT);
Packit 40b132
Packit 40b132
        pkixErrorResult = pkix_RevocationMethod_Init(
Packit 40b132
            (pkix_RevocationMethod*)method, methodType, flags,  priority,
Packit 40b132
            localRevChecker, externalRevChecker, plContext);
Packit 40b132
        if (pkixErrorResult) {
Packit 40b132
            goto cleanup;
Packit 40b132
        }
Packit 40b132
        method->certVerifyFcn = (PKIX_PL_VerifyCallback)verifyFn;
Packit 40b132
Packit 40b132
        *pChecker = (pkix_RevocationMethod*)method;
Packit 40b132
        method = NULL;
Packit 40b132
Packit 40b132
cleanup:
Packit 40b132
        PKIX_DECREF(method);
Packit 40b132
Packit 40b132
        PKIX_RETURN(OCSPCHECKER);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * FUNCTION: pkix_OcspChecker_MapResultCodeToRevStatus
Packit 40b132
 */
Packit 40b132
PKIX_RevocationStatus
Packit 40b132
pkix_OcspChecker_MapResultCodeToRevStatus(SECErrorCodes resultCode)
Packit 40b132
{
Packit 40b132
        switch (resultCode) {
Packit 40b132
            case SEC_ERROR_REVOKED_CERTIFICATE:
Packit 40b132
                return PKIX_RevStatus_Revoked;
Packit 40b132
            default:
Packit 40b132
                return PKIX_RevStatus_NoInfo;
Packit 40b132
        }
Packit 40b132
}
Packit 40b132
Packit 40b132
/* --Public-Functions--------------------------------------------- */
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * FUNCTION: pkix_OcspChecker_Check (see comments in pkix_checker.h)
Packit 40b132
 */
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * The OCSPChecker is created in an idle state, and remains in this state until
Packit 40b132
 * either (a) the default Responder has been set and enabled, and a Check
Packit 40b132
 * request is received with no responder specified, or (b) a Check request is
Packit 40b132
 * received with a specified responder. A request message is constructed and
Packit 40b132
 * given to the HttpClient. If non-blocking I/O is used the client may return
Packit 40b132
 * with WOULDBLOCK, in which case the OCSPChecker returns the WOULDBLOCK
Packit 40b132
 * condition to its caller in turn. On a subsequent call the I/O is resumed.
Packit 40b132
 * When a response is received it is decoded and the results provided to the
Packit 40b132
 * caller.
Packit 40b132
 *
Packit 40b132
 */
Packit 40b132
PKIX_Error *
Packit 40b132
pkix_OcspChecker_CheckLocal(
Packit 40b132
        PKIX_PL_Cert *cert,
Packit 40b132
        PKIX_PL_Cert *issuer,
Packit 40b132
        PKIX_PL_Date *date,
Packit 40b132
        pkix_RevocationMethod *checkerObject,
Packit 40b132
        PKIX_ProcessingParams *procParams,
Packit 40b132
        PKIX_UInt32 methodFlags,
Packit 40b132
        PKIX_Boolean chainVerificationState,
Packit 40b132
        PKIX_RevocationStatus *pRevStatus,
Packit 40b132
        PKIX_UInt32 *pReasonCode,
Packit 40b132
        void *plContext)
Packit 40b132
{
Packit 40b132
        PKIX_PL_OcspCertID    *cid = NULL;
Packit 40b132
        PKIX_Boolean           hasFreshStatus = PKIX_FALSE;
Packit 40b132
        PKIX_Boolean           statusIsGood = PKIX_FALSE;
Packit 40b132
        SECErrorCodes          resultCode = SEC_ERROR_REVOKED_CERTIFICATE_OCSP;
Packit 40b132
        PKIX_RevocationStatus  revStatus = PKIX_RevStatus_NoInfo;
Packit 40b132
Packit 40b132
        PKIX_ENTER(OCSPCHECKER, "pkix_OcspChecker_CheckLocal");
Packit 40b132
Packit 40b132
        PKIX_CHECK(
Packit 40b132
            PKIX_PL_OcspCertID_Create(cert, NULL, &cid,
Packit 40b132
                                      plContext),
Packit 40b132
            PKIX_OCSPCERTIDCREATEFAILED);
Packit 40b132
        if (!cid) {
Packit 40b132
            goto cleanup;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PKIX_CHECK(
Packit 40b132
            PKIX_PL_OcspCertID_GetFreshCacheStatus(cid, date,
Packit 40b132
                                                   &hasFreshStatus,
Packit 40b132
                                                   &statusIsGood,
Packit 40b132
                                                   &resultCode,
Packit 40b132
                                                   plContext),
Packit 40b132
            PKIX_OCSPCERTIDGETFRESHCACHESTATUSFAILED);
Packit 40b132
        if (hasFreshStatus) {
Packit 40b132
            if (statusIsGood) {
Packit 40b132
                revStatus = PKIX_RevStatus_Success;
Packit 40b132
                resultCode = 0;
Packit 40b132
            } else {
Packit 40b132
                revStatus = pkix_OcspChecker_MapResultCodeToRevStatus(resultCode);
Packit 40b132
            }
Packit 40b132
        }
Packit 40b132
Packit 40b132
cleanup:
Packit 40b132
        *pRevStatus = revStatus;
Packit 40b132
Packit 40b132
        /* ocsp carries only tree statuses: good, bad, and unknown.
Packit 40b132
         * revStatus is used to pass them. reasonCode is always set
Packit 40b132
         * to be unknown. */
Packit 40b132
        *pReasonCode = crlEntryReasonUnspecified;
Packit 40b132
        PKIX_DECREF(cid);
Packit 40b132
Packit 40b132
        PKIX_RETURN(OCSPCHECKER);
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * The OCSPChecker is created in an idle state, and remains in this state until
Packit 40b132
 * either (a) the default Responder has been set and enabled, and a Check
Packit 40b132
 * request is received with no responder specified, or (b) a Check request is
Packit 40b132
 * received with a specified responder. A request message is constructed and
Packit 40b132
 * given to the HttpClient. When a response is received it is decoded and the
Packit 40b132
 * results provided to the caller.
Packit 40b132
 *
Packit 40b132
 * During the most recent enhancement of this function, it has been found that
Packit 40b132
 * it doesn't correctly implement non-blocking I/O.
Packit 40b132
 * 
Packit 40b132
 * The nbioContext is used in two places, for "response-obtaining" and
Packit 40b132
 * for "response-verification".
Packit 40b132
 * 
Packit 40b132
 * However, if this function gets called to resume, it always
Packit 40b132
 * repeats the "request creation" and "response fetching" steps!
Packit 40b132
 * As a result, the earlier operation is never resumed.
Packit 40b132
 */
Packit 40b132
PKIX_Error *
Packit 40b132
pkix_OcspChecker_CheckExternal(
Packit 40b132
        PKIX_PL_Cert *cert,
Packit 40b132
        PKIX_PL_Cert *issuer,
Packit 40b132
        PKIX_PL_Date *date,
Packit 40b132
        pkix_RevocationMethod *checkerObject,
Packit 40b132
        PKIX_ProcessingParams *procParams,
Packit 40b132
        PKIX_UInt32 methodFlags,
Packit 40b132
        PKIX_RevocationStatus *pRevStatus,
Packit 40b132
        PKIX_UInt32 *pReasonCode,
Packit 40b132
        void **pNBIOContext,
Packit 40b132
        void *plContext)
Packit 40b132
{
Packit 40b132
        SECErrorCodes resultCode = SEC_ERROR_REVOKED_CERTIFICATE_OCSP;
Packit 40b132
        PKIX_Boolean uriFound = PKIX_FALSE;
Packit 40b132
        PKIX_Boolean passed = PKIX_TRUE;
Packit 40b132
        pkix_OcspChecker *checker = NULL;
Packit 40b132
        PKIX_PL_OcspCertID *cid = NULL;
Packit 40b132
        PKIX_PL_OcspRequest *request = NULL;
Packit 40b132
        PKIX_PL_OcspResponse *response = NULL;
Packit 40b132
        PKIX_PL_Date *validity = NULL;
Packit 40b132
        PKIX_RevocationStatus revStatus = PKIX_RevStatus_NoInfo;
Packit 40b132
        void *nbioContext = NULL;
Packit 40b132
        enum { stageGET, stagePOST } currentStage;
Packit 40b132
        PRBool retry = PR_FALSE;
Packit 40b132
Packit 40b132
        PKIX_ENTER(OCSPCHECKER, "pkix_OcspChecker_CheckExternal");
Packit 40b132
Packit 40b132
        PKIX_CHECK(
Packit 40b132
            pkix_CheckType((PKIX_PL_Object*)checkerObject,
Packit 40b132
                           PKIX_OCSPCHECKER_TYPE, plContext),
Packit 40b132
                PKIX_OBJECTNOTOCSPCHECKER);
Packit 40b132
Packit 40b132
        checker = (pkix_OcspChecker *)checkerObject;
Packit 40b132
Packit 40b132
        PKIX_CHECK(
Packit 40b132
            PKIX_PL_OcspCertID_Create(cert, NULL, &cid,
Packit 40b132
                                      plContext),
Packit 40b132
            PKIX_OCSPCERTIDCREATEFAILED);
Packit 40b132
        
Packit 40b132
        /* create request */
Packit 40b132
        PKIX_CHECK(
Packit 40b132
            pkix_pl_OcspRequest_Create(cert, cid, validity, NULL, 
Packit 40b132
                                       methodFlags, &uriFound, &request,
Packit 40b132
                                       plContext),
Packit 40b132
            PKIX_OCSPREQUESTCREATEFAILED);
Packit 40b132
        
Packit 40b132
        if (uriFound == PKIX_FALSE) {
Packit 40b132
            /* no caching for certs lacking URI */
Packit 40b132
            resultCode = 0;
Packit 40b132
            goto cleanup;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        if (methodFlags & CERT_REV_M_FORCE_POST_METHOD_FOR_OCSP) {
Packit 40b132
            /* Do not try HTTP GET, only HTTP POST */
Packit 40b132
            currentStage = stagePOST;
Packit 40b132
        } else {
Packit 40b132
            /* Try HTTP GET first, falling back to POST */
Packit 40b132
            currentStage = stageGET;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        do {
Packit 40b132
                const char *method;
Packit 40b132
                passed = PKIX_TRUE;
Packit 40b132
Packit 40b132
                retry = PR_FALSE;
Packit 40b132
                if (currentStage == stageGET) {
Packit 40b132
                        method = "GET";
Packit 40b132
                } else {
Packit 40b132
                        PORT_Assert(currentStage == stagePOST);
Packit 40b132
                        method = "POST";
Packit 40b132
                }
Packit 40b132
Packit 40b132
                /* send request and create a response object */
Packit 40b132
                PKIX_CHECK_NO_GOTO(
Packit 40b132
                    pkix_pl_OcspResponse_Create(request, method, NULL,
Packit 40b132
                                                checker->certVerifyFcn,
Packit 40b132
                                                &nbioContext,
Packit 40b132
                                                &response,
Packit 40b132
                                                plContext),
Packit 40b132
                    PKIX_OCSPRESPONSECREATEFAILED);
Packit 40b132
Packit 40b132
                if (pkixErrorResult) {
Packit 40b132
                    passed = PKIX_FALSE;
Packit 40b132
                }
Packit 40b132
Packit 40b132
                if (passed && nbioContext != 0) {
Packit 40b132
                        *pNBIOContext = nbioContext;
Packit 40b132
                        goto cleanup;
Packit 40b132
                }
Packit 40b132
Packit 40b132
                if (passed){
Packit 40b132
                        PKIX_CHECK_NO_GOTO(
Packit 40b132
                            pkix_pl_OcspResponse_Decode(response, &passed,
Packit 40b132
                                                        &resultCode, plContext),
Packit 40b132
                            PKIX_OCSPRESPONSEDECODEFAILED);
Packit 40b132
                        if (pkixErrorResult) {
Packit 40b132
                            passed = PKIX_FALSE;
Packit 40b132
                        }
Packit 40b132
                }
Packit 40b132
                
Packit 40b132
                if (passed){
Packit 40b132
                        PKIX_CHECK_NO_GOTO(
Packit 40b132
                            pkix_pl_OcspResponse_GetStatus(response, &passed,
Packit 40b132
                                                           &resultCode, plContext),
Packit 40b132
                            PKIX_OCSPRESPONSEGETSTATUSRETURNEDANERROR);
Packit 40b132
                        if (pkixErrorResult) {
Packit 40b132
                            passed = PKIX_FALSE;
Packit 40b132
                        }
Packit 40b132
                }
Packit 40b132
Packit 40b132
                if (passed){
Packit 40b132
                        PKIX_CHECK_NO_GOTO(
Packit 40b132
                            pkix_pl_OcspResponse_VerifySignature(response, cert,
Packit 40b132
                                                                 procParams, &passed, 
Packit 40b132
                                                                 &nbioContext, plContext),
Packit 40b132
                            PKIX_OCSPRESPONSEVERIFYSIGNATUREFAILED);
Packit 40b132
                        if (pkixErrorResult) {
Packit 40b132
                            passed = PKIX_FALSE;
Packit 40b132
                        } else {
Packit 40b132
                                if (nbioContext != 0) {
Packit 40b132
                                        *pNBIOContext = nbioContext;
Packit 40b132
                                        goto cleanup;
Packit 40b132
                                }
Packit 40b132
                        }
Packit 40b132
                }
Packit 40b132
Packit 40b132
                if (!passed && currentStage == stagePOST) {
Packit 40b132
                        /* We won't retry a POST failure, so it's final.
Packit 40b132
                         * Because the following block with its call to
Packit 40b132
                         *   pkix_pl_OcspResponse_GetStatusForCert
Packit 40b132
                         * will take care of caching good or bad state,
Packit 40b132
                         * but we only execute that next block if there hasn't
Packit 40b132
                         * been a failure yet, we must cache the POST
Packit 40b132
                         * failure now.
Packit 40b132
                         */
Packit 40b132
                         
Packit 40b132
                        if (cid && cid->certID) {
Packit 40b132
                                /* Caching MIGHT consume the cid. */
Packit 40b132
                                PKIX_Error *err;
Packit 40b132
                                err = PKIX_PL_OcspCertID_RememberOCSPProcessingFailure(
Packit 40b132
                                        cid, plContext);
Packit 40b132
                                if (err) {
Packit 40b132
                                        PKIX_PL_Object_DecRef((PKIX_PL_Object*)err, plContext);
Packit 40b132
                                }
Packit 40b132
                        }
Packit 40b132
                }
Packit 40b132
Packit 40b132
                if (passed){
Packit 40b132
                        PKIX_Boolean allowCachingOfFailures =
Packit 40b132
                                (currentStage == stagePOST) ? PKIX_TRUE : PKIX_FALSE;
Packit 40b132
                        
Packit 40b132
                        PKIX_CHECK_NO_GOTO(
Packit 40b132
                            pkix_pl_OcspResponse_GetStatusForCert(cid, response,
Packit 40b132
                                                                  allowCachingOfFailures,
Packit 40b132
                                                                  date,
Packit 40b132
                                                                  &passed, &resultCode,
Packit 40b132
                                                                  plContext),
Packit 40b132
                            PKIX_OCSPRESPONSEGETSTATUSFORCERTFAILED);
Packit 40b132
                        if (pkixErrorResult) {
Packit 40b132
                            passed = PKIX_FALSE;
Packit 40b132
                        } else if (passed == PKIX_FALSE) {
Packit 40b132
                                revStatus = pkix_OcspChecker_MapResultCodeToRevStatus(resultCode);
Packit 40b132
                        } else {
Packit 40b132
                                revStatus = PKIX_RevStatus_Success;
Packit 40b132
                        }
Packit 40b132
                }
Packit 40b132
Packit 40b132
                if (currentStage == stageGET && revStatus != PKIX_RevStatus_Success &&
Packit 40b132
                                                revStatus != PKIX_RevStatus_Revoked) {
Packit 40b132
                        /* we'll retry */
Packit 40b132
                        PKIX_DECREF(response);
Packit 40b132
                        retry = PR_TRUE;
Packit 40b132
                        currentStage = stagePOST;
Packit 40b132
                        revStatus = PKIX_RevStatus_NoInfo;
Packit 40b132
                        if (pkixErrorResult) {
Packit 40b132
                                PKIX_PL_Object_DecRef((PKIX_PL_Object*)pkixErrorResult,
Packit 40b132
                                                      plContext);
Packit 40b132
                                pkixErrorResult = NULL;
Packit 40b132
                        }
Packit 40b132
                }
Packit 40b132
        } while (retry);
Packit 40b132
Packit 40b132
cleanup:
Packit 40b132
        if (revStatus == PKIX_RevStatus_NoInfo && (uriFound || 
Packit 40b132
	    methodFlags & PKIX_REV_M_REQUIRE_INFO_ON_MISSING_SOURCE) &&
Packit 40b132
            methodFlags & PKIX_REV_M_FAIL_ON_MISSING_FRESH_INFO) {
Packit 40b132
            revStatus = PKIX_RevStatus_Revoked;
Packit 40b132
        }
Packit 40b132
        *pRevStatus = revStatus;
Packit 40b132
Packit 40b132
        /* ocsp carries only three statuses: good, bad, and unknown.
Packit 40b132
         * revStatus is used to pass them. reasonCode is always set
Packit 40b132
         * to be unknown. */
Packit 40b132
        *pReasonCode = crlEntryReasonUnspecified;
Packit 40b132
Packit 40b132
        PKIX_DECREF(cid);
Packit 40b132
        PKIX_DECREF(request);
Packit 40b132
        PKIX_DECREF(response);
Packit 40b132
Packit 40b132
        PKIX_RETURN(OCSPCHECKER);
Packit 40b132
}
Packit 40b132
Packit 40b132