aisl/src/ssl.c

71 lines
1.1 KiB
C

/*
* 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"
#include "str-utils.h"
#ifndef AISL_WITHOUT_SSL
aisl_ssl_t
aisl_ssl_new( const char * key_file,
const char * crt_file,
const char * host,
SSL_CTX * ctx )
{
aisl_ssl_t ssl;
if ((ssl = calloc(1, sizeof(struct aisl_ssl))) != NULL)
{
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);
}
return NULL;
}
void
aisl_ssl_free( aisl_ssl_t ssl )
{
if (ssl->host)
free(ssl->host);
if (ssl->key_file)
{
free(ssl->key_file);
SSL_CTX_free(ssl->ctx);
}
if (ssl->crt_file)
free(ssl->crt_file);
free(ssl);
}
#endif