aisl/src/ssl.c

71 lines
1.1 KiB
C
Raw Normal View History

2019-03-08 23:19:54 +01:00
/*
* ssl.c
* Copyright (C) 2019 Ilja Kartašov <ik@lowenware.com>
*
* Distributed under terms of the MIT license.
*/
#include <openssl/err.h>
#include "ssl.h"
2019-03-13 16:47:23 +01:00
#include "str-utils.h"
2019-03-08 23:19:54 +01:00
#ifndef AISL_WITHOUT_SSL
2019-03-13 16:47:23 +01:00
aisl_ssl_t
aisl_ssl_new( const char * key_file,
const char * crt_file,
const char * host,
SSL_CTX * ctx )
2019-03-08 23:19:54 +01:00
{
2019-03-13 16:47:23 +01:00
aisl_ssl_t ssl;
2019-03-08 23:19:54 +01:00
2019-03-13 16:47:23 +01:00
if ((ssl = calloc(1, sizeof(struct aisl_ssl))) != NULL)
2019-03-08 23:19:54 +01:00
{
2019-03-13 16:47:23 +01:00
if ((ssl->host = str_copy( host ? host : "*" )) != NULL)
{
if (ctx)
{
ssl->ctx = ctx;
return ssl;
}
else
{
if ((ssl->key_file = str_copy(key_file)) != NULL)
{
if ((ssl->crt_file = str_copy(crt_file)) != NULL)
{
return ssl;
}
}
}
}
aisl_ssl_free(ssl);
2019-03-08 23:19:54 +01:00
}
2019-03-13 16:47:23 +01:00
return NULL;
2019-03-08 23:19:54 +01:00
}
2019-03-13 16:47:23 +01:00
void
aisl_ssl_free( aisl_ssl_t ssl )
2019-03-08 23:19:54 +01:00
{
2019-03-13 16:47:23 +01:00
if (ssl->host)
free(ssl->host);
2019-03-08 23:19:54 +01:00
2019-03-13 16:47:23 +01:00
if (ssl->key_file)
2019-03-08 23:19:54 +01:00
{
2019-03-13 16:47:23 +01:00
free(ssl->key_file);
SSL_CTX_free(ssl->ctx);
2019-03-08 23:19:54 +01:00
}
2019-03-13 16:47:23 +01:00
if (ssl->crt_file)
free(ssl->crt_file);
2019-03-08 23:19:54 +01:00
2019-03-13 16:47:23 +01:00
free(ssl);
2019-03-08 23:19:54 +01:00
}
2019-03-13 16:47:23 +01:00
2019-03-08 23:19:54 +01:00
#endif