michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: /* michael@0: * pkix_verifynode.c michael@0: * michael@0: * Verify Node Object Type Definition michael@0: * michael@0: */ michael@0: michael@0: #include "pkix_verifynode.h" michael@0: michael@0: /* --Private-VerifyNode-Functions---------------------------------- */ michael@0: michael@0: /* michael@0: * FUNCTION: pkix_VerifyNode_Create michael@0: * DESCRIPTION: michael@0: * michael@0: * This function creates a VerifyNode using the Cert pointed to by "cert", michael@0: * the depth given by "depth", and the Error pointed to by "error", storing michael@0: * the result at "pObject". michael@0: * michael@0: * PARAMETERS michael@0: * "cert" michael@0: * Address of Cert for the node. Must be non-NULL michael@0: * "depth" michael@0: * UInt32 value of the depth for this node. michael@0: * "error" michael@0: * Address of Error for the node. michael@0: * "pObject" michael@0: * Address where the VerifyNode pointer will be stored. Must be non-NULL. michael@0: * "plContext" michael@0: * Platform-specific context pointer. michael@0: * THREAD SAFETY: michael@0: * Thread Safe (see Thread Safety Definitions in Programmer's Guide) michael@0: * RETURNS: michael@0: * Returns NULL if the function succeeds. michael@0: * Returns a Fatal Error if the function fails in an unrecoverable way. michael@0: */ michael@0: PKIX_Error * michael@0: pkix_VerifyNode_Create( michael@0: PKIX_PL_Cert *cert, michael@0: PKIX_UInt32 depth, michael@0: PKIX_Error *error, michael@0: PKIX_VerifyNode **pObject, michael@0: void *plContext) michael@0: { michael@0: PKIX_VerifyNode *node = NULL; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_VerifyNode_Create"); michael@0: PKIX_NULLCHECK_TWO(cert, pObject); michael@0: michael@0: PKIX_CHECK(PKIX_PL_Object_Alloc michael@0: (PKIX_VERIFYNODE_TYPE, michael@0: sizeof (PKIX_VerifyNode), michael@0: (PKIX_PL_Object **)&node, michael@0: plContext), michael@0: PKIX_COULDNOTCREATEVERIFYNODEOBJECT); michael@0: michael@0: PKIX_INCREF(cert); michael@0: node->verifyCert = cert; michael@0: michael@0: PKIX_INCREF(error); michael@0: node->error = error; michael@0: michael@0: node->depth = depth; michael@0: michael@0: node->children = NULL; michael@0: michael@0: *pObject = node; michael@0: node = NULL; michael@0: michael@0: cleanup: michael@0: michael@0: PKIX_DECREF(node); michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: pkix_VerifyNode_AddToChain michael@0: * DESCRIPTION: michael@0: * michael@0: * Adds the VerifyNode pointed to by "child", at the appropriate depth, to the michael@0: * List of children of the VerifyNode pointed to by "parentNode". The chain of michael@0: * VerifyNodes is traversed until a VerifyNode is found at a depth one less michael@0: * than that specified in "child". An Error is returned if there is no parent michael@0: * at a suitable depth. michael@0: * michael@0: * If "parentNode" has a NULL pointer for the List of children, a new List is michael@0: * created containing "child". Otherwise "child" is appended to the existing michael@0: * List. michael@0: * michael@0: * Depth, in this context, means distance from the root node, which michael@0: * is at depth zero. michael@0: * michael@0: * PARAMETERS: michael@0: * "parentNode" michael@0: * Address of VerifyNode whose List of child VerifyNodes is to be michael@0: * created or appended to. Must be non-NULL. michael@0: * "child" michael@0: * Address of VerifyNode to be added to parentNode's List. Must be michael@0: * non-NULL. michael@0: * "plContext" michael@0: * Platform-specific context pointer. michael@0: * THREAD SAFETY: michael@0: * Not Thread Safe (see Thread Safety Definitions in Programmer's Guide) michael@0: * RETURNS: michael@0: * Returns NULL if the function succeeds. michael@0: * Returns a VerifyNode Error if the function fails in a non-fatal way. michael@0: * Returns a Fatal Error if the function fails in an unrecoverable way. michael@0: */ michael@0: PKIX_Error * michael@0: pkix_VerifyNode_AddToChain( michael@0: PKIX_VerifyNode *parentNode, michael@0: PKIX_VerifyNode *child, michael@0: void *plContext) michael@0: { michael@0: PKIX_VerifyNode *successor = NULL; michael@0: PKIX_List *listOfChildren = NULL; michael@0: PKIX_UInt32 numChildren = 0; michael@0: PKIX_UInt32 parentDepth = 0; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_VerifyNode_AddToChain"); michael@0: PKIX_NULLCHECK_TWO(parentNode, child); michael@0: michael@0: parentDepth = parentNode->depth; michael@0: listOfChildren = parentNode->children; michael@0: if (listOfChildren == NULL) { michael@0: michael@0: if (parentDepth != (child->depth - 1)) { michael@0: PKIX_ERROR(PKIX_NODESMISSINGFROMCHAIN); michael@0: } michael@0: michael@0: PKIX_CHECK(PKIX_List_Create(&listOfChildren, plContext), michael@0: PKIX_LISTCREATEFAILED); michael@0: michael@0: PKIX_CHECK(PKIX_List_AppendItem michael@0: (listOfChildren, (PKIX_PL_Object *)child, plContext), michael@0: PKIX_COULDNOTAPPENDCHILDTOPARENTSVERIFYNODELIST); michael@0: michael@0: parentNode->children = listOfChildren; michael@0: } else { michael@0: /* get number of children */ michael@0: PKIX_CHECK(PKIX_List_GetLength michael@0: (listOfChildren, &numChildren, plContext), michael@0: PKIX_LISTGETLENGTHFAILED); michael@0: michael@0: if (numChildren != 1) { michael@0: PKIX_ERROR(PKIX_AMBIGUOUSPARENTAGEOFVERIFYNODE); michael@0: } michael@0: michael@0: /* successor = listOfChildren[0] */ michael@0: PKIX_CHECK(PKIX_List_GetItem michael@0: (listOfChildren, michael@0: 0, michael@0: (PKIX_PL_Object **)&successor, michael@0: plContext), michael@0: PKIX_LISTGETITEMFAILED); michael@0: michael@0: PKIX_CHECK(pkix_VerifyNode_AddToChain michael@0: (successor, child, plContext), michael@0: PKIX_VERIFYNODEADDTOCHAINFAILED); michael@0: } michael@0: michael@0: PKIX_CHECK(PKIX_PL_Object_InvalidateCache michael@0: ((PKIX_PL_Object *)parentNode, plContext), michael@0: PKIX_OBJECTINVALIDATECACHEFAILED); michael@0: michael@0: cleanup: michael@0: PKIX_DECREF(successor); michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: pkix_VerifyNode_SetDepth michael@0: * DESCRIPTION: michael@0: * michael@0: * The function sets the depth field of each VerifyNode in the List "children" michael@0: * to the value given by "depth", and recursively sets the depth of any michael@0: * successive generations to the successive values. michael@0: * michael@0: * PARAMETERS: michael@0: * "children" michael@0: * The List of VerifyNodes. Must be non-NULL. michael@0: * "depth" michael@0: * The value of the depth field to be set in members of the List. michael@0: * "plContext" michael@0: * Platform-specific context pointer. michael@0: * THREAD SAFETY: michael@0: * Thread Safe (see Thread Safety Definitions in Programmer's Guide) michael@0: * RETURNS: michael@0: * Returns NULL if the function succeeds. michael@0: * Returns a Fatal Error if the function fails in an unrecoverable way. michael@0: */ michael@0: static PKIX_Error * michael@0: pkix_VerifyNode_SetDepth(PKIX_List *children, michael@0: PKIX_UInt32 depth, michael@0: void *plContext) michael@0: { michael@0: PKIX_UInt32 numChildren = 0; michael@0: PKIX_UInt32 chIx = 0; michael@0: PKIX_VerifyNode *child = NULL; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_VerifyNode_SetDepth"); michael@0: PKIX_NULLCHECK_ONE(children); michael@0: michael@0: PKIX_CHECK(PKIX_List_GetLength(children, &numChildren, plContext), michael@0: PKIX_LISTGETLENGTHFAILED); michael@0: michael@0: for (chIx = 0; chIx < numChildren; chIx++) { michael@0: PKIX_CHECK(PKIX_List_GetItem michael@0: (children, chIx, (PKIX_PL_Object **)&child, plContext), michael@0: PKIX_LISTGETITEMFAILED); michael@0: michael@0: child->depth = depth; michael@0: michael@0: if (child->children != NULL) { michael@0: PKIX_CHECK(pkix_VerifyNode_SetDepth michael@0: (child->children, depth + 1, plContext), michael@0: PKIX_VERIFYNODESETDEPTHFAILED); michael@0: } michael@0: michael@0: PKIX_DECREF(child); michael@0: } michael@0: michael@0: cleanup: michael@0: michael@0: PKIX_DECREF(child); michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: pkix_VerifyNode_AddToTree michael@0: * DESCRIPTION: michael@0: * michael@0: * Adds the VerifyNode pointed to by "child" to the List of children of the michael@0: * VerifyNode pointed to by "parentNode". If "parentNode" has a NULL pointer michael@0: * for the List of children, a new List is created containing "child". michael@0: * Otherwise "child" is appended to the existing List. The depth field of michael@0: * "child" is set to one more than the corresponding value in "parent", and michael@0: * if the "child" itself has child nodes, their depth fields are updated michael@0: * accordingly. michael@0: * michael@0: * Depth, in this context, means distance from the root node, which michael@0: * is at depth zero. michael@0: * michael@0: * PARAMETERS: michael@0: * "parentNode" michael@0: * Address of VerifyNode whose List of child VerifyNodes is to be michael@0: * created or appended to. Must be non-NULL. michael@0: * "child" michael@0: * Address of VerifyNode to be added to parentNode's List. Must be michael@0: * non-NULL. michael@0: * "plContext" michael@0: * Platform-specific context pointer. michael@0: * THREAD SAFETY: michael@0: * Not Thread Safe (see Thread Safety Definitions in Programmer's Guide) michael@0: * RETURNS: michael@0: * Returns NULL if the function succeeds. michael@0: * Returns a Fatal Error if the function fails in an unrecoverable way. michael@0: */ michael@0: PKIX_Error * michael@0: pkix_VerifyNode_AddToTree( michael@0: PKIX_VerifyNode *parentNode, michael@0: PKIX_VerifyNode *child, michael@0: void *plContext) michael@0: { michael@0: PKIX_List *listOfChildren = NULL; michael@0: PKIX_UInt32 parentDepth = 0; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_VerifyNode_AddToTree"); michael@0: PKIX_NULLCHECK_TWO(parentNode, child); michael@0: michael@0: parentDepth = parentNode->depth; michael@0: listOfChildren = parentNode->children; michael@0: if (listOfChildren == NULL) { michael@0: michael@0: PKIX_CHECK(PKIX_List_Create(&listOfChildren, plContext), michael@0: PKIX_LISTCREATEFAILED); michael@0: michael@0: parentNode->children = listOfChildren; michael@0: } michael@0: michael@0: child->depth = parentDepth + 1; michael@0: michael@0: PKIX_CHECK(PKIX_List_AppendItem michael@0: (parentNode->children, (PKIX_PL_Object *)child, plContext), michael@0: PKIX_COULDNOTAPPENDCHILDTOPARENTSVERIFYNODELIST); michael@0: michael@0: if (child->children != NULL) { michael@0: PKIX_CHECK(pkix_VerifyNode_SetDepth michael@0: (child->children, child->depth + 1, plContext), michael@0: PKIX_VERIFYNODESETDEPTHFAILED); michael@0: } michael@0: michael@0: michael@0: cleanup: michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: pkix_SingleVerifyNode_ToString michael@0: * DESCRIPTION: michael@0: * michael@0: * Creates a String representation of the attributes of the VerifyNode pointed michael@0: * to by "node", other than its children, and stores the result at "pString". michael@0: * michael@0: * PARAMETERS: michael@0: * "node" michael@0: * Address of VerifyNode to be described by the string. Must be non-NULL. michael@0: * "pString" michael@0: * Address where object pointer will be stored. Must be non-NULL. michael@0: * "plContext" michael@0: * Platform-specific context pointer. michael@0: * THREAD SAFETY: michael@0: * Conditionally Thread Safe michael@0: * (see Thread Safety Definitions in Programmer's Guide) michael@0: * RETURNS: michael@0: * Returns NULL if function succeeds michael@0: * Returns a VerifyNode Error if the function fails in a non-fatal way. michael@0: * Returns a Fatal Error if the function fails in a fatal way michael@0: */ michael@0: PKIX_Error * michael@0: pkix_SingleVerifyNode_ToString( michael@0: PKIX_VerifyNode *node, michael@0: PKIX_PL_String **pString, michael@0: void *plContext) michael@0: { michael@0: PKIX_PL_String *fmtString = NULL; michael@0: PKIX_PL_String *errorString = NULL; michael@0: PKIX_PL_String *outString = NULL; michael@0: michael@0: PKIX_PL_X500Name *issuerName = NULL; michael@0: PKIX_PL_X500Name *subjectName = NULL; michael@0: PKIX_PL_String *issuerString = NULL; michael@0: PKIX_PL_String *subjectString = NULL; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_SingleVerifyNode_ToString"); michael@0: PKIX_NULLCHECK_THREE(node, pString, node->verifyCert); michael@0: michael@0: PKIX_TOSTRING(node->error, &errorString, plContext, michael@0: PKIX_ERRORTOSTRINGFAILED); michael@0: michael@0: PKIX_CHECK(PKIX_PL_Cert_GetIssuer michael@0: (node->verifyCert, &issuerName, plContext), michael@0: PKIX_CERTGETISSUERFAILED); michael@0: michael@0: PKIX_TOSTRING(issuerName, &issuerString, plContext, michael@0: PKIX_X500NAMETOSTRINGFAILED); michael@0: michael@0: PKIX_CHECK(PKIX_PL_Cert_GetSubject michael@0: (node->verifyCert, &subjectName, plContext), michael@0: PKIX_CERTGETSUBJECTFAILED); michael@0: michael@0: PKIX_TOSTRING(subjectName, &subjectString, plContext, michael@0: PKIX_X500NAMETOSTRINGFAILED); michael@0: michael@0: PKIX_CHECK(PKIX_PL_String_Create michael@0: (PKIX_ESCASCII, michael@0: "CERT[Issuer:%s, Subject:%s], depth=%d, error=%s", michael@0: 0, michael@0: &fmtString, michael@0: plContext), michael@0: PKIX_CANTCREATESTRING); michael@0: michael@0: PKIX_CHECK(PKIX_PL_Sprintf michael@0: (&outString, michael@0: plContext, michael@0: fmtString, michael@0: issuerString, michael@0: subjectString, michael@0: node->depth, michael@0: errorString), michael@0: PKIX_SPRINTFFAILED); michael@0: michael@0: *pString = outString; michael@0: michael@0: cleanup: michael@0: michael@0: PKIX_DECREF(fmtString); michael@0: PKIX_DECREF(errorString); michael@0: PKIX_DECREF(issuerName); michael@0: PKIX_DECREF(subjectName); michael@0: PKIX_DECREF(issuerString); michael@0: PKIX_DECREF(subjectString); michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: pkix_VerifyNode_ToString_Helper michael@0: * DESCRIPTION: michael@0: * michael@0: * Produces a String representation of a VerifyNode tree below the VerifyNode michael@0: * pointed to by "rootNode", with each line of output prefixed by the String michael@0: * pointed to by "indent", and stores the result at "pTreeString". It is michael@0: * called recursively, with ever-increasing indentation, for successively michael@0: * lower nodes on the tree. michael@0: * michael@0: * PARAMETERS: michael@0: * "rootNode" michael@0: * Address of VerifyNode subtree. Must be non-NULL. michael@0: * "indent" michael@0: * Address of String to be prefixed to each line of output. May be NULL michael@0: * if no indentation is desired michael@0: * "pTreeString" michael@0: * Address where the resulting String will be stored; must be non-NULL michael@0: * "plContext" michael@0: * Platform-specific context pointer. michael@0: * THREAD SAFETY: michael@0: * Conditionally Thread Safe michael@0: * (see Thread Safety Definitions in Programmer's Guide) michael@0: * RETURNS: michael@0: * Returns NULL if the function succeeds. michael@0: * Returns a VerifyNode Error if the function fails in a non-fatal way. michael@0: * Returns a Fatal Error if the function fails in an unrecoverable way. michael@0: */ michael@0: static PKIX_Error * michael@0: pkix_VerifyNode_ToString_Helper( michael@0: PKIX_VerifyNode *rootNode, michael@0: PKIX_PL_String *indent, michael@0: PKIX_PL_String **pTreeString, michael@0: void *plContext) michael@0: { michael@0: PKIX_PL_String *nextIndentFormat = NULL; michael@0: PKIX_PL_String *thisNodeFormat = NULL; michael@0: PKIX_PL_String *childrenFormat = NULL; michael@0: PKIX_PL_String *nextIndentString = NULL; michael@0: PKIX_PL_String *resultString = NULL; michael@0: PKIX_PL_String *thisItemString = NULL; michael@0: PKIX_PL_String *childString = NULL; michael@0: PKIX_VerifyNode *childNode = NULL; michael@0: PKIX_UInt32 numberOfChildren = 0; michael@0: PKIX_UInt32 childIndex = 0; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_VerifyNode_ToString_Helper"); michael@0: michael@0: PKIX_NULLCHECK_TWO(rootNode, pTreeString); michael@0: michael@0: /* Create a string for this node */ michael@0: PKIX_CHECK(pkix_SingleVerifyNode_ToString michael@0: (rootNode, &thisItemString, plContext), michael@0: PKIX_ERRORINSINGLEVERIFYNODETOSTRING); michael@0: michael@0: if (indent) { michael@0: PKIX_CHECK(PKIX_PL_String_Create michael@0: (PKIX_ESCASCII, michael@0: "%s%s", michael@0: 0, michael@0: &thisNodeFormat, michael@0: plContext), michael@0: PKIX_ERRORCREATINGFORMATSTRING); michael@0: michael@0: PKIX_CHECK(PKIX_PL_Sprintf michael@0: (&resultString, michael@0: plContext, michael@0: thisNodeFormat, michael@0: indent, michael@0: thisItemString), michael@0: PKIX_ERRORINSPRINTF); michael@0: } else { michael@0: PKIX_CHECK(PKIX_PL_String_Create michael@0: (PKIX_ESCASCII, michael@0: "%s", michael@0: 0, michael@0: &thisNodeFormat, michael@0: plContext), michael@0: PKIX_ERRORCREATINGFORMATSTRING); michael@0: michael@0: PKIX_CHECK(PKIX_PL_Sprintf michael@0: (&resultString, michael@0: plContext, michael@0: thisNodeFormat, michael@0: thisItemString), michael@0: PKIX_ERRORINSPRINTF); michael@0: } michael@0: michael@0: PKIX_DECREF(thisItemString); michael@0: thisItemString = resultString; michael@0: michael@0: /* if no children, we are done */ michael@0: if (rootNode->children) { michael@0: PKIX_CHECK(PKIX_List_GetLength michael@0: (rootNode->children, &numberOfChildren, plContext), michael@0: PKIX_LISTGETLENGTHFAILED); michael@0: } michael@0: michael@0: if (numberOfChildren != 0) { michael@0: /* michael@0: * We create a string for each child in turn, michael@0: * concatenating them to thisItemString. michael@0: */ michael@0: michael@0: /* Prepare an indent string for each child */ michael@0: if (indent) { michael@0: PKIX_CHECK(PKIX_PL_String_Create michael@0: (PKIX_ESCASCII, michael@0: "%s. ", michael@0: 0, michael@0: &nextIndentFormat, michael@0: plContext), michael@0: PKIX_ERRORCREATINGFORMATSTRING); michael@0: michael@0: PKIX_CHECK(PKIX_PL_Sprintf michael@0: (&nextIndentString, michael@0: plContext, michael@0: nextIndentFormat, michael@0: indent), michael@0: PKIX_ERRORINSPRINTF); michael@0: } else { michael@0: PKIX_CHECK(PKIX_PL_String_Create michael@0: (PKIX_ESCASCII, michael@0: ". ", michael@0: 0, michael@0: &nextIndentString, michael@0: plContext), michael@0: PKIX_ERRORCREATINGINDENTSTRING); michael@0: } michael@0: michael@0: /* Prepare the format for concatenation. */ michael@0: PKIX_CHECK(PKIX_PL_String_Create michael@0: (PKIX_ESCASCII, michael@0: "%s\n%s", michael@0: 0, michael@0: &childrenFormat, michael@0: plContext), michael@0: PKIX_ERRORCREATINGFORMATSTRING); michael@0: michael@0: for (childIndex = 0; michael@0: childIndex < numberOfChildren; michael@0: childIndex++) { michael@0: PKIX_CHECK(PKIX_List_GetItem michael@0: (rootNode->children, michael@0: childIndex, michael@0: (PKIX_PL_Object **)&childNode, michael@0: plContext), michael@0: PKIX_LISTGETITEMFAILED); michael@0: michael@0: PKIX_CHECK(pkix_VerifyNode_ToString_Helper michael@0: (childNode, michael@0: nextIndentString, michael@0: &childString, michael@0: plContext), michael@0: PKIX_ERRORCREATINGCHILDSTRING); michael@0: michael@0: michael@0: PKIX_CHECK(PKIX_PL_Sprintf michael@0: (&resultString, michael@0: plContext, michael@0: childrenFormat, michael@0: thisItemString, michael@0: childString), michael@0: PKIX_ERRORINSPRINTF); michael@0: michael@0: PKIX_DECREF(childNode); michael@0: PKIX_DECREF(childString); michael@0: PKIX_DECREF(thisItemString); michael@0: michael@0: thisItemString = resultString; michael@0: } michael@0: } michael@0: michael@0: *pTreeString = thisItemString; michael@0: michael@0: cleanup: michael@0: if (PKIX_ERROR_RECEIVED) { michael@0: PKIX_DECREF(thisItemString); michael@0: } michael@0: michael@0: PKIX_DECREF(nextIndentFormat); michael@0: PKIX_DECREF(thisNodeFormat); michael@0: PKIX_DECREF(childrenFormat); michael@0: PKIX_DECREF(nextIndentString); michael@0: PKIX_DECREF(childString); michael@0: PKIX_DECREF(childNode); michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: pkix_VerifyNode_ToString michael@0: * (see comments for PKIX_PL_ToStringCallback in pkix_pl_system.h) michael@0: */ michael@0: static PKIX_Error * michael@0: pkix_VerifyNode_ToString( michael@0: PKIX_PL_Object *object, michael@0: PKIX_PL_String **pTreeString, michael@0: void *plContext) michael@0: { michael@0: PKIX_VerifyNode *rootNode = NULL; michael@0: PKIX_PL_String *resultString = NULL; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_VerifyNode_ToString"); michael@0: michael@0: PKIX_NULLCHECK_TWO(object, pTreeString); michael@0: michael@0: PKIX_CHECK(pkix_CheckType(object, PKIX_VERIFYNODE_TYPE, plContext), michael@0: PKIX_OBJECTNOTVERIFYNODE); michael@0: michael@0: rootNode = (PKIX_VerifyNode *)object; michael@0: michael@0: PKIX_CHECK(pkix_VerifyNode_ToString_Helper michael@0: (rootNode, NULL, &resultString, plContext), michael@0: PKIX_ERRORCREATINGSUBTREESTRING); michael@0: michael@0: *pTreeString = resultString; michael@0: michael@0: cleanup: michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: pkix_VerifyNode_Destroy michael@0: * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h) michael@0: */ michael@0: static PKIX_Error * michael@0: pkix_VerifyNode_Destroy( michael@0: PKIX_PL_Object *object, michael@0: void *plContext) michael@0: { michael@0: PKIX_VerifyNode *node = NULL; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_VerifyNode_Destroy"); michael@0: michael@0: PKIX_NULLCHECK_ONE(object); michael@0: michael@0: PKIX_CHECK(pkix_CheckType(object, PKIX_VERIFYNODE_TYPE, plContext), michael@0: PKIX_OBJECTNOTVERIFYNODE); michael@0: michael@0: node = (PKIX_VerifyNode*)object; michael@0: michael@0: PKIX_DECREF(node->verifyCert); michael@0: PKIX_DECREF(node->children); michael@0: PKIX_DECREF(node->error); michael@0: michael@0: node->depth = 0; michael@0: michael@0: cleanup: michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: pkix_SingleVerifyNode_Hashcode michael@0: * DESCRIPTION: michael@0: * michael@0: * Computes the hashcode of the attributes of the VerifyNode pointed to by michael@0: * "node", other than its parents and children, and stores the result at michael@0: * "pHashcode". michael@0: * michael@0: * PARAMETERS: michael@0: * "node" michael@0: * Address of VerifyNode to be hashcoded; must be non-NULL michael@0: * "pHashcode" michael@0: * Address where UInt32 result will be stored; must be non-NULL michael@0: * "plContext" michael@0: * Platform-specific context pointer. michael@0: * THREAD SAFETY: michael@0: * Conditionally Thread Safe michael@0: * (see Thread Safety Definitions in Programmer's Guide) michael@0: * RETURNS: michael@0: * Returns NULL if function succeeds michael@0: * Returns a VerifyNode Error if the function fails in a non-fatal way. michael@0: * Returns a Fatal Error if the function fails in a fatal way michael@0: */ michael@0: static PKIX_Error * michael@0: pkix_SingleVerifyNode_Hashcode( michael@0: PKIX_VerifyNode *node, michael@0: PKIX_UInt32 *pHashcode, michael@0: void *plContext) michael@0: { michael@0: PKIX_UInt32 errorHash = 0; michael@0: PKIX_UInt32 nodeHash = 0; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_SingleVerifyNode_Hashcode"); michael@0: PKIX_NULLCHECK_TWO(node, pHashcode); michael@0: michael@0: PKIX_HASHCODE michael@0: (node->verifyCert, michael@0: &nodeHash, michael@0: plContext, michael@0: PKIX_FAILUREHASHINGCERT); michael@0: michael@0: PKIX_CHECK(PKIX_PL_Object_Hashcode michael@0: ((PKIX_PL_Object *)node->error, michael@0: &errorHash, michael@0: plContext), michael@0: PKIX_FAILUREHASHINGERROR); michael@0: michael@0: nodeHash = 31*nodeHash + errorHash; michael@0: *pHashcode = nodeHash; michael@0: michael@0: cleanup: michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: pkix_VerifyNode_Hashcode michael@0: * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h) michael@0: */ michael@0: static PKIX_Error * michael@0: pkix_VerifyNode_Hashcode( michael@0: PKIX_PL_Object *object, michael@0: PKIX_UInt32 *pHashcode, michael@0: void *plContext) michael@0: { michael@0: PKIX_VerifyNode *node = NULL; michael@0: PKIX_UInt32 childrenHash = 0; michael@0: PKIX_UInt32 nodeHash = 0; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_VerifyNode_Hashcode"); michael@0: PKIX_NULLCHECK_TWO(object, pHashcode); michael@0: michael@0: PKIX_CHECK(pkix_CheckType michael@0: (object, PKIX_VERIFYNODE_TYPE, plContext), michael@0: PKIX_OBJECTNOTVERIFYNODE); michael@0: michael@0: node = (PKIX_VerifyNode *)object; michael@0: michael@0: PKIX_CHECK(pkix_SingleVerifyNode_Hashcode michael@0: (node, &nodeHash, plContext), michael@0: PKIX_SINGLEVERIFYNODEHASHCODEFAILED); michael@0: michael@0: PKIX_HASHCODE michael@0: (node->children, michael@0: &childrenHash, michael@0: plContext, michael@0: PKIX_OBJECTHASHCODEFAILED); michael@0: michael@0: nodeHash = 31*nodeHash + childrenHash; michael@0: michael@0: *pHashcode = nodeHash; michael@0: michael@0: cleanup: michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: pkix_SingleVerifyNode_Equals michael@0: * DESCRIPTION: michael@0: * michael@0: * Compares for equality the components of the VerifyNode pointed to by michael@0: * "firstPN", other than its parents and children, with those of the michael@0: * VerifyNode pointed to by "secondPN" and stores the result at "pResult" michael@0: * (PKIX_TRUE if equal; PKIX_FALSE if not). michael@0: * michael@0: * PARAMETERS: michael@0: * "firstPN" michael@0: * Address of first of the VerifyNodes to be compared; must be non-NULL michael@0: * "secondPN" michael@0: * Address of second of the VerifyNodes to be compared; must be non-NULL michael@0: * "pResult" michael@0: * Address where Boolean will be stored; must be non-NULL michael@0: * "plContext" michael@0: * Platform-specific context pointer. michael@0: * THREAD SAFETY: michael@0: * Conditionally Thread Safe michael@0: * (see Thread Safety Definitions in Programmer's Guide) michael@0: * RETURNS: michael@0: * Returns NULL if function succeeds michael@0: * Returns a VerifyNode Error if the function fails in a non-fatal way. michael@0: * Returns a Fatal Error if the function fails in a fatal way michael@0: */ michael@0: static PKIX_Error * michael@0: pkix_SingleVerifyNode_Equals( michael@0: PKIX_VerifyNode *firstVN, michael@0: PKIX_VerifyNode *secondVN, michael@0: PKIX_Boolean *pResult, michael@0: void *plContext) michael@0: { michael@0: PKIX_Boolean compResult = PKIX_FALSE; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_SingleVerifyNode_Equals"); michael@0: PKIX_NULLCHECK_THREE(firstVN, secondVN, pResult); michael@0: michael@0: /* If both references are identical, they must be equal */ michael@0: if (firstVN == secondVN) { michael@0: compResult = PKIX_TRUE; michael@0: goto cleanup; michael@0: } michael@0: michael@0: /* michael@0: * It seems we have to do the comparisons. Do michael@0: * the easiest ones first. michael@0: */ michael@0: if ((firstVN->depth) != (secondVN->depth)) { michael@0: goto cleanup; michael@0: } michael@0: michael@0: /* These fields must be non-NULL */ michael@0: PKIX_NULLCHECK_TWO(firstVN->verifyCert, secondVN->verifyCert); michael@0: michael@0: PKIX_EQUALS michael@0: (firstVN->verifyCert, michael@0: secondVN->verifyCert, michael@0: &compResult, michael@0: plContext, michael@0: PKIX_OBJECTEQUALSFAILED); michael@0: michael@0: if (compResult == PKIX_FALSE) { michael@0: goto cleanup; michael@0: } michael@0: michael@0: PKIX_EQUALS michael@0: (firstVN->error, michael@0: secondVN->error, michael@0: &compResult, michael@0: plContext, michael@0: PKIX_OBJECTEQUALSFAILED); michael@0: michael@0: cleanup: michael@0: michael@0: *pResult = compResult; michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: pkix_VerifyNode_Equals michael@0: * (see comments for PKIX_PL_Equals_Callback in pkix_pl_system.h) michael@0: */ michael@0: static PKIX_Error * michael@0: pkix_VerifyNode_Equals( michael@0: PKIX_PL_Object *firstObject, michael@0: PKIX_PL_Object *secondObject, michael@0: PKIX_Boolean *pResult, michael@0: void *plContext) michael@0: { michael@0: PKIX_VerifyNode *firstVN = NULL; michael@0: PKIX_VerifyNode *secondVN = NULL; michael@0: PKIX_UInt32 secondType; michael@0: PKIX_Boolean compResult = PKIX_FALSE; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_VerifyNode_Equals"); michael@0: PKIX_NULLCHECK_THREE(firstObject, secondObject, pResult); michael@0: michael@0: /* test that firstObject is a VerifyNode */ michael@0: PKIX_CHECK(pkix_CheckType michael@0: (firstObject, PKIX_VERIFYNODE_TYPE, plContext), michael@0: PKIX_FIRSTOBJECTNOTVERIFYNODE); michael@0: michael@0: /* michael@0: * Since we know firstObject is a VerifyNode, michael@0: * if both references are identical, they must be equal michael@0: */ michael@0: if (firstObject == secondObject){ michael@0: compResult = PKIX_TRUE; michael@0: goto cleanup; michael@0: } michael@0: michael@0: /* michael@0: * If secondObject isn't a VerifyNode, we michael@0: * don't throw an error. We simply return FALSE. michael@0: */ michael@0: PKIX_CHECK(PKIX_PL_Object_GetType michael@0: (secondObject, &secondType, plContext), michael@0: PKIX_COULDNOTGETTYPEOFSECONDARGUMENT); michael@0: michael@0: if (secondType != PKIX_VERIFYNODE_TYPE) { michael@0: goto cleanup; michael@0: } michael@0: michael@0: /* michael@0: * Oh, well, we have to do the comparisons. Do michael@0: * the easiest ones first. michael@0: */ michael@0: firstVN = (PKIX_VerifyNode *)firstObject; michael@0: secondVN = (PKIX_VerifyNode *)secondObject; michael@0: michael@0: PKIX_CHECK(pkix_SingleVerifyNode_Equals michael@0: (firstVN, secondVN, &compResult, plContext), michael@0: PKIX_SINGLEVERIFYNODEEQUALSFAILED); michael@0: michael@0: if (compResult == PKIX_FALSE) { michael@0: goto cleanup; michael@0: } michael@0: michael@0: PKIX_EQUALS michael@0: (firstVN->children, michael@0: secondVN->children, michael@0: &compResult, michael@0: plContext, michael@0: PKIX_OBJECTEQUALSFAILEDONCHILDREN); michael@0: michael@0: cleanup: michael@0: michael@0: *pResult = compResult; michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: pkix_VerifyNode_DuplicateHelper michael@0: * DESCRIPTION: michael@0: * michael@0: * Duplicates the VerifyNode whose address is pointed to by "original", michael@0: * and stores the result at "pNewNode", if a non-NULL pointer is provided michael@0: * for "pNewNode". In addition, the created VerifyNode is added as a child michael@0: * to "parent", if a non-NULL pointer is provided for "parent". Then this michael@0: * function is called recursively to duplicate each of the children of michael@0: * "original". At the top level this function is called with a null michael@0: * "parent" and a non-NULL "pNewNode". Below the top level "parent" will michael@0: * be non-NULL and "pNewNode" will be NULL. michael@0: * michael@0: * PARAMETERS: michael@0: * "original" michael@0: * Address of VerifyNode to be copied; must be non-NULL michael@0: * "parent" michael@0: * Address of VerifyNode to which the created node is to be added as a michael@0: * child; NULL for the top-level call and non-NULL below the top level michael@0: * "pNewNode" michael@0: * Address to store the node created; should be NULL if "parent" is michael@0: * non-NULL and vice versa michael@0: * "plContext" michael@0: * Platform-specific context pointer. michael@0: * THREAD SAFETY: michael@0: * Conditionally Thread Safe michael@0: * (see Thread Safety Definitions in Programmer's Guide) michael@0: * RETURNS: michael@0: * Returns NULL if function succeeds michael@0: * Returns a VerifyNode Error if the function fails in a non-fatal way. michael@0: * Returns a Fatal Error if the function fails in a fatal way michael@0: */ michael@0: static PKIX_Error * michael@0: pkix_VerifyNode_DuplicateHelper( michael@0: PKIX_VerifyNode *original, michael@0: PKIX_VerifyNode *parent, michael@0: PKIX_VerifyNode **pNewNode, michael@0: void *plContext) michael@0: { michael@0: PKIX_UInt32 numChildren = 0; michael@0: PKIX_UInt32 childIndex = 0; michael@0: PKIX_List *children = NULL; /* List of PKIX_VerifyNode */ michael@0: PKIX_VerifyNode *copy = NULL; michael@0: PKIX_VerifyNode *child = NULL; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_VerifyNode_DuplicateHelper"); michael@0: michael@0: PKIX_NULLCHECK_TWO michael@0: (original, original->verifyCert); michael@0: michael@0: /* michael@0: * These components are immutable, so copying the pointers michael@0: * is sufficient. The create function increments the reference michael@0: * counts as it stores the pointers into the new object. michael@0: */ michael@0: PKIX_CHECK(pkix_VerifyNode_Create michael@0: (original->verifyCert, michael@0: original->depth, michael@0: original->error, michael@0: ©, michael@0: plContext), michael@0: PKIX_VERIFYNODECREATEFAILED); michael@0: michael@0: /* Are there any children to duplicate? */ michael@0: children = original->children; michael@0: michael@0: if (children) { michael@0: PKIX_CHECK(PKIX_List_GetLength(children, &numChildren, plContext), michael@0: PKIX_LISTGETLENGTHFAILED); michael@0: } michael@0: michael@0: for (childIndex = 0; childIndex < numChildren; childIndex++) { michael@0: PKIX_CHECK(PKIX_List_GetItem michael@0: (children, michael@0: childIndex, michael@0: (PKIX_PL_Object **)&child, michael@0: plContext), michael@0: PKIX_LISTGETITEMFAILED); michael@0: michael@0: PKIX_CHECK(pkix_VerifyNode_DuplicateHelper michael@0: (child, copy, NULL, plContext), michael@0: PKIX_VERIFYNODEDUPLICATEHELPERFAILED); michael@0: michael@0: PKIX_DECREF(child); michael@0: } michael@0: michael@0: if (pNewNode) { michael@0: *pNewNode = copy; michael@0: copy = NULL; /* no DecRef if we give our handle away */ michael@0: } michael@0: michael@0: cleanup: michael@0: PKIX_DECREF(copy); michael@0: PKIX_DECREF(child); michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: pkix_VerifyNode_Duplicate michael@0: * (see comments for PKIX_PL_Duplicate_Callback in pkix_pl_system.h) michael@0: */ michael@0: static PKIX_Error * michael@0: pkix_VerifyNode_Duplicate( michael@0: PKIX_PL_Object *object, michael@0: PKIX_PL_Object **pNewObject, michael@0: void *plContext) michael@0: { michael@0: PKIX_VerifyNode *original = NULL; michael@0: PKIX_VerifyNode *copy = NULL; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_VerifyNode_Duplicate"); michael@0: michael@0: PKIX_NULLCHECK_TWO(object, pNewObject); michael@0: michael@0: PKIX_CHECK(pkix_CheckType michael@0: (object, PKIX_VERIFYNODE_TYPE, plContext), michael@0: PKIX_OBJECTNOTVERIFYNODE); michael@0: michael@0: original = (PKIX_VerifyNode *)object; michael@0: michael@0: PKIX_CHECK(pkix_VerifyNode_DuplicateHelper michael@0: (original, NULL, ©, plContext), michael@0: PKIX_VERIFYNODEDUPLICATEHELPERFAILED); michael@0: michael@0: *pNewObject = (PKIX_PL_Object *)copy; michael@0: michael@0: cleanup: michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: pkix_VerifyNode_RegisterSelf michael@0: * DESCRIPTION: michael@0: * michael@0: * Registers PKIX_VERIFYNODE_TYPE and its related michael@0: * functions with systemClasses[] michael@0: * michael@0: * THREAD SAFETY: michael@0: * Not Thread Safe - for performance and complexity reasons michael@0: * michael@0: * Since this function is only called by PKIX_PL_Initialize, michael@0: * which should only be called once, it is acceptable that michael@0: * this function is not thread-safe. michael@0: */ michael@0: PKIX_Error * michael@0: pkix_VerifyNode_RegisterSelf(void *plContext) michael@0: { michael@0: michael@0: extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES]; michael@0: pkix_ClassTable_Entry entry; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "pkix_VerifyNode_RegisterSelf"); michael@0: michael@0: entry.description = "VerifyNode"; michael@0: entry.objCounter = 0; michael@0: entry.typeObjectSize = sizeof(PKIX_VerifyNode); michael@0: entry.destructor = pkix_VerifyNode_Destroy; michael@0: entry.equalsFunction = pkix_VerifyNode_Equals; michael@0: entry.hashcodeFunction = pkix_VerifyNode_Hashcode; michael@0: entry.toStringFunction = pkix_VerifyNode_ToString; michael@0: entry.comparator = NULL; michael@0: entry.duplicateFunction = pkix_VerifyNode_Duplicate; michael@0: michael@0: systemClasses[PKIX_VERIFYNODE_TYPE] = entry; michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* --Public-VerifyNode-Functions----------------------------------- */ michael@0: michael@0: /* michael@0: * FUNCTION: PKIX_VerifyNode_SetError michael@0: * DESCRIPTION: michael@0: * michael@0: * This function sets the Error field of the VerifyNode pointed to by "node" michael@0: * to contain the Error pointed to by "error". michael@0: * michael@0: * PARAMETERS: michael@0: * "node" michael@0: * The address of the VerifyNode to be modified. Must be non-NULL. michael@0: * "error" michael@0: * The address of the Error to be stored. michael@0: * "plContext" michael@0: * Platform-specific context pointer. michael@0: * THREAD SAFETY: michael@0: * Thread Safe (see Thread Safety Definitions in Programmer's Guide) michael@0: * RETURNS: michael@0: * Returns NULL if the function succeeds. michael@0: * Returns a Fatal Error if the function fails in an unrecoverable way. michael@0: */ michael@0: PKIX_Error * michael@0: pkix_VerifyNode_SetError( michael@0: PKIX_VerifyNode *node, michael@0: PKIX_Error *error, michael@0: void *plContext) michael@0: { michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "PKIX_VerifyNode_SetError"); michael@0: michael@0: PKIX_NULLCHECK_TWO(node, error); michael@0: michael@0: PKIX_DECREF(node->error); /* should have been NULL */ michael@0: PKIX_INCREF(error); michael@0: node->error = error; michael@0: michael@0: cleanup: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: } michael@0: michael@0: /* michael@0: * FUNCTION: PKIX_VerifyNode_FindError michael@0: * DESCRIPTION: michael@0: * michael@0: * Finds meaningful error in the log. For now, just returns the first michael@0: * error it finds in. In the future the function should be changed to michael@0: * return a top priority error. michael@0: * michael@0: * PARAMETERS: michael@0: * "node" michael@0: * The address of the VerifyNode to be modified. Must be non-NULL. michael@0: * "error" michael@0: * The address of a pointer the error will be returned to. michael@0: * "plContext" michael@0: * Platform-specific context pointer. michael@0: * THREAD SAFETY: michael@0: * Thread Safe (see Thread Safety Definitions in Programmer's Guide) michael@0: * RETURNS: michael@0: * Returns NULL if the function succeeds. michael@0: * Returns a Fatal Error if the function fails in an unrecoverable way. michael@0: */ michael@0: PKIX_Error * michael@0: pkix_VerifyNode_FindError( michael@0: PKIX_VerifyNode *node, michael@0: PKIX_Error **error, michael@0: void *plContext) michael@0: { michael@0: PKIX_VerifyNode *childNode = NULL; michael@0: michael@0: PKIX_ENTER(VERIFYNODE, "PKIX_VerifyNode_FindError"); michael@0: michael@0: /* Make sure the return address is initialized with NULL */ michael@0: PKIX_DECREF(*error); michael@0: michael@0: if (!node) michael@0: goto cleanup; michael@0: michael@0: /* First, try to get error from lowest level. */ michael@0: if (node->children) { michael@0: PKIX_UInt32 length = 0; michael@0: PKIX_UInt32 index = 0; michael@0: michael@0: PKIX_CHECK( michael@0: PKIX_List_GetLength(node->children, &length, michael@0: plContext), michael@0: PKIX_LISTGETLENGTHFAILED); michael@0: for (index = 0;index < length;index++) { michael@0: PKIX_CHECK( michael@0: PKIX_List_GetItem(node->children, index, michael@0: (PKIX_PL_Object**)&childNode, plContext), michael@0: PKIX_LISTGETITEMFAILED); michael@0: if (!childNode) michael@0: continue; michael@0: PKIX_CHECK( michael@0: pkix_VerifyNode_FindError(childNode, error, michael@0: plContext), michael@0: PKIX_VERIFYNODEFINDERRORFAILED); michael@0: PKIX_DECREF(childNode); michael@0: if (*error) { michael@0: goto cleanup; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (node->error && node->error->plErr) { michael@0: PKIX_INCREF(node->error); michael@0: *error = node->error; michael@0: } michael@0: michael@0: cleanup: michael@0: PKIX_DECREF(childNode); michael@0: michael@0: PKIX_RETURN(VERIFYNODE); michael@0: }