Commit d3b78b13 authored by jselbie's avatar jselbie

Merged changed from FeralInteractive and added support for CommonCrypto on Mac

parent 8aff602e
# BOOST_INCLUDE := -I/home/jselbie/boost_1_57_0 BOOST_INCLUDE := -I/Users/jselbie/boost_1_52_0
# OPENSSL_INCLUDE := -I/home/jselbie/lib/openssl #OPENSSL_INCLUDE := -I/Users/jselbie/openssl/include
DEFINES := -DNDEBUG DEFINES := -DNDEBUG
......
...@@ -18,13 +18,6 @@ ...@@ -18,13 +18,6 @@
#include "polling.h" #include "polling.h"
#include "fasthash.h" #include "fasthash.h"
#ifdef IS_LINUX
#ifndef HAS_EPOLL
#pragma message "polling.cpp: WARNING - EPOLL IS NOT AVAILABLE"
#endif
#endif
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#include "commonincludes.hpp" #include "commonincludes.hpp"
#include <openssl/hmac.h> //#include <openssl/hmac.h>
#include "stuncore.h" #include "stuncore.h"
#include "stunsocket.h" #include "stunsocket.h"
#include "stunsocketthread.h" #include "stunsocketthread.h"
......
...@@ -24,8 +24,15 @@ ...@@ -24,8 +24,15 @@
#include "stunbuilder.h" #include "stunbuilder.h"
#include <boost/crc.hpp> #include <boost/crc.hpp>
#ifndef __APPLE__
#include <openssl/md5.h> #include <openssl/md5.h>
#include <openssl/hmac.h> #include <openssl/hmac.h>
#else
#define COMMON_DIGEST_FOR_OPENSSL
#include <CommonCrypto/CommonCrypto.h>
#endif
#include "stunauth.h" #include "stunauth.h"
...@@ -498,11 +505,17 @@ HRESULT CStunMessageBuilder::AddMessageIntegrityImpl(uint8_t* key, size_t keysiz ...@@ -498,11 +505,17 @@ HRESULT CStunMessageBuilder::AddMessageIntegrityImpl(uint8_t* key, size_t keysiz
// now do a little pointer math so that HMAC can write exactly to where the hash bytes will appear // now do a little pointer math so that HMAC can write exactly to where the hash bytes will appear
pDstBuf = ((uint8_t*)pData) + length + 4; pDstBuf = ((uint8_t*)pData) + length + 4;
pHashResult = HMAC(EVP_sha1(), key, keysize, (uint8_t*)pData, length, pDstBuf, &resultlength);
#ifndef __APPLE__
pHashResult = HMAC(EVP_sha1(), key, keysize, (uint8_t*)pData, length, pDstBuf, &resultlength);
ASSERT(resultlength == 20); ASSERT(resultlength == 20);
ASSERT(pHashResult != NULL); ASSERT(pHashResult != NULL);
Cleanup: #else
CCHmac(kCCHmacAlgSHA1, key, keysize,(uint8_t*)pData, length, pDstBuf);
UNREFERENCED_VARIABLE(resultlength);
#endif
Cleanup:
return hr; return hr;
} }
...@@ -557,7 +570,12 @@ HRESULT CStunMessageBuilder::AddMessageIntegrityLongTerm(const char* pszUserName ...@@ -557,7 +570,12 @@ HRESULT CStunMessageBuilder::AddMessageIntegrityLongTerm(const char* pszUserName
ASSERT((pDst-key) == lenTotal); ASSERT((pDst-key) == lenTotal);
#ifndef __APPLE__
pResult = MD5(key, lenTotal, hash); pResult = MD5(key, lenTotal, hash);
#else
pResult = CC_MD5(key, lenTotal, hash);
#endif
ASSERT(pResult != NULL); ASSERT(pResult != NULL);
hr= AddMessageIntegrityImpl(hash, MD5_DIGEST_LENGTH); hr= AddMessageIntegrityImpl(hash, MD5_DIGEST_LENGTH);
......
...@@ -22,9 +22,16 @@ ...@@ -22,9 +22,16 @@
#include "stunutils.h" #include "stunutils.h"
#include "socketaddress.h" #include "socketaddress.h"
#include <boost/crc.hpp> #include <boost/crc.hpp>
#ifndef __APPLE__
#include <openssl/evp.h> #include <openssl/evp.h>
#include <openssl/hmac.h> #include <openssl/hmac.h>
#include <openssl/md5.h> #include <openssl/md5.h>
#else
#define COMMON_DIGEST_FOR_OPENSSL
#include <CommonCrypto/CommonCrypto.h>
#endif
#include "stunauth.h" #include "stunauth.h"
#include "fasthash.h" #include "fasthash.h"
...@@ -145,7 +152,11 @@ HRESULT CStunMessageReader::ValidateMessageIntegrity(uint8_t* key, size_t keylen ...@@ -145,7 +152,11 @@ HRESULT CStunMessageReader::ValidateMessageIntegrity(uint8_t* key, size_t keylen
const size_t c_hmacsize = 20; const size_t c_hmacsize = 20;
uint8_t hmaccomputed[c_hmacsize] = {}; // zero-init uint8_t hmaccomputed[c_hmacsize] = {}; // zero-init
unsigned int hmaclength = c_hmacsize; unsigned int hmaclength = c_hmacsize;
#ifndef __APPLE__
HMAC_CTX ctx = {}; HMAC_CTX ctx = {};
#else
CCHmacContext ctx = {};
#endif
uint32_t chunk32; uint32_t chunk32;
uint16_t chunk16; uint16_t chunk16;
size_t len, nChunks; size_t len, nChunks;
...@@ -182,13 +193,21 @@ HRESULT CStunMessageReader::ValidateMessageIntegrity(uint8_t* key, size_t keylen ...@@ -182,13 +193,21 @@ HRESULT CStunMessageReader::ValidateMessageIntegrity(uint8_t* key, size_t keylen
stream.Attach(spBuffer, false); stream.Attach(spBuffer, false);
// Here comes the fun part. If there is a fingerprint attribute, we have to adjust the length header in computing the hash // Here comes the fun part. If there is a fingerprint attribute, we have to adjust the length header in computing the hash
#ifndef __APPLE__
HMAC_CTX_init(&ctx); HMAC_CTX_init(&ctx);
fContextInit = true;
HMAC_Init(&ctx, key, keylength, EVP_sha1()); HMAC_Init(&ctx, key, keylength, EVP_sha1());
#else
CCHmacInit(&ctx, kCCHmacAlgSHA1, key, keylength);
#endif
fContextInit = true;
// message type // message type
Chk(stream.ReadUint16(&chunk16)); Chk(stream.ReadUint16(&chunk16));
#ifndef __APPLE__
HMAC_Update(&ctx, (unsigned char*)&chunk16, sizeof(chunk16)); HMAC_Update(&ctx, (unsigned char*)&chunk16, sizeof(chunk16));
#else
CCHmacUpdate(&ctx, &chunk16, sizeof(chunk16));
#endif
// message length // message length
Chk(stream.ReadUint16(&chunk16)); Chk(stream.ReadUint16(&chunk16));
...@@ -203,7 +222,12 @@ HRESULT CStunMessageReader::ValidateMessageIntegrity(uint8_t* key, size_t keylen ...@@ -203,7 +222,12 @@ HRESULT CStunMessageReader::ValidateMessageIntegrity(uint8_t* key, size_t keylen
chunk16 = htons(adjustedlengthHeader); chunk16 = htons(adjustedlengthHeader);
} }
#ifndef __APPLE__
HMAC_Update(&ctx, (unsigned char*)&chunk16, sizeof(chunk16)); HMAC_Update(&ctx, (unsigned char*)&chunk16, sizeof(chunk16));
#else
CCHmacUpdate(&ctx, &chunk16, sizeof(chunk16));
#endif
// now include everything up to the hash attribute itself. // now include everything up to the hash attribute itself.
len = pAttribIntegrity->offset; len = pAttribIntegrity->offset;
...@@ -217,10 +241,19 @@ HRESULT CStunMessageReader::ValidateMessageIntegrity(uint8_t* key, size_t keylen ...@@ -217,10 +241,19 @@ HRESULT CStunMessageReader::ValidateMessageIntegrity(uint8_t* key, size_t keylen
for (size_t count = 0; count < nChunks; count++) for (size_t count = 0; count < nChunks; count++)
{ {
Chk(stream.ReadUint32(&chunk32)); Chk(stream.ReadUint32(&chunk32));
#ifndef __APPLE__
HMAC_Update(&ctx, (unsigned char*)&chunk32, sizeof(chunk32)); HMAC_Update(&ctx, (unsigned char*)&chunk32, sizeof(chunk32));
#else
CCHmacUpdate(&ctx, &chunk32, sizeof(chunk32));
#endif
} }
#ifndef __APPLE__
HMAC_Final(&ctx, hmaccomputed, &hmaclength); HMAC_Final(&ctx, hmaccomputed, &hmaclength);
#else
CCHmacFinal(&ctx, hmaccomputed);
#endif
// now compare the bytes // now compare the bytes
cmp = memcmp(hmaccomputed, spBuffer->GetData() + pAttribIntegrity->offset, c_hmacsize); cmp = memcmp(hmaccomputed, spBuffer->GetData() + pAttribIntegrity->offset, c_hmacsize);
...@@ -230,7 +263,11 @@ HRESULT CStunMessageReader::ValidateMessageIntegrity(uint8_t* key, size_t keylen ...@@ -230,7 +263,11 @@ HRESULT CStunMessageReader::ValidateMessageIntegrity(uint8_t* key, size_t keylen
Cleanup: Cleanup:
if (fContextInit) if (fContextInit)
{ {
#ifndef __APPLE__
HMAC_CTX_cleanup(&ctx); HMAC_CTX_cleanup(&ctx);
#else
UNREFERENCED_VARIABLE(fContextInit);
#endif
} }
return hr; return hr;
...@@ -289,7 +326,19 @@ HRESULT CStunMessageReader::ValidateMessageIntegrityLong(const char* pszUser, co ...@@ -289,7 +326,19 @@ HRESULT CStunMessageReader::ValidateMessageIntegrityLong(const char* pszUser, co
ASSERT((pDst-key) == totallength); ASSERT((pDst-key) == totallength);
#ifndef __APPLE__
ChkIfA(NULL == MD5(key, totallength, hash), E_FAIL); ChkIfA(NULL == MD5(key, totallength, hash), E_FAIL);
#else
{
CC_MD5_CTX context = {};
CC_MD5_Init(&context);
CC_MD5_Update(&context, key, totallength);
CC_MD5_Final(hash, &context);
}
#endif
Chk(ValidateMessageIntegrity(hash, ARRAYSIZE(hash))); Chk(ValidateMessageIntegrity(hash, ARRAYSIZE(hash)));
Cleanup: Cleanup:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment