Skip to content

Commit 490364c

Browse files
schwabecron2
authored andcommitted
Add unit test for printing various details of certificates
These unit tests will ensure that refactoring of these methods does not change the output. Change-Id: Iacbd8195cdedc7226bddc686ca8dccf9f25f8842 Signed-off-by: Arne Schwabe <arne@rfc2549.org> Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1598 Message-Id: <20260331173403.3082-1-gert@greenie.muc.de> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg36389.html Signed-off-by: Gert Doering <gert@greenie.muc.de> (cherry picked from commit 27d1b9a)
1 parent 4d16f2a commit 490364c

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

tests/unit_tests/openvpn/test_ssl.c

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
#include "test_common.h"
4747
#include "ssl.h"
4848
#include "buffer.h"
49+
#include "cert_data.h"
4950
#include "packet_id.h"
51+
#include "ssl_verify.h"
5052

5153
/* Mock function to be allowed to include win32.c which is required for
5254
* getting the temp directory */
@@ -750,6 +752,83 @@ test_data_channel_known_vectors_shortpktid(void **state)
750752
test_data_channel_known_vectors_run(false);
751753
}
752754

755+
#if defined(ENABLE_CRYPTO_MBEDTLS)
756+
static openvpn_x509_cert_t *
757+
get_certificate(const char *cert_str)
758+
{
759+
mbedtls_x509_crt *cert;
760+
ALLOC_OBJ_CLEAR(cert, mbedtls_x509_crt);
761+
int ret = mbedtls_x509_crt_parse(cert, (const unsigned char *)cert_str,
762+
strlen(cert_str) + 1);
763+
764+
assert_int_equal(ret, 0);
765+
return cert;
766+
}
767+
768+
static void
769+
free_certificate(openvpn_x509_cert_t *cert)
770+
{
771+
mbedtls_x509_crt_free(cert);
772+
free(cert);
773+
}
774+
#else
775+
static openvpn_x509_cert_t *
776+
get_certificate(const char *cert_str)
777+
{
778+
BIO *in = BIO_new_mem_buf((char *)cert1, -1);
779+
assert_non_null(in);
780+
X509 *cert = PEM_read_bio_X509(in, NULL, NULL, NULL);
781+
assert_non_null(cert);
782+
BIO_free(in);
783+
return cert;
784+
}
785+
786+
static void
787+
free_certificate(openvpn_x509_cert_t *cert)
788+
{
789+
X509_free(cert);
790+
}
791+
#endif
792+
793+
void
794+
crypto_test_print_cert_details(void **state)
795+
{
796+
openvpn_x509_cert_t *cert = get_certificate(cert1);
797+
struct gc_arena gc = gc_new();
798+
799+
const char *fp = backend_x509_get_serial_hex(cert, &gc);
800+
801+
/* we messed this up between TLS libraries. But let's at least notice in
802+
* the future ...*/
803+
#if defined(ENABLE_CRYPTO_MBEDTLS)
804+
assert_string_equal(fp, "82:6B:DD:CC:BD:E5:5E:B7:08:F1:2D:68:00:3C:24:DE");
805+
#else
806+
assert_string_equal(fp, "82:6b:dd:cc:bd:e5:5e:b7:08:f1:2d:68:00:3c:24:de");
807+
#endif
808+
809+
const char *sn = backend_x509_get_serial(cert, &gc);
810+
assert_string_equal(sn, "173359713849739808110610111821055272158");
811+
812+
char username[TLS_USERNAME_LEN + 1] = { 0 }; /* null-terminated */
813+
814+
int ret = backend_x509_get_username(username, sizeof(username), "CN",
815+
cert);
816+
817+
assert_string_equal(username, "ovpn-test-ec1");
818+
assert_int_equal(ret, SUCCESS);
819+
820+
#ifndef ENABLE_CRYPTO_MBEDTLS
821+
/* mbed TLS does not implement this */
822+
ret = backend_x509_get_username(username, sizeof(username), "serialNumber",
823+
cert);
824+
assert_int_equal(ret, SUCCESS);
825+
assert_string_equal(username, "0x826BDDCCBDE55EB708F12D68003C24DE");
826+
#endif
827+
828+
gc_free(&gc);
829+
free_certificate(cert);
830+
}
831+
753832

754833
int
755834
main(void)
@@ -773,7 +852,9 @@ main(void)
773852
cmocka_unit_test(test_data_channel_roundtrip_aes_256_cbc),
774853
cmocka_unit_test(test_data_channel_roundtrip_bf_cbc),
775854
cmocka_unit_test(test_data_channel_known_vectors_epoch),
776-
cmocka_unit_test(test_data_channel_known_vectors_shortpktid)
855+
cmocka_unit_test(test_data_channel_known_vectors_shortpktid),
856+
cmocka_unit_test(crypto_test_print_cert_details)
857+
777858
};
778859

779860
#if defined(ENABLE_CRYPTO_OPENSSL)

0 commit comments

Comments
 (0)