From b33e1e38e5b02a4219423e3e28a85a3f4749e3a5 Mon Sep 17 00:00:00 2001 From: Jannis Hoffmann Date: Tue, 9 Jul 2024 11:07:00 +0200 Subject: added first test openssl_get_subj_alt_dns --- tests/openssl_get_subj_alt_dns.c | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/openssl_get_subj_alt_dns.c (limited to 'tests/openssl_get_subj_alt_dns.c') diff --git a/tests/openssl_get_subj_alt_dns.c b/tests/openssl_get_subj_alt_dns.c new file mode 100644 index 0000000..29cd111 --- /dev/null +++ b/tests/openssl_get_subj_alt_dns.c @@ -0,0 +1,62 @@ +#include +#include + +#include +#include +#include + + +int main() +{ + FILE *fp = fopen("testdata/www-fehcom-net.pem", "r"); + if (!fp) { + fprintf(stderr, "Unable to open certificate file\n"); + return 1; + } + + X509 *cert = PEM_read_X509(fp, NULL, NULL, NULL); + if (!cert) { + fprintf(stderr, "Unable to parse certificate\n"); + return 1; + } + + fclose(fp); + + GENERAL_NAMES *san = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); + if (!san) { + fprintf(stderr, "No Subject Alternative Names found\n"); + return 1; + } + + int num_sans = sk_GENERAL_NAME_num(san); + for (int i = 0; i < num_sans; i++) { + const GENERAL_NAME *current_name = sk_GENERAL_NAME_value(san, i); + + switch (current_name->type) { + case GEN_DNS: + printf("DNS: %s\n", ASN1_STRING_get0_data(current_name->d.dNSName)); + break; + case GEN_EMAIL: + printf("Email: %s\n", ASN1_STRING_get0_data(current_name->d.rfc822Name)); + break; + case GEN_URI: + printf("URI: %s\n", ASN1_STRING_get0_data(current_name->d.uniformResourceIdentifier)); + break; + case GEN_IPADD: + int len = ASN1_STRING_length(current_name->d.iPAddress); + if (len != 4) { + fprintf(stderr, "Length mismatch in ip\n"); + return 1; + } + const unsigned char *data = ASN1_STRING_get0_data(current_name->d.iPAddress); + printf("IP Address: %d.%d.%d.%d\n", data[0], data[1], data[2], data[3]); + break; + // Add more types if needed + } + } + GENERAL_NAMES_free(san); + + X509_free(cert); + + return EXIT_SUCCESS; +} -- cgit v1.2.3