25 #include <freerdp/config.h>
27 #include <winpr/assert.h>
28 #include <winpr/path.h>
30 #include <freerdp/settings.h>
32 #include <freerdp/crypto/crypto.h>
33 #include <freerdp/crypto/certificate_data.h>
35 #include "certificate.h"
37 struct rdp_certificate_data
43 char cached_hash[MAX_PATH + 10];
46 char* cached_fingerprint;
48 char* cached_pem_chain;
54 static char* ensure_lowercase(
char* str,
size_t length)
56 const size_t len = strnlen(str, length);
57 for (
size_t x = 0; x < len; x++)
58 str[x] = (
char)tolower(str[x]);
61 static const char* freerdp_certificate_data_hash_(
const char* hostname, UINT16 port,
char* name,
64 (void)_snprintf(name, length,
"%s_%" PRIu16
".pem", hostname, port);
65 return ensure_lowercase(name, length);
68 static BOOL freerdp_certificate_data_load_cache(rdpCertificateData* data)
74 freerdp_certificate_data_hash_(data->hostname, data->port, data->cached_hash,
75 sizeof(data->cached_hash));
76 if (strnlen(data->cached_hash,
sizeof(data->cached_hash)) == 0)
79 data->cached_subject = freerdp_certificate_get_subject(data->cert);
80 if (!data->cached_subject)
81 data->cached_subject = calloc(1, 1);
84 data->cached_pem = freerdp_certificate_get_pem_ex(data->cert, &pemlen, FALSE);
85 if (!data->cached_pem)
88 size_t pemchainlen = 0;
89 data->cached_pem_chain = freerdp_certificate_get_pem_ex(data->cert, &pemchainlen, TRUE);
90 if (!data->cached_pem_chain)
93 data->cached_fingerprint = freerdp_certificate_get_fingerprint(data->cert);
94 if (!data->cached_fingerprint)
97 data->cached_issuer = freerdp_certificate_get_issuer(data->cert);
98 if (!data->cached_issuer)
99 data->cached_issuer = calloc(1, 1);
106 static rdpCertificateData* freerdp_certificate_data_new_nocopy(
const char* hostname, UINT16 port,
107 rdpCertificate* xcert)
109 rdpCertificateData* certdata = NULL;
111 if (!hostname || !xcert)
114 certdata = (rdpCertificateData*)calloc(1,
sizeof(rdpCertificateData));
119 certdata->port = port;
120 certdata->hostname = _strdup(hostname);
121 if (!certdata->hostname)
123 ensure_lowercase(certdata->hostname, strlen(certdata->hostname));
125 certdata->cert = xcert;
126 if (!freerdp_certificate_data_load_cache(certdata))
128 certdata->cert = NULL;
134 freerdp_certificate_data_free(certdata);
138 rdpCertificateData* freerdp_certificate_data_new(
const char* hostname, UINT16 port,
139 const rdpCertificate* xcert)
141 rdpCertificate* copy = freerdp_certificate_clone(xcert);
142 rdpCertificateData* data = freerdp_certificate_data_new_nocopy(hostname, port, copy);
144 freerdp_certificate_free(copy);
148 rdpCertificateData* freerdp_certificate_data_new_from_pem(
const char* hostname, UINT16 port,
149 const char* pem,
size_t length)
151 if (!pem || (length == 0))
154 rdpCertificate* cert = freerdp_certificate_new_from_pem(pem);
155 rdpCertificateData* data = freerdp_certificate_data_new_nocopy(hostname, port, cert);
157 freerdp_certificate_free(cert);
161 rdpCertificateData* freerdp_certificate_data_new_from_file(
const char* hostname, UINT16 port,
167 rdpCertificate* cert = freerdp_certificate_new_from_file(file);
168 rdpCertificateData* data = freerdp_certificate_data_new_nocopy(hostname, port, cert);
170 freerdp_certificate_free(cert);
174 void freerdp_certificate_data_free(rdpCertificateData* data)
179 free(data->hostname);
180 freerdp_certificate_free(data->cert);
181 free(data->cached_subject);
182 free(data->cached_issuer);
183 free(data->cached_fingerprint);
184 free(data->cached_pem);
185 free(data->cached_pem_chain);
190 const char* freerdp_certificate_data_get_host(
const rdpCertificateData* cert)
194 return cert->hostname;
197 UINT16 freerdp_certificate_data_get_port(
const rdpCertificateData* cert)
204 const char* freerdp_certificate_data_get_pem(
const rdpCertificateData* cert)
206 return freerdp_certificate_data_get_pem_ex(cert, TRUE);
209 const char* freerdp_certificate_data_get_pem_ex(
const rdpCertificateData* cert, BOOL withFullChain)
214 return cert->cached_pem_chain;
215 return cert->cached_pem;
218 const char* freerdp_certificate_data_get_subject(
const rdpCertificateData* cert)
223 return cert->cached_subject;
226 const char* freerdp_certificate_data_get_issuer(
const rdpCertificateData* cert)
231 return cert->cached_issuer;
233 const char* freerdp_certificate_data_get_fingerprint(
const rdpCertificateData* cert)
238 return cert->cached_fingerprint;
241 BOOL freerdp_certificate_data_equal(
const rdpCertificateData* a,
const rdpCertificateData* b)
248 if (strcmp(a->hostname, b->hostname) != 0)
250 if (a->port != b->port)
253 const char* pem1 = freerdp_certificate_data_get_fingerprint(a);
254 const char* pem2 = freerdp_certificate_data_get_fingerprint(b);
256 rc = strcmp(pem1, pem2) == 0;
263 const char* freerdp_certificate_data_get_hash(
const rdpCertificateData* cert)
268 return cert->cached_hash;
271 char* freerdp_certificate_data_hash(
const char* hostname, UINT16 port)
273 char name[MAX_PATH + 10] = { 0 };
274 freerdp_certificate_data_hash_(hostname, port, name,
sizeof(name));
275 return _strdup(name);