261 lines
8.8 KiB
C++

//-------------------------------------------------------------------
// Copyright (C) The Internet Society (1999). All Rights Reserved.
//
// This document and translations of it may be copied and furnished to
// others, and derivative works that comment on or otherwise explain it
// or assist in its implementation may be prepared, copied, published
// and distributed, in whole or in part, without restriction of any
// kind, provided that the above copyright notice and this paragraph are
// included on all such copies and derivative works. However, this
// document itself may not be modified in any way, such as by removing
// the copyright notice or references to the Internet Society or other
// Internet organizations, except as needed for the purpose of
// developing Internet standards in which case the procedures for
// copyrights defined in the Internet Standards process must be
// followed, or as required to translate it into languages other than
// English.
//
// The limited permissions granted above are perpetual and will not be
// revoked by the Internet Society or its successors or assigns.
//
// This document and the information contained herein is provided on an
// "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
// TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
// HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//
//
// Modifiyed by Intel Corporation, 2005
//-------------------------------------------------------------------------
#include <sys/types.h>
#ifndef _WIN32
#include <openssl/md5.h>
#else
#include <windows.h>
#include <wincrypt.h>
#endif
#include <string.h>
#include <ctype.h>
#include "digcalc.h"
int stricmp(const char *b, const char *a)
{
while (*a != '\0' && *b != '\0' && tolower(*a) == tolower(*b))
{ a++;
b++;
}
return *a == *b ? 0 :
tolower(*a) < tolower(*b) ? -1 : 1;
}
void CvtHex(
HASH Bin,
HASHHEX Hex
)
{
unsigned short i;
unsigned char j;
for (i = 0; i < HASHLEN; i++) {
j = (Bin[i] >> 4) & 0xf;
if (j <= 9)
Hex[i*2] = (j + '0');
else
Hex[i*2] = (j + 'a' - 10);
j = Bin[i] & 0xf;
if (j <= 9)
Hex[i*2+1] = (j + '0');
else
Hex[i*2+1] = (j + 'a' - 10);
};
Hex[HASHHEXLEN] = '\0';
};
/* calculate H(A1) as per spec */
void DigestCalcHA1(
char * pszAlg,
char * pszUserName,
char * pszRealm,
char * pszPassword,
char * pszNonce,
char * pszCNonce,
HASHHEX SessionKey
)
{
HASH HA1;
HASHHEX HA1HEX;
#ifdef _WIN32
HCRYPTPROV hCryptProv = NULL; //Cryptography provider context(CSP) handler.
HCRYPTHASH hMD5Hash = NULL; //MD5 hash table handler
HCRYPTHASH hHexHash = NULL; //MD5 hash table handler
DWORD data = HASHLEN; //size of hash buffer
// Get a handle to a cryptography provider context.
if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
goto close;
// Acquire a hash object handle.
if (!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hMD5Hash))
goto close;
CryptHashData(hMD5Hash, (const BYTE *)pszUserName, strlen(pszUserName), 0);
CryptHashData(hMD5Hash, (const BYTE *)":", 1, 0);
CryptHashData(hMD5Hash, (const BYTE *)pszRealm, strlen(pszRealm), 0);
CryptHashData(hMD5Hash, (const BYTE *)":", 1, 0);
CryptHashData(hMD5Hash, (const BYTE *)pszPassword, strlen(pszPassword), 0);
CryptGetHashParam(hMD5Hash, HP_HASHVAL, HA1, &data, 0);
if (stricmp(pszAlg, "md5-sess") == 0) {
CvtHex(HA1, HA1HEX);
if (!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHexHash))
goto close;
CryptHashData(hHexHash, (const BYTE *)HA1HEX, HASHHEXLEN, 0);
CryptHashData(hHexHash, (const BYTE *)":", 1, 0);
CryptHashData(hHexHash, (const BYTE *)pszNonce, strlen(pszNonce), 0);
CryptHashData(hHexHash, (const BYTE *)":", 1, 0);
CryptHashData(hHexHash, (const BYTE *)pszCNonce, strlen(pszCNonce), 0);
CryptGetHashParam(hHexHash, HP_HASHVAL, HA1, &data, 0);
}
CvtHex(HA1, SessionKey);
close:
if (hCryptProv != NULL)
CryptReleaseContext(hCryptProv,0);
if (hMD5Hash != NULL)
CryptDestroyHash(hMD5Hash);
if (hHexHash != NULL)
CryptDestroyHash(hHexHash);
#else
MD5_CTX Md5Ctx;
MD5_Init(&Md5Ctx);
MD5_Update(&Md5Ctx, pszUserName, strlen(pszUserName));
MD5_Update(&Md5Ctx, ":", 1);
MD5_Update(&Md5Ctx, pszRealm, strlen(pszRealm));
MD5_Update(&Md5Ctx, ":", 1);
MD5_Update(&Md5Ctx, pszPassword, strlen(pszPassword));
MD5_Final(HA1, &Md5Ctx);
if (stricmp(pszAlg, "md5-sess") == 0) {
CvtHex(HA1, HA1HEX);
MD5_Init(&Md5Ctx);
MD5_Update(&Md5Ctx, HA1HEX, HASHHEXLEN);
MD5_Update(&Md5Ctx, ":", 1);
MD5_Update(&Md5Ctx, pszNonce, strlen(pszNonce));
MD5_Update(&Md5Ctx, ":", 1);
MD5_Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
MD5_Final(HA1, &Md5Ctx);
};
CvtHex(HA1, SessionKey);
#endif //_WIN32
};
/* calculate request-digest/response-digest as per HTTP Digest spec */
void DigestCalcResponse(
HASHHEX HA1, /* H(A1) */
char * pszNonce, /* nonce from server */
char * pszNonceCount, /* 8 hex digits */
char * pszCNonce, /* client nonce */
char * pszQop, /* qop-value: "", "auth", "auth-int" */
char * pszMethod, /* method from the request */
char * pszDigestUri, /* requested URL */
HASHHEX HEntity, /* H(entity body) if qop="auth-int" */
HASHHEX Response /* request-digest or response-digest */
)
{
HASH HA2;
HASHHEX HA2Hex;
HASH RespHash;
#ifdef _WIN32
HCRYPTPROV hCryptProv = NULL; //Cryptography provider context(CSP) handler.
HCRYPTHASH hMD5Hash = NULL; //MD5 hash table handler
HCRYPTHASH hHexHash = NULL; //MD5 hash table handler
DWORD data = HASHLEN; //size of hash buffer
// Get a handle to a cryptography provider context.
if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
goto close;
// Acquire a hash object handle.
if (!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hMD5Hash))
goto close;
CryptHashData(hMD5Hash, (const BYTE *)pszMethod, strlen(pszMethod), 0);
CryptHashData(hMD5Hash, (const BYTE *)":", 1, 0);
CryptHashData(hMD5Hash, (const BYTE *)pszDigestUri, strlen(pszDigestUri), 0);
if (stricmp(pszQop, "auth-int") == 0) {
CryptHashData(hMD5Hash, (const BYTE *)":", 1, 0);
CryptHashData(hMD5Hash, (const BYTE *)HEntity, HASHHEXLEN, 0);
};
CryptGetHashParam(hMD5Hash, HP_HASHVAL, HA2, &data, 0);
CvtHex(HA2, HA2Hex);
if (!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHexHash))
goto close;
CryptHashData(hHexHash, (const BYTE *)HA1, HASHHEXLEN, 0);
CryptHashData(hHexHash, (const BYTE *)":", 1, 0);
CryptHashData(hHexHash, (const BYTE *)pszNonce, strlen(pszNonce), 0);
CryptHashData(hHexHash, (const BYTE *)":", 1, 0);
if (*pszQop) {
CryptHashData(hHexHash, (const BYTE *)pszNonceCount, strlen(pszNonceCount), 0);
CryptHashData(hHexHash, (const BYTE *)":", 1, 0);
CryptHashData(hHexHash, (const BYTE *)pszCNonce, strlen(pszCNonce), 0);
CryptHashData(hHexHash, (const BYTE *)":", 1, 0);
CryptHashData(hHexHash, (const BYTE *)pszQop, strlen(pszQop), 0);
CryptHashData(hHexHash, (const BYTE *)":", 1, 0);
}
CryptHashData(hHexHash, (const BYTE *)HA2Hex, HASHHEXLEN, 0);
CryptGetHashParam(hHexHash, HP_HASHVAL, RespHash, &data, 0);
CvtHex(RespHash, Response);
close:
if (hCryptProv != NULL)
CryptReleaseContext(hCryptProv,0);
if (hMD5Hash != NULL)
CryptDestroyHash(hMD5Hash);
if (hHexHash != NULL)
CryptDestroyHash(hHexHash);
#else
MD5_CTX Md5Ctx;
// calculate H(A2)
MD5_Init(&Md5Ctx);
MD5_Update(&Md5Ctx, pszMethod, strlen(pszMethod));
MD5_Update(&Md5Ctx, ":", 1);
MD5_Update(&Md5Ctx, pszDigestUri, strlen(pszDigestUri));
if (stricmp(pszQop, "auth-int") == 0) {
MD5_Update(&Md5Ctx, ":", 1);
MD5_Update(&Md5Ctx, HEntity, HASHHEXLEN);
};
MD5_Final(HA2, &Md5Ctx);
CvtHex(HA2, HA2Hex);
// calculate response
MD5_Init(&Md5Ctx);
MD5_Update(&Md5Ctx, HA1, HASHHEXLEN);
MD5_Update(&Md5Ctx, ":", 1);
MD5_Update(&Md5Ctx, pszNonce, strlen(pszNonce));
MD5_Update(&Md5Ctx, ":", 1);
if (*pszQop) {
MD5_Update(&Md5Ctx, pszNonceCount, strlen(pszNonceCount));
MD5_Update(&Md5Ctx, ":", 1);
MD5_Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
MD5_Update(&Md5Ctx, ":", 1);
MD5_Update(&Md5Ctx, pszQop, strlen(pszQop));
MD5_Update(&Md5Ctx, ":", 1);
};
MD5_Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
MD5_Final(RespHash, &Md5Ctx);
CvtHex(RespHash, Response);
#endif //_WIN32
};