Skip to content

Commit

Permalink
refactor(listener): move release of certificate file to quicer_tls
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhuyan committed Sep 4, 2023
1 parent 0b9ad40 commit 43b2fbe
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 34 deletions.
60 changes: 26 additions & 34 deletions c_src/quicer_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ ERL_NIF_TERM
listen2(ErlNifEnv *env, __unused_parm__ int argc, const ERL_NIF_TERM argv[])
{
QUIC_STATUS Status = QUIC_STATUS_SUCCESS;
ERL_NIF_TERM ret = ATOM_OK;

ERL_NIF_TERM elisten_on = argv[0];
ERL_NIF_TERM options = argv[1];
Expand All @@ -249,9 +250,9 @@ listen2(ErlNifEnv *env, __unused_parm__ int argc, const ERL_NIF_TERM argv[])
return ERROR_TUPLE_2(ATOM_BADARG);
}

// Start Build CredConfig from options
// Start build CredConfig from with listen opts
QUIC_CREDENTIAL_CONFIG CredConfig;
// CxPlatZeroMemory(&CredConfig, sizeof(QUIC_CREDENTIAL_CONFIG));

CredConfig.Flags = QUIC_CREDENTIAL_FLAG_NONE;

if (!parse_cert_options(env, options, &CredConfig))
Expand Down Expand Up @@ -289,23 +290,23 @@ listen2(ErlNifEnv *env, __unused_parm__ int argc, const ERL_NIF_TERM argv[])

if (!build_trustedstore(l_ctx->cacertfile, &l_ctx->trusted_store))
{
destroy_l_ctx(l_ctx);
return ERROR_TUPLE_2(ATOM_CERT_ERROR);
ret = ERROR_TUPLE_2(ATOM_CERT_ERROR);
goto exit;
}
}

// Set owner for l_ctx
if (!enif_self(env, &(l_ctx->listenerPid)))
{
destroy_l_ctx(l_ctx);
return ERROR_TUPLE_2(ATOM_BAD_PID);
ret = ERROR_TUPLE_2(ATOM_BAD_PID);
goto exit;
}

// Get Reg for l_ctx, quic_registration is optional
if (!parse_registration(env, options, &l_ctx->r_ctx))
{
destroy_l_ctx(l_ctx);
return ERROR_TUPLE_2(ATOM_QUIC_REGISTRATION);
ret = ERROR_TUPLE_2(ATOM_QUIC_REGISTRATION);
goto exit;
}

if (l_ctx->r_ctx)
Expand All @@ -329,35 +330,19 @@ listen2(ErlNifEnv *env, __unused_parm__ int argc, const ERL_NIF_TERM argv[])
&l_ctx->config_resource->Configuration,
&CredConfig);

// Cleanup CredConfig
if (QUIC_CREDENTIAL_TYPE_CERTIFICATE_FILE == CredConfig.Type)
{
free((char *)CredConfig.CertificateFile->CertificateFile);
free((char *)CredConfig.CertificateFile->PrivateKeyFile);
CxPlatFree(CredConfig.CertificateFile, QUICER_CERTIFICATE_FILE);
}
else if (QUIC_CREDENTIAL_TYPE_CERTIFICATE_FILE_PROTECTED == CredConfig.Type)
{
free((char *)CredConfig.CertificateFileProtected->CertificateFile);
free((char *)CredConfig.CertificateFileProtected->PrivateKeyFile);
free((char *)CredConfig.CertificateFileProtected->PrivateKeyPassword);
CxPlatFree(CredConfig.CertificateFileProtected,
QUICER_CERTIFICATE_FILE_PROTECTED);
}

if (!IS_SAME_TERM(ATOM_OK, estatus))
{
destroy_l_ctx(l_ctx);
return ERROR_TUPLE_3(ATOM_CONFIG_ERROR, estatus);
ret = ERROR_TUPLE_3(ATOM_CONFIG_ERROR, estatus);
goto exit;
}

// mon will be removed when triggered or when l_ctx is dealloc.
if (0
!= enif_monitor_process(
env, l_ctx, &l_ctx->listenerPid, &l_ctx->owner_mon))
{
destroy_l_ctx(l_ctx);
return ERROR_TUPLE_2(ATOM_BAD_MON);
ret = ERROR_TUPLE_2(ATOM_BAD_MON);
goto exit;
}

// Now open listener
Expand All @@ -370,8 +355,8 @@ listen2(ErlNifEnv *env, __unused_parm__ int argc, const ERL_NIF_TERM argv[])
{
// Server Configuration should be destroyed
l_ctx->config_resource->Configuration = NULL;
destroy_l_ctx(l_ctx);
return ERROR_TUPLE_3(ATOM_LISTENER_OPEN_ERROR, ATOM_STATUS(Status));
ret = ERROR_TUPLE_3(ATOM_LISTENER_OPEN_ERROR, ATOM_STATUS(Status));
goto exit;
}
l_ctx->is_closed = FALSE;

Expand All @@ -388,8 +373,8 @@ listen2(ErlNifEnv *env, __unused_parm__ int argc, const ERL_NIF_TERM argv[])

if (!load_alpn(env, &options, &alpn_buffer_length, alpn_buffers))
{
destroy_l_ctx(l_ctx);
return ERROR_TUPLE_2(ATOM_ALPN);
ret = ERROR_TUPLE_2(ATOM_ALPN);
goto exit;
}

// Start Listener
Expand All @@ -400,11 +385,18 @@ listen2(ErlNifEnv *env, __unused_parm__ int argc, const ERL_NIF_TERM argv[])
TP_NIF_3(start_fail, (uintptr_t)(l_ctx->Listener), Status);
MsQuic->ListenerClose(l_ctx->Listener);
l_ctx->Listener = NULL;
destroy_l_ctx(l_ctx);
return ERROR_TUPLE_3(ATOM_LISTENER_START_ERROR, ATOM_STATUS(Status));
ret = ERROR_TUPLE_3(ATOM_LISTENER_START_ERROR, ATOM_STATUS(Status));
goto exit;
}
ERL_NIF_TERM listenHandle = enif_make_resource(env, l_ctx);

free_certificate(&CredConfig);
return OK_TUPLE_2(listenHandle);

exit: //errors..
free_certificate(&CredConfig);
destroy_l_ctx(l_ctx);
return ret;
}

ERL_NIF_TERM
Expand Down
28 changes: 28 additions & 0 deletions c_src/quicer_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,31 @@ build_trustedstore(const char *cacertfile, X509_STORE **trusted_store)
*trusted_store = store;
return TRUE;
}

/*
* Free certfile/certfileprotected of QUIC_CREDENTIAL_CONFIG
*
*/
void
free_certificate(QUIC_CREDENTIAL_CONFIG *cc)
{
if (!cc)
{
return;
}

if (QUIC_CREDENTIAL_TYPE_CERTIFICATE_FILE == cc->Type)
{
free((char *)cc->CertificateFile->CertificateFile);
free((char *)cc->CertificateFile->PrivateKeyFile);
CxPlatFree(cc->CertificateFile, QUICER_CERTIFICATE_FILE);
}
else if (QUIC_CREDENTIAL_TYPE_CERTIFICATE_FILE_PROTECTED == cc->Type)
{
free((char *)cc->CertificateFileProtected->CertificateFile);
free((char *)cc->CertificateFileProtected->PrivateKeyFile);
free((char *)cc->CertificateFileProtected->PrivateKeyPassword);
CxPlatFree(cc->CertificateFileProtected,
QUICER_CERTIFICATE_FILE_PROTECTED);
}
}
2 changes: 2 additions & 0 deletions c_src/quicer_tls.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ parse_cacertfile_option(ErlNifEnv *env,
BOOLEAN
build_trustedstore(const char *cacertfile, X509_STORE **trusted_store);

void
free_certificate(QUIC_CREDENTIAL_CONFIG *cc);
#endif // QUICER_TLS_H_

0 comments on commit 43b2fbe

Please sign in to comment.