4#include <openssl/pem.h>
5#include <openssl/x509.h>
8#include <winpr/wtypes.h>
10#include <winpr/path.h>
11#include <winpr/sysinfo.h>
12#include <winpr/thread.h>
13#include <winpr/tools/makecert.h>
16static void name_entry(
const X509_NAME* name,
int nid,
char* text,
size_t size)
19 if (name && (size <= INT32_MAX) && (X509_NAME_get_text_by_NID(name, nid, text, (
int)size) < 0))
25static BOOL make(
const char* dir,
const char* file,
char** argv,
int argc,
char* cn,
char* issuerCN,
26 char* org,
size_t size)
32 MAKECERT_CONTEXT* makecert = makecert_context_new();
35 if (makecert_context_process(makecert, argc, argv) < 0)
37 if (makecert_context_set_output_file_name(makecert, file) != 1)
39 if (makecert_context_output_certificate_file(makecert, dir) != 1)
42 path = GetCombinedPathV(dir,
"%s.crt", file);
45 bio = BIO_new_file(path,
"r");
48 x509 = PEM_read_bio_X509(bio,
nullptr,
nullptr,
nullptr);
52 name_entry(X509_get_subject_name(x509), NID_commonName, cn, size);
53 name_entry(X509_get_issuer_name(x509), NID_commonName, issuerCN, size);
54 name_entry(X509_get_subject_name(x509), NID_organizationName, org, size);
61 (void)winpr_DeleteFile(path);
63 makecert_context_free(makecert);
68static BOOL check(
const char* what,
const char* dir,
char** argv,
int argc,
const char* expectedCN,
69 const char* expectedOrg)
72 char issuerCN[256] = { 0 };
73 char org[256] = { 0 };
74 if (!make(dir,
"TestMakecertCommonName", argv, argc, cn, issuerCN, org,
sizeof(cn)))
76 (void)fprintf(stderr,
"%s: no certificate\n", what);
80 const BOOL ok = (strcmp(cn, expectedCN) == 0) && (strcmp(issuerCN, expectedCN) == 0) &&
81 (strcmp(org, expectedOrg) == 0);
83 (void)fprintf(stderr,
"%s: CN '%s' issuer CN '%s' O '%s', expected CN '%s' O '%s'\n", what,
84 cn, issuerCN, org, expectedCN, expectedOrg);
89static BOOL refused(
const char* what,
const char* dir,
char** argv,
int argc)
92 char issuerCN[256] = { 0 };
93 char org[256] = { 0 };
94 if (make(dir,
"TestMakecertCommonName", argv, argc, cn, issuerCN, org,
sizeof(cn)))
96 (void)fprintf(stderr,
"%s: a certificate with CN '%s', expected none\n", what, cn);
104static BOOL host_name(
char* name, DWORD size)
107 if (GetComputerNameExA(ComputerNamePhysicalDnsFullyQualified, name, &length) && (length > 0))
110 return GetComputerNameExA(ComputerNamePhysicalNetBIOS, name, &length) && (length > 0);
113static const char* guid2str(
const GUID* guid,
char* buffer,
size_t len)
117 RPC_CSTR strguid =
nullptr;
119 RPC_STATUS rpcStatus = UuidToStringA(guid, &strguid);
121 if (rpcStatus != RPC_S_OK)
124 (void)sprintf_s(buffer, len,
"%s", strguid);
125 RpcStringFreeA(&strguid);
129int TestMakecertCommonName(
int argc,
char* argv[])
135 char host[256] = WINPR_C_ARRAY_INIT;
137 if (!host_name(host,
sizeof(host)))
139 (void)fprintf(stderr,
"%s: host %s: failed to create\n", host);
143 GUID uuid = WINPR_C_ARRAY_INIT;
144 if (UuidCreate(&uuid) != RPC_S_OK)
147 char strhash[
sizeof(uuid) * 2 + 8] = WINPR_C_ARRAY_INIT;
148 dir = GetKnownSubPathV(KNOWN_PATH_TEMP,
"TestMakecertCommonName-%s",
149 guid2str(&uuid, strhash,
sizeof(strhash)));
150 if (!dir || !winpr_PathMakePath(dir,
nullptr))
152 (void)fprintf(stderr,
"%s: test directory %s: failed to create\n", __func__, dir);
158 char* noName[] = {
"makecert",
"-rdp",
"-live",
"-silent",
"-y",
"1" };
159 char* plain[] = {
"makecert",
"-rdp",
"-live",
"-silent",
"-n",
"myhost" };
160 char* records[] = {
"makecert",
"-rdp",
"-live",
"-silent",
"-n",
"CN=abc,O=def" };
165 "-n",
"fv-az123-456.abcdefghijklmnopqrstuvwxyz0123.bx.internal.cloudapp.net"
168 char longLabel[71] = { 0 };
169 memset(longLabel,
'x',
sizeof(longLabel) - 1);
170 char* oneLabel[] = {
"makecert",
"-rdp",
"-live",
"-silent",
"-n", longLabel };
171 char longFirst[64 +
sizeof(
".example.com")] = { 0 };
172 (void)_snprintf(longFirst,
sizeof(longFirst),
"%.64s.example.com", longLabel);
173 char* firstLabel[] = {
"makecert",
"-rdp",
"-live",
"-silent",
"-n", longFirst };
175 BOOL ok = check(
"no -n", dir, noName, (
int)ARRAYSIZE(noName), host,
"");
176 ok &= check(
"-n myhost", dir, plain, (
int)ARRAYSIZE(plain),
"myhost",
"");
177 ok &= check(
"-n CN=abc,O=def", dir, records, (
int)ARRAYSIZE(records),
"abc",
"def");
178 ok &= check(
"long name", dir, longName, (
int)ARRAYSIZE(longName),
"fv-az123-456",
"");
179 ok &= refused(
"long label", dir, oneLabel, (
int)ARRAYSIZE(oneLabel));
180 ok &= refused(
"long first label", dir, firstLabel, (
int)ARRAYSIZE(firstLabel));
182 char* empty[] = {
"makecert",
"-rdp",
"-live",
"-silent",
"-n",
"" };
183 ok &= check(
"empty name", dir, empty, (
int)ARRAYSIZE(empty),
"",
"");
190 (void)winpr_RemoveDirectory(dir);