/src/FreeRDP/libfreerdp/crypto/x509_utils.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Cryptographic Abstraction Layer |
4 | | * |
5 | | * Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2023 Armin Novak <anovak@thincast.com> |
7 | | * Copyright 2023 Thincast Technologies GmbH |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <ctype.h> |
23 | | |
24 | | #include <openssl/objects.h> |
25 | | #include <openssl/x509v3.h> |
26 | | #include <openssl/pem.h> |
27 | | #include <openssl/rsa.h> |
28 | | #include <openssl/err.h> |
29 | | |
30 | | #include <freerdp/config.h> |
31 | | |
32 | | #include <winpr/crt.h> |
33 | | #include <winpr/string.h> |
34 | | #include <winpr/assert.h> |
35 | | |
36 | | #include <freerdp/log.h> |
37 | | |
38 | | #include "x509_utils.h" |
39 | | |
40 | | #define TAG FREERDP_TAG("crypto") |
41 | | |
42 | | BYTE* x509_utils_get_hash(const X509* xcert, const char* hash, size_t* length) |
43 | 0 | { |
44 | 0 | UINT32 fp_len = EVP_MAX_MD_SIZE; |
45 | 0 | BYTE* fp = nullptr; |
46 | 0 | const EVP_MD* md = EVP_get_digestbyname(hash); |
47 | 0 | if (!md) |
48 | 0 | { |
49 | 0 | WLog_ERR(TAG, "System does not support %s hash!", hash); |
50 | 0 | return nullptr; |
51 | 0 | } |
52 | 0 | if (!xcert || !length) |
53 | 0 | { |
54 | 0 | WLog_ERR(TAG, "Invalid arguments: xcert=%p, length=%p", |
55 | 0 | WINPR_CXX_COMPAT_CAST(const void*, xcert), |
56 | 0 | WINPR_CXX_COMPAT_CAST(const void*, length)); |
57 | 0 | return nullptr; |
58 | 0 | } |
59 | | |
60 | 0 | fp = calloc(fp_len + 1, sizeof(BYTE)); |
61 | 0 | if (!fp) |
62 | 0 | { |
63 | 0 | WLog_ERR(TAG, "could not allocate %" PRIu32 " bytes", fp_len); |
64 | 0 | return nullptr; |
65 | 0 | } |
66 | | |
67 | 0 | if (X509_digest(xcert, md, fp, &fp_len) != 1) |
68 | 0 | { |
69 | 0 | free(fp); |
70 | 0 | WLog_ERR(TAG, "certificate does not have a %s hash!", hash); |
71 | 0 | return nullptr; |
72 | 0 | } |
73 | | |
74 | 0 | *length = fp_len; |
75 | 0 | return fp; |
76 | 0 | } |
77 | | |
78 | | BYTE* x509_utils_get_hashless_signature_hash(const X509* xcert, size_t* length) |
79 | 0 | { |
80 | 0 | WINPR_ASSERT(xcert); |
81 | 0 | WINPR_ASSERT(length); |
82 | | |
83 | | /* RFC 5929 section 4.1 leaves tls-server-end-point undefined when the certificate's |
84 | | * signatureAlgorithm uses no hash (Ed25519, Ed448, ML-DSA, SLH-DSA). Use the hash |
85 | | * X509_digest_sig() picks for these: SHA-512 for Ed25519 (the RFC 8419 default), SHA-256 |
86 | | * for ML-DSA and SLH-DSA. Ed448 (SHAKE256) gives no result with OpenSSL 3.5, which needs an |
87 | | * output length for it. RSA-PSS and the algorithms with a hash are not handled here. */ |
88 | | #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) && !defined(LIBRESSL_VERSION_NUMBER) |
89 | | const int nid = X509_get_signature_nid(xcert); |
90 | | int md_nid = NID_undef; |
91 | | int pk_nid = NID_undef; |
92 | | if ((nid == NID_rsassaPss) || (OBJ_find_sigid_algs(nid, &md_nid, &pk_nid) != 1) || |
93 | | (md_nid != NID_undef) || (pk_nid == NID_undef)) |
94 | | return nullptr; |
95 | | |
96 | | int fallback = 0; |
97 | | BYTE* hash = nullptr; |
98 | | ASN1_OCTET_STRING* digest = X509_digest_sig(xcert, nullptr, &fallback); |
99 | | const int len = digest ? ASN1_STRING_length(digest) : 0; |
100 | | if (fallback && (len > 0)) |
101 | | { |
102 | | hash = calloc((size_t)len + 1, sizeof(BYTE)); |
103 | | if (hash) |
104 | | { |
105 | | memcpy(hash, ASN1_STRING_get0_data(digest), (size_t)len); |
106 | | *length = (size_t)len; |
107 | | } |
108 | | } |
109 | | ASN1_OCTET_STRING_free(digest); |
110 | | return hash; |
111 | | #else |
112 | 0 | WINPR_UNUSED(xcert); |
113 | 0 | WINPR_UNUSED(length); |
114 | 0 | return nullptr; |
115 | 0 | #endif |
116 | 0 | } |
117 | | |
118 | | WINPR_ATTR_NODISCARD |
119 | | static char* crypto_print_name(const X509_NAME* name) |
120 | 0 | { |
121 | 0 | char* buffer = nullptr; |
122 | 0 | BIO* outBIO = BIO_new(BIO_s_mem()); |
123 | 0 | if (!outBIO) |
124 | 0 | return nullptr; |
125 | | |
126 | 0 | if (X509_NAME_print_ex(outBIO, name, 0, XN_FLAG_ONELINE) > 0) |
127 | 0 | buffer = x509_utils_bio_read(outBIO, nullptr); |
128 | |
|
129 | 0 | BIO_free_all(outBIO); |
130 | 0 | return buffer; |
131 | 0 | } |
132 | | |
133 | | char* x509_utils_get_subject(const X509* xcert) |
134 | 0 | { |
135 | 0 | char* subject = nullptr; |
136 | 0 | if (!xcert) |
137 | 0 | { |
138 | 0 | WLog_ERR(TAG, "Invalid certificate nullptr"); |
139 | 0 | return nullptr; |
140 | 0 | } |
141 | 0 | subject = crypto_print_name(X509_get_subject_name(xcert)); |
142 | 0 | if (!subject) |
143 | 0 | WLog_WARN(TAG, "certificate does not have a subject!"); |
144 | 0 | return subject; |
145 | 0 | } |
146 | | |
147 | | /* GENERAL_NAME type labels */ |
148 | | |
149 | | static const char* general_name_type_labels[] = { "OTHERNAME", "EMAIL ", "DNS ", |
150 | | "X400 ", "DIRNAME ", "EDIPARTY ", |
151 | | "URI ", "IPADD ", "RID " }; |
152 | | |
153 | | WINPR_ATTR_NODISCARD |
154 | | static const char* general_name_type_label(int general_name_type) |
155 | 0 | { |
156 | 0 | if ((0 <= general_name_type) && |
157 | 0 | ((size_t)general_name_type < ARRAYSIZE(general_name_type_labels))) |
158 | 0 | { |
159 | 0 | return general_name_type_labels[general_name_type]; |
160 | 0 | } |
161 | 0 | else |
162 | 0 | { |
163 | 0 | static char buffer[80] = WINPR_C_ARRAY_INIT; |
164 | 0 | (void)snprintf(buffer, sizeof(buffer), "Unknown general name type (%d)", general_name_type); |
165 | 0 | return buffer; |
166 | 0 | } |
167 | 0 | } |
168 | | |
169 | | /* |
170 | | |
171 | | map_subject_alt_name(x509, general_name_type, mapper, data) |
172 | | |
173 | | Call the function mapper with subjectAltNames found in the x509 |
174 | | certificate and data. if generate_name_type is GEN_ALL, the the |
175 | | mapper is called for all the names, else it's called only for names |
176 | | of the given type. |
177 | | |
178 | | |
179 | | We implement two extractors: |
180 | | |
181 | | - a string extractor that can be used to get the subjectAltNames of |
182 | | the following types: GEN_URI, GEN_DNS, GEN_EMAIL |
183 | | |
184 | | - a ASN1_OBJECT filter/extractor that can be used to get the |
185 | | subjectAltNames of OTHERNAME type. |
186 | | |
187 | | Note: usually, it's a string, but some type of otherNames can be |
188 | | associated with different classes of objects. eg. a KPN may be a |
189 | | sequence of realm and principal name, instead of a single string |
190 | | object. |
191 | | |
192 | | Not implemented yet: extractors for the types: GEN_X400, GEN_DIRNAME, |
193 | | GEN_EDIPARTY, GEN_RID, GEN_IPADD (the later can contain nul-bytes). |
194 | | |
195 | | |
196 | | mapper(name, data, index, count) |
197 | | |
198 | | The mapper is passed: |
199 | | - the GENERAL_NAME selected, |
200 | | - the data, |
201 | | - the index of the general name in the subjectAltNames, |
202 | | - the total number of names in the subjectAltNames. |
203 | | |
204 | | The last parameter let's the mapper allocate arrays to collect objects. |
205 | | Note: if names are filtered, not all the indices from 0 to count-1 are |
206 | | passed to mapper, only the indices selected. |
207 | | |
208 | | When the mapper returns 0, map_subject_alt_name stops the iteration immediately. |
209 | | |
210 | | */ |
211 | | |
212 | 0 | #define GEN_ALL (-1) |
213 | | |
214 | | typedef int (*general_name_mapper_pr)(const X509* x509, GENERAL_NAME* name, void* data, int index, |
215 | | int count); |
216 | | |
217 | | static void map_subject_alt_name(const X509* x509, int general_name_type, |
218 | | general_name_mapper_pr mapper, void* data) |
219 | 0 | { |
220 | 0 | STACK_OF(GENERAL_NAME)* gens = X509_get_ext_d2i(x509, NID_subject_alt_name, nullptr, nullptr); |
221 | |
|
222 | 0 | if (!gens) |
223 | 0 | return; |
224 | | |
225 | 0 | const int num = sk_GENERAL_NAME_num(gens); |
226 | |
|
227 | 0 | for (int i = 0; (i < num); i++) |
228 | 0 | { |
229 | 0 | GENERAL_NAME* name = sk_GENERAL_NAME_value(gens, i); |
230 | |
|
231 | 0 | if (name) |
232 | 0 | { |
233 | 0 | if ((general_name_type == GEN_ALL) || (general_name_type == name->type)) |
234 | 0 | { |
235 | 0 | if (!mapper(x509, name, data, i, num)) |
236 | 0 | { |
237 | 0 | break; |
238 | 0 | } |
239 | 0 | } |
240 | 0 | } |
241 | 0 | } |
242 | |
|
243 | 0 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
244 | 0 | } |
245 | | |
246 | | /* |
247 | | extract_string -- string extractor |
248 | | |
249 | | - the strings array is allocated lazily, when we first have to store a |
250 | | string. |
251 | | |
252 | | - allocated contains the size of the strings array, or -1 if |
253 | | allocation failed. |
254 | | |
255 | | - count contains the actual count of strings in the strings array. |
256 | | |
257 | | - maximum limits the number of strings we can store in the strings |
258 | | array: beyond, the extractor returns 0 to short-cut the search. |
259 | | |
260 | | extract_string stores in the string list OPENSSL strings, |
261 | | that must be freed with OPENSSL_free. |
262 | | |
263 | | */ |
264 | | |
265 | | typedef struct string_list |
266 | | { |
267 | | char** strings; |
268 | | size_t* lengths; |
269 | | size_t allocated; |
270 | | size_t count; |
271 | | size_t maximum; |
272 | | } string_list; |
273 | | |
274 | | static string_list string_list_initialize(void) |
275 | 0 | { |
276 | 0 | const string_list empty = { |
277 | 0 | .strings = nullptr, .lengths = nullptr, .allocated = 0, .count = 0, .maximum = INT_MAX |
278 | 0 | }; |
279 | 0 | return empty; |
280 | 0 | } |
281 | | |
282 | | static BOOL string_list_allocate(string_list* list, size_t allocate_count) |
283 | 0 | { |
284 | 0 | WINPR_ASSERT(list); |
285 | 0 | if (!list->strings && (list->allocated == 0) && (allocate_count > 0)) |
286 | 0 | { |
287 | 0 | list->strings = (char**)calloc(allocate_count, sizeof(char*)); |
288 | 0 | list->lengths = calloc(allocate_count, sizeof(size_t)); |
289 | 0 | list->count = 0; |
290 | 0 | if (!list->strings || !list->lengths) |
291 | 0 | { |
292 | 0 | free((void*)list->strings); |
293 | 0 | free(list->lengths); |
294 | 0 | list->strings = nullptr; |
295 | 0 | list->lengths = nullptr; |
296 | 0 | return FALSE; |
297 | 0 | } |
298 | 0 | list->allocated = allocate_count; |
299 | 0 | } |
300 | 0 | return TRUE; |
301 | 0 | } |
302 | | |
303 | | static void string_list_free(string_list* list) |
304 | 0 | { |
305 | | /* Note: we don't free the contents of the strings array: this */ |
306 | | /* is handled by the caller, either by returning this */ |
307 | | /* content, or freeing it itself. */ |
308 | 0 | free((void*)list->strings); |
309 | 0 | free(list->lengths); |
310 | 0 | } |
311 | | |
312 | | WINPR_ATTR_NODISCARD |
313 | | static BOOL check_string_is_email(WINPR_ATTR_UNUSED const X509* x509, const unsigned char* ustr, |
314 | | size_t length) |
315 | 0 | { |
316 | 0 | const size_t MAX_EMAIL_LENGTH = 256; |
317 | 0 | const size_t MIN_EMAIL_LENGTH = 5; |
318 | |
|
319 | 0 | if (ustr == nullptr) |
320 | 0 | return FALSE; |
321 | | |
322 | 0 | const char* email = (const char*)ustr; |
323 | 0 | const size_t len = strnlen(email, length); |
324 | 0 | if ((len < MIN_EMAIL_LENGTH) || (len > MAX_EMAIL_LENGTH)) |
325 | 0 | return FALSE; |
326 | | |
327 | 0 | size_t at_pos = 0; |
328 | 0 | size_t at_count = 0; |
329 | |
|
330 | 0 | for (size_t i = 0; i < len; i++) |
331 | 0 | { |
332 | 0 | char cur = email[i]; |
333 | 0 | if (cur == '@') |
334 | 0 | { |
335 | | /* @ must not be first or last */ |
336 | 0 | if (i == 0) |
337 | 0 | return FALSE; |
338 | 0 | if (i == len - 1) |
339 | 0 | return FALSE; |
340 | 0 | at_pos = i; |
341 | 0 | at_count++; |
342 | 0 | } |
343 | 0 | if (isspace(cur)) |
344 | 0 | return FALSE; |
345 | 0 | } |
346 | | |
347 | | /* only one @ allowed */ |
348 | 0 | if (at_count != 1) |
349 | 0 | { |
350 | 0 | return FALSE; |
351 | 0 | } |
352 | | |
353 | | /* local part */ |
354 | 0 | if ((email[0] == '.') || (email[at_pos - 1] == '.')) |
355 | 0 | return FALSE; |
356 | | |
357 | | /* .. forbidden */ |
358 | 0 | for (size_t i = 0; i < at_pos - 1; i++) |
359 | 0 | { |
360 | 0 | if ((email[i] == '.') && (email[i + 1] == '.')) |
361 | 0 | return FALSE; |
362 | 0 | } |
363 | | |
364 | | // Validate the domain part (after '@') |
365 | 0 | const char* domain = &email[at_pos + 1]; |
366 | 0 | size_t domain_len = strnlen(domain, len); |
367 | |
|
368 | 0 | if (!winpr_str_is_valid_hostnameN(domain, domain_len)) |
369 | 0 | return FALSE; |
370 | | |
371 | | /* local part */ |
372 | 0 | for (size_t i = 0; i < at_pos; i++) |
373 | 0 | { |
374 | 0 | if (!isalnum(email[i]) && email[i] != '.' && email[i] != '-' && email[i] != '_' && |
375 | 0 | email[i] != '+') |
376 | 0 | { |
377 | 0 | return FALSE; |
378 | 0 | } |
379 | 0 | } |
380 | | |
381 | 0 | return TRUE; |
382 | 0 | } |
383 | | |
384 | | WINPR_ATTR_NODISCARD |
385 | | static BOOL check_string_is_host_or_ip(WINPR_ATTR_UNUSED const X509* x509, |
386 | | const unsigned char* ustr, size_t length) |
387 | 0 | { |
388 | 0 | const char* str = (const char*)ustr; |
389 | 0 | if (strnlen(str, length) != length) |
390 | 0 | return FALSE; |
391 | 0 | return winpr_str_is_valid_hostnameN(str, length); |
392 | 0 | } |
393 | | |
394 | | WINPR_ATTR_NODISCARD |
395 | | static BOOL check_string_is_dns_name(const X509* x509, const unsigned char* ustr, size_t length) |
396 | 0 | { |
397 | | /* RFC 6125 wildcard DNS names (e.g. *.example.com): validate the part after |
398 | | * the leading label; tls_match_hostname applies the single-label rule. |
399 | | * Like OpenSSL, require at least two non-empty labels after the wildcard, |
400 | | * so *.com, *..com and *.com. are rejected. */ |
401 | 0 | if ((length > 2) && (ustr[0] == '*') && (ustr[1] == '.')) |
402 | 0 | { |
403 | 0 | const unsigned char* suffix = &ustr[2]; |
404 | 0 | const size_t suffixLength = length - 2; |
405 | 0 | size_t dots = 0; |
406 | |
|
407 | 0 | if ((suffix[0] == '.') || (suffix[suffixLength - 1] == '.')) |
408 | 0 | return FALSE; |
409 | | |
410 | 0 | for (size_t x = 1; x < suffixLength; x++) |
411 | 0 | { |
412 | 0 | if (suffix[x] == '.') |
413 | 0 | { |
414 | 0 | if (suffix[x - 1] == '.') |
415 | 0 | return FALSE; |
416 | 0 | dots++; |
417 | 0 | } |
418 | 0 | } |
419 | | |
420 | 0 | if (dots == 0) |
421 | 0 | return FALSE; |
422 | 0 | return check_string_is_host_or_ip(x509, suffix, suffixLength); |
423 | 0 | } |
424 | 0 | return check_string_is_host_or_ip(x509, ustr, length); |
425 | 0 | } |
426 | | |
427 | | WINPR_ATTR_NODISCARD |
428 | | static BOOL check_string_is_host_or_ip_or_email(WINPR_ATTR_UNUSED const X509* x509, |
429 | | const unsigned char* ustr, size_t length) |
430 | 0 | { |
431 | 0 | if (check_string_is_host_or_ip(x509, ustr, length)) |
432 | 0 | return TRUE; |
433 | 0 | return check_string_is_email(x509, ustr, length); |
434 | 0 | } |
435 | | |
436 | | WINPR_ATTR_NODISCARD |
437 | | static int |
438 | | extract_string_generic(const X509* x509, GENERAL_NAME* name, void* data, int index, int count, |
439 | | BOOL (*fkt)(const X509* x509, const unsigned char* str, size_t length)) |
440 | 0 | { |
441 | 0 | string_list* list = data; |
442 | 0 | WINPR_ASSERT(list); |
443 | 0 | WINPR_ASSERT(fkt); |
444 | |
|
445 | 0 | WINPR_ASSERT(name); |
446 | 0 | WINPR_UNUSED(index); |
447 | |
|
448 | 0 | const ASN1_STRING* str = nullptr; |
449 | 0 | switch (name->type) |
450 | 0 | { |
451 | 0 | case GEN_URI: |
452 | 0 | str = name->d.uniformResourceIdentifier; |
453 | 0 | break; |
454 | | |
455 | 0 | case GEN_DNS: |
456 | 0 | str = name->d.dNSName; |
457 | 0 | break; |
458 | | |
459 | 0 | case GEN_EMAIL: |
460 | 0 | str = name->d.rfc822Name; |
461 | 0 | break; |
462 | | |
463 | 0 | case GEN_IPADD: |
464 | 0 | str = name->d.iPAddress; |
465 | 0 | break; |
466 | | |
467 | 0 | default: |
468 | 0 | return 1; |
469 | 0 | } |
470 | | |
471 | 0 | unsigned char* cstring = nullptr; |
472 | 0 | const int rc = ASN1_STRING_to_UTF8(&cstring, str); |
473 | 0 | if (rc < 0) |
474 | 0 | { |
475 | 0 | WLog_ERR(TAG, "ASN1_STRING_to_UTF8() failed for %s: %s", |
476 | 0 | general_name_type_label(name->type), ERR_error_string(ERR_get_error(), nullptr)); |
477 | 0 | return 1; |
478 | 0 | } |
479 | | |
480 | 0 | if (!fkt(x509, cstring, WINPR_ASSERTING_INT_CAST(size_t, rc))) |
481 | 0 | { |
482 | 0 | WLog_ERR(TAG, "ASN1_STRING_to_UTF8() does not conform to expected format %s: %s", |
483 | 0 | general_name_type_label(name->type), (const char*)str); |
484 | 0 | OPENSSL_free(cstring); |
485 | 0 | return -1; |
486 | 0 | } |
487 | | |
488 | 0 | if (!string_list_allocate(list, WINPR_ASSERTING_INT_CAST(WINPR_CIPHER_TYPE, count)) || |
489 | 0 | (list->allocated <= 0)) |
490 | 0 | { |
491 | 0 | WLog_ERR(TAG, "ASN1_STRING_to_UTF8() allocation failed: %s", |
492 | 0 | general_name_type_label(name->type)); |
493 | 0 | OPENSSL_free(cstring); |
494 | 0 | return 0; |
495 | 0 | } |
496 | | |
497 | 0 | list->strings[list->count] = (char*)cstring; |
498 | 0 | list->lengths[list->count] = WINPR_ASSERTING_INT_CAST(size_t, rc); |
499 | 0 | list->count++; |
500 | |
|
501 | 0 | if (list->count >= list->maximum) |
502 | 0 | { |
503 | 0 | WLog_ERR(TAG, "ASN1_STRING_to_UTF8() limit exceeded: %s", |
504 | 0 | general_name_type_label(name->type)); |
505 | 0 | return 0; |
506 | 0 | } |
507 | | |
508 | 0 | return 1; |
509 | 0 | } |
510 | | |
511 | | WINPR_ATTR_NODISCARD |
512 | | static int extract_string(const X509* x509, GENERAL_NAME* name, void* data, int index, int count) |
513 | 0 | { |
514 | 0 | return extract_string_generic(x509, name, data, index, count, check_string_is_dns_name); |
515 | 0 | } |
516 | | |
517 | | static int extract_email(const X509* x509, GENERAL_NAME* name, void* data, int index, int count) |
518 | 0 | { |
519 | 0 | return extract_string_generic(x509, name, data, index, count, check_string_is_email); |
520 | 0 | } |
521 | | |
522 | | /* |
523 | | extract_othername_object -- object extractor. |
524 | | |
525 | | - the objects array is allocated lazily, when we first have to store a |
526 | | string. |
527 | | |
528 | | - allocated contains the size of the objects array, or -1 if |
529 | | allocation failed. |
530 | | |
531 | | - count contains the actual count of objects in the objects array. |
532 | | |
533 | | - maximum limits the number of objects we can store in the objects |
534 | | array: beyond, the extractor returns 0 to short-cut the search. |
535 | | |
536 | | extract_othername_objects stores in the objects array ASN1_TYPE * |
537 | | pointers directly obtained from the GENERAL_NAME. |
538 | | */ |
539 | | |
540 | | typedef struct object_list |
541 | | { |
542 | | ASN1_OBJECT* type_id; |
543 | | char** strings; |
544 | | size_t* lengths; |
545 | | |
546 | | size_t allocated; |
547 | | size_t count; |
548 | | size_t maximum; |
549 | | } object_list; |
550 | | |
551 | | static object_list object_list_initialize(void) |
552 | 0 | { |
553 | 0 | const object_list empty = { .type_id = nullptr, |
554 | 0 | .strings = nullptr, |
555 | 0 | .lengths = nullptr, |
556 | 0 | .allocated = 0, |
557 | 0 | .count = 0, |
558 | 0 | .maximum = INT_MAX }; |
559 | 0 | return empty; |
560 | 0 | } |
561 | | |
562 | | WINPR_ATTR_NODISCARD |
563 | | static BOOL object_list_allocate(object_list* list, size_t allocate_count) |
564 | 0 | { |
565 | 0 | if (!list->strings && (list->allocated == 0) && (allocate_count > 0)) |
566 | 0 | { |
567 | 0 | list->strings = (char**)calloc(allocate_count, sizeof(list->strings[0])); |
568 | 0 | list->lengths = calloc(allocate_count, sizeof(size_t)); |
569 | 0 | list->count = 0; |
570 | 0 | if (!list->strings || !list->lengths) |
571 | 0 | { |
572 | 0 | free((void*)list->strings); |
573 | 0 | free(list->lengths); |
574 | 0 | list->strings = nullptr; |
575 | 0 | list->lengths = nullptr; |
576 | 0 | return FALSE; |
577 | 0 | } |
578 | 0 | list->allocated = allocate_count; |
579 | 0 | } |
580 | 0 | return TRUE; |
581 | 0 | } |
582 | | |
583 | | WINPR_ATTR_MALLOC(free, 1) |
584 | | static char* object_string(const X509* x509, ASN1_TYPE* object, size_t* pLength) |
585 | 0 | { |
586 | 0 | unsigned char* utf8String = nullptr; |
587 | |
|
588 | 0 | WINPR_ASSERT(object); |
589 | 0 | WINPR_ASSERT(pLength); |
590 | |
|
591 | 0 | *pLength = 0; |
592 | | |
593 | | /* TODO: check that object.type is a string type. */ |
594 | 0 | const int length = ASN1_STRING_to_UTF8(&utf8String, object->value.asn1_string); |
595 | |
|
596 | 0 | if (length < 0) |
597 | 0 | return nullptr; |
598 | | |
599 | 0 | char* result = nullptr; |
600 | 0 | if (check_string_is_host_or_ip_or_email(x509, utf8String, |
601 | 0 | WINPR_ASSERTING_INT_CAST(size_t, length))) |
602 | 0 | { |
603 | 0 | result = strndup((char*)utf8String, WINPR_ASSERTING_INT_CAST(size_t, length)); |
604 | 0 | if (result) |
605 | 0 | *pLength = WINPR_ASSERTING_INT_CAST(size_t, length); |
606 | 0 | } |
607 | 0 | else |
608 | 0 | WLog_ERR(TAG, "Found invalid object_string entry in certificate: '%s'", utf8String); |
609 | 0 | OPENSSL_free(utf8String); |
610 | 0 | return result; |
611 | 0 | } |
612 | | |
613 | | static void object_list_free(object_list* list) |
614 | 0 | { |
615 | 0 | WINPR_ASSERT(list); |
616 | 0 | free((void*)list->strings); |
617 | 0 | free(list->lengths); |
618 | 0 | } |
619 | | |
620 | | WINPR_ATTR_NODISCARD |
621 | | static int extract_othername_object_as_string(const X509* x509, GENERAL_NAME* name, void* data, |
622 | | int index, int count) |
623 | 0 | { |
624 | 0 | object_list* list = data; |
625 | 0 | WINPR_UNUSED(index); |
626 | 0 | WINPR_ASSERT(x509); |
627 | |
|
628 | 0 | if (count < 0) |
629 | 0 | return -1; |
630 | | |
631 | 0 | if (name->type != GEN_OTHERNAME) |
632 | 0 | { |
633 | 0 | return 1; |
634 | 0 | } |
635 | | |
636 | 0 | if (0 != OBJ_cmp(name->d.otherName->type_id, list->type_id)) |
637 | 0 | { |
638 | 0 | return 1; |
639 | 0 | } |
640 | | |
641 | 0 | if (!object_list_allocate(list, WINPR_ASSERTING_INT_CAST(size_t, count)) || |
642 | 0 | (list->allocated <= 0)) |
643 | 0 | { |
644 | 0 | return 0; |
645 | 0 | } |
646 | | |
647 | 0 | list->strings[list->count] = |
648 | 0 | object_string(x509, name->d.otherName->value, &list->lengths[list->count]); |
649 | 0 | if (list->strings[list->count]) |
650 | 0 | { |
651 | 0 | list->count++; |
652 | 0 | } |
653 | |
|
654 | 0 | if (list->count >= list->maximum) |
655 | 0 | { |
656 | 0 | return 0; |
657 | 0 | } |
658 | | |
659 | 0 | return 1; |
660 | 0 | } |
661 | | |
662 | | char* x509_utils_get_email(const X509* x509) |
663 | 0 | { |
664 | 0 | string_list list = string_list_initialize(); |
665 | 0 | list.maximum = 1; |
666 | 0 | map_subject_alt_name(x509, GEN_EMAIL, extract_email, &list); |
667 | |
|
668 | 0 | if (list.count == 0) |
669 | 0 | { |
670 | 0 | string_list_free(&list); |
671 | 0 | return nullptr; |
672 | 0 | } |
673 | | |
674 | 0 | char* result = strndup(list.strings[0], list.lengths[0]); |
675 | 0 | OPENSSL_free(list.strings[0]); |
676 | 0 | string_list_free(&list); |
677 | 0 | return result; |
678 | 0 | } |
679 | | |
680 | | char* x509_utils_get_upn(const X509* x509) |
681 | 0 | { |
682 | 0 | object_list list = object_list_initialize(); |
683 | |
|
684 | 0 | list.type_id = OBJ_nid2obj(NID_ms_upn); |
685 | 0 | list.maximum = 1; |
686 | 0 | map_subject_alt_name(x509, GEN_OTHERNAME, extract_othername_object_as_string, &list); |
687 | |
|
688 | 0 | if (list.count == 0) |
689 | 0 | { |
690 | 0 | object_list_free(&list); |
691 | 0 | return nullptr; |
692 | 0 | } |
693 | | |
694 | 0 | char* result = list.strings[0]; |
695 | 0 | object_list_free(&list); |
696 | 0 | return result; |
697 | 0 | } |
698 | | |
699 | | char* x509_utils_get_date(const X509* x509, BOOL startDate) |
700 | 0 | { |
701 | 0 | WINPR_ASSERT(x509); |
702 | |
|
703 | 0 | const ASN1_TIME* date = startDate ? X509_get0_notBefore(x509) : X509_get0_notAfter(x509); |
704 | 0 | if (!date) |
705 | 0 | return nullptr; |
706 | | |
707 | 0 | BIO* bmem = BIO_new(BIO_s_mem()); |
708 | 0 | if (!bmem) |
709 | 0 | return nullptr; |
710 | | |
711 | 0 | char* str = nullptr; |
712 | 0 | if (ASN1_TIME_print(bmem, date)) |
713 | 0 | { |
714 | 0 | BUF_MEM* bptr = nullptr; |
715 | |
|
716 | 0 | BIO_get_mem_ptr(bmem, &bptr); |
717 | 0 | str = strndup(bptr->data, bptr->length); |
718 | 0 | } |
719 | 0 | else |
720 | 0 | { // Log error |
721 | 0 | } |
722 | 0 | BIO_free_all(bmem); |
723 | 0 | return str; |
724 | 0 | } |
725 | | |
726 | | void x509_utils_dns_names_free(size_t count, size_t* lengths, char** dns_names) |
727 | 0 | { |
728 | 0 | free(lengths); |
729 | |
|
730 | 0 | if (dns_names) |
731 | 0 | { |
732 | 0 | for (size_t i = 0; i < count; i++) |
733 | 0 | { |
734 | 0 | if (dns_names[i]) |
735 | 0 | { |
736 | 0 | OPENSSL_free(dns_names[i]); |
737 | 0 | } |
738 | 0 | } |
739 | |
|
740 | 0 | free((void*)dns_names); |
741 | 0 | } |
742 | 0 | } |
743 | | |
744 | | WINPR_ATTR_NODISCARD |
745 | | static char** x509_utils_get_names(const X509* xcert, int general_name_type, size_t* count, |
746 | | size_t** lengths) |
747 | 0 | { |
748 | 0 | string_list list = string_list_initialize(); |
749 | 0 | map_subject_alt_name(xcert, general_name_type, extract_string, &list); |
750 | 0 | (*count) = list.count; |
751 | |
|
752 | 0 | if (list.count <= 0) |
753 | 0 | { |
754 | 0 | string_list_free(&list); |
755 | 0 | return nullptr; |
756 | 0 | } |
757 | | |
758 | | /* lengths are not useful, since we converted the |
759 | | strings to utf-8, there cannot be nul-bytes in them. */ |
760 | 0 | char** result = (char**)calloc(list.count, sizeof(*result)); |
761 | 0 | (*lengths) = calloc(list.count, sizeof(**lengths)); |
762 | |
|
763 | 0 | if (!result || !(*lengths)) |
764 | 0 | { |
765 | 0 | string_list_free(&list); |
766 | 0 | free((void*)result); |
767 | 0 | free(*lengths); |
768 | 0 | (*lengths) = nullptr; |
769 | 0 | (*count) = 0; |
770 | 0 | return nullptr; |
771 | 0 | } |
772 | | |
773 | 0 | for (size_t i = 0; i < list.count; i++) |
774 | 0 | { |
775 | 0 | result[i] = list.strings[i]; |
776 | 0 | (*lengths)[i] = list.lengths[i]; |
777 | 0 | } |
778 | |
|
779 | 0 | string_list_free(&list); |
780 | 0 | return result; |
781 | 0 | } |
782 | | |
783 | | char** x509_utils_get_dns_names(const X509* xcert, size_t* count, size_t** pplengths) |
784 | 0 | { |
785 | 0 | return x509_utils_get_names(xcert, GEN_DNS, count, pplengths); |
786 | 0 | } |
787 | | |
788 | | char** x509_utils_get_ip_names(const X509* xcert, size_t* count, size_t** pplengths) |
789 | 0 | { |
790 | 0 | return x509_utils_get_names(xcert, GEN_IPADD, count, pplengths); |
791 | 0 | } |
792 | | |
793 | | char* x509_utils_get_issuer(const X509* xcert) |
794 | 0 | { |
795 | 0 | char* issuer = nullptr; |
796 | 0 | if (!xcert) |
797 | 0 | { |
798 | 0 | WLog_ERR(TAG, "Invalid certificate nullptr"); |
799 | 0 | return nullptr; |
800 | 0 | } |
801 | 0 | issuer = crypto_print_name(X509_get_issuer_name(xcert)); |
802 | 0 | if (!issuer) |
803 | 0 | WLog_WARN(TAG, "certificate does not have an issuer!"); |
804 | 0 | return issuer; |
805 | 0 | } |
806 | | |
807 | | WINPR_ATTR_NODISCARD |
808 | | static int asn1_object_cmp(const ASN1_OBJECT* const* a, const ASN1_OBJECT* const* b) |
809 | 0 | { |
810 | 0 | if (!a || !b) |
811 | 0 | return (a == b) ? 0 : (a ? 1 : -1); |
812 | | |
813 | 0 | if (!*a || !*b) |
814 | 0 | return (*a == *b) ? 0 : (*a ? 1 : -1); |
815 | | |
816 | 0 | return OBJ_cmp(*a, *b); |
817 | 0 | } |
818 | | |
819 | | BOOL x509_utils_check_eku(const X509* xcert, int nid) |
820 | 0 | { |
821 | 0 | BOOL ret = FALSE; |
822 | 0 | STACK_OF(ASN1_OBJECT)* oid_stack = nullptr; |
823 | 0 | ASN1_OBJECT* oid = nullptr; |
824 | |
|
825 | 0 | if (!xcert) |
826 | 0 | return FALSE; |
827 | | |
828 | 0 | oid = OBJ_nid2obj(nid); |
829 | 0 | if (!oid) |
830 | 0 | return FALSE; |
831 | | |
832 | 0 | oid_stack = X509_get_ext_d2i(xcert, NID_ext_key_usage, nullptr, nullptr); |
833 | 0 | if (!oid_stack) |
834 | 0 | return FALSE; |
835 | | |
836 | | /* sk_ASN1_OBJECT_set_cmp_func does some function pointer casting that produces a warning. We |
837 | | * can not fix upstream, so silence it here. */ |
838 | 0 | WINPR_PRAGMA_DIAG_PUSH |
839 | 0 | WINPR_PRAGMA_DIAG_IGNORED_CAST_FUNCTION_TYPE |
840 | 0 | sk_ASN1_OBJECT_set_cmp_func(oid_stack, asn1_object_cmp); |
841 | 0 | WINPR_PRAGMA_DIAG_POP |
842 | 0 | if (sk_ASN1_OBJECT_find(oid_stack, oid) >= 0) |
843 | 0 | ret = TRUE; |
844 | |
|
845 | 0 | sk_ASN1_OBJECT_pop_free(oid_stack, ASN1_OBJECT_free); |
846 | 0 | return ret; |
847 | 0 | } |
848 | | |
849 | | void x509_utils_print_info(const X509* xcert) |
850 | 0 | { |
851 | 0 | char* subject = x509_utils_get_subject(xcert); |
852 | 0 | char* issuer = x509_utils_get_issuer(xcert); |
853 | 0 | char* fp = (char*)x509_utils_get_hash(xcert, "sha256", nullptr); |
854 | |
|
855 | 0 | if (!fp) |
856 | 0 | { |
857 | 0 | WLog_ERR(TAG, "error computing fingerprint"); |
858 | 0 | goto out_free_issuer; |
859 | 0 | } |
860 | | |
861 | 0 | WLog_INFO(TAG, "Certificate details:"); |
862 | 0 | WLog_INFO(TAG, "\tSubject: %s", subject); |
863 | 0 | WLog_INFO(TAG, "\tIssuer: %s", issuer); |
864 | 0 | WLog_INFO(TAG, "\tThumbprint: %s", fp); |
865 | 0 | WLog_INFO(TAG, |
866 | 0 | "The above X.509 certificate could not be verified, possibly because you do not have " |
867 | 0 | "the CA certificate in your certificate store, or the certificate has expired. " |
868 | 0 | "Please look at the OpenSSL documentation on how to add a private CA to the store."); |
869 | 0 | free(fp); |
870 | 0 | out_free_issuer: |
871 | 0 | free(issuer); |
872 | 0 | free(subject); |
873 | 0 | } |
874 | | |
875 | | X509* x509_utils_from_pem(const char* data, size_t len, BOOL fromFile) |
876 | 0 | { |
877 | 0 | BIO* bio = nullptr; |
878 | 0 | if (fromFile) |
879 | 0 | bio = BIO_new_file(data, "rb"); |
880 | 0 | else |
881 | 0 | { |
882 | 0 | if (len > INT_MAX) |
883 | 0 | return nullptr; |
884 | | |
885 | 0 | bio = BIO_new_mem_buf(data, (int)len); |
886 | 0 | } |
887 | | |
888 | 0 | if (!bio) |
889 | 0 | { |
890 | 0 | WLog_ERR(TAG, "BIO_new failed for certificate"); |
891 | 0 | return nullptr; |
892 | 0 | } |
893 | | |
894 | 0 | X509* x509 = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr); |
895 | 0 | BIO_free_all(bio); |
896 | 0 | if (!x509) |
897 | 0 | WLog_ERR(TAG, "PEM_read_bio_X509 returned nullptr [input length %" PRIuz "]", len); |
898 | |
|
899 | 0 | return x509; |
900 | 0 | } |
901 | | |
902 | | WINPR_ATTR_NODISCARD |
903 | | static WINPR_MD_TYPE hash_nid_to_winpr(int hash_nid) |
904 | 0 | { |
905 | 0 | switch (hash_nid) |
906 | 0 | { |
907 | 0 | case NID_md2: |
908 | 0 | return WINPR_MD_MD2; |
909 | 0 | case NID_md4: |
910 | 0 | return WINPR_MD_MD4; |
911 | 0 | case NID_md5: |
912 | 0 | return WINPR_MD_MD5; |
913 | 0 | case NID_sha1: |
914 | 0 | return WINPR_MD_SHA1; |
915 | 0 | case NID_sha224: |
916 | 0 | return WINPR_MD_SHA224; |
917 | 0 | case NID_sha256: |
918 | 0 | return WINPR_MD_SHA256; |
919 | 0 | case NID_sha384: |
920 | 0 | return WINPR_MD_SHA384; |
921 | 0 | case NID_sha512: |
922 | 0 | return WINPR_MD_SHA512; |
923 | 0 | case NID_ripemd160: |
924 | 0 | return WINPR_MD_RIPEMD160; |
925 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x1010101fL) && !defined(LIBRESSL_VERSION_NUMBER) |
926 | 0 | case NID_sha3_224: |
927 | 0 | return WINPR_MD_SHA3_224; |
928 | 0 | case NID_sha3_256: |
929 | 0 | return WINPR_MD_SHA3_256; |
930 | 0 | case NID_sha3_384: |
931 | 0 | return WINPR_MD_SHA3_384; |
932 | 0 | case NID_sha3_512: |
933 | 0 | return WINPR_MD_SHA3_512; |
934 | 0 | case NID_shake128: |
935 | 0 | return WINPR_MD_SHAKE128; |
936 | 0 | case NID_shake256: |
937 | 0 | return WINPR_MD_SHAKE256; |
938 | 0 | #endif |
939 | 0 | case NID_undef: |
940 | 0 | default: |
941 | 0 | return WINPR_MD_NONE; |
942 | 0 | } |
943 | 0 | } |
944 | | |
945 | | WINPR_ATTR_NODISCARD |
946 | | static WINPR_MD_TYPE get_rsa_pss_digest(const X509_ALGOR* alg) |
947 | 0 | { |
948 | 0 | WINPR_MD_TYPE ret = WINPR_MD_NONE; |
949 | 0 | WINPR_MD_TYPE message_digest = WINPR_MD_NONE; |
950 | 0 | WINPR_MD_TYPE mgf1_digest = WINPR_MD_NONE; |
951 | 0 | int param_type = 0; |
952 | 0 | const void* param_value = nullptr; |
953 | 0 | const ASN1_STRING* sequence = nullptr; |
954 | 0 | const unsigned char* inp = nullptr; |
955 | 0 | RSA_PSS_PARAMS* params = nullptr; |
956 | 0 | X509_ALGOR* mgf1_digest_alg = nullptr; |
957 | | |
958 | | /* The RSA-PSS digest is encoded in a complex structure, defined in |
959 | | https://www.rfc-editor.org/rfc/rfc4055.html. */ |
960 | 0 | X509_ALGOR_get0(nullptr, ¶m_type, ¶m_value, alg); |
961 | | |
962 | | /* param_type and param_value the parameter in ASN1_TYPE form, but split into two parameters. A |
963 | | SEQUENCE is has type V_ASN1_SEQUENCE, and the value is an ASN1_STRING with the encoded |
964 | | structure. */ |
965 | 0 | if (param_type != V_ASN1_SEQUENCE) |
966 | 0 | goto end; |
967 | 0 | sequence = param_value; |
968 | | |
969 | | /* Decode the structure. */ |
970 | 0 | inp = ASN1_STRING_get0_data(sequence); |
971 | 0 | params = d2i_RSA_PSS_PARAMS(nullptr, &inp, ASN1_STRING_length(sequence)); |
972 | 0 | if (params == nullptr) |
973 | 0 | goto end; |
974 | | |
975 | | /* RSA-PSS uses two hash algorithms, a message digest and also an MGF function which is, itself, |
976 | | parameterized by a hash function. Both fields default to SHA-1, so we must also check for the |
977 | | value being nullptr. */ |
978 | 0 | message_digest = WINPR_MD_SHA1; |
979 | 0 | if (params->hashAlgorithm != nullptr) |
980 | 0 | { |
981 | 0 | const ASN1_OBJECT* obj = nullptr; |
982 | 0 | X509_ALGOR_get0(&obj, nullptr, nullptr, params->hashAlgorithm); |
983 | 0 | message_digest = hash_nid_to_winpr(OBJ_obj2nid(obj)); |
984 | 0 | if (message_digest == WINPR_MD_NONE) |
985 | 0 | goto end; |
986 | 0 | } |
987 | | |
988 | 0 | mgf1_digest = WINPR_MD_SHA1; |
989 | 0 | if (params->maskGenAlgorithm != nullptr) |
990 | 0 | { |
991 | 0 | const ASN1_OBJECT* obj = nullptr; |
992 | 0 | int mgf_param_type = 0; |
993 | 0 | const void* mgf_param_value = nullptr; |
994 | 0 | const ASN1_STRING* mgf_param_sequence = nullptr; |
995 | | /* First, check this is MGF-1, the only one ever defined. */ |
996 | 0 | X509_ALGOR_get0(&obj, &mgf_param_type, &mgf_param_value, params->maskGenAlgorithm); |
997 | 0 | if (OBJ_obj2nid(obj) != NID_mgf1) |
998 | 0 | goto end; |
999 | | |
1000 | | /* MGF-1 is, itself, parameterized by a hash function, encoded as an AlgorithmIdentifier. */ |
1001 | 0 | if (mgf_param_type != V_ASN1_SEQUENCE) |
1002 | 0 | goto end; |
1003 | 0 | mgf_param_sequence = mgf_param_value; |
1004 | 0 | inp = ASN1_STRING_get0_data(mgf_param_sequence); |
1005 | 0 | mgf1_digest_alg = d2i_X509_ALGOR(nullptr, &inp, ASN1_STRING_length(mgf_param_sequence)); |
1006 | 0 | if (mgf1_digest_alg == nullptr) |
1007 | 0 | goto end; |
1008 | | |
1009 | | /* Finally, extract the digest. */ |
1010 | 0 | X509_ALGOR_get0(&obj, nullptr, nullptr, mgf1_digest_alg); |
1011 | 0 | mgf1_digest = hash_nid_to_winpr(OBJ_obj2nid(obj)); |
1012 | 0 | if (mgf1_digest == WINPR_MD_NONE) |
1013 | 0 | goto end; |
1014 | 0 | } |
1015 | | |
1016 | | /* If the two digests do not match, it is ambiguous which to return. tls-server-end-point leaves |
1017 | | it undefined, so return none. |
1018 | | https://www.rfc-editor.org/rfc/rfc5929.html#section-4.1 */ |
1019 | 0 | if (message_digest != mgf1_digest) |
1020 | 0 | goto end; |
1021 | 0 | ret = message_digest; |
1022 | |
|
1023 | 0 | end: |
1024 | 0 | RSA_PSS_PARAMS_free(params); |
1025 | 0 | X509_ALGOR_free(mgf1_digest_alg); |
1026 | 0 | return ret; |
1027 | 0 | } |
1028 | | |
1029 | | WINPR_MD_TYPE x509_utils_get_signature_alg(const X509* xcert) |
1030 | 0 | { |
1031 | 0 | WINPR_ASSERT(xcert); |
1032 | |
|
1033 | 0 | const int nid = X509_get_signature_nid(xcert); |
1034 | |
|
1035 | 0 | if (nid == NID_rsassaPss) |
1036 | 0 | { |
1037 | 0 | const X509_ALGOR* alg = nullptr; |
1038 | 0 | X509_get0_signature(nullptr, &alg, xcert); |
1039 | 0 | return get_rsa_pss_digest(alg); |
1040 | 0 | } |
1041 | | |
1042 | 0 | int hash_nid = 0; |
1043 | 0 | if (OBJ_find_sigid_algs(nid, &hash_nid, nullptr) != 1) |
1044 | 0 | return WINPR_MD_NONE; |
1045 | | |
1046 | 0 | return hash_nid_to_winpr(hash_nid); |
1047 | 0 | } |
1048 | | |
1049 | | char* x509_utils_get_common_name(const X509* xcert, size_t* plength) |
1050 | 0 | { |
1051 | 0 | const X509_NAME* subject_name = X509_get_subject_name(xcert); |
1052 | 0 | if (subject_name == nullptr) |
1053 | 0 | return nullptr; |
1054 | | |
1055 | 0 | const int index = X509_NAME_get_index_by_NID(subject_name, NID_commonName, -1); |
1056 | 0 | if (index < 0) |
1057 | 0 | return nullptr; |
1058 | | |
1059 | 0 | const X509_NAME_ENTRY* entry = X509_NAME_get_entry(subject_name, index); |
1060 | 0 | if (entry == nullptr) |
1061 | 0 | return nullptr; |
1062 | | |
1063 | 0 | const ASN1_STRING* entry_data = X509_NAME_ENTRY_get_data(entry); |
1064 | 0 | if (entry_data == nullptr) |
1065 | 0 | return nullptr; |
1066 | | |
1067 | 0 | BYTE* common_name_raw = nullptr; |
1068 | 0 | const int length = ASN1_STRING_to_UTF8(&common_name_raw, entry_data); |
1069 | 0 | if (length < 0) |
1070 | 0 | return nullptr; |
1071 | | |
1072 | 0 | char* common_name = nullptr; |
1073 | 0 | if (check_string_is_dns_name(xcert, common_name_raw, WINPR_ASSERTING_INT_CAST(size_t, length))) |
1074 | 0 | { |
1075 | 0 | if (plength) |
1076 | 0 | *plength = (size_t)length; |
1077 | |
|
1078 | 0 | common_name = strndup((char*)common_name_raw, (size_t)length); |
1079 | 0 | } |
1080 | 0 | OPENSSL_free(common_name_raw); |
1081 | 0 | return common_name; |
1082 | 0 | } |
1083 | | |
1084 | | WINPR_ATTR_NODISCARD |
1085 | | static int verify_cb(int ok, X509_STORE_CTX* csc) |
1086 | 0 | { |
1087 | 0 | if (ok != 1) |
1088 | 0 | { |
1089 | 0 | WINPR_ASSERT(csc); |
1090 | 0 | int err = X509_STORE_CTX_get_error(csc); |
1091 | 0 | int derr = X509_STORE_CTX_get_error_depth(csc); |
1092 | 0 | X509* where = X509_STORE_CTX_get_current_cert(csc); |
1093 | 0 | const char* what = X509_verify_cert_error_string(err); |
1094 | 0 | char* name = x509_utils_get_subject(where); |
1095 | |
|
1096 | 0 | WLog_WARN(TAG, "Certificate verification failure '%s (%d)' at stack position %d", what, err, |
1097 | 0 | derr); |
1098 | 0 | WLog_WARN(TAG, "%s", name); |
1099 | |
|
1100 | 0 | free(name); |
1101 | 0 | } |
1102 | 0 | return ok; |
1103 | 0 | } |
1104 | | |
1105 | | BOOL x509_utils_verify(X509* xcert, STACK_OF(X509) * chain, const char* certificate_store_path) |
1106 | 0 | { |
1107 | 0 | const int purposes[] = { X509_PURPOSE_SSL_SERVER }; |
1108 | 0 | BOOL status = FALSE; |
1109 | |
|
1110 | 0 | if (!xcert) |
1111 | 0 | return FALSE; |
1112 | | |
1113 | 0 | X509_STORE* cert_ctx = X509_STORE_new(); |
1114 | |
|
1115 | 0 | if (cert_ctx == nullptr) |
1116 | 0 | goto end; |
1117 | | |
1118 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
1119 | | OpenSSL_add_all_algorithms(); |
1120 | | #else |
1121 | 0 | OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS | |
1122 | 0 | OPENSSL_INIT_LOAD_CONFIG, |
1123 | 0 | nullptr); |
1124 | 0 | #endif |
1125 | |
|
1126 | 0 | if (X509_STORE_set_default_paths(cert_ctx) != 1) |
1127 | 0 | goto end; |
1128 | | |
1129 | 0 | X509_LOOKUP* lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir()); |
1130 | |
|
1131 | 0 | if (lookup == nullptr) |
1132 | 0 | goto end; |
1133 | | |
1134 | 0 | X509_LOOKUP_add_dir(lookup, nullptr, X509_FILETYPE_DEFAULT); |
1135 | |
|
1136 | 0 | if (certificate_store_path != nullptr) |
1137 | 0 | { |
1138 | 0 | X509_LOOKUP_add_dir(lookup, certificate_store_path, X509_FILETYPE_PEM); |
1139 | 0 | } |
1140 | |
|
1141 | 0 | X509_STORE_set_flags(cert_ctx, 0); |
1142 | |
|
1143 | 0 | for (size_t i = 0; i < ARRAYSIZE(purposes); i++) |
1144 | 0 | { |
1145 | 0 | int err = -1; |
1146 | 0 | int rc = -1; |
1147 | 0 | int purpose = purposes[i]; |
1148 | 0 | X509_STORE_CTX* csc = X509_STORE_CTX_new(); |
1149 | |
|
1150 | 0 | if (csc == nullptr) |
1151 | 0 | goto skip; |
1152 | 0 | if (!X509_STORE_CTX_init(csc, cert_ctx, xcert, chain)) |
1153 | 0 | goto skip; |
1154 | | |
1155 | 0 | X509_STORE_CTX_set_purpose(csc, purpose); |
1156 | 0 | X509_STORE_CTX_set_verify_cb(csc, verify_cb); |
1157 | |
|
1158 | 0 | rc = X509_verify_cert(csc); |
1159 | 0 | err = X509_STORE_CTX_get_error(csc); |
1160 | 0 | skip: |
1161 | 0 | X509_STORE_CTX_free(csc); |
1162 | 0 | if (rc == 1) |
1163 | 0 | { |
1164 | 0 | status = TRUE; |
1165 | 0 | break; |
1166 | 0 | } |
1167 | 0 | else if (err != X509_V_ERR_INVALID_PURPOSE) |
1168 | 0 | break; |
1169 | 0 | } |
1170 | | |
1171 | 0 | X509_STORE_free(cert_ctx); |
1172 | 0 | end: |
1173 | 0 | return status; |
1174 | 0 | } |
1175 | | |
1176 | | char* x509_utils_bio_read(BIO* bio, size_t* plen) |
1177 | 0 | { |
1178 | 0 | char* buffer = nullptr; |
1179 | 0 | WINPR_ASSERT(bio); |
1180 | |
|
1181 | 0 | if (plen) |
1182 | 0 | *plen = 0; |
1183 | |
|
1184 | 0 | BIO_flush(bio); |
1185 | |
|
1186 | 0 | const UINT64 size = BIO_number_written(bio); |
1187 | 0 | if (size > INT_MAX) |
1188 | 0 | return nullptr; |
1189 | | |
1190 | 0 | buffer = calloc(1, (size_t)size + 1ull); |
1191 | |
|
1192 | 0 | if (!buffer) |
1193 | 0 | return nullptr; |
1194 | | |
1195 | 0 | ERR_clear_error(); |
1196 | 0 | const int rc = BIO_read(bio, buffer, (int)size); |
1197 | 0 | if (rc <= 0) |
1198 | 0 | goto fail; |
1199 | | |
1200 | 0 | if (plen) |
1201 | 0 | *plen = size; |
1202 | 0 | return buffer; |
1203 | | |
1204 | 0 | fail: |
1205 | 0 | free(buffer); |
1206 | 0 | return nullptr; |
1207 | 0 | } |
1208 | | |
1209 | | void x509_utils_ip_names_free(size_t count, size_t* lengths, char** dns_names) |
1210 | 0 | { |
1211 | 0 | x509_utils_dns_names_free(count, lengths, dns_names); |
1212 | 0 | } |