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 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.
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_generalname.c
40 * GeneralName Object Definitions
44 #include "pkix_pl_generalname.h"
46 /* --Private-GeneralName-Functions------------------------------------- */
49 * FUNCTION: pkix_pl_GeneralName_GetNssGeneralName
52 * Retrieves the NSS representation of the PKIX_PL_GeneralName pointed by
53 * "genName" and stores it at "pNssGenName". The NSS data type CERTGeneralName
54 * is stored in this object when the object was created.
58 * Address of PKIX_PL_GeneralName. Must be non-NULL.
60 * Address where CERTGeneralName will be stored. Must be non-NULL.
61 * "plContext" - Platform-specific context pointer.
63 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
65 * Returns NULL if the function succeeds.
66 * Returns a GeneralName Error if the function fails in a non-fatal way.
67 * Returns a Fatal Error if the function fails in an unrecoverable way.
70 pkix_pl_GeneralName_GetNssGeneralName(
71 PKIX_PL_GeneralName *genName,
72 CERTGeneralName **pNssGenName,
75 CERTGeneralName *nssGenName = NULL;
77 PKIX_ENTER(GENERALNAME, "pkix_pl_GeneralName_GetNssGeneralName");
78 PKIX_NULLCHECK_THREE(genName, pNssGenName, genName->nssGeneralNameList);
80 nssGenName = genName->nssGeneralNameList->name;
82 *pNssGenName = nssGenName;
84 PKIX_RETURN(GENERALNAME);
88 * FUNCTION: pkix_pl_OtherName_Create
91 * Creates new OtherName which represents the CERTGeneralName pointed to by
92 * "nssAltName" and stores it at "pOtherName".
96 * Address of CERTGeneralName. Must be non-NULL.
98 * Address where object pointer will be stored. Must be non-NULL.
99 * "plContext" - Platform-specific context pointer.
101 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
103 * Returns NULL if the function succeeds.
104 * Returns a GeneralName Error if the function fails in a non-fatal way.
105 * Returns a Fatal Error if the function fails in an unrecoverable way.
108 pkix_pl_OtherName_Create(
109 CERTGeneralName *nssAltName,
110 OtherName **pOtherName,
113 OtherName *otherName = NULL;
118 PKIX_ENTER(GENERALNAME, "pkix_pl_OtherName_Create");
119 PKIX_NULLCHECK_TWO(nssAltName, pOtherName);
121 PKIX_CHECK(PKIX_PL_Malloc
122 (sizeof (OtherName), (void **)&otherName, plContext),
125 /* make a copy of the name field */
126 PKIX_GENERALNAME_DEBUG("\t\tCalling SECITEM_CopyItem).\n");
127 rv = SECITEM_CopyItem
128 (NULL, &otherName->name, &nssAltName->name.OthName.name);
129 if (rv != SECSuccess) {
130 PKIX_ERROR(PKIX_OUTOFMEMORY);
133 /* make a copy of the oid field */
134 PKIX_GENERALNAME_DEBUG("\t\tCalling SECITEM_CopyItem).\n");
135 rv = SECITEM_CopyItem
136 (NULL, &otherName->oid, &nssAltName->name.OthName.oid);
137 if (rv != SECSuccess) {
138 PKIX_ERROR(PKIX_OUTOFMEMORY);
141 *pOtherName = otherName;
145 if (otherName && PKIX_ERROR_RECEIVED){
146 secItemName = otherName->name;
147 secItemOID = otherName->oid;
149 PKIX_GENERALNAME_DEBUG("\t\tCalling SECITEM_FreeItem).\n");
150 SECITEM_FreeItem(&secItemName, PR_FALSE);
152 PKIX_GENERALNAME_DEBUG("\t\tCalling SECITEM_FreeItem).\n");
153 SECITEM_FreeItem(&secItemOID, PR_FALSE);
155 PKIX_FREE(otherName);
159 PKIX_RETURN(GENERALNAME);
163 * FUNCTION: pkix_pl_DirectoryName_Create
166 * Creates a new X500Name which represents the directoryName component of the
167 * CERTGeneralName pointed to by "nssAltName" and stores it at "pX500Name".
171 * Address of CERTGeneralName. Must be non-NULL.
173 * Address where object pointer will be stored. Must be non-NULL.
174 * "plContext" - Platform-specific context pointer.
176 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
178 * Returns NULL if the function succeeds.
179 * Returns a GeneralName Error if the function fails in a non-fatal way.
180 * Returns a Fatal Error if the function fails in an unrecoverable way.
183 pkix_pl_DirectoryName_Create(
184 CERTGeneralName *nssAltName,
185 PKIX_PL_X500Name **pX500Name,
188 PKIX_PL_X500Name *pkixDN = NULL;
189 CERTName *dirName = NULL;
190 PKIX_PL_String *pkixDNString = NULL;
191 char *utf8String = NULL;
193 PKIX_ENTER(GENERALNAME, "pkix_pl_DirectoryName_Create");
194 PKIX_NULLCHECK_TWO(nssAltName, pX500Name);
196 dirName = &nssAltName->name.directoryName;
198 PKIX_CHECK(PKIX_PL_X500Name_CreateFromCERTName(NULL, dirName,
200 PKIX_X500NAMECREATEFROMCERTNAMEFAILED);
207 PKIX_DECREF(pkixDNString);
209 PKIX_RETURN(GENERALNAME);
213 * FUNCTION: pkix_pl_GeneralName_Create
216 * Creates new GeneralName which represents the CERTGeneralName pointed to by
217 * "nssAltName" and stores it at "pGenName".
221 * Address of CERTGeneralName. Must be non-NULL.
223 * Address where object pointer will be stored. Must be non-NULL.
224 * "plContext" - Platform-specific context pointer.
226 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
228 * Returns NULL if the function succeeds.
229 * Returns a GeneralName Error if the function fails in a non-fatal way.
230 * Returns a Fatal Error if the function fails in an unrecoverable way.
233 pkix_pl_GeneralName_Create(
234 CERTGeneralName *nssAltName,
235 PKIX_PL_GeneralName **pGenName,
238 PKIX_PL_GeneralName *genName = NULL;
239 PKIX_PL_X500Name *pkixDN = NULL;
240 PKIX_PL_OID *pkixOID = NULL;
241 OtherName *otherName = NULL;
242 CERTGeneralNameList *nssGenNameList = NULL;
243 CERTGeneralNameType nameType;
245 PKIX_ENTER(GENERALNAME, "pkix_pl_GeneralName_Create");
246 PKIX_NULLCHECK_TWO(nssAltName, pGenName);
248 /* create a PKIX_PL_GeneralName object */
249 PKIX_CHECK(PKIX_PL_Object_Alloc
250 (PKIX_GENERALNAME_TYPE,
251 sizeof (PKIX_PL_GeneralName),
252 (PKIX_PL_Object **)&genName,
254 PKIX_COULDNOTCREATEOBJECT);
256 nameType = nssAltName->type;
259 * We use CERT_CreateGeneralNameList to create just one CERTGeneralName
260 * item for memory allocation reason. If we want to just create one
261 * item, we have to use the calling path CERT_NewGeneralName, then
262 * CERT_CopyOneGeneralName. With this calling path, if we pass
263 * the arena argument as NULL, in CERT_CopyOneGeneralName's subsequent
264 * call to CERT_CopyName, it assumes arena should be valid, hence
265 * segmentation error (not sure this is a NSS bug, certainly it is
266 * not consistent). But on the other hand, we don't want to keep an
267 * arena record here explicitely for every PKIX_PL_GeneralName.
268 * So I concluded it is better to use CERT_CreateGeneralNameList,
269 * which keeps an arena pointer in its data structure and also masks
270 * out details calls from this libpkix level.
273 PKIX_GENERALNAME_DEBUG("\t\tCalling CERT_CreateGeneralNameList).\n");
274 nssGenNameList = CERT_CreateGeneralNameList(nssAltName);
276 if (nssGenNameList == NULL) {
277 PKIX_ERROR(PKIX_CERTCREATEGENERALNAMELISTFAILED);
280 genName->nssGeneralNameList = nssGenNameList;
282 /* initialize fields */
283 genName->type = nameType;
284 genName->directoryName = NULL;
285 genName->OthName = NULL;
286 genName->other = NULL;
292 PKIX_CHECK(pkix_pl_OtherName_Create
293 (nssAltName, &otherName, plContext),
294 PKIX_OTHERNAMECREATEFAILED);
296 genName->OthName = otherName;
299 case certDirectoryName:
301 PKIX_CHECK(pkix_pl_DirectoryName_Create
302 (nssAltName, &pkixDN, plContext),
303 PKIX_DIRECTORYNAMECREATEFAILED);
305 genName->directoryName = pkixDN;
308 PKIX_CHECK(PKIX_PL_OID_CreateBySECItem(&nssAltName->name.other,
309 &pkixOID, plContext),
310 PKIX_OIDCREATEFAILED);
312 genName->oid = pkixOID;
315 case certEDIPartyName:
318 case certX400Address:
320 genName->other = SECITEM_DupItem(&nssAltName->name.other);
321 if (!genName->other) {
322 PKIX_ERROR(PKIX_OUTOFMEMORY);
326 PKIX_ERROR(PKIX_NAMETYPENOTSUPPORTED);
333 PKIX_DECREF(genName);
335 PKIX_RETURN(GENERALNAME);
339 * FUNCTION: pkix_pl_GeneralName_ToString_Helper
342 * Helper function that creates a string representation of the GeneralName
343 * pointed to by "name" and stores it at "pString" Different mechanisms are
344 * used to create the string, depending on the type of the GeneralName.
348 * Address of GeneralName whose string representation is desired.
351 * Address where object pointer will be stored. Must be non-NULL.
352 * "plContext" - Platform-specific context pointer.
354 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
356 * Returns NULL if the function succeeds.
357 * Returns a GeneralName Error if the function fails in a non-fatal way.
358 * Returns a Fatal Error if the function fails in an unrecoverable way.
361 pkix_pl_GeneralName_ToString_Helper(
362 PKIX_PL_GeneralName *name,
363 PKIX_PL_String **pString,
366 PKIX_PL_X500Name *pkixDN = NULL;
367 PKIX_PL_OID *pkixOID = NULL;
368 char *x400AsciiName = NULL;
369 char *ediPartyName = NULL;
370 char *asciiName = NULL;
372 PKIX_ENTER(GENERALNAME, "pkix_pl_GeneralName_ToString_Helper");
373 PKIX_NULLCHECK_TWO(name, pString);
375 switch (name->type) {
380 * Note that we can't use PKIX_ESCASCII here because
381 * name->other->data is not guaranteed to be null-terminated.
384 PKIX_NULLCHECK_ONE(name->other);
386 PKIX_CHECK(PKIX_PL_String_Create(PKIX_UTF8,
391 PKIX_STRINGCREATEFAILED);
393 case certEDIPartyName:
394 /* XXX print out the actual bytes */
395 ediPartyName = "EDIPartyName: <DER-encoded value>";
396 PKIX_CHECK(PKIX_PL_String_Create(PKIX_ESCASCII,
401 PKIX_STRINGCREATEFAILED);
403 case certX400Address:
404 /* XXX print out the actual bytes */
405 x400AsciiName = "X400Address: <DER-encoded value>";
406 PKIX_CHECK(PKIX_PL_String_Create(PKIX_ESCASCII,
411 PKIX_STRINGCREATEFAILED);
414 PKIX_CHECK(pkix_pl_ipAddrBytes2Ascii
415 (name->other, &asciiName, plContext),
416 PKIX_IPADDRBYTES2ASCIIFAILED);
418 PKIX_CHECK(PKIX_PL_String_Create(PKIX_ESCASCII,
423 PKIX_STRINGCREATEFAILED);
426 PKIX_NULLCHECK_ONE(name->OthName);
428 /* we only print type-id - don't know how to print value */
429 /* XXX print out the bytes of the value */
430 PKIX_CHECK(pkix_pl_oidBytes2Ascii
431 (&name->OthName->oid, &asciiName, plContext),
432 PKIX_OIDBYTES2ASCIIFAILED);
434 PKIX_CHECK(PKIX_PL_String_Create
440 PKIX_STRINGCREATEFAILED);
444 PKIX_CHECK(PKIX_PL_Object_ToString
445 ((PKIX_PL_Object *)pkixOID, pString, plContext),
446 PKIX_OIDTOSTRINGFAILED);
448 case certDirectoryName:
449 pkixDN = name->directoryName;
450 PKIX_CHECK(PKIX_PL_Object_ToString
451 ((PKIX_PL_Object *)pkixDN, pString, plContext),
452 PKIX_X500NAMETOSTRINGFAILED);
456 (PKIX_TOSTRINGFORTHISGENERALNAMETYPENOTSUPPORTED);
461 PKIX_FREE(asciiName);
463 PKIX_RETURN(GENERALNAME);
467 * FUNCTION: pkix_pl_GeneralName_Destroy
468 * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
471 pkix_pl_GeneralName_Destroy(
472 PKIX_PL_Object *object,
475 PKIX_PL_GeneralName *name = NULL;
479 PKIX_ENTER(GENERALNAME, "pkix_pl_GeneralName_Destroy");
480 PKIX_NULLCHECK_ONE(object);
482 PKIX_CHECK(pkix_CheckType(object, PKIX_GENERALNAME_TYPE, plContext),
483 PKIX_OBJECTNOTGENERALNAME);
485 name = (PKIX_PL_GeneralName *)object;
487 PKIX_GENERALNAME_DEBUG("\t\tCalling SECITEM_FreeItem).\n");
488 SECITEM_FreeItem(name->other, PR_TRUE);
492 secItemName = name->OthName->name;
493 secItemOID = name->OthName->oid;
495 PKIX_GENERALNAME_DEBUG("\t\tCalling SECITEM_FreeItem).\n");
496 SECITEM_FreeItem(&secItemName, PR_FALSE);
498 PKIX_GENERALNAME_DEBUG("\t\tCalling SECITEM_FreeItem).\n");
499 SECITEM_FreeItem(&secItemOID, PR_FALSE);
501 PKIX_FREE(name->OthName);
502 name->OthName = NULL;
505 if (name->nssGeneralNameList != NULL) {
506 PKIX_GENERALNAME_DEBUG
507 ("\t\tCalling CERT_DestroyGeneralNameList).\n");
508 CERT_DestroyGeneralNameList(name->nssGeneralNameList);
511 PKIX_DECREF(name->directoryName);
512 PKIX_DECREF(name->oid);
516 PKIX_RETURN(GENERALNAME);
520 * FUNCTION: pkix_pl_GeneralName_ToString
521 * (see comments for PKIX_PL_ToStringCallback in pkix_pl_system.h)
524 pkix_pl_GeneralName_ToString(
525 PKIX_PL_Object *object,
526 PKIX_PL_String **pString,
529 PKIX_PL_String *nameString = NULL;
530 PKIX_PL_GeneralName *name = NULL;
532 PKIX_ENTER(GENERALNAME, "pkix_pl_GeneralName_toString");
533 PKIX_NULLCHECK_TWO(object, pString);
535 PKIX_CHECK(pkix_CheckType(object, PKIX_GENERALNAME_TYPE, plContext),
536 PKIX_OBJECTNOTGENERALNAME);
538 name = (PKIX_PL_GeneralName *)object;
540 PKIX_CHECK(pkix_pl_GeneralName_ToString_Helper
541 (name, &nameString, plContext),
542 PKIX_GENERALNAMETOSTRINGHELPERFAILED);
544 *pString = nameString;
550 PKIX_RETURN(GENERALNAME);
554 * FUNCTION: pkix_pl_GeneralName_Hashcode
555 * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h)
558 pkix_pl_GeneralName_Hashcode(
559 PKIX_PL_Object *object,
560 PKIX_UInt32 *pHashcode,
563 PKIX_PL_GeneralName *name = NULL;
564 PKIX_UInt32 firstHash, secondHash, nameHash;
566 PKIX_ENTER(GENERALNAME, "pkix_pl_GeneralName_Hashcode");
567 PKIX_NULLCHECK_TWO(object, pHashcode);
569 PKIX_CHECK(pkix_CheckType(object, PKIX_GENERALNAME_TYPE, plContext),
570 PKIX_OBJECTNOTGENERALNAME);
572 name = (PKIX_PL_GeneralName *)object;
574 switch (name->type) {
577 case certX400Address:
578 case certEDIPartyName:
581 PKIX_NULLCHECK_ONE(name->other);
583 ((const unsigned char *)
591 PKIX_CHECK(PKIX_PL_Object_Hashcode
592 ((PKIX_PL_Object *)name->oid,
595 PKIX_OIDHASHCODEFAILED);
598 PKIX_NULLCHECK_ONE(name->OthName);
600 ((const unsigned char *)
601 name->OthName->oid.data,
602 name->OthName->oid.len,
608 ((const unsigned char *)
609 name->OthName->name.data,
610 name->OthName->name.len,
615 nameHash = firstHash + secondHash;
617 case certDirectoryName:
618 PKIX_CHECK(PKIX_PL_Object_Hashcode
623 PKIX_X500NAMEHASHCODEFAILED);
627 *pHashcode = nameHash;
631 PKIX_RETURN(GENERALNAME);
636 * FUNCTION: pkix_pl_GeneralName_Equals
637 * (see comments for PKIX_PL_Equals_Callback in pkix_pl_system.h)
640 pkix_pl_GeneralName_Equals(
641 PKIX_PL_Object *firstObject,
642 PKIX_PL_Object *secondObject,
643 PKIX_Boolean *pResult,
646 PKIX_PL_GeneralName *firstName = NULL;
647 PKIX_PL_GeneralName *secondName = NULL;
648 PKIX_UInt32 secondType;
650 PKIX_ENTER(GENERALNAME, "pkix_pl_GeneralName_Equals");
651 PKIX_NULLCHECK_THREE(firstObject, secondObject, pResult);
653 /* test that firstObject is a GeneralName */
654 PKIX_CHECK(pkix_CheckType
655 (firstObject, PKIX_GENERALNAME_TYPE, plContext),
656 PKIX_FIRSTOBJECTNOTGENERALNAME);
659 * Since we know firstObject is a GeneralName, if both references are
660 * identical, they must be equal
662 if (firstObject == secondObject){
663 *pResult = PKIX_TRUE;
668 * If secondObject isn't a GeneralName, we don't throw an error.
669 * We simply return a Boolean result of FALSE
671 *pResult = PKIX_FALSE;
672 PKIX_CHECK(PKIX_PL_Object_GetType
673 (secondObject, &secondType, plContext),
674 PKIX_COULDNOTGETTYPEOFSECONDARGUMENT);
675 if (secondType != PKIX_GENERALNAME_TYPE){
679 firstName = (PKIX_PL_GeneralName *)firstObject;
680 secondName = (PKIX_PL_GeneralName *)secondObject;
682 if (firstName->type != secondName->type){
686 switch (firstName->type) {
689 case certX400Address:
690 case certEDIPartyName:
693 PKIX_GENERALNAME_DEBUG("\t\tCalling SECITEM_CompareItem).\n");
694 if (SECITEM_CompareItem(firstName->other,
695 secondName->other) != SECEqual) {
700 PKIX_CHECK(PKIX_PL_Object_Equals
701 ((PKIX_PL_Object *)firstName->oid,
702 (PKIX_PL_Object *)secondName->oid,
705 PKIX_OIDEQUALSFAILED);
708 PKIX_NULLCHECK_TWO(firstName->OthName, secondName->OthName);
709 PKIX_GENERALNAME_DEBUG("\t\tCalling SECITEM_CompareItem).\n");
710 if (SECITEM_CompareItem(&firstName->OthName->oid,
711 &secondName->OthName->oid)
713 SECITEM_CompareItem(&firstName->OthName->name,
714 &secondName->OthName->name)
719 case certDirectoryName:
720 PKIX_CHECK(PKIX_PL_Object_Equals
721 ((PKIX_PL_Object *)firstName->directoryName,
722 (PKIX_PL_Object *)secondName->directoryName,
725 PKIX_X500NAMEEQUALSFAILED);
729 *pResult = PKIX_TRUE;
733 PKIX_RETURN(GENERALNAME);
737 * FUNCTION: pkix_pl_GeneralName_RegisterSelf
739 * Registers PKIX_GENERALNAME_TYPE and related functions with systemClasses[]
741 * Not Thread Safe - for performance and complexity reasons
743 * Since this function is only called by PKIX_PL_Initialize, which should
744 * only be called once, it is acceptable that this function is not
748 pkix_pl_GeneralName_RegisterSelf(void *plContext)
751 extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
752 pkix_ClassTable_Entry entry;
754 PKIX_ENTER(GENERALNAME, "pkix_pl_GeneralName_RegisterSelf");
756 entry.description = "GeneralName";
757 entry.objCounter = 0;
758 entry.typeObjectSize = sizeof(PKIX_PL_GeneralName);
759 entry.destructor = pkix_pl_GeneralName_Destroy;
760 entry.equalsFunction = pkix_pl_GeneralName_Equals;
761 entry.hashcodeFunction = pkix_pl_GeneralName_Hashcode;
762 entry.toStringFunction = pkix_pl_GeneralName_ToString;
763 entry.comparator = NULL;
764 entry.duplicateFunction = pkix_duplicateImmutable;
766 systemClasses[PKIX_GENERALNAME_TYPE] = entry;
768 PKIX_RETURN(GENERALNAME);
771 /* --Public-Functions------------------------------------------------------- */
773 #ifdef BUILD_LIBPKIX_TESTS
775 * FUNCTION: PKIX_PL_GeneralName_Create (see comments in pkix_pl_pki.h)
778 PKIX_PL_GeneralName_Create(
779 PKIX_UInt32 nameType,
780 PKIX_PL_String *stringRep,
781 PKIX_PL_GeneralName **pGName,
784 PKIX_PL_X500Name *pkixDN = NULL;
785 PKIX_PL_OID *pkixOID = NULL;
786 SECItem *secItem = NULL;
787 char *asciiString = NULL;
788 PKIX_UInt32 length = 0;
789 PKIX_PL_GeneralName *genName = NULL;
790 CERTGeneralName *nssGenName = NULL;
791 CERTGeneralNameList *nssGenNameList = NULL;
792 CERTName *nssCertName = NULL;
793 PLArenaPool *arena = NULL;
795 PKIX_ENTER(GENERALNAME, "PKIX_PL_GeneralName_Create");
796 PKIX_NULLCHECK_TWO(pGName, stringRep);
798 PKIX_CHECK(PKIX_PL_String_GetEncoded
801 (void **)&asciiString,
804 PKIX_STRINGGETENCODEDFAILED);
806 /* Create a temporary CERTGeneralName */
807 PKIX_GENERALNAME_DEBUG("\t\tCalling PL_strlen).\n");
808 length = PL_strlen(asciiString);
809 PKIX_GENERALNAME_DEBUG("\t\tCalling SECITEM_AllocItem).\n");
810 secItem = SECITEM_AllocItem(NULL, NULL, length);
811 PKIX_GENERALNAME_DEBUG("\t\tCalling PORT_Memcpy).\n");
812 (void) PORT_Memcpy(secItem->data, asciiString, length);
813 PKIX_CERT_DEBUG("\t\tCalling PORT_NewArena).\n");
814 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
816 PKIX_ERROR(PKIX_OUTOFMEMORY);
818 PKIX_GENERALNAME_DEBUG("\t\tCalling CERT_NewGeneralName).\n");
819 nssGenName = CERT_NewGeneralName(arena, nameType);
820 if (nssGenName == NULL) {
821 PKIX_ERROR(PKIX_ALLOCATENEWCERTGENERALNAMEFAILED);
828 nssGenName->name.other = *secItem;
831 case certDirectoryName:
833 PKIX_CHECK(PKIX_PL_X500Name_Create
834 (stringRep, &pkixDN, plContext),
835 PKIX_X500NAMECREATEFAILED);
837 PKIX_GENERALNAME_DEBUG("\t\tCalling CERT_AsciiToName).\n");
838 nssCertName = CERT_AsciiToName(asciiString);
839 nssGenName->name.directoryName = *nssCertName;
843 PKIX_CHECK(PKIX_PL_OID_Create
844 (asciiString, &pkixOID, plContext),
845 PKIX_OIDCREATEFAILED);
846 nssGenName->name.other = *secItem;
849 /* including IPAddress, EDIPartyName, OtherName, X400Address */
850 PKIX_ERROR(PKIX_UNABLETOCREATEGENERALNAMEOFTHISTYPE);
853 /* create a PKIX_PL_GeneralName object */
854 PKIX_CHECK(PKIX_PL_Object_Alloc
855 (PKIX_GENERALNAME_TYPE,
856 sizeof (PKIX_PL_GeneralName),
857 (PKIX_PL_Object **)&genName,
859 PKIX_COULDNOTCREATEOBJECT);
861 /* create a CERTGeneralNameList */
862 nssGenName->type = nameType;
863 PKIX_GENERALNAME_DEBUG("\t\tCalling CERT_CreateGeneralNameList).\n");
864 nssGenNameList = CERT_CreateGeneralNameList(nssGenName);
865 if (nssGenNameList == NULL) {
866 PKIX_ERROR(PKIX_CERTCREATEGENERALNAMELISTFAILED);
868 genName->nssGeneralNameList = nssGenNameList;
870 /* initialize fields */
871 genName->type = nameType;
872 genName->directoryName = pkixDN;
873 genName->OthName = NULL;
874 genName->other = secItem;
875 genName->oid = pkixOID;
880 PKIX_FREE(asciiString);
882 if (nssCertName != NULL) {
883 PKIX_CERT_DEBUG("\t\tCalling CERT_DestroyName).\n");
884 CERT_DestroyName(nssCertName);
887 if (arena){ /* will free nssGenName */
888 PKIX_CERT_DEBUG("\t\tCalling PORT_FreeArena).\n");
889 PORT_FreeArena(arena, PR_FALSE);
892 if (PKIX_ERROR_RECEIVED){
894 PKIX_DECREF(pkixOID);
896 PKIX_GENERALNAME_DEBUG("\t\tCalling SECITEM_FreeItem).\n");
898 SECITEM_FreeItem(secItem, PR_TRUE);
903 PKIX_RETURN(GENERALNAME);
906 #endif /* BUILD_LIBPKIX_TESTS */