tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

pkix_pl_certpolicyinfo.c (10723B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 /*
      5 * pkix_pl_certpolicyinfo.c
      6 *
      7 * CertPolicyInfo Type Functions
      8 *
      9 */
     10 
     11 #include "pkix_pl_certpolicyinfo.h"
     12 
     13 /*
     14 * FUNCTION: pkix_pl_CertPolicyInfo_Create
     15 * DESCRIPTION:
     16 *
     17 *  Creates a new CertPolicyInfo Object using the OID pointed to by "oid" and
     18 *  the List of CertPolicyQualifiers pointed to by "qualifiers", and stores it
     19 *  at "pObject". If a non-NULL list is provided, the caller is expected to
     20 *  have already set it to be immutable. The caller may provide an empty List,
     21 *  but a NULL List is preferable so a user does not need to call
     22 *  List_GetLength to get the number of qualifiers.
     23 *
     24 * PARAMETERS
     25 *  "oid"
     26 *      OID of the desired PolicyInfo ID; must be non-NULL
     27 *  "qualifiers"
     28 *      List of CertPolicyQualifiers; may be NULL or empty
     29 *  "pObject"
     30 *      Address where object pointer will be stored. Must be non-NULL.
     31 *  "plContext"
     32 *      Platform-specific context pointer.
     33 * THREAD SAFETY:
     34 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
     35 * RETURNS:
     36 *  Returns NULL if the function succeeds.
     37 *  Returns a Fatal Error if the function fails in an unrecoverable way.
     38 */
     39 PKIX_Error *
     40 pkix_pl_CertPolicyInfo_Create(
     41        PKIX_PL_OID *oid,
     42        PKIX_List *qualifiers,
     43        PKIX_PL_CertPolicyInfo **pObject,
     44        void *plContext)
     45 {
     46        PKIX_PL_CertPolicyInfo *policyInfo = NULL;
     47 
     48        PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_Create");
     49 
     50        PKIX_NULLCHECK_TWO(oid, pObject);
     51 
     52        PKIX_CHECK(PKIX_PL_Object_Alloc
     53                (PKIX_CERTPOLICYINFO_TYPE,
     54                sizeof (PKIX_PL_CertPolicyInfo),
     55                (PKIX_PL_Object **)&policyInfo,
     56                plContext),
     57                PKIX_COULDNOTCREATECERTPOLICYINFOOBJECT);
     58 
     59        PKIX_INCREF(oid);
     60        policyInfo->cpID = oid;
     61 
     62        PKIX_INCREF(qualifiers);
     63        policyInfo->policyQualifiers = qualifiers;
     64 
     65        *pObject = policyInfo;
     66        policyInfo = NULL;
     67 
     68 cleanup:
     69        PKIX_DECREF(policyInfo);
     70 
     71        PKIX_RETURN(CERTPOLICYINFO);
     72 }
     73 
     74 /*
     75 * FUNCTION: pkix_pl_CertPolicyInfo_Destroy
     76 * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
     77 */
     78 static PKIX_Error *
     79 pkix_pl_CertPolicyInfo_Destroy(
     80        PKIX_PL_Object *object,
     81        void *plContext)
     82 {
     83        PKIX_PL_CertPolicyInfo *certPI = NULL;
     84 
     85        PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_Destroy");
     86 
     87        PKIX_NULLCHECK_ONE(object);
     88 
     89        PKIX_CHECK(pkix_CheckType(object, PKIX_CERTPOLICYINFO_TYPE, plContext),
     90                PKIX_OBJECTNOTCERTPOLICYINFO);
     91 
     92        certPI = (PKIX_PL_CertPolicyInfo*)object;
     93 
     94        PKIX_DECREF(certPI->cpID);
     95        PKIX_DECREF(certPI->policyQualifiers);
     96 
     97 cleanup:
     98 
     99        PKIX_RETURN(CERTPOLICYINFO);
    100 }
    101 
    102 /*
    103 * FUNCTION: pkix_pl_CertPolicyInfo_ToString
    104 * (see comments for PKIX_PL_ToStringCallback in pkix_pl_system.h)
    105 */
    106 static PKIX_Error *
    107 pkix_pl_CertPolicyInfo_ToString(
    108        PKIX_PL_Object *object,
    109        PKIX_PL_String **pString,
    110        void *plContext)
    111 {
    112        PKIX_PL_CertPolicyInfo *certPI = NULL;
    113        PKIX_PL_String *oidString = NULL;
    114        PKIX_PL_String *listString = NULL;
    115        PKIX_PL_String *format = NULL;
    116        PKIX_PL_String *outString = NULL;
    117 
    118        PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_ToString");
    119 
    120        PKIX_NULLCHECK_TWO(object, pString);
    121 
    122        PKIX_CHECK(pkix_CheckType(object, PKIX_CERTPOLICYINFO_TYPE, plContext),
    123                PKIX_OBJECTNOTCERTPOLICYINFO);
    124 
    125        certPI = (PKIX_PL_CertPolicyInfo *)object;
    126 
    127        PKIX_NULLCHECK_ONE(certPI->cpID);
    128 
    129        PKIX_TOSTRING
    130                (certPI->cpID,
    131                &oidString,
    132                plContext,
    133                PKIX_OIDTOSTRINGFAILED);
    134 
    135        PKIX_TOSTRING
    136                (certPI->policyQualifiers,
    137                &listString,
    138                plContext,
    139                PKIX_LISTTOSTRINGFAILED);
    140 
    141        /* Put them together in the form OID[Qualifiers] */
    142        PKIX_CHECK(PKIX_PL_String_Create
    143                (PKIX_ESCASCII, "%s[%s]", 0, &format, plContext),
    144                PKIX_ERRORINSTRINGCREATE);
    145 
    146        PKIX_CHECK(PKIX_PL_Sprintf
    147                (&outString, plContext, format, oidString, listString),
    148                PKIX_ERRORINSPRINTF);
    149 
    150        *pString = outString;
    151 
    152 cleanup:
    153 
    154        PKIX_DECREF(oidString);
    155        PKIX_DECREF(listString);
    156        PKIX_DECREF(format);
    157        PKIX_RETURN(CERTPOLICYINFO);
    158 }
    159 
    160 /*
    161 * FUNCTION: pkix_pl_CertPolicyInfo_Hashcode
    162 * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h)
    163 */
    164 static PKIX_Error *
    165 pkix_pl_CertPolicyInfo_Hashcode(
    166        PKIX_PL_Object *object,
    167        PKIX_UInt32 *pHashcode,
    168        void *plContext)
    169 {
    170        PKIX_PL_CertPolicyInfo *certPI = NULL;
    171        PKIX_UInt32 oidHash = 0;
    172        PKIX_UInt32 listHash = 0;
    173 
    174        PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_Hashcode");
    175 
    176        PKIX_NULLCHECK_TWO(object, pHashcode);
    177 
    178        PKIX_CHECK(pkix_CheckType(object, PKIX_CERTPOLICYINFO_TYPE, plContext),
    179                PKIX_OBJECTNOTCERTPOLICYINFO);
    180 
    181        certPI = (PKIX_PL_CertPolicyInfo *)object;
    182 
    183        PKIX_NULLCHECK_ONE(certPI->cpID);
    184 
    185        PKIX_HASHCODE
    186                (certPI->cpID,
    187                &oidHash,
    188                plContext,
    189                PKIX_ERRORINOIDHASHCODE);
    190 
    191        PKIX_HASHCODE
    192                (certPI->policyQualifiers,
    193                &listHash,
    194                plContext,
    195                PKIX_ERRORINLISTHASHCODE);
    196 
    197        *pHashcode = (31 * oidHash) + listHash;
    198 
    199 cleanup:
    200 
    201        PKIX_RETURN(CERTPOLICYINFO);
    202 }
    203 
    204 
    205 /*
    206 * FUNCTION: pkix_pl_CertPolicyInfo_Equals
    207 * (see comments for PKIX_PL_Equals_Callback in pkix_pl_system.h)
    208 */
    209 static PKIX_Error *
    210 pkix_pl_CertPolicyInfo_Equals(
    211        PKIX_PL_Object *firstObject,
    212        PKIX_PL_Object *secondObject,
    213        PKIX_Boolean *pResult,
    214        void *plContext)
    215 {
    216        PKIX_PL_CertPolicyInfo *firstCPI = NULL;
    217        PKIX_PL_CertPolicyInfo *secondCPI = NULL;
    218        PKIX_UInt32 secondType = 0;
    219        PKIX_Boolean compare = PKIX_FALSE;
    220 
    221        PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_Equals");
    222        PKIX_NULLCHECK_THREE(firstObject, secondObject, pResult);
    223 
    224        /* test that firstObject is a CertPolicyInfo */
    225        PKIX_CHECK(pkix_CheckType
    226                (firstObject, PKIX_CERTPOLICYINFO_TYPE, plContext),
    227                PKIX_FIRSTOBJECTNOTCERTPOLICYINFO);
    228 
    229        /*
    230         * Since we know firstObject is a CertPolicyInfo,
    231         * if both references are identical, they must be equal
    232         */
    233        if (firstObject == secondObject){
    234                *pResult = PKIX_TRUE;
    235                goto cleanup;
    236        }
    237 
    238        /*
    239         * If secondObject isn't a CertPolicyInfo, we
    240         * don't throw an error. We simply return FALSE.
    241         */
    242        PKIX_CHECK(PKIX_PL_Object_GetType
    243                (secondObject, &secondType, plContext),
    244                PKIX_COULDNOTGETTYPEOFSECONDARGUMENT);
    245        if (secondType != PKIX_CERTPOLICYINFO_TYPE) {
    246                *pResult = PKIX_FALSE;
    247                goto cleanup;
    248        }
    249 
    250        firstCPI = (PKIX_PL_CertPolicyInfo *)firstObject;
    251        secondCPI = (PKIX_PL_CertPolicyInfo *)secondObject;
    252 
    253        /*
    254         * Compare the value of the OID components
    255         */
    256 
    257        PKIX_NULLCHECK_TWO(firstCPI->cpID, secondCPI->cpID);
    258 
    259        PKIX_EQUALS
    260                (firstCPI->cpID,
    261                secondCPI->cpID,
    262                &compare,
    263                plContext,
    264                PKIX_OIDEQUALSFAILED);
    265 
    266        /*
    267         * If the OIDs did not match, we don't need to
    268         * compare the Lists. If the OIDs did match,
    269         * the return value is the value of the
    270         * List comparison.
    271         */
    272        if (compare) {
    273                PKIX_EQUALS
    274                        (firstCPI->policyQualifiers,
    275                        secondCPI->policyQualifiers,
    276                        &compare,
    277                        plContext,
    278                        PKIX_LISTEQUALSFAILED);
    279        }
    280 
    281        *pResult = compare;
    282 
    283 cleanup:
    284 
    285        PKIX_RETURN(CERTPOLICYINFO);
    286 }
    287 
    288 /*
    289 * FUNCTION: pkix_pl_CertPolicyInfo_RegisterSelf
    290 * DESCRIPTION:
    291 *  Registers PKIX_CERTPOLICYINFO_TYPE and its related
    292 *  functions with systemClasses[]
    293 * THREAD SAFETY:
    294 *  Not Thread Safe - for performance and complexity reasons
    295 *
    296 *  Since this function is only called by PKIX_PL_Initialize,
    297 *  which should only be called once, it is acceptable that
    298 *  this function is not thread-safe.
    299 */
    300 PKIX_Error *
    301 pkix_pl_CertPolicyInfo_RegisterSelf(void *plContext)
    302 {
    303        extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
    304        pkix_ClassTable_Entry entry;
    305 
    306        PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_RegisterSelf");
    307 
    308        entry.description = "CertPolicyInfo";
    309        entry.objCounter = 0;
    310        entry.typeObjectSize = sizeof(PKIX_PL_CertPolicyInfo);
    311        entry.destructor = pkix_pl_CertPolicyInfo_Destroy;
    312        entry.equalsFunction = pkix_pl_CertPolicyInfo_Equals;
    313        entry.hashcodeFunction = pkix_pl_CertPolicyInfo_Hashcode;
    314        entry.toStringFunction = pkix_pl_CertPolicyInfo_ToString;
    315        entry.comparator = NULL;
    316        entry.duplicateFunction = pkix_duplicateImmutable;
    317 
    318        systemClasses[PKIX_CERTPOLICYINFO_TYPE] = entry;
    319 
    320        PKIX_RETURN(CERTPOLICYINFO);
    321 }
    322 
    323 /* --Public-CertPolicyInfo-Functions------------------------- */
    324 
    325 /*
    326 * FUNCTION: PKIX_PL_CertPolicyInfo_GetPolicyId
    327 * (see comments in pkix_pl_pki.h)
    328 */
    329 PKIX_Error *
    330 PKIX_PL_CertPolicyInfo_GetPolicyId(
    331        PKIX_PL_CertPolicyInfo *policyInfo,
    332        PKIX_PL_OID **pPolicyId,
    333        void *plContext)
    334 {
    335        PKIX_ENTER(CERTPOLICYINFO, "PKIX_PL_CertPolicyInfo_GetPolicyId");
    336 
    337        PKIX_NULLCHECK_TWO(policyInfo, pPolicyId);
    338 
    339        PKIX_INCREF(policyInfo->cpID);
    340 
    341        *pPolicyId = policyInfo->cpID;
    342 
    343 cleanup:
    344        PKIX_RETURN(CERTPOLICYINFO);
    345 }
    346 
    347 /*
    348 * FUNCTION: PKIX_PL_CertPolicyInfo_GetPolQualifiers
    349 * (see comments in pkix_pl_pki.h)
    350 */
    351 PKIX_Error *
    352 PKIX_PL_CertPolicyInfo_GetPolQualifiers(
    353        PKIX_PL_CertPolicyInfo *policyInfo,
    354        PKIX_List **pQuals,
    355        void *plContext)
    356 {
    357        PKIX_ENTER(CERTPOLICYINFO, "PKIX_PL_CertPolicyInfo_GetPolQualifiers");
    358 
    359        PKIX_NULLCHECK_TWO(policyInfo, pQuals);
    360 
    361        PKIX_INCREF(policyInfo->policyQualifiers);
    362 
    363        /*
    364         * This List is created in PKIX_PL_Cert_DecodePolicyInfo
    365         * and is set immutable immediately after being created.
    366         */
    367        *pQuals = policyInfo->policyQualifiers;
    368 
    369 cleanup:
    370        PKIX_RETURN(CERTPOLICYINFO);
    371 }