PageRenderTime 62ms CodeModel.GetById 15ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 1ms

/security/nss/lib/libpkix/pkix_pl_nss/pki/pkix_pl_certpolicyinfo.c

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