FreeRDP
Loading...
Searching...
No Matches
TestMakecertCommonName.c
1#include <stdio.h>
2#include <string.h>
3
4#include <openssl/pem.h>
5#include <openssl/x509.h>
6
7#include <winpr/rpc.h>
8#include <winpr/wtypes.h>
9#include <winpr/file.h>
10#include <winpr/path.h>
11#include <winpr/sysinfo.h>
12#include <winpr/thread.h>
13#include <winpr/tools/makecert.h>
14
15/* One entry of the certificate's subject or issuer, empty when it has none. */
16static void name_entry(const X509_NAME* name, int nid, char* text, size_t size)
17{
18 text[0] = '\0';
19 if (name && (size <= INT32_MAX) && (X509_NAME_get_text_by_NID(name, nid, text, (int)size) < 0))
20 text[0] = '\0';
21}
22
23/* Makes a certificate with these arguments in dir, and reads its subject and issuer back. */
24WINPR_ATTR_NODISCARD
25static BOOL make(const char* dir, const char* file, char** argv, int argc, char* cn, char* issuerCN,
26 char* org, size_t size)
27{
28 BOOL rc = FALSE;
29 char* path = nullptr;
30 BIO* bio = nullptr;
31 X509* x509 = nullptr;
32 MAKECERT_CONTEXT* makecert = makecert_context_new();
33 if (!makecert)
34 goto fail;
35 if (makecert_context_process(makecert, argc, argv) < 0)
36 goto fail;
37 if (makecert_context_set_output_file_name(makecert, file) != 1)
38 goto fail;
39 if (makecert_context_output_certificate_file(makecert, dir) != 1)
40 goto fail;
41
42 path = GetCombinedPathV(dir, "%s.crt", file);
43 if (!path)
44 goto fail;
45 bio = BIO_new_file(path, "r");
46 if (!bio)
47 goto fail;
48 x509 = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr);
49 if (!x509)
50 goto fail;
51
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);
55 rc = TRUE;
56fail:
57 X509_free(x509);
58 /* close the file before deleting it: Windows does not delete a file that is still open */
59 BIO_free(bio);
60 if (path)
61 (void)winpr_DeleteFile(path);
62 free(path);
63 makecert_context_free(makecert);
64 return rc;
65}
66
67WINPR_ATTR_NODISCARD
68static BOOL check(const char* what, const char* dir, char** argv, int argc, const char* expectedCN,
69 const char* expectedOrg)
70{
71 char cn[256] = { 0 };
72 char issuerCN[256] = { 0 };
73 char org[256] = { 0 };
74 if (!make(dir, "TestMakecertCommonName", argv, argc, cn, issuerCN, org, sizeof(cn)))
75 {
76 (void)fprintf(stderr, "%s: no certificate\n", what);
77 return FALSE;
78 }
79
80 const BOOL ok = (strcmp(cn, expectedCN) == 0) && (strcmp(issuerCN, expectedCN) == 0) &&
81 (strcmp(org, expectedOrg) == 0);
82 if (!ok)
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);
85 return ok;
86}
87
88WINPR_ATTR_NODISCARD
89static BOOL refused(const char* what, const char* dir, char** argv, int argc)
90{
91 char cn[256] = { 0 };
92 char issuerCN[256] = { 0 };
93 char org[256] = { 0 };
94 if (make(dir, "TestMakecertCommonName", argv, argc, cn, issuerCN, org, sizeof(cn)))
95 {
96 (void)fprintf(stderr, "%s: a certificate with CN '%s', expected none\n", what, cn);
97 return FALSE;
98 }
99 return TRUE;
100}
101
102/* makecert's own default name: the host's DNS name, else its NetBIOS name */
103WINPR_ATTR_NODISCARD
104static BOOL host_name(char* name, DWORD size)
105{
106 DWORD length = size;
107 if (GetComputerNameExA(ComputerNamePhysicalDnsFullyQualified, name, &length) && (length > 0))
108 return TRUE;
109 length = size;
110 return GetComputerNameExA(ComputerNamePhysicalNetBIOS, name, &length) && (length > 0);
111}
112
113static const char* guid2str(const GUID* guid, char* buffer, size_t len)
114{
115 if (!guid)
116 return nullptr;
117 RPC_CSTR strguid = nullptr;
118
119 RPC_STATUS rpcStatus = UuidToStringA(guid, &strguid);
120
121 if (rpcStatus != RPC_S_OK)
122 return nullptr;
123
124 (void)sprintf_s(buffer, len, "%s", strguid);
125 RpcStringFreeA(&strguid);
126 return buffer;
127}
128
129int TestMakecertCommonName(int argc, char* argv[])
130{
131 WINPR_UNUSED(argc);
132 WINPR_UNUSED(argv);
133
134 int rc = -1;
135 char host[256] = WINPR_C_ARRAY_INIT;
136 char* dir = nullptr;
137 if (!host_name(host, sizeof(host)))
138 {
139 (void)fprintf(stderr, "%s: host %s: failed to create\n", host);
140 goto fail;
141 }
142
143 GUID uuid = WINPR_C_ARRAY_INIT;
144 if (UuidCreate(&uuid) != RPC_S_OK)
145 goto fail;
146
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))
151 {
152 (void)fprintf(stderr, "%s: test directory %s: failed to create\n", __func__, dir);
153 goto fail;
154 }
155
156 {
157 /* as the shadow server makes its certificate: no -n, the host name */
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" };
161 /* a common name holds 64 characters: a cloud machine's long name keeps its host name */
162 char* longName[] = {
163 "makecert", "-rdp",
164 "-live", "-silent",
165 "-n", "fv-az123-456.abcdefghijklmnopqrstuvwxyz0123.bx.internal.cloudapp.net"
166 };
167 /* a label holds 63 characters: a longer first label is no host name, an error */
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 };
174
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));
181 /* no name at all: a certificate without a common name, as before */
182 char* empty[] = { "makecert", "-rdp", "-live", "-silent", "-n", "" };
183 ok &= check("empty name", dir, empty, (int)ARRAYSIZE(empty), "", "");
184 if (ok)
185 rc = 0;
186 }
187
188fail:
189 if (dir)
190 (void)winpr_RemoveDirectory(dir);
191 free(dir);
192 return rc;
193}