25 #include <boost/crc.hpp> 28 #include <openssl/md5.h> 29 #include <openssl/hmac.h> 31 #define COMMON_DIGEST_FOR_OPENSSL 32 #include <CommonCrypto/CommonCrypto.h> 57 uint16_t msgTypeField=0;
63 msgTypeField = (msgType & 0x0f80) << 2;
64 msgTypeField |= (msgType & 0x0070) << 1;
65 msgTypeField |= (msgType & 0x000f);
66 msgTypeField |= (msgClass & 0x02) << 7;
67 msgTypeField |= (msgClass & 0x01) << 4;
108 uint64_t clock = __rdtsc();
109 entropy ^= (uint32_t)(clock);
113 int randomfile = ::open(
"/dev/urandom", O_RDONLY);
116 int readret = read(randomfile, &entropy,
sizeof(entropy));
127 entropy ^=
reinterpret_cast<uintptr_t
>(
this);
128 entropy ^= time(NULL);
140 memcpy(transid.
id, &stun_cookie_nbo,
sizeof(stun_cookie_nbo));
143 transid.
id[x] = (uint8_t)(rand() % 256);
167 uint8_t padBytes[4] = {0};
170 uint16_t sizeheader = size;
178 padding = (size % 4) ? (4 - (size % 4)) : 0;
184 sizeheader += padding;
213 hr =
AddAttribute(attribType, pstr, pstr?strlen(pstr):0);
222 uint8_t padBytes[4] = {0};
223 size_t strsize = (pszReason==NULL) ? 0 : strlen(pszReason);
224 size_t size = strsize + 4;
225 size_t sizeheader = size;
234 padding = (size%4) ? (4-size%4) : 0;
240 padding = 4 - (size % 4);
245 sizeheader += padding;
252 cl = (uint8_t)(errorNumber / 100);
253 ernum = (uint8_t)(errorNumber % 100);
275 uint16_t size = count *
sizeof(uint16_t);
276 uint16_t unpaddedsize = size;
291 size +=
sizeof(uint16_t);
348 const uint16_t PADDING_BUFFER_SIZE = 128;
349 static char padding_bytes[PADDING_BUFFER_SIZE] = {};
354 paddingSize = paddingSize + 4 - (paddingSize % 4);
360 while (paddingSize > 0)
362 uint16_t blocksize = (paddingSize >= PADDING_BUFFER_SIZE) ? PADDING_BUFFER_SIZE : paddingSize;
364 paddingSize -= blocksize;
402 uint32_t changeData = 0;
414 changeData = htonl(changeData);
422 boost::crc_32_type result;
440 pData = spBuffer->GetData();
441 length = spBuffer->GetSize();
445 result.process_bytes(pData, length);
447 value = result.checksum();
450 offset = -(int)(
sizeof(value));
479 const size_t c_hmacsize = 20;
480 uint8_t hmacvaluedummy[c_hmacsize] = {};
481 unsigned int resultlength = c_hmacsize;
482 uint8_t* pDstBuf = NULL;
487 unsigned char* pHashResult = NULL;
499 pData = spBuffer->GetData();
500 length = spBuffer->GetSize();
507 pDstBuf = ((uint8_t*)pData) + length + 4;
510 pHashResult = HMAC(EVP_sha1(), key, keysize, (uint8_t*)pData, length, pDstBuf, &resultlength);
511 ASSERT(resultlength == 20);
512 ASSERT(pHashResult != NULL);
514 CCHmac(kCCHmacAlgSHA1, key, keysize,(uint8_t*)pData, length, pDstBuf);
531 uint8_t key[MAX_KEY_SIZE + 1];
533 uint8_t hash[MD5_DIGEST_LENGTH] = {};
534 uint8_t* pResult = NULL;
537 size_t lenUserName = pszUserName ? strlen(pszUserName) : 0;
538 size_t lenRealm = pszRealm ? strlen(pszRealm) : 0;
539 size_t lenPassword = pszPassword ? strlen(pszPassword) : 0;
540 size_t lenTotal = lenUserName + lenRealm + lenPassword + 2;
555 memcpy(pDst, pszUserName, lenUserName);
561 memcpy(pDst, pszRealm, lenRealm);
567 memcpy(pDst, pszPassword, lenPassword);
571 ASSERT((pDst-key) == lenTotal);
574 pResult = MD5(key, lenTotal, hash);
576 pResult = CC_MD5(key, lenTotal, hash);
StunTransactionId _transactionid
const uint16_t STUN_ATTRIBUTE_USERNAME
HRESULT AddUnknownAttributes(const uint16_t *arrAttributeIds, size_t count)
const uint16_t STUN_ATTRIBUTE_MAPPEDADDRESS_SIZE_IPV6
HRESULT AddPaddingAttribute(uint16_t paddingSize)
HRESULT AddStringAttribute(uint16_t attribType, const char *pstr)
HRESULT AddRandomTransactionId(StunTransactionId *pTransId)
HRESULT WriteUint16(uint16_t val)
const uint16_t STUN_ATTRIBUTE_ERRORCODE
HRESULT AddRealm(const char *pszRealm)
int AtomicIncrement(int *pInt)
const uint16_t STUN_IPV6_LENGTH
const uint16_t STUN_ATTRIBUTE_PADDING
const uint16_t STUN_ATTRIBUTE_RESPONSE_ORIGIN
HRESULT AddXorMappedAddress(const CSocketAddress &addr)
HRESULT AddAttributeHeader(uint16_t attribType, uint16_t size)
HRESULT SeekRelative(int nOffset)
void ApplyStunXorMap(const StunTransactionId &id)
HRESULT AddBindingRequestHeader()
HRESULT GetBuffer(CRefCountedBuffer *pRefCountedBuffer)
const uint32_t STUN_COOKIE
const uint16_t STUN_ATTRIBUTE_REALM
HRESULT AddMappedAddressImpl(uint16_t attribute, const CSocketAddress &addr)
#define UNREFERENCED_VARIABLE(unrefparam)
bool IsNullOrEmpty(const char *psz)
const uint16_t STUN_ATTRIBUTE_FINGERPRINT
const uint16_t STUN_ATTRIBUTE_NONCE
#define ChkIf(expr, hrerror)
HRESULT AddMappedAddress(const CSocketAddress &addr)
const uint16_t STUN_ATTRIBUTE_UNKNOWNATTRIBUTES
size_t GetIP_NBO(void *pAddr, size_t length) const
static int g_sequence_number
void SetLegacyMode(bool fLegacyMode)
HRESULT AddAttribute(uint16_t attribType, const void *data, uint16_t size)
const uint16_t STUN_ATTRIBUTE_CHANGEREQUEST
const uint16_t STUN_ATTRIBUTE_XORMAPPEDADDRESS
const uint32_t MAX_STUN_AUTH_STRING_SIZE
const uint16_t STUN_ATTRIBUTE_RESPONSE_PORT
const uint16_t STUN_IPV4_LENGTH
HRESULT AddHeader(StunMessageType msgType, StunMessageClass msgClass)
HRESULT AddTransactionId(const StunTransactionId &transid)
HRESULT AddBindingResponseHeader(bool fSuccess)
HRESULT AddResponseOriginAddress(const CSocketAddress &other)
HRESULT AddMessageIntegrityLongTerm(const char *pszUserName, const char *pszRealm, const char *pszPassword)
const uint16_t STUN_ATTRIBUTE_MAPPEDADDRESS
HRESULT AddFingerprintAttribute()
HRESULT AddNonce(const char *pszNonce)
HRESULT AddResponsePort(uint16_t port)
const uint16_t STUN_ATTRIBUTE_CHANGEDADDRESS
HRESULT GetResult(CRefCountedBuffer *pspBuffer)
const uint8_t STUN_ATTRIBUTE_FIELD_IPV4
HRESULT Write(const void *data, size_t size)
HRESULT AddErrorCode(uint16_t errorNumber, const char *pszReason)
HRESULT AddMessageIntegrityShortTerm(const char *pszPassword)
const uint16_t STUN_ATTRIBUTE_XORMAPPEDADDRESS_OPTIONAL
HRESULT AddChangeRequest(const StunChangeRequestAttribute &changeAttrib)
uint8_t id[STUN_TRANSACTION_ID_LENGTH]
uint16_t GetPort_NBO() const
HRESULT SeekDirect(size_t pos)
const uint16_t STUN_ATTRIBUTE_SOURCEADDRESS
const uint16_t STUN_TRANSACTION_ID_LENGTH
const uint16_t STUN_ATTRIBUTE_MESSAGEINTEGRITY
HRESULT SetSizeHint(size_t size)
const uint16_t STUN_ATTRIBUTE_MAPPEDADDRESS_SIZE_IPV4
const uint32_t STUN_FINGERPRINT_XOR
uint16_t GetFamily() const
boost::shared_ptr< CBuffer > CRefCountedBuffer
HRESULT WriteUint32(uint32_t val)
HRESULT AddMessageIntegrityImpl(uint8_t *key, size_t keysize)
HRESULT WriteInt16(int16_t val)
#define ChkIfA(expr, hrerror)
const uint32_t STUN_HEADER_SIZE
CDataStream & GetStream()
const uint8_t STUN_ATTRIBUTE_FIELD_IPV6
HRESULT AddUserName(const char *pszUserName)
HRESULT WriteUint8(uint8_t val)
const uint16_t STUN_ATTRIBUTE_OTHER_ADDRESS
HRESULT AddOtherAddress(const CSocketAddress &other)