-
Notifications
You must be signed in to change notification settings - Fork 102
/
libspdm_crypt_cert.c
2029 lines (1827 loc) · 73.3 KB
/
libspdm_crypt_cert.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright Notice:
* Copyright 2021-2024 DMTF. All rights reserved.
* License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
**/
#include "internal/libspdm_crypt_lib.h"
#if LIBSPDM_CERT_PARSE_SUPPORT
/**pathLenConstraint is optional.
* In https://www.pkisolutions.com/basic-constraints-certificate-extension/:
* pathLenConstraint: How many CAs are allowed in the chain below current CA certificate.
* This setting has no meaning for end entity certificates.
**/
/**
* leaf cert spdm extension len
* len > 2 * (spdm id-DMTF-spdm size + 2)
**/
#ifndef LIBSPDM_MAX_EXTENSION_LEN
#define LIBSPDM_MAX_EXTENSION_LEN 30
#endif
#ifndef LIBSPDM_MAX_NAME_SIZE
#define LIBSPDM_MAX_NAME_SIZE 100
#endif
/*max public key encryption algo oid len*/
#ifndef LIBSPDM_MAX_ENCRYPTION_ALGO_OID_LEN
#define LIBSPDM_MAX_ENCRYPTION_ALGO_OID_LEN 10
#endif
/*leaf cert basic constraints len,CA = false: 30 03 01 01 00*/
#ifndef LIBSPDM_MAX_BASIC_CONSTRAINTS_CA_LEN
#define LIBSPDM_MAX_BASIC_CONSTRAINTS_CA_LEN 5
#endif
/**
* 0x02 is integer;
* 0x82 indicates that the length is expressed in two bytes;
* 0x01 and 0x01 are rsa key len;
**/
#if (LIBSPDM_RSA_SSA_2048_SUPPORT) || (LIBSPDM_RSA_PSS_2048_SUPPORT)
#define KEY_ENCRY_ALGO_RSA2048_FLAG {0x02, 0x82, 0x01, 0x01}
/* the other case is ASN1 code different when integer is 1 on highest position*/
#define KEY_ENCRY_ALGO_RSA2048_FLAG_OTHER {0x02, 0x82, 0x01, 0x00}
#endif
#if (LIBSPDM_RSA_SSA_3072_SUPPORT) || (LIBSPDM_RSA_PSS_3072_SUPPORT)
#define KEY_ENCRY_ALGO_RSA3072_FLAG {0x02, 0x82, 0x01, 0x81}
/* the other case is ASN1 code different when integer is 1 on highest position*/
#define KEY_ENCRY_ALGO_RSA3072_FLAG_OTHER {0x02, 0x82, 0x01, 0x80}
#endif
#if (LIBSPDM_RSA_SSA_4096_SUPPORT) || (LIBSPDM_RSA_PSS_4096_SUPPORT)
#define KEY_ENCRY_ALGO_RSA4096_FLAG {0x02, 0x82, 0x02, 0x01}
/* the other case is ASN1 code different when integer is 1 on highest position*/
#define KEY_ENCRY_ALGO_RSA4096_FLAG_OTHER {0x02, 0x82, 0x02, 0x00}
#endif
/**
* https://oidref.com/1.2.840.10045.3.1.7
* ECC256 curve OID: 1.2.840.10045.3.1.7
* https://oidref.com/1.3.132.0.34
* ECC384 curve OID: 1.3.132.0.34
* https://oidref.com/1.3.132.0.35
* ECC521 curve OID: 1.3.132.0.35
**/
#if LIBSPDM_ECDSA_P256_SUPPORT
#define KEY_ENCRY_ALGO_ECC256_OID {0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07}
#endif
#if LIBSPDM_ECDSA_P384_SUPPORT
#define KEY_ENCRY_ALGO_ECC384_OID {0x2B, 0x81, 0x04, 0x00, 0x22}
#endif
#if LIBSPDM_ECDSA_P521_SUPPORT
#define KEY_ENCRY_ALGO_ECC521_OID {0x2B, 0x81, 0x04, 0x00, 0x23}
#endif
/**
* EDxxx OID: https://datatracker.ietf.org/doc/html/rfc8420
* ED448 OID: 1.3.101.113
* ED25519 OID: 1.3.101.112
**/
#if LIBSPDM_EDDSA_ED25519_SUPPORT
#define ENCRY_ALGO_ED25519_OID {0x2B, 0x65, 0x70}
#endif
#if LIBSPDM_EDDSA_ED448_SUPPORT
#define ENCRY_ALGO_ED448_OID {0x2B, 0x65, 0x71}
#endif
/*leaf cert basic_constraints false case1: CA: false and CA object is excluded */
#define BASIC_CONSTRAINTS_STRING_FALSE_CASE1 {0x30, 0x00}
/*leaf cert basic_constraints false case2: CA: false */
#define BASIC_CONSTRAINTS_STRING_FALSE_CASE2 {0x30, 0x03, 0x01, 0x01, 0x00}
/*leaf cert basic_constraints true case: CA: true */
#define BASIC_CONSTRAINTS_STRING_TRUE_CASE {0x30, 0x03, 0x01, 0x01, 0xFF}
/**
* Retrieve the asymmetric public key from one DER-encoded X509 certificate.
*
* @param cert Pointer to the DER-encoded X509 certificate.
* @param cert_size Size of the X509 certificate in bytes.
* @param context Pointer to newly generated asymmetric context which contain the retrieved public
* key component. Use libspdm_asym_free() function to free the resource.
*
* @retval true public key was retrieved successfully.
* @retval false Fail to retrieve public key from X509 certificate.
**/
typedef bool (*libspdm_asym_get_public_key_from_x509_func)(const uint8_t *cert,
size_t cert_size,
void **context);
/**
* Return asymmetric GET_PUBLIC_KEY_FROM_X509 function, based upon the negotiated asymmetric algorithm.
*
* @param base_asym_algo SPDM base_asym_algo
*
* @return asymmetric GET_PUBLIC_KEY_FROM_X509 function
**/
libspdm_asym_get_public_key_from_x509_func
static libspdm_get_asym_get_public_key_from_x509(uint32_t base_asym_algo)
{
switch (base_asym_algo) {
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_3072:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_4096:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_2048:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_3072:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_4096:
#if (LIBSPDM_RSA_SSA_SUPPORT) || (LIBSPDM_RSA_PSS_SUPPORT)
#if !LIBSPDM_RSA_SSA_2048_SUPPORT
LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048);
#endif
#if !LIBSPDM_RSA_SSA_3072_SUPPORT
LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_3072);
#endif
#if !LIBSPDM_RSA_SSA_4096_SUPPORT
LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_4096);
#endif
#if !LIBSPDM_RSA_PSS_2048_SUPPORT
LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_2048);
#endif
#if !LIBSPDM_RSA_PSS_3072_SUPPORT
LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_3072);
#endif
#if !LIBSPDM_RSA_PSS_4096_SUPPORT
LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_4096);
#endif
return libspdm_rsa_get_public_key_from_x509;
#else
LIBSPDM_ASSERT(false);
break;
#endif
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P256:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P384:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P521:
#if LIBSPDM_ECDSA_SUPPORT
#if !LIBSPDM_ECDSA_P256_SUPPORT
LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P256);
#endif
#if !LIBSPDM_ECDSA_P384_SUPPORT
LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P384);
#endif
#if !LIBSPDM_ECDSA_P521_SUPPORT
LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P521);
#endif
return libspdm_ec_get_public_key_from_x509;
#else
LIBSPDM_ASSERT(false);
break;
#endif
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED25519:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED448:
#if (LIBSPDM_EDDSA_ED25519_SUPPORT) || (LIBSPDM_EDDSA_ED448_SUPPORT)
#if !LIBSPDM_EDDSA_ED25519_SUPPORT
LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED25519);
#endif
#if !LIBSPDM_EDDSA_ED448_SUPPORT
LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED448);
#endif
return libspdm_ecd_get_public_key_from_x509;
#else
LIBSPDM_ASSERT(false);
break;
#endif
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_SM2_ECC_SM2_P256:
#if LIBSPDM_SM2_DSA_SUPPORT
return libspdm_sm2_get_public_key_from_x509;
#else
LIBSPDM_ASSERT(false);
break;
#endif
default:
LIBSPDM_ASSERT(false);
break;
}
return NULL;
}
/**
* Retrieve the asymmetric public key from one DER-encoded X509 certificate,
* based upon negotiated asymmetric algorithm.
*
* @param base_asym_algo SPDM base_asym_algo
* @param cert Pointer to the DER-encoded X509 certificate.
* @param cert_size size of the X509 certificate in bytes.
* @param context Pointer to newly generated asymmetric context which contain the retrieved public key component.
* Use libspdm_asym_free() function to free the resource.
*
* @retval true public key was retrieved successfully.
* @retval false Fail to retrieve public key from X509 certificate.
**/
bool libspdm_asym_get_public_key_from_x509(uint32_t base_asym_algo,
const uint8_t *cert,
size_t cert_size,
void **context)
{
libspdm_asym_get_public_key_from_x509_func get_public_key_from_x509_function;
get_public_key_from_x509_function = libspdm_get_asym_get_public_key_from_x509(base_asym_algo);
if (get_public_key_from_x509_function == NULL) {
return false;
}
return get_public_key_from_x509_function(cert, cert_size, context);
}
/**
* Return requester asymmetric GET_PUBLIC_KEY_FROM_X509 function, based upon the negotiated requester asymmetric algorithm.
*
* @param req_base_asym_alg SPDM req_base_asym_alg
*
* @return requester asymmetric GET_PUBLIC_KEY_FROM_X509 function
**/
static libspdm_asym_get_public_key_from_x509_func
libspdm_get_req_asym_get_public_key_from_x509(uint16_t req_base_asym_alg)
{
return libspdm_get_asym_get_public_key_from_x509(req_base_asym_alg);
}
/**
* Retrieve the asymmetric public key from one DER-encoded X509 certificate,
* based upon negotiated requester asymmetric algorithm.
*
* @param req_base_asym_alg SPDM req_base_asym_alg
* @param cert Pointer to the DER-encoded X509 certificate.
* @param cert_size size of the X509 certificate in bytes.
* @param context Pointer to newly generated asymmetric context which contain the retrieved public key component.
* Use libspdm_asym_free() function to free the resource.
*
* @retval true public key was retrieved successfully.
* @retval false Fail to retrieve public key from X509 certificate.
**/
bool libspdm_req_asym_get_public_key_from_x509(uint16_t req_base_asym_alg,
const uint8_t *cert,
size_t cert_size,
void **context)
{
libspdm_asym_get_public_key_from_x509_func get_public_key_from_x509_function;
get_public_key_from_x509_function =
libspdm_get_req_asym_get_public_key_from_x509(req_base_asym_alg);
if (get_public_key_from_x509_function == NULL) {
return false;
}
return get_public_key_from_x509_function(cert, cert_size, context);
}
/**
* Check the X509 DataTime is within a valid range.
*
* @param spdm_context A pointer to the SPDM context.
* @param from notBefore Pointer to date_time object.
* @param from_size notBefore date_time object size.
* @param to notAfter Pointer to date_time object.
* @param to_size notAfter date_time object size.
*
* @retval true verification pass.
* @retval false verification fail.
**/
static bool libspdm_internal_x509_date_time_check(const uint8_t *from,
size_t from_size,
const uint8_t *to,
size_t to_size)
{
int32_t ret;
bool status;
uint8_t f0[64];
uint8_t t0[64];
size_t f0_size;
size_t t0_size;
f0_size = 64;
t0_size = 64;
status = libspdm_x509_set_date_time("19700101000000Z", f0, &f0_size);
if (!status) {
return false;
}
status = libspdm_x509_set_date_time("99991231235959Z", t0, &t0_size);
if (!status) {
return false;
}
/* from >= f0*/
ret = libspdm_x509_compare_date_time(from, f0);
if (ret < 0) {
return false;
}
/* to <= t0*/
ret = libspdm_x509_compare_date_time(t0, to);
if (ret < 0) {
return false;
}
return true;
}
/**
* This function returns the SPDM public key encryption algorithm OID len.
*
* @param[in] base_asym_algo SPDM base_asym_algo
*
* @return SPDM public key encryption algorithms OID len.
**/
static uint32_t libspdm_get_public_key_algo_OID_len(uint32_t base_asym_algo)
{
switch (base_asym_algo) {
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048:
#if LIBSPDM_RSA_SSA_2048_SUPPORT
return 4;
#else
return 0;
#endif
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_2048:
#if LIBSPDM_RSA_PSS_2048_SUPPORT
return 4;
#else
return 0;
#endif
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_3072:
#if LIBSPDM_RSA_SSA_3072_SUPPORT
return 4;
#else
return 0;
#endif
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_3072:
#if LIBSPDM_RSA_PSS_3072_SUPPORT
return 4;
#else
return 0;
#endif
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_4096:
#if LIBSPDM_RSA_SSA_4096_SUPPORT
return 4;
#else
return 0;
#endif
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_4096:
#if LIBSPDM_RSA_PSS_4096_SUPPORT
return 4;
#else
return 0;
#endif
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P256:
#if LIBSPDM_ECDSA_P256_SUPPORT
return 8;
#else
return 0;
#endif
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P384:
#if LIBSPDM_ECDSA_P384_SUPPORT
return 5;
#else
return 0;
#endif
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P521:
#if LIBSPDM_ECDSA_P521_SUPPORT
return 5;
#else
return 0;
#endif
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED25519:
#if LIBSPDM_EDDSA_ED25519_SUPPORT
return 3;
#else
return 0;
#endif
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED448:
#if LIBSPDM_EDDSA_ED448_SUPPORT
return 3;
#else
return 0;
#endif
default:
LIBSPDM_ASSERT(false);
return 0;
}
}
/**
* This function get the SPDM public key encryption algorithm OID.
*
* @param[in] base_asym_algo SPDM base_asym_algo
* @param[in,out] oid SPDM public key encryption algorithm OID
* @param[in,out] oid_other Other SPDM public key encryption algorithm OID
* because of ASN1 code for integer
*
* @retval true get OID sucessful.
* @retval false get OID fail.
**/
static bool libspdm_get_public_key_algo_OID(uint32_t base_asym_algo, uint8_t *oid,
uint8_t *oid_other)
{
uint32_t oid_len;
oid_len = libspdm_get_public_key_algo_OID_len(base_asym_algo);
if(oid_len == 0) {
return false;
}
switch (base_asym_algo) {
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_2048: {
#if (LIBSPDM_RSA_SSA_2048_SUPPORT) || (LIBSPDM_RSA_PSS_2048_SUPPORT)
uint8_t encry_algo_oid_rsa2048[] = KEY_ENCRY_ALGO_RSA2048_FLAG;
uint8_t encry_algo_oid_rsa2048_ohter[] = KEY_ENCRY_ALGO_RSA2048_FLAG_OTHER;
libspdm_copy_mem(oid, oid_len, encry_algo_oid_rsa2048, oid_len);
libspdm_copy_mem(oid_other, oid_len, encry_algo_oid_rsa2048_ohter, oid_len);
return true;
#else
return false;
#endif
}
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_3072:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_3072: {
#if (LIBSPDM_RSA_SSA_3072_SUPPORT) || (LIBSPDM_RSA_PSS_3072_SUPPORT)
uint8_t encry_algo_oid_rsa3072[] = KEY_ENCRY_ALGO_RSA3072_FLAG;
uint8_t encry_algo_oid_rsa3072_ohter[] = KEY_ENCRY_ALGO_RSA3072_FLAG_OTHER;
libspdm_copy_mem(oid, oid_len, encry_algo_oid_rsa3072, oid_len);
libspdm_copy_mem(oid_other, oid_len, encry_algo_oid_rsa3072_ohter, oid_len);
return true;
#else
return false;
#endif
}
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_4096:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_4096: {
#if (LIBSPDM_RSA_SSA_4096_SUPPORT) || (LIBSPDM_RSA_PSS_4096_SUPPORT)
uint8_t encry_algo_oid_rsa4096[] = KEY_ENCRY_ALGO_RSA4096_FLAG;
uint8_t encry_algo_oid_rsa4096_ohter[] = KEY_ENCRY_ALGO_RSA4096_FLAG_OTHER;
libspdm_copy_mem(oid, oid_len, encry_algo_oid_rsa4096, oid_len);
libspdm_copy_mem(oid_other, oid_len, encry_algo_oid_rsa4096_ohter, oid_len);
return true;
#else
return false;
#endif
}
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P256: {
#if LIBSPDM_ECDSA_P256_SUPPORT
uint8_t encry_algo_oid_ecc256[] = KEY_ENCRY_ALGO_ECC256_OID;
libspdm_copy_mem(oid, oid_len, encry_algo_oid_ecc256, oid_len);
return true;
#else
return false;
#endif
}
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P384: {
#if LIBSPDM_ECDSA_P384_SUPPORT
uint8_t encry_algo_oid_ecc384[] = KEY_ENCRY_ALGO_ECC384_OID;
libspdm_copy_mem(oid, oid_len, encry_algo_oid_ecc384, oid_len);
return true;
#else
return false;
#endif
}
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P521: {
#if LIBSPDM_ECDSA_P521_SUPPORT
uint8_t encry_algo_oid_ecc521[] = KEY_ENCRY_ALGO_ECC521_OID;
libspdm_copy_mem(oid, oid_len, encry_algo_oid_ecc521, oid_len);
return true;
#else
return false;
#endif
}
/*sm2 oid TBD*/
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_SM2_ECC_SM2_P256:
return true;
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED25519: {
#if LIBSPDM_EDDSA_ED25519_SUPPORT
uint8_t encry_algo_oid_ed25519[] = ENCRY_ALGO_ED25519_OID;
libspdm_copy_mem(oid, oid_len, encry_algo_oid_ed25519, oid_len);
return true;
#else
return false;
#endif
break;
}
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED448: {
#if LIBSPDM_EDDSA_ED448_SUPPORT
uint8_t encry_algo_oid_ed448[] = ENCRY_ALGO_ED448_OID;
libspdm_copy_mem(oid, oid_len, encry_algo_oid_ed448, oid_len);
return true;
#else
return false;
#endif
break;
}
default:
LIBSPDM_ASSERT(false);
return false;
}
}
/**
* Verify cert public key encryption algorithm is matched to negotiated base_aysm algo
*
* @param[in] cert Pointer to the DER-encoded certificate data.
* @param[in] cert_size The size of certificate data in bytes.
* @param[in] base_asym_algo SPDM base_asym_algo
* @param[out] oid cert public key encryption algorithm OID
* @param[in] oid_size the buffer size for required OID
*
* @retval true get public key oid from cert successfully
* @retval false get public key oid from cert fail
**/
static bool libspdm_get_public_key_oid(const uint8_t *cert, size_t cert_size,
uint8_t *oid, size_t oid_size, uint32_t base_asym_algo)
{
bool ret;
uint8_t *ptr;
int32_t length;
size_t obj_len;
uint8_t *end;
uint8_t index;
uint8_t sequence_time;
length = (int32_t)cert_size;
ptr = (uint8_t*)(size_t)cert;
obj_len = 0;
end = ptr + length;
ret = true;
/* TBSCertificate have 5 sequence before subjectPublicKeyInfo*/
sequence_time = 5;
/*all cert sequence*/
ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
if (!ret) {
return false;
}
/*TBSCertificate sequence*/
ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
if (!ret) {
return false;
}
end = ptr + obj_len;
/*version*/
ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
LIBSPDM_CRYPTO_ASN1_CONTEXT_SPECIFIC |
LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
if (!ret) {
return false;
}
ptr += obj_len;
/*serialNumber*/
ret = libspdm_asn1_get_tag(&ptr, end, &obj_len, LIBSPDM_CRYPTO_ASN1_INTEGER);
if (!ret) {
return false;
}
/**
* signature AlgorithmIdentifier,
* issuer Name,
* validity Validity,
* subject Name,
* subjectPublicKeyInfo
**/
for (index = 0; index < sequence_time; index++) {
ptr += obj_len;
ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
if (!ret) {
return false;
}
}
switch (base_asym_algo)
{
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_2048:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_3072:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_3072:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_4096:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_4096:
ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
if (!ret) {
return false;
}
ptr += obj_len;
ret = libspdm_asn1_get_tag(&ptr, end, &obj_len, LIBSPDM_CRYPTO_ASN1_BIT_STRING);
if (!ret) {
return false;
}
/*get rsa key len*/
ptr++;
ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
if (!ret) {
return false;
}
libspdm_copy_mem(oid, oid_size, ptr, oid_size);
break;
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P256:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P384:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P521:
ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
if (!ret) {
return false;
}
ret = libspdm_asn1_get_tag(&ptr, end, &obj_len, LIBSPDM_CRYPTO_ASN1_OID);
if (!ret) {
return false;
}
/*get ecc second oid*/
ptr +=obj_len;
ret = libspdm_asn1_get_tag(&ptr, end, &obj_len, LIBSPDM_CRYPTO_ASN1_OID);
if (!ret) {
return false;
}
if (oid_size != obj_len) {
return false;
}
libspdm_copy_mem(oid, oid_size, ptr, obj_len);
break;
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED25519:
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED448:
ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
if (!ret) {
return false;
}
/*get eddsa oid*/
ret = libspdm_asn1_get_tag(&ptr, end, &obj_len, LIBSPDM_CRYPTO_ASN1_OID);
if (!ret) {
return false;
}
if (oid_size != obj_len) {
return false;
}
libspdm_copy_mem(oid, oid_size, ptr, obj_len);
break;
default:
LIBSPDM_ASSERT(false);
return false;
}
return true;
}
/**
* Verify cert public key encryption algorithm is matched to negotiated base_aysm algo
*
* @param[in] cert Pointer to the DER-encoded certificate data.
* @param[in] cert_size The size of certificate data in bytes.
* @param[in] base_asym_algo SPDM base_asym_algo
*
* @retval true verify pass
* @retval false verify fail
**/
static bool libspdm_verify_cert_subject_public_key_info(const uint8_t *cert, size_t cert_size,
uint32_t base_asym_algo)
{
size_t oid_len;
bool status;
/*public key encrypt algo OID from cert*/
uint8_t cert_public_key_crypt_algo_oid[LIBSPDM_MAX_ENCRYPTION_ALGO_OID_LEN];
/*public key encrypt algo OID from libspdm stored*/
uint8_t libspdm_public_key_crypt_algo_oid[LIBSPDM_MAX_ENCRYPTION_ALGO_OID_LEN];
uint8_t libspdm_public_key_crypt_algo_oid_other[LIBSPDM_MAX_ENCRYPTION_ALGO_OID_LEN];
libspdm_zero_mem(libspdm_public_key_crypt_algo_oid, LIBSPDM_MAX_ENCRYPTION_ALGO_OID_LEN);
libspdm_zero_mem(libspdm_public_key_crypt_algo_oid_other, LIBSPDM_MAX_ENCRYPTION_ALGO_OID_LEN);
/*work around: skip the sm2*/
if (base_asym_algo == SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_SM2_ECC_SM2_P256) {
return true;
}
oid_len = libspdm_get_public_key_algo_OID_len(base_asym_algo);
if(oid_len == 0) {
return false;
}
/*get public key encrypt algo OID from libspdm stored*/
status = libspdm_get_public_key_algo_OID(base_asym_algo,
libspdm_public_key_crypt_algo_oid,
libspdm_public_key_crypt_algo_oid_other);
if (!status) {
return status;
}
/*get public key encrypt algo OID from cert*/
status = libspdm_get_public_key_oid(cert, cert_size, cert_public_key_crypt_algo_oid, oid_len,
base_asym_algo);
if (!status || (!libspdm_consttime_is_mem_equal(cert_public_key_crypt_algo_oid,
libspdm_public_key_crypt_algo_oid, oid_len) &&
!libspdm_consttime_is_mem_equal(cert_public_key_crypt_algo_oid,
libspdm_public_key_crypt_algo_oid_other,
oid_len))) {
return false;
}
return status;
}
/**
* Verify leaf cert basic_constraints CA is false
*
* @param[in] cert Pointer to the DER-encoded certificate data.
* @param[in] cert_size The size of certificate data in bytes.
* @param[in] need_basic_constraints This value indicates whether basic_constraints must be present in the Cert
*
* @retval true verify pass,two case: 1.basic constraints is not present in cert, when need_basic_constraints is false;
* 2. cert basic_constraints CA is false;
* @retval false verify fail
**/
static bool libspdm_verify_leaf_cert_basic_constraints(const uint8_t *cert, size_t cert_size,
bool need_basic_constraints)
{
bool status;
/*basic_constraints from cert*/
uint8_t cert_basic_constraints[LIBSPDM_MAX_BASIC_CONSTRAINTS_CA_LEN];
size_t len;
uint8_t basic_constraints_false_case1[] = BASIC_CONSTRAINTS_STRING_FALSE_CASE1;
uint8_t basic_constraints_false_case2[] = BASIC_CONSTRAINTS_STRING_FALSE_CASE2;
len = LIBSPDM_MAX_BASIC_CONSTRAINTS_CA_LEN;
status = libspdm_x509_get_extended_basic_constraints(cert, cert_size,
cert_basic_constraints, &len);
if (!status) {
return false;
} else if (len == 0) {
/* basic constraints is not present in cert */
if (need_basic_constraints) {
return false;
} else {
return true;
}
}
if ((len == sizeof(basic_constraints_false_case1)) &&
(libspdm_consttime_is_mem_equal(cert_basic_constraints,
basic_constraints_false_case1,
sizeof(basic_constraints_false_case1)))) {
return true;
}
if ((len == sizeof(basic_constraints_false_case2)) &&
(libspdm_consttime_is_mem_equal(cert_basic_constraints,
basic_constraints_false_case2,
sizeof(basic_constraints_false_case2)))) {
return true;
}
return false;
}
/**
* Verify leaf cert basic_constraints CA is false
*
* @param[in] cert Pointer to the DER-encoded certificate data.
* @param[in] cert_size The size of certificate data in bytes.
*
* @retval true verify pass,two case: 1.basic constraints is not present in cert;
* 2. cert basic_constraints CA is false;
* @retval false verify fail
**/
static bool libspdm_verify_set_cert_leaf_cert_basic_constraints(
const uint8_t *cert, size_t cert_size, uint8_t cert_model)
{
bool status;
/*basic_constraints from cert*/
uint8_t cert_basic_constraints[LIBSPDM_MAX_BASIC_CONSTRAINTS_CA_LEN];
size_t len;
uint8_t basic_constraints_false_case1[] = BASIC_CONSTRAINTS_STRING_FALSE_CASE1;
uint8_t basic_constraints_false_case2[] = BASIC_CONSTRAINTS_STRING_FALSE_CASE2;
uint8_t basic_constraints_true_case[] = BASIC_CONSTRAINTS_STRING_TRUE_CASE;
len = LIBSPDM_MAX_BASIC_CONSTRAINTS_CA_LEN;
status = libspdm_x509_get_extended_basic_constraints(cert, cert_size,
cert_basic_constraints, &len);
if (cert_model == SPDM_CERTIFICATE_INFO_CERT_MODEL_DEVICE_CERT) {
/*device cert model*/
if (!status) {
return false;
} else if (len == 0) {
/* basic constraints is not present in cert */
return true;
}
if ((len == sizeof(basic_constraints_false_case1)) &&
(libspdm_consttime_is_mem_equal(cert_basic_constraints,
basic_constraints_false_case1,
sizeof(basic_constraints_false_case1)))) {
return true;
}
if ((len == sizeof(basic_constraints_false_case2)) &&
(libspdm_consttime_is_mem_equal(cert_basic_constraints,
basic_constraints_false_case2,
sizeof(basic_constraints_false_case2)))) {
return true;
}
} else {
/*alias cert model*/
if (status && (len == sizeof(basic_constraints_true_case)) &&
(libspdm_consttime_is_mem_equal(cert_basic_constraints,
basic_constraints_true_case,
sizeof(basic_constraints_true_case)))) {
return true;
}
}
return false;
}
/**
* Verify leaf cert spdm defined extended key usage
*
* @param[in] cert Pointer to the DER-encoded certificate data.
* @param[in] cert_size The size of certificate data in bytes.
* @param[in] is_requester_cert Is the function verifying requester or responder cert.
*
* @retval true verify pass, two cases:
* 1. spdm defined eku is not present in cert;
* 2. spdm defined eku is compliant with requester/responder identity;
* @retval false verify fail, two cases:
* 1. requester's cert has only responder auth oid in eku;
* 2. responder's cert has only requester auth oid in eku;
**/
static bool libspdm_verify_leaf_cert_spdm_eku(const uint8_t *cert, size_t cert_size,
bool is_requester_cert)
{
bool status;
uint8_t eku[256];
size_t eku_size;
bool req_auth_oid_find_success;
bool rsp_auth_oid_find_success;
uint8_t *ptr;
size_t obj_len;
/* SPDM defined OID */
uint8_t eku_requester_auth_oid[] = SPDM_OID_DMTF_EKU_REQUESTER_AUTH;
uint8_t eku_responder_auth_oid[] = SPDM_OID_DMTF_EKU_RESPONDER_AUTH;
eku_size = sizeof(eku);
status = libspdm_x509_get_extended_key_usage(cert, cert_size, eku, &eku_size);
if (!status) {
return false;
} else if (eku_size == 0) {
/* eku is not present in cert */
return true;
}
ptr = eku;
obj_len = 0;
req_auth_oid_find_success = false;
rsp_auth_oid_find_success = false;
status = libspdm_asn1_get_tag(&ptr, eku + eku_size, &obj_len,
LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
if (!status) {
return false;
}
while(ptr < eku + eku_size) {
status = libspdm_asn1_get_tag(&ptr, eku + eku_size, &obj_len, LIBSPDM_CRYPTO_ASN1_OID);
if (!status) {
return false;
}
if ((obj_len == sizeof(eku_requester_auth_oid)) &&
(libspdm_consttime_is_mem_equal(ptr, eku_requester_auth_oid,
sizeof(eku_requester_auth_oid)))) {
req_auth_oid_find_success = true;
}
if ((obj_len == sizeof(eku_responder_auth_oid)) &&
(libspdm_consttime_is_mem_equal(ptr, eku_responder_auth_oid,
sizeof(eku_responder_auth_oid)))) {
rsp_auth_oid_find_success = true;
}
ptr += obj_len;
}
if (ptr != eku + eku_size) {
return false;
}
if (is_requester_cert) {
/* it should not only contain responder auth oid */
if (!req_auth_oid_find_success && rsp_auth_oid_find_success) {
return false;
}
} else {
/* it should not only contain requester auth oid */
if (req_auth_oid_find_success && !rsp_auth_oid_find_success) {
return false;
}
}
return true;
}
/**
* Verify leaf cert spdm defined extension
*
* @param[in] cert Pointer to the DER-encoded certificate data.
* @param[in] cert_size The size of certificate data in bytes.
* @param[in] is_requester_cert Is the function verifying requester or responder cert.
*
* @retval true verify pass
* @retval false verify fail,two case: 1. return is not RETURN_SUCCESS or RETURN_NOT_FOUND;
* 2. hardware_identity_oid is found in AliasCert model;
**/
static bool libspdm_verify_leaf_cert_spdm_extension(const uint8_t *cert, size_t cert_size,
bool is_requester_cert,
uint8_t cert_model)
{
bool status;
bool find_sucessful;
uint8_t spdm_extension[LIBSPDM_MAX_EXTENSION_LEN];
size_t len;
uint8_t *ptr;
uint8_t *temptr;
size_t obj_len;
/* SPDM defined OID */
uint8_t oid_spdm_extension[] = SPDM_OID_DMTF_SPDM_EXTENSION;
uint8_t hardware_identity_oid[] = SPDM_OID_DMTF_HARDWARE_IDENTITY;
len = LIBSPDM_MAX_EXTENSION_LEN;
if (cert == NULL || cert_size == 0) {
return false;
}
status = libspdm_x509_get_extension_data(cert, cert_size,
(const uint8_t *)oid_spdm_extension,
sizeof(oid_spdm_extension),
spdm_extension,
&len);
if (!status) {
return false;
} else if(len == 0) {
return true;
}
/*find the spdm hardware identity OID*/
find_sucessful = false;
ptr = spdm_extension;
obj_len = 0;
/*id-spdm-cert-oids ::= SEQUENCE SIZE (1..MAX) OF id-spdm-cert-oid*/
status = libspdm_asn1_get_tag(
&ptr, spdm_extension + len, &obj_len,
LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
if (!status) {
return false;
}
while(ptr < spdm_extension + len) {
status = libspdm_asn1_get_tag(
&ptr, spdm_extension + len, &obj_len,
LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);