diff options
author | Jannis Hoffmann <jannis@fehcom.de> | 2024-07-09 11:07:00 +0200 |
---|---|---|
committer | Jannis Hoffmann <jannis@fehcom.de> | 2024-07-09 11:07:00 +0200 |
commit | b33e1e38e5b02a4219423e3e28a85a3f4749e3a5 (patch) | |
tree | 19e16397080201525f2b0ff9b75982c129167bbe | |
parent | c9f72eaa698201cfb2aad0339350c5e13461d0d3 (diff) |
added first test openssl_get_subj_alt_dns
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | tests/meson.build | 5 | ||||
-rw-r--r-- | tests/openssl_get_subj_alt_dns.c | 62 | ||||
-rw-r--r-- | tests/testdata/www-fehcom-net.pem | 19 |
4 files changed, 87 insertions, 0 deletions
diff --git a/meson.build b/meson.build index 2286717..8e5b7de 100644 --- a/meson.build +++ b/meson.build @@ -32,3 +32,4 @@ endif subdir('src') subdir('man') +subdir('tests') diff --git a/tests/meson.build b/tests/meson.build new file mode 100644 index 0000000..20629a4 --- /dev/null +++ b/tests/meson.build @@ -0,0 +1,5 @@ + +t1 = executable('openssl_get_subj_alt_dns', 'openssl_get_subj_alt_dns.c', + dependencies : [ssl_dep, crypto_dep]) + +test('get_subj_alt_dns', t1, workdir : meson.current_source_dir()) 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 <stddef.h> +#include <stdio.h> + +#include <openssl/pem.h> +#include <openssl/x509.h> +#include <openssl/x509v3.h> + + +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; +} diff --git a/tests/testdata/www-fehcom-net.pem b/tests/testdata/www-fehcom-net.pem new file mode 100644 index 0000000..a0060ad --- /dev/null +++ b/tests/testdata/www-fehcom-net.pem @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE-----
+MIIDIzCCAsigAwIBAgIUF8cQlMTQxzgyyuxvqPBZEpuHBOYwCgYIKoZIzj0EAwIw
+gYcxCzAJBgNVBAYTAkRFMRgwFgYDVQQIEw9SaGVpbmxhbmQtUGZhbHoxDjAMBgNV
+BAcTBUhvZWhuMQ8wDQYDVQQKEwZGZWhjb20xFzAVBgNVBAMTDnd3dy5mZWhjb20u
+bmV0MSQwIgYJKoZIhvcNAQkBFhVwb3N0bWFzdGVyQGZlaGNvbS5uZXQwHhcNMjMx
+MTExMjAxODMwWhcNMzIwMTI4MjAxODMwWjCBhzELMAkGA1UEBhMCREUxGDAWBgNV
+BAgTD1JoZWlubGFuZC1QZmFsejEOMAwGA1UEBxMFSG9laG4xDzANBgNVBAoTBkZl
+aGNvbTEXMBUG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