diff --git a/.gitignore b/.gitignore index f611d08..d6c0bf4 100644 --- a/.gitignore +++ b/.gitignore @@ -73,5 +73,7 @@ build *.pc .DS_Store +# CLion +.idea diff --git a/include/stun_crypto.h b/include/stun_crypto.h new file mode 100644 index 0000000..2c7085f --- /dev/null +++ b/include/stun_crypto.h @@ -0,0 +1,22 @@ +/* + * See license file + */ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +unsigned char* stunlib_util_md5(const void* data, size_t len, unsigned char* md); + +void stunlib_util_sha1_hmac(const void* key, size_t keyLength, const void* data, size_t dataLength, void* macOut, unsigned int* macLength); + +void stunlib_util_random(void* buffer, size_t size); + +uint32_t stunlib_util_crc32(long crc, const uint8_t* buf, size_t len); + +#ifdef __cplusplus +} +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a6217f6..a5e8726 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,7 @@ set ( stunlib_srcs stunlib.c turnclient.c stuntrace.c + stun_crypto.c ) set (ADDITIONAL_LIBS "") @@ -24,10 +25,11 @@ install ( TARGETS stunlib set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake) -find_package( ZLIB REQUIRED ) +find_package( ZLIB ) if ( ZLIB_FOUND ) include_directories( ${ZLIB_INCLUDE_DIRS} ) list(APPEND ADDITIONAL_LIBS ${ZLIB_LIBRARIES}) + add_definitions(-DSTUNLIB_USE_ZLIB) endif( ZLIB_FOUND ) @@ -35,11 +37,13 @@ find_package( OpenSSL ) if( OPENSSL_FOUND ) include_directories( ${OPENSSL_INCLUDE_DIR} ) list(APPEND ADDITIONAL_LIBS ${OPENSSL_LIBRARIES}) + add_definitions(-DSTUNLIB_USE_OPENSSL) endif( OPENSSL_FOUND ) # Todo fix propper library discovery. if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") list(APPEND ADDITIONAL_LIBS "bsd") + add_definitions(-DSTUNLIB_USE_BSD) endif() target_link_libraries ( stunlib PRIVATE sockaddrutil diff --git a/src/stun_crypto.c b/src/stun_crypto.c new file mode 100644 index 0000000..8b01239 --- /dev/null +++ b/src/stun_crypto.c @@ -0,0 +1,60 @@ +/* + * See license file + */ + +#include "stun_crypto.h" + +#if defined(STUNLIB_USE_OPENSSL) +# include +# include +# include + +unsigned char* stunlib_util_md5(const void *data, size_t len, unsigned char *md) { + return MD5( (uint8_t*)data, len, md ); +} + +void stunlib_util_sha1_hmac(const void *key, size_t keyLength, const void *data, size_t dataLength, void *macOut, unsigned int* macLength) { + HMAC(EVP_sha1(), + key, + keyLength, + data, + dataLength, + macOut, macLength); +} + +#elif defined(__APPLE__) +# define COMMON_DIGEST_FOR_OPENSSL +# include +# include + +unsigned char* stunlib_util_md5(const void *data, size_t len, unsigned char *md) { + return CC_MD5((uint8_t*)data, (CC_LONG) len, md); +} + +void stunlib_util_sha1_hmac(const void *key, + size_t keyLength, + const void *data, + size_t dataLength, + void *macOut, + __attribute__((unused)) unsigned int* macLength) { + CCHmac(kCCHmacAlgSHA1, key, keyLength, data, dataLength, macOut); +} + +#endif // defined(__APPLE__) + +#if defined(STUNLIB_USE_BSD) +# include +#endif + +#if defined(STUNLIB_USE_BSD) || defined(__APPLE__) +void stunlib_util_random(void* buffer, size_t size) { + arc4random_buf(buffer, size); +} +#endif + +#if defined(STUNLIB_USE_ZLIB) +#include +uint32_t stunlib_util_crc32(long crc, const uint8_t* buf, size_t len) { + return crc32(crc, buf, len); +} +#endif diff --git a/src/stunlib.c b/src/stunlib.c index 39a01a6..b9684c8 100644 --- a/src/stunlib.c +++ b/src/stunlib.c @@ -2,21 +2,7 @@ * See license file */ #include "stunlib.h" -#if defined(__APPLE__) -# define COMMON_DIGEST_FOR_OPENSSL -# include -# include - -# define SHA1 CC_SHA1 -#else - -#include -#include -#include -#include -#endif - - +#include "stun_crypto.h" #include @@ -2578,22 +2564,11 @@ stunlib_checkIntegrity(const uint8_t* buf, write_16(&pCurrPtr, msgIntLength); pCurrPtr = (uint8_t*)bufCopy; - #if defined(__APPLE__) - CCHmac(kCCHmacAlgSHA1, - integrityKey, - integrityKeyLen, - pCurrPtr, - message->messageIntegrity.offset, - &hash[0]); - - #else - HMAC(EVP_sha1(), - integrityKey, - integrityKeyLen, - pCurrPtr, - message->messageIntegrity.offset, - &hash[0], &len); -#endif + stunlib_util_sha1_hmac(integrityKey, + (size_t) integrityKeyLen, + pCurrPtr, + message->messageIntegrity.offset, + &hash[0], &len); if (memcmp(&hash, message->messageIntegrity.hash,20) != 0) { /* @@ -3171,20 +3146,11 @@ stunlib_encodeMessage(StunMessage* message, (void)length; /*calculate and insert integrity hash*/ pCurrPtr = (uint8_t*)buf; -#if defined(__APPLE__) - CCHmac(kCCHmacAlgSHA1, - md5key, keyLen, - pCurrPtr, /*stunmsg string*/ - message->messageIntegrity.offset, - &message->messageIntegrity.hash[0]); -#else - length = 0; - HMAC(EVP_sha1(), - md5key, keyLen, - pCurrPtr, /*stunmsg string*/ - message->messageIntegrity.offset, - &message->messageIntegrity.hash[0], &length); -#endif + stunlib_util_sha1_hmac(md5key, keyLen, + pCurrPtr, + message->messageIntegrity.offset, + &message->messageIntegrity.hash[0], &length); + pCurrPtr = (uint8_t*)buf + message->messageIntegrity.offset; if ( !stunEncodeIntegrityAtr(&message->messageIntegrity, &pCurrPtr, &restlen, bufLen) ) @@ -3393,7 +3359,7 @@ stunlib_transIdIsEqual(const StunMsgId* a, void stunlib_createId(StunMsgId* pId) { - arc4random_buf(pId, STUN_MSG_ID_SIZE); + stunlib_util_random(pId, STUN_MSG_ID_SIZE); } @@ -3428,7 +3394,7 @@ uint32_t stunlib_calculateFingerprint(const uint8_t* buf, size_t len) { - return crc32(0L, buf, len) ^ 0x5354554e; + return stunlib_util_crc32(0L, buf, len) ^ 0x5354554e; } @@ -3473,9 +3439,5 @@ stunlib_createMD5Key(unsigned char* md5key, { abort(); } -#if defined(__APPLE__) - CC_MD5( (uint8_t*)keyStr, bytes_written, md5key ); -#else - MD5( (uint8_t*)keyStr, bytes_written, md5key ); -#endif + stunlib_util_md5((uint8_t*)keyStr, (size_t) bytes_written, md5key ); }