nss: import at 3.0.1 beta 1
[mozilla-nss.git] / security / nss / lib / libpkix / pkix_pl_nss / pki / pkix_pl_certpolicymap.c
blobcd0b7e0550e18ec7dc48ff95a0320c1d318aa207
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_certpolicymap.c
40 * CertPolicyMap Type Functions
44 #include "pkix_pl_certpolicymap.h"
47 * FUNCTION: pkix_pl_CertPolicyMap_Create
48 * DESCRIPTION:
50 * Creates a new CertPolicyMap Object pairing the OID given by
51 * "issuerDomainPolicy" with the OID given by "subjectDomainPolicy", and
52 * stores the result at "pCertPolicyMap".
54 * PARAMETERS
55 * "issuerDomainPolicy"
56 * Address of the OID of the IssuerDomainPolicy. Must be non-NULL.
57 * "subjectDomainPolicy"
58 * Address of the OID of the SubjectDomainPolicy. Must be non-NULL.
59 * "pCertPolicyMap"
60 * Address where CertPolicyMap pointer will be stored. Must be non-NULL.
61 * "plContext"
62 * Platform-specific context pointer.
63 * THREAD SAFETY:
64 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
65 * RETURNS:
66 * Returns NULL if the function succeeds.
67 * Returns a CertPolicyMap Error if the function fails in a non-fatal way.
68 * Returns a Fatal Error if the function fails in an unrecoverable way.
70 PKIX_Error *
71 pkix_pl_CertPolicyMap_Create(
72 PKIX_PL_OID *issuerDomainPolicy,
73 PKIX_PL_OID *subjectDomainPolicy,
74 PKIX_PL_CertPolicyMap **pCertPolicyMap,
75 void *plContext)
77 PKIX_PL_CertPolicyMap *policyMap = NULL;
79 PKIX_ENTER(CERTPOLICYMAP, "pkix_pl_CertPolicyMap_Create");
81 PKIX_NULLCHECK_THREE
82 (issuerDomainPolicy, subjectDomainPolicy, pCertPolicyMap);
84 PKIX_CHECK(PKIX_PL_Object_Alloc
85 (PKIX_CERTPOLICYMAP_TYPE,
86 sizeof (PKIX_PL_CertPolicyMap),
87 (PKIX_PL_Object **)&policyMap,
88 plContext),
89 PKIX_COULDNOTCREATECERTPOLICYMAPOBJECT);
91 PKIX_INCREF(issuerDomainPolicy);
92 policyMap->issuerDomainPolicy = issuerDomainPolicy;
94 PKIX_INCREF(subjectDomainPolicy);
95 policyMap->subjectDomainPolicy = subjectDomainPolicy;
97 *pCertPolicyMap = policyMap;
99 cleanup:
101 PKIX_RETURN(CERTPOLICYMAP);
105 * FUNCTION: pkix_pl_CertPolicyMap_Destroy
106 * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
108 static PKIX_Error *
109 pkix_pl_CertPolicyMap_Destroy(
110 PKIX_PL_Object *object,
111 void *plContext)
113 PKIX_PL_CertPolicyMap *certMap = NULL;
115 PKIX_ENTER(CERTPOLICYMAP, "pkix_pl_CertPolicyMap_Destroy");
117 PKIX_NULLCHECK_ONE(object);
119 PKIX_CHECK(pkix_CheckType(object, PKIX_CERTPOLICYMAP_TYPE, plContext),
120 PKIX_OBJECTNOTCERTPOLICYMAP);
122 certMap = (PKIX_PL_CertPolicyMap*)object;
124 PKIX_DECREF(certMap->issuerDomainPolicy);
125 PKIX_DECREF(certMap->subjectDomainPolicy);
127 cleanup:
129 PKIX_RETURN(CERTPOLICYMAP);
133 * FUNCTION: pkix_pl_CertPolicyMap_ToString
134 * (see comments for PKIX_PL_ToStringCallback in pkix_pl_system.h)
136 static PKIX_Error *
137 pkix_pl_CertPolicyMap_ToString(
138 PKIX_PL_Object *object,
139 PKIX_PL_String **pString,
140 void *plContext)
142 PKIX_PL_CertPolicyMap *certMap = NULL;
143 PKIX_PL_String *format = NULL;
144 PKIX_PL_String *outString = NULL;
145 PKIX_PL_String *issuerString = NULL;
146 PKIX_PL_String *subjectString = NULL;
148 PKIX_ENTER(CERTPOLICYMAP, "pkix_pl_CertPolicyMap_ToString");
150 PKIX_NULLCHECK_TWO(object, pString);
152 PKIX_CHECK(pkix_CheckType(object, PKIX_CERTPOLICYMAP_TYPE, plContext),
153 PKIX_OBJECTNOTCERTPOLICYMAP);
155 certMap = (PKIX_PL_CertPolicyMap *)object;
157 PKIX_TOSTRING
158 (certMap->issuerDomainPolicy,
159 &issuerString,
160 plContext,
161 PKIX_OBJECTTOSTRINGFAILED);
163 PKIX_TOSTRING
164 (certMap->subjectDomainPolicy,
165 &subjectString,
166 plContext,
167 PKIX_OBJECTTOSTRINGFAILED);
169 /* Put them together in the form issuerPolicy=>subjectPolicy */
170 PKIX_CHECK(PKIX_PL_String_Create
171 (PKIX_ESCASCII, "%s=>%s", 0, &format, plContext),
172 PKIX_ERRORINSTRINGCREATE);
174 PKIX_CHECK(PKIX_PL_Sprintf
175 (&outString, plContext, format, issuerString, subjectString),
176 PKIX_ERRORINSPRINTF);
178 *pString = outString;
180 cleanup:
181 PKIX_DECREF(format);
182 PKIX_DECREF(issuerString);
183 PKIX_DECREF(subjectString);
185 PKIX_RETURN(CERTPOLICYMAP);
189 * FUNCTION: pkix_pl_CertPolicyMap_Hashcode
190 * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h)
192 static PKIX_Error *
193 pkix_pl_CertPolicyMap_Hashcode(
194 PKIX_PL_Object *object,
195 PKIX_UInt32 *pHashcode,
196 void *plContext)
198 PKIX_UInt32 issuerHash = 0;
199 PKIX_UInt32 subjectHash = 0;
200 PKIX_PL_CertPolicyMap *certMap = NULL;
202 PKIX_ENTER(CERTPOLICYMAP, "pkix_pl_CertPolicyMap_Hashcode");
204 PKIX_NULLCHECK_TWO(object, pHashcode);
206 PKIX_CHECK(pkix_CheckType(object, PKIX_CERTPOLICYMAP_TYPE, plContext),
207 PKIX_OBJECTNOTCERTPOLICYMAP);
209 certMap = (PKIX_PL_CertPolicyMap *)object;
211 PKIX_HASHCODE
212 (certMap->issuerDomainPolicy,
213 &issuerHash,
214 plContext,
215 PKIX_OBJECTHASHCODEFAILED);
217 PKIX_HASHCODE
218 (certMap->subjectDomainPolicy,
219 &subjectHash,
220 plContext,
221 PKIX_OBJECTHASHCODEFAILED);
223 *pHashcode = issuerHash*31 + subjectHash;
225 cleanup:
227 PKIX_RETURN(CERTPOLICYMAP);
231 * FUNCTION: pkix_pl_CertPolicyMap_Equals
232 * (see comments for PKIX_PL_Equals_Callback in pkix_pl_system.h)
234 static PKIX_Error *
235 pkix_pl_CertPolicyMap_Equals(
236 PKIX_PL_Object *firstObject,
237 PKIX_PL_Object *secondObject,
238 PKIX_Boolean *pResult,
239 void *plContext)
241 PKIX_PL_CertPolicyMap *firstCertMap = NULL;
242 PKIX_PL_CertPolicyMap *secondCertMap = NULL;
243 PKIX_UInt32 secondType = 0;
244 PKIX_Boolean compare = PKIX_FALSE;
246 PKIX_ENTER(CERTPOLICYMAP, "pkix_pl_CertPolicyMap_Equals");
247 PKIX_NULLCHECK_THREE(firstObject, secondObject, pResult);
249 /* test that firstObject is a CertPolicyMap */
250 PKIX_CHECK(pkix_CheckType
251 (firstObject, PKIX_CERTPOLICYMAP_TYPE, plContext),
252 PKIX_FIRSTOBJECTNOTCERTPOLICYMAP);
255 * Since we know firstObject is a CertPolicyMap,
256 * if both references are identical, they must be equal
258 if (firstObject == secondObject){
259 *pResult = PKIX_TRUE;
260 goto cleanup;
264 * If secondObject isn't a CertPolicyMap, we
265 * don't throw an error. We simply return FALSE.
267 PKIX_CHECK(PKIX_PL_Object_GetType
268 (secondObject, &secondType, plContext),
269 PKIX_COULDNOTGETTYPEOFSECONDARGUMENT);
270 if (secondType != PKIX_CERTPOLICYMAP_TYPE) {
271 *pResult = PKIX_FALSE;
272 goto cleanup;
275 firstCertMap = (PKIX_PL_CertPolicyMap *)firstObject;
276 secondCertMap = (PKIX_PL_CertPolicyMap *)secondObject;
278 PKIX_EQUALS
279 (firstCertMap->issuerDomainPolicy,
280 secondCertMap->issuerDomainPolicy,
281 &compare,
282 plContext,
283 PKIX_OBJECTEQUALSFAILED);
285 if (compare) {
286 PKIX_EQUALS
287 (firstCertMap->subjectDomainPolicy,
288 secondCertMap->subjectDomainPolicy,
289 &compare,
290 plContext,
291 PKIX_OBJECTEQUALSFAILED);
294 *pResult = compare;
296 cleanup:
298 PKIX_RETURN(CERTPOLICYMAP);
302 * FUNCTION: pkix_pl_CertPolicyMap_Duplicate
303 * (see comments for PKIX_PL_Duplicate_Callback in pkix_pl_system.h)
305 static PKIX_Error *
306 pkix_pl_CertPolicyMap_Duplicate(
307 PKIX_PL_Object *object,
308 PKIX_PL_Object **pNewObject,
309 void *plContext)
311 PKIX_PL_CertPolicyMap *original = NULL;
312 PKIX_PL_CertPolicyMap *copy = NULL;
314 PKIX_ENTER(CERTPOLICYMAP, "pkix_pl_CertPolicyMap_Duplicate");
316 PKIX_NULLCHECK_TWO(object, pNewObject);
318 PKIX_CHECK(pkix_CheckType
319 (object, PKIX_CERTPOLICYMAP_TYPE, plContext),
320 PKIX_OBJECTARGUMENTNOTPOLICYMAP);
322 original = (PKIX_PL_CertPolicyMap *)object;
324 PKIX_CHECK(pkix_pl_CertPolicyMap_Create
325 (original->issuerDomainPolicy,
326 original->subjectDomainPolicy,
327 &copy,
328 plContext),
329 PKIX_CERTPOLICYMAPCREATEFAILED);
331 *pNewObject = (PKIX_PL_Object *)copy;
333 cleanup:
335 PKIX_RETURN(CERTPOLICYMAP);
339 * FUNCTION: pkix_pl_CertPolicyMap_RegisterSelf
340 * DESCRIPTION:
341 * Registers PKIX_CERTPOLICYMAP_TYPE and its related
342 * functions with systemClasses[]
343 * THREAD SAFETY:
344 * Not Thread Safe - for performance and complexity reasons
346 * Since this function is only called by PKIX_PL_Initialize,
347 * which should only be called once, it is acceptable that
348 * this function is not thread-safe.
350 PKIX_Error *
351 pkix_pl_CertPolicyMap_RegisterSelf(void *plContext)
353 extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
354 pkix_ClassTable_Entry entry;
356 PKIX_ENTER(CERTPOLICYMAP, "pkix_pl_CertPolicyMap_RegisterSelf");
358 entry.description = "CertPolicyMap";
359 entry.objCounter = 0;
360 entry.typeObjectSize = sizeof(PKIX_PL_CertPolicyMap);
361 entry.destructor = pkix_pl_CertPolicyMap_Destroy;
362 entry.equalsFunction = pkix_pl_CertPolicyMap_Equals;
363 entry.hashcodeFunction = pkix_pl_CertPolicyMap_Hashcode;
364 entry.toStringFunction = pkix_pl_CertPolicyMap_ToString;
365 entry.comparator = NULL;
366 entry.duplicateFunction = pkix_pl_CertPolicyMap_Duplicate;
368 systemClasses[PKIX_CERTPOLICYMAP_TYPE] = entry;
370 PKIX_RETURN(CERTPOLICYMAP);
373 /* --Public-CertPolicyMap-Functions------------------------- */
376 * FUNCTION: PKIX_PL_CertPolicyMap_GetIssuerDomainPolicy
377 * (see comments in pkix_pl_pki.h)
379 PKIX_Error *
380 PKIX_PL_CertPolicyMap_GetIssuerDomainPolicy(
381 PKIX_PL_CertPolicyMap *policyMapping,
382 PKIX_PL_OID **pIssuerDomainPolicy,
383 void *plContext)
385 PKIX_ENTER
386 (CERTPOLICYMAP, "PKIX_PL_CertPolicyMap_GetIssuerDomainPolicy");
388 PKIX_NULLCHECK_TWO(policyMapping, pIssuerDomainPolicy);
390 PKIX_INCREF(policyMapping->issuerDomainPolicy);
391 *pIssuerDomainPolicy = policyMapping->issuerDomainPolicy;
393 cleanup:
394 PKIX_RETURN(CERTPOLICYMAP);
398 * FUNCTION: PKIX_PL_CertPolicyMap_GetSubjectDomainPolicy
399 * (see comments in pkix_pl_pki.h)
401 PKIX_Error *
402 PKIX_PL_CertPolicyMap_GetSubjectDomainPolicy(
403 PKIX_PL_CertPolicyMap *policyMapping,
404 PKIX_PL_OID **pSubjectDomainPolicy,
405 void *plContext)
407 PKIX_ENTER
408 (CERTPOLICYMAP, "PKIX_PL_CertPolicyMap_GetSubjectDomainPolicy");
410 PKIX_NULLCHECK_TWO(policyMapping, pSubjectDomainPolicy);
412 PKIX_INCREF(policyMapping->subjectDomainPolicy);
413 *pSubjectDomainPolicy = policyMapping->subjectDomainPolicy;
415 cleanup:
416 PKIX_RETURN(CERTPOLICYMAP);