summaryrefslogtreecommitdiff
path: root/tests/openssl_get_subj_alt_dns.c
diff options
context:
space:
mode:
authorJannis Hoffmann <jannis@fehcom.de>2024-07-09 11:07:00 +0200
committerJannis Hoffmann <jannis@fehcom.de>2024-07-09 11:07:00 +0200
commitb33e1e38e5b02a4219423e3e28a85a3f4749e3a5 (patch)
tree19e16397080201525f2b0ff9b75982c129167bbe /tests/openssl_get_subj_alt_dns.c
parentc9f72eaa698201cfb2aad0339350c5e13461d0d3 (diff)
added first test openssl_get_subj_alt_dns
Diffstat (limited to 'tests/openssl_get_subj_alt_dns.c')
-rw-r--r--tests/openssl_get_subj_alt_dns.c62
1 files changed, 62 insertions, 0 deletions
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;
+}