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
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
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 ***** */
40 * X500Name Object Functions
44 #include "pkix_pl_x500name.h"
46 /* --Private-X500Name-Functions------------------------------------- */
49 * FUNCTION: pkix_pl_X500Name_CompareDERBytes
52 * Checks whether the DER encoding of the X500Name pointed to by
53 * "firstX500Name" is byte-for-byte equal with the DER encoding of the
54 * X500Name pointed to by "secondX500Name" and stores the Boolean result at
59 * Address of first X500Name. Must be non-NULL.
61 * Address of second X500Name. Must be non-NULL.
63 * Address where Boolean will be stored. Must be non-NULL.
65 * Platform-specific context pointer.
67 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
69 * Returns NULL if the function succeeds.
70 * Returns a X500Name Error if the function fails in a non-fatal way.
71 * Returns a Fatal Error if the function fails in an unrecoverable way.
74 pkix_pl_X500Name_CompareDERBytes(
75 PKIX_PL_X500Name
*firstX500Name
,
76 PKIX_PL_X500Name
*secondX500Name
,
77 PKIX_Boolean
*pResult
,
80 CERTName
*firstName
= NULL
;
81 CERTName
*secondName
= NULL
;
82 CERTRDN
**ardns
= NULL
;
83 CERTRDN
**brdns
= NULL
;
86 CERTAVA
**aavas
= NULL
;
87 CERTAVA
**bavas
= NULL
;
93 PKIX_ENTER(X500NAME
, "pkix_pl_X500Name_CompareDERBytes");
94 PKIX_NULLCHECK_THREE(firstX500Name
, secondX500Name
, pResult
);
96 firstName
= firstX500Name
->nssDN
;
97 secondName
= secondX500Name
->nssDN
;
99 PKIX_NULLCHECK_TWO(firstName
, secondName
);
101 ardns
= firstName
->rdns
;
102 brdns
= secondName
->rdns
;
104 /* if array of rdn's not same length, names can't be equal */
105 ac
= pkix_countArray((void**) ardns
);
106 bc
= pkix_countArray((void**) brdns
);
108 *pResult
= PKIX_FALSE
;
113 PKIX_NULLCHECK_TWO(ardns
, brdns
);
120 PKIX_NULLCHECK_TWO(ardn
, brdn
);
124 /* if array of ava's not same length, names can't be equal */
125 ac
= pkix_countArray((void**) aavas
);
126 bc
= pkix_countArray((void**) bavas
);
128 *pResult
= PKIX_FALSE
;
133 PKIX_NULLCHECK_TWO(aavas
, bavas
);
140 PKIX_X500NAME_DEBUG("\t\tCalling "
141 "SECITEM_CompareItem).\n");
142 rv
= SECITEM_CompareItem(&aava
->type
, &bava
->type
);
144 *pResult
= PKIX_FALSE
;
148 PKIX_X500NAME_DEBUG("\t\tCalling "
149 "SECITEM_CompareItem).\n");
150 rv
= SECITEM_CompareItem(&aava
->value
, &bava
->value
);
152 *pResult
= PKIX_FALSE
;
158 *pResult
= (rv
== SECEqual
);
162 PKIX_RETURN(X500NAME
);
166 * FUNCTION: pkix_pl_X500Name_ToString_Helper
169 * Helper function that creates a string representation of the X500Name
170 * pointed to by "name" and stores it at "pString".
174 * Address of X500Name whose string representation is desired.
177 * Address where object pointer will be stored. Must be non-NULL.
178 * "plContext" - Platform-specific context pointer.
180 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
182 * Returns NULL if the function succeeds.
183 * Returns a X500Name Error if the function fails in a non-fatal way.
184 * Returns a Fatal Error if the function fails in an unrecoverable way.
187 pkix_pl_X500Name_ToString_Helper(
188 PKIX_PL_X500Name
*name
,
189 PKIX_PL_String
**pString
,
192 CERTName
*nssDN
= NULL
;
193 char *utf8String
= NULL
;
194 PKIX_UInt32 utf8Length
;
196 PKIX_ENTER(X500NAME
, "pkix_pl_X500Name_ToString_Helper");
197 PKIX_NULLCHECK_TWO(name
, pString
);
200 PKIX_X500NAME_DEBUG("\t\tCalling CERT_NameToAscii).\n");
201 /* this should really be called CERT_NameToUTF8 */
202 utf8String
= CERT_NameToAscii(nssDN
);
204 PKIX_ERROR(PKIX_CERTNAMETOASCIIFAILED
);
207 PKIX_X500NAME_DEBUG("\t\tCalling PL_strlen).\n");
208 utf8Length
= PL_strlen(utf8String
);
210 PKIX_CHECK(PKIX_PL_String_Create
211 (PKIX_UTF8
, utf8String
, utf8Length
, pString
, plContext
),
212 PKIX_STRINGCREATEFAILED
);
218 PKIX_RETURN(X500NAME
);
222 * FUNCTION: pkix_pl_X500Name_Destroy
223 * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
226 pkix_pl_X500Name_Destroy(
227 PKIX_PL_Object
*object
,
230 PKIX_PL_X500Name
*name
= NULL
;
232 PKIX_ENTER(X500NAME
, "pkix_pl_X500Name_Destroy");
233 PKIX_NULLCHECK_ONE(object
);
235 PKIX_CHECK(pkix_CheckType(object
, PKIX_X500NAME_TYPE
, plContext
),
236 PKIX_OBJECTNOTANX500NAME
);
238 name
= (PKIX_PL_X500Name
*)object
;
240 PKIX_X500NAME_DEBUG("\t\tCalling CERT_DestroyName).\n");
241 CERT_DestroyName(name
->nssDN
);
246 PKIX_RETURN(X500NAME
);
250 * FUNCTION: pkix_pl_X500Name_ToString
251 * (see comments for PKIX_PL_ToStringCallback in pkix_pl_system.h)
254 pkix_pl_X500Name_ToString(
255 PKIX_PL_Object
*object
,
256 PKIX_PL_String
**pString
,
259 PKIX_PL_String
*nameString
= NULL
;
260 PKIX_PL_X500Name
*name
= NULL
;
262 PKIX_ENTER(X500NAME
, "pkix_pl_X500Name_toString");
263 PKIX_NULLCHECK_TWO(object
, pString
);
265 PKIX_CHECK(pkix_CheckType(object
, PKIX_X500NAME_TYPE
, plContext
),
266 PKIX_OBJECTNOTANX500NAME
);
268 name
= (PKIX_PL_X500Name
*)object
;
270 PKIX_CHECK(pkix_pl_X500Name_ToString_Helper
271 (name
, &nameString
, plContext
),
272 PKIX_X500NAMETOSTRINGHELPERFAILED
);
274 *pString
= nameString
;
278 PKIX_RETURN(X500NAME
);
282 * FUNCTION: pkix_pl_X500Name_Hashcode
283 * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h)
286 pkix_pl_X500Name_Hashcode(
287 PKIX_PL_Object
*object
,
288 PKIX_UInt32
*pHashcode
,
291 PKIX_PL_X500Name
*name
= NULL
;
292 SECItem
*resultSecItem
= NULL
;
293 PRArenaPool
*arena
= NULL
;
294 CERTName
*nssDN
= NULL
;
295 PKIX_UInt32 nameHash
;
298 PKIX_ENTER(X500NAME
, "pkix_pl_X500Name_Hashcode");
299 PKIX_NULLCHECK_TWO(object
, pHashcode
);
301 PKIX_CHECK(pkix_CheckType(object
, PKIX_X500NAME_TYPE
, plContext
),
302 PKIX_OBJECTNOTANX500NAME
);
304 name
= (PKIX_PL_X500Name
*)object
;
306 /* we hash over the bytes in the DER encoding */
310 PKIX_X500NAME_DEBUG("\t\tCalling PORT_NewArena).\n");
311 arena
= PORT_NewArena(DER_DEFAULT_CHUNKSIZE
);
313 PKIX_ERROR(PKIX_PORTNEWARENAFAILED
);
316 PKIX_X500NAME_DEBUG("\t\tCalling PORT_ArenaZNew).\n");
317 derBytes
= PORT_ArenaZNew(arena
, SECItem
);
318 if (derBytes
== NULL
) {
319 PKIX_ERROR(PKIX_PORTARENAZNEWFAILED
);
322 PKIX_X500NAME_DEBUG("\t\tCalling SEC_ASN1EncodeItem).\n");
324 SEC_ASN1EncodeItem(arena
, derBytes
, nssDN
, CERT_NameTemplate
);
326 if (resultSecItem
== NULL
){
327 PKIX_ERROR(PKIX_SECASN1ENCODEITEMFAILED
);
331 (derBytes
->data
, derBytes
->len
, &nameHash
, plContext
),
334 *pHashcode
= nameHash
;
339 /* Note that freeing the arena also frees derBytes */
340 PKIX_X500NAME_DEBUG("\t\tCalling PORT_FreeArena).\n");
341 PORT_FreeArena(arena
, PR_FALSE
);
344 PKIX_RETURN(X500NAME
);
349 * FUNCTION: pkix_pl_X500Name_Equals
350 * (see comments for PKIX_PL_Equals_Callback in pkix_pl_system.h)
353 pkix_pl_X500Name_Equals(
354 PKIX_PL_Object
*firstObject
,
355 PKIX_PL_Object
*secondObject
,
356 PKIX_Boolean
*pResult
,
359 PKIX_PL_X500Name
*firstX500Name
= NULL
;
360 PKIX_PL_X500Name
*secondX500Name
= NULL
;
361 PKIX_UInt32 secondType
;
363 PKIX_ENTER(X500NAME
, "pkix_pl_X500Name_Equals");
364 PKIX_NULLCHECK_THREE(firstObject
, secondObject
, pResult
);
366 /* test that firstObject is an X500Name */
367 PKIX_CHECK(pkix_CheckType(firstObject
, PKIX_X500NAME_TYPE
, plContext
),
368 PKIX_FIRSTOBJECTARGUMENTNOTANX500NAME
);
371 * Since we know firstObject is an X500Name, if both references are
372 * identical, they must be equal
374 if (firstObject
== secondObject
){
375 *pResult
= PKIX_TRUE
;
380 * If secondObject isn't an X500Name, we don't throw an error.
381 * We simply return a Boolean result of FALSE
383 *pResult
= PKIX_FALSE
;
384 PKIX_CHECK(PKIX_PL_Object_GetType
385 (secondObject
, &secondType
, plContext
),
386 PKIX_COULDNOTGETTYPEOFSECONDARGUMENT
);
387 if (secondType
!= PKIX_X500NAME_TYPE
) goto cleanup
;
389 firstX500Name
= (PKIX_PL_X500Name
*)firstObject
;
390 secondX500Name
= (PKIX_PL_X500Name
*)secondObject
;
392 /* we simply do byte comparison on DER encodings of DN's */
393 PKIX_CHECK(pkix_pl_X500Name_CompareDERBytes
394 (firstX500Name
, secondX500Name
, pResult
, plContext
),
395 PKIX_X500NAMECOMPAREDERBYTESFAILED
);
399 PKIX_RETURN(X500NAME
);
403 * FUNCTION: pkix_pl_X500Name_RegisterSelf
405 * Registers PKIX_X500NAME_TYPE and its related functions with systemClasses[]
407 * Not Thread Safe - for performance and complexity reasons
409 * Since this function is only called by PKIX_PL_Initialize, which should
410 * only be called once, it is acceptable that this function is not
414 pkix_pl_X500Name_RegisterSelf(void *plContext
)
417 extern pkix_ClassTable_Entry systemClasses
[PKIX_NUMTYPES
];
418 pkix_ClassTable_Entry entry
;
420 PKIX_ENTER(X500NAME
, "pkix_pl_X500Name_RegisterSelf");
422 entry
.description
= "X500Name";
423 entry
.destructor
= pkix_pl_X500Name_Destroy
;
424 entry
.equalsFunction
= pkix_pl_X500Name_Equals
;
425 entry
.hashcodeFunction
= pkix_pl_X500Name_Hashcode
;
426 entry
.toStringFunction
= pkix_pl_X500Name_ToString
;
427 entry
.comparator
= NULL
;
428 entry
.duplicateFunction
= pkix_duplicateImmutable
;
430 systemClasses
[PKIX_X500NAME_TYPE
] = entry
;
432 PKIX_RETURN(X500NAME
);
435 /* --Public-Functions------------------------------------------------------- */
438 * FUNCTION: PKIX_PL_X500Name_Create (see comments in pkix_pl_pki.h)
441 PKIX_PL_X500Name_Create(
442 PKIX_PL_String
*stringRep
,
443 PKIX_PL_X500Name
**pName
,
446 PKIX_PL_X500Name
*x500Name
= NULL
;
447 CERTName
*nssDN
= NULL
;
448 char *utf8String
= NULL
;
449 PKIX_UInt32 utf8Length
;
451 PKIX_ENTER(X500NAME
, "PKIX_PL_X500Name_Create()");
452 PKIX_NULLCHECK_TWO(pName
, stringRep
);
455 * convert the input PKIX_PL_String to PKIX_UTF8_NULL_TERM.
456 * we need to use this format specifier because
457 * CERT_AsciiToName expects a NULL-terminated UTF8 string.
458 * Since UTF8 allow NUL characters in the middle of the
459 * string, this is buggy. However, as a workaround, using
460 * PKIX_UTF8_NULL_TERM gives us a NULL-terminated UTF8 string.
463 PKIX_CHECK(PKIX_PL_String_GetEncoded
466 (void **)&utf8String
,
469 PKIX_STRINGGETENCODEDFAILED
);
471 PKIX_X500NAME_DEBUG("\t\tCalling CERT_AsciiToName).\n");
472 /* this should be really be called CERT_UTF8ToName */
473 nssDN
= CERT_AsciiToName(utf8String
);
475 PKIX_ERROR(PKIX_COULDNOTCREATENSSDN
);
478 /* create a PKIX_PL_X500Name object */
479 PKIX_CHECK(PKIX_PL_Object_Alloc
481 sizeof (PKIX_PL_X500Name
),
482 (PKIX_PL_Object
**)&x500Name
,
484 PKIX_COULDNOTCREATEX500NAMEOBJECT
);
486 /* populate the nssDN field */
487 x500Name
->nssDN
= nssDN
;
493 PKIX_FREE(utf8String
);
495 if (nssDN
&& PKIX_ERROR_RECEIVED
){
496 PKIX_X500NAME_DEBUG("\t\tCalling CERT_DestroyName).\n");
497 CERT_DestroyName(nssDN
);
501 PKIX_RETURN(X500NAME
);
505 * FUNCTION: PKIX_PL_X500Name_Match (see comments in pkix_pl_pki.h)
508 PKIX_PL_X500Name_Match(
509 PKIX_PL_X500Name
*firstX500Name
,
510 PKIX_PL_X500Name
*secondX500Name
,
511 PKIX_Boolean
*pResult
,
514 CERTName
*firstName
= NULL
;
515 CERTName
*secondName
= NULL
;
516 SECComparison cmpResult
;
518 PKIX_ENTER(X500NAME
, "PKIX_PL_X500Name_Equals");
519 PKIX_NULLCHECK_THREE(firstX500Name
, secondX500Name
, pResult
);
521 if (firstX500Name
== secondX500Name
){
522 *pResult
= PKIX_TRUE
;
526 firstName
= firstX500Name
->nssDN
;
527 secondName
= secondX500Name
->nssDN
;
529 PKIX_NULLCHECK_TWO(firstName
, secondName
);
531 PKIX_X500NAME_DEBUG("\t\tCalling CERT_CompareName).\n");
532 cmpResult
= CERT_CompareName(firstName
, secondName
);
534 *pResult
= (cmpResult
== SECEqual
);
538 PKIX_RETURN(X500NAME
);
542 * FUNCTION: pkix_pl_X500Name_GetSECName
545 * Encodes as a SECItem the CERTName embodied by the X500Name object pointed
546 * to by "xname", using the arena pointed to by "arena", and stores the result
547 * at "pSECName". If the name cannot be successfully encoded, NULL is stored
552 * Address of X500Name whose CERTName flag is to be encoded. Must be
555 * Address of the PRArenaPool to be used in the encoding, and in which
556 * "pSECName" will be allocated. Must be non-NULL.
558 * Address where result will be stored. Must be non-NULL.
560 * Platform-specific context pointer.
563 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
566 * Returns NULL if the function succeeds.
567 * Returns a Fatal Error if the function fails in an unrecoverable way.
570 pkix_pl_X500Name_GetSECName(
571 PKIX_PL_X500Name
*xname
,
577 PKIX_ENTER(X500NAME
, "pkix_pl_X500Name_GetCertName");
579 PKIX_NULLCHECK_THREE(xname
, arena
, pSECName
);
582 * SEC_ASN1EncodeItem returns NULL if unsuccessful. We just
583 * store the NULL result.
585 PKIX_PL_NSSCALLRV(X500NAME
, *pSECName
, SEC_ASN1EncodeItem
,
586 (arena
, NULL
, (void *)xname
->nssDN
, CERT_NameTemplate
));
588 PKIX_RETURN(X500NAME
);
592 * FUNCTION: pkix_pl_X500Name_CreateFromUtf8
595 * Creates an X500Name object from the RFC1485 string representation pointed
596 * to by "stringRep", and stores the result at "pName". If the string cannot
597 * be successfully converted, a non-fatal error is returned.
601 * Address of the RFC1485 string to be converted. Must be non-NULL.
603 * Address where the X500Name result will be stored. Must be non-NULL.
605 * Platform-specific context pointer.
608 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
611 * Returns NULL if the function succeeds.
612 * Returns an X500NAME Error if the function fails in a non-fatal way.
613 * Returns a Fatal Error if the function fails in an unrecoverable way.
616 pkix_pl_X500Name_CreateFromUtf8(
618 PKIX_PL_X500Name
**pName
,
621 PKIX_PL_X500Name
*x500Name
= NULL
;
622 CERTName
*nssDN
= NULL
;
624 PKIX_ENTER(X500NAME
, "pkix_pl_X500Name_CreateFromUtf8");
625 PKIX_NULLCHECK_TWO(pName
, stringRep
);
627 PKIX_PL_NSSCALLRV(X500NAME
, nssDN
, CERT_AsciiToName
, (stringRep
));
630 PKIX_ERROR(PKIX_COULDNOTCREATENSSDN
);
633 /* create a PKIX_PL_X500Name object */
634 PKIX_CHECK(PKIX_PL_Object_Alloc
636 sizeof (PKIX_PL_X500Name
),
637 (PKIX_PL_Object
**)&x500Name
,
639 PKIX_COULDNOTCREATEX500NAMEOBJECT
);
641 /* populate the nssDN field */
642 x500Name
->nssDN
= nssDN
;
648 if (nssDN
&& PKIX_ERROR_RECEIVED
){
649 PKIX_X500NAME_DEBUG("\t\tCalling CERT_DestroyName).\n");
650 CERT_DestroyName(nssDN
);
654 PKIX_RETURN(X500NAME
);
658 * FUNCTION: pkix_pl_X500Name_GetCommonName
661 * Extracts the CommonName component of the X500Name object pointed to by
662 * "xname", and stores the result at "pCommonName". If the CommonName cannot
663 * be successfully extracted, NULL is stored at "pCommonName".
665 * The returned string must be freed with PORT_Free.
669 * Address of X500Name whose CommonName is to be extracted. Must be
672 * Address where result will be stored. Must be non-NULL.
674 * Platform-specific context pointer.
677 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
680 * Returns NULL if the function succeeds.
681 * Returns a Fatal Error if the function fails in an unrecoverable way.
684 pkix_pl_X500Name_GetCommonName(
685 PKIX_PL_X500Name
*xname
,
686 unsigned char **pCommonName
,
689 PKIX_ENTER(X500NAME
, "pkix_pl_X500Name_GetCommonName");
690 PKIX_NULLCHECK_TWO(xname
, pCommonName
);
695 (unsigned char *)CERT_GetCommonName
,
698 PKIX_RETURN(X500NAME
);
702 * FUNCTION: pkix_pl_X500Name_GetCountryName
705 * Extracts the CountryName component of the X500Name object pointed to by
706 * "xname", and stores the result at "pCountryName". If the CountryName cannot
707 * be successfully extracted, NULL is stored at "pCountryName".
709 * The returned string must be freed with PORT_Free.
713 * Address of X500Name whose CountryName is to be extracted. Must be
716 * Address where result will be stored. Must be non-NULL.
718 * Platform-specific context pointer.
721 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
724 * Returns NULL if the function succeeds.
725 * Returns a Fatal Error if the function fails in an unrecoverable way.
728 pkix_pl_X500Name_GetCountryName(
729 PKIX_PL_X500Name
*xname
,
730 unsigned char **pCountryName
,
733 PKIX_ENTER(X500NAME
, "pkix_pl_X500Name_GetCountryName");
734 PKIX_NULLCHECK_TWO(xname
, pCountryName
);
739 (unsigned char *)CERT_GetCountryName
,
742 PKIX_RETURN(X500NAME
);
746 * FUNCTION: pkix_pl_X500Name_GetOrgName
749 * Extracts the OrganizationName component of the X500Name object pointed to by
750 * "xname", and stores the result at "pOrgName". If the OrganizationName cannot
751 * be successfully extracted, NULL is stored at "pOrgName".
753 * The returned string must be freed with PORT_Free.
757 * Address of X500Name whose OrganizationName is to be extracted. Must be
760 * Address where result will be stored. Must be non-NULL.
762 * Platform-specific context pointer.
765 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
768 * Returns NULL if the function succeeds.
769 * Returns a Fatal Error if the function fails in an unrecoverable way.
772 pkix_pl_X500Name_GetOrgName(
773 PKIX_PL_X500Name
*xname
,
774 unsigned char **pOrgName
,
777 PKIX_ENTER(X500NAME
, "pkix_pl_X500Name_GetOrgName");
778 PKIX_NULLCHECK_TWO(xname
, pOrgName
);
783 (unsigned char *)CERT_GetOrgName
,
786 PKIX_RETURN(X500NAME
);
789 pkix_pl_X500Name_GetCERTName(
790 PKIX_PL_X500Name
*xname
,
791 CERTName
**pCERTName
,
794 PKIX_ENTER(X500NAME
, "pkix_pl_X500Name_GetCERTName");
795 PKIX_NULLCHECK_TWO(xname
, pCERTName
);
797 *pCERTName
= xname
->nssDN
;
799 PKIX_RETURN(X500NAME
);