diff options
Diffstat (limited to 'src/keysdata.c')
-rw-r--r-- | src/keysdata.c | 271 |
1 files changed, 93 insertions, 178 deletions
diff --git a/src/keysdata.c b/src/keysdata.c index 30c800df..925521fc 100644 --- a/src/keysdata.c +++ b/src/keysdata.c @@ -1,13 +1,18 @@ -/** +/* * XML Security Library (http://www.aleksey.com/xmlsec). * - * Key data. * * This is free software; see Copyright file in the source * distribution for preciese wording. * * Copyright (C) 2002-2016 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved. */ +/** + * SECTION:keysdata + * @Short_description: Crypto key data object functions. + * @Stability: Stable + * + */ #include "globals.h" @@ -25,13 +30,13 @@ #include <xmlsec/keyinfo.h> #include <xmlsec/errors.h> - /************************************************************************** * * Global xmlSecKeyDataIds list functions * *************************************************************************/ static xmlSecPtrList xmlSecAllKeyDataIds; +static int xmlSecImportPersistKey = 0; /** * xmlSecKeyDataIdsGet: @@ -59,21 +64,13 @@ xmlSecKeyDataIdsInit(void) { ret = xmlSecPtrListInitialize(xmlSecKeyDataIdsGet(), xmlSecKeyDataIdListId); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecPtrListPtrInitialize", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "xmlSecKeyDataIdListId"); + xmlSecInternalError("xmlSecPtrListInitialize(xmlSecKeyDataIdListId)", NULL); return(-1); } ret = xmlSecKeyDataIdsRegisterDefault(); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyDataIdsRegisterDefault", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyDataIdsRegisterDefault", NULL); return(-1); } @@ -107,12 +104,8 @@ xmlSecKeyDataIdsRegister(xmlSecKeyDataId id) { ret = xmlSecPtrListAdd(xmlSecKeyDataIdsGet(), (xmlSecPtr)id); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecPtrListAdd", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "dataId=%s", - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id))); + xmlSecInternalError("xmlSecPtrListAdd", + xmlSecKeyDataKlassGetName(id)); return(-1); } @@ -131,39 +124,23 @@ xmlSecKeyDataIdsRegister(xmlSecKeyDataId id) { int xmlSecKeyDataIdsRegisterDefault(void) { if(xmlSecKeyDataIdsRegister(xmlSecKeyDataNameId) < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyDataIdsRegister", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "xmlSecKeyDataNameId"); + xmlSecInternalError("xmlSecKeyDataIdsRegister(xmlSecKeyDataNameId)", NULL); return(-1); } if(xmlSecKeyDataIdsRegister(xmlSecKeyDataValueId) < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyDataIdsRegister", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "xmlSecKeyDataValueId"); + xmlSecInternalError("xmlSecKeyDataIdsRegister(xmlSecKeyDataValueId)", NULL); return(-1); } if(xmlSecKeyDataIdsRegister(xmlSecKeyDataRetrievalMethodId) < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyDataIdsRegister", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "xmlSecKeyDataRetrievalMethodId"); + xmlSecInternalError("xmlSecKeyDataIdsRegister(xmlSecKeyDataRetrievalMethodId", NULL); return(-1); } #ifndef XMLSEC_NO_XMLENC if(xmlSecKeyDataIdsRegister(xmlSecKeyDataEncryptedKeyId) < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyDataIdsRegister", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "xmlSecKeyDataEncryptedKeyId"); + xmlSecInternalError("xmlSecKeyDataIdsRegister(xmlSecKeyDataEncryptedKeyId)", NULL); return(-1); } #endif /* XMLSEC_NO_XMLENC */ @@ -200,11 +177,8 @@ xmlSecKeyDataCreate(xmlSecKeyDataId id) { /* Allocate a new xmlSecKeyData and fill the fields. */ data = (xmlSecKeyDataPtr)xmlMalloc(id->objSize); if(data == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - NULL, - XMLSEC_ERRORS_R_MALLOC_FAILED, - "size=%d", id->objSize); + xmlSecMallocError(id->objSize, + xmlSecKeyDataKlassGetName(id)); return(NULL); } memset(data, 0, id->objSize); @@ -213,11 +187,8 @@ xmlSecKeyDataCreate(xmlSecKeyDataId id) { if(id->initialize != NULL) { ret = (id->initialize)(data); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - "id->initialize", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("id->initialize", + xmlSecKeyDataKlassGetName(id)); xmlSecKeyDataDestroy(data); return(NULL); } @@ -246,21 +217,15 @@ xmlSecKeyDataDuplicate(xmlSecKeyDataPtr data) { newData = xmlSecKeyDataCreate(data->id); if(newData == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)), - "xmlSecKeyDataCreate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyDataCreate", + xmlSecKeyDataGetName(data)); return(NULL); } ret = (data->id->duplicate)(newData, data); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)), - "id->duplicate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("id->duplicate", + xmlSecKeyDataGetName(data)); xmlSecKeyDataDestroy(newData); return(NULL); } @@ -398,11 +363,9 @@ xmlSecKeyDataGenerate(xmlSecKeyDataPtr data, xmlSecSize sizeBits, /* write data */ ret = data->id->generate(data, sizeBits, type); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)), - "id->generate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "size=%d", sizeBits); + xmlSecInternalError2("id->generate", + xmlSecKeyDataGetName(data), + "size=%d", sizeBits); return(-1); } return(0); @@ -517,11 +480,8 @@ xmlSecKeyDataBinaryValueInitialize(xmlSecKeyDataPtr data) { ret = xmlSecBufferInitialize(buffer, 0); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)), - "xmlSecBufferInitialize", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecBufferInitialize", + xmlSecKeyDataGetName(data)); return(-1); } @@ -555,11 +515,8 @@ xmlSecKeyDataBinaryValueDuplicate(xmlSecKeyDataPtr dst, xmlSecKeyDataPtr src) { xmlSecBufferGetData(buffer), xmlSecBufferGetSize(buffer)); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataGetName(dst)), - "xmlSecKeyDataBinaryValueSetBuffer", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyDataBinaryValueSetBuffer", + xmlSecKeyDataGetName(dst)); return(-1); } @@ -612,22 +569,15 @@ xmlSecKeyDataBinaryValueXmlRead(xmlSecKeyDataId id, xmlSecKeyPtr key, str = xmlNodeGetContent(node); if(str == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - xmlSecErrorsSafeString(xmlSecNodeGetName(node)), - XMLSEC_ERRORS_R_INVALID_NODE_CONTENT, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInvalidNodeContentError(node, xmlSecKeyDataKlassGetName(id), "empty"); return(-1); } /* usual trick: decode into the same buffer */ ret = xmlSecBase64Decode(str, (xmlSecByte*)str, xmlStrlen(str)); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - "xmlSecBase64Decode", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecBase64Decode", + xmlSecKeyDataKlassGetName(id)); xmlFree(str); return(-1); } @@ -639,32 +589,28 @@ xmlSecKeyDataBinaryValueXmlRead(xmlSecKeyDataId id, xmlSecKeyPtr key, xmlSecBufferPtr buffer; if(!xmlSecKeyDataCheckId(data, id)) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)), - XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecOtherError2(XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST, + xmlSecKeyDataGetName(data), + "id=%s", + xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id))); xmlFree(str); return(-1); } buffer = xmlSecKeyDataBinaryValueGetBuffer(data); - if((buffer != NULL) && ((xmlSecSize)xmlSecBufferGetSize(buffer) != len)) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)), - XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST, - "cur-data-size=%d;new-data-size=%d", - xmlSecBufferGetSize(buffer), len); + if((buffer != NULL) && (xmlSecBufferGetSize(buffer) != len)) { + xmlSecOtherError3(XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST, + xmlSecKeyDataGetName(data), + "cur-data-size=%lu;new-data-size=%lu", + (unsigned long)xmlSecBufferGetSize(buffer), + (unsigned long)len); xmlFree(str); return(-1); } if((buffer != NULL) && (len > 0) && (memcmp(xmlSecBufferGetData(buffer), str, len) != 0)) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)), - XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST, - "key already has a different value"); + xmlSecOtherError(XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST, + xmlSecKeyDataGetName(data), + "key already has a different value"); xmlFree(str); return(-1); } @@ -680,22 +626,17 @@ xmlSecKeyDataBinaryValueXmlRead(xmlSecKeyDataId id, xmlSecKeyPtr key, data = xmlSecKeyDataCreate(id); if(data == NULL ) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - "xmlSecKeyDataCreate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyDataCreate", + xmlSecKeyDataKlassGetName(id)); xmlFree(str); return(-1); } ret = xmlSecKeyDataBinaryValueSetBuffer(data, (xmlSecByte*)str, len); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - "xmlSecKeyDataBinaryValueSetBuffer", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "size=%d", len); + xmlSecInternalError2("xmlSecKeyDataBinaryValueSetBuffer", + xmlSecKeyDataKlassGetName(id), + "size=%d", len); xmlSecKeyDataDestroy(data); xmlFree(str); return(-1); @@ -703,22 +644,16 @@ xmlSecKeyDataBinaryValueXmlRead(xmlSecKeyDataId id, xmlSecKeyPtr key, xmlFree(str); if(xmlSecKeyReqMatchKeyValue(&(keyInfoCtx->keyReq), data) != 1) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - "xmlSecKeyReqMatchKeyValue", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyReqMatchKeyValue", + xmlSecKeyDataKlassGetName(id)); xmlSecKeyDataDestroy(data); return(0); } ret = xmlSecKeySetValue(key, data); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - "xmlSecKeySetValue", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeySetValue", + xmlSecKeyDataKlassGetName(id)); xmlSecKeyDataDestroy(data); return(-1); } @@ -765,11 +700,8 @@ xmlSecKeyDataBinaryValueXmlWrite(xmlSecKeyDataId id, xmlSecKeyPtr key, xmlSecBufferGetSize(buffer), keyInfoCtx->base64LineSize); if(str == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - "xmlSecBase64Encode", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecBase64Encode", + xmlSecKeyDataKlassGetName(id)); return(-1); } xmlNodeSetContent(node, str); @@ -808,30 +740,26 @@ xmlSecKeyDataBinaryValueBinRead(xmlSecKeyDataId id, xmlSecKeyPtr key, xmlSecBufferPtr buffer; if(!xmlSecKeyDataCheckId(data, id)) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)), - XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecOtherError2(XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST, + xmlSecKeyDataGetName(data), + "id=%s", + xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id))); return(-1); } buffer = xmlSecKeyDataBinaryValueGetBuffer(data); - if((buffer != NULL) && ((xmlSecSize)xmlSecBufferGetSize(buffer) != bufSize)) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)), - XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST, - "cur-data-size=%d;new-data-size=%d", - xmlSecBufferGetSize(buffer), bufSize); + if((buffer != NULL) && (xmlSecBufferGetSize(buffer) != bufSize)) { + xmlSecOtherError3(XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST, + xmlSecKeyDataGetName(data), + "cur-data-size=%lu;new-data-size=%lu", + (unsigned long)xmlSecBufferGetSize(buffer), + (unsigned long)bufSize); return(-1); } if((buffer != NULL) && (bufSize > 0) && (memcmp(xmlSecBufferGetData(buffer), buf, bufSize) != 0)) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)), - XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST, - "key already has a different value"); + xmlSecOtherError(XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST, + xmlSecKeyDataGetName(data), + "key already has a different value"); return(-1); } if(buffer != NULL) { @@ -844,42 +772,31 @@ xmlSecKeyDataBinaryValueBinRead(xmlSecKeyDataId id, xmlSecKeyPtr key, data = xmlSecKeyDataCreate(id); if(data == NULL ) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - "xmlSecKeyDataCreate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyDataCreate", + xmlSecKeyDataKlassGetName(id)); return(-1); } ret = xmlSecKeyDataBinaryValueSetBuffer(data, buf, bufSize); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - "xmlSecKeyDataBinaryValueSetBuffer", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "size=%d", bufSize); + xmlSecInternalError2("xmlSecKeyDataBinaryValueSetBuffer", + xmlSecKeyDataKlassGetName(id), + "size=%d", bufSize); xmlSecKeyDataDestroy(data); return(-1); } if(xmlSecKeyReqMatchKeyValue(&(keyInfoCtx->keyReq), data) != 1) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - "xmlSecKeyReqMatchKeyValue", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyReqMatchKeyValue", + xmlSecKeyDataKlassGetName(id)); xmlSecKeyDataDestroy(data); return(0); } ret = xmlSecKeySetValue(key, data); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - "xmlSecKeySetValue", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeySetValue", + xmlSecKeyDataKlassGetName(id)); xmlSecKeyDataDestroy(data); return(-1); } @@ -926,11 +843,8 @@ xmlSecKeyDataBinaryValueBinWrite(xmlSecKeyDataId id, xmlSecKeyPtr key, (*bufSize) = xmlSecBufferGetSize(buffer); (*buf) = (xmlSecByte*) xmlMalloc((*bufSize)); if((*buf) == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)), - NULL, - XMLSEC_ERRORS_R_MALLOC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecMallocError((*bufSize), + xmlSecKeyDataKlassGetName(id)); return(-1); } memcpy((*buf), xmlSecBufferGetData(buffer), (*bufSize)); @@ -1314,11 +1228,8 @@ xmlSecKeyDataStoreCreate(xmlSecKeyDataStoreId id) { /* Allocate a new xmlSecKeyDataStore and fill the fields. */ store = (xmlSecKeyDataStorePtr)xmlMalloc(id->objSize); if(store == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataStoreKlassGetName(id)), - NULL, - XMLSEC_ERRORS_R_MALLOC_FAILED, - "size=%d", id->objSize); + xmlSecMallocError(id->objSize, + xmlSecKeyDataStoreKlassGetName(id)); return(NULL); } memset(store, 0, id->objSize); @@ -1327,11 +1238,8 @@ xmlSecKeyDataStoreCreate(xmlSecKeyDataStoreId id) { if(id->initialize != NULL) { ret = (id->initialize)(store); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataStoreKlassGetName(id)), - "id->initialize", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("id->initialize", + xmlSecKeyDataStoreKlassGetName(id)); xmlSecKeyDataStoreDestroy(store); return(NULL); } @@ -1384,4 +1292,11 @@ xmlSecKeyDataStorePtrListGetKlass(void) { return(&xmlSecKeyDataStorePtrListKlass); } +void xmlSecImportSetPersistKey(void) { + xmlSecImportPersistKey = 1; +} + +int xmlSecImportGetPersistKey(void) { + return xmlSecImportPersistKey; +} |