Coverage Report

Created: 2026-05-30 06:41

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