22 #include <openssl/objects.h>
23 #include <openssl/x509v3.h>
24 #include <openssl/pem.h>
25 #include <openssl/err.h>
27 #include <freerdp/config.h>
29 #include <winpr/crt.h>
30 #include <winpr/string.h>
31 #include <winpr/assert.h>
33 #include <freerdp/log.h>
35 #include "x509_utils.h"
37 #define TAG FREERDP_TAG("crypto")
39 BYTE* x509_utils_get_hash(
const X509* xcert,
const char* hash,
size_t* length)
41 UINT32 fp_len = EVP_MAX_MD_SIZE;
43 const EVP_MD* md = EVP_get_digestbyname(hash);
46 WLog_ERR(TAG,
"System does not support %s hash!", hash);
49 if (!xcert || !length)
51 WLog_ERR(TAG,
"Invalid arguments: xcert=%p, length=%p", xcert, length);
55 fp = calloc(fp_len + 1,
sizeof(BYTE));
58 WLog_ERR(TAG,
"could not allocate %" PRIuz
" bytes", fp_len);
62 if (X509_digest(xcert, md, fp, &fp_len) != 1)
65 WLog_ERR(TAG,
"certificate does not have a %s hash!", hash);
73 static char* crypto_print_name(
const X509_NAME* name)
76 BIO* outBIO = BIO_new(BIO_s_mem());
78 if (X509_NAME_print_ex(outBIO, name, 0, XN_FLAG_ONELINE) > 0)
80 UINT64 size = BIO_number_written(outBIO);
83 buffer = calloc(1, (
size_t)size + 1);
89 const int rc = BIO_read(outBIO, buffer, (
int)size);
103 char* x509_utils_get_subject(
const X509* xcert)
105 char* subject = NULL;
108 WLog_ERR(TAG,
"Invalid certificate %p", xcert);
111 subject = crypto_print_name(X509_get_subject_name(xcert));
113 WLog_WARN(TAG,
"certificate does not have a subject!");
119 static const char* general_name_type_labels[] = {
"OTHERNAME",
"EMAIL ",
"DNS ",
120 "X400 ",
"DIRNAME ",
"EDIPARTY ",
121 "URI ",
"IPADD ",
"RID " };
123 static const char* general_name_type_label(
int general_name_type)
125 if ((0 <= general_name_type) &&
126 ((
size_t)general_name_type < ARRAYSIZE(general_name_type_labels)))
128 return general_name_type_labels[general_name_type];
132 static char buffer[80] = { 0 };
133 (void)snprintf(buffer,
sizeof(buffer),
"Unknown general name type (%d)", general_name_type);
183 typedef int (*general_name_mapper_pr)(GENERAL_NAME* name,
void* data,
int index,
int count);
185 static void map_subject_alt_name(
const X509* x509,
int general_name_type,
186 general_name_mapper_pr mapper,
void* data)
189 STACK_OF(GENERAL_NAME)* gens = NULL;
190 gens = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL, NULL);
197 num = sk_GENERAL_NAME_num(gens);
199 for (
int i = 0; (i < num); i++)
201 GENERAL_NAME* name = sk_GENERAL_NAME_value(gens, i);
205 if ((general_name_type == GEN_ALL) || (general_name_type == name->type))
207 if (!mapper(name, data, i, num))
215 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
237 typedef struct string_list
245 static void string_list_initialize(string_list* list)
250 list->maximum = INT_MAX;
253 static void string_list_allocate(string_list* list,
size_t allocate_count)
255 if (!list->strings && list->allocated == 0)
257 list->strings = (
char**)calloc(allocate_count,
sizeof(
char*));
258 list->allocated = list->strings ? allocate_count : 0;
263 static void string_list_free(string_list* list)
268 free((
void*)list->strings);
271 static int extract_string(GENERAL_NAME* name,
void* data,
int index,
int count)
273 string_list* list = data;
274 unsigned char* cstring = 0;
275 ASN1_STRING* str = NULL;
282 str = name->d.uniformResourceIdentifier;
286 str = name->d.dNSName;
290 str = name->d.rfc822Name;
297 if ((ASN1_STRING_to_UTF8(&cstring, str)) < 0)
299 WLog_ERR(TAG,
"ASN1_STRING_to_UTF8() failed for %s: %s",
300 general_name_type_label(name->type), ERR_error_string(ERR_get_error(), NULL));
304 string_list_allocate(list, WINPR_ASSERTING_INT_CAST(WINPR_CIPHER_TYPE, count));
306 if (list->allocated <= 0)
308 OPENSSL_free(cstring);
312 list->strings[list->count] = (
char*)cstring;
315 if (list->count >= list->maximum)
341 typedef struct object_list
343 ASN1_OBJECT* type_id;
350 static void object_list_initialize(object_list* list)
356 list->maximum = INT_MAX;
359 static void object_list_allocate(object_list* list,
size_t allocate_count)
361 if (!list->strings && (list->allocated == 0) && (allocate_count > 0))
363 list->strings = (
char**)calloc(allocate_count,
sizeof(list->strings[0]));
364 list->allocated = list->strings ? allocate_count : 0;
369 static char* object_string(ASN1_TYPE*
object)
372 unsigned char* utf8String = NULL;
375 const int length = ASN1_STRING_to_UTF8(&utf8String, object->value.asn1_string);
382 result = strndup((
char*)utf8String, WINPR_ASSERTING_INT_CAST(
size_t, length));
383 OPENSSL_free(utf8String);
387 static void object_list_free(object_list* list)
390 free((
void*)list->strings);
393 static int extract_othername_object_as_string(GENERAL_NAME* name,
void* data,
int index,
int count)
395 object_list* list = data;
401 if (name->type != GEN_OTHERNAME)
406 if (0 != OBJ_cmp(name->d.otherName->type_id, list->type_id))
411 object_list_allocate(list, WINPR_ASSERTING_INT_CAST(
size_t, count));
413 if (list->allocated <= 0)
418 list->strings[list->count] = object_string(name->d.otherName->value);
420 if (list->strings[list->count])
425 if (list->count >= list->maximum)
433 char* x509_utils_get_email(
const X509* x509)
437 string_list_initialize(&list);
439 map_subject_alt_name(x509, GEN_EMAIL, extract_string, &list);
443 string_list_free(&list);
447 result = _strdup(list.strings[0]);
448 OPENSSL_free(list.strings[0]);
449 string_list_free(&list);
453 char* x509_utils_get_upn(
const X509* x509)
456 object_list list = { 0 };
457 object_list_initialize(&list);
458 list.type_id = OBJ_nid2obj(NID_ms_upn);
460 map_subject_alt_name(x509, GEN_OTHERNAME, extract_othername_object_as_string, &list);
464 object_list_free(&list);
468 result = list.strings[0];
469 object_list_free(&list);
473 char* x509_utils_get_date(
const X509* x509, BOOL startDate)
477 const ASN1_TIME* date = startDate ? X509_get0_notBefore(x509) : X509_get0_notAfter(x509);
481 BIO* bmem = BIO_new(BIO_s_mem());
486 if (ASN1_TIME_print(bmem, date))
488 BUF_MEM* bptr = NULL;
490 BIO_get_mem_ptr(bmem, &bptr);
491 str = strndup(bptr->data, bptr->length);
500 void x509_utils_dns_names_free(
size_t count,
size_t* lengths,
char** dns_names)
506 for (
size_t i = 0; i < count; i++)
510 OPENSSL_free(dns_names[i]);
514 free((
void*)dns_names);
518 char** x509_utils_get_dns_names(
const X509* x509,
size_t* count,
size_t** lengths)
521 string_list list = { 0 };
522 string_list_initialize(&list);
523 map_subject_alt_name(x509, GEN_DNS, extract_string, &list);
524 (*count) = list.count;
528 string_list_free(&list);
534 result = (
char**)calloc(list.count,
sizeof(*result));
535 (*lengths) = calloc(list.count,
sizeof(**lengths));
537 if (!result || !(*lengths))
539 string_list_free(&list);
547 for (
size_t i = 0; i < list.count; i++)
549 result[i] = list.strings[i];
550 (*lengths)[i] = strlen(result[i]);
553 string_list_free(&list);
557 char* x509_utils_get_issuer(
const X509* xcert)
562 WLog_ERR(TAG,
"Invalid certificate %p", xcert);
565 issuer = crypto_print_name(X509_get_issuer_name(xcert));
567 WLog_WARN(TAG,
"certificate does not have an issuer!");
571 BOOL x509_utils_check_eku(
const X509* xcert,
int nid)
574 STACK_OF(ASN1_OBJECT)* oid_stack = NULL;
575 ASN1_OBJECT* oid = NULL;
580 oid = OBJ_nid2obj(nid);
584 oid_stack = X509_get_ext_d2i(xcert, NID_ext_key_usage, NULL, NULL);
588 if (sk_ASN1_OBJECT_find(oid_stack, oid) >= 0)
591 sk_ASN1_OBJECT_pop_free(oid_stack, ASN1_OBJECT_free);
595 void x509_utils_print_info(
const X509* xcert)
599 char* subject = NULL;
600 subject = x509_utils_get_subject(xcert);
601 issuer = x509_utils_get_issuer(xcert);
602 fp = (
char*)x509_utils_get_hash(xcert,
"sha256", NULL);
606 WLog_ERR(TAG,
"error computing fingerprint");
607 goto out_free_issuer;
610 WLog_INFO(TAG,
"Certificate details:");
611 WLog_INFO(TAG,
"\tSubject: %s", subject);
612 WLog_INFO(TAG,
"\tIssuer: %s", issuer);
613 WLog_INFO(TAG,
"\tThumbprint: %s", fp);
615 "The above X.509 certificate could not be verified, possibly because you do not have "
616 "the CA certificate in your certificate store, or the certificate has expired. "
617 "Please look at the OpenSSL documentation on how to add a private CA to the store.");
624 X509* x509_utils_from_pem(
const char* data,
size_t len, BOOL fromFile)
629 bio = BIO_new_file(data,
"rb");
635 bio = BIO_new_mem_buf(data, (
int)len);
640 WLog_ERR(TAG,
"BIO_new failed for certificate");
644 x509 = PEM_read_bio_X509(bio, NULL, NULL, 0);
647 WLog_ERR(TAG,
"PEM_read_bio_X509 returned NULL [input length %" PRIuz
"]", len);
652 static WINPR_MD_TYPE hash_nid_to_winpr(
int hash_nid)
663 return WINPR_MD_SHA1;
665 return WINPR_MD_SHA224;
667 return WINPR_MD_SHA256;
669 return WINPR_MD_SHA384;
671 return WINPR_MD_SHA512;
673 return WINPR_MD_RIPEMD160;
674 #if (OPENSSL_VERSION_NUMBER >= 0x1010101fL) && !defined(LIBRESSL_VERSION_NUMBER)
676 return WINPR_MD_SHA3_224;
678 return WINPR_MD_SHA3_256;
680 return WINPR_MD_SHA3_384;
682 return WINPR_MD_SHA3_512;
684 return WINPR_MD_SHAKE128;
686 return WINPR_MD_SHAKE256;
690 return WINPR_MD_NONE;
694 static WINPR_MD_TYPE get_rsa_pss_digest(
const X509_ALGOR* alg)
696 WINPR_MD_TYPE ret = WINPR_MD_NONE;
697 WINPR_MD_TYPE message_digest = WINPR_MD_NONE;
698 WINPR_MD_TYPE mgf1_digest = WINPR_MD_NONE;
700 const void* param_value = NULL;
701 const ASN1_STRING* sequence = NULL;
702 const unsigned char* inp = NULL;
703 RSA_PSS_PARAMS* params = NULL;
704 X509_ALGOR* mgf1_digest_alg = NULL;
708 X509_ALGOR_get0(NULL, ¶m_type, ¶m_value, alg);
713 if (param_type != V_ASN1_SEQUENCE)
715 sequence = param_value;
718 inp = ASN1_STRING_get0_data(sequence);
719 params = d2i_RSA_PSS_PARAMS(NULL, &inp, ASN1_STRING_length(sequence));
726 message_digest = WINPR_MD_SHA1;
727 if (params->hashAlgorithm != NULL)
729 const ASN1_OBJECT* obj = NULL;
730 X509_ALGOR_get0(&obj, NULL, NULL, params->hashAlgorithm);
731 message_digest = hash_nid_to_winpr(OBJ_obj2nid(obj));
732 if (message_digest == WINPR_MD_NONE)
736 mgf1_digest = WINPR_MD_SHA1;
737 if (params->maskGenAlgorithm != NULL)
739 const ASN1_OBJECT* obj = NULL;
740 int mgf_param_type = 0;
741 const void* mgf_param_value = NULL;
742 const ASN1_STRING* mgf_param_sequence = NULL;
744 X509_ALGOR_get0(&obj, &mgf_param_type, &mgf_param_value, params->maskGenAlgorithm);
745 if (OBJ_obj2nid(obj) != NID_mgf1)
749 if (mgf_param_type != V_ASN1_SEQUENCE)
751 mgf_param_sequence = mgf_param_value;
752 inp = ASN1_STRING_get0_data(mgf_param_sequence);
753 mgf1_digest_alg = d2i_X509_ALGOR(NULL, &inp, ASN1_STRING_length(mgf_param_sequence));
754 if (mgf1_digest_alg == NULL)
758 X509_ALGOR_get0(&obj, NULL, NULL, mgf1_digest_alg);
759 mgf1_digest = hash_nid_to_winpr(OBJ_obj2nid(obj));
760 if (mgf1_digest == WINPR_MD_NONE)
767 if (message_digest != mgf1_digest)
769 ret = message_digest;
772 RSA_PSS_PARAMS_free(params);
773 X509_ALGOR_free(mgf1_digest_alg);
777 WINPR_MD_TYPE x509_utils_get_signature_alg(
const X509* xcert)
781 const int nid = X509_get_signature_nid(xcert);
783 if (nid == NID_rsassaPss)
785 const X509_ALGOR* alg = NULL;
786 X509_get0_signature(NULL, &alg, xcert);
787 return get_rsa_pss_digest(alg);
791 if (OBJ_find_sigid_algs(nid, &hash_nid, NULL) != 1)
792 return WINPR_MD_NONE;
794 return hash_nid_to_winpr(hash_nid);
797 char* x509_utils_get_common_name(
const X509* xcert,
size_t* plength)
799 X509_NAME* subject_name = X509_get_subject_name(xcert);
800 if (subject_name == NULL)
803 const int index = X509_NAME_get_index_by_NID(subject_name, NID_commonName, -1);
807 const X509_NAME_ENTRY* entry = X509_NAME_get_entry(subject_name, index);
811 const ASN1_STRING* entry_data = X509_NAME_ENTRY_get_data(entry);
812 if (entry_data == NULL)
815 BYTE* common_name_raw = NULL;
816 const int length = ASN1_STRING_to_UTF8(&common_name_raw, entry_data);
821 *plength = (size_t)length;
823 char* common_name = _strdup((
char*)common_name_raw);
824 OPENSSL_free(common_name_raw);
828 static int verify_cb(
int ok, X509_STORE_CTX* csc)
833 int err = X509_STORE_CTX_get_error(csc);
834 int derr = X509_STORE_CTX_get_error_depth(csc);
835 X509* where = X509_STORE_CTX_get_current_cert(csc);
836 const char* what = X509_verify_cert_error_string(err);
837 char* name = x509_utils_get_subject(where);
839 WLog_WARN(TAG,
"Certificate verification failure '%s (%d)' at stack position %d", what, err,
841 WLog_WARN(TAG,
"%s", name);
848 BOOL x509_utils_verify(X509* xcert, STACK_OF(X509) * chain,
const char* certificate_store_path)
850 const int purposes[3] = { X509_PURPOSE_SSL_SERVER, X509_PURPOSE_SSL_CLIENT, X509_PURPOSE_ANY };
851 X509_STORE_CTX* csc = NULL;
853 X509_LOOKUP* lookup = NULL;
858 X509_STORE* cert_ctx = X509_STORE_new();
860 if (cert_ctx == NULL)
863 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
864 OpenSSL_add_all_algorithms();
866 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS |
867 OPENSSL_INIT_LOAD_CONFIG,
871 if (X509_STORE_set_default_paths(cert_ctx) != 1)
874 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
879 X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
881 if (certificate_store_path != NULL)
883 X509_LOOKUP_add_dir(lookup, certificate_store_path, X509_FILETYPE_PEM);
886 X509_STORE_set_flags(cert_ctx, 0);
888 for (
size_t i = 0; i < ARRAYSIZE(purposes); i++)
892 int purpose = purposes[i];
893 csc = X509_STORE_CTX_new();
897 if (!X509_STORE_CTX_init(csc, cert_ctx, xcert, chain))
900 X509_STORE_CTX_set_purpose(csc, purpose);
901 X509_STORE_CTX_set_verify_cb(csc, verify_cb);
903 rc = X509_verify_cert(csc);
904 err = X509_STORE_CTX_get_error(csc);
906 X509_STORE_CTX_free(csc);
912 else if (err != X509_V_ERR_INVALID_PURPOSE)
916 X509_STORE_free(cert_ctx);