FreeRDP
Loading...
Searching...
No Matches
TestFuzzServerCerts.h
1
24#pragma once
25
26#include <winpr/wtypes.h>
27#include <winpr/path.h>
28#include <winpr/tools/makecert.h>
29
30static const char testCredentialsName[] = "TestFuzzServerCerts";
31
32WINPR_ATTR_NODISCARD
33static inline BOOL server_create_certificate(const char* filepath, const char* name)
34{
35 BOOL rc = FALSE;
36 char* makecert_argv[6] = { "makecert", "-rdp", "-live", "-silent", "-y", "5" };
37
38 if (!winpr_PathFileExists(filepath))
39 {
40 if (!winpr_PathMakePath(filepath, nullptr))
41 return FALSE;
42 }
43
44 WINPR_STATIC_ASSERT(ARRAYSIZE(makecert_argv) <= INT_MAX);
45 const size_t makecert_argc = ARRAYSIZE(makecert_argv);
46
47 char* fcert = GetCombinedPathV(filepath, "%s.%s", name, "crt");
48 char* fkey = GetCombinedPathV(filepath, "%s.%s", name, "key");
49 const BOOL res = winpr_PathFileExists(fcert) && winpr_PathFileExists(fkey);
50 free(fcert);
51 free(fkey);
52 if (res)
53 return TRUE;
54
55 MAKECERT_CONTEXT* makecert = makecert_context_new();
56
57 if (!makecert)
58 goto out_fail;
59
60 if (makecert_context_process(makecert, (int)makecert_argc, makecert_argv) < 0)
61 goto out_fail;
62
63 if (makecert_context_set_output_file_name(makecert, name) != 1)
64 goto out_fail;
65
66 WINPR_ASSERT(filepath);
67 if (makecert_context_output_certificate_file(makecert, filepath) != 1)
68 goto out_fail;
69
70 if (makecert_context_output_private_key_file(makecert, filepath) != 1)
71 goto out_fail;
72
73 rc = TRUE;
74out_fail:
75 makecert_context_free(makecert);
76 return rc;
77}
78
79WINPR_ATTR_MALLOC(free, 1)
80static inline char* getTestCredentialsFilePath(void)
81{
82 char* path = GetKnownSubPath(KNOWN_PATH_TEMP, testCredentialsName);
83 if (!path)
84 return nullptr;
85 if (!server_create_certificate(path, testCredentialsName))
86 {
87 free(path);
88 return nullptr;
89 }
90 return path;
91}
92
93WINPR_ATTR_MALLOC(free, 1)
94static inline char* getTestCredentialsFileNameFor(const char* ext)
95{
96 if (!ext)
97 return nullptr;
98
99 char* path = getTestCredentialsFilePath();
100 if (!path)
101 return nullptr;
102
103 char* name = GetCombinedPathV(path, "%s.%s", testCredentialsName, ext);
104 free(path);
105 return name;
106}