Haxe Lib Datachannel
This commit is contained in:
249
lib/datachannel/cpp/src/aegis128x4.c
Normal file
249
lib/datachannel/cpp/src/aegis128x4.c
Normal file
@ -0,0 +1,249 @@
|
||||
#define HL_NAME(n) datachannel_##n
|
||||
|
||||
#include <hl.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// AEGIS-128X4 (RFC 10032, section 5.4) keystream DRBG.
|
||||
// VAES-512 path processes all four lanes per instruction; AES-NI path
|
||||
// runs the lanes sequentially. Non-x86 builds report unavailable and the
|
||||
// caller falls back to the portable implementation.
|
||||
|
||||
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
|
||||
#define AEGIS_X86 1
|
||||
#include <immintrin.h>
|
||||
#if defined(_MSC_VER)
|
||||
#include <intrin.h>
|
||||
#else
|
||||
#include <cpuid.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#define TARGET_AVX512 __attribute__((target("avx512f,vaes")))
|
||||
#define TARGET_AESNI __attribute__((target("aes")))
|
||||
#else
|
||||
#define TARGET_AVX512
|
||||
#define TARGET_AESNI
|
||||
#endif
|
||||
|
||||
#ifdef AEGIS_X86
|
||||
|
||||
static void cpuidex(int leaf, int subleaf, int regs[4])
|
||||
{
|
||||
#if defined(_MSC_VER)
|
||||
__cpuidex(regs, leaf, subleaf);
|
||||
#else
|
||||
__cpuid_count(leaf, subleaf, regs[0], regs[1], regs[2], regs[3]);
|
||||
#endif
|
||||
}
|
||||
|
||||
static uint64_t xgetbv0(void)
|
||||
{
|
||||
#if defined(_MSC_VER)
|
||||
return _xgetbv(0);
|
||||
#else
|
||||
uint32_t eax, edx;
|
||||
__asm__ volatile("xgetbv" : "=a"(eax), "=d"(edx) : "c"(0));
|
||||
return ((uint64_t)edx << 32) | eax;
|
||||
#endif
|
||||
}
|
||||
|
||||
// 0 = no AES-NI, 1 = AES-NI, 2 = VAES + AVX-512F with OS ZMM support
|
||||
static int detect(void)
|
||||
{
|
||||
int r[4];
|
||||
cpuidex(1, 0, r);
|
||||
if (!((r[2] >> 25) & 1)) return 0; // AES-NI
|
||||
int osxsave = (r[2] >> 27) & 1;
|
||||
int avx = (r[2] >> 28) & 1;
|
||||
if (!(osxsave && avx)) return 1;
|
||||
uint64_t xcr0 = xgetbv0();
|
||||
if ((xcr0 & 0x6) != 0x6) return 1; // SSE+AVX state
|
||||
cpuidex(7, 0, r);
|
||||
int avx512f = (r[1] >> 16) & 1;
|
||||
int vaes = (r[2] >> 9) & 1;
|
||||
if (avx512f && vaes && (xcr0 & 0xE0) == 0xE0) return 2; // opmask+ZMM state
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int mode = 0;
|
||||
static uint8_t buf[128];
|
||||
static int bufpos = 128;
|
||||
|
||||
// --- VAES-512: lane i of register j holds V[j,i] ---
|
||||
|
||||
static __m512i s4[8];
|
||||
|
||||
TARGET_AVX512 static void update512(__m512i m0, __m512i m1)
|
||||
{
|
||||
__m512i a0 = s4[0], a1 = s4[1], a2 = s4[2], a3 = s4[3];
|
||||
__m512i a4 = s4[4], a5 = s4[5], a6 = s4[6], a7 = s4[7];
|
||||
s4[0] = _mm512_aesenc_epi128(a7, _mm512_xor_si512(a0, m0));
|
||||
s4[1] = _mm512_aesenc_epi128(a0, a1);
|
||||
s4[2] = _mm512_aesenc_epi128(a1, a2);
|
||||
s4[3] = _mm512_aesenc_epi128(a2, a3);
|
||||
s4[4] = _mm512_aesenc_epi128(a3, _mm512_xor_si512(a4, m1));
|
||||
s4[5] = _mm512_aesenc_epi128(a4, a5);
|
||||
s4[6] = _mm512_aesenc_epi128(a5, a6);
|
||||
s4[7] = _mm512_aesenc_epi128(a6, a7);
|
||||
}
|
||||
|
||||
TARGET_AVX512 static void init512(const uint8_t* key, const uint8_t* nonce)
|
||||
{
|
||||
const __m128i c0_128 = _mm_setr_epi8(
|
||||
0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d,
|
||||
0x15, 0x22, 0x37, 0x59, (char)0x90, (char)0xe9, 0x79, 0x62);
|
||||
const __m128i c1_128 = _mm_setr_epi8(
|
||||
(char)0xdb, 0x3d, 0x18, 0x55, 0x6d, (char)0xc2, 0x2f, (char)0xf1,
|
||||
0x20, 0x11, 0x31, 0x42, 0x73, (char)0xb5, 0x28, (char)0xdd);
|
||||
__m512i kv = _mm512_broadcast_i32x4(_mm_loadu_si128((const __m128i*)key));
|
||||
__m512i nv = _mm512_broadcast_i32x4(_mm_loadu_si128((const __m128i*)nonce));
|
||||
__m512i c0 = _mm512_broadcast_i32x4(c0_128);
|
||||
__m512i c1 = _mm512_broadcast_i32x4(c1_128);
|
||||
__m512i kn = _mm512_xor_si512(kv, nv);
|
||||
s4[0] = kn;
|
||||
s4[1] = c1;
|
||||
s4[2] = c0;
|
||||
s4[3] = c1;
|
||||
s4[4] = kn;
|
||||
s4[5] = _mm512_xor_si512(kv, c0);
|
||||
s4[6] = _mm512_xor_si512(kv, c1);
|
||||
s4[7] = _mm512_xor_si512(kv, c0);
|
||||
// ctx[i] = Byte(i) || Byte(D-1) || zeros, one per 128-bit lane
|
||||
__m512i ctx = _mm512_set_epi32(
|
||||
0, 0, 0, 0x303, 0, 0, 0, 0x302, 0, 0, 0, 0x301, 0, 0, 0, 0x300);
|
||||
for (int r = 0; r < 10; r++) {
|
||||
s4[3] = _mm512_xor_si512(s4[3], ctx);
|
||||
s4[7] = _mm512_xor_si512(s4[7], ctx);
|
||||
update512(nv, kv);
|
||||
}
|
||||
}
|
||||
|
||||
TARGET_AVX512 static void block512(uint8_t* out)
|
||||
{
|
||||
__m512i z0 = _mm512_xor_si512(_mm512_xor_si512(s4[1], s4[6]),
|
||||
_mm512_and_si512(s4[2], s4[3]));
|
||||
__m512i z1 = _mm512_xor_si512(_mm512_xor_si512(s4[2], s4[5]),
|
||||
_mm512_and_si512(s4[6], s4[7]));
|
||||
_mm512_storeu_si512((void*)out, z0);
|
||||
_mm512_storeu_si512((void*)(out + 64), z1);
|
||||
update512(_mm512_setzero_si512(), _mm512_setzero_si512());
|
||||
}
|
||||
|
||||
// --- AES-NI: s1[j][i] holds V[j,i] ---
|
||||
|
||||
static __m128i s1[8][4];
|
||||
|
||||
TARGET_AESNI static void update128(__m128i m0[4], __m128i m1[4])
|
||||
{
|
||||
for (int i = 0; i < 4; i++) {
|
||||
__m128i a0 = s1[0][i], a1 = s1[1][i], a2 = s1[2][i], a3 = s1[3][i];
|
||||
__m128i a4 = s1[4][i], a5 = s1[5][i], a6 = s1[6][i], a7 = s1[7][i];
|
||||
s1[0][i] = _mm_aesenc_si128(a7, _mm_xor_si128(a0, m0[i]));
|
||||
s1[1][i] = _mm_aesenc_si128(a0, a1);
|
||||
s1[2][i] = _mm_aesenc_si128(a1, a2);
|
||||
s1[3][i] = _mm_aesenc_si128(a2, a3);
|
||||
s1[4][i] = _mm_aesenc_si128(a3, _mm_xor_si128(a4, m1[i]));
|
||||
s1[5][i] = _mm_aesenc_si128(a4, a5);
|
||||
s1[6][i] = _mm_aesenc_si128(a5, a6);
|
||||
s1[7][i] = _mm_aesenc_si128(a6, a7);
|
||||
}
|
||||
}
|
||||
|
||||
TARGET_AESNI static void init128(const uint8_t* key, const uint8_t* nonce)
|
||||
{
|
||||
const __m128i c0 = _mm_setr_epi8(
|
||||
0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d,
|
||||
0x15, 0x22, 0x37, 0x59, (char)0x90, (char)0xe9, 0x79, 0x62);
|
||||
const __m128i c1 = _mm_setr_epi8(
|
||||
(char)0xdb, 0x3d, 0x18, 0x55, 0x6d, (char)0xc2, 0x2f, (char)0xf1,
|
||||
0x20, 0x11, 0x31, 0x42, 0x73, (char)0xb5, 0x28, (char)0xdd);
|
||||
__m128i k = _mm_loadu_si128((const __m128i*)key);
|
||||
__m128i n = _mm_loadu_si128((const __m128i*)nonce);
|
||||
__m128i kn = _mm_xor_si128(k, n);
|
||||
__m128i m0[4], m1[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
s1[0][i] = kn;
|
||||
s1[1][i] = c1;
|
||||
s1[2][i] = c0;
|
||||
s1[3][i] = c1;
|
||||
s1[4][i] = kn;
|
||||
s1[5][i] = _mm_xor_si128(k, c0);
|
||||
s1[6][i] = _mm_xor_si128(k, c1);
|
||||
s1[7][i] = _mm_xor_si128(k, c0);
|
||||
m0[i] = n;
|
||||
m1[i] = k;
|
||||
}
|
||||
for (int r = 0; r < 10; r++) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
__m128i ctx = _mm_set_epi32(0, 0, 0, 0x300 + i);
|
||||
s1[3][i] = _mm_xor_si128(s1[3][i], ctx);
|
||||
s1[7][i] = _mm_xor_si128(s1[7][i], ctx);
|
||||
}
|
||||
update128(m0, m1);
|
||||
}
|
||||
}
|
||||
|
||||
TARGET_AESNI static void block128(uint8_t* out)
|
||||
{
|
||||
for (int i = 0; i < 4; i++) {
|
||||
__m128i z0 = _mm_xor_si128(_mm_xor_si128(s1[1][i], s1[6][i]),
|
||||
_mm_and_si128(s1[2][i], s1[3][i]));
|
||||
__m128i z1 = _mm_xor_si128(_mm_xor_si128(s1[2][i], s1[5][i]),
|
||||
_mm_and_si128(s1[6][i], s1[7][i]));
|
||||
_mm_storeu_si128((__m128i*)(out + i * 16), z0);
|
||||
_mm_storeu_si128((__m128i*)(out + 64 + i * 16), z1);
|
||||
}
|
||||
__m128i zero[4];
|
||||
for (int i = 0; i < 4; i++) zero[i] = _mm_setzero_si128();
|
||||
update128(zero, zero);
|
||||
}
|
||||
|
||||
#endif // AEGIS_X86
|
||||
|
||||
HL_PRIM bool HL_NAME(aegis_available)()
|
||||
{
|
||||
#ifdef AEGIS_X86
|
||||
return detect() != 0;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
HL_PRIM bool HL_NAME(aegis_init)(vbyte* key, vbyte* nonce)
|
||||
{
|
||||
#ifdef AEGIS_X86
|
||||
mode = detect();
|
||||
if (mode == 2) init512((const uint8_t*)key, (const uint8_t*)nonce);
|
||||
else if (mode == 1) init128((const uint8_t*)key, (const uint8_t*)nonce);
|
||||
else return false;
|
||||
bufpos = 128;
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(aegis_random)(vbyte* out, int len)
|
||||
{
|
||||
#ifdef AEGIS_X86
|
||||
int off = 0;
|
||||
while (off < len) {
|
||||
if (bufpos >= 128) {
|
||||
if (mode == 2) block512(buf);
|
||||
else block128(buf);
|
||||
bufpos = 0;
|
||||
}
|
||||
int n = len - off;
|
||||
if (n > 128 - bufpos) n = 128 - bufpos;
|
||||
memcpy(out + off, buf + bufpos, n);
|
||||
bufpos += n;
|
||||
off += n;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
DEFINE_PRIM(_BOOL, aegis_available, _NO_ARG);
|
||||
DEFINE_PRIM(_BOOL, aegis_init, _BYTES _BYTES);
|
||||
DEFINE_PRIM(_VOID, aegis_random, _BYTES _I32);
|
||||
721
lib/datachannel/cpp/src/datachannel.c
Normal file
721
lib/datachannel/cpp/src/datachannel.c
Normal file
@ -0,0 +1,721 @@
|
||||
#define HL_NAME(n) datachannel_##n
|
||||
|
||||
#include <hl.h>
|
||||
#include <rtc/rtc.h>
|
||||
|
||||
typedef struct _hl_rtc_peerconnection hl_rtc_peerconnection;
|
||||
struct _hl_rtc_peerconnection {
|
||||
void (*finalize)(hl_rtc_peerconnection*);
|
||||
int pc;
|
||||
vclosure* descCb;
|
||||
vclosure* candidateCb;
|
||||
vclosure* stateCb;
|
||||
vclosure* gatheringStateCb;
|
||||
|
||||
// For datachannel
|
||||
vclosure* datachannelCb;
|
||||
};
|
||||
|
||||
typedef struct _hl_rtc_datachannel hl_rtc_datachannel;
|
||||
struct _hl_rtc_datachannel {
|
||||
void (*finalize)(hl_rtc_datachannel*);
|
||||
int dc;
|
||||
vclosure* openCb;
|
||||
vclosure* closedCb;
|
||||
vclosure* errCb;
|
||||
vclosure* msgCb;
|
||||
vclosure* bufferLowCb;
|
||||
};
|
||||
|
||||
typedef struct _callback_result callback_result;
|
||||
struct _callback_result {
|
||||
vclosure** closure;
|
||||
vdynamic arg1;
|
||||
vdynamic arg2;
|
||||
int args;
|
||||
int arg1Size;
|
||||
int arg2Size;
|
||||
callback_result* next;
|
||||
const char* callback_name;
|
||||
};
|
||||
|
||||
typedef struct _datachannel_callback datachannel_callback;
|
||||
struct _datachannel_callback {
|
||||
int dc;
|
||||
hl_rtc_peerconnection* pc;
|
||||
datachannel_callback* next;
|
||||
};
|
||||
|
||||
hl_mutex* callback_result_mutex = NULL;
|
||||
hl_semaphore* callback_result_semaphore = NULL;
|
||||
hl_semaphore* datachannel_callbacks_semaphore = NULL;
|
||||
callback_result* callback_results = NULL;
|
||||
callback_result* callback_results_end = NULL;
|
||||
datachannel_callback* datachannel_callbacks = NULL;
|
||||
datachannel_callback* datachannel_callbacks_end = NULL;
|
||||
|
||||
callback_result* callback_result_alloc()
|
||||
{
|
||||
if (callback_results_end == NULL)
|
||||
{
|
||||
callback_results_end = callback_results = (callback_result*)malloc(sizeof(callback_result));
|
||||
callback_results_end->next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
callback_results_end->next = (callback_result*)malloc(sizeof(callback_result));
|
||||
callback_results_end = callback_results_end->next;
|
||||
callback_results_end->next = NULL;
|
||||
}
|
||||
return callback_results_end;
|
||||
}
|
||||
|
||||
datachannel_callback* datachannel_callback_alloc()
|
||||
{
|
||||
if (datachannel_callbacks_end == NULL)
|
||||
{
|
||||
datachannel_callbacks_end = datachannel_callbacks = (datachannel_callback*)malloc(sizeof(datachannel_callback));
|
||||
datachannel_callbacks_end->next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
datachannel_callbacks_end->next = (datachannel_callback*)malloc(sizeof(datachannel_callback));
|
||||
datachannel_callbacks_end = datachannel_callbacks_end->next;
|
||||
datachannel_callbacks_end->next = NULL;
|
||||
}
|
||||
return datachannel_callbacks_end;
|
||||
}
|
||||
|
||||
void hl_rtc_peerconnection_finalize(hl_rtc_peerconnection* pc)
|
||||
{
|
||||
rtcSetLocalCandidateCallback(pc->pc, NULL);
|
||||
rtcSetLocalDescriptionCallback(pc->pc, NULL);
|
||||
rtcSetStateChangeCallback(pc->pc, NULL);
|
||||
rtcSetGatheringStateChangeCallback(pc->pc, NULL);
|
||||
rtcSetDataChannelCallback(pc->pc, NULL);
|
||||
rtcDeletePeerConnection(pc->pc);
|
||||
hl_remove_root(&pc->candidateCb);
|
||||
hl_remove_root(&pc->datachannelCb);
|
||||
hl_remove_root(&pc->descCb);
|
||||
hl_remove_root(&pc->gatheringStateCb);
|
||||
hl_remove_root(&pc->stateCb);
|
||||
}
|
||||
|
||||
void hl_rtc_datachannel_finalize(hl_rtc_datachannel* dc)
|
||||
{
|
||||
rtcSetOpenCallback(dc->dc, NULL);
|
||||
rtcSetClosedCallback(dc->dc, NULL);
|
||||
rtcSetErrorCallback(dc->dc, NULL);
|
||||
rtcSetMessageCallback(dc->dc, NULL);
|
||||
rtcSetBufferedAmountLowCallback(dc->dc, NULL);
|
||||
rtcDeleteDataChannel(dc->dc);
|
||||
hl_remove_root(&dc->bufferLowCb);
|
||||
hl_remove_root(&dc->closedCb);
|
||||
hl_remove_root(&dc->errCb);
|
||||
hl_remove_root(&dc->msgCb);
|
||||
hl_remove_root(&dc->openCb);
|
||||
}
|
||||
|
||||
static void RTC_API descriptionCallback(int pc, const char* sdp, const char* type, void* ptr);
|
||||
static void RTC_API candidateCallback(int pc, const char* cand, const char* mid, void* ptr);
|
||||
static void RTC_API stateChangeCallback(int pc, rtcState state, void* ptr);
|
||||
static void RTC_API gatheringStateCallback(int pc, rtcGatheringState state, void* ptr);
|
||||
static void RTC_API openCallback(int id, void* ptr);
|
||||
static void RTC_API closedCallback(int id, void* ptr);
|
||||
static void RTC_API errorCallback(int id, const char*, void* ptr);
|
||||
static void RTC_API messageCallback(int id, const char* message, int size, void* ptr);
|
||||
static void RTC_API bufferedAmountLowCallback(int id, void* ptr);
|
||||
static void RTC_API dataChannelCallback(int pc, int dc, void* ptr);
|
||||
|
||||
hl_rtc_datachannel* hl_rtc_make_datachannel(int dc)
|
||||
{
|
||||
hl_rtc_datachannel* hldc = (hl_rtc_datachannel*)hl_gc_alloc_finalizer(sizeof(hl_rtc_datachannel));
|
||||
hldc->finalize = hl_rtc_datachannel_finalize;
|
||||
hldc->dc = dc;
|
||||
hldc->openCb = NULL;
|
||||
hldc->closedCb = NULL;
|
||||
hldc->errCb = NULL;
|
||||
hldc->msgCb = NULL;
|
||||
hldc->bufferLowCb = NULL;
|
||||
hl_add_root(&hldc->bufferLowCb);
|
||||
hl_add_root(&hldc->closedCb);
|
||||
hl_add_root(&hldc->errCb);
|
||||
hl_add_root(&hldc->msgCb);
|
||||
hl_add_root(&hldc->openCb);
|
||||
rtcSetUserPointer(dc, hldc); // It gets messed up so we need to re-set it
|
||||
rtcSetOpenCallback(dc, openCallback);
|
||||
rtcSetClosedCallback(dc, closedCallback);
|
||||
rtcSetErrorCallback(dc, errorCallback);
|
||||
rtcSetMessageCallback(dc, messageCallback);
|
||||
rtcSetBufferedAmountLowCallback(dc, bufferedAmountLowCallback);
|
||||
return hldc;
|
||||
}
|
||||
|
||||
#define _TPC _ABSTRACT(hl_rtc_peerconnection)
|
||||
#define _TDC _ABSTRACT(hl_rtc_datachannel)
|
||||
|
||||
HL_PRIM void HL_NAME(initialize)()
|
||||
{
|
||||
if (callback_result_mutex == NULL)
|
||||
{
|
||||
callback_result_mutex = hl_mutex_alloc(false);
|
||||
hl_add_root(&callback_result_mutex);
|
||||
}
|
||||
|
||||
if (callback_result_semaphore == NULL)
|
||||
{
|
||||
callback_result_semaphore = hl_semaphore_alloc(0);
|
||||
hl_add_root(&callback_result_semaphore);
|
||||
}
|
||||
|
||||
if (datachannel_callbacks_semaphore == NULL)
|
||||
{
|
||||
datachannel_callbacks_semaphore = hl_semaphore_alloc(0);
|
||||
hl_add_root(&datachannel_callbacks_semaphore);
|
||||
}
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(finalize)()
|
||||
{
|
||||
if (callback_result_mutex != NULL)
|
||||
{
|
||||
hl_remove_root(&callback_result_mutex);
|
||||
hl_mutex_free(callback_result_mutex);
|
||||
callback_result_mutex = NULL;
|
||||
}
|
||||
|
||||
if (callback_result_semaphore != NULL)
|
||||
{
|
||||
hl_remove_root(&callback_result_semaphore);
|
||||
hl_semaphore_free(callback_result_semaphore);
|
||||
callback_result_semaphore = NULL;
|
||||
}
|
||||
|
||||
if (datachannel_callbacks_semaphore != NULL)
|
||||
{
|
||||
hl_remove_root(&datachannel_callbacks_semaphore);
|
||||
hl_semaphore_free(datachannel_callbacks_semaphore);
|
||||
datachannel_callbacks_semaphore = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
HL_PRIM hl_rtc_peerconnection* HL_NAME(create_peer_connection)(varray* iceServers, vstring* bindAddress, int portBegin, int portEnd, int mtu, int maxMessageSize)
|
||||
{
|
||||
rtcConfiguration conf;
|
||||
memset(&conf, 0, sizeof(conf));
|
||||
const char** iceServersMem = (const char**)hl_gc_alloc_raw(sizeof(char*) * iceServers->size);
|
||||
for (int i = 0; i < iceServers->size; i++) {
|
||||
vbyte* iceServer = hl_aptr(iceServers, vbyte*)[i];
|
||||
iceServersMem[i] = (char*)iceServer;
|
||||
}
|
||||
conf.iceServers = iceServersMem;
|
||||
conf.iceServersCount = iceServers->size;
|
||||
conf.bindAddress = hl_to_utf8(bindAddress->bytes);
|
||||
conf.portRangeBegin = portBegin;
|
||||
conf.portRangeEnd = portEnd;
|
||||
conf.mtu = mtu;
|
||||
conf.maxMessageSize = maxMessageSize;
|
||||
int pc = rtcCreatePeerConnection(&conf);
|
||||
hl_rtc_peerconnection* hlpc = (hl_rtc_peerconnection*)hl_gc_alloc_finalizer(sizeof(hl_rtc_peerconnection));
|
||||
hlpc->finalize = hl_rtc_peerconnection_finalize;
|
||||
hlpc->pc = pc;
|
||||
hlpc->candidateCb = NULL;
|
||||
hlpc->datachannelCb = NULL;
|
||||
hlpc->descCb = NULL;
|
||||
hlpc->gatheringStateCb = NULL;
|
||||
hlpc->stateCb = NULL;
|
||||
hl_add_root(&hlpc->candidateCb);
|
||||
hl_add_root(&hlpc->datachannelCb);
|
||||
hl_add_root(&hlpc->descCb);
|
||||
hl_add_root(&hlpc->gatheringStateCb);
|
||||
hl_add_root(&hlpc->stateCb);
|
||||
rtcSetUserPointer(pc, hlpc);
|
||||
rtcSetLocalDescriptionCallback(pc, descriptionCallback);
|
||||
rtcSetLocalCandidateCallback(pc, candidateCallback);
|
||||
rtcSetStateChangeCallback(pc, stateChangeCallback);
|
||||
rtcSetGatheringStateChangeCallback(pc, gatheringStateCallback);
|
||||
return hlpc;
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(close_peer_connection)(hl_rtc_peerconnection* pc)
|
||||
{
|
||||
rtcClosePeerConnection(pc->pc);
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(set_peer_connection_callbacks)(hl_rtc_peerconnection* pc, vclosure* descCb, vclosure* candidateCb, vclosure* stateCb, vclosure* gatheringStateCb)
|
||||
{
|
||||
pc->descCb = descCb;
|
||||
pc->candidateCb = candidateCb;
|
||||
pc->stateCb = stateCb;
|
||||
pc->gatheringStateCb = gatheringStateCb;
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(set_remote_description)(hl_rtc_peerconnection* pc, vbyte* desc, vstring* type)
|
||||
{
|
||||
char* typestr = hl_to_utf8(type->bytes);
|
||||
if (strcmp(typestr, "offer") == 0) {
|
||||
rtcSetRemoteDescription(pc->pc, (const char*)desc, "offer");
|
||||
}
|
||||
if (strcmp(typestr, "answer") == 0) {
|
||||
rtcSetRemoteDescription(pc->pc, (const char*)desc, "answer");
|
||||
}
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(add_remote_candidate)(hl_rtc_peerconnection* pc, vbyte* candidate)
|
||||
{
|
||||
rtcAddRemoteCandidate(pc->pc, (const char*)candidate, NULL);
|
||||
}
|
||||
|
||||
HL_PRIM hl_rtc_datachannel* HL_NAME(create_datachannel)(hl_rtc_peerconnection* pc, vstring* name)
|
||||
{
|
||||
char* namestr = hl_to_utf8(name->bytes);
|
||||
int dc = rtcCreateDataChannel(pc->pc, namestr);
|
||||
return hl_rtc_make_datachannel(dc);
|
||||
}
|
||||
|
||||
HL_PRIM hl_rtc_datachannel* HL_NAME(create_datachannel_ex)(hl_rtc_peerconnection* pc, vstring* name, bool unordered, int maxRetransmits, int maxLifetime)
|
||||
{
|
||||
char* namestr = hl_to_utf8(name->bytes);
|
||||
rtcDataChannelInit dcInit;
|
||||
memset(&dcInit, 0, sizeof(rtcDataChannelInit));
|
||||
dcInit.reliability.unreliable = maxLifetime != 0 || maxRetransmits != 0;
|
||||
dcInit.reliability.unordered = unordered;
|
||||
dcInit.reliability.maxPacketLifeTime = maxLifetime;
|
||||
dcInit.reliability.maxRetransmits = maxRetransmits;
|
||||
int dc = rtcCreateDataChannelEx(pc->pc, namestr, &dcInit);
|
||||
return hl_rtc_make_datachannel(dc);
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(set_peerconnection_datachannel_cb)(hl_rtc_peerconnection* pc, vclosure* openCb)
|
||||
{
|
||||
rtcSetDataChannelCallback(pc->pc, dataChannelCallback);
|
||||
pc->datachannelCb = openCb;
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(set_datachannel_callbacks)(hl_rtc_datachannel* dc, vclosure* openCb, vclosure* closeCb, vclosure* errorCb, vclosure* msgCb, vclosure* bufferLowCb)
|
||||
{
|
||||
dc->openCb = openCb;
|
||||
dc->closedCb = closeCb;
|
||||
dc->errCb = errorCb;
|
||||
dc->msgCb = msgCb;
|
||||
dc->bufferLowCb = bufferLowCb;
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(datachannel_send_message)(hl_rtc_datachannel* dc, vbyte* bytes, int len)
|
||||
{
|
||||
rtcSendMessage(dc->dc, (const char*)bytes, len);
|
||||
}
|
||||
|
||||
HL_PRIM vdynobj* HL_NAME(get_datachannel_reliability)(hl_rtc_datachannel* dc)
|
||||
{
|
||||
rtcReliability r;
|
||||
rtcGetDataChannelReliability(dc->dc, &r);
|
||||
vdynamic* obj = (vdynamic*)hl_alloc_dynobj();
|
||||
hl_dyn_seti(obj, hl_hash_utf8("unordered"), &hlt_bool, r.unordered);
|
||||
hl_dyn_seti(obj, hl_hash_utf8("maxRetransmits"), &hlt_i32, r.maxRetransmits);
|
||||
hl_dyn_seti(obj, hl_hash_utf8("maxLifetime"), &hlt_i32, r.maxPacketLifeTime);
|
||||
return (vdynobj*)obj;
|
||||
}
|
||||
|
||||
HL_PRIM vbyte* HL_NAME(get_local_address)(hl_rtc_peerconnection* pc)
|
||||
{
|
||||
char buf[256];
|
||||
int buflen = rtcGetLocalAddress(pc->pc, buf, 256);
|
||||
if (buflen > 0)
|
||||
{
|
||||
vbyte* mem = (vbyte*)hl_gc_alloc_noptr(buflen);
|
||||
memcpy(mem, buf, buflen);
|
||||
return mem;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
HL_PRIM vbyte* HL_NAME(get_remote_address)(hl_rtc_peerconnection* pc)
|
||||
{
|
||||
char buf[256];
|
||||
int buflen = rtcGetRemoteAddress(pc->pc, buf, 256);
|
||||
if (buflen > 0)
|
||||
{
|
||||
vbyte* mem = (vbyte*)hl_gc_alloc_noptr(buflen);
|
||||
memcpy(mem, buf, buflen);
|
||||
return mem;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
HL_PRIM vbyte* HL_NAME(get_local_description)(hl_rtc_peerconnection* pc)
|
||||
{
|
||||
char buf[16384];
|
||||
int buflen = rtcGetLocalDescription(pc->pc, buf, 16384);
|
||||
if (buflen > 0)
|
||||
{
|
||||
vbyte* mem = (vbyte*)hl_gc_alloc_noptr(buflen);
|
||||
memcpy(mem, buf, buflen);
|
||||
return mem;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
HL_PRIM int HL_NAME(get_buffered_amount)(hl_rtc_datachannel* dc)
|
||||
{
|
||||
return rtcGetBufferedAmount(dc->dc);
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(set_buffered_amount_low_threshold)(hl_rtc_datachannel* dc, int amt)
|
||||
{
|
||||
rtcSetBufferedAmountLowThreshold(dc->dc, amt);
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(process_events)()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
datachannel_callback* res = NULL;
|
||||
hl_mutex_acquire(callback_result_mutex);
|
||||
if (datachannel_callbacks != NULL)
|
||||
{
|
||||
res = datachannel_callbacks;
|
||||
datachannel_callbacks = datachannel_callbacks->next;
|
||||
if (datachannel_callbacks == NULL)
|
||||
datachannel_callbacks_end = NULL;
|
||||
}
|
||||
hl_mutex_release(callback_result_mutex);
|
||||
|
||||
if (res == NULL)
|
||||
break;
|
||||
|
||||
hl_rtc_datachannel* hldc = hl_rtc_make_datachannel(res->dc);
|
||||
|
||||
vdynamic* args[2];
|
||||
vdynamic pcarg;
|
||||
pcarg.t = res->pc->datachannelCb->t->fun->args[0]; // same type as hl_rtc_datachannel
|
||||
pcarg.v.ptr = hldc;
|
||||
args[0] = &pcarg;
|
||||
|
||||
char buffer[256];
|
||||
if (rtcGetDataChannelLabel(hldc->dc, buffer, 256) >= 0)
|
||||
{
|
||||
vdynamic arg;
|
||||
arg.t = &hlt_bytes;
|
||||
arg.v.bytes = (vbyte*)hl_gc_alloc_noptr(256);
|
||||
memcpy(arg.v.bytes, buffer, 256);
|
||||
|
||||
args[1] = &arg;
|
||||
hl_dyn_call(res->pc->datachannelCb, args, 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
vdynamic arg;
|
||||
arg.t = &hlt_bytes;
|
||||
arg.v.bytes = (vbyte*)hl_gc_alloc_noptr(8);
|
||||
memcpy(arg.v.bytes, "unnamed", 8);
|
||||
|
||||
args[1] = &arg;
|
||||
hl_dyn_call(res->pc->datachannelCb, args, 2);
|
||||
}
|
||||
|
||||
free(res);
|
||||
}
|
||||
|
||||
// Regular callbacks: one semaphore token per queued item.
|
||||
while (true)
|
||||
{
|
||||
if (!hl_semaphore_try_acquire(callback_result_semaphore, NULL))
|
||||
break;
|
||||
|
||||
callback_result* res = NULL;
|
||||
hl_mutex_acquire(callback_result_mutex);
|
||||
if (callback_results != NULL)
|
||||
{
|
||||
res = callback_results;
|
||||
callback_results = callback_results->next;
|
||||
if (callback_results == NULL)
|
||||
callback_results_end = NULL;
|
||||
}
|
||||
hl_mutex_release(callback_result_mutex);
|
||||
|
||||
if (res == NULL)
|
||||
break;
|
||||
|
||||
vdynamic* args[2];
|
||||
vdynamic arg1 = res->arg1;
|
||||
vdynamic arg2 = res->arg2;
|
||||
if (res->args >= 1 && res->arg1.t == &hlt_bytes)
|
||||
{
|
||||
arg1.v.bytes = (vbyte*)hl_gc_alloc_noptr(res->arg1Size);
|
||||
memcpy(arg1.v.bytes, res->arg1.v.bytes, res->arg1Size);
|
||||
free(res->arg1.v.bytes);
|
||||
}
|
||||
if (res->args >= 2 && res->arg2.t == &hlt_bytes)
|
||||
{
|
||||
arg2.v.bytes = (vbyte*)hl_gc_alloc_noptr(res->arg2Size);
|
||||
memcpy(arg2.v.bytes, res->arg2.v.bytes, res->arg2Size);
|
||||
free(res->arg2.v.bytes);
|
||||
}
|
||||
|
||||
if (res->closure != NULL)
|
||||
{
|
||||
switch (res->args)
|
||||
{
|
||||
case 0:
|
||||
hl_dyn_call(*res->closure, NULL, 0);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
args[0] = &arg1;
|
||||
hl_dyn_call(*res->closure, args, 1);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
args[0] = &arg1;
|
||||
args[1] = &arg2;
|
||||
hl_dyn_call(*res->closure, args, 2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free(res);
|
||||
}
|
||||
}
|
||||
|
||||
static void RTC_API descriptionCallback(int pc, const char* sdp, const char* type, void* ptr)
|
||||
{
|
||||
hl_rtc_peerconnection* hlpc = (hl_rtc_peerconnection*)ptr;
|
||||
|
||||
int sdpLen = strlen(sdp);
|
||||
vbyte* b1 = (vbyte*)malloc(sdpLen + 1); // hl_gc_alloc_noptr(sdpLen + 1);
|
||||
memcpy(b1, sdp, sdpLen);
|
||||
b1[sdpLen] = '\0';
|
||||
|
||||
int typeLen = strlen(type);
|
||||
vbyte* b2 = (vbyte*)malloc(typeLen + 1); // hl_gc_alloc_noptr(typeLen + 1);
|
||||
memcpy(b2, type, typeLen);
|
||||
b2[typeLen] = '\0';
|
||||
|
||||
hl_mutex_acquire(callback_result_mutex);
|
||||
callback_result* res = callback_result_alloc();
|
||||
res->arg1.t = &hlt_bytes;
|
||||
res->arg1.v.bytes = b1;
|
||||
res->arg2.t = &hlt_bytes;
|
||||
res->arg2.v.bytes = b2;
|
||||
res->closure = &hlpc->descCb;
|
||||
res->args = 2;
|
||||
res->arg1Size = sdpLen + 1;
|
||||
res->arg2Size = typeLen + 1;
|
||||
res->callback_name = "onDescription";
|
||||
hl_mutex_release(callback_result_mutex);
|
||||
hl_semaphore_release(callback_result_semaphore);
|
||||
}
|
||||
|
||||
static void RTC_API candidateCallback(int pc, const char* cand, const char* mid, void* ptr)
|
||||
{
|
||||
hl_rtc_peerconnection* hlpc = (hl_rtc_peerconnection*)ptr;
|
||||
|
||||
int sdpLen = strlen(cand);
|
||||
vbyte* b1 = (vbyte*)malloc(sdpLen + 1); // hl_gc_alloc_noptr(sdpLen + 1);
|
||||
memcpy(b1, cand, sdpLen);
|
||||
b1[sdpLen] = '\0';
|
||||
|
||||
hl_mutex_acquire(callback_result_mutex);
|
||||
callback_result* res = callback_result_alloc();
|
||||
res->arg1.t = &hlt_bytes;
|
||||
res->arg1.v.bytes = b1;
|
||||
res->closure = &hlpc->candidateCb;
|
||||
res->args = 1;
|
||||
res->arg1Size = sdpLen + 1;
|
||||
res->callback_name = "onCandidate";
|
||||
hl_mutex_release(callback_result_mutex);
|
||||
hl_semaphore_release(callback_result_semaphore);
|
||||
}
|
||||
|
||||
static void RTC_API stateChangeCallback(int pc, rtcState state, void* ptr)
|
||||
{
|
||||
hl_rtc_peerconnection* hlpc = (hl_rtc_peerconnection*)ptr;
|
||||
|
||||
hl_mutex_acquire(callback_result_mutex);
|
||||
callback_result* res = callback_result_alloc();
|
||||
res->arg1.t = &hlt_i32;
|
||||
res->arg1.v.i = state;
|
||||
res->closure = &hlpc->stateCb;
|
||||
res->args = 1;
|
||||
res->callback_name = "onStateChange";
|
||||
hl_mutex_release(callback_result_mutex);
|
||||
hl_semaphore_release(callback_result_semaphore);
|
||||
}
|
||||
|
||||
static void RTC_API gatheringStateCallback(int pc, rtcGatheringState state, void* ptr)
|
||||
{
|
||||
hl_rtc_peerconnection* hlpc = (hl_rtc_peerconnection*)ptr;
|
||||
|
||||
hl_mutex_acquire(callback_result_mutex);
|
||||
callback_result* res = callback_result_alloc();
|
||||
res->arg1.t = &hlt_i32;
|
||||
res->arg1.v.i = state;
|
||||
res->closure = &hlpc->gatheringStateCb;
|
||||
res->args = 1;
|
||||
res->callback_name = "onGatheringStateChange";
|
||||
hl_mutex_release(callback_result_mutex);
|
||||
hl_semaphore_release(callback_result_semaphore);
|
||||
}
|
||||
|
||||
static void RTC_API openCallback(int id, void* ptr)
|
||||
{
|
||||
hl_rtc_datachannel* hldc = (hl_rtc_datachannel*)ptr;
|
||||
if (hldc->openCb == NULL) return;
|
||||
char buffer[256];
|
||||
if (rtcGetDataChannelLabel(hldc->dc, buffer, 256) >= 0)
|
||||
{
|
||||
hl_mutex_acquire(callback_result_mutex);
|
||||
callback_result* res = callback_result_alloc();
|
||||
res->arg1.t = &hlt_bytes;
|
||||
res->arg1.v.bytes = (vbyte*)malloc(256);
|
||||
memcpy(res->arg1.v.bytes, buffer, 256);
|
||||
res->arg1Size = 256;
|
||||
res->closure = &hldc->openCb;
|
||||
res->args = 1;
|
||||
res->callback_name = "onOpen";
|
||||
hl_mutex_release(callback_result_mutex);
|
||||
hl_semaphore_release(callback_result_semaphore);
|
||||
}
|
||||
else
|
||||
{
|
||||
hl_mutex_acquire(callback_result_mutex);
|
||||
callback_result* res = callback_result_alloc();
|
||||
res->arg1.t = &hlt_bytes;
|
||||
res->arg1.v.bytes = (vbyte*)malloc(8);
|
||||
memcpy(res->arg1.v.bytes, "unnamed", 8);
|
||||
res->arg1Size = 8;
|
||||
res->closure = &hldc->openCb;
|
||||
res->args = 1;
|
||||
res->callback_name = "onOpen";
|
||||
hl_mutex_release(callback_result_mutex);
|
||||
hl_semaphore_release(callback_result_semaphore);
|
||||
}
|
||||
}
|
||||
|
||||
static void RTC_API closedCallback(int id, void* ptr)
|
||||
{
|
||||
hl_rtc_datachannel* hldc = (hl_rtc_datachannel*)ptr;
|
||||
|
||||
hl_mutex_acquire(callback_result_mutex);
|
||||
callback_result* res = callback_result_alloc();
|
||||
res->closure = &hldc->closedCb;
|
||||
res->args = 0;
|
||||
res->callback_name = "onClose";
|
||||
hl_mutex_release(callback_result_mutex);
|
||||
hl_semaphore_release(callback_result_semaphore);
|
||||
}
|
||||
|
||||
static void RTC_API errorCallback(int id, const char* error, void* ptr)
|
||||
{
|
||||
hl_rtc_datachannel* hldc = (hl_rtc_datachannel*)ptr;
|
||||
int errorlen = strlen(error);
|
||||
|
||||
hl_mutex_acquire(callback_result_mutex);
|
||||
callback_result* res = callback_result_alloc();
|
||||
res->arg1.t = &hlt_bytes;
|
||||
res->arg1.v.bytes = (vbyte*)malloc(errorlen + 1);
|
||||
memcpy(res->arg1.v.bytes, error, errorlen);
|
||||
res->arg1.v.bytes[errorlen] = '\0';
|
||||
res->arg1Size = errorlen + 1;
|
||||
res->closure = &hldc->errCb;
|
||||
res->args = 1;
|
||||
res->callback_name = "onError";
|
||||
hl_mutex_release(callback_result_mutex);
|
||||
hl_semaphore_release(callback_result_semaphore);
|
||||
}
|
||||
|
||||
static void RTC_API messageCallback(int id, const char* message, int size, void* ptr)
|
||||
{
|
||||
hl_rtc_datachannel* hldc = (hl_rtc_datachannel*)ptr;
|
||||
|
||||
if (size < 0) // negative size indicates a null-terminated string
|
||||
{
|
||||
int len = strlen(message);
|
||||
|
||||
hl_mutex_acquire(callback_result_mutex);
|
||||
callback_result* res = callback_result_alloc();
|
||||
res->arg1.t = &hlt_bytes;
|
||||
res->arg1.v.bytes = (vbyte*)malloc(len + 1);
|
||||
memcpy(res->arg1.v.bytes, message, len);
|
||||
res->arg1.v.bytes[len] = '\0';
|
||||
res->arg1Size = len + 1;
|
||||
res->arg2.t = &hlt_i32;
|
||||
res->arg2.v.i = len + 1;
|
||||
res->closure = &hldc->msgCb;
|
||||
res->args = 2;
|
||||
res->callback_name = "onMessage";
|
||||
hl_mutex_release(callback_result_mutex);
|
||||
hl_semaphore_release(callback_result_semaphore);
|
||||
}
|
||||
else
|
||||
{
|
||||
hl_mutex_acquire(callback_result_mutex);
|
||||
callback_result* res = callback_result_alloc();
|
||||
res->arg1.t = &hlt_bytes;
|
||||
res->arg1.v.bytes = (vbyte*)malloc(size);
|
||||
memcpy(res->arg1.v.bytes, message, size);
|
||||
res->arg1Size = size;
|
||||
res->arg2.t = &hlt_i32;
|
||||
res->arg2.v.i = size;
|
||||
res->closure = &hldc->msgCb;
|
||||
res->args = 2;
|
||||
res->callback_name = "onMessage";
|
||||
hl_mutex_release(callback_result_mutex);
|
||||
hl_semaphore_release(callback_result_semaphore);
|
||||
}
|
||||
}
|
||||
|
||||
static void RTC_API dataChannelCallback(int pc, int dc, void* ptr)
|
||||
{
|
||||
hl_rtc_peerconnection* hlpc = (hl_rtc_peerconnection*)ptr;
|
||||
|
||||
|
||||
hl_mutex_acquire(callback_result_mutex);
|
||||
datachannel_callback* res = datachannel_callback_alloc();
|
||||
res->dc = dc;
|
||||
res->pc = hlpc;
|
||||
hl_mutex_release(callback_result_mutex);
|
||||
}
|
||||
|
||||
static void RTC_API bufferedAmountLowCallback(int dc, void* ptr)
|
||||
{
|
||||
hl_rtc_datachannel* hldc = (hl_rtc_datachannel*)ptr;
|
||||
|
||||
hl_mutex_acquire(callback_result_mutex);
|
||||
callback_result* res = callback_result_alloc();
|
||||
res->closure = &hldc->bufferLowCb;
|
||||
res->args = 0;
|
||||
res->callback_name = "onBufferLow";
|
||||
hl_mutex_release(callback_result_mutex);
|
||||
hl_semaphore_release(callback_result_semaphore);
|
||||
}
|
||||
|
||||
DEFINE_PRIM(_VOID, initialize, _NO_ARG);
|
||||
DEFINE_PRIM(_VOID, finalize, _NO_ARG);
|
||||
DEFINE_PRIM(_VOID, process_events, _NO_ARG);
|
||||
DEFINE_PRIM(_TPC, create_peer_connection, _ARR _STRING _I32 _I32 _I32 _I32);
|
||||
DEFINE_PRIM(_VOID, close_peer_connection, _TPC);
|
||||
DEFINE_PRIM(_VOID, set_peer_connection_callbacks, _TPC _FUN(_VOID, _BYTES _BYTES) _FUN(_VOID, _BYTES) _FUN(_VOID, _I32) _FUN(_VOID, _I32));
|
||||
DEFINE_PRIM(_VOID, set_remote_description, _TPC _BYTES _STRING);
|
||||
DEFINE_PRIM(_VOID, add_remote_candidate, _TPC _BYTES);
|
||||
DEFINE_PRIM(_TDC, create_datachannel, _TPC _STRING);
|
||||
DEFINE_PRIM(_TDC, create_datachannel_ex, _TPC _STRING _BOOL _I32 _I32);
|
||||
DEFINE_PRIM(_VOID, set_peerconnection_datachannel_cb, _TPC _FUN(_VOID, _TDC _BYTES));
|
||||
DEFINE_PRIM(_VOID, set_datachannel_callbacks, _TDC _FUN(_VOID, _BYTES) _FUN(_VOID, _NO_ARG) _FUN(_VOID, _BYTES) _FUN(_VOID, _BYTES _I32) _FUN(_VOID, _NO_ARG));
|
||||
DEFINE_PRIM(_VOID, datachannel_send_message, _TDC _BYTES _I32);
|
||||
DEFINE_PRIM(_DYN, get_datachannel_reliability, _TDC);
|
||||
DEFINE_PRIM(_BYTES, get_local_address, _TPC);
|
||||
DEFINE_PRIM(_BYTES, get_remote_address, _TPC);
|
||||
DEFINE_PRIM(_BYTES, get_local_description, _TPC);
|
||||
DEFINE_PRIM(_I32, get_buffered_amount, _TDC);
|
||||
DEFINE_PRIM(_VOID, set_buffered_amount_low_threshold, _TDC _I32)
|
||||
763
lib/datachannel/cpp/src/ssl3.c
Normal file
763
lib/datachannel/cpp/src/ssl3.c
Normal file
@ -0,0 +1,763 @@
|
||||
#define HL_NAME(n) ssl_##n
|
||||
|
||||
#define _WINSOCKAPI_
|
||||
#include <hl.h>
|
||||
#ifdef HL_WIN
|
||||
#include <winsock2.h>
|
||||
#include <wincrypt.h>
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#include <strings.h>
|
||||
#include <errno.h>
|
||||
typedef int SOCKET;
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef HL_MAC
|
||||
#include <Security/Security.h>
|
||||
#endif
|
||||
|
||||
#define SOCKET_ERROR (-1)
|
||||
#define NRETRYS 20
|
||||
|
||||
#include "mbedtls/platform.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/md.h"
|
||||
#include "mbedtls/pk.h"
|
||||
#include "mbedtls/oid.h"
|
||||
#include "mbedtls/x509_crt.h"
|
||||
#include "mbedtls/ssl.h"
|
||||
|
||||
#ifdef HL_CONSOLE
|
||||
mbedtls_x509_crt *hl_init_cert_chain();
|
||||
#endif
|
||||
|
||||
#if defined(HL_WIN) || defined(HL_MAC) || defined(HL_IOS) || defined(HL_TVOS)
|
||||
# define MSG_NOSIGNAL 0
|
||||
#endif
|
||||
|
||||
// Duplicate from socket.c
|
||||
typedef struct _hl_socket {
|
||||
SOCKET sock;
|
||||
} hl_socket;
|
||||
|
||||
typedef struct _hl_ssl_cert hl_ssl_cert;
|
||||
struct _hl_ssl_cert {
|
||||
void(*finalize)(hl_ssl_cert *);
|
||||
mbedtls_x509_crt *c;
|
||||
};
|
||||
|
||||
typedef struct _hl_ssl_pkey hl_ssl_pkey;
|
||||
struct _hl_ssl_pkey {
|
||||
void(*finalize)(hl_ssl_pkey *);
|
||||
mbedtls_pk_context *k;
|
||||
};
|
||||
|
||||
#define _SOCK _ABSTRACT(hl_socket)
|
||||
#define TSSL _ABSTRACT(mbedtls_ssl_context)
|
||||
#define TCONF _ABSTRACT(mbedtls_ssl_config)
|
||||
#define TCERT _ABSTRACT(hl_ssl_cert)
|
||||
#define TPKEY _ABSTRACT(hl_ssl_pkey)
|
||||
|
||||
static bool ssl_init_done = false;
|
||||
static mbedtls_entropy_context entropy;
|
||||
static mbedtls_ctr_drbg_context ctr_drbg;
|
||||
|
||||
static bool is_ssl_blocking( int r ) {
|
||||
return r == MBEDTLS_ERR_SSL_WANT_READ || r == MBEDTLS_ERR_SSL_WANT_WRITE;
|
||||
}
|
||||
|
||||
static int ssl_block_error( int r ) {
|
||||
return is_ssl_blocking(r) ? -1 : -2;
|
||||
}
|
||||
|
||||
static void cert_finalize(hl_ssl_cert *c) {
|
||||
mbedtls_x509_crt_free(c->c);
|
||||
free(c->c);
|
||||
c->c = NULL;
|
||||
}
|
||||
|
||||
static void pkey_finalize(hl_ssl_pkey *k) {
|
||||
mbedtls_pk_free(k->k);
|
||||
free(k->k);
|
||||
k->k = NULL;
|
||||
}
|
||||
|
||||
static int ssl_error(int ret) {
|
||||
char buf[128];
|
||||
uchar buf16[128];
|
||||
mbedtls_strerror(ret, buf, sizeof(buf));
|
||||
hl_from_utf8(buf16, (int)strlen(buf), buf);
|
||||
hl_error("%s",buf16);
|
||||
return ret;
|
||||
}
|
||||
|
||||
HL_PRIM mbedtls_ssl_context *HL_NAME(ssl_new)(mbedtls_ssl_config *config) {
|
||||
int ret;
|
||||
mbedtls_ssl_context *ssl;
|
||||
ssl = (mbedtls_ssl_context *)hl_gc_alloc_noptr(sizeof(mbedtls_ssl_context));
|
||||
mbedtls_ssl_init(ssl);
|
||||
if ((ret = mbedtls_ssl_setup(ssl, config)) != 0) {
|
||||
mbedtls_ssl_free(ssl);
|
||||
ssl_error(ret);
|
||||
return NULL;
|
||||
}
|
||||
return ssl;
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(ssl_close)(mbedtls_ssl_context *ssl) {
|
||||
mbedtls_ssl_free(ssl);
|
||||
}
|
||||
|
||||
HL_PRIM int HL_NAME(ssl_handshake)(mbedtls_ssl_context *ssl) {
|
||||
int r;
|
||||
r = mbedtls_ssl_handshake(ssl);
|
||||
if( is_ssl_blocking(r) )
|
||||
return -1;
|
||||
if( r == MBEDTLS_ERR_SSL_CONN_EOF )
|
||||
return -2;
|
||||
if( r != 0 )
|
||||
return ssl_error(r);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static bool is_block_error() {
|
||||
#ifdef HL_WIN
|
||||
int err = WSAGetLastError();
|
||||
if (err == WSAEWOULDBLOCK || err == WSAEALREADY || err == WSAETIMEDOUT)
|
||||
#else
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS || errno == EALREADY)
|
||||
#endif
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
static int net_read(void *fd, unsigned char *buf, size_t len) {
|
||||
int r = recv((SOCKET)(int_val)fd, (char *)buf, (int)len, MSG_NOSIGNAL);
|
||||
if( r == SOCKET_ERROR && is_block_error() )
|
||||
return MBEDTLS_ERR_SSL_WANT_READ;
|
||||
return r;
|
||||
}
|
||||
|
||||
static int net_write(void *fd, const unsigned char *buf, size_t len) {
|
||||
int r = send((SOCKET)(int_val)fd, (char *)buf, (int)len, MSG_NOSIGNAL);
|
||||
if( r == SOCKET_ERROR && is_block_error() )
|
||||
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
||||
return r;
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(ssl_set_socket)(mbedtls_ssl_context *ssl, hl_socket *socket) {
|
||||
mbedtls_ssl_set_bio(ssl, (void*)(int_val)socket->sock, net_write, net_read, NULL);
|
||||
}
|
||||
|
||||
static int arr_read( void *arr, unsigned char *buf, size_t len ) {
|
||||
int r = ((int (*)(vdynamic*,unsigned char*,int))hl_aptr(arr,vclosure*)[1]->fun)( hl_aptr(arr,vdynamic*)[0], buf, (int)len );
|
||||
if( r == -2 ) return MBEDTLS_ERR_SSL_WANT_READ;
|
||||
return r;
|
||||
}
|
||||
|
||||
static int arr_write( void *arr, const unsigned char *buf, size_t len ) {
|
||||
int r = ((int (*)(vdynamic*,const unsigned char*,int))hl_aptr(arr,vclosure*)[2]->fun)( hl_aptr(arr,vdynamic*)[0], buf, (int)len );
|
||||
if( r == -2 ) return MBEDTLS_ERR_SSL_WANT_WRITE;
|
||||
return r;
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(ssl_set_bio)( mbedtls_ssl_context *ssl, varray *ctx ) {
|
||||
mbedtls_ssl_set_bio(ssl, ctx, arr_write, arr_read, NULL);
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(ssl_set_hostname)(mbedtls_ssl_context *ssl, vbyte *hostname) {
|
||||
int ret;
|
||||
if ((ret = mbedtls_ssl_set_hostname(ssl, (char*)hostname)) != 0)
|
||||
ssl_error(ret);
|
||||
}
|
||||
|
||||
HL_PRIM hl_ssl_cert *HL_NAME(ssl_get_peer_certificate)(mbedtls_ssl_context *ssl) {
|
||||
hl_ssl_cert *cert = (hl_ssl_cert*)hl_gc_alloc_noptr(sizeof(hl_ssl_cert));
|
||||
cert->c = (mbedtls_x509_crt*)mbedtls_ssl_get_peer_cert(ssl);
|
||||
return cert;
|
||||
}
|
||||
|
||||
DEFINE_PRIM(TSSL, ssl_new, TCONF);
|
||||
DEFINE_PRIM(_VOID, ssl_close, TSSL);
|
||||
DEFINE_PRIM(_I32, ssl_handshake, TSSL);
|
||||
DEFINE_PRIM(_VOID, ssl_set_bio, TSSL _DYN);
|
||||
DEFINE_PRIM(_VOID, ssl_set_socket, TSSL _SOCK);
|
||||
DEFINE_PRIM(_VOID, ssl_set_hostname, TSSL _BYTES);
|
||||
DEFINE_PRIM(TCERT, ssl_get_peer_certificate, TSSL);
|
||||
|
||||
HL_PRIM int HL_NAME(ssl_send_char)(mbedtls_ssl_context *ssl, int c) {
|
||||
unsigned char cc;
|
||||
int r;
|
||||
cc = (unsigned char)c;
|
||||
r = mbedtls_ssl_write(ssl, &cc, 1);
|
||||
if( r < 0 )
|
||||
return ssl_block_error(r);
|
||||
return 1;
|
||||
}
|
||||
|
||||
HL_PRIM int HL_NAME(ssl_send)(mbedtls_ssl_context *ssl, vbyte *buf, int pos, int len) {
|
||||
int r = mbedtls_ssl_write(ssl, (const unsigned char *)buf + pos, len);
|
||||
if( r < 0 )
|
||||
return ssl_block_error(r);
|
||||
return r;
|
||||
}
|
||||
|
||||
HL_PRIM int HL_NAME(ssl_recv_char)(mbedtls_ssl_context *ssl) {
|
||||
unsigned char c;
|
||||
int ret = mbedtls_ssl_read(ssl, &c, 1);
|
||||
if( ret != 1 )
|
||||
return ssl_block_error(ret);
|
||||
return c;
|
||||
}
|
||||
|
||||
HL_PRIM int HL_NAME(ssl_recv)(mbedtls_ssl_context *ssl, vbyte *buf, int pos, int len) {
|
||||
int ret = mbedtls_ssl_read(ssl, (unsigned char*)buf+pos, len);
|
||||
if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
|
||||
return 0;
|
||||
if( ret < 0 )
|
||||
return ssl_block_error(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
DEFINE_PRIM(_I32, ssl_send_char, TSSL _I32);
|
||||
DEFINE_PRIM(_I32, ssl_send, TSSL _BYTES _I32 _I32);
|
||||
DEFINE_PRIM(_I32, ssl_recv_char, TSSL);
|
||||
DEFINE_PRIM(_I32, ssl_recv, TSSL _BYTES _I32 _I32);
|
||||
|
||||
HL_PRIM mbedtls_ssl_config *HL_NAME(conf_new)(bool server) {
|
||||
int ret;
|
||||
mbedtls_ssl_config *conf;
|
||||
conf = (mbedtls_ssl_config *)hl_gc_alloc_noptr(sizeof(mbedtls_ssl_config));
|
||||
mbedtls_ssl_config_init(conf);
|
||||
if ((ret = mbedtls_ssl_config_defaults(conf, server ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
|
||||
MBEDTLS_SSL_TRANSPORT_STREAM, 0)) != 0) {
|
||||
mbedtls_ssl_config_free(conf);
|
||||
ssl_error(ret);
|
||||
return NULL;
|
||||
}
|
||||
mbedtls_ssl_conf_rng(conf, mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||
return conf;
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(conf_close)(mbedtls_ssl_config *conf) {
|
||||
mbedtls_ssl_config_free(conf);
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(conf_set_ca)(mbedtls_ssl_config *conf, hl_ssl_cert *cert) {
|
||||
mbedtls_ssl_conf_ca_chain(conf, cert->c, NULL);
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(conf_set_verify)(mbedtls_ssl_config *conf, int mode) {
|
||||
if (mode == 2)
|
||||
mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
|
||||
else if (mode == 1)
|
||||
mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_REQUIRED);
|
||||
else
|
||||
mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE);
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(conf_set_cert)(mbedtls_ssl_config *conf, hl_ssl_cert *cert, hl_ssl_pkey *key) {
|
||||
int r;
|
||||
if ((r = mbedtls_ssl_conf_own_cert(conf, cert->c, key->k)) != 0)
|
||||
ssl_error(r);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
hl_type *t;
|
||||
hl_ssl_cert *cert;
|
||||
hl_ssl_pkey *key;
|
||||
} sni_callb_ret;
|
||||
|
||||
static int sni_callback(void *arg, mbedtls_ssl_context *ctx, const unsigned char *name, size_t len) {
|
||||
if (name && arg) {
|
||||
vclosure *c = (vclosure*)arg;
|
||||
sni_callb_ret *ret;
|
||||
if( c->hasValue )
|
||||
ret = ((sni_callb_ret*(*)(void*, vbyte*))c->fun)(c->value, (vbyte*)name);
|
||||
else
|
||||
ret = ((sni_callb_ret*(*)(vbyte*))c->fun)((vbyte*)name);
|
||||
if (ret && ret->cert && ret->key) {
|
||||
return mbedtls_ssl_set_hs_own_cert(ctx, ret->cert->c, ret->key->k);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
HL_PRIM void HL_NAME(conf_set_servername_callback)(mbedtls_ssl_config *conf, vclosure *cb) {
|
||||
mbedtls_ssl_conf_sni(conf, sni_callback, (void *)cb);
|
||||
}
|
||||
|
||||
|
||||
DEFINE_PRIM(TCONF, conf_new, _BOOL);
|
||||
DEFINE_PRIM(_VOID, conf_close, TCONF);
|
||||
DEFINE_PRIM(_VOID, conf_set_ca, TCONF TCERT);
|
||||
DEFINE_PRIM(_VOID, conf_set_verify, TCONF _I32);
|
||||
DEFINE_PRIM(_VOID, conf_set_cert, TCONF TCERT TPKEY);
|
||||
DEFINE_PRIM(_VOID, conf_set_servername_callback, TCONF _FUN(_OBJ(TCERT TPKEY), _BYTES));
|
||||
|
||||
HL_PRIM hl_ssl_cert *HL_NAME(cert_load_file)(vbyte *file) {
|
||||
#ifdef HL_CONSOLE
|
||||
return NULL;
|
||||
#else
|
||||
int r;
|
||||
hl_ssl_cert *cert;
|
||||
mbedtls_x509_crt *x = (mbedtls_x509_crt*)malloc(sizeof(mbedtls_x509_crt));
|
||||
mbedtls_x509_crt_init(x);
|
||||
if ((r = mbedtls_x509_crt_parse_file(x, (char*)file)) != 0) {
|
||||
mbedtls_x509_crt_free(x);
|
||||
free(x);
|
||||
ssl_error(r);
|
||||
return NULL;
|
||||
}
|
||||
cert = (hl_ssl_cert*)hl_gc_alloc_finalizer(sizeof(hl_ssl_cert));
|
||||
cert->c = x;
|
||||
cert->finalize = cert_finalize;
|
||||
return cert;
|
||||
#endif
|
||||
}
|
||||
|
||||
HL_PRIM hl_ssl_cert *HL_NAME(cert_load_path)(vbyte *path) {
|
||||
#ifdef HL_CONSOLE
|
||||
return NULL;
|
||||
#else
|
||||
int r;
|
||||
hl_ssl_cert *cert;
|
||||
mbedtls_x509_crt *x = (mbedtls_x509_crt*)malloc(sizeof(mbedtls_x509_crt));
|
||||
mbedtls_x509_crt_init(x);
|
||||
if ((r = mbedtls_x509_crt_parse_path(x, (char*)path)) != 0) {
|
||||
mbedtls_x509_crt_free(x);
|
||||
free(x);
|
||||
ssl_error(r);
|
||||
return NULL;
|
||||
}
|
||||
cert = (hl_ssl_cert*)hl_gc_alloc_finalizer(sizeof(hl_ssl_cert));
|
||||
cert->c = x;
|
||||
cert->finalize = cert_finalize;
|
||||
return cert;
|
||||
#endif
|
||||
}
|
||||
|
||||
HL_PRIM hl_ssl_cert *HL_NAME(cert_load_defaults)() {
|
||||
hl_ssl_cert *v = NULL;
|
||||
mbedtls_x509_crt *chain = NULL;
|
||||
#if defined(HL_WIN)
|
||||
HCERTSTORE store;
|
||||
PCCERT_CONTEXT cert;
|
||||
|
||||
if (store = CertOpenSystemStore(0, (LPCWSTR)L"Root")) {
|
||||
cert = NULL;
|
||||
while (cert = CertEnumCertificatesInStore(store, cert)) {
|
||||
if (chain == NULL) {
|
||||
chain = (mbedtls_x509_crt*)malloc(sizeof(mbedtls_x509_crt));
|
||||
mbedtls_x509_crt_init(chain);
|
||||
}
|
||||
mbedtls_x509_crt_parse_der(chain, (unsigned char *)cert->pbCertEncoded, cert->cbCertEncoded);
|
||||
}
|
||||
CertCloseStore(store, 0);
|
||||
}
|
||||
#elif defined(HL_MAC)
|
||||
CFMutableDictionaryRef search;
|
||||
CFArrayRef result;
|
||||
SecKeychainRef keychain;
|
||||
SecCertificateRef item;
|
||||
CFDataRef dat;
|
||||
// Load keychain
|
||||
if (SecKeychainOpen("/System/Library/Keychains/SystemRootCertificates.keychain", &keychain) != errSecSuccess)
|
||||
return NULL;
|
||||
|
||||
// Search for certificates
|
||||
search = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
|
||||
CFDictionarySetValue(search, kSecClass, kSecClassCertificate);
|
||||
CFDictionarySetValue(search, kSecMatchLimit, kSecMatchLimitAll);
|
||||
CFDictionarySetValue(search, kSecReturnRef, kCFBooleanTrue);
|
||||
CFDictionarySetValue(search, kSecMatchSearchList, CFArrayCreate(NULL, (const void **)&keychain, 1, NULL));
|
||||
if (SecItemCopyMatching(search, (CFTypeRef *)&result) == errSecSuccess) {
|
||||
CFIndex n = CFArrayGetCount(result);
|
||||
for (CFIndex i = 0; i < n; i++) {
|
||||
item = (SecCertificateRef)CFArrayGetValueAtIndex(result, i);
|
||||
|
||||
// Get certificate in DER format
|
||||
dat = SecCertificateCopyData(item);
|
||||
if (dat) {
|
||||
if (chain == NULL) {
|
||||
chain = (mbedtls_x509_crt*)malloc(sizeof(mbedtls_x509_crt));
|
||||
mbedtls_x509_crt_init(chain);
|
||||
}
|
||||
mbedtls_x509_crt_parse_der(chain, (unsigned char *)CFDataGetBytePtr(dat), CFDataGetLength(dat));
|
||||
CFRelease(dat);
|
||||
}
|
||||
}
|
||||
}
|
||||
CFRelease(keychain);
|
||||
#elif defined(HL_CONSOLE)
|
||||
chain = hl_init_cert_chain();
|
||||
#endif
|
||||
if (chain != NULL) {
|
||||
v = (hl_ssl_cert*)hl_gc_alloc_finalizer(sizeof(hl_ssl_cert));
|
||||
v->c = chain;
|
||||
v->finalize = cert_finalize;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static vbyte *asn1_buf_to_string(mbedtls_asn1_buf *dat) {
|
||||
unsigned int i, c;
|
||||
hl_buffer *buf = hl_alloc_buffer();
|
||||
for (i = 0; i < dat->len; i++) {
|
||||
c = dat->p[i];
|
||||
if (c < 32 || c == 127 || (c > 128 && c < 160))
|
||||
hl_buffer_char(buf, '?');
|
||||
else
|
||||
hl_buffer_char(buf, c);
|
||||
}
|
||||
return (vbyte*)hl_buffer_content(buf,NULL);
|
||||
}
|
||||
|
||||
HL_PRIM vbyte *HL_NAME(cert_get_subject)(hl_ssl_cert *cert, vbyte *objname) {
|
||||
mbedtls_x509_name *obj;
|
||||
int r;
|
||||
const char *oname, *rname;
|
||||
obj = &cert->c->subject;
|
||||
if (obj == NULL)
|
||||
hl_error("Invalid subject");
|
||||
rname = (char*)objname;
|
||||
while (obj != NULL) {
|
||||
r = mbedtls_oid_get_attr_short_name(&obj->oid, &oname);
|
||||
if (r == 0 && strcmp(oname, rname) == 0)
|
||||
return asn1_buf_to_string(&obj->val);
|
||||
obj = obj->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
HL_PRIM vbyte *HL_NAME(cert_get_issuer)(hl_ssl_cert *cert, vbyte *objname) {
|
||||
mbedtls_x509_name *obj;
|
||||
int r;
|
||||
const char *oname, *rname;
|
||||
obj = &cert->c->issuer;
|
||||
if (obj == NULL)
|
||||
hl_error("Invalid issuer");
|
||||
rname = (char*)objname;
|
||||
while (obj != NULL) {
|
||||
r = mbedtls_oid_get_attr_short_name(&obj->oid, &oname);
|
||||
if (r == 0 && strcmp(oname, rname) == 0)
|
||||
return asn1_buf_to_string(&obj->val);
|
||||
obj = obj->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
HL_PRIM varray *HL_NAME(cert_get_altnames)(hl_ssl_cert *cert) {
|
||||
mbedtls_asn1_sequence *cur;
|
||||
int count = 0;
|
||||
int pos = 0;
|
||||
varray *a = NULL;
|
||||
vbyte **current = NULL;
|
||||
mbedtls_x509_crt *crt = cert->c;
|
||||
if (mbedtls_x509_crt_has_ext_type(crt, MBEDTLS_X509_EXT_SUBJECT_ALT_NAME)) {
|
||||
cur = &crt->subject_alt_names;
|
||||
while (cur != NULL) {
|
||||
if (pos == count) {
|
||||
int ncount = count == 0 ? 16 : count * 2;
|
||||
varray *narr = hl_alloc_array(&hlt_bytes, ncount);
|
||||
vbyte **ncur = hl_aptr(narr, vbyte*);
|
||||
memcpy(ncur, current, count * sizeof(void*));
|
||||
current = ncur;
|
||||
a = narr;
|
||||
count = ncount;
|
||||
}
|
||||
current[pos++] = asn1_buf_to_string(&cur->buf);
|
||||
cur = cur->next;
|
||||
}
|
||||
}
|
||||
if (a == NULL) a = hl_alloc_array(&hlt_bytes, 0);
|
||||
a->size = pos;
|
||||
return a;
|
||||
}
|
||||
|
||||
static varray *x509_time_to_array(mbedtls_x509_time *t) {
|
||||
varray *a = NULL;
|
||||
int *p;
|
||||
if (!t)
|
||||
hl_error("Invalid x509 time");
|
||||
a = hl_alloc_array(&hlt_i32, 6);
|
||||
p = hl_aptr(a, int);
|
||||
p[0] = t->year;
|
||||
p[1] = t->mon;
|
||||
p[2] = t->day;
|
||||
p[3] = t->hour;
|
||||
p[4] = t->min;
|
||||
p[5] = t->sec;
|
||||
return a;
|
||||
}
|
||||
|
||||
HL_PRIM varray *HL_NAME(cert_get_not_before)(hl_ssl_cert *cert) {
|
||||
return x509_time_to_array(&cert->c->valid_from);
|
||||
}
|
||||
|
||||
HL_PRIM varray *HL_NAME(cert_get_notafter)(hl_ssl_cert *cert) {
|
||||
return x509_time_to_array(&cert->c->valid_to);
|
||||
}
|
||||
|
||||
HL_PRIM hl_ssl_cert *HL_NAME(cert_get_next)(hl_ssl_cert *cert) {
|
||||
hl_ssl_cert *ncert;
|
||||
if (cert->c->next == NULL)
|
||||
return NULL;
|
||||
ncert = (hl_ssl_cert*)hl_gc_alloc_noptr(sizeof(hl_ssl_cert));
|
||||
ncert->c = cert->c->next;
|
||||
return ncert;
|
||||
}
|
||||
|
||||
HL_PRIM hl_ssl_cert *HL_NAME(cert_add_pem)(hl_ssl_cert *cert, vbyte *data) {
|
||||
mbedtls_x509_crt *crt;
|
||||
int r, len;
|
||||
unsigned char *buf;
|
||||
if (cert != NULL)
|
||||
crt = cert->c;
|
||||
else{
|
||||
crt = (mbedtls_x509_crt*)malloc(sizeof(mbedtls_x509_crt));
|
||||
mbedtls_x509_crt_init(crt);
|
||||
}
|
||||
len = (int)strlen((char*)data) + 1;
|
||||
buf = (unsigned char *)malloc(len);
|
||||
memcpy(buf, (char*)data, len - 1);
|
||||
buf[len - 1] = '\0';
|
||||
r = mbedtls_x509_crt_parse(crt, buf, len);
|
||||
free(buf);
|
||||
if (r < 0) {
|
||||
if (cert == NULL) {
|
||||
mbedtls_x509_crt_free(crt);
|
||||
free(crt);
|
||||
}
|
||||
ssl_error(r);
|
||||
return NULL;
|
||||
}
|
||||
if (cert == NULL) {
|
||||
cert = (hl_ssl_cert*)hl_gc_alloc_finalizer(sizeof(hl_ssl_cert));
|
||||
cert->c = crt;
|
||||
cert->finalize = cert_finalize;
|
||||
}
|
||||
return cert;
|
||||
}
|
||||
|
||||
HL_PRIM hl_ssl_cert *HL_NAME(cert_add_der)(hl_ssl_cert *cert, vbyte *data, int len) {
|
||||
mbedtls_x509_crt *crt;
|
||||
int r;
|
||||
if (cert != NULL)
|
||||
crt = cert->c;
|
||||
else {
|
||||
crt = (mbedtls_x509_crt*)malloc(sizeof(mbedtls_x509_crt));
|
||||
mbedtls_x509_crt_init(crt);
|
||||
}
|
||||
if ((r = mbedtls_x509_crt_parse_der(crt, (const unsigned char*)data, len)) < 0) {
|
||||
if (cert == NULL) {
|
||||
mbedtls_x509_crt_free(crt);
|
||||
free(crt);
|
||||
}
|
||||
ssl_error(r);
|
||||
return NULL;
|
||||
}
|
||||
if (cert == NULL) {
|
||||
cert = (hl_ssl_cert*)hl_gc_alloc_finalizer(sizeof(hl_ssl_cert));
|
||||
cert->c = crt;
|
||||
cert->finalize = cert_finalize;
|
||||
}
|
||||
return cert;
|
||||
}
|
||||
|
||||
|
||||
DEFINE_PRIM(TCERT, cert_load_defaults, _NO_ARG);
|
||||
DEFINE_PRIM(TCERT, cert_load_file, _BYTES);
|
||||
DEFINE_PRIM(TCERT, cert_load_path, _BYTES);
|
||||
DEFINE_PRIM(_BYTES, cert_get_subject, TCERT _BYTES);
|
||||
DEFINE_PRIM(_BYTES, cert_get_issuer, TCERT _BYTES);
|
||||
DEFINE_PRIM(_ARR, cert_get_altnames, TCERT);
|
||||
DEFINE_PRIM(_ARR, cert_get_not_before, TCERT);
|
||||
DEFINE_PRIM(_ARR, cert_get_notafter, TCERT);
|
||||
DEFINE_PRIM(TCERT, cert_get_next, TCERT);
|
||||
DEFINE_PRIM(TCERT, cert_add_pem, TCERT _BYTES);
|
||||
DEFINE_PRIM(TCERT, cert_add_der, TCERT _BYTES _I32);
|
||||
|
||||
|
||||
HL_PRIM hl_ssl_pkey *HL_NAME(key_from_der)(vbyte *data, int len, bool pub) {
|
||||
int r;
|
||||
hl_ssl_pkey *key;
|
||||
mbedtls_pk_context *pk = (mbedtls_pk_context *)malloc(sizeof(mbedtls_pk_context));
|
||||
mbedtls_pk_init(pk);
|
||||
if (pub)
|
||||
r = mbedtls_pk_parse_public_key(pk, (const unsigned char*)data, len);
|
||||
else
|
||||
r = mbedtls_pk_parse_key(pk, (const unsigned char*)data, len, NULL, 0, mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||
if (r != 0) {
|
||||
mbedtls_pk_free(pk);
|
||||
free(pk);
|
||||
ssl_error(r);
|
||||
return NULL;
|
||||
}
|
||||
key = (hl_ssl_pkey*)hl_gc_alloc_finalizer(sizeof(hl_ssl_pkey));
|
||||
key->k = pk;
|
||||
key->finalize = pkey_finalize;
|
||||
return key;
|
||||
}
|
||||
|
||||
HL_PRIM hl_ssl_pkey *HL_NAME(key_from_pem)(vbyte *data, bool pub, vbyte *pass) {
|
||||
int r, len;
|
||||
hl_ssl_pkey *key;
|
||||
unsigned char *buf;
|
||||
mbedtls_pk_context *pk = (mbedtls_pk_context *)malloc(sizeof(mbedtls_pk_context));
|
||||
mbedtls_pk_init(pk);
|
||||
len = (int)strlen((char*)data) + 1;
|
||||
buf = (unsigned char *)malloc(len);
|
||||
memcpy(buf, (char*)data, len - 1);
|
||||
buf[len - 1] = '\0';
|
||||
if (pub)
|
||||
r = mbedtls_pk_parse_public_key(pk, buf, len);
|
||||
else if (pass == NULL)
|
||||
r = mbedtls_pk_parse_key(pk, buf, len, NULL, 0, mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||
else
|
||||
r = mbedtls_pk_parse_key(pk, buf, len, (const unsigned char*)pass, strlen((char*)pass), mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||
free(buf);
|
||||
if (r != 0) {
|
||||
mbedtls_pk_free(pk);
|
||||
free(pk);
|
||||
ssl_error(r);
|
||||
return NULL;
|
||||
}
|
||||
key = (hl_ssl_pkey*)hl_gc_alloc_finalizer(sizeof(hl_ssl_pkey));
|
||||
key->k = pk;
|
||||
key->finalize = pkey_finalize;
|
||||
return key;
|
||||
}
|
||||
|
||||
DEFINE_PRIM(TPKEY, key_from_der, _BYTES _I32 _BOOL);
|
||||
DEFINE_PRIM(TPKEY, key_from_pem, _BYTES _BOOL _BYTES);
|
||||
|
||||
HL_PRIM vbyte *HL_NAME(dgst_make)(vbyte *data, int len, vbyte *alg, int *size) {
|
||||
const mbedtls_md_info_t *md;
|
||||
int mdlen, r = -1;
|
||||
vbyte *out;
|
||||
|
||||
md = mbedtls_md_info_from_string((char*)alg);
|
||||
if (md == NULL) {
|
||||
hl_error("Invalid hash algorithm");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mdlen = mbedtls_md_get_size(md);
|
||||
*size = mdlen;
|
||||
out = hl_gc_alloc_noptr(mdlen);
|
||||
if ((r = mbedtls_md(md, (const unsigned char *)data, len, out)) != 0){
|
||||
ssl_error(r);
|
||||
return NULL;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
HL_PRIM vbyte *HL_NAME(dgst_sign)(vbyte *data, int len, hl_ssl_pkey *key, vbyte *alg, int *size) {
|
||||
const mbedtls_md_info_t *md;
|
||||
int r = -1;
|
||||
vbyte *out;
|
||||
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
||||
size_t ssize = size ? *size : 0;
|
||||
|
||||
md = mbedtls_md_info_from_string((char*)alg);
|
||||
if (md == NULL) {
|
||||
hl_error("Invalid hash algorithm");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((r = mbedtls_md(md, (unsigned char *)data, len, hash)) != 0){
|
||||
ssl_error(r);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out = hl_gc_alloc_noptr(MBEDTLS_MPI_MAX_SIZE);
|
||||
if ((r = mbedtls_pk_sign(key->k, mbedtls_md_get_type(md), hash, 0, out, MBEDTLS_MPI_MAX_SIZE, (size ? &ssize : NULL), mbedtls_ctr_drbg_random, &ctr_drbg)) != 0){
|
||||
ssl_error(r);
|
||||
return NULL;
|
||||
}
|
||||
if( size ) *size = (int)ssize;
|
||||
return out;
|
||||
}
|
||||
|
||||
HL_PRIM bool HL_NAME(dgst_verify)(vbyte *data, int dlen, vbyte *sign, int slen, hl_ssl_pkey *key, vbyte *alg) {
|
||||
const mbedtls_md_info_t *md;
|
||||
int r = -1;
|
||||
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
||||
|
||||
md = mbedtls_md_info_from_string((char*)alg);
|
||||
if (md == NULL) {
|
||||
hl_error("Invalid hash algorithm");
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((r = mbedtls_md(md, (const unsigned char *)data, dlen, hash)) != 0)
|
||||
return ssl_error(r);
|
||||
|
||||
if ((r = mbedtls_pk_verify(key->k, mbedtls_md_get_type(md), hash, 0, (unsigned char *)sign, slen)) != 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
DEFINE_PRIM(_BYTES, dgst_make, _BYTES _I32 _BYTES _REF(_I32));
|
||||
DEFINE_PRIM(_BYTES, dgst_sign, _BYTES _I32 TPKEY _BYTES _REF(_I32));
|
||||
DEFINE_PRIM(_BOOL, dgst_verify, _BYTES _I32 _BYTES _I32 TPKEY _BYTES);
|
||||
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C) && defined(_MSC_VER)
|
||||
|
||||
static void threading_mutex_init_alt(mbedtls_threading_mutex_t *mutex) {
|
||||
if (mutex == NULL)
|
||||
return;
|
||||
InitializeCriticalSection(&mutex->cs);
|
||||
mutex->is_valid = 1;
|
||||
}
|
||||
|
||||
static void threading_mutex_free_alt(mbedtls_threading_mutex_t *mutex) {
|
||||
if (mutex == NULL || !mutex->is_valid)
|
||||
return;
|
||||
DeleteCriticalSection(&mutex->cs);
|
||||
mutex->is_valid = 0;
|
||||
}
|
||||
|
||||
static int threading_mutex_lock_alt(mbedtls_threading_mutex_t *mutex) {
|
||||
if (mutex == NULL || !mutex->is_valid)
|
||||
return(MBEDTLS_ERR_THREADING_BAD_INPUT_DATA);
|
||||
|
||||
EnterCriticalSection(&mutex->cs);
|
||||
return(0);
|
||||
}
|
||||
|
||||
static int threading_mutex_unlock_alt(mbedtls_threading_mutex_t *mutex) {
|
||||
if (mutex == NULL || !mutex->is_valid)
|
||||
return(MBEDTLS_ERR_THREADING_BAD_INPUT_DATA);
|
||||
|
||||
LeaveCriticalSection(&mutex->cs);
|
||||
return(0);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
HL_PRIM void HL_NAME(ssl_init)() {
|
||||
if (ssl_init_done)
|
||||
return;
|
||||
ssl_init_done = true;
|
||||
#if defined(MBEDTLS_THREADING_C) && defined(_MSC_VER)
|
||||
mbedtls_threading_set_alt(threading_mutex_init_alt, threading_mutex_free_alt,
|
||||
threading_mutex_lock_alt, threading_mutex_unlock_alt);
|
||||
#endif
|
||||
|
||||
// Init RNG
|
||||
mbedtls_entropy_init(&entropy);
|
||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
||||
mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0);
|
||||
}
|
||||
|
||||
DEFINE_PRIM(_VOID, ssl_init, _NO_ARG);
|
||||
Reference in New Issue
Block a user