-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathhandler.c
More file actions
2709 lines (2373 loc) · 93.4 KB
/
handler.c
File metadata and controls
2709 lines (2373 loc) · 93.4 KB
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 (c) Citrix Systems, Inc
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Portions derived from edk2 (https://github.com/tianocore/edk2)
* with the following license:
*
* Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.
* Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.
* Copyright (c) 2011 - 2015, ARM Limited. All rights reserved.
* Copyright (c) 2014 - 2015, Linaro Limited. All rights reserved.
* Copyright (c) 2013 - 2015, Red Hat, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <openssl/objects.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/pkcs7.h>
#include <openssl/err.h>
#include <backend.h>
#include <debug.h>
#include <efi.h>
#include <guid.h>
#include <serialize.h>
#include <handler.h>
#include <xapidb.h>
#include <mor.h>
#include <ppi.h>
struct auth_info {
const char *pretty_name;
const uint8_t *name;
UINTN name_len;
const EFI_GUID *guid;
const char *path;
bool append;
bool required;
uint8_t *data;
off_t data_len;
};
/* Some values from edk2. */
static const uint8_t mOidValue[9] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02};
static const uint8_t mSignatureSupport[] = {
0x12,0xa5,0x6c,0x82,0x10,0xcf,0xc9,0x4a,0xb1,0x87,0xbe,0x01,0x49,0x66,0x31,0xbd, /* EFI_CERT_SHA1_GUID */
0x26,0x16,0xc4,0xc1,0x4c,0x50,0x92,0x40,0xac,0xa9,0x41,0xf9,0x36,0x93,0x43,0x28, /* EFI_CERT_SHA256_GUID */
0xe8,0x66,0x57,0x3c,0x9c,0x26,0x34,0x4e,0xaa,0x14,0xed,0x77,0x6e,0x85,0xb3,0xb6, /* EFI_CERT_RSA2048_GUID */
0xa1,0x59,0xc0,0xa5,0xe4,0x94,0xa7,0x4a,0x87,0xb5,0xab,0x15,0x5c,0x2b,0xf0,0x72, /* EFI_CERT_X509_GUID */
};
static const uint8_t mSha256OidValue[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01};
static const EFI_SIGNATURE_ITEM mSupportSigItem[] = {
{{{0x26, 0x16, 0xc4, 0xc1, 0x4c, 0x50, 0x92, 0x40, 0xac, 0xa9, 0x41, 0xf9, 0x36, 0x93, 0x43, 0x28}}, 0, 32 }, /* EFI_CERT_SHA256_GUID */
{{{0xe8, 0x66, 0x57, 0x3c, 0x9c, 0x26, 0x34, 0x4e, 0xaa, 0x14, 0xed, 0x77, 0x6e, 0x85, 0xb3, 0xb6}}, 0, 256 }, /* EFI_CERT_RSA2048_GUID */
{{{0x90, 0x61, 0xb3, 0xe2, 0x9b, 0x87, 0x3d, 0x4a, 0xad, 0x8d, 0xf2, 0xe7, 0xbb, 0xa3, 0x27, 0x84}}, 0, 256 }, /* EFI_CERT_RSA2048_SHA256_GUID */
{{{0x12, 0xa5, 0x6c, 0x82, 0x10, 0xcf, 0xc9, 0x4a, 0xb1, 0x87, 0xbe, 0x01, 0x49, 0x66, 0x31, 0xbd}}, 0, 20 }, /* EFI_CERT_SHA1_GUID */
{{{0x4f, 0x44, 0xf8, 0x67, 0x43, 0x87, 0xf1, 0x48, 0xa3, 0x28, 0x1e, 0xaa, 0xb8, 0x73, 0x60, 0x80}}, 0, 256 }, /* EFI_CERT_RSA2048_SHA1_GUID */
{{{0xa1, 0x59, 0xc0, 0xa5, 0xe4, 0x94, 0xa7, 0x4a, 0x87, 0xb5, 0xab, 0x15, 0x5c, 0x2b, 0xf0, 0x72}}, 0, ((UINT32) ~0)}, /* EFI_CERT_X509_GUID */
{{{0x33, 0x52, 0x6e, 0x0b, 0x5c, 0xa6, 0xc9, 0x44, 0x94, 0x07, 0xd9, 0xab, 0x83, 0xbf, 0xc8, 0xbd}}, 0, 28 }, /* EFI_CERT_SHA224_GUID */
{{{0x07, 0x53, 0x3e, 0xff, 0xd0, 0x9f, 0xc9, 0x48, 0x85, 0xf1, 0x8a, 0xd5, 0x6c, 0x70, 0x1e, 0x01}}, 0, 48 }, /* EFI_CERT_SHA384_GUID */
{{{0xae, 0x0f, 0x3e, 0x09, 0xc4, 0xa6, 0x50, 0x4f, 0x9f, 0x1b, 0xd4, 0x1e, 0x2b, 0x89, 0xc1, 0x9a}}, 0, 64 }, /* EFI_CERT_SHA512_GUID */
{{{0x92, 0xa4, 0xd2, 0x3b, 0xc0, 0x96, 0x79, 0x40, 0xb4, 0x20, 0xfc, 0xf9, 0x8e, 0xf1, 0x03, 0xed}}, 0, 48 }, /* EFI_CERT_X509_SHA256_GUID */
{{{0x6e, 0x87, 0x76, 0x70, 0xc2, 0x80, 0xe6, 0x4e, 0xaa, 0xd2, 0x28, 0xb3, 0x49, 0xa6, 0x86, 0x5b}}, 0, 64 }, /* EFI_CERT_X509_SHA384_GUID */
{{{0x63, 0xbf, 0x6d, 0x44, 0x02, 0x25, 0xda, 0x4c, 0xbc, 0xfa, 0x24, 0x65, 0xd2, 0xb0, 0xfe, 0x9d}}, 0, 80 } /* EFI_CERT_X509_SHA512_GUID */
};
#define NUM_OF_SUPPORTED_SIG_ITEMS (sizeof(mSupportSigItem) / sizeof(EFI_SIGNATURE_ITEM))
static const uint8_t EFI_SETUP_MODE_NAME[] = {'S',0,'e',0,'t',0,'u',0,'p',0,'M',0,'o',0,'d',0,'e',0};
static const uint8_t EFI_AUDIT_MODE_NAME[] = {'A',0,'u',0,'d',0,'i',0,'t',0,'M',0,'o',0,'d',0,'e',0};
static const uint8_t EFI_DEPLOYED_MODE_NAME[] = {'D',0,'e',0,'p',0,'l',0,'o',0,'y',0,'e',0,'d',0,'M',0,'o',0,'d',0,'e',0};
static const uint8_t EFI_PLATFORM_KEY_NAME[] = {'P',0,'K',0};
const uint8_t EFI_KEY_EXCHANGE_KEY_NAME[] = {'K',0,'E',0,'K',0};
static const uint8_t EFI_SECURE_BOOT_MODE_NAME[] = {'S',0,'e',0,'c',0,'u',0,'r',0,'e',0,'B',0,'o',0,'o',0,'t',0};
static const uint8_t EFI_SIGNATURE_SUPPORT_NAME[] = {'S',0,'i',0,'g',0,'n',0,'a',0,'t',0,'u',0,'r',0,'e',0,'S',0,'u',0,'p',0,'p',0,'o',0,'r',0,'t',0};
const uint8_t TCG2_PHYSICAL_PRESENCEFLAGSLOCK_NAME[] = {
'T', 0, 'c', 0, 'g', 0, '2', 0, 'P', 0, 'h', 0, 'y', 0, 's', 0, 'i', 0, 'c', 0, 'a', 0, 'l', 0, 'P', 0, 'r', 0, 'e', 0,
's', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 'F', 0, 'l', 0, 'a', 0, 'g', 0, 's', 0, 'L', 0, 'o', 0, 'c', 0,'k', 0
};
const size_t TCG2_PHYSICAL_PRESENCEFLAGSLOCK_NAME_SIZE = sizeof(TCG2_PHYSICAL_PRESENCEFLAGSLOCK_NAME);
static const uint8_t TCG2_PHYSICAL_PRESENCEFLAGS_NAME[] = {
'T', 0, 'c', 0, 'g', 0, '2', 0, 'P', 0, 'h', 0, 'y', 0, 's', 0, 'i', 0, 'c', 0, 'a', 0, 'l', 0, 'P', 0, 'r', 0, 'e', 0,
's', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 'F', 0, 'l', 0, 'a', 0, 'g', 0, 's', 0
};
static const uint8_t EFI_IMAGE_SECURITY_DATABASE[] = {'d',0,'b',0};
static const uint8_t EFI_IMAGE_SECURITY_DATABASE1[] = {'d',0,'b',0,'x',0};
static const uint8_t EFI_IMAGE_SECURITY_DATABASE2[] = {'d',0,'b',0,'t',0};
#define AUTH_PATH_PREFIX "/var/lib/varstored"
/*
* The macro AUTH_ONLY_PK_REQUIRED makes KEK and DB files optional, allowing
* varstored and varstore-sb-state to copy only the PK file (which is always
* present) and switch the VM to user mode. This will prevent the VM to boot
* if it has SecureBoot enabled by the user but UEFI certificates are missing.
*/
#ifdef AUTH_ONLY_PK_REQUIRED
#define AUTH_DB_REQUIRED false
#define AUTH_KEK_REQUIRED false
#else
#define AUTH_DB_REQUIRED true
#define AUTH_KEK_REQUIRED true
#endif
/*
* Array of auth_info structs containing the information about the keys
* we need. Avoid switching to user mode before importing other keys by
* importing PK key last, otherwise this would require signing other keys
* in Dom0.
*/
static struct auth_info auth_info[] = {
{"dbx", EFI_IMAGE_SECURITY_DATABASE1, sizeof(EFI_IMAGE_SECURITY_DATABASE1),
&gEfiImageSecurityDatabaseGuid, AUTH_PATH_PREFIX "/dbx.auth", true, false},
{"db", EFI_IMAGE_SECURITY_DATABASE, sizeof(EFI_IMAGE_SECURITY_DATABASE),
&gEfiImageSecurityDatabaseGuid, AUTH_PATH_PREFIX "/db.auth", false, AUTH_DB_REQUIRED},
{"KEK", EFI_KEY_EXCHANGE_KEY_NAME, sizeof(EFI_KEY_EXCHANGE_KEY_NAME),
&gEfiGlobalVariableGuid, AUTH_PATH_PREFIX "/KEK.auth", false, AUTH_KEK_REQUIRED},
{"PK", EFI_PLATFORM_KEY_NAME, sizeof(EFI_PLATFORM_KEY_NAME),
&gEfiGlobalVariableGuid, AUTH_PATH_PREFIX "/PK.auth", false, true},
};
struct efi_variable *var_list;
bool secure_boot_enable;
bool auth_enforce = true;
bool persistent = true;
static uint64_t
get_space_usage(void)
{
struct efi_variable *l;
uint64_t total = 0;
l = var_list;
while (l) {
total += l->name_len + l->data_len + VARIABLE_SIZE_OVERHEAD;
l = l->next;
}
return total;
}
/*
* Parse an ASN1_TIME into year, month, day components.
* Returns true on success.
*/
static bool
parse_asn1_time(const ASN1_TIME *atime, int *year, int *month, int *day)
{
struct tm tm_val = {0};
if (!atime)
return false;
if (ASN1_TIME_to_tm(atime, &tm_val) != 1)
return false;
*year = tm_val.tm_year + 1900;
*month = tm_val.tm_mon + 1;
*day = tm_val.tm_mday;
return true;
}
/*
* Parse X.509 certificates from EFI signature list data.
* For each X.509 signature list entry, iterate through the individual
* signature data entries, parse each DER-encoded certificate using OpenSSL,
* and extract the subject, issue date and expiry date.
*
* If certs is non-NULL and max_certs > 0, fills in cert_info structs.
* Returns the total count of X.509 certificates, or -1 on error.
*/
int
parse_certs_from_siglist(const uint8_t *data, UINTN data_len,
struct cert_info *certs, int max_certs,
int verbose)
{
int count = 0;
UINTN offset = 0;
while (offset + sizeof(EFI_SIGNATURE_LIST) <= data_len) {
const EFI_SIGNATURE_LIST *siglist =
(const EFI_SIGNATURE_LIST *)(data + offset);
UINT32 sig_list_size = siglist->SignatureListSize;
UINT32 sig_header_size = siglist->SignatureHeaderSize;
UINT32 sig_size = siglist->SignatureSize;
UINTN sig_data_offset;
if (sig_list_size == 0 || sig_list_size > data_len - offset)
return -1;
if (memcmp(&siglist->SignatureType, &gEfiCertX509Guid, GUID_LEN)) {
offset += sig_list_size;
continue;
}
/* Start of signature data entries */
sig_data_offset = offset + sizeof(EFI_SIGNATURE_LIST) + sig_header_size;
if (sig_size < EFI_SIG_DATA_SIZE)
return -1;
/* Iterate each EFI_SIGNATURE_DATA in this list */
while (sig_data_offset + sig_size <= offset + sig_list_size) {
const uint8_t *cert_der;
UINTN cert_der_len;
X509 *x509;
/* Skip SignatureOwner GUID to get to the DER certificate */
cert_der = data + sig_data_offset + EFI_SIG_DATA_SIZE;
cert_der_len = sig_size - EFI_SIG_DATA_SIZE;
x509 = d2i_X509(NULL, &cert_der, (long)cert_der_len);
if (x509) {
const ASN1_TIME *not_before = X509_get0_notBefore(x509);
const ASN1_TIME *not_after = X509_get0_notAfter(x509);
char subject_buf[MAX_CERT_SUBJECT];
X509_NAME_oneline(X509_get_subject_name(x509),
subject_buf, sizeof(subject_buf));
if (certs && count < max_certs) {
struct cert_info *ci = &certs[count];
snprintf(ci->subject, sizeof(ci->subject), "%s",
subject_buf);
if (!parse_asn1_time(not_before,
&ci->not_before_year,
&ci->not_before_month,
&ci->not_before_day)) {
ci->not_before_year = 0;
ci->not_before_month = 0;
ci->not_before_day = 0;
}
if (!parse_asn1_time(not_after,
&ci->not_after_year,
&ci->not_after_month,
&ci->not_after_day)) {
ci->not_after_year = 0;
ci->not_after_month = 0;
ci->not_after_day = 0;
}
}
if (verbose)
INFO(" cert[%d]: subject=%s, "
"notBefore=%04d-%02d-%02d, "
"notAfter=%04d-%02d-%02d\n",
count, subject_buf,
certs && count < max_certs ?
certs[count].not_before_year : 0,
certs && count < max_certs ?
certs[count].not_before_month : 0,
certs && count < max_certs ?
certs[count].not_before_day : 0,
certs && count < max_certs ?
certs[count].not_after_year : 0,
certs && count < max_certs ?
certs[count].not_after_month : 0,
certs && count < max_certs ?
certs[count].not_after_day : 0);
X509_free(x509);
count++;
} else {
if (verbose)
INFO("Failed to parse X.509 certificate at offset %lu\n",
sig_data_offset);
}
sig_data_offset += sig_size;
}
offset += sig_list_size;
}
return count;
}
/*
* Check whether the given signature list data contains a latest-era certificate,
* i.e. one issued in 2023 and expiring in 2038.
* Returns true if such a certificate is found.
*/
bool
siglist_has_latest_cert(const uint8_t *data, UINTN data_len, int verbose)
{
struct cert_info certs[MAX_CERTS_IN_SIGLIST];
int count, i;
count = parse_certs_from_siglist(data, data_len,
certs, MAX_CERTS_IN_SIGLIST,
verbose);
if (count <= 0)
return false;
for (i = 0; i < count && i < MAX_CERTS_IN_SIGLIST; i++) {
if (NOT_EXPIRE(certs[i].not_before_year, certs[i].not_after_year))
return true;
}
return false;
}
/*
* Check whether both KEK and db in the loaded variable store contain at least
* one up-to-date certificate (i.e. one that satisfies NOT_EXPIRE). Both
* variables must be present and up-to-date for true to be returned.
*/
bool
check_kek_and_db_uptodate(void)
{
uint8_t *kek_data = NULL, *db_data = NULL;
UINTN kek_len = 0, db_len = 0;
bool result = false;
if (internal_get_variable(EFI_KEY_EXCHANGE_KEY_NAME,
sizeof(EFI_KEY_EXCHANGE_KEY_NAME),
&gEfiGlobalVariableGuid,
&kek_data, &kek_len) != EFI_SUCCESS)
goto out;
if (internal_get_variable(EFI_IMAGE_SECURITY_DATABASE,
sizeof(EFI_IMAGE_SECURITY_DATABASE),
&gEfiImageSecurityDatabaseGuid,
&db_data, &db_len) != EFI_SUCCESS)
goto out;
result = siglist_has_latest_cert(kek_data, kek_len, 0) &&
siglist_has_latest_cert(db_data, db_len, 0);
out:
free(kek_data);
free(db_data);
return result;
}
/* A limited version of SetVariable for internal use.
* Passing data_len == 0 requests deletion of the variable.
*/
EFI_STATUS
internal_set_variable(const uint8_t *name, UINTN name_len, const EFI_GUID *guid,
const uint8_t *data, UINTN data_len, UINT32 attr)
{
struct efi_variable *l, *prev = NULL;
uint8_t *new_data = NULL;
l = var_list;
while (l) {
if (l->name_len == name_len &&
!memcmp(l->name, name, name_len) &&
!memcmp(&l->guid, guid, GUID_LEN)) {
if (data_len == 0) {
if (prev)
prev->next = l->next;
else
var_list = l->next;
free(l->name);
free(l->data);
free(l);
return EFI_SUCCESS;
}
new_data = malloc(data_len);
if (!new_data)
return EFI_DEVICE_ERROR;
memcpy(new_data, data, data_len);
free(l->data);
l->data = new_data;
l->data_len = data_len;
l->attributes = attr;
return EFI_SUCCESS;
}
prev = l;
l = l->next;
}
if (data_len == 0)
return EFI_NOT_FOUND;
new_data = malloc(data_len);
if (!new_data)
return EFI_DEVICE_ERROR;
memcpy(new_data, data, data_len);
l = calloc(1, sizeof *l);
if (!l) {
free(new_data);
return EFI_DEVICE_ERROR;
}
l->name = malloc(name_len);
if (!l->name) {
free(l);
free(new_data);
return EFI_DEVICE_ERROR;
}
memcpy(l->name, name, name_len);
l->name_len = name_len;
memcpy(&l->guid, guid, GUID_LEN);
l->data = new_data;
l->data_len = data_len;
l->attributes = attr;
l->next = var_list;
var_list = l;
return EFI_SUCCESS;
}
static struct efi_variable *
find_variable(const uint8_t *name, UINTN name_len, const EFI_GUID *guid)
{
struct efi_variable *l;
l = var_list;
while (l) {
if (l->name_len == name_len &&
!memcmp(l->name, name, name_len) &&
!memcmp(&l->guid, guid, GUID_LEN))
return l;
l = l->next;
}
return NULL;
}
/* A limited version of GetVariable for internal use. */
EFI_STATUS
internal_get_variable(const uint8_t *name, UINTN name_len, const EFI_GUID *guid,
uint8_t **data, UINTN *data_len)
{
struct efi_variable *l;
l = var_list;
while (l) {
if (l->name_len == name_len &&
!memcmp(l->name, name, name_len) &&
!memcmp(&l->guid, guid, GUID_LEN)) {
*data = malloc(l->data_len);
if (!*data)
return EFI_DEVICE_ERROR;
memcpy(*data, l->data, l->data_len);
*data_len = l->data_len;
return EFI_SUCCESS;
}
l = l->next;
}
return EFI_NOT_FOUND;
}
static void
do_get_variable(uint8_t *comm_buf)
{
uint8_t *ptr, *name;
EFI_GUID guid;
UINTN name_len, data_len;
BOOLEAN at_runtime;
struct efi_variable *l;
ptr = comm_buf;
unserialize_uint32(&ptr); /* version */
unserialize_command(&ptr);
name = unserialize_data(&ptr, &name_len, NAME_LIMIT);
if (!name) {
serialize_result(&comm_buf, name_len == 0 ? EFI_NOT_FOUND : EFI_DEVICE_ERROR);
return;
}
unserialize_guid(&ptr, &guid);
data_len = unserialize_uintn(&ptr);
at_runtime = unserialize_boolean(&ptr);
ptr = comm_buf;
l = var_list;
while (l) {
if (l->name_len == name_len &&
!memcmp(l->name, name, name_len) &&
!memcmp(&l->guid, &guid, GUID_LEN)) {
if (at_runtime && !(l->attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {
l = l->next;
continue;
}
if (data_len < l->data_len) {
serialize_result(&ptr, EFI_BUFFER_TOO_SMALL);
serialize_uintn(&ptr, l->data_len);
} else {
serialize_result(&ptr, EFI_SUCCESS);
serialize_uint32(&ptr, l->attributes);
serialize_data(&ptr, l->data, l->data_len);
}
goto out;
}
l = l->next;
}
serialize_result(&ptr, EFI_NOT_FOUND);
out:
free(name);
}
static X509 *
X509_from_buf(const uint8_t *buf, long len)
{
const uint8_t *ptr = buf;
return d2i_X509(NULL, &ptr, len);
}
static uint8_t *
X509_to_buf(X509 *cert, int *len)
{
uint8_t *ptr, *buf;
*len = i2d_X509(cert, NULL);
buf = malloc(*len);
if (!buf)
return NULL;
ptr = buf;
i2d_X509(cert, &ptr);
return buf;
}
/*
* Get the TBS certificate from an X509 certificate.
* Adapted from edk2.
*
* tbs_cert should be freed by the caller.
*/
static EFI_STATUS
X509_get_tbs_cert(X509 *cert, uint8_t **tbs_cert, UINTN *tbs_len)
{
int asn1_tag, obj_class, len, ret;
long tmp_len;
uint8_t *buf, *ptr, *tbs_ptr;
buf = X509_to_buf(cert, &len);
if (!buf)
return EFI_DEVICE_ERROR;
ptr = buf;
tmp_len = 0;
ret = ASN1_get_object((const unsigned char **)&ptr, &tmp_len, &asn1_tag,
&obj_class, len);
if (ret == 0x80 || asn1_tag != V_ASN1_SEQUENCE) {
free(buf);
return EFI_SECURITY_VIOLATION;
}
tbs_ptr = ptr;
ret = ASN1_get_object((const unsigned char **)&ptr, &tmp_len, &asn1_tag,
&obj_class, tmp_len);
if (ret == 0x80 || asn1_tag != V_ASN1_SEQUENCE) {
free(buf);
return EFI_SECURITY_VIOLATION;
}
*tbs_len = tmp_len + (ptr - tbs_ptr);
*tbs_cert = malloc(*tbs_len);
if (!*tbs_cert) {
free(buf);
return EFI_DEVICE_ERROR;
}
memcpy(*tbs_cert, tbs_ptr, *tbs_len);
free(buf);
return EFI_SUCCESS;
}
/*
* Calculate SHA256 digest of:
* SignerCert CommonName + ToplevelCert tbsCertificate
* Adapted from edk2.
*/
static EFI_STATUS
sha256_sig(STACK_OF(X509) *certs, X509 *top_level_cert, uint8_t *digest)
{
EVP_MD_CTX *ctx;
char name[128];
X509_NAME *x509_name;
uint8_t *tbs_cert;
UINTN tbs_cert_len;
EFI_STATUS status;
int name_len;
x509_name = X509_get_subject_name(sk_X509_value(certs, 0));
if (!x509_name)
return EFI_SECURITY_VIOLATION;
name_len = X509_NAME_get_text_by_NID(x509_name, NID_commonName,
name, sizeof(name));
if (name_len < 0)
return EFI_SECURITY_VIOLATION;
name_len++; /* Include trailing NUL character */
status = X509_get_tbs_cert(top_level_cert, &tbs_cert, &tbs_cert_len);
if (status != EFI_SUCCESS)
return status;
status = EFI_DEVICE_ERROR;
ctx = EVP_MD_CTX_new();
if (!ctx)
goto out;
if (!EVP_DigestInit_ex(ctx, EVP_sha256(), NULL))
goto out_ctx;
if (!EVP_DigestUpdate(ctx, name, strlen(name)))
goto out_ctx;
if (!EVP_DigestUpdate(ctx, tbs_cert, tbs_cert_len))
goto out_ctx;
if (!EVP_DigestFinal_ex(ctx, digest, NULL))
goto out_ctx;
status = EFI_SUCCESS;
out_ctx:
EVP_MD_CTX_free(ctx);
out:
free(tbs_cert);
return status;
}
#ifndef X509_V_FLAG_NO_CHECK_TIME
#define OPENSSL_NO_CHECK_TIME 0
/*
* Verification callback function to override the existing callbacks in
* OpenSSL. This is required due to the lack of X509_V_FLAG_NO_CHECK_TIME in
* OpenSSL 1.0.2. This function has been taken directly from an older version
* of edk2 and been to use X509_V_ERR_CERT_HAS_EXPIRED and
* X509_V_ERR_CERT_NOT_YET_VALID since verification of the timestamps in
* certificates is not typically done in firmware due to untrustworthy system
* time. This part was taken from a patch sent to the edk2 mailing list by
* David Woodhouse entitled "CryptoPkg: Remove OpenSSL hack and manually ignore
* validity time range".
*/
static int
X509_verify_cb(int status, X509_STORE_CTX *context)
{
X509_OBJECT *obj = NULL;
int error;
int index;
int count;
error = X509_STORE_CTX_get_error(context);
if ((error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT) ||
(error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY)) {
obj = malloc(sizeof(*obj));
if (!obj)
return 0;
obj->type = X509_LU_X509;
obj->data.x509 = context->current_cert;
CRYPTO_w_lock (CRYPTO_LOCK_X509_STORE);
if (X509_OBJECT_retrieve_match(context->ctx->objs, obj)) {
status = 1;
} else {
if (error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY) {
count = sk_X509_num(context->chain);
for (index = 0; index < count; index++) {
obj->data.x509 = sk_X509_value(context->chain, index);
if (X509_OBJECT_retrieve_match(context->ctx->objs, obj)) {
status = 1;
break;
}
}
}
}
CRYPTO_w_unlock (CRYPTO_LOCK_X509_STORE);
}
if ((error == X509_V_ERR_CERT_UNTRUSTED) ||
(error == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE) ||
(error == X509_V_ERR_CERT_HAS_EXPIRED) ||
(error == X509_V_ERR_CERT_NOT_YET_VALID))
status = 1;
free(obj);
return status;
}
#else
#define OPENSSL_NO_CHECK_TIME X509_V_FLAG_NO_CHECK_TIME
#endif
/*
* Check whether input p7data is a wrapped ContentInfo structure or not. Wrap
* it if needed. While the specification seems to indicate that it should not
* be wrapped (i.e. it should just be signed data), edk2 accepts either and at
* least one existing tool signs SetVariable updates with a wrapped structure.
* Adapted from edk2. This function contains several magic numbers since it is
* parsing DER-encoded PKCS #7 ASN.1 object by hand. The caller must free
* wrap_data on success.
*/
static EFI_STATUS
wrap_pkcs7_data(const uint8_t *p7data, UINTN p7_len,
uint8_t **wrap_data, UINTN *wrap_len)
{
uint8_t *sig_data;
/*
* We need to look at the first 17 bytes to determine whether the input
* data is wrapped or not. The length needs to be at least this long in
* either case so check and bail early if needed.
*/
if (p7_len < 17) {
return EFI_SECURITY_VIOLATION;
}
if ((p7data[4] == 0x06) && (p7data[5] == 0x09) &&
!memcmp(p7data + 6, mOidValue, sizeof(mOidValue)) &&
(p7data[15] == 0xa0) && (p7data[16] == 0x82)) {
*wrap_data = malloc(p7_len);
if (!*wrap_data)
return EFI_DEVICE_ERROR;
memcpy(*wrap_data, p7data, p7_len);
*wrap_len = p7_len;
return EFI_SUCCESS;
}
*wrap_len = p7_len + 19;
*wrap_data = malloc(*wrap_len);
if (!*wrap_data)
return EFI_DEVICE_ERROR;
sig_data = *wrap_data;
sig_data[0] = 0x30;
sig_data[1] = 0x82;
sig_data[2] = ((uint16_t)(*wrap_len - 4)) >> 8;
sig_data[3] = ((uint16_t)(*wrap_len - 4)) & 0xff;
sig_data[4] = 0x06;
sig_data[5] = 0x09;
memcpy(sig_data + 6, mOidValue, sizeof(mOidValue));
sig_data[15] = 0xA0;
sig_data[16] = 0x82;
sig_data[17] = ((uint16_t)p7_len) >> 8;
sig_data[18] = ((uint16_t)p7_len) & 0xff;
memcpy(sig_data + 19, p7data, p7_len);
return EFI_SUCCESS;
}
/*
* Verify the validity of PKCS#7 data.
* Adapted from edk2.
*/
static EFI_STATUS
pkcs7_verify(const uint8_t *p7data, UINTN p7_len, X509 *trusted_cert,
uint8_t *verify_buf, UINTN verify_len)
{
EFI_STATUS status;
const uint8_t *ptr;
PKCS7 *pkcs7 = NULL;
BIO *data_bio = NULL;
X509_STORE *cert_store = NULL;
ptr = p7data;
pkcs7 = d2i_PKCS7(NULL, &ptr, (int)p7_len);
if (!pkcs7) {
status = EFI_SECURITY_VIOLATION;
goto out;
}
if (!PKCS7_type_is_signed(pkcs7)) {
status = EFI_SECURITY_VIOLATION;
goto out;
}
cert_store = X509_STORE_new();
if (!cert_store) {
status = EFI_DEVICE_ERROR;
goto out;
}
#ifndef X509_V_FLAG_NO_CHECK_TIME
cert_store->verify_cb = X509_verify_cb;
#endif
if (!(X509_STORE_add_cert(cert_store, trusted_cert))) {
status = EFI_SECURITY_VIOLATION;
goto out;
}
data_bio = BIO_new(BIO_s_mem());
if (!data_bio) {
status = EFI_DEVICE_ERROR;
goto out;
}
if (BIO_write(data_bio, verify_buf, (int)verify_len) != verify_len) {
status = EFI_SECURITY_VIOLATION;
goto out;
}
X509_STORE_set_flags(cert_store,
X509_V_FLAG_PARTIAL_CHAIN | OPENSSL_NO_CHECK_TIME);
X509_STORE_set_purpose(cert_store, X509_PURPOSE_ANY);
if (PKCS7_verify(pkcs7, NULL, cert_store, data_bio, NULL, PKCS7_BINARY))
status = EFI_SUCCESS;
else {
if (log_level >= LOG_LVL_DEBUG) {
ERR_load_crypto_strings();
DBG("verify_error : %s\n", ERR_error_string(ERR_get_error(), NULL));
ERR_free_strings();
}
status = EFI_SECURITY_VIOLATION;
}
out:
BIO_free(data_bio);
X509_STORE_free(cert_store);
PKCS7_free(pkcs7);
return status;
}
/*
* Get the signer's certificates from PKCS#7 signed data.
* Adapted from edk2.
*
* The caller is responsible for free the pkcs7 context and the stack of certs
* (but not the certs themselves). The certs should not be used after the
* context is freed.
*/
static EFI_STATUS
pkcs7_get_signers(const uint8_t *p7data, UINTN p7_len,
PKCS7 **pkcs7, STACK_OF(X509) **certs)
{
const uint8_t *ptr;
ptr = p7data;
*pkcs7 = d2i_PKCS7(NULL, &ptr, (int)p7_len);
if (!*pkcs7)
return EFI_SECURITY_VIOLATION;
if (!PKCS7_type_is_signed(*pkcs7)) {
PKCS7_free(*pkcs7);
*pkcs7 = NULL;
return EFI_SECURITY_VIOLATION;
}
*certs = PKCS7_get0_signers(*pkcs7, NULL, PKCS7_BINARY);
if (!*certs) {
PKCS7_free(*pkcs7);
*pkcs7 = NULL;
return EFI_SECURITY_VIOLATION;
}
return EFI_SUCCESS;
}
/* Returns true iff b is later than a */
static bool time_later(EFI_TIME *a, EFI_TIME *b)
{
if (a->Year != b->Year)
return b->Year > a->Year;
else if (a->Month != b->Month)
return b->Month > a->Month;
else if (a->Day != b->Day)
return b->Day > a->Day;
else if (a->Hour != b->Hour)
return b->Hour > a->Hour;
else if (a->Minute != b->Minute)
return b->Minute > a->Minute;
else
return b->Second > a->Second;
}
enum auth_type {
AUTH_TYPE_PK,
AUTH_TYPE_KEK,
AUTH_TYPE_PAYLOAD,
AUTH_TYPE_PRIVATE,
AUTH_TYPE_NONE,
};
static EFI_STATUS
check_signature_list_format(uint8_t *data, UINTN data_len, bool is_pk)
{
EFI_SIGNATURE_LIST *sig_list;
int count;
UINTN remaining, list_items;
int i;
size_t list_body_size;
if (data_len == 0)
return EFI_SUCCESS;
count = 0;
sig_list = (EFI_SIGNATURE_LIST *)data;
remaining = data_len;
while ((remaining >= sizeof(*sig_list)) &&
(remaining >= sig_list->SignatureListSize)) {
for (i = 0; i < NUM_OF_SUPPORTED_SIG_ITEMS; i++) {
if (!memcmp(&sig_list->SignatureType, &mSupportSigItem[i].SigType,
GUID_LEN)) {
if (mSupportSigItem[i].SigDataSize != (UINT32)~0 &&
(sig_list->SignatureSize - GUID_LEN) != mSupportSigItem[i].SigDataSize)
return EFI_INVALID_PARAMETER;
if (mSupportSigItem[i].SigHeaderSize != ((UINT32) ~0) &&
sig_list->SignatureHeaderSize != mSupportSigItem[i].SigHeaderSize)
return EFI_INVALID_PARAMETER;
break;
}
}
if (i == NUM_OF_SUPPORTED_SIG_ITEMS)
return EFI_INVALID_PARAMETER;
/*
* Check SignatureHeaderSize since its size may be undefined in
* mSupportSigItem.
*/
if (sig_list->SignatureHeaderSize >
(sig_list->SignatureListSize - sizeof(EFI_SIGNATURE_LIST)))
return EFI_INVALID_PARAMETER;
/*
* Check SignatureSize since its size may be undefined in
* mSupportSigItem.
*/
if (sig_list->SignatureSize == 0)
return EFI_INVALID_PARAMETER;
list_body_size = sig_list->SignatureListSize - sizeof(EFI_SIGNATURE_LIST) -
sig_list->SignatureHeaderSize;
if (list_body_size % sig_list->SignatureSize != 0)