/** * xmlSecKeyDataIdListDebugXmlDump: * @list: the pointer to key data ids list. * @output: the pointer to output FILE. * * Prints binary key data debug information to @output in XML format. */ void xmlSecKeyDataIdListDebugXmlDump(xmlSecPtrListPtr list, FILE* output) { xmlSecKeyDataId dataId; xmlSecSize i, size; xmlSecAssert(xmlSecPtrListCheckId(list, xmlSecKeyDataIdListId)); xmlSecAssert(output != NULL); fprintf(output, "<KeyDataIdsList>\n"); size = xmlSecPtrListGetSize(list); for(i = 0; i < size; ++i) { dataId = (xmlSecKeyDataId)xmlSecPtrListGetItem(list, i); xmlSecAssert(dataId != NULL); xmlSecAssert(dataId->name != NULL); fprintf(output, "<DataId name=\""); xmlSecPrintXmlString(output, dataId->name); fprintf(output, "\"/>"); } fprintf(output, "</KeyDataIdsList>\n"); }
/** * xmlSecKeyAdoptData: * @key: the pointer to key. * @data: the pointer to key data. * * Adds @data to the @key. The @data object will be destroyed * by @key. * * Returns: 0 on success or a negative value otherwise. */ int xmlSecKeyAdoptData(xmlSecKeyPtr key, xmlSecKeyDataPtr data) { xmlSecKeyDataPtr tmp; xmlSecSize pos, size; xmlSecAssert2(key != NULL, -1); xmlSecAssert2(xmlSecKeyDataIsValid(data), -1); /* special cases */ if(data->id == xmlSecKeyDataValueId) { if(key->value != NULL) { xmlSecKeyDataDestroy(key->value); } key->value = data; return(0); } if(key->dataList == NULL) { key->dataList = xmlSecPtrListCreate(xmlSecKeyDataListId); if(key->dataList == NULL) { xmlSecError(XMLSEC_ERRORS_HERE, NULL, "xmlSecPtrListCreate", XMLSEC_ERRORS_R_XMLSEC_FAILED, XMLSEC_ERRORS_NO_MESSAGE); return(-1); } } size = xmlSecPtrListGetSize(key->dataList); for(pos = 0; pos < size; ++pos) { tmp = (xmlSecKeyDataPtr)xmlSecPtrListGetItem(key->dataList, pos); if((tmp != NULL) && (tmp->id == data->id)) { return(xmlSecPtrListSet(key->dataList, data, pos)); } } return(xmlSecPtrListAdd(key->dataList, data)); }
static xmlSecKeyPtr xmlSecSimpleKeysStoreFindKey(xmlSecKeyStorePtr store, const xmlChar* name, xmlSecKeyInfoCtxPtr keyInfoCtx) { xmlSecPtrListPtr list; xmlSecKeyPtr key; xmlSecSize pos, size; xmlSecAssert2(xmlSecKeyStoreCheckId(store, xmlSecSimpleKeysStoreId), NULL); xmlSecAssert2(keyInfoCtx != NULL, NULL); list = xmlSecSimpleKeysStoreGetList(store); xmlSecAssert2(xmlSecPtrListCheckId(list, xmlSecKeyPtrListId), NULL); size = xmlSecPtrListGetSize(list); for(pos = 0; pos < size; ++pos) { key = (xmlSecKeyPtr)xmlSecPtrListGetItem(list, pos); if((key != NULL) && (xmlSecKeyMatch(key, name, &(keyInfoCtx->keyReq)) == 1)) { return(xmlSecKeyDuplicate(key)); } } return(NULL); }
/** * xmlSecKeyDataIdListDebugDump: * @list: the pointer to key data ids list. * @output: the pointer to output FILE. * * Prints binary key data debug information to @output. */ void xmlSecKeyDataIdListDebugDump(xmlSecPtrListPtr list, FILE* output) { xmlSecKeyDataId dataId; xmlSecSize i, size; xmlSecAssert(xmlSecPtrListCheckId(list, xmlSecKeyDataIdListId)); xmlSecAssert(output != NULL); size = xmlSecPtrListGetSize(list); for(i = 0; i < size; ++i) { dataId = (xmlSecKeyDataId)xmlSecPtrListGetItem(list, i); xmlSecAssert(dataId != NULL); xmlSecAssert(dataId->name != NULL); if(i > 0) { fprintf(output, ",\"%s\"", dataId->name); } else { fprintf(output, "\"%s\"", dataId->name); } } fprintf(output, "\n"); }
/** * xmlSecKeyDataIdListFindByName: * @list: the pointer to key data ids list. * @name: the desired key data klass name. * @usage: the desired key data usage. * * Lookups data klass in the list with given @name and @usage in @list. * * Returns: key data klass is found and NULL otherwise. */ xmlSecKeyDataId xmlSecKeyDataIdListFindByName(xmlSecPtrListPtr list, const xmlChar* name, xmlSecKeyDataUsage usage) { xmlSecKeyDataId dataId; xmlSecSize i, size; xmlSecAssert2(xmlSecPtrListCheckId(list, xmlSecKeyDataIdListId), xmlSecKeyDataIdUnknown); xmlSecAssert2(name != NULL, xmlSecKeyDataIdUnknown); size = xmlSecPtrListGetSize(list); for(i = 0; i < size; ++i) { dataId = (xmlSecKeyDataId)xmlSecPtrListGetItem(list, i); xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, xmlSecKeyDataIdUnknown); if(((usage & dataId->usage) != 0) && (dataId->name != NULL) && xmlStrEqual(name, BAD_CAST dataId->name)) { return(dataId); } } return(xmlSecKeyDataIdUnknown); }
/** * xmlSecKeyGetData: * @key: the pointer to key. * @dataId: the requested data klass. * * Gets key's data. * * Returns: additional data associated with the @key (see also * #xmlSecKeyAdoptData function). */ xmlSecKeyDataPtr xmlSecKeyGetData(xmlSecKeyPtr key, xmlSecKeyDataId dataId) { xmlSecAssert2(key != NULL, NULL); xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, NULL); /* special cases */ if(dataId == xmlSecKeyDataValueId) { return(key->value); } else if(key->dataList != NULL) { xmlSecKeyDataPtr tmp; xmlSecSize pos, size; size = xmlSecPtrListGetSize(key->dataList); for(pos = 0; pos < size; ++pos) { tmp = (xmlSecKeyDataPtr)xmlSecPtrListGetItem(key->dataList, pos); if((tmp != NULL) && (tmp->id == dataId)) { return(tmp); } } } return(NULL); }
/** * xmlSecSimpleKeysStoreSave: * @store: the pointer to simple keys store. * @filename: the filename. * @type: the saved keys type (public, private, ...). * * Writes keys from @store to an XML file. * * Returns 0 on success or a negative value if an error occurs. */ int xmlSecSimpleKeysStoreSave(xmlSecKeyStorePtr store, const char *filename, xmlSecKeyDataType type) { xmlSecKeyInfoCtx keyInfoCtx; xmlSecPtrListPtr list; xmlSecKeyPtr key; xmlSecSize i, keysSize; xmlDocPtr doc; xmlNodePtr cur; xmlSecKeyDataPtr data; xmlSecPtrListPtr idsList; xmlSecKeyDataId dataId; xmlSecSize idsSize, j; int ret; xmlSecAssert2(xmlSecKeyStoreCheckId(store, xmlSecSimpleKeysStoreId), -1); xmlSecAssert2(filename != NULL, -1); list = xmlSecSimpleKeysStoreGetList(store); xmlSecAssert2(xmlSecPtrListCheckId(list, xmlSecKeyPtrListId), -1); /* create doc */ doc = xmlSecCreateTree(BAD_CAST "Keys", xmlSecNs); if(doc == NULL) { xmlSecErr_a_ignorar5(XMLSEC_ERRORS_HERE, xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)), "xmlSecCreateTree", XMLSEC_ERRORS_R_XMLSEC_FAILED, XMLSEC_ERRORS_NO_MESSAGE); return(-1); } idsList = xmlSecKeyDataIdsGet(); xmlSecAssert2(idsList != NULL, -1); keysSize = xmlSecPtrListGetSize(list); idsSize = xmlSecPtrListGetSize(idsList); for(i = 0; i < keysSize; ++i) { key = (xmlSecKeyPtr)xmlSecPtrListGetItem(list, i); xmlSecAssert2(key != NULL, -1); cur = xmlSecAddChild(xmlDocGetRootElement(doc), xmlSecNodeKeyInfo, xmlSecDSigNs); if(cur == NULL) { xmlSecErr_a_ignorar6(XMLSEC_ERRORS_HERE, xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)), "xmlSecAddChild", XMLSEC_ERRORS_R_XMLSEC_FAILED, "node=%s", xmlSecErrorsSafeString(xmlSecNodeKeyInfo)); xmlFreeDoc(doc); return(-1); } /* special data key name */ if(xmlSecKeyGetName(key) != NULL) { if(xmlSecAddChild(cur, xmlSecNodeKeyName, xmlSecDSigNs) == NULL) { xmlSecErr_a_ignorar6(XMLSEC_ERRORS_HERE, xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)), "xmlSecAddChild", XMLSEC_ERRORS_R_XMLSEC_FAILED, "node=%s", xmlSecErrorsSafeString(xmlSecNodeKeyName)); xmlFreeDoc(doc); return(-1); } } /* create nodes for other keys data */ for(j = 0; j < idsSize; ++j) { dataId = (xmlSecKeyDataId)xmlSecPtrListGetItem(idsList, j); xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, -1); if(dataId->dataNodeName == NULL) { continue; } data = xmlSecKeyGetData(key, dataId); if(data == NULL) { continue; } if(xmlSecAddChild(cur, dataId->dataNodeName, dataId->dataNodeNs) == NULL) { xmlSecErr_a_ignorar6(XMLSEC_ERRORS_HERE, xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)), "xmlSecAddChild", XMLSEC_ERRORS_R_XMLSEC_FAILED, "node=%s", xmlSecErrorsSafeString(dataId->dataNodeName)); xmlFreeDoc(doc); return(-1); } } ret = xmlSecKeyInfoCtxInitialize(&keyInfoCtx, NULL); if(ret < 0) { xmlSecErr_a_ignorar5(XMLSEC_ERRORS_HERE, xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)), "xmlSecKeyInfoCtxInitialize", XMLSEC_ERRORS_R_XMLSEC_FAILED, XMLSEC_ERRORS_NO_MESSAGE); xmlFreeDoc(doc); return(-1); } keyInfoCtx.mode = xmlSecKeyInfoModeWrite; keyInfoCtx.keyReq.keyId = xmlSecKeyDataIdUnknown; keyInfoCtx.keyReq.keyType = type; keyInfoCtx.keyReq.keyUsage = xmlSecKeyDataUsageAny; /* finally write key in the node */ ret = xmlSecKeyInfoNodeWrite(cur, key, &keyInfoCtx); if(ret < 0) { xmlSecErr_a_ignorar5(XMLSEC_ERRORS_HERE, xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)), "xmlSecKeyInfoNodeWrite", XMLSEC_ERRORS_R_XMLSEC_FAILED, XMLSEC_ERRORS_NO_MESSAGE); xmlSecKeyInfoCtxFinalize(&keyInfoCtx); xmlFreeDoc(doc); return(-1); } xmlSecKeyInfoCtxFinalize(&keyInfoCtx); } /* now write result */ ret = xmlSaveFormatFile(filename, doc, 1); if(ret < 0) { xmlSecErr_a_ignorar6(XMLSEC_ERRORS_HERE, xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)), "xmlSaveFormatFile", XMLSEC_ERRORS_R_XML_FAILED, "filename=%s", xmlSecErrorsSafeString(filename)); xmlFreeDoc(doc); return(-1); } xmlFreeDoc(doc); return(0); }