Skip to content

Commit

Permalink
tls_wrappers/openssl: more detailed error logs
Browse files Browse the repository at this point in the history
Signed-off-by: Kun Lai <[email protected]>
  • Loading branch information
imlk0 authored and haosanzi committed Apr 13, 2023
1 parent a804b43 commit 2e527ae
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/core/rtls_core_generate_certificate.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ rats_tls_err_t rtls_core_generate_certificate(rtls_core_context_t *ctx)
if (privkey_len) {
tls_wrapper_err_t t_err;

#if 0
#ifndef SGX
/* Dump private key of this certificate */
FILE *fp = fopen("/tmp/privkey.der", "wb");
fwrite(privkey_buf, privkey_len, 1, fp);
fclose(fp);
#endif
#endif

t_err = ctx->tls_wrapper->opts->use_privkey(ctx->tls_wrapper, ctx->config.cert_algo,
privkey_buf, privkey_len);
if (t_err != TLS_WRAPPER_ERR_NONE) {
Expand Down
9 changes: 9 additions & 0 deletions src/crypto_wrappers/openssl/gen_cert.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ crypto_wrapper_err_t openssl_gen_cert(crypto_wrapper_ctx_t *ctx, rats_tls_cert_a
RTLS_DEBUG("self-signing certificate generated. cert_buf: %p, cert_len: %u\n",
cert_info->cert_buf, cert_info->cert_len);

#if 0
#ifndef SGX
/* Dump certificate */
FILE *fp = fopen("/tmp/cert_generated.der", "wb");
fwrite(cert_info->cert_buf, cert_info->cert_len, 1, fp);
fclose(fp);
#endif
#endif

ret = CRYPTO_WRAPPER_ERR_NONE;

err:
Expand Down
12 changes: 7 additions & 5 deletions src/tls_wrappers/openssl/negotiate.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ tls_wrapper_err_t openssl_internal_negotiate(tls_wrapper_ctx_t *ctx, unsigned lo
return -TLS_WRAPPER_ERR_INVALID;
}

ERR_clear_error();
int err;
if (conf_flags & RATS_TLS_CONF_FLAGS_SERVER)
err = SSL_accept(ssl);
Expand All @@ -59,11 +60,13 @@ tls_wrapper_err_t openssl_internal_negotiate(tls_wrapper_ctx_t *ctx, unsigned lo

if (err != 1) {
if (conf_flags & RATS_TLS_CONF_FLAGS_SERVER)
RTLS_DEBUG("failed to negotiate %#x\n", err);
RTLS_ERR("failed to negotiate %d, SSL_get_error(): %d\n", err,
SSL_get_error(ssl, err));
else
RTLS_DEBUG("failed to connect %#x\n", err);

print_openssl_err(ssl, err);
RTLS_ERR("failed to connect %d, SSL_get_error(): %d\n", err,
SSL_get_error(ssl, err));
// TODO: handle result of SSL_get_error()
print_openssl_err_all(ssl, err);

return OPENSSL_ERR_CODE(err);
}
Expand All @@ -78,7 +81,6 @@ tls_wrapper_err_t openssl_internal_negotiate(tls_wrapper_ctx_t *ctx, unsigned lo
return TLS_WRAPPER_ERR_NONE;
}


tls_wrapper_err_t openssl_tls_negotiate(tls_wrapper_ctx_t *ctx, int fd)
{
RTLS_DEBUG("ctx %p, fd %d\n", ctx, fd);
Expand Down
2 changes: 1 addition & 1 deletion src/tls_wrappers/openssl/openssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef struct {
SSL *ssl;
} openssl_ctx_t;

static inline void print_openssl_err(SSL *ssl, int ret)
static inline void print_openssl_err_all()
{
unsigned long l;

Expand Down
7 changes: 6 additions & 1 deletion src/tls_wrappers/openssl/receive.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ tls_wrapper_err_t openssl_tls_receive(tls_wrapper_ctx_t *ctx, void *buf, size_t
if (ssl_ctx == NULL || ssl_ctx->ssl == NULL)
return -TLS_WRAPPER_ERR_RECEIVE;

ERR_clear_error();

int rc = SSL_read(ssl_ctx->ssl, buf, (int)*buf_size);
if (rc <= 0) {
RTLS_ERR("ERROR: openssl_receive()\n");
// TODO: handle result of SSL_get_error()
RTLS_ERR("SSL_read() failed: %d, SSL_get_error(): %d\n", rc,
SSL_get_error(ssl_ctx->ssl, rc));
print_openssl_err_all();
return -TLS_WRAPPER_ERR_RECEIVE;
}
*buf_size = (size_t)rc;
Expand Down
7 changes: 6 additions & 1 deletion src/tls_wrappers/openssl/transmit.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ tls_wrapper_err_t openssl_tls_transmit(tls_wrapper_ctx_t *ctx, void *buf, size_t
if (ssl_ctx == NULL || ssl_ctx->ssl == NULL)
return -TLS_WRAPPER_ERR_TRANSMIT;

ERR_clear_error();

int rc = SSL_write(ssl_ctx->ssl, buf, (int)*buf_size);
if (rc <= 0) {
RTLS_DEBUG("ERROR: tls_wrapper_openssl transmit()\n");
// TODO: handle result of SSL_get_error()
RTLS_ERR("SSL_write() failed: %d, SSL_get_error(): %d\n", rc,
SSL_get_error(ssl_ctx->ssl, rc));
print_openssl_err_all();
return -TLS_WRAPPER_ERR_TRANSMIT;
}
*buf_size = (size_t)rc;
Expand Down
3 changes: 2 additions & 1 deletion src/tls_wrappers/openssl/use_privkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ tls_wrapper_err_t openssl_tls_use_privkey(tls_wrapper_ctx_t *ctx, rats_tls_cert_
int ret = SSL_CTX_use_PrivateKey_ASN1(EPKEY, ssl_ctx->sctx, privkey_buf, (long)privkey_len);

if (ret != SSL_SUCCESS) {
RTLS_ERR("failed to use private key %d\n", ret);
RTLS_ERR("failed to use private key.\n");
print_openssl_err_all();
return OPENSSL_ERR_CODE(ret);
}

Expand Down

0 comments on commit 2e527ae

Please sign in to comment.