1 #include <winpr/file.h>
2 #include <winpr/string.h>
3 #include "../x509_utils.h"
5 typedef char* (*get_field_pr)(
const X509*);
13 const char* field_description;
14 get_field_pr get_field;
15 const char* expected_result;
18 static char* x509_utils_subject_common_name_wo_length(
const X509* xcert)
21 return x509_utils_get_common_name(xcert, &length);
24 static char* certificate_path(
const char* filename)
32 static const char dirsep =
'/';
33 #ifdef TEST_SOURCE_DIR
34 const char* file = TEST_SOURCE_DIR;
35 const size_t flen = strlen(file) +
sizeof(dirsep) + strlen(filename) +
sizeof(char);
36 char* result = calloc(1, flen);
39 (void)_snprintf(result, flen,
"%s%c%s", file, dirsep, filename);
42 const char* file = __FILE__;
43 const char* last_dirsep = strrchr(file, dirsep);
47 const size_t filenameLen = strlen(filename);
48 const size_t dirsepLen = last_dirsep - file + 1;
49 char* result = malloc(dirsepLen + filenameLen + 1);
52 strncpy(result, file, dirsepLen);
53 strncpy(result + dirsepLen, filename, filenameLen + 1);
59 return _strdup(filename);
64 static const certificate_test_t certificate_tests[] = {
66 { ENABLED,
"Certificate Common Name", x509_utils_subject_common_name_wo_length,
67 "TESTJEAN TESTMARTIN 9999999" },
69 { ENABLED,
"Certificate subject", x509_utils_get_subject,
70 "CN = TESTJEAN TESTMARTIN 9999999, C = FR, O = MINISTERE DES TESTS, OU = 0002 110014016, OU "
71 "= PERSONNES, UID = 9999999, GN = TESTJEAN, SN = TESTMARTIN" },
73 { DISABLED,
"Kerberos principal name", 0,
"testjean.testmartin@kpn.test.example.com" },
75 { ENABLED,
"Certificate e-mail", x509_utils_get_email,
"testjean.testmartin@test.example.com"
79 { ENABLED,
"Microsoft's Universal Principal Name", x509_utils_get_upn,
80 "testjean.testmartin.9999999@upn.test.example.com" },
82 { ENABLED,
"Certificate issuer", x509_utils_get_issuer,
83 "CN = ADMINISTRATION CENTRALE DES TESTS, C = FR, O = MINISTERE DES TESTS, OU = 0002 "
87 static int TestCertificateFile(
const char* certificate_path,
88 const certificate_test_t* ccertificate_tests,
size_t count)
92 X509* certificate = x509_utils_from_pem(certificate_path, strlen(certificate_path), TRUE);
96 printf(
"%s: failure: cannot read certificate file '%s'\n", __func__, certificate_path);
101 for (
size_t i = 0; i < count; i++)
103 const certificate_test_t* test = &ccertificate_tests[i];
106 if (test->status == DISABLED)
111 result = (test->get_field ? test->get_field(certificate) : 0);
115 printf(
"%s: crypto got %-40s -> \"%s\"\n", __func__, test->field_description, result);
117 if (0 != strcmp(result, test->expected_result))
119 printf(
"%s: failure: for %s, actual: \"%s\", expected \"%s\"\n", __func__,
120 test->field_description, result, test->expected_result);
128 printf(
"%s: failure: cannot get %s\n", __func__, test->field_description);
133 X509_free(certificate);
162 const char* filename;
163 WINPR_MD_TYPE expected;
164 } signature_alg_test_t;
166 static const signature_alg_test_t signature_alg_tests[] = {
167 {
"rsa_pkcs1_sha1_cert.pem", WINPR_MD_SHA1 },
168 {
"rsa_pkcs1_sha256_cert.pem", WINPR_MD_SHA256 },
169 {
"rsa_pkcs1_sha384_cert.pem", WINPR_MD_SHA384 },
170 {
"rsa_pkcs1_sha512_cert.pem", WINPR_MD_SHA512 },
172 {
"ecdsa_sha1_cert.pem", WINPR_MD_SHA1 },
173 {
"ecdsa_sha256_cert.pem", WINPR_MD_SHA256 },
174 {
"ecdsa_sha384_cert.pem", WINPR_MD_SHA384 },
175 {
"ecdsa_sha512_cert.pem", WINPR_MD_SHA512 },
177 {
"rsa_pss_sha1_cert.pem", WINPR_MD_SHA1 },
178 {
"rsa_pss_sha256_cert.pem", WINPR_MD_SHA256 },
179 {
"rsa_pss_sha384_cert.pem", WINPR_MD_SHA384 },
180 {
"rsa_pss_sha512_cert.pem", WINPR_MD_SHA512 },
185 {
"rsa_pss_sha256_mgf1_sha384_cert.pem", WINPR_MD_NONE },
188 static int TestSignatureAlgorithm(
const signature_alg_test_t* test)
191 WINPR_MD_TYPE signature_alg = WINPR_MD_NONE;
192 char* path = certificate_path(test->filename);
193 X509* certificate = x509_utils_from_pem(path, strlen(path), TRUE);
197 printf(
"%s: failure: cannot read certificate file '%s'\n", __func__, path);
202 signature_alg = x509_utils_get_signature_alg(certificate);
203 if (signature_alg != test->expected)
205 const char* signature_alg_string =
206 signature_alg == WINPR_MD_NONE ?
"none" : winpr_md_type_to_string(signature_alg);
207 const char* expected_string =
208 test->expected == WINPR_MD_NONE ?
"none" : winpr_md_type_to_string(test->expected);
209 printf(
"%s: failure: for \"%s\", actual: %s, expected %s\n", __func__, test->filename,
210 signature_alg_string, expected_string);
216 X509_free(certificate);
221 int Test_x509_utils(
int argc,
char* argv[])
223 char* cert_path = certificate_path(
"Test_x509_cert_info.pem");
228 ret = TestCertificateFile(cert_path, certificate_tests, ARRAYSIZE(certificate_tests));
233 for (
size_t i = 0; i < ARRAYSIZE(signature_alg_tests); i++)
235 ret = TestSignatureAlgorithm(&signature_alg_tests[i]);