Debian Codegraph @

src/security/core/tests/common/cert_utils.c

raw
1 /*
2 * Copyright(c) 2006 to 2020 ZettaScale Technology and others
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0 which is available at
6 * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
7 * v. 1.0 which is available at
8 * http://www.eclipse.org/org/documents/edl-v10.php.
9 *
10 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11 */
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "dds/ddsrt/heap.h"
17 #include "dds/ddsrt/string.h"
18 #include "dds/security/openssl_support.h"
19 #include "CUnit/Test.h"
20 #include "cert_utils.h"
21
22 #define MAX_EMAIL 255
23 #define EMAIL_HOST "cycloneddssecurity.adlinktech.com"
24
25 static X509 * get_x509(int not_valid_before, int not_valid_after, const char * cn, const char * email)
26 {
27 X509 * cert = X509_new ();
28 CU_ASSERT_FATAL (cert != NULL);
29 ASN1_INTEGER_set (X509_get_serialNumber (cert), 1);
30 X509_gmtime_adj (X509_get_notBefore (cert), not_valid_before);
31 X509_gmtime_adj (X509_get_notAfter (cert), not_valid_after);
32
33 X509_NAME * name = X509_get_subject_name (cert);
34 X509_NAME_add_entry_by_txt (name, "C", MBSTRING_ASC, (unsigned char *) "NL", -1, -1, 0);
35 X509_NAME_add_entry_by_txt (name, "O", MBSTRING_ASC, (unsigned char *) "Example Organization", -1, -1, 0);
36 X509_NAME_add_entry_by_txt (name, "CN", MBSTRING_ASC, (unsigned char *) cn, -1, -1, 0);
37 X509_NAME_add_entry_by_txt (name, "emailAddress", MBSTRING_ASC, (unsigned char *) email, -1, -1, 0);
38 return cert;
39 }
40
41 static char * get_x509_data(X509 * cert)
42 {
43 // Create BIO for writing output
44 BIO *output_bio = BIO_new (BIO_s_mem ());
45 if (!PEM_write_bio_X509 (output_bio, cert)) {
46 printf ("Error writing certificate\n");
47 ERR_print_errors_fp (stderr);
48 CU_ASSERT_FATAL (false);
49 }
50
51 // Get string
52 char *output_tmp = NULL;
53 size_t output_sz = (size_t) BIO_get_mem_data (output_bio, &output_tmp);
54 char * output = ddsrt_malloc (output_sz + 1);
55 memcpy (output, output_tmp, output_sz);
56 output[output_sz] = 0;
57 BIO_free (output_bio);
58
59 return output;
60 }
61
62 static EVP_PKEY * get_priv_key(const char * priv_key_str)
63 {
64 BIO *pkey_bio = BIO_new_mem_buf (priv_key_str, -1);
65 EVP_PKEY *priv_key = PEM_read_bio_PrivateKey (pkey_bio, NULL, NULL, 0);
66 CU_ASSERT_FATAL (priv_key != NULL);
67 BIO_free (pkey_bio);
68 return priv_key;
69 }
70
71 static X509 * get_cert(const char * cert_str)
72 {
73 BIO *cert_bio = BIO_new_mem_buf (cert_str, -1);
74 X509 *cert = PEM_read_bio_X509 (cert_bio, NULL, NULL, 0);
75 CU_ASSERT_FATAL (cert != NULL);
76 BIO_free (cert_bio);
77 return cert;
78 }
79
80 char * generate_ca(const char *ca_name, const char * ca_priv_key_str, int not_valid_before, int not_valid_after)
81 {
82 EVP_PKEY *ca_priv_key = get_priv_key (ca_priv_key_str);
83
84 char * email = malloc (MAX_EMAIL);
85 snprintf(email, MAX_EMAIL, "%s@%s" , ca_name, EMAIL_HOST);
86 X509 * ca_cert = get_x509 (not_valid_before, not_valid_after, ca_name, email);
87 ddsrt_free (email);
88
89 X509_set_pubkey (ca_cert, ca_priv_key);
90 X509_set_issuer_name (ca_cert, X509_get_subject_name (ca_cert)); /* self-signed */
91 X509_sign (ca_cert, ca_priv_key, EVP_sha256 ());
92 char * output = get_x509_data (ca_cert);
93
94 EVP_PKEY_free (ca_priv_key);
95 X509_free (ca_cert);
96
97 return output;
98 }
99
100 char * generate_identity(const char * ca_cert_str, const char * ca_priv_key_str, const char * name, const char * priv_key_str, int not_valid_before, int not_valid_after, char ** subject)
101 {
102 X509 *ca_cert = get_cert (ca_cert_str);
103 EVP_PKEY *ca_key_pkey = get_priv_key (ca_priv_key_str);
104 EVP_PKEY *id_pkey = get_priv_key (priv_key_str);
105 EVP_PKEY *ca_cert_pkey = X509_get_pubkey (ca_cert);
106 X509_REQ *csr = X509_REQ_new ();
107 X509_REQ_set_pubkey (csr, id_pkey);
108 X509_REQ_sign (csr, id_pkey, EVP_sha256 ());
109
110 char * email = malloc (MAX_EMAIL);
111 snprintf(email, MAX_EMAIL, "%s@%s" , name, EMAIL_HOST);
112 X509 * cert = get_x509 (not_valid_before, not_valid_after, name, email);
113 ddsrt_free (email);
114
115 EVP_PKEY *csr_pkey = X509_REQ_get_pubkey (csr);
116 X509_set_pubkey (cert, csr_pkey);
117 X509_set_issuer_name (cert, X509_get_subject_name (ca_cert));
118 X509_sign (cert, ca_key_pkey, EVP_sha256 ());
119 char * output = get_x509_data (cert);
120
121 if (subject)
122 {
123 X509_NAME *subj_name = X509_get_subject_name (cert);
124 char * subj_openssl = X509_NAME_oneline (subj_name, NULL, 0);
125 *subject = ddsrt_strdup (subj_openssl);
126 OPENSSL_free (subj_openssl);
127 }
128
129 X509_REQ_free (csr);
130 EVP_PKEY_free (id_pkey);
131 EVP_PKEY_free (ca_cert_pkey);
132 EVP_PKEY_free (ca_key_pkey);
133 EVP_PKEY_free (csr_pkey);
134 X509_free (cert);
135 X509_free (ca_cert);
136
137 return output;
138 }