git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3668 ed5197a5-7fde-0310-b194-c3ffbd925b24
109 lines
3.7 KiB
C
109 lines
3.7 KiB
C
#include "tlscontextwrapper.h"
|
|
|
|
#include "task.h"
|
|
#include "tls.h"
|
|
|
|
#include <assert.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
static JSClassID _classId;
|
|
static int _count;
|
|
|
|
typedef struct _tf_tls_context_wrapper_t {
|
|
tf_tls_context_t* context;
|
|
tf_task_t* task;
|
|
JSValue object;
|
|
} tf_tls_context_wrapper_t;
|
|
|
|
static JSValue _tls_context_wrapper_set_certificate(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
static JSValue _tls_context_wrapper_set_private_key(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
static JSValue _tls_context_wrapper_add_trusted_certificate(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _tls_context_wrapper_create(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
static void _tls_context_wrapper_finalizer(JSRuntime *runtime, JSValue value);
|
|
|
|
JSValue _tls_context_wrapper_set_certificate(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
{
|
|
tf_tls_context_wrapper_t* wrapper = JS_GetOpaque(this_val, _classId);
|
|
const char* value = JS_ToCString(context, argv[0]);
|
|
tf_tls_context_set_certificate(wrapper->context, value);
|
|
JS_FreeCString(context, value);
|
|
return JS_UNDEFINED;
|
|
}
|
|
|
|
JSValue _tls_context_wrapper_set_private_key(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
{
|
|
tf_tls_context_wrapper_t* wrapper = JS_GetOpaque(this_val, _classId);
|
|
const char* value = JS_ToCString(context, argv[0]);
|
|
tf_tls_context_set_private_key(wrapper->context, value);
|
|
JS_FreeCString(context, value);
|
|
return JS_UNDEFINED;
|
|
}
|
|
|
|
JSValue _tls_context_wrapper_add_trusted_certificate(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
{
|
|
tf_tls_context_wrapper_t* wrapper = JS_GetOpaque(this_val, _classId);
|
|
const char* value = JS_ToCString(context, argv[0]);
|
|
tf_tls_context_add_trusted_certificate(wrapper->context, value);
|
|
JS_FreeCString(context, value);
|
|
return JS_UNDEFINED;
|
|
}
|
|
|
|
JSValue tf_tls_context_wrapper_init(JSContext* context)
|
|
{
|
|
JS_NewClassID(&_classId);
|
|
JSClassDef def =
|
|
{
|
|
.class_name = "TlsContext",
|
|
.finalizer = _tls_context_wrapper_finalizer,
|
|
};
|
|
if (JS_NewClass(JS_GetRuntime(context), _classId, &def) != 0)
|
|
{
|
|
fprintf(stderr, "Failed to register TlsContext.\n");
|
|
}
|
|
return JS_NewCFunction2(context, _tls_context_wrapper_create, "TlsContext", 0, JS_CFUNC_constructor, 0);
|
|
}
|
|
|
|
tf_tls_context_t* tf_tls_context_wrapper_get(JSValue value)
|
|
{
|
|
tf_tls_context_wrapper_t* wrapper = JS_GetOpaque(value, _classId);
|
|
return wrapper ? wrapper->context : NULL;
|
|
}
|
|
|
|
int tf_tls_context_wrapper_get_count()
|
|
{
|
|
return _count;
|
|
}
|
|
|
|
JSValue _tls_context_wrapper_create(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
{
|
|
tf_tls_context_wrapper_t* wrapper = malloc(sizeof(tf_tls_context_wrapper_t));
|
|
memset(wrapper, 0, sizeof(*wrapper));
|
|
|
|
++_count;
|
|
wrapper->object = JS_NewObjectClass(context, _classId);
|
|
JS_SetOpaque(wrapper->object, wrapper);
|
|
|
|
JS_SetPropertyStr(context, wrapper->object, "setCertificate", JS_NewCFunction(context, _tls_context_wrapper_set_certificate, "setCertificate", 1));
|
|
JS_SetPropertyStr(context, wrapper->object, "setPrivateKey", JS_NewCFunction(context, _tls_context_wrapper_set_private_key, "setPrivateKey", 1));
|
|
JS_SetPropertyStr(context, wrapper->object, "addTrustedCertificate", JS_NewCFunction(context, _tls_context_wrapper_add_trusted_certificate, "addTrustedCertificate", 1));
|
|
|
|
wrapper->context = tf_tls_context_create();
|
|
wrapper->task = tf_task_get(context);
|
|
|
|
return wrapper->object;
|
|
}
|
|
|
|
void _tls_context_wrapper_finalizer(JSRuntime *runtime, JSValue value)
|
|
{
|
|
tf_tls_context_wrapper_t* wrapper = JS_GetOpaque(value, _classId);
|
|
if (wrapper->context)
|
|
{
|
|
tf_tls_context_destroy(wrapper->context);
|
|
wrapper->context = NULL;
|
|
}
|
|
--_count;
|
|
free(wrapper);
|
|
}
|