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