diff options
Diffstat (limited to 'src/keys.c')
-rw-r--r-- | src/keys.c | 267 |
1 files changed, 71 insertions, 196 deletions
@@ -1,13 +1,19 @@ -/** +/* * XML Security Library (http://www.aleksey.com/xmlsec). * - * Keys. * * 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:keys + * @Short_description: Crypto key object functions. + * @Stability: Stable + * + */ + #include "globals.h" #include <stdlib.h> @@ -24,6 +30,7 @@ #include <xmlsec/keyinfo.h> #include <xmlsec/errors.h> + /************************************************************************** * * xmlSecKeyUseWith @@ -107,34 +114,21 @@ xmlSecKeyUseWithCreate(const xmlChar* application, const xmlChar* identifier) { /* Allocate a new xmlSecKeyUseWith and fill the fields. */ keyUseWith = (xmlSecKeyUseWithPtr)xmlMalloc(sizeof(xmlSecKeyUseWith)); if(keyUseWith == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - NULL, - XMLSEC_ERRORS_R_MALLOC_FAILED, - "sizeof(xmlSecKeyUseWith)=%d", - (int)sizeof(xmlSecKeyUseWith)); + xmlSecMallocError(sizeof(xmlSecKeyUseWith), NULL); return(NULL); } memset(keyUseWith, 0, sizeof(xmlSecKeyUseWith)); ret = xmlSecKeyUseWithInitialize(keyUseWith); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyUseWithInitialize", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyUseWithInitialize", NULL); xmlSecKeyUseWithDestroy(keyUseWith); return(NULL); } ret = xmlSecKeyUseWithSet(keyUseWith, application, identifier); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyUseWithSet", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyUseWithSet", NULL); xmlSecKeyUseWithDestroy(keyUseWith); return(NULL); } @@ -161,21 +155,13 @@ xmlSecKeyUseWithDuplicate(xmlSecKeyUseWithPtr keyUseWith) { newKeyUseWith = xmlSecKeyUseWithCreate(NULL, NULL); if(newKeyUseWith == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyUseWithCreate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyUseWithCreate", NULL); return(NULL); } ret = xmlSecKeyUseWithCopy(newKeyUseWith, keyUseWith); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyUseWithCopy", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyUseWithCopy", NULL); xmlSecKeyUseWithDestroy(keyUseWith); return(NULL); } @@ -224,24 +210,14 @@ xmlSecKeyUseWithSet(xmlSecKeyUseWithPtr keyUseWith, const xmlChar* application, if(application != NULL) { keyUseWith->application = xmlStrdup(application); if(keyUseWith->application == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - NULL, - XMLSEC_ERRORS_R_MALLOC_FAILED, - "xmlStrlen(application)=%d", - xmlStrlen(application)); + xmlSecStrdupError(application, NULL); return(-1); } } if(identifier != NULL) { keyUseWith->identifier = xmlStrdup(identifier); if(keyUseWith->identifier == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - NULL, - XMLSEC_ERRORS_R_MALLOC_FAILED, - "xmlStrlen(identifier)=%d", - xmlStrlen(identifier)); + xmlSecStrdupError(identifier, NULL); return(-1); } } @@ -341,11 +317,7 @@ xmlSecKeyReqInitialize(xmlSecKeyReqPtr keyReq) { keyReq->keyUsage = xmlSecKeyUsageAny; /* by default you can do whatever you want with the key */ ret = xmlSecPtrListInitialize(&keyReq->keyUseWithList, xmlSecKeyUseWithPtrListId); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecPtrListInitialize", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecPtrListInitialize", NULL); return(-1); } @@ -408,11 +380,7 @@ xmlSecKeyReqCopy(xmlSecKeyReqPtr dst, xmlSecKeyReqPtr src) { ret = xmlSecPtrListCopy(&dst->keyUseWithList, &src->keyUseWithList); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecPtrListCopy", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecPtrListCopy", NULL); return(-1); } @@ -543,12 +511,7 @@ xmlSecKeyCreate(void) { /* Allocate a new xmlSecKey and fill the fields. */ key = (xmlSecKeyPtr)xmlMalloc(sizeof(xmlSecKey)); if(key == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - NULL, - XMLSEC_ERRORS_R_MALLOC_FAILED, - "sizeof(xmlSecKey)=%d", - (int)sizeof(xmlSecKey)); + xmlSecMallocError(sizeof(xmlSecKey), NULL); return(NULL); } memset(key, 0, sizeof(xmlSecKey)); @@ -614,11 +577,7 @@ xmlSecKeyCopy(xmlSecKeyPtr keyDst, xmlSecKeyPtr keySrc) { if(keySrc->name != NULL) { keyDst->name = xmlStrdup(keySrc->name); if(keyDst->name == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - NULL, - XMLSEC_ERRORS_R_STRDUP_FAILED, - "len=%d", xmlStrlen(keySrc->name)); + xmlSecStrdupError(keySrc->name, NULL); return(-1); } } @@ -626,11 +585,7 @@ xmlSecKeyCopy(xmlSecKeyPtr keyDst, xmlSecKeyPtr keySrc) { if(keySrc->value != NULL) { keyDst->value = xmlSecKeyDataDuplicate(keySrc->value); if(keyDst->value == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyDataDuplicate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyDataDuplicate", NULL); return(-1); } } @@ -638,11 +593,7 @@ xmlSecKeyCopy(xmlSecKeyPtr keyDst, xmlSecKeyPtr keySrc) { if(keySrc->dataList != NULL) { keyDst->dataList = xmlSecPtrListDuplicate(keySrc->dataList); if(keyDst->dataList == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecPtrListDuplicate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecPtrListDuplicate", NULL); return(-1); } } @@ -671,21 +622,13 @@ xmlSecKeyDuplicate(xmlSecKeyPtr key) { newKey = xmlSecKeyCreate(); if(newKey == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyCreate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyCreate", NULL); return(NULL); } ret = xmlSecKeyCopy(newKey, key); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyCopy", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyCopy", NULL); xmlSecKeyDestroy(newKey); return(NULL); } @@ -771,11 +714,7 @@ xmlSecKeySetName(xmlSecKeyPtr key, const xmlChar* name) { if(name != NULL) { key->name = xmlStrdup(name); if(key->name == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - NULL, - XMLSEC_ERRORS_R_STRDUP_FAILED, - "len=%d", xmlStrlen(name)); + xmlSecStrdupError(name, NULL); return(-1); } } @@ -878,23 +817,17 @@ xmlSecKeyEnsureData(xmlSecKeyPtr key, xmlSecKeyDataId dataId) { data = xmlSecKeyDataCreate(dataId); if(data == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyDataCreate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "dataId=%s", - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId))); + xmlSecInternalError2("xmlSecKeyDataCreate", NULL, + "dataId=%s", + xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId))); return(NULL); } ret = xmlSecKeyAdoptData(key, data); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyAdoptData", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "dataId=%s", - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId))); + xmlSecInternalError2("xmlSecKeyAdoptData", NULL, + "dataId=%s", + xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId))); xmlSecKeyDataDestroy(data); return(NULL); } @@ -932,11 +865,7 @@ xmlSecKeyAdoptData(xmlSecKeyPtr key, xmlSecKeyDataPtr data) { 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); + xmlSecInternalError("xmlSecPtrListCreate", NULL); return(-1); } } @@ -1067,43 +996,32 @@ xmlSecKeyGenerate(xmlSecKeyDataId dataId, xmlSecSize sizeBits, xmlSecKeyDataType data = xmlSecKeyDataCreate(dataId); if(data == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)), - "xmlSecKeyDataCreate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyDataCreate", + xmlSecKeyDataKlassGetName(dataId)); return(NULL); } ret = xmlSecKeyDataGenerate(data, sizeBits, type); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)), - "xmlSecKeyDataGenerate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "size=%d;type=%d", sizeBits, type); + xmlSecInternalError3("xmlSecKeyDataGenerate", + xmlSecKeyDataKlassGetName(dataId), + "size=%d;type=%d", sizeBits, type); xmlSecKeyDataDestroy(data); return(NULL); } key = xmlSecKeyCreate(); if(key == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)), - "xmlSecKeyCreate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyCreate", + xmlSecKeyDataKlassGetName(dataId)); xmlSecKeyDataDestroy(data); return(NULL); } ret = xmlSecKeySetValue(key, data); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)), - "xmlSecKeySetValue", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeySetValue", + xmlSecKeyDataKlassGetName(dataId)); xmlSecKeyDataDestroy(data); xmlSecKeyDestroy(key); return(NULL); @@ -1130,11 +1048,7 @@ xmlSecKeyGenerateByName(const xmlChar* name, xmlSecSize sizeBits, xmlSecKeyDataT dataId = xmlSecKeyDataIdListFindByName(xmlSecKeyDataIdsGet(), name, xmlSecKeyDataUsageAny); if(dataId == xmlSecKeyDataIdUnknown) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - xmlSecErrorsSafeString(name), - XMLSEC_ERRORS_R_KEY_DATA_NOT_FOUND, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecOtherError(XMLSEC_ERRORS_R_KEY_DATA_NOT_FOUND, name, NULL); return(NULL); } @@ -1162,21 +1076,15 @@ xmlSecKeyReadBuffer(xmlSecKeyDataId dataId, xmlSecBuffer* buffer) { /* create key data */ key = xmlSecKeyCreate(); if(key == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)), - "xmlSecKeyCreate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyCreate", + xmlSecKeyDataKlassGetName(dataId)); return(NULL); } ret = xmlSecKeyInfoCtxInitialize(&keyInfoCtx, NULL); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)), - "xmlSecKeyInfoCtxInitialize", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyInfoCtxInitialize", + xmlSecKeyDataKlassGetName(dataId)); xmlSecKeyDestroy(key); return(NULL); } @@ -1187,11 +1095,8 @@ xmlSecKeyReadBuffer(xmlSecKeyDataId dataId, xmlSecBuffer* buffer) { xmlSecBufferGetSize(buffer), &keyInfoCtx); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)), - "xmlSecKeyDataBinRead", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyDataBinRead", + xmlSecKeyDataKlassGetName(dataId)); xmlSecKeyInfoCtxFinalize(&keyInfoCtx); xmlSecKeyDestroy(key); return(NULL); @@ -1222,34 +1127,27 @@ xmlSecKeyReadBinaryFile(xmlSecKeyDataId dataId, const char* filename) { /* read file to buffer */ ret = xmlSecBufferInitialize(&buffer, 0); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)), - "xmlSecBufferInitialize", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecBufferInitialize", + xmlSecKeyDataKlassGetName(dataId)); return(NULL); } ret = xmlSecBufferReadFile(&buffer, filename); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)), - "xmlSecBufferReadFile", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "filename=%s", - xmlSecErrorsSafeString(filename)); + xmlSecInternalError2("xmlSecBufferReadFile", + xmlSecKeyDataKlassGetName(dataId), + "filename=%s", + xmlSecErrorsSafeString(filename)); xmlSecBufferFinalize(&buffer); return(NULL); } key = xmlSecKeyReadBuffer(dataId, &buffer); if(key == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)), - "xmlSecKeyReadBuffer", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "filename=%s", - xmlSecErrorsSafeString(filename)); + xmlSecInternalError2("xmlSecKeyReadBuffer", + xmlSecKeyDataKlassGetName(dataId), + "filename=%s", + xmlSecErrorsSafeString(filename)); xmlSecBufferFinalize(&buffer); return(NULL); } @@ -1281,31 +1179,22 @@ xmlSecKeyReadMemory(xmlSecKeyDataId dataId, const xmlSecByte* data, xmlSecSize d /* read file to buffer */ ret = xmlSecBufferInitialize(&buffer, 0); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)), - "xmlSecBufferInitialize", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecBufferInitialize", + xmlSecKeyDataKlassGetName(dataId)); return(NULL); } if (xmlSecBufferAppend(&buffer, data, dataSize) < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)), - "xmlSecBufferAppend", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecBufferAppend", + xmlSecKeyDataKlassGetName(dataId)); xmlSecBufferFinalize(&buffer); return(NULL); } key = xmlSecKeyReadBuffer(dataId, &buffer); if(key == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)), - "xmlSecKeyReadBuffer", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyReadBuffer", + xmlSecKeyDataKlassGetName(dataId)); xmlSecBufferFinalize(&buffer); return(NULL); } @@ -1335,23 +1224,17 @@ xmlSecKeysMngrGetKey(xmlNodePtr keyInfoNode, xmlSecKeyInfoCtxPtr keyInfoCtx) { /* first try to read data from <dsig:KeyInfo/> node */ key = xmlSecKeyCreate(); if(key == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyCreate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeyCreate", NULL); return(NULL); } if(keyInfoNode != NULL) { ret = xmlSecKeyInfoNodeRead(keyInfoNode, key, keyInfoCtx); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeyInfoNodeRead", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "node=%s", - xmlSecErrorsSafeString(xmlSecNodeGetName(keyInfoNode))); + xmlSecInternalError2("xmlSecKeyInfoNodeRead", + NULL, + "node=%s", + xmlSecErrorsSafeString(xmlSecNodeGetName(keyInfoNode))); xmlSecKeyDestroy(key); return(NULL); } @@ -1367,11 +1250,7 @@ xmlSecKeysMngrGetKey(xmlNodePtr keyInfoNode, xmlSecKeyInfoCtxPtr keyInfoCtx) { if(keyInfoCtx->keysMngr != NULL) { key = xmlSecKeysMngrFindKey(keyInfoCtx->keysMngr, NULL, keyInfoCtx); if(key == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecKeysMngrFindKey", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecKeysMngrFindKey", NULL); return(NULL); } if(xmlSecKeyGetValue(key) != NULL) { @@ -1380,11 +1259,7 @@ xmlSecKeysMngrGetKey(xmlNodePtr keyInfoNode, xmlSecKeyInfoCtxPtr keyInfoCtx) { xmlSecKeyDestroy(key); } - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - NULL, - XMLSEC_ERRORS_R_KEY_NOT_FOUND, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecOtherError(XMLSEC_ERRORS_R_KEY_NOT_FOUND, NULL, NULL); return(NULL); } |