/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 <openssl/objects.h> |
23 | | #include <openssl/x509v3.h> |
24 | | #include <openssl/pem.h> |
25 | | #include <openssl/rsa.h> |
26 | | #include <openssl/err.h> |
27 | | |
28 | | #include <freerdp/config.h> |
29 | | |
30 | | #include <winpr/crt.h> |
31 | | #include <winpr/string.h> |
32 | | #include <winpr/assert.h> |
33 | | |
34 | | #include <freerdp/log.h> |
35 | | |
36 | | #include "x509_utils.h" |
37 | | |
38 | | #define TAG FREERDP_TAG("crypto") |
39 | | |
40 | | BYTE* x509_utils_get_hash(const X509* xcert, const char* hash, size_t* length) |
41 | 114 | { |
42 | 114 | UINT32 fp_len = EVP_MAX_MD_SIZE; |
43 | 114 | BYTE* fp = nullptr; |
44 | 114 | const EVP_MD* md = EVP_get_digestbyname(hash); |
45 | 114 | if (!md) |
46 | 0 | { |
47 | 0 | WLog_ERR(TAG, "System does not support %s hash!", hash); |
48 | 0 | return nullptr; |
49 | 0 | } |
50 | 114 | if (!xcert || !length) |
51 | 0 | { |
52 | 0 | WLog_ERR(TAG, "Invalid arguments: xcert=%p, length=%p", |
53 | 0 | WINPR_CXX_COMPAT_CAST(const void*, xcert), |
54 | 0 | WINPR_CXX_COMPAT_CAST(const void*, length)); |
55 | 0 | return nullptr; |
56 | 0 | } |
57 | | |
58 | 114 | fp = calloc(fp_len + 1, sizeof(BYTE)); |
59 | 114 | if (!fp) |
60 | 0 | { |
61 | 0 | WLog_ERR(TAG, "could not allocate %" PRIu32 " bytes", fp_len); |
62 | 0 | return nullptr; |
63 | 0 | } |
64 | | |
65 | 114 | if (X509_digest(xcert, md, fp, &fp_len) != 1) |
66 | 0 | { |
67 | 0 | free(fp); |
68 | 0 | WLog_ERR(TAG, "certificate does not have a %s hash!", hash); |
69 | 0 | return nullptr; |
70 | 0 | } |
71 | | |
72 | 114 | *length = fp_len; |
73 | 114 | return fp; |
74 | 114 | } |
75 | | |
76 | | static char* crypto_print_name(const X509_NAME* name) |
77 | 228 | { |
78 | 228 | char* buffer = nullptr; |
79 | 228 | BIO* outBIO = BIO_new(BIO_s_mem()); |
80 | | |
81 | 228 | if (X509_NAME_print_ex(outBIO, name, 0, XN_FLAG_ONELINE) > 0) |
82 | 135 | { |
83 | 135 | buffer = x509_utils_bio_read(outBIO, nullptr); |
84 | 135 | } |
85 | | |
86 | 228 | fail: |
87 | 228 | BIO_free_all(outBIO); |
88 | 228 | return buffer; |
89 | 228 | } |
90 | | |
91 | | char* x509_utils_get_subject(const X509* xcert) |
92 | 114 | { |
93 | 114 | char* subject = nullptr; |
94 | 114 | if (!xcert) |
95 | 0 | { |
96 | 0 | WLog_ERR(TAG, "Invalid certificate nullptr"); |
97 | 0 | return nullptr; |
98 | 0 | } |
99 | 114 | subject = crypto_print_name(X509_get_subject_name(xcert)); |
100 | 114 | if (!subject) |
101 | 0 | WLog_WARN(TAG, "certificate does not have a subject!"); |
102 | 114 | return subject; |
103 | 114 | } |
104 | | |
105 | | /* GENERAL_NAME type labels */ |
106 | | |
107 | | static const char* general_name_type_labels[] = { "OTHERNAME", "EMAIL ", "DNS ", |
108 | | "X400 ", "DIRNAME ", "EDIPARTY ", |
109 | | "URI ", "IPADD ", "RID " }; |
110 | | |
111 | | static const char* general_name_type_label(int general_name_type) |
112 | 0 | { |
113 | 0 | if ((0 <= general_name_type) && |
114 | 0 | ((size_t)general_name_type < ARRAYSIZE(general_name_type_labels))) |
115 | 0 | { |
116 | 0 | return general_name_type_labels[general_name_type]; |
117 | 0 | } |
118 | 0 | else |
119 | 0 | { |
120 | 0 | static char buffer[80] = WINPR_C_ARRAY_INIT; |
121 | 0 | (void)snprintf(buffer, sizeof(buffer), "Unknown general name type (%d)", general_name_type); |
122 | 0 | return buffer; |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | | /* |
127 | | |
128 | | map_subject_alt_name(x509, general_name_type, mapper, data) |
129 | | |
130 | | Call the function mapper with subjectAltNames found in the x509 |
131 | | certificate and data. if generate_name_type is GEN_ALL, the the |
132 | | mapper is called for all the names, else it's called only for names |
133 | | of the given type. |
134 | | |
135 | | |
136 | | We implement two extractors: |
137 | | |
138 | | - a string extractor that can be used to get the subjectAltNames of |
139 | | the following types: GEN_URI, GEN_DNS, GEN_EMAIL |
140 | | |
141 | | - a ASN1_OBJECT filter/extractor that can be used to get the |
142 | | subjectAltNames of OTHERNAME type. |
143 | | |
144 | | Note: usually, it's a string, but some type of otherNames can be |
145 | | associated with different classes of objects. eg. a KPN may be a |
146 | | sequence of realm and principal name, instead of a single string |
147 | | object. |
148 | | |
149 | | Not implemented yet: extractors for the types: GEN_X400, GEN_DIRNAME, |
150 | | GEN_EDIPARTY, GEN_RID, GEN_IPADD (the later can contain nul-bytes). |
151 | | |
152 | | |
153 | | mapper(name, data, index, count) |
154 | | |
155 | | The mapper is passed: |
156 | | - the GENERAL_NAME selected, |
157 | | - the data, |
158 | | - the index of the general name in the subjectAltNames, |
159 | | - the total number of names in the subjectAltNames. |
160 | | |
161 | | The last parameter let's the mapper allocate arrays to collect objects. |
162 | | Note: if names are filtered, not all the indices from 0 to count-1 are |
163 | | passed to mapper, only the indices selected. |
164 | | |
165 | | When the mapper returns 0, map_subject_alt_name stops the iteration immediately. |
166 | | |
167 | | */ |
168 | | |
169 | 0 | #define GEN_ALL (-1) |
170 | | |
171 | | typedef int (*general_name_mapper_pr)(GENERAL_NAME* name, void* data, int index, int count); |
172 | | |
173 | | static void map_subject_alt_name(const X509* x509, int general_name_type, |
174 | | general_name_mapper_pr mapper, void* data) |
175 | 0 | { |
176 | 0 | int num = 0; |
177 | 0 | STACK_OF(GENERAL_NAME)* gens = nullptr; |
178 | 0 | gens = X509_get_ext_d2i(x509, NID_subject_alt_name, nullptr, nullptr); |
179 | |
|
180 | 0 | if (!gens) |
181 | 0 | { |
182 | 0 | return; |
183 | 0 | } |
184 | | |
185 | 0 | num = sk_GENERAL_NAME_num(gens); |
186 | |
|
187 | 0 | for (int i = 0; (i < num); i++) |
188 | 0 | { |
189 | 0 | GENERAL_NAME* name = sk_GENERAL_NAME_value(gens, i); |
190 | |
|
191 | 0 | if (name) |
192 | 0 | { |
193 | 0 | if ((general_name_type == GEN_ALL) || (general_name_type == name->type)) |
194 | 0 | { |
195 | 0 | if (!mapper(name, data, i, num)) |
196 | 0 | { |
197 | 0 | break; |
198 | 0 | } |
199 | 0 | } |
200 | 0 | } |
201 | 0 | } |
202 | |
|
203 | 0 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
204 | 0 | } |
205 | | |
206 | | /* |
207 | | extract_string -- string extractor |
208 | | |
209 | | - the strings array is allocated lazily, when we first have to store a |
210 | | string. |
211 | | |
212 | | - allocated contains the size of the strings array, or -1 if |
213 | | allocation failed. |
214 | | |
215 | | - count contains the actual count of strings in the strings array. |
216 | | |
217 | | - maximum limits the number of strings we can store in the strings |
218 | | array: beyond, the extractor returns 0 to short-cut the search. |
219 | | |
220 | | extract_string stores in the string list OPENSSL strings, |
221 | | that must be freed with OPENSSL_free. |
222 | | |
223 | | */ |
224 | | |
225 | | typedef struct string_list |
226 | | { |
227 | | char** strings; |
228 | | size_t allocated; |
229 | | size_t count; |
230 | | size_t maximum; |
231 | | } string_list; |
232 | | |
233 | | static void string_list_initialize(string_list* list) |
234 | 0 | { |
235 | 0 | list->strings = nullptr; |
236 | 0 | list->allocated = 0; |
237 | 0 | list->count = 0; |
238 | 0 | list->maximum = INT_MAX; |
239 | 0 | } |
240 | | |
241 | | static void string_list_allocate(string_list* list, size_t allocate_count) |
242 | 0 | { |
243 | 0 | if (!list->strings && list->allocated == 0) |
244 | 0 | { |
245 | 0 | list->strings = (char**)calloc(allocate_count, sizeof(char*)); |
246 | 0 | list->allocated = list->strings ? allocate_count : 0; |
247 | 0 | list->count = 0; |
248 | 0 | } |
249 | 0 | } |
250 | | |
251 | | static void string_list_free(string_list* list) |
252 | 0 | { |
253 | | /* Note: we don't free the contents of the strings array: this */ |
254 | | /* is handled by the caller, either by returning this */ |
255 | | /* content, or freeing it itself. */ |
256 | 0 | free((void*)list->strings); |
257 | 0 | } |
258 | | |
259 | | static int extract_string(GENERAL_NAME* name, void* data, int index, int count) |
260 | 0 | { |
261 | 0 | string_list* list = data; |
262 | 0 | unsigned char* cstring = nullptr; |
263 | 0 | ASN1_STRING* str = nullptr; |
264 | |
|
265 | 0 | WINPR_UNUSED(index); |
266 | |
|
267 | 0 | switch (name->type) |
268 | 0 | { |
269 | 0 | case GEN_URI: |
270 | 0 | str = name->d.uniformResourceIdentifier; |
271 | 0 | break; |
272 | | |
273 | 0 | case GEN_DNS: |
274 | 0 | str = name->d.dNSName; |
275 | 0 | break; |
276 | | |
277 | 0 | case GEN_EMAIL: |
278 | 0 | str = name->d.rfc822Name; |
279 | 0 | break; |
280 | | |
281 | 0 | default: |
282 | 0 | return 1; |
283 | 0 | } |
284 | | |
285 | 0 | if ((ASN1_STRING_to_UTF8(&cstring, str)) < 0) |
286 | 0 | { |
287 | 0 | WLog_ERR(TAG, "ASN1_STRING_to_UTF8() failed for %s: %s", |
288 | 0 | general_name_type_label(name->type), ERR_error_string(ERR_get_error(), nullptr)); |
289 | 0 | return 1; |
290 | 0 | } |
291 | | |
292 | 0 | string_list_allocate(list, WINPR_ASSERTING_INT_CAST(WINPR_CIPHER_TYPE, count)); |
293 | |
|
294 | 0 | if (list->allocated <= 0) |
295 | 0 | { |
296 | 0 | OPENSSL_free(cstring); |
297 | 0 | return 0; |
298 | 0 | } |
299 | | |
300 | 0 | list->strings[list->count] = (char*)cstring; |
301 | 0 | list->count++; |
302 | |
|
303 | 0 | if (list->count >= list->maximum) |
304 | 0 | { |
305 | 0 | return 0; |
306 | 0 | } |
307 | | |
308 | 0 | return 1; |
309 | 0 | } |
310 | | |
311 | | /* |
312 | | extract_othername_object -- object extractor. |
313 | | |
314 | | - the objects array is allocated lazily, when we first have to store a |
315 | | string. |
316 | | |
317 | | - allocated contains the size of the objects array, or -1 if |
318 | | allocation failed. |
319 | | |
320 | | - count contains the actual count of objects in the objects array. |
321 | | |
322 | | - maximum limits the number of objects we can store in the objects |
323 | | array: beyond, the extractor returns 0 to short-cut the search. |
324 | | |
325 | | extract_othername_objects stores in the objects array ASN1_TYPE * |
326 | | pointers directly obtained from the GENERAL_NAME. |
327 | | */ |
328 | | |
329 | | typedef struct object_list |
330 | | { |
331 | | ASN1_OBJECT* type_id; |
332 | | char** strings; |
333 | | size_t allocated; |
334 | | size_t count; |
335 | | size_t maximum; |
336 | | } object_list; |
337 | | |
338 | | static void object_list_initialize(object_list* list) |
339 | 0 | { |
340 | 0 | list->type_id = nullptr; |
341 | 0 | list->strings = nullptr; |
342 | 0 | list->allocated = 0; |
343 | 0 | list->count = 0; |
344 | 0 | list->maximum = INT_MAX; |
345 | 0 | } |
346 | | |
347 | | static void object_list_allocate(object_list* list, size_t allocate_count) |
348 | 0 | { |
349 | 0 | if (!list->strings && (list->allocated == 0) && (allocate_count > 0)) |
350 | 0 | { |
351 | 0 | list->strings = (char**)calloc(allocate_count, sizeof(list->strings[0])); |
352 | 0 | list->allocated = list->strings ? allocate_count : 0; |
353 | 0 | list->count = 0; |
354 | 0 | } |
355 | 0 | } |
356 | | |
357 | | static char* object_string(ASN1_TYPE* object) |
358 | 0 | { |
359 | 0 | char* result = nullptr; |
360 | 0 | unsigned char* utf8String = nullptr; |
361 | | |
362 | | /* TODO: check that object.type is a string type. */ |
363 | 0 | const int length = ASN1_STRING_to_UTF8(&utf8String, object->value.asn1_string); |
364 | |
|
365 | 0 | if (length < 0) |
366 | 0 | { |
367 | 0 | return nullptr; |
368 | 0 | } |
369 | | |
370 | 0 | result = strndup((char*)utf8String, WINPR_ASSERTING_INT_CAST(size_t, length)); |
371 | 0 | OPENSSL_free(utf8String); |
372 | 0 | return result; |
373 | 0 | } |
374 | | |
375 | | static void object_list_free(object_list* list) |
376 | 0 | { |
377 | 0 | WINPR_ASSERT(list); |
378 | 0 | free((void*)list->strings); |
379 | 0 | } |
380 | | |
381 | | static int extract_othername_object_as_string(GENERAL_NAME* name, void* data, int index, int count) |
382 | 0 | { |
383 | 0 | object_list* list = data; |
384 | 0 | WINPR_UNUSED(index); |
385 | |
|
386 | 0 | if (count < 0) |
387 | 0 | return -1; |
388 | | |
389 | 0 | if (name->type != GEN_OTHERNAME) |
390 | 0 | { |
391 | 0 | return 1; |
392 | 0 | } |
393 | | |
394 | 0 | if (0 != OBJ_cmp(name->d.otherName->type_id, list->type_id)) |
395 | 0 | { |
396 | 0 | return 1; |
397 | 0 | } |
398 | | |
399 | 0 | object_list_allocate(list, WINPR_ASSERTING_INT_CAST(size_t, count)); |
400 | |
|
401 | 0 | if (list->allocated <= 0) |
402 | 0 | { |
403 | 0 | return 0; |
404 | 0 | } |
405 | | |
406 | 0 | list->strings[list->count] = object_string(name->d.otherName->value); |
407 | |
|
408 | 0 | if (list->strings[list->count]) |
409 | 0 | { |
410 | 0 | list->count++; |
411 | 0 | } |
412 | |
|
413 | 0 | if (list->count >= list->maximum) |
414 | 0 | { |
415 | 0 | return 0; |
416 | 0 | } |
417 | | |
418 | 0 | return 1; |
419 | 0 | } |
420 | | |
421 | | char* x509_utils_get_email(const X509* x509) |
422 | 0 | { |
423 | 0 | char* result = nullptr; |
424 | 0 | string_list list; |
425 | 0 | string_list_initialize(&list); |
426 | 0 | list.maximum = 1; |
427 | 0 | map_subject_alt_name(x509, GEN_EMAIL, extract_string, &list); |
428 | |
|
429 | 0 | if (list.count == 0) |
430 | 0 | { |
431 | 0 | string_list_free(&list); |
432 | 0 | return nullptr; |
433 | 0 | } |
434 | | |
435 | 0 | result = _strdup(list.strings[0]); |
436 | 0 | OPENSSL_free(list.strings[0]); |
437 | 0 | string_list_free(&list); |
438 | 0 | return result; |
439 | 0 | } |
440 | | |
441 | | char* x509_utils_get_upn(const X509* x509) |
442 | 0 | { |
443 | 0 | char* result = nullptr; |
444 | 0 | object_list list = WINPR_C_ARRAY_INIT; |
445 | 0 | object_list_initialize(&list); |
446 | 0 | list.type_id = OBJ_nid2obj(NID_ms_upn); |
447 | 0 | list.maximum = 1; |
448 | 0 | map_subject_alt_name(x509, GEN_OTHERNAME, extract_othername_object_as_string, &list); |
449 | |
|
450 | 0 | if (list.count == 0) |
451 | 0 | { |
452 | 0 | object_list_free(&list); |
453 | 0 | return nullptr; |
454 | 0 | } |
455 | | |
456 | 0 | result = list.strings[0]; |
457 | 0 | object_list_free(&list); |
458 | 0 | return result; |
459 | 0 | } |
460 | | |
461 | | char* x509_utils_get_date(const X509* x509, BOOL startDate) |
462 | 0 | { |
463 | 0 | WINPR_ASSERT(x509); |
464 | |
|
465 | 0 | const ASN1_TIME* date = startDate ? X509_get0_notBefore(x509) : X509_get0_notAfter(x509); |
466 | 0 | if (!date) |
467 | 0 | return nullptr; |
468 | | |
469 | 0 | BIO* bmem = BIO_new(BIO_s_mem()); |
470 | 0 | if (!bmem) |
471 | 0 | return nullptr; |
472 | | |
473 | 0 | char* str = nullptr; |
474 | 0 | if (ASN1_TIME_print(bmem, date)) |
475 | 0 | { |
476 | 0 | BUF_MEM* bptr = nullptr; |
477 | |
|
478 | 0 | BIO_get_mem_ptr(bmem, &bptr); |
479 | 0 | str = strndup(bptr->data, bptr->length); |
480 | 0 | } |
481 | 0 | else |
482 | 0 | { // Log error |
483 | 0 | } |
484 | 0 | BIO_free_all(bmem); |
485 | 0 | return str; |
486 | 0 | } |
487 | | |
488 | | void x509_utils_dns_names_free(size_t count, size_t* lengths, char** dns_names) |
489 | 0 | { |
490 | 0 | free(lengths); |
491 | |
|
492 | 0 | if (dns_names) |
493 | 0 | { |
494 | 0 | for (size_t i = 0; i < count; i++) |
495 | 0 | { |
496 | 0 | if (dns_names[i]) |
497 | 0 | { |
498 | 0 | OPENSSL_free(dns_names[i]); |
499 | 0 | } |
500 | 0 | } |
501 | |
|
502 | 0 | free((void*)dns_names); |
503 | 0 | } |
504 | 0 | } |
505 | | |
506 | | char** x509_utils_get_dns_names(const X509* xcert, size_t* count, size_t** lengths) |
507 | 0 | { |
508 | 0 | char** result = nullptr; |
509 | 0 | string_list list = WINPR_C_ARRAY_INIT; |
510 | 0 | string_list_initialize(&list); |
511 | 0 | map_subject_alt_name(xcert, GEN_DNS, extract_string, &list); |
512 | 0 | (*count) = list.count; |
513 | |
|
514 | 0 | if (list.count <= 0) |
515 | 0 | { |
516 | 0 | string_list_free(&list); |
517 | 0 | return nullptr; |
518 | 0 | } |
519 | | |
520 | | /* lengths are not useful, since we converted the |
521 | | strings to utf-8, there cannot be nul-bytes in them. */ |
522 | 0 | result = (char**)calloc(list.count, sizeof(*result)); |
523 | 0 | (*lengths) = calloc(list.count, sizeof(**lengths)); |
524 | |
|
525 | 0 | if (!result || !(*lengths)) |
526 | 0 | { |
527 | 0 | string_list_free(&list); |
528 | 0 | free((void*)result); |
529 | 0 | free(*lengths); |
530 | 0 | (*lengths) = nullptr; |
531 | 0 | (*count) = 0; |
532 | 0 | return nullptr; |
533 | 0 | } |
534 | | |
535 | 0 | for (size_t i = 0; i < list.count; i++) |
536 | 0 | { |
537 | 0 | result[i] = list.strings[i]; |
538 | 0 | (*lengths)[i] = strlen(result[i]); |
539 | 0 | } |
540 | |
|
541 | 0 | string_list_free(&list); |
542 | 0 | return result; |
543 | 0 | } |
544 | | |
545 | | char* x509_utils_get_issuer(const X509* xcert) |
546 | 114 | { |
547 | 114 | char* issuer = nullptr; |
548 | 114 | if (!xcert) |
549 | 0 | { |
550 | 0 | WLog_ERR(TAG, "Invalid certificate nullptr"); |
551 | 0 | return nullptr; |
552 | 0 | } |
553 | 114 | issuer = crypto_print_name(X509_get_issuer_name(xcert)); |
554 | 114 | if (!issuer) |
555 | 93 | WLog_WARN(TAG, "certificate does not have an issuer!"); |
556 | 114 | return issuer; |
557 | 114 | } |
558 | | |
559 | | static int asn1_object_cmp(const ASN1_OBJECT* const* a, const ASN1_OBJECT* const* b) |
560 | 0 | { |
561 | 0 | if (!a || !b) |
562 | 0 | return (a == b) ? 0 : (a ? 1 : -1); |
563 | | |
564 | 0 | if (!*a || !*b) |
565 | 0 | return (*a == *b) ? 0 : (*a ? 1 : -1); |
566 | | |
567 | 0 | return OBJ_cmp(*a, *b); |
568 | 0 | } |
569 | | |
570 | | BOOL x509_utils_check_eku(const X509* xcert, int nid) |
571 | 0 | { |
572 | 0 | BOOL ret = FALSE; |
573 | 0 | STACK_OF(ASN1_OBJECT)* oid_stack = nullptr; |
574 | 0 | ASN1_OBJECT* oid = nullptr; |
575 | |
|
576 | 0 | if (!xcert) |
577 | 0 | return FALSE; |
578 | | |
579 | 0 | oid = OBJ_nid2obj(nid); |
580 | 0 | if (!oid) |
581 | 0 | return FALSE; |
582 | | |
583 | 0 | oid_stack = X509_get_ext_d2i(xcert, NID_ext_key_usage, nullptr, nullptr); |
584 | 0 | if (!oid_stack) |
585 | 0 | return FALSE; |
586 | | |
587 | 0 | sk_ASN1_OBJECT_set_cmp_func(oid_stack, asn1_object_cmp); |
588 | 0 | if (sk_ASN1_OBJECT_find(oid_stack, oid) >= 0) |
589 | 0 | ret = TRUE; |
590 | |
|
591 | 0 | sk_ASN1_OBJECT_pop_free(oid_stack, ASN1_OBJECT_free); |
592 | 0 | return ret; |
593 | 0 | } |
594 | | |
595 | | void x509_utils_print_info(const X509* xcert) |
596 | 0 | { |
597 | 0 | char* fp = nullptr; |
598 | 0 | char* issuer = nullptr; |
599 | 0 | char* subject = nullptr; |
600 | 0 | subject = x509_utils_get_subject(xcert); |
601 | 0 | issuer = x509_utils_get_issuer(xcert); |
602 | 0 | fp = (char*)x509_utils_get_hash(xcert, "sha256", nullptr); |
603 | |
|
604 | 0 | if (!fp) |
605 | 0 | { |
606 | 0 | WLog_ERR(TAG, "error computing fingerprint"); |
607 | 0 | goto out_free_issuer; |
608 | 0 | } |
609 | | |
610 | 0 | WLog_INFO(TAG, "Certificate details:"); |
611 | 0 | WLog_INFO(TAG, "\tSubject: %s", subject); |
612 | 0 | WLog_INFO(TAG, "\tIssuer: %s", issuer); |
613 | 0 | WLog_INFO(TAG, "\tThumbprint: %s", fp); |
614 | 0 | WLog_INFO(TAG, |
615 | 0 | "The above X.509 certificate could not be verified, possibly because you do not have " |
616 | 0 | "the CA certificate in your certificate store, or the certificate has expired. " |
617 | 0 | "Please look at the OpenSSL documentation on how to add a private CA to the store."); |
618 | 0 | free(fp); |
619 | 0 | out_free_issuer: |
620 | 0 | free(issuer); |
621 | 0 | free(subject); |
622 | 0 | } |
623 | | |
624 | | X509* x509_utils_from_pem(const char* data, size_t len, BOOL fromFile) |
625 | 1.05k | { |
626 | 1.05k | X509* x509 = nullptr; |
627 | 1.05k | BIO* bio = nullptr; |
628 | 1.05k | if (fromFile) |
629 | 0 | bio = BIO_new_file(data, "rb"); |
630 | 1.05k | else |
631 | 1.05k | { |
632 | 1.05k | if (len > INT_MAX) |
633 | 0 | return nullptr; |
634 | | |
635 | 1.05k | bio = BIO_new_mem_buf(data, (int)len); |
636 | 1.05k | } |
637 | | |
638 | 1.05k | if (!bio) |
639 | 0 | { |
640 | 0 | WLog_ERR(TAG, "BIO_new failed for certificate"); |
641 | 0 | return nullptr; |
642 | 0 | } |
643 | | |
644 | 1.05k | x509 = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr); |
645 | 1.05k | BIO_free_all(bio); |
646 | 1.05k | if (!x509) |
647 | 921 | WLog_ERR(TAG, "PEM_read_bio_X509 returned nullptr [input length %" PRIuz "]", len); |
648 | | |
649 | 1.05k | return x509; |
650 | 1.05k | } |
651 | | |
652 | | static WINPR_MD_TYPE hash_nid_to_winpr(int hash_nid) |
653 | 0 | { |
654 | 0 | switch (hash_nid) |
655 | 0 | { |
656 | 0 | case NID_md2: |
657 | 0 | return WINPR_MD_MD2; |
658 | 0 | case NID_md4: |
659 | 0 | return WINPR_MD_MD4; |
660 | 0 | case NID_md5: |
661 | 0 | return WINPR_MD_MD5; |
662 | 0 | case NID_sha1: |
663 | 0 | return WINPR_MD_SHA1; |
664 | 0 | case NID_sha224: |
665 | 0 | return WINPR_MD_SHA224; |
666 | 0 | case NID_sha256: |
667 | 0 | return WINPR_MD_SHA256; |
668 | 0 | case NID_sha384: |
669 | 0 | return WINPR_MD_SHA384; |
670 | 0 | case NID_sha512: |
671 | 0 | return WINPR_MD_SHA512; |
672 | 0 | case NID_ripemd160: |
673 | 0 | return WINPR_MD_RIPEMD160; |
674 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x1010101fL) && !defined(LIBRESSL_VERSION_NUMBER) |
675 | 0 | case NID_sha3_224: |
676 | 0 | return WINPR_MD_SHA3_224; |
677 | 0 | case NID_sha3_256: |
678 | 0 | return WINPR_MD_SHA3_256; |
679 | 0 | case NID_sha3_384: |
680 | 0 | return WINPR_MD_SHA3_384; |
681 | 0 | case NID_sha3_512: |
682 | 0 | return WINPR_MD_SHA3_512; |
683 | 0 | case NID_shake128: |
684 | 0 | return WINPR_MD_SHAKE128; |
685 | 0 | case NID_shake256: |
686 | 0 | return WINPR_MD_SHAKE256; |
687 | 0 | #endif |
688 | 0 | case NID_undef: |
689 | 0 | default: |
690 | 0 | return WINPR_MD_NONE; |
691 | 0 | } |
692 | 0 | } |
693 | | |
694 | | static WINPR_MD_TYPE get_rsa_pss_digest(const X509_ALGOR* alg) |
695 | 0 | { |
696 | 0 | WINPR_MD_TYPE ret = WINPR_MD_NONE; |
697 | 0 | WINPR_MD_TYPE message_digest = WINPR_MD_NONE; |
698 | 0 | WINPR_MD_TYPE mgf1_digest = WINPR_MD_NONE; |
699 | 0 | int param_type = 0; |
700 | 0 | const void* param_value = nullptr; |
701 | 0 | const ASN1_STRING* sequence = nullptr; |
702 | 0 | const unsigned char* inp = nullptr; |
703 | 0 | RSA_PSS_PARAMS* params = nullptr; |
704 | 0 | X509_ALGOR* mgf1_digest_alg = nullptr; |
705 | | |
706 | | /* The RSA-PSS digest is encoded in a complex structure, defined in |
707 | | https://www.rfc-editor.org/rfc/rfc4055.html. */ |
708 | 0 | X509_ALGOR_get0(nullptr, ¶m_type, ¶m_value, alg); |
709 | | |
710 | | /* param_type and param_value the parameter in ASN1_TYPE form, but split into two parameters. A |
711 | | SEQUENCE is has type V_ASN1_SEQUENCE, and the value is an ASN1_STRING with the encoded |
712 | | structure. */ |
713 | 0 | if (param_type != V_ASN1_SEQUENCE) |
714 | 0 | goto end; |
715 | 0 | sequence = param_value; |
716 | | |
717 | | /* Decode the structure. */ |
718 | 0 | inp = ASN1_STRING_get0_data(sequence); |
719 | 0 | params = d2i_RSA_PSS_PARAMS(nullptr, &inp, ASN1_STRING_length(sequence)); |
720 | 0 | if (params == nullptr) |
721 | 0 | goto end; |
722 | | |
723 | | /* RSA-PSS uses two hash algorithms, a message digest and also an MGF function which is, itself, |
724 | | parameterized by a hash function. Both fields default to SHA-1, so we must also check for the |
725 | | value being nullptr. */ |
726 | 0 | message_digest = WINPR_MD_SHA1; |
727 | 0 | if (params->hashAlgorithm != nullptr) |
728 | 0 | { |
729 | 0 | const ASN1_OBJECT* obj = nullptr; |
730 | 0 | X509_ALGOR_get0(&obj, nullptr, nullptr, params->hashAlgorithm); |
731 | 0 | message_digest = hash_nid_to_winpr(OBJ_obj2nid(obj)); |
732 | 0 | if (message_digest == WINPR_MD_NONE) |
733 | 0 | goto end; |
734 | 0 | } |
735 | | |
736 | 0 | mgf1_digest = WINPR_MD_SHA1; |
737 | 0 | if (params->maskGenAlgorithm != nullptr) |
738 | 0 | { |
739 | 0 | const ASN1_OBJECT* obj = nullptr; |
740 | 0 | int mgf_param_type = 0; |
741 | 0 | const void* mgf_param_value = nullptr; |
742 | 0 | const ASN1_STRING* mgf_param_sequence = nullptr; |
743 | | /* First, check this is MGF-1, the only one ever defined. */ |
744 | 0 | X509_ALGOR_get0(&obj, &mgf_param_type, &mgf_param_value, params->maskGenAlgorithm); |
745 | 0 | if (OBJ_obj2nid(obj) != NID_mgf1) |
746 | 0 | goto end; |
747 | | |
748 | | /* MGF-1 is, itself, parameterized by a hash function, encoded as an AlgorithmIdentifier. */ |
749 | 0 | if (mgf_param_type != V_ASN1_SEQUENCE) |
750 | 0 | goto end; |
751 | 0 | mgf_param_sequence = mgf_param_value; |
752 | 0 | inp = ASN1_STRING_get0_data(mgf_param_sequence); |
753 | 0 | mgf1_digest_alg = d2i_X509_ALGOR(nullptr, &inp, ASN1_STRING_length(mgf_param_sequence)); |
754 | 0 | if (mgf1_digest_alg == nullptr) |
755 | 0 | goto end; |
756 | | |
757 | | /* Finally, extract the digest. */ |
758 | 0 | X509_ALGOR_get0(&obj, nullptr, nullptr, mgf1_digest_alg); |
759 | 0 | mgf1_digest = hash_nid_to_winpr(OBJ_obj2nid(obj)); |
760 | 0 | if (mgf1_digest == WINPR_MD_NONE) |
761 | 0 | goto end; |
762 | 0 | } |
763 | | |
764 | | /* If the two digests do not match, it is ambiguous which to return. tls-server-end-point leaves |
765 | | it undefined, so return none. |
766 | | https://www.rfc-editor.org/rfc/rfc5929.html#section-4.1 */ |
767 | 0 | if (message_digest != mgf1_digest) |
768 | 0 | goto end; |
769 | 0 | ret = message_digest; |
770 | |
|
771 | 0 | end: |
772 | 0 | RSA_PSS_PARAMS_free(params); |
773 | 0 | X509_ALGOR_free(mgf1_digest_alg); |
774 | 0 | return ret; |
775 | 0 | } |
776 | | |
777 | | WINPR_MD_TYPE x509_utils_get_signature_alg(const X509* xcert) |
778 | 0 | { |
779 | 0 | WINPR_ASSERT(xcert); |
780 | |
|
781 | 0 | const int nid = X509_get_signature_nid(xcert); |
782 | |
|
783 | 0 | if (nid == NID_rsassaPss) |
784 | 0 | { |
785 | 0 | const X509_ALGOR* alg = nullptr; |
786 | 0 | X509_get0_signature(nullptr, &alg, xcert); |
787 | 0 | return get_rsa_pss_digest(alg); |
788 | 0 | } |
789 | | |
790 | 0 | int hash_nid = 0; |
791 | 0 | if (OBJ_find_sigid_algs(nid, &hash_nid, nullptr) != 1) |
792 | 0 | return WINPR_MD_NONE; |
793 | | |
794 | 0 | return hash_nid_to_winpr(hash_nid); |
795 | 0 | } |
796 | | |
797 | | char* x509_utils_get_common_name(const X509* xcert, size_t* plength) |
798 | 0 | { |
799 | 0 | X509_NAME* subject_name = X509_get_subject_name(xcert); |
800 | 0 | if (subject_name == nullptr) |
801 | 0 | return nullptr; |
802 | | |
803 | 0 | const int index = X509_NAME_get_index_by_NID(subject_name, NID_commonName, -1); |
804 | 0 | if (index < 0) |
805 | 0 | return nullptr; |
806 | | |
807 | 0 | const X509_NAME_ENTRY* entry = X509_NAME_get_entry(subject_name, index); |
808 | 0 | if (entry == nullptr) |
809 | 0 | return nullptr; |
810 | | |
811 | 0 | const ASN1_STRING* entry_data = X509_NAME_ENTRY_get_data(entry); |
812 | 0 | if (entry_data == nullptr) |
813 | 0 | return nullptr; |
814 | | |
815 | 0 | BYTE* common_name_raw = nullptr; |
816 | 0 | const int length = ASN1_STRING_to_UTF8(&common_name_raw, entry_data); |
817 | 0 | if (length < 0) |
818 | 0 | return nullptr; |
819 | | |
820 | 0 | if (plength) |
821 | 0 | *plength = (size_t)length; |
822 | |
|
823 | 0 | char* common_name = _strdup((char*)common_name_raw); |
824 | 0 | OPENSSL_free(common_name_raw); |
825 | 0 | return common_name; |
826 | 0 | } |
827 | | |
828 | | static int verify_cb(int ok, X509_STORE_CTX* csc) |
829 | 0 | { |
830 | 0 | if (ok != 1) |
831 | 0 | { |
832 | 0 | WINPR_ASSERT(csc); |
833 | 0 | int err = X509_STORE_CTX_get_error(csc); |
834 | 0 | int derr = X509_STORE_CTX_get_error_depth(csc); |
835 | 0 | X509* where = X509_STORE_CTX_get_current_cert(csc); |
836 | 0 | const char* what = X509_verify_cert_error_string(err); |
837 | 0 | char* name = x509_utils_get_subject(where); |
838 | |
|
839 | 0 | WLog_WARN(TAG, "Certificate verification failure '%s (%d)' at stack position %d", what, err, |
840 | 0 | derr); |
841 | 0 | WLog_WARN(TAG, "%s", name); |
842 | |
|
843 | 0 | free(name); |
844 | 0 | } |
845 | 0 | return ok; |
846 | 0 | } |
847 | | |
848 | | BOOL x509_utils_verify(X509* xcert, STACK_OF(X509) * chain, const char* certificate_store_path) |
849 | 0 | { |
850 | 0 | const int purposes[3] = { X509_PURPOSE_SSL_SERVER, X509_PURPOSE_SSL_CLIENT, X509_PURPOSE_ANY }; |
851 | 0 | X509_STORE_CTX* csc = nullptr; |
852 | 0 | BOOL status = FALSE; |
853 | 0 | X509_LOOKUP* lookup = nullptr; |
854 | |
|
855 | 0 | if (!xcert) |
856 | 0 | return FALSE; |
857 | | |
858 | 0 | X509_STORE* cert_ctx = X509_STORE_new(); |
859 | |
|
860 | 0 | if (cert_ctx == nullptr) |
861 | 0 | goto end; |
862 | | |
863 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
864 | | OpenSSL_add_all_algorithms(); |
865 | | #else |
866 | 0 | OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS | |
867 | 0 | OPENSSL_INIT_LOAD_CONFIG, |
868 | 0 | nullptr); |
869 | 0 | #endif |
870 | |
|
871 | 0 | if (X509_STORE_set_default_paths(cert_ctx) != 1) |
872 | 0 | goto end; |
873 | | |
874 | 0 | lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir()); |
875 | |
|
876 | 0 | if (lookup == nullptr) |
877 | 0 | goto end; |
878 | | |
879 | 0 | X509_LOOKUP_add_dir(lookup, nullptr, X509_FILETYPE_DEFAULT); |
880 | |
|
881 | 0 | if (certificate_store_path != nullptr) |
882 | 0 | { |
883 | 0 | X509_LOOKUP_add_dir(lookup, certificate_store_path, X509_FILETYPE_PEM); |
884 | 0 | } |
885 | |
|
886 | 0 | X509_STORE_set_flags(cert_ctx, 0); |
887 | |
|
888 | 0 | for (size_t i = 0; i < ARRAYSIZE(purposes); i++) |
889 | 0 | { |
890 | 0 | int err = -1; |
891 | 0 | int rc = -1; |
892 | 0 | int purpose = purposes[i]; |
893 | 0 | csc = X509_STORE_CTX_new(); |
894 | |
|
895 | 0 | if (csc == nullptr) |
896 | 0 | goto skip; |
897 | 0 | if (!X509_STORE_CTX_init(csc, cert_ctx, xcert, chain)) |
898 | 0 | goto skip; |
899 | | |
900 | 0 | X509_STORE_CTX_set_purpose(csc, purpose); |
901 | 0 | X509_STORE_CTX_set_verify_cb(csc, verify_cb); |
902 | |
|
903 | 0 | rc = X509_verify_cert(csc); |
904 | 0 | err = X509_STORE_CTX_get_error(csc); |
905 | 0 | skip: |
906 | 0 | X509_STORE_CTX_free(csc); |
907 | 0 | if (rc == 1) |
908 | 0 | { |
909 | 0 | status = TRUE; |
910 | 0 | break; |
911 | 0 | } |
912 | 0 | else if (err != X509_V_ERR_INVALID_PURPOSE) |
913 | 0 | break; |
914 | 0 | } |
915 | | |
916 | 0 | X509_STORE_free(cert_ctx); |
917 | 0 | end: |
918 | 0 | return status; |
919 | 0 | } |
920 | | |
921 | | char* x509_utils_bio_read(BIO* bio, size_t* plen) |
922 | 363 | { |
923 | 363 | char* buffer = nullptr; |
924 | 363 | WINPR_ASSERT(bio); |
925 | | |
926 | 363 | if (plen) |
927 | 228 | *plen = 0; |
928 | | |
929 | 363 | BIO_flush(bio); |
930 | | |
931 | 363 | const UINT64 size = BIO_number_written(bio); |
932 | 363 | if (size > INT_MAX) |
933 | 0 | return nullptr; |
934 | | |
935 | 363 | buffer = calloc(1, (size_t)size + 1ull); |
936 | | |
937 | 363 | if (!buffer) |
938 | 0 | return nullptr; |
939 | | |
940 | 363 | ERR_clear_error(); |
941 | 363 | const int rc = BIO_read(bio, buffer, (int)size); |
942 | 363 | if (rc <= 0) |
943 | 0 | goto fail; |
944 | | |
945 | 363 | if (plen) |
946 | 228 | *plen = size; |
947 | 363 | return buffer; |
948 | | |
949 | 0 | fail: |
950 | 0 | free(buffer); |
951 | 0 | return nullptr; |
952 | 363 | } |