Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix and normalize CSR generation (engine.generate_csr()): #183

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/mbedtls/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -957,13 +957,15 @@ static int generate_csr(tlsuv_private_key_t key, char **pem, size_t *pemlen, ...
mbedtls_x509write_csr_set_key(&csr, pk);
uint8_t pembuf[4096];
if ((ret = mbedtls_x509write_csr_pem(&csr, pembuf, sizeof(pembuf), mbedtls_ctr_drbg_random, &ctr_drbg)) < 0) {
UM_LOG(ERR, "mbedtls_x509write_csr_pem returned %d", ret);
UM_LOG(ERR, "mbedtls_x509write_csr_pem returned %d/%s", ret, mbedtls_error(ret));
goto on_error;
}
on_error:
if (ret == 0) {
*pem = strdup((const char*)pembuf);
*pemlen = strlen((const char*)pembuf) + 1;
if (pemlen) {
*pemlen = strlen((const char *)pembuf);
}
}
mbedtls_x509write_csr_free(&csr);
return ret;
Expand Down
3 changes: 3 additions & 0 deletions src/openssl/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@


static void tls_set_cert_verify(tls_context *ctx, int (*verify_f)(void *cert, void *v_ctx), void *v_ctx) {
struct openssl_ctx *c = ctx;

Check warning on line 624 in src/openssl/engine.c

View workflow job for this annotation

GitHub Actions / build (ubuntu, openssl)

initialization of ‘struct openssl_ctx *’ from incompatible pointer type ‘tls_context *’ {aka ‘struct tls_context_s *’} [-Wincompatible-pointer-types]

Check warning on line 624 in src/openssl/engine.c

View workflow job for this annotation

GitHub Actions / build (macOS, openssl)

incompatible pointer types initializing 'struct openssl_ctx *' with an expression of type 'tls_context *' (aka 'struct tls_context_s *') [-Wincompatible-pointer-types]
c->cert_verify_f = verify_f;
c->verify_ctx = v_ctx;
SSL_CTX_set_verify(c->ctx, SSL_VERIFY_PEER, NULL);
Expand All @@ -641,7 +641,7 @@
}

static void tls_free_ctx(tls_context *ctx) {
struct openssl_ctx *c = ctx;

Check warning on line 644 in src/openssl/engine.c

View workflow job for this annotation

GitHub Actions / build (ubuntu, openssl)

initialization of ‘struct openssl_ctx *’ from incompatible pointer type ‘tls_context *’ {aka ‘struct tls_context_s *’} [-Wincompatible-pointer-types]

Check warning on line 644 in src/openssl/engine.c

View workflow job for this annotation

GitHub Actions / build (macOS, openssl)

incompatible pointer types initializing 'struct openssl_ctx *' with an expression of type 'tls_context *' (aka 'struct tls_context_s *') [-Wincompatible-pointer-types]
if (c->alpn_protocols) {
free(c->alpn_protocols);
}
Expand Down Expand Up @@ -716,7 +716,7 @@
}

static int tls_set_own_key(tls_context *ctx, tlsuv_private_key_t key) {
struct openssl_ctx *c = ctx;

Check warning on line 719 in src/openssl/engine.c

View workflow job for this annotation

GitHub Actions / build (ubuntu, openssl)

initialization of ‘struct openssl_ctx *’ from incompatible pointer type ‘tls_context *’ {aka ‘struct tls_context_s *’} [-Wincompatible-pointer-types]

Check warning on line 719 in src/openssl/engine.c

View workflow job for this annotation

GitHub Actions / build (macOS, openssl)

incompatible pointer types initializing 'struct openssl_ctx *' with an expression of type 'tls_context *' (aka 'struct tls_context_s *') [-Wincompatible-pointer-types]
SSL_CTX *ssl = c->ctx;

// sanity check
Expand Down Expand Up @@ -746,13 +746,13 @@

static int tls_set_own_cert(tls_context *ctx, tlsuv_private_key_t key,
tls_cert cert) {
struct openssl_ctx *c = ctx;

Check warning on line 749 in src/openssl/engine.c

View workflow job for this annotation

GitHub Actions / build (ubuntu, openssl)

initialization of ‘struct openssl_ctx *’ from incompatible pointer type ‘tls_context *’ {aka ‘struct tls_context_s *’} [-Wincompatible-pointer-types]

Check warning on line 749 in src/openssl/engine.c

View workflow job for this annotation

GitHub Actions / build (macOS, openssl)

incompatible pointer types initializing 'struct openssl_ctx *' with an expression of type 'tls_context *' (aka 'struct tls_context_s *') [-Wincompatible-pointer-types]
SSL_CTX *ssl = c->ctx;

if (key == NULL) {
SSL_CTX_use_PrivateKey(ssl, NULL);
if (c->own_key) {
c->own_key->free(c->own_key);

Check warning on line 755 in src/openssl/engine.c

View workflow job for this annotation

GitHub Actions / build (ubuntu, openssl)

passing argument 1 of ‘c->own_key->free’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 755 in src/openssl/engine.c

View workflow job for this annotation

GitHub Actions / build (macOS, openssl)

incompatible pointer types passing 'struct priv_key_s *' to parameter of type 'struct tlsuv_private_key_s *' [-Wincompatible-pointer-types]
}
c->own_key = NULL;

Expand Down Expand Up @@ -784,7 +784,7 @@

// OpenSSL requires setting certificate before private key
// https://www.openssl.org/docs/man3.0/man3/SSL_CTX_use_PrivateKey.html
struct priv_key_s *pk = key;

Check warning on line 787 in src/openssl/engine.c

View workflow job for this annotation

GitHub Actions / build (ubuntu, openssl)

initialization of ‘struct priv_key_s *’ from incompatible pointer type ‘tlsuv_private_key_t’ {aka ‘struct tlsuv_private_key_s *’} [-Wincompatible-pointer-types]

Check warning on line 787 in src/openssl/engine.c

View workflow job for this annotation

GitHub Actions / build (macOS, openssl)

incompatible pointer types initializing 'struct priv_key_s *' with an expression of type 'tlsuv_private_key_t' (aka 'struct tlsuv_private_key_s *') [-Wincompatible-pointer-types]
c->own_cert = tls_set_cert_internal(ssl, store);
X509_STORE_free(store);

Expand Down Expand Up @@ -1036,6 +1036,9 @@
size_t len = BIO_ctrl_pending(b);
*pem = calloc(1, len + 1);
BIO_read(b, *pem, (int)len);
if (pemlen) {
*pemlen = len;
}
}

BIO_free(b);
Expand Down
25 changes: 18 additions & 7 deletions tests/key_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,25 @@ TEST_CASE("gen csr", "[engine]") {
REQUIRE(ctx->generate_key(&key) == 0);

char *pem;
size_t pemlen;
REQUIRE(ctx->generate_csr_to_pem(key, &pem, &pemlen,
"C", "US",
"O", "OpenZiti",
"OU", "Developers",
"CN", "CSR test",
NULL) == 0);
size_t pemlen = 0;
CHECK(ctx->generate_csr_to_pem(key, &pem, &pemlen,
"C", "US",
"O", "OpenZiti",
"OU", "Developers",
"CN", "CSR test",
NULL) == 0);
CHECK(pemlen == strlen(pem));
printf("CSR:\n%.*s\n", (int)pemlen, pem);
free(pem);
pem = nullptr;

CHECK(ctx->generate_csr_to_pem(key, &pem, nullptr,
"C", "US",
"O", "OpenZiti",
"OU", "Developers",
"CN", "CSR test",
NULL) == 0);
printf("CSR:\n%s\n", pem);

key->free(key);
ctx->free_ctx(ctx);
Expand Down
Loading