diff options
Diffstat (limited to 'src/mscrypto/kw_des.c')
-rw-r--r-- | src/mscrypto/kw_des.c | 218 |
1 files changed, 70 insertions, 148 deletions
diff --git a/src/mscrypto/kw_des.c b/src/mscrypto/kw_des.c index 227e76d5..285cfecd 100644 --- a/src/mscrypto/kw_des.c +++ b/src/mscrypto/kw_des.c @@ -1,14 +1,19 @@ -/** - * - * XMLSec library +/* + * XML Security Library (http://www.aleksey.com/xmlsec). * - * DES Algorithm support * * 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:kw_des + * @Short_description: DES Key Transport transforms implementation for Microsoft Crypto API. + * @Stability: Private + * + */ + #ifndef XMLSEC_NO_DES #include "globals.h" @@ -187,55 +192,36 @@ xmlSecMSCryptoKWDes3Initialize(xmlSecTransformPtr transform) { ctx->keyId = xmlSecMSCryptoKeyDataDesId; ctx->keySize = XMLSEC_KW_DES3_KEY_LENGTH; } else { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - NULL, - XMLSEC_ERRORS_R_INVALID_TRANSFORM, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInvalidTransfromError(transform) return(-1); } ret = xmlSecBufferInitialize(&(ctx->keyBuffer), 0); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecBufferInitialize", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecBufferInitialize", + xmlSecTransformGetName(transform)); return(-1); } /* find providers */ ctx->desCryptProvider = xmlSecMSCryptoFindProvider(ctx->desProviders, NULL, CRYPT_VERIFYCONTEXT, TRUE); if(ctx->desCryptProvider == 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecMSCryptoFindProvider(des)", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); - + xmlSecInternalError("xmlSecMSCryptoFindProvider(des)", + xmlSecTransformGetName(transform)); return(-1); } ctx->sha1CryptProvider = xmlSecMSCryptoFindProvider(ctx->sha1Providers, NULL, CRYPT_VERIFYCONTEXT, TRUE); if(ctx->sha1CryptProvider == 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecMSCryptoFindProvider(sha1)", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); - + xmlSecInternalError("xmlSecMSCryptoFindProvider(sha1)", + xmlSecTransformGetName(transform)); return(-1); } /* Create dummy key to be able to import plain session keys */ if (!xmlSecMSCryptoCreatePrivateExponentOneKey(ctx->desCryptProvider, &(ctx->pubPrivKey))) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecMSCryptoCreatePrivateExponentOneKey", - XMLSEC_ERRORS_R_CRYPTO_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); - + xmlSecMSCryptoError("xmlSecMSCryptoCreatePrivateExponentOneKey", + xmlSecTransformGetName(transform)); return(-1); } @@ -311,22 +297,16 @@ xmlSecMSCryptoKWDes3SetKey(xmlSecTransformPtr transform, xmlSecKeyPtr key) { keySize = xmlSecBufferGetSize(buffer); if(keySize < XMLSEC_KW_DES3_KEY_LENGTH) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - NULL, - XMLSEC_ERRORS_R_INVALID_KEY_DATA_SIZE, - "key length %d is not enough (%d expected)", - keySize, XMLSEC_KW_DES3_KEY_LENGTH); + xmlSecInvalidKeyDataSizeError(keySize, XMLSEC_KW_DES3_KEY_LENGTH, + xmlSecTransformGetName(transform)); return(-1); } ret = xmlSecBufferSetData(&(ctx->keyBuffer), xmlSecBufferGetData(buffer), XMLSEC_KW_DES3_KEY_LENGTH); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecBufferSetData", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "size=%d", XMLSEC_KW_DES3_KEY_LENGTH); + xmlSecInternalError2("xmlSecBufferSetData", + xmlSecTransformGetName(transform), + "size=%d", XMLSEC_KW_DES3_KEY_LENGTH); return(-1); } @@ -365,12 +345,9 @@ xmlSecMSCryptoKWDes3Execute(xmlSecTransformPtr transform, int last, xmlSecTransf /* just do nothing */ } else if((transform->status == xmlSecTransformStatusWorking) && (last != 0)) { if((inSize % XMLSEC_KW_DES3_BLOCK_LENGTH) != 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - NULL, - XMLSEC_ERRORS_R_INVALID_SIZE, - "%d bytes - not %d bytes aligned", - inSize, XMLSEC_KW_DES3_BLOCK_LENGTH); + xmlSecInvalidSizeNotMultipleOfError("Input data", + inSize, XMLSEC_KW_DES3_BLOCK_LENGTH, + xmlSecTransformGetName(transform)); return(-1); } @@ -386,11 +363,9 @@ xmlSecMSCryptoKWDes3Execute(xmlSecTransformPtr transform, int last, xmlSecTransf ret = xmlSecBufferSetMaxSize(out, outSize); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecBufferSetMaxSize", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "size=%d", outSize); + xmlSecInternalError2("xmlSecBufferSetMaxSize", + xmlSecTransformGetName(transform), + "size=%d", outSize); return(-1); } @@ -399,12 +374,9 @@ xmlSecMSCryptoKWDes3Execute(xmlSecTransformPtr transform, int last, xmlSecTransf xmlSecBufferGetData(in), inSize, xmlSecBufferGetData(out), outSize); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecKWDes3Encode", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "key=%d,in=%d,out=%d", - keySize, inSize, outSize); + xmlSecInternalError4("xmlSecKWDes3Encode", xmlSecTransformGetName(transform), + "key=%d,in=%d,out=%d", + keySize, inSize, outSize); return(-1); } outSize = ret; @@ -413,12 +385,9 @@ xmlSecMSCryptoKWDes3Execute(xmlSecTransformPtr transform, int last, xmlSecTransf xmlSecBufferGetData(in), inSize, xmlSecBufferGetData(out), outSize); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecKWDes3Decode", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "key=%d,in=%d,out=%d", - keySize, inSize, outSize); + xmlSecInternalError4("xmlSecKWDes3Decode", xmlSecTransformGetName(transform), + "key=%d,in=%d,out=%d", + keySize, inSize, outSize); return(-1); } outSize = ret; @@ -426,21 +395,17 @@ xmlSecMSCryptoKWDes3Execute(xmlSecTransformPtr transform, int last, xmlSecTransf ret = xmlSecBufferSetSize(out, outSize); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecBufferSetSize", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "size=%d", outSize); + xmlSecInternalError2("xmlSecBufferSetSize", + xmlSecTransformGetName(transform), + "size=%d", outSize); return(-1); } ret = xmlSecBufferRemoveHead(in, inSize); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecBufferRemoveHead", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "size=%d", inSize); + xmlSecInternalError2("xmlSecBufferRemoveHead", + xmlSecTransformGetName(transform), + "size=%d", inSize); return(-1); } @@ -449,11 +414,7 @@ xmlSecMSCryptoKWDes3Execute(xmlSecTransformPtr transform, int last, xmlSecTransf /* the only way we can get here is if there is no input */ xmlSecAssert2(xmlSecBufferGetSize(&(transform->inBuf)) == 0, -1); } else { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - NULL, - XMLSEC_ERRORS_R_INVALID_STATUS, - "status=%d", transform->status); + xmlSecInvalidTransfromStatusError(transform); return(-1); } return(0); @@ -488,11 +449,7 @@ xmlSecMSCryptoKWDes3Sha1(void * context, 0, &mscHash); if((ret == 0) || (mscHash == 0)) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "CryptCreateHash", - XMLSEC_ERRORS_R_CRYPTO_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecMSCryptoError("CryptCreateHash", NULL); return(-1); } @@ -502,11 +459,8 @@ xmlSecMSCryptoKWDes3Sha1(void * context, inSize, 0); if(ret == 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "CryptHashData", - XMLSEC_ERRORS_R_CRYPTO_FAILED, - "size=%d", inSize); + xmlSecMSCryptoError2("CryptHashData", NULL, + "size=%d", inSize); CryptDestroyHash(mscHash); return(-1); } @@ -519,11 +473,8 @@ xmlSecMSCryptoKWDes3Sha1(void * context, &retLen, 0); if (ret == 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "CryptGetHashParam(HP_HASHVAL)", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "size=%d", outSize); + xmlSecMSCryptoError2("CryptGetHashParam(HP_HASHVAL)", NULL, + "size=%d", outSize); CryptDestroyHash(mscHash); return(-1); } @@ -538,7 +489,6 @@ xmlSecMSCryptoKWDes3GenerateRandom(void * context, xmlSecByte * out, xmlSecSize outSize) { xmlSecMSCryptoKWDes3CtxPtr ctx = (xmlSecMSCryptoKWDes3CtxPtr)context; - int ret; xmlSecAssert2(ctx != NULL, -1); xmlSecAssert2(ctx->desCryptProvider != 0, -1); @@ -546,11 +496,8 @@ xmlSecMSCryptoKWDes3GenerateRandom(void * context, xmlSecAssert2(outSize > 0, -1); if(!CryptGenRandom(ctx->desCryptProvider, outSize, out)) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "CryptGenRandom", - XMLSEC_ERRORS_R_CRYPTO_FAILED, - "len=%d", outSize); + xmlSecMSCryptoError2("CryptGenRandom", NULL, + "len=%d", outSize); return(-1); } @@ -565,7 +512,6 @@ xmlSecMSCryptoKWDes3BlockEncrypt(void * context, xmlSecMSCryptoKWDes3CtxPtr ctx = (xmlSecMSCryptoKWDes3CtxPtr)context; DWORD dwBlockLen, dwBlockLenLen, dwCLen; HCRYPTKEY cryptKey = 0; - int ret; xmlSecAssert2(ctx != NULL, -1); xmlSecAssert2(xmlSecBufferGetData(&(ctx->keyBuffer)) != NULL, -1); @@ -587,11 +533,7 @@ xmlSecMSCryptoKWDes3BlockEncrypt(void * context, TRUE, &cryptKey)) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecMSCryptoImportPlainSessionBlob", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecMSCryptoImportPlainSessionBlob", NULL); return(-1); } xmlSecAssert2(cryptKey != 0, -1); @@ -599,23 +541,20 @@ xmlSecMSCryptoKWDes3BlockEncrypt(void * context, /* iv len == block len */ dwBlockLenLen = sizeof(DWORD); if (!CryptGetKeyParam(cryptKey, KP_BLOCKLEN, (BYTE *)&dwBlockLen, &dwBlockLenLen, 0)) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "CryptGetKeyParam", - XMLSEC_ERRORS_R_CRYPTO_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecMSCryptoError("CryptGetKeyParam", NULL); CryptDestroyKey(cryptKey); return(-1); } /* set IV */ - if((ivSize < dwBlockLen / 8) || (!CryptSetKeyParam(cryptKey, KP_IV, iv, 0))) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "CryptSetKeyParam", - XMLSEC_ERRORS_R_CRYPTO_FAILED, - "ivSize=%d, dwBlockLen=%d", - ivSize, dwBlockLen / 8); + if(ivSize < dwBlockLen / 8) { + xmlSecInvalidSizeLessThanError("ivSize", ivSize, dwBlockLen / 8, NULL); + CryptDestroyKey(cryptKey); + return(-1); + } + + if(!CryptSetKeyParam(cryptKey, KP_IV, iv, 0)) { + xmlSecMSCryptoError("CryptSetKeyParam", NULL); CryptDestroyKey(cryptKey); return(-1); } @@ -627,11 +566,7 @@ xmlSecMSCryptoKWDes3BlockEncrypt(void * context, } dwCLen = inSize; if(!CryptEncrypt(cryptKey, 0, FALSE, 0, out, &dwCLen, outSize)) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "CryptEncrypt", - XMLSEC_ERRORS_R_CRYPTO_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecMSCryptoError("CryptEncrypt", NULL); CryptDestroyKey(cryptKey); return(-1); } @@ -649,7 +584,6 @@ xmlSecMSCryptoKWDes3BlockDecrypt(void * context, xmlSecMSCryptoKWDes3CtxPtr ctx = (xmlSecMSCryptoKWDes3CtxPtr)context; DWORD dwBlockLen, dwBlockLenLen, dwCLen; HCRYPTKEY cryptKey = 0; - int ret; xmlSecAssert2(ctx != NULL, -1); xmlSecAssert2(xmlSecBufferGetData(&(ctx->keyBuffer)) != NULL, -1); @@ -671,11 +605,7 @@ xmlSecMSCryptoKWDes3BlockDecrypt(void * context, TRUE, &cryptKey)) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecMSCryptoImportPlainSessionBlob", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecMSCryptoImportPlainSessionBlob", NULL); return(-1); } xmlSecAssert2(cryptKey != 0, -1); @@ -683,23 +613,19 @@ xmlSecMSCryptoKWDes3BlockDecrypt(void * context, /* iv len == block len */ dwBlockLenLen = sizeof(DWORD); if (!CryptGetKeyParam(cryptKey, KP_BLOCKLEN, (BYTE *)&dwBlockLen, &dwBlockLenLen, 0)) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "CryptGetKeyParam", - XMLSEC_ERRORS_R_CRYPTO_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecMSCryptoError("CryptGetKeyParam", NULL); CryptDestroyKey(cryptKey); return(-1); } /* set IV */ - if((ivSize < dwBlockLen / 8) || (!CryptSetKeyParam(cryptKey, KP_IV, iv, 0))) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "CryptSetKeyParam", - XMLSEC_ERRORS_R_CRYPTO_FAILED, - "ivSize=%d, dwBlockLen=%d", - ivSize, dwBlockLen / 8); + if(ivSize < dwBlockLen / 8) { + xmlSecInvalidSizeLessThanError("ivSize", ivSize, dwBlockLen / 8, NULL); + CryptDestroyKey(cryptKey); + return(-1); + } + if(!CryptSetKeyParam(cryptKey, KP_IV, iv, 0)) { + xmlSecMSCryptoError("CryptSetKeyParam", NULL); CryptDestroyKey(cryptKey); return(-1); } @@ -711,11 +637,7 @@ xmlSecMSCryptoKWDes3BlockDecrypt(void * context, } dwCLen = inSize; if(!CryptDecrypt(cryptKey, 0, FALSE, 0, out, &dwCLen)) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "CryptEncrypt", - XMLSEC_ERRORS_R_CRYPTO_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecMSCryptoError("CryptEncrypt", NULL); CryptDestroyKey(cryptKey); return(-1); } |