-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest.c
More file actions
3301 lines (2734 loc) · 109 KB
/
Copy pathtest.c
File metadata and controls
3301 lines (2734 loc) · 109 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.
*/
/* Including this directly allows us to poke into the implementation. */
#include "handler.c"
#include "mor.c"
#include "xapidb-lib.c"
#include "tools/cert-check.c"
#include <glib.h>
#include <openssl/pem.h>
#include <assert.h>
#include <sys/wait.h>
static char *save_name = "test.dat";
const enum log_level log_level = LOG_LVL_ERROR;
/* The communication buffer. */
static uint8_t buf[16 * 4096];
/* Wide char support */
typedef struct {
uint16_t *data;
size_t length;
} dstring;
static const EFI_GUID nullguid =
{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
/* Sample data */
static dstring *tname1;
static const EFI_GUID tguid1 =
{{1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}};
static const uint8_t tdata1[] = {1, 0, 5, 6, 7};
static dstring *tname2;
static const EFI_GUID tguid2 =
{{1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}};
static const uint8_t tdata2[] = {0, 8, 6, 9, 0, 4, 5};
static dstring *tname3;
static const EFI_GUID tguid3 =
{{6, 4, 5, 7, 3, 8, 9, 1, 3, 2, 3, 4, 5, 6, 7, 8}};
static uint8_t tdata3[] = {9};
static dstring *tname4;
static const EFI_GUID tguid4 =
{{7, 4, 3, 2, 1, 7, 9, 10, 15, 2, 5, 6, 14, 15, 10, 1}};
static const uint8_t tdata4[] = {10, 255, 0, 6, 7, 8, 120, 244};
static dstring *tname5;
static const EFI_GUID tguid5 =
{{1, 3, 5, 7, 9, 8, 6, 4, 2, 10, 11, 12, 13, 14, 15, 0}};
static const uint8_t tdata5[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
static dstring *signatureSupport_name;
static dstring *auditMode_name;
static dstring *deployedMode_name;
static dstring *setupMode_name;
static dstring *secureBoot_name;
static dstring *PK_name;
static dstring *KEK_name;
static dstring *db_name;
static dstring *dbx_name;
static dstring *dbt_name;
static dstring *morControl_name;
static dstring *morControlLock_name;
static const EFI_GUID testOwnerGuid =
{{7, 5, 3, 8, 9, 6, 1, 3, 2, 3, 4, 5, 4, 6, 7, 8}};
static const EFI_TIME test_timea = {2018, 6, 20, 13, 38, 1, 0, 0, 0, 0, 0};
static const EFI_TIME test_timeb = {2018, 6, 20, 13, 38, 2, 0, 0, 0, 0, 0};
static const EFI_TIME test_timec = {2018, 6, 20, 13, 38, 3, 0, 0, 0, 0, 0};
static EFI_SIGNATURE_LIST *certA;
static size_t certA_len;
static EFI_SIGNATURE_LIST *certB;
static size_t certB_len;
static EFI_SIGNATURE_LIST *certPK;
static size_t certPK_len;
#define BSIZ 1024 /* general buffer size */
/* Assert statements, which provide info about the failure */
#define assert_cmpmem(m1, l1, m2, l2) \
g_assert_true(l1 == l2 && memcmp(m1, m2, l1) == 0)
#define vsd_assert_status(_txt, _x, _y, _z, ...) \
do { \
if (!((_x) _y (_z))) \
printf("\nWhile checking " _txt " assert failed: %s %llu " #_y \
" %s %llu\n", ## __VA_ARGS__, \
EFI_ERROR(_x) ? "ERROR" : "", (~EFI_MAX_BIT & _x), \
EFI_ERROR(_z) ? "ERROR" : "", (~EFI_MAX_BIT & _z)); \
g_assert_cmpuint(_x, _y, _z); \
} while (0);
#define vsd_assert_cmpuint(_txt, _x, _y, _z, ...) \
do { \
if (!((_x) _y (_z))) \
printf("\nWhile checking "_txt " assert failed: %lu " #_y \
" %lu\n", ## __VA_ARGS__, (UINTN)(_x), (UINTN)(_z)); \
g_assert_cmpuint(_x, _y, _z); \
} while (0);
#define vsd_assert_nonnull(_txt, _x, ...) \
do { \
if ((_x) == NULL) \
printf("\nWhile checking " _txt " assert non null failed.\n", \
## __VA_ARGS__); \
g_assert_nonnull(_x); \
} while (0);
/*
* dstring handling functions
*/
static size_t dstring_data_size(const dstring *name)
{
return name->length * sizeof(uint16_t);
}
/* convert a dstring into something printable */
static char *get_dstring_pretty(const dstring *s)
{
const int size_of_char_hex = strlen("{xx}");
/* Alloc space for worst case */
char *str = malloc(s->length * size_of_char_hex + 1);
char *pos = str;
int i;
for (i = 0; i < s->length; i++) {
if (s->data[i] > 31 && s->data[i] < 128)
*(pos++) = s->data[i];
else {
sprintf(pos, "{%02x}", s->data[i] && 0xff);
pos += 4;
}
}
*pos = '\0';
return str;
}
static dstring *alloc_dstring_unset(size_t length)
{
dstring *dstr = malloc(sizeof(dstring));
assert(dstr);
dstr->length = length;
dstr->data = calloc(length, sizeof(uint16_t));
assert(dstr->data);
return dstr;
}
static dstring *alloc_dstring(const char *string)
{
size_t i;
size_t len = strlen(string);
dstring *dstr = alloc_dstring_unset(len);
for (i = 0; i < len; i++)
dstr->data[i] = string[i];
return dstr;
}
static void fill_dstring(dstring *dstr, uint16_t value)
{
size_t i;
for (i = 0; i < dstr->length; i++)
dstr->data[i] = value;
}
static void free_dstring(dstring *d)
{
if (d)
free(d->data);
free(d);
}
/*
* Helper functions
*/
static enum backend_init_status testdb_init(void)
{
struct efi_variable *l;
FILE *f = fopen(save_name, "r");
if (!f) {
fprintf(stderr, "failed to open %s : %s\n", save_name, strerror(errno));
abort();
}
for (;;) {
UINTN name_len;
if (fread(&name_len, sizeof name_len, 1, f) != 1)
break;
l = malloc(sizeof *l);
if (!l)
abort();
l->name_len = name_len;
l->name = malloc(l->name_len);
fread(l->name, 1, l->name_len, f);
fread(&l->data_len, sizeof l->data_len, 1, f);
l->data = malloc(l->data_len);
fread(l->data, 1, l->data_len, f);
fread(&l->guid, 1, GUID_LEN, f);
fread(&l->attributes, 1, sizeof l->attributes, f);
l->next = var_list;
var_list = l;
}
fclose(f);
return BACKEND_INIT_SUCCESS;
}
static bool testdb_save(bool refresh)
{
(void)refresh;
struct efi_variable *l;
FILE *f = fopen(save_name, "w");
if (!f) {
fprintf(stderr, "failed to open %s %s\n", save_name, strerror(errno));
abort();
}
l = var_list;
while (l) {
if (l->attributes & EFI_VARIABLE_NON_VOLATILE) {
fwrite(&l->name_len, sizeof l->name_len, 1, f);
fwrite(l->name, 1, l->name_len, f);
fwrite(&l->data_len, sizeof l->data_len, 1, f);
fwrite(l->data, 1, l->data_len, f);
fwrite(&l->guid, 1, GUID_LEN, f);
fwrite(&l->attributes, sizeof l->attributes, 1, f);
}
l = l->next;
}
fclose(f);
return true;
}
const struct backend testdb = {
.parse_arg = NULL,
.check_args = NULL,
.init = testdb_init,
.save = NULL,
.resume = NULL,
.set_variable = testdb_save,
};
const struct backend *db = &testdb;
static void read_x509_into_CertList(char *certfile,
EFI_SIGNATURE_LIST **ret_cert, size_t *len);
static void setup_globals(void)
{
tname1 = alloc_dstring("foo");
tname2 = alloc_dstring("foobar");
tname3 = alloc_dstring("foobar");
tname4 = alloc_dstring("baz");
tname5 = alloc_dstring("xyzabcdefgh");
signatureSupport_name = alloc_dstring("SignatureSupport");
auditMode_name = alloc_dstring("AuditMode");
deployedMode_name = alloc_dstring("DeployedMode");
setupMode_name = alloc_dstring("SetupMode");
secureBoot_name = alloc_dstring("SecureBoot");
PK_name = alloc_dstring("PK");
KEK_name = alloc_dstring("KEK");
db_name = alloc_dstring("db");
dbx_name = alloc_dstring("dbx");
dbt_name = alloc_dstring("dbt");
morControl_name = alloc_dstring("MemoryOverwriteRequestControl");
morControlLock_name = alloc_dstring("MemoryOverwriteRequestControlLock");
secure_boot_enable = true;
read_x509_into_CertList("testcertA.pem", &certA, &certA_len);
read_x509_into_CertList("testcertB.pem", &certB, &certB_len);
read_x509_into_CertList("testPK.pem", &certPK, &certPK_len);
}
static void free_globals(void)
{
free_dstring(tname1);
free_dstring(tname2);
free_dstring(tname3);
free_dstring(tname4);
free_dstring(tname5);
free_dstring(signatureSupport_name);
free_dstring(auditMode_name);
free_dstring(deployedMode_name);
free_dstring(setupMode_name);
free_dstring(secureBoot_name);
free_dstring(PK_name);
free_dstring(KEK_name);
free_dstring(db_name);
free_dstring(dbx_name);
free_dstring(dbt_name);
free_dstring(morControl_name);
free_dstring(morControlLock_name);
free(certA);
free(certB);
free(certPK);
}
static void reset_vars(void)
{
struct efi_variable *l, *tmp;
l = var_list;
while (l) {
tmp = l;
l = tmp->next;
free(tmp->name);
free(tmp->data);
free(tmp);
}
var_list = NULL;
}
static void call_get_variable(const dstring *name, const EFI_GUID *guid,
UINTN avail, BOOLEAN at_runtime)
{
uint8_t *ptr = buf;
serialize_uint32(&ptr, 1);
serialize_uint32(&ptr, (UINT32)COMMAND_GET_VARIABLE);
serialize_data(&ptr, (uint8_t *)name->data, dstring_data_size(name));
serialize_guid(&ptr, guid);
serialize_uintn(&ptr, avail);
*ptr++ = at_runtime;
dispatch_command(buf);
}
static EFI_STATUS call_get_variable_data(const dstring *name,
const EFI_GUID *guid,
UINTN avail, BOOLEAN at_runtime,
uint8_t **data, UINTN *len)
{
uint8_t *ptr = buf;
EFI_STATUS status;
call_get_variable(name, guid, avail, at_runtime);
status = unserialize_uintn(&ptr);
unserialize_uint32(&ptr); /* attr */
*data = unserialize_data(&ptr, len, BSIZ);
return status;
}
static void call_query_variable_info(void)
{
uint8_t *ptr = buf;
serialize_uint32(&ptr, 1);
serialize_uint32(&ptr, (UINT32)COMMAND_QUERY_VARIABLE_INFO);
serialize_uint32(&ptr, 0);
dispatch_command(buf);
}
static void call_get_next_variable(UINTN avail, const dstring *name,
const EFI_GUID *guid, BOOLEAN at_runtime)
{
uint8_t *ptr = buf;
size_t len = name ? dstring_data_size(name) : 0;
const uint8_t *data = (uint8_t *)(name ? name->data : NULL);
serialize_uint32(&ptr, 1);
serialize_uint32(&ptr, (UINT32)COMMAND_GET_NEXT_VARIABLE);
serialize_uintn(&ptr, avail);
serialize_data(&ptr, data, len);
serialize_guid(&ptr, guid);
*ptr++ = at_runtime;
dispatch_command(buf);
}
static void call_set_variable(const dstring *name, const EFI_GUID *guid,
const uint8_t *data, UINTN data_len,
UINT32 attr, BOOLEAN at_runtime)
{
uint8_t *ptr = buf;
size_t name_size = dstring_data_size(name);
serialize_uint32(&ptr, 1);
serialize_uint32(&ptr, (UINT32)COMMAND_SET_VARIABLE);
serialize_data(&ptr, (uint8_t *)name->data, name_size);
serialize_guid(&ptr, guid);
serialize_data(&ptr, data, data_len);
serialize_uint32(&ptr, attr);
serialize_boolean(&ptr, at_runtime);
dispatch_command(buf);
}
/*
* This calls SetVariable and checks the result against expected. In the
* failing case, the line number provided reported.
*/
static EFI_STATUS setVariable_check_line(const dstring *name,
const EFI_GUID *guid,
const uint8_t *data, UINTN len,
UINT32 attr, EFI_STATUS expected,
int line)
{
uint8_t *ptr;
EFI_STATUS status;
char *nice_name;
call_set_variable(name, guid, data, len, attr, 0);
ptr = buf;
status = unserialize_uintn(&ptr);
nice_name = get_dstring_pretty(name);
vsd_assert_status("set_variable(\"%s\", ...) for line %d", status, ==,
expected, nice_name, line);
free(nice_name);
return status;
}
#define sv_check(_name, _guid, _data, _len, _attr, _expected) \
setVariable_check_line(_name, _guid, _data, _len, _attr, \
_expected, __LINE__);
#define sv_ok(_name, _guid, _data, _len, _attr) \
setVariable_check_line(_name, _guid, _data, _len, _attr, \
EFI_SUCCESS, __LINE__);
/*
* Crypto/signing functions
*/
struct sign_details
{
char *cert;
char *key;
char *digest;
};
static const struct sign_details sign_testPK =
{"testPK.pem", "testPK.key", "SHA256"};
static const struct sign_details sign_bad_digest =
{"testPK.pem", "testPK.key", "SHA224"};
static const struct sign_details sign_certB =
{"testcertB.pem", "testcertB.key", "SHA256"};
static void setup_ssl(void)
{
ERR_load_crypto_strings();
OpenSSL_add_all_digests();
OpenSSL_add_all_ciphers();
}
struct cert_list {
struct cert_list *next;
X509 *cert;
};
static void read_x509_list_into_CertList(char **certfile,
EFI_SIGNATURE_LIST **ret_cert, size_t *len)
{
EFI_SIGNATURE_LIST *pk_cert;
EFI_SIGNATURE_DATA *pk_cert_data;
unsigned char *tmp;
int pk_cert_len;
BIO *cert_bio;
X509 *cert;
struct cert_list *cert_list, *next, **cert_tail_ptr;
int i, num_certs = 0;
size_t cert_len, largest_cert_len = 0;
ERR_clear_error();
cert_tail_ptr = &cert_list;
for (i = 0; certfile[i]; i++) {
*cert_tail_ptr = malloc(sizeof(struct cert_list));
assert(*cert_tail_ptr);
num_certs++;
cert_bio = BIO_new_file(certfile[i], "r");
assert(cert_bio);
cert = PEM_read_bio_X509(cert_bio, NULL, NULL, NULL);
vsd_assert_nonnull("reading cert \"%s\"", cert, certfile[i]);
BIO_free(cert_bio);
cert_len = i2d_X509(cert, NULL);
if (largest_cert_len < cert_len)
largest_cert_len = cert_len;
(*cert_tail_ptr)->cert = cert;
cert_tail_ptr = &(*cert_tail_ptr)->next;
}
*cert_tail_ptr = NULL;
pk_cert_len = sizeof(EFI_SIGNATURE_LIST) + num_certs *
(largest_cert_len + offsetof(EFI_SIGNATURE_DATA, SignatureData));
pk_cert = calloc(1, pk_cert_len);
vsd_assert_nonnull("malloc pk cert", pk_cert);
/* certs all loaded, and memory allocated - now populate pk_cert */
memcpy(&pk_cert->SignatureType, &gEfiCertX509Guid, sizeof(gEfiCertX509Guid));
pk_cert->SignatureListSize = pk_cert_len;
pk_cert->SignatureHeaderSize = 0;
pk_cert->SignatureSize = largest_cert_len +
offsetof(EFI_SIGNATURE_DATA, SignatureData);
pk_cert_data = (void *)pk_cert + sizeof(EFI_SIGNATURE_LIST);
cert = NULL;
while (cert_list)
{
next = cert_list->next;
tmp = (uint8_t *)pk_cert_data + offsetof(EFI_SIGNATURE_DATA, SignatureData);
i2d_X509(cert_list->cert, &tmp);
pk_cert_data->SignatureOwner = testOwnerGuid;
pk_cert_data = (EFI_SIGNATURE_DATA *)((uint8_t *)pk_cert_data + pk_cert->SignatureSize);
X509_free(cert_list->cert);
free(cert_list);
cert_list = next;
}
*ret_cert = pk_cert;
*len = pk_cert_len;
}
static void read_x509_into_CertList(char *certfile, EFI_SIGNATURE_LIST **ret_cert,
size_t *len)
{
char *list[2] = { certfile, NULL };
read_x509_list_into_CertList(list, ret_cert, len);
}
static size_t sign(uint8_t **signed_buf, const dstring *varname,
const EFI_GUID *vendor_guid, UINT32 attributes,
const EFI_TIME *timestamp, const uint8_t *data,
size_t data_size, const struct sign_details *sd)
{
PKCS7 *p7;
BIO *bio;
X509 *cert;
EVP_PKEY *pkey;
const EVP_MD *md;
void *ret;
int sig_size;
size_t auth_size;
uint8_t *buf;
EFI_VARIABLE_AUTHENTICATION_2 *var_auth;
unsigned char *auth_sigbuf;
size_t name_size = dstring_data_size(varname);
int request_len = name_size + sizeof(EFI_GUID) + sizeof(UINT32) +
sizeof(EFI_TIME) + data_size;
uint8_t *request = malloc(request_len);
uint8_t *ptr = request;
vsd_assert_nonnull("malloc signing request buffer", request);
/*
* signature is over variable name (no null), the vendor GUID, the
* attributes, the timestamp and the contents
*/
/* copy var name */
memcpy(ptr, varname->data, name_size);
ptr += name_size;
/* copy vendor_guid */
memcpy(ptr, vendor_guid, GUID_LEN);
ptr += sizeof(EFI_GUID);
/* copy attibutes */
memcpy(ptr, &attributes, sizeof(attributes));
ptr += sizeof(attributes);
/* copy timestamp */
memcpy(ptr, timestamp, sizeof(*timestamp));
ptr += sizeof(*timestamp);
/* copy data */
if (data)
memcpy(ptr, data, data_size);
/* sign */
ERR_clear_error();
bio = BIO_new_file(sd->cert, "r");
assert(bio);
cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
vsd_assert_nonnull("PEM_read_bio_X509(\"%s\")", cert, sd->cert);
BIO_free(bio);
bio = BIO_new_file(sd->key, "r");
assert(bio);
pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
vsd_assert_nonnull("PEM_read_bio_PrivateKey(\"%s\")", pkey, sd->key);
BIO_free(bio);
bio = BIO_new_mem_buf(request, request_len);
assert(bio);
p7 = PKCS7_sign(NULL, NULL, NULL, bio, PKCS7_BINARY | PKCS7_PARTIAL |
PKCS7_DETACHED | PKCS7_NOATTR);
assert(p7);
md = EVP_get_digestbyname(sd->digest);
vsd_assert_nonnull("digest", md);
ret = PKCS7_sign_add_signer(p7, cert, pkey, md,
PKCS7_BINARY | PKCS7_DETACHED | PKCS7_NOATTR);
vsd_assert_nonnull("PKCS7 add signer", ret);
PKCS7_final(p7, bio, PKCS7_BINARY | PKCS7_DETACHED | PKCS7_NOATTR);
BIO_free(bio);
free(request);
EVP_PKEY_free(pkey);
X509_free(cert);
sig_size = i2d_PKCS7(p7, NULL);
auth_size = offsetof(EFI_VARIABLE_AUTHENTICATION_2, AuthInfo.CertData) +
sig_size;
buf = malloc(auth_size + data_size);
vsd_assert_nonnull("malloc output buffer", request);
var_auth = (EFI_VARIABLE_AUTHENTICATION_2 *)buf;
var_auth->TimeStamp = *timestamp;
memcpy(&var_auth->AuthInfo.CertType, &gEfiCertPkcs7Guid,
sizeof(gEfiCertPkcs7Guid));
var_auth->AuthInfo.Hdr.dwLength = sig_size +
offsetof(WIN_CERTIFICATE_UEFI_GUID, CertData);
var_auth->AuthInfo.Hdr.wRevision = 0x0200;
var_auth->AuthInfo.Hdr.wCertificateType = WIN_CERT_TYPE_EFI_GUID;
auth_sigbuf = var_auth->AuthInfo.CertData;
i2d_PKCS7(p7, &auth_sigbuf);
PKCS7_free(p7);
if (data)
memcpy(buf + auth_size, data, data_size);
*signed_buf = buf;
return auth_size + data_size;
}
static void sign_and_check_(const dstring *varname, const EFI_GUID *vendor_guid,
UINT32 attributes, const EFI_TIME *timestamp,
const uint8_t *data, size_t data_size,
const struct sign_details *sd, EFI_STATUS expected,
int line)
{
uint8_t *sign_buffer;
int len;
len = sign(&sign_buffer, varname, vendor_guid, attributes, timestamp,
data, data_size, sd);
setVariable_check_line(varname, vendor_guid, sign_buffer, len,
attributes, expected, line);
free(sign_buffer);
}
#define sign_and_check(_name, _vend, _attr, _time, _data, _size, _sig, _e_d) \
sign_and_check_(_name, _vend, _attr, _time, _data, _size, _sig, _e_d, __LINE__)
/*
* This function checks the variable's data is as expected.
* The expected data is provided, such that it can be compared
*/
#define check_variable_data(_name, _guid, _avai, _runtime, _expected, _len) \
check_variable_data_(_name, _guid, _avai, _runtime, _expected, _len, __LINE__)
static void check_variable_data_(const dstring *name, const EFI_GUID *guid,
UINTN avail, BOOLEAN at_runtime,
const uint8_t *expected_data,
UINTN expected_len, int line)
{
uint8_t *ret_data;
EFI_STATUS status;
int cmp;
UINTN len;
char *nice_name = get_dstring_pretty(name);
status = call_get_variable_data(name, guid, avail, at_runtime, &ret_data, &len);
vsd_assert_status("get_variable(\"%s\") at %d", status, ==, 0,
nice_name, line);
vsd_assert_cmpuint("data length for \"%s\" at %d", expected_len, ==, len,
nice_name, line);
cmp = memcmp(ret_data, expected_data, len);
vsd_assert_cmpuint("cmp of data for \"%s\" at %d", cmp, ==, 0,
nice_name, line);
free(ret_data);
free(nice_name);
}
static void test_get_variable_no_name(void)
{
uint8_t *ptr;
EFI_STATUS status;
dstring *empty = alloc_dstring("");
reset_vars();
/* An empty name should not be found. */
call_get_variable(empty, &nullguid, BSIZ, 0);
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_NOT_FOUND);
free_dstring(empty);
}
static void test_get_variable_long_name(void)
{
uint8_t *ptr;
EFI_STATUS status;
dstring *bigname;
reset_vars();
bigname = alloc_dstring_unset(NAME_LIMIT / sizeof(uint16_t) + 1);
fill_dstring(bigname, 42);
/* Test the maximum variable name length. */
call_get_variable(bigname, &nullguid, BSIZ, 0);
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_DEVICE_ERROR);
free_dstring(bigname);
}
static void test_get_variable_not_found(void)
{
uint8_t *ptr;
EFI_STATUS status;
reset_vars();
sv_ok(tname1, &tguid1, tdata1, sizeof(tdata1), ATTR_B);
sv_ok(tname2, &tguid2, tdata2, sizeof(tdata2), ATTR_B);
sv_ok(tname3, &tguid3, tdata3, sizeof(tdata3), ATTR_B);
/* Name is correct, guid is wrong */
call_get_variable(tname2, &tguid4, BSIZ, 0);
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_NOT_FOUND);
/* Name is wrong, guid is correct */
call_get_variable(tname4, &tguid2, BSIZ, 0);
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_NOT_FOUND);
/* Boot service only variable cannot be found at runtime */
call_get_variable(tname2, &tguid2, BSIZ, 1);
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_NOT_FOUND);
}
static void test_get_variable_found(void)
{
uint8_t *ptr;
uint8_t *data;
UINTN data_len;
UINT32 attr;
EFI_STATUS status;
reset_vars();
sv_ok(tname1, &tguid1, tdata1, sizeof(tdata1), ATTR_B);
sv_ok(tname2, &tguid2, tdata2, sizeof(tdata2), ATTR_BR);
sv_ok(tname3, &tguid3, tdata3, sizeof(tdata3), ATTR_B);
/* Variable is correctly retrieved. */
call_get_variable(tname1, &tguid1, BSIZ, 0);
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_SUCCESS);
attr = unserialize_uint32(&ptr);
g_assert_cmpuint(attr, ==, ATTR_B);
data = unserialize_data(&ptr, &data_len, BSIZ);
g_assert_cmpuint(data_len, ==, sizeof(tdata1));
g_assert(!memcmp(tdata1, data, data_len));
free(data);
/* Runtime variable can be found at runtime */
call_get_variable(tname2, &tguid2, BSIZ, 1);
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_SUCCESS);
attr = unserialize_uint32(&ptr);
g_assert_cmpuint(attr, ==, ATTR_BR);
data = unserialize_data(&ptr, &data_len, BSIZ);
g_assert_cmpuint(data_len, ==, sizeof(tdata2));
g_assert(!memcmp(tdata2, data, data_len));
free(data);
/* Variable is correctly retrieved. */
call_get_variable(tname3, &tguid3, BSIZ, 0);
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_SUCCESS);
attr = unserialize_uint32(&ptr);
g_assert_cmpuint(attr, ==, ATTR_B);
data = unserialize_data(&ptr, &data_len, BSIZ);
g_assert_cmpuint(data_len, ==, sizeof(tdata3));
g_assert(!memcmp(tdata3, data, data_len));
free(data);
}
static void test_get_variable_too_small(void)
{
uint8_t *ptr;
UINTN data_len;
EFI_STATUS status;
reset_vars();
sv_ok(tname1, &tguid1, tdata1, sizeof(tdata1), ATTR_B);
/*
* If the output buffer is too small, check that the correct size is
* returned.
*/
call_get_variable(tname1, &tguid1, sizeof(tdata1) - 1, 0);
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_BUFFER_TOO_SMALL);
data_len = unserialize_uintn(&ptr);
g_assert_cmpuint(data_len, ==, sizeof(tdata1));
}
static void test_query_variable_info(void)
{
uint8_t *ptr;
EFI_STATUS status;
dstring *longname;
reset_vars();
/*
* Use a long variable name to ensure the variable is larger than the
* "overhead" size.
*/
longname = alloc_dstring_unset(VARIABLE_SIZE_OVERHEAD / sizeof(uint16_t));
fill_dstring(longname, 'a');
/* Check the defined limits with no variables. */
call_query_variable_info();
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_SUCCESS);
g_assert_cmpuint(TOTAL_LIMIT, ==, unserialize_uintn(&ptr));
g_assert_cmpuint(TOTAL_LIMIT, ==, unserialize_uintn(&ptr));
g_assert_cmpuint(DATA_LIMIT, ==, unserialize_uintn(&ptr));
sv_ok(longname, &tguid1, tdata1, sizeof(tdata1), ATTR_B);
/* Inserting a variable updates the limits correctly. */
call_query_variable_info();
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_SUCCESS);
g_assert_cmpuint(TOTAL_LIMIT, ==, unserialize_uintn(&ptr));
g_assert_cmpuint(TOTAL_LIMIT - dstring_data_size(longname) -
sizeof(tdata1) - VARIABLE_SIZE_OVERHEAD,
==, unserialize_uintn(&ptr));
g_assert_cmpuint(DATA_LIMIT, ==, unserialize_uintn(&ptr));
/* Updating a variable updates the limits correctly. */
sv_ok(longname, &tguid1, tdata2, sizeof(tdata2), ATTR_B);
call_query_variable_info();
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_SUCCESS);
g_assert_cmpuint(TOTAL_LIMIT, ==, unserialize_uintn(&ptr));
g_assert_cmpuint(TOTAL_LIMIT - dstring_data_size(longname) -
sizeof(tdata2) - VARIABLE_SIZE_OVERHEAD,
==, unserialize_uintn(&ptr));
g_assert_cmpuint(DATA_LIMIT, ==, unserialize_uintn(&ptr));
/* Appending to a variable updates the limits correctly. */
sv_ok(longname, &tguid1, tdata1, sizeof(tdata1),
ATTR_B | EFI_VARIABLE_APPEND_WRITE);
call_query_variable_info();
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_SUCCESS);
g_assert_cmpuint(TOTAL_LIMIT, ==, unserialize_uintn(&ptr));
g_assert_cmpuint(TOTAL_LIMIT - dstring_data_size(longname) -
sizeof(tdata2) - sizeof(tdata1) - VARIABLE_SIZE_OVERHEAD,
==, unserialize_uintn(&ptr));
g_assert_cmpuint(DATA_LIMIT, ==, unserialize_uintn(&ptr));
/* Deleting a variable updates the limits correctly. */
sv_ok(longname, &tguid1, tdata1, sizeof(tdata1), 0);
call_query_variable_info();
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_SUCCESS);
g_assert_cmpuint(TOTAL_LIMIT, ==, unserialize_uintn(&ptr));
g_assert_cmpuint(TOTAL_LIMIT, ==, unserialize_uintn(&ptr));
g_assert_cmpuint(DATA_LIMIT, ==, unserialize_uintn(&ptr));
free_dstring(longname);
}
static void test_get_next_variable_empty(void)
{
uint8_t *ptr;
EFI_STATUS status;
reset_vars();
/* No variables */
call_get_next_variable(BSIZ, NULL, &nullguid, 0);
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_NOT_FOUND);
}
static void test_get_next_variable_long_name(void)
{
uint8_t *ptr;
EFI_STATUS status;
dstring *tmp_name;
reset_vars();
tmp_name = alloc_dstring_unset(NAME_LIMIT / sizeof(uint16_t) + 1);
fill_dstring(tmp_name, 42);
/* Input name exceeds the limit */
call_get_next_variable(BSIZ, tmp_name, &nullguid, 0);
ptr = buf;
status = unserialize_uintn(&ptr);
g_assert_cmpuint(status, ==, EFI_DEVICE_ERROR);
free_dstring(tmp_name);
}
static void test_get_next_variable_only_runtime(void)
{
uint8_t *ptr, *data;
UINTN data_len;
EFI_STATUS status;
EFI_GUID guid;
/*
* Insert a mixture of variables.
* Only runtime variables should be returned at runtime.
*/
reset_vars();
sv_ok(tname5, &tguid5, tdata5, sizeof(tdata5), ATTR_B);
sv_ok(tname2, &tguid2, tdata2, sizeof(tdata2), ATTR_BR);
sv_ok(tname1, &tguid1, tdata1, sizeof(tdata1), ATTR_B);