odyssey/src/mm_tls.c

126 lines
2.3 KiB
C
Raw Normal View History

2017-03-24 09:27:49 +00:00
/*
* machinarium.
*
* cooperative multitasking engine.
*/
#include <machinarium_private.h>
#include <machinarium.h>
MACHINE_API machine_tls_t
machine_create_tls(machine_t machine)
{
mm_tls_t *tls;
tls = malloc(sizeof(*tls));
if (tls == NULL)
return NULL;
tls->verify = MM_TLS_NONE;
2017-03-24 13:31:24 +00:00
tls->protocols = NULL;
tls->ca_path = NULL;
tls->ca_file = NULL;
tls->cert_file = NULL;
tls->key_file = NULL;
2017-03-24 13:19:48 +00:00
(void)machine;
2017-03-24 09:27:49 +00:00
return tls;
}
MACHINE_API void
machine_free_tls(machine_tls_t obj)
{
mm_tls_t *tls = obj;
2017-03-24 13:31:24 +00:00
if (tls->protocols)
free(tls->protocols);
if (tls->ca_path)
free(tls->ca_path);
if (tls->ca_file)
free(tls->ca_file);
if (tls->cert_file)
free(tls->cert_file);
if (tls->key_file)
free(tls->key_file);
2017-03-24 09:27:49 +00:00
free(tls);
}
2017-03-24 09:55:18 +00:00
MACHINE_API int
machine_tls_set_mode(machine_tls_t obj, char *mode)
{
mm_tls_t *tls = obj;
if (strcasecmp(mode, "none") == 0)
tls->verify = MM_TLS_NONE;
2017-03-24 09:55:18 +00:00
else
if (strcasecmp(mode, "peer") == 0)
tls->verify = MM_TLS_PEER;
2017-03-24 09:55:18 +00:00
else
if (strcasecmp(mode, "peer_strict") == 0)
tls->verify = MM_TLS_PEER_STRICT;
2017-03-24 09:55:18 +00:00
else
return -1;
return 0;
}
2017-03-24 09:27:49 +00:00
MACHINE_API int
machine_tls_set_protocols(machine_tls_t obj, char *protocols)
{
mm_tls_t *tls = obj;
2017-03-24 13:31:24 +00:00
char *string = strdup(protocols);
if (string == NULL)
return -1;
if (tls->protocols)
free(tls->protocols);
tls->protocols = string;
2017-03-24 09:27:49 +00:00
return 0;
}
MACHINE_API int
machine_tls_set_ca_path(machine_tls_t obj, char *path)
{
mm_tls_t *tls = obj;
2017-03-24 13:31:24 +00:00
char *string = strdup(path);
if (string == NULL)
return -1;
if (tls->ca_path)
free(tls->ca_path);
tls->ca_path = string;
2017-03-24 13:19:48 +00:00
return 0;
2017-03-24 09:27:49 +00:00
}
MACHINE_API int
machine_tls_set_ca_file(machine_tls_t obj, char *path)
{
mm_tls_t *tls = obj;
2017-03-24 13:31:24 +00:00
char *string = strdup(path);
if (string == NULL)
return -1;
if (tls->ca_file)
free(tls->ca_file);
tls->ca_file = string;
2017-03-24 13:19:48 +00:00
return 0;
2017-03-24 09:27:49 +00:00
}
MACHINE_API int
machine_tls_set_cert_file(machine_tls_t obj, char *path)
{
mm_tls_t *tls = obj;
2017-03-24 13:31:24 +00:00
char *string = strdup(path);
if (string == NULL)
return -1;
if (tls->cert_file)
free(tls->cert_file);
tls->cert_file = string;
2017-03-24 13:19:48 +00:00
return 0;
2017-03-24 09:27:49 +00:00
}
MACHINE_API int
machine_tls_set_key_file(machine_tls_t obj, char *path)
{
mm_tls_t *tls = obj;
2017-03-24 13:31:24 +00:00
char *string = strdup(path);
if (string == NULL)
return -1;
if (tls->key_file)
free(tls->key_file);
tls->key_file = string;
2017-03-24 13:19:48 +00:00
return 0;
2017-03-24 09:27:49 +00:00
}