Skip to content

Commit

Permalink
CLEANUP: Replace several trivial functions by in-place assignments.
Browse files Browse the repository at this point in the history
Many other context parameters were already set directly when parsing
the given options, and there was little reason for further indirection
because the SSL context is not opaque to begin with.

In fact, too many similarly named entities made navigating the code
harder than necessary and also quite a bit longer.
  • Loading branch information
triska committed Jul 2, 2017
1 parent f021c06 commit e070ba0
Showing 1 changed file with 17 additions and 112 deletions.
129 changes: 17 additions & 112 deletions ssl4pl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1718,109 +1718,6 @@ ssl_config_free( void * ctx
}
}

static char *
ssl_set_cacert(PL_SSL *config, const char *cacert)
/*
* Store certificate authority location in config storage
*/
{
if (cacert) {
if (config->cacert) free(config->cacert);
config->cacert = ssl_strdup(cacert);
}
return config->cacert;
}


static char *
ssl_set_certificate_file(PL_SSL *config, const char *certificate_file)
/*
* Store certificate file location in config storage
*/
{
if (certificate_file) {
if (config->certificate_file) free(config->certificate_file);
config->certificate_file = ssl_strdup(certificate_file);
}
return config->certificate_file;
}

static char *
ssl_set_keyf(PL_SSL *config, const char *keyf)
/*
* Store private key location in config storage
*/
{
if (keyf) {
if (config->key_file) free(config->key_file);
config->key_file = ssl_strdup(keyf);
}
return config->key_file;
}

static STACK_OF(X509_CRL) *
ssl_set_crl_list(PL_SSL *config, STACK_OF(X509_CRL) *crl)
/*
* Store CRL location in config storage
*/
{
if (crl)
{ if (config->crl_list)
{ sk_X509_CRL_pop_free(config->crl_list, X509_CRL_free);
}
config->crl_list = crl;
}
return config->crl_list;
}

static char *
ssl_set_cipher_list(PL_SSL *config, const char *cipher_list)
{ if ( cipher_list )
{ if ( config->cipher_list )
free(config->cipher_list);
config->cipher_list = ssl_strdup(cipher_list);
}

return config->cipher_list;
}

static char *
ssl_set_ecdh_curve(PL_SSL *config, const char *ecdh_curve)
{ if ( ecdh_curve )
{ if ( config->ecdh_curve )
free(config->ecdh_curve);
config->ecdh_curve = ssl_strdup(ecdh_curve);
}

return config->ecdh_curve;
}

static char *
ssl_set_password(PL_SSL *config, const char *password)
/*
* Store supplied private key password in config storage
*/
{
if (password) {
if (config->password) free(config->password);
config->password = ssl_strdup(password);
}
return config->password;
}

static char *
ssl_set_host(PL_SSL *config, const char *host)
/*
* Store supplied host in config storage
*/
{
if (host) {
if (config->host) free(config->host);
config->host = ssl_strdup(host);
}
return config->host;
}


static int
ssl_cb_cert_verify(int preverify_ok, X509_STORE_CTX *ctx)
Expand Down Expand Up @@ -2968,22 +2865,24 @@ parse_malleable_options(PL_SSL *conf, module_t module, term_t options)
if ( !get_char_arg(1, head, &s) )
return FALSE;

ssl_set_cipher_list(conf, s);
if (conf->cipher_list) free(conf->cipher_list);
conf->cipher_list = ssl_strdup(s);
} else if ( name == ATOM_ecdh_curve && arity == 1 )
{ char *s;

if ( !get_char_arg(1, head, &s) )
return FALSE;

ssl_set_ecdh_curve(conf, s);

if (conf->ecdh_curve) free(conf->ecdh_curve);
conf->ecdh_curve = ssl_strdup(s);
} else if ( name == ATOM_host && arity == 1 )
{ char *s;

if ( !get_char_arg(1, head, &s) )
return FALSE;

ssl_set_host(conf, s);
if (conf->host) free(conf->host);
conf->host = ssl_strdup(s);
} else if ( name == ATOM_peer_cert && arity == 1 )
{ int val;

Expand Down Expand Up @@ -3175,7 +3074,8 @@ pl_ssl_context(term_t role, term_t config, term_t options, term_t method)
if ( !get_char_arg(1, head, &s) )
return FALSE;

ssl_set_password(conf, s);
if (conf->password) free(conf->password);
conf->password = ssl_strdup(s);
} else if ( name == ATOM_require_crl && arity == 1 )
{ int val;

Expand All @@ -3200,7 +3100,9 @@ pl_ssl_context(term_t role, term_t config, term_t options, term_t method)
return PL_existence_error("file", list_head);
}
}
ssl_set_crl_list(conf, crls);
if (conf->crl_list)
sk_X509_CRL_pop_free(conf->crl_list, X509_CRL_free);
conf->crl_list = crls;
} else if ( name == ATOM_cacert_file && arity == 1 )
{ term_t val = PL_new_term_ref();
char *file;
Expand All @@ -3217,7 +3119,8 @@ pl_ssl_context(term_t role, term_t config, term_t options, term_t method)
else
return PL_domain_error("system_cacert", val);
} else if ( PL_get_file_name(val, &file, PL_FILE_EXIST) )
{ ssl_set_cacert(conf, file);
{ if (conf->cacert) free(conf->cacert);
conf->cacert = ssl_strdup(file);
} else
return FALSE;
} else if ( name == ATOM_certificate_file && arity == 1 )
Expand All @@ -3226,7 +3129,8 @@ pl_ssl_context(term_t role, term_t config, term_t options, term_t method)
if ( !get_file_arg(1, head, &file) )
return FALSE;

ssl_set_certificate_file(conf, file);
if (conf->certificate_file) free(conf->certificate_file);
conf->certificate_file = ssl_strdup(file);
} else if ( name == ATOM_certificate_key_pairs && arity == 1 )
{ term_t cert_head = PL_new_term_ref();
term_t cert_tail = PL_new_term_ref();
Expand Down Expand Up @@ -3263,7 +3167,8 @@ pl_ssl_context(term_t role, term_t config, term_t options, term_t method)
if ( !get_file_arg(1, head, &file) )
return FALSE;

ssl_set_keyf(conf, file);
if (conf->key_file) free(conf->key_file);
conf->key_file = ssl_strdup(file);
} else if ( name == ATOM_pem_password_hook && arity == 1 )
{ term_t cb = PL_new_term_ref();
_PL_get_arg(1, head, cb);
Expand Down

0 comments on commit e070ba0

Please sign in to comment.