nss: import at 3.0.1 beta 1
[mozilla-nss.git] / security / nss / lib / libpkix / pkix_pl_nss / pki / pkix_pl_certpolicyinfo.c
blob97f5e934e68ed9ff028603ceb45f502b2ea5f30c
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
14 * The Original Code is the PKIX-C library.
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.
21 * Contributor(s):
22 * Sun Microsystems, Inc.
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.
36 * ***** END LICENSE BLOCK ***** */
38 * pkix_pl_certpolicyinfo.c
40 * CertPolicyInfo Type Functions
44 #include "pkix_pl_certpolicyinfo.h"
47 * FUNCTION: pkix_pl_CertPolicyInfo_Create
48 * DESCRIPTION:
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.
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.
72 PKIX_Error *
73 pkix_pl_CertPolicyInfo_Create(
74 PKIX_PL_OID *oid,
75 PKIX_List *qualifiers,
76 PKIX_PL_CertPolicyInfo **pObject,
77 void *plContext)
79 PKIX_PL_CertPolicyInfo *policyInfo = NULL;
81 PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_Create");
83 PKIX_NULLCHECK_TWO(oid, pObject);
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);
92 PKIX_INCREF(oid);
93 policyInfo->cpID = oid;
95 PKIX_INCREF(qualifiers);
96 policyInfo->policyQualifiers = qualifiers;
98 *pObject = policyInfo;
99 policyInfo = NULL;
101 cleanup:
102 PKIX_DECREF(policyInfo);
104 PKIX_RETURN(CERTPOLICYINFO);
108 * FUNCTION: pkix_pl_CertPolicyInfo_Destroy
109 * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
111 static PKIX_Error *
112 pkix_pl_CertPolicyInfo_Destroy(
113 PKIX_PL_Object *object,
114 void *plContext)
116 PKIX_PL_CertPolicyInfo *certPI = NULL;
118 PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_Destroy");
120 PKIX_NULLCHECK_ONE(object);
122 PKIX_CHECK(pkix_CheckType(object, PKIX_CERTPOLICYINFO_TYPE, plContext),
123 PKIX_OBJECTNOTCERTPOLICYINFO);
125 certPI = (PKIX_PL_CertPolicyInfo*)object;
127 PKIX_DECREF(certPI->cpID);
128 PKIX_DECREF(certPI->policyQualifiers);
130 cleanup:
132 PKIX_RETURN(CERTPOLICYINFO);
136 * FUNCTION: pkix_pl_CertPolicyInfo_ToString
137 * (see comments for PKIX_PL_ToStringCallback in pkix_pl_system.h)
139 static PKIX_Error *
140 pkix_pl_CertPolicyInfo_ToString(
141 PKIX_PL_Object *object,
142 PKIX_PL_String **pString,
143 void *plContext)
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;
151 PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_ToString");
153 PKIX_NULLCHECK_TWO(object, pString);
155 PKIX_CHECK(pkix_CheckType(object, PKIX_CERTPOLICYINFO_TYPE, plContext),
156 PKIX_OBJECTNOTCERTPOLICYINFO);
158 certPI = (PKIX_PL_CertPolicyInfo *)object;
160 PKIX_NULLCHECK_ONE(certPI->cpID);
162 PKIX_TOSTRING
163 (certPI->cpID,
164 &oidString,
165 plContext,
166 PKIX_OIDTOSTRINGFAILED);
168 PKIX_TOSTRING
169 (certPI->policyQualifiers,
170 &listString,
171 plContext,
172 PKIX_LISTTOSTRINGFAILED);
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);
179 PKIX_CHECK(PKIX_PL_Sprintf
180 (&outString, plContext, format, oidString, listString),
181 PKIX_ERRORINSPRINTF);
183 *pString = outString;
185 cleanup:
187 PKIX_DECREF(oidString);
188 PKIX_DECREF(listString);
189 PKIX_DECREF(format);
190 PKIX_RETURN(CERTPOLICYINFO);
194 * FUNCTION: pkix_pl_CertPolicyInfo_Hashcode
195 * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h)
197 static PKIX_Error *
198 pkix_pl_CertPolicyInfo_Hashcode(
199 PKIX_PL_Object *object,
200 PKIX_UInt32 *pHashcode,
201 void *plContext)
203 PKIX_PL_CertPolicyInfo *certPI = NULL;
204 PKIX_UInt32 oidHash = 0;
205 PKIX_UInt32 listHash = 0;
207 PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_Hashcode");
209 PKIX_NULLCHECK_TWO(object, pHashcode);
211 PKIX_CHECK(pkix_CheckType(object, PKIX_CERTPOLICYINFO_TYPE, plContext),
212 PKIX_OBJECTNOTCERTPOLICYINFO);
214 certPI = (PKIX_PL_CertPolicyInfo *)object;
216 PKIX_NULLCHECK_ONE(certPI->cpID);
218 PKIX_HASHCODE
219 (certPI->cpID,
220 &oidHash,
221 plContext,
222 PKIX_ERRORINOIDHASHCODE);
224 PKIX_HASHCODE
225 (certPI->policyQualifiers,
226 &listHash,
227 plContext,
228 PKIX_ERRORINLISTHASHCODE);
230 *pHashcode = (31 * oidHash) + listHash;
232 cleanup:
234 PKIX_RETURN(CERTPOLICYINFO);
239 * FUNCTION: pkix_pl_CertPolicyInfo_Equals
240 * (see comments for PKIX_PL_Equals_Callback in pkix_pl_system.h)
242 static PKIX_Error *
243 pkix_pl_CertPolicyInfo_Equals(
244 PKIX_PL_Object *firstObject,
245 PKIX_PL_Object *secondObject,
246 PKIX_Boolean *pResult,
247 void *plContext)
249 PKIX_PL_CertPolicyInfo *firstCPI = NULL;
250 PKIX_PL_CertPolicyInfo *secondCPI = NULL;
251 PKIX_UInt32 secondType = 0;
252 PKIX_Boolean compare = PKIX_FALSE;
254 PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_Equals");
255 PKIX_NULLCHECK_THREE(firstObject, secondObject, pResult);
257 /* test that firstObject is a CertPolicyInfo */
258 PKIX_CHECK(pkix_CheckType
259 (firstObject, PKIX_CERTPOLICYINFO_TYPE, plContext),
260 PKIX_FIRSTOBJECTNOTCERTPOLICYINFO);
263 * Since we know firstObject is a CertPolicyInfo,
264 * if both references are identical, they must be equal
266 if (firstObject == secondObject){
267 *pResult = PKIX_TRUE;
268 goto cleanup;
272 * If secondObject isn't a CertPolicyInfo, we
273 * don't throw an error. We simply return FALSE.
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;
283 firstCPI = (PKIX_PL_CertPolicyInfo *)firstObject;
284 secondCPI = (PKIX_PL_CertPolicyInfo *)secondObject;
287 * Compare the value of the OID components
290 PKIX_NULLCHECK_TWO(firstCPI->cpID, secondCPI->cpID);
292 PKIX_EQUALS
293 (firstCPI->cpID,
294 secondCPI->cpID,
295 &compare,
296 plContext,
297 PKIX_OIDEQUALSFAILED);
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.
305 if (compare) {
306 PKIX_EQUALS
307 (firstCPI->policyQualifiers,
308 secondCPI->policyQualifiers,
309 &compare,
310 plContext,
311 PKIX_LISTEQUALSFAILED);
314 *pResult = compare;
316 cleanup:
318 PKIX_RETURN(CERTPOLICYINFO);
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
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.
333 PKIX_Error *
334 pkix_pl_CertPolicyInfo_RegisterSelf(void *plContext)
336 extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
337 pkix_ClassTable_Entry entry;
339 PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_RegisterSelf");
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;
351 systemClasses[PKIX_CERTPOLICYINFO_TYPE] = entry;
353 PKIX_RETURN(CERTPOLICYINFO);
356 /* --Public-CertPolicyInfo-Functions------------------------- */
359 * FUNCTION: PKIX_PL_CertPolicyInfo_GetPolicyId
360 * (see comments in pkix_pl_pki.h)
362 PKIX_Error *
363 PKIX_PL_CertPolicyInfo_GetPolicyId(
364 PKIX_PL_CertPolicyInfo *policyInfo,
365 PKIX_PL_OID **pPolicyId,
366 void *plContext)
368 PKIX_ENTER(CERTPOLICYINFO, "PKIX_PL_CertPolicyInfo_GetPolicyId");
370 PKIX_NULLCHECK_TWO(policyInfo, pPolicyId);
372 PKIX_INCREF(policyInfo->cpID);
374 *pPolicyId = policyInfo->cpID;
376 cleanup:
377 PKIX_RETURN(CERTPOLICYINFO);
381 * FUNCTION: PKIX_PL_CertPolicyInfo_GetPolQualifiers
382 * (see comments in pkix_pl_pki.h)
384 PKIX_Error *
385 PKIX_PL_CertPolicyInfo_GetPolQualifiers(
386 PKIX_PL_CertPolicyInfo *policyInfo,
387 PKIX_List **pQuals,
388 void *plContext)
390 PKIX_ENTER(CERTPOLICYINFO, "PKIX_PL_CertPolicyInfo_GetPolQualifiers");
392 PKIX_NULLCHECK_TWO(policyInfo, pQuals);
394 PKIX_INCREF(policyInfo->policyQualifiers);
397 * This List is created in PKIX_PL_Cert_DecodePolicyInfo
398 * and is set immutable immediately after being created.
400 *pQuals = policyInfo->policyQualifiers;
402 cleanup:
403 PKIX_RETURN(CERTPOLICYINFO);