Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,7 @@ build
*.pc
.DS_Store

# CLion
.idea


22 changes: 22 additions & 0 deletions include/stun_crypto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* See license file
*/

#include <stdlib.h>
#include <inttypes.h>

#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
6 changes: 5 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set ( stunlib_srcs
stunlib.c
turnclient.c
stuntrace.c
stun_crypto.c
)

set (ADDITIONAL_LIBS "")
Expand All @@ -24,22 +25,25 @@ 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 )


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
Expand Down
60 changes: 60 additions & 0 deletions src/stun_crypto.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* See license file
*/

#include "stun_crypto.h"

#if defined(STUNLIB_USE_OPENSSL)
# include <openssl/md5.h>
# include <openssl/evp.h>
# include <openssl/hmac.h>

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 <CommonCrypto/CommonDigest.h>
# include <CommonCrypto/CommonHMAC.h>

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 <bsd/stdlib.h>
#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 <zlib.h>
uint32_t stunlib_util_crc32(long crc, const uint8_t* buf, size_t len) {
return crc32(crc, buf, len);
}
#endif
66 changes: 14 additions & 52 deletions src/stunlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,7 @@
* See license file
*/
#include "stunlib.h"
#if defined(__APPLE__)
# define COMMON_DIGEST_FOR_OPENSSL
# include <CommonCrypto/CommonDigest.h>
# include <CommonCrypto/CommonHMAC.h>

# define SHA1 CC_SHA1
#else

#include <bsd/stdlib.h>
#include <openssl/md5.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#endif


#include "stun_crypto.h"

#include <zlib.h>

Expand Down Expand Up @@ -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)
{
/*
Expand Down Expand Up @@ -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) )
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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 );
}