forked from rhboot/shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MokManager.c
2688 lines (2260 loc) · 63.4 KB
/
MokManager.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
// SPDX-License-Identifier: BSD-2-Clause-Patent
#include "shim.h"
#include <Library/BaseCryptLib.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/asn1.h>
#include <openssl/bn.h>
#define PASSWORD_MAX 256
#define PASSWORD_MIN 1
#define SB_PASSWORD_LEN 16
#define NAME_LINE_MAX 70
#ifndef SHIM_VENDOR
#define SHIM_VENDOR L"Shim"
#endif
#define CERT_STRING L"Select an X509 certificate to enroll:\n\n"
#define HASH_STRING L"Select a file to trust:\n\n"
#define CompareMemberGuid(x, y) CompareMem(x, y, sizeof(EFI_GUID))
typedef struct {
UINT32 MokSize;
UINT8 *Mok;
EFI_GUID Type;
} __attribute__ ((packed)) MokListNode;
typedef struct {
UINT32 MokSBState;
UINT32 PWLen;
CHAR16 Password[SB_PASSWORD_LEN];
} __attribute__ ((packed)) MokSBvar;
typedef struct {
UINT32 MokDBState;
UINT32 PWLen;
CHAR16 Password[SB_PASSWORD_LEN];
} __attribute__ ((packed)) MokDBvar;
typedef struct {
UINT32 MokTMLState;
UINT32 PWLen;
CHAR16 Password[SB_PASSWORD_LEN];
} __attribute__ ((packed)) MokTMLvar;
typedef struct {
INT32 Timeout;
} __attribute__ ((packed)) MokTimeoutvar;
static EFI_STATUS get_sha1sum(void *Data, int DataSize, UINT8 * hash)
{
EFI_STATUS efi_status;
unsigned int ctxsize;
void *ctx = NULL;
ctxsize = Sha1GetContextSize();
ctx = AllocatePool(ctxsize);
if (!ctx) {
console_notify(L"Unable to allocate memory for hash context");
return EFI_OUT_OF_RESOURCES;
}
if (!Sha1Init(ctx)) {
console_notify(L"Unable to initialise hash");
efi_status = EFI_OUT_OF_RESOURCES;
goto done;
}
if (!(Sha1Update(ctx, Data, DataSize))) {
console_notify(L"Unable to generate hash");
efi_status = EFI_OUT_OF_RESOURCES;
goto done;
}
if (!(Sha1Final(ctx, hash))) {
console_notify(L"Unable to finalise hash");
efi_status = EFI_OUT_OF_RESOURCES;
goto done;
}
efi_status = EFI_SUCCESS;
done:
return efi_status;
}
static BOOLEAN is_sha2_hash(EFI_GUID Type)
{
if (CompareGuid(&Type, &EFI_CERT_SHA224_GUID) == 0)
return TRUE;
else if (CompareGuid(&Type, &EFI_CERT_SHA256_GUID) == 0)
return TRUE;
else if (CompareGuid(&Type, &EFI_CERT_SHA384_GUID) == 0)
return TRUE;
else if (CompareGuid(&Type, &EFI_CERT_SHA512_GUID) == 0)
return TRUE;
return FALSE;
}
static UINT32 sha_size(EFI_GUID Type)
{
if (CompareGuid(&Type, &EFI_CERT_SHA1_GUID) == 0)
return SHA1_DIGEST_SIZE;
else if (CompareGuid(&Type, &EFI_CERT_SHA224_GUID) == 0)
return SHA224_DIGEST_LENGTH;
else if (CompareGuid(&Type, &EFI_CERT_SHA256_GUID) == 0)
return SHA256_DIGEST_SIZE;
else if (CompareGuid(&Type, &EFI_CERT_SHA384_GUID) == 0)
return SHA384_DIGEST_LENGTH;
else if (CompareGuid(&Type, &EFI_CERT_SHA512_GUID) == 0)
return SHA512_DIGEST_LENGTH;
return 0;
}
static BOOLEAN is_valid_siglist(EFI_GUID Type, UINT32 SigSize)
{
UINT32 hash_sig_size;
if (CompareGuid (&Type, &X509_GUID) == 0 && SigSize != 0)
return TRUE;
if (!is_sha2_hash(Type))
return FALSE;
hash_sig_size = sha_size(Type) + sizeof(EFI_GUID);
if (SigSize != hash_sig_size)
return FALSE;
return TRUE;
}
static UINT32 count_keys(void *Data, UINTN DataSize)
{
EFI_SIGNATURE_LIST *CertList = Data;
UINTN dbsize = DataSize;
UINT32 MokNum = 0;
void *end = Data + DataSize;
while ((dbsize > 0) && (dbsize >= CertList->SignatureListSize)) {
/* Use ptr arithmetics to ensure bounded access. Do not allow 0
* SignatureListSize that will cause endless loop. */
if ((void *)(CertList + 1) > end
|| CertList->SignatureListSize == 0) {
console_notify
(L"Invalid MOK detected! Ignoring MOK List.");
return 0;
}
if (CertList->SignatureListSize == 0 ||
CertList->SignatureListSize <= CertList->SignatureSize) {
console_errorbox(L"Corrupted signature list");
return 0;
}
if (!is_valid_siglist
(CertList->SignatureType, CertList->SignatureSize)) {
console_errorbox(L"Invalid signature list found");
return 0;
}
MokNum++;
dbsize -= CertList->SignatureListSize;
CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList +
CertList->SignatureListSize);
}
return MokNum;
}
static MokListNode *build_mok_list(UINT32 num, void *Data, UINTN DataSize)
{
MokListNode *list;
EFI_SIGNATURE_LIST *CertList = Data;
EFI_SIGNATURE_DATA *Cert;
UINTN dbsize = DataSize;
UINTN count = 0;
void *end = Data + DataSize;
list = AllocatePool(sizeof(MokListNode) * num);
if (!list) {
console_notify(L"Unable to allocate MOK list");
return NULL;
}
while ((dbsize > 0) && (dbsize >= CertList->SignatureListSize)) {
/* CertList out of bounds? */
if ((void *)(CertList + 1) > end
|| CertList->SignatureListSize == 0) {
FreePool(list);
return NULL;
}
/* Omit the signature check here since we already did it
in count_keys() */
Cert = (EFI_SIGNATURE_DATA *) (((UINT8 *) CertList) +
sizeof(EFI_SIGNATURE_LIST) +
CertList->SignatureHeaderSize);
/* Cert out of bounds? */
if ((void *)(Cert + 1) > end
|| CertList->SignatureSize <= sizeof(EFI_GUID)) {
FreePool(list);
return NULL;
}
list[count].Type = CertList->SignatureType;
if (CompareGuid (&CertList->SignatureType, &X509_GUID) == 0) {
list[count].MokSize = CertList->SignatureSize -
sizeof(EFI_GUID);
list[count].Mok = (void *)Cert->SignatureData;
} else {
list[count].MokSize = CertList->SignatureListSize -
sizeof(EFI_SIGNATURE_LIST);
list[count].Mok = (void *)Cert;
}
/* MOK out of bounds? */
if (list[count].MokSize > (unsigned long)end -
(unsigned long)list[count].Mok) {
FreePool(list);
return NULL;
}
count++;
dbsize -= CertList->SignatureListSize;
CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList +
CertList->SignatureListSize);
}
return list;
}
typedef struct {
int nid;
CHAR16 *name;
} NidName;
static NidName nidname[] = {
{NID_commonName, L"CN"},
{NID_organizationName, L"O"},
{NID_countryName, L"C"},
{NID_stateOrProvinceName, L"ST"},
{NID_localityName, L"L"},
{-1, NULL}
};
static CHAR16 *get_x509_name(X509_NAME * X509Name)
{
CHAR16 name[NAME_LINE_MAX + 1];
CHAR16 part[NAME_LINE_MAX + 1];
char str[NAME_LINE_MAX];
int i, len, rest, first;
name[0] = '\0';
rest = NAME_LINE_MAX;
first = 1;
for (i = 0; nidname[i].name != NULL; i++) {
int add;
len = X509_NAME_get_text_by_NID(X509Name, nidname[i].nid,
str, NAME_LINE_MAX);
if (len <= 0)
continue;
if (first)
add = len + (int)StrLen(nidname[i].name) + 1;
else
add = len + (int)StrLen(nidname[i].name) + 3;
if (add > rest)
continue;
if (first) {
SPrint(part, NAME_LINE_MAX * sizeof(CHAR16), L"%s=%a",
nidname[i].name, str);
} else {
SPrint(part, NAME_LINE_MAX * sizeof(CHAR16), L", %s=%a",
nidname[i].name, str);
}
StrCat(name, part);
rest -= add;
first = 0;
}
if (rest >= 0 && rest < NAME_LINE_MAX)
return PoolPrint(L"%s", name);
return NULL;
}
static CHAR16 *get_x509_time(ASN1_TIME * time)
{
BIO *bio = BIO_new(BIO_s_mem());
char str[30];
int len;
ASN1_TIME_print(bio, time);
len = BIO_read(bio, str, 29);
if (len < 0)
len = 0;
str[len] = '\0';
BIO_free(bio);
return PoolPrint(L"%a", str);
}
static void show_x509_info(X509 * X509Cert, UINT8 * hash)
{
ASN1_INTEGER *serial;
BIGNUM *bnser;
unsigned char hexbuf[30];
X509_NAME *X509Name;
ASN1_TIME *time;
CHAR16 *issuer = NULL;
CHAR16 *subject = NULL;
CHAR16 *from = NULL;
CHAR16 *until = NULL;
EXTENDED_KEY_USAGE *extusage;
POOL_PRINT hash_string1;
POOL_PRINT hash_string2;
POOL_PRINT serial_string;
int fields = 0;
CHAR16 **text;
int i = 0;
ZeroMem(&hash_string1, sizeof(hash_string1));
ZeroMem(&hash_string2, sizeof(hash_string2));
ZeroMem(&serial_string, sizeof(serial_string));
serial = X509_get_serialNumber(X509Cert);
if (serial) {
int i, n;
bnser = ASN1_INTEGER_to_BN(serial, NULL);
n = BN_bn2bin(bnser, hexbuf);
for (i = 0; i < n; i++) {
CatPrint(&serial_string, L"%02x:", hexbuf[i]);
}
BN_free(bnser);
}
if (serial_string.str)
fields++;
X509Name = X509_get_issuer_name(X509Cert);
if (X509Name) {
issuer = get_x509_name(X509Name);
if (issuer)
fields++;
}
X509Name = X509_get_subject_name(X509Cert);
if (X509Name) {
subject = get_x509_name(X509Name);
if (subject)
fields++;
}
time = X509_get_notBefore(X509Cert);
if (time) {
from = get_x509_time(time);
if (from)
fields++;
}
time = X509_get_notAfter(X509Cert);
if (time) {
until = get_x509_time(time);
if (until)
fields++;
}
for (i = 0; i < 10; i++)
CatPrint(&hash_string1, L"%02x ", hash[i]);
for (i = 10; i < 20; i++)
CatPrint(&hash_string2, L"%02x ", hash[i]);
if (hash_string1.str)
fields++;
if (hash_string2.str)
fields++;
if (!fields)
return;
i = 0;
extusage = X509_get_ext_d2i(X509Cert, NID_ext_key_usage, NULL, NULL);
text = AllocateZeroPool(sizeof(CHAR16 *) *
(fields * 3 +
sk_ASN1_OBJECT_num(extusage) + 3));
if (extusage) {
int j = 0;
text[i++] = StrDuplicate(L"[Extended Key Usage]");
for (j = 0; j < sk_ASN1_OBJECT_num(extusage); j++) {
POOL_PRINT extkeyusage;
ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(extusage, j);
int buflen = 80;
char buf[buflen];
ZeroMem(&extkeyusage, sizeof(extkeyusage));
OBJ_obj2txt(buf, buflen, obj, 0);
CatPrint(&extkeyusage, L"OID: %a", buf);
text[i++] = StrDuplicate(extkeyusage.str);
FreePool(extkeyusage.str);
}
text[i++] = StrDuplicate(L"");
EXTENDED_KEY_USAGE_free(extusage);
}
if (serial_string.str) {
text[i++] = StrDuplicate(L"[Serial Number]");
text[i++] = serial_string.str;
text[i++] = StrDuplicate(L"");
}
if (issuer) {
text[i++] = StrDuplicate(L"[Issuer]");
text[i++] = issuer;
text[i++] = StrDuplicate(L"");
}
if (subject) {
text[i++] = StrDuplicate(L"[Subject]");
text[i++] = subject;
text[i++] = StrDuplicate(L"");
}
if (from) {
text[i++] = StrDuplicate(L"[Valid Not Before]");
text[i++] = from;
text[i++] = StrDuplicate(L"");
}
if (until) {
text[i++] = StrDuplicate(L"[Valid Not After]");
text[i++] = until;
text[i++] = StrDuplicate(L"");
}
if (hash_string1.str) {
text[i++] = StrDuplicate(L"[Fingerprint]");
text[i++] = hash_string1.str;
}
if (hash_string2.str) {
text[i++] = hash_string2.str;
text[i++] = StrDuplicate(L"");
}
text[i] = NULL;
console_print_box(text, -1);
for (i = 0; text[i] != NULL; i++)
FreePool(text[i]);
FreePool(text);
}
static void show_sha_digest(EFI_GUID Type, UINT8 * hash)
{
CHAR16 *text[5];
POOL_PRINT hash_string1;
POOL_PRINT hash_string2;
int i;
int length;
if (CompareGuid(&Type, &EFI_CERT_SHA1_GUID) == 0) {
length = SHA1_DIGEST_SIZE;
text[0] = L"SHA1 hash";
} else if (CompareGuid(&Type, &EFI_CERT_SHA224_GUID) == 0) {
length = SHA224_DIGEST_LENGTH;
text[0] = L"SHA224 hash";
} else if (CompareGuid(&Type, &EFI_CERT_SHA256_GUID) == 0) {
length = SHA256_DIGEST_SIZE;
text[0] = L"SHA256 hash";
} else if (CompareGuid(&Type, &EFI_CERT_SHA384_GUID) == 0) {
length = SHA384_DIGEST_LENGTH;
text[0] = L"SHA384 hash";
} else if (CompareGuid(&Type, &EFI_CERT_SHA512_GUID) == 0) {
length = SHA512_DIGEST_LENGTH;
text[0] = L"SHA512 hash";
} else {
return;
}
ZeroMem(&hash_string1, sizeof(hash_string1));
ZeroMem(&hash_string2, sizeof(hash_string2));
text[1] = L"";
for (i = 0; i < length / 2; i++)
CatPrint(&hash_string1, L"%02x ", hash[i]);
for (i = length / 2; i < length; i++)
CatPrint(&hash_string2, L"%02x ", hash[i]);
text[2] = hash_string1.str;
text[3] = hash_string2.str;
text[4] = NULL;
console_print_box(text, -1);
if (hash_string1.str)
FreePool(hash_string1.str);
if (hash_string2.str)
FreePool(hash_string2.str);
}
static void show_efi_hash(EFI_GUID Type, void *Mok, UINTN MokSize)
{
UINTN sig_size;
UINTN hash_num;
UINT8 *hash;
CHAR16 **menu_strings;
CHAR16 *selection[] = { L"[Hash List]", NULL };
UINTN key_num = 0;
UINTN i;
sig_size = sha_size(Type) + sizeof(EFI_GUID);
if ((MokSize % sig_size) != 0) {
console_errorbox(L"Corrupted Hash List");
return;
}
hash_num = MokSize / sig_size;
if (hash_num == 1) {
hash = (UINT8 *) Mok + sizeof(EFI_GUID);
show_sha_digest(Type, hash);
return;
}
menu_strings = AllocateZeroPool(sizeof(CHAR16 *) * (hash_num + 2));
if (!menu_strings) {
console_errorbox(L"Out of Resources");
return;
}
for (i = 0; i < hash_num; i++) {
menu_strings[i] = PoolPrint(L"View hash %d", i);
}
menu_strings[i] = StrDuplicate(L"Back");
menu_strings[i + 1] = NULL;
while (key_num < hash_num) {
int rc;
key_num = rc = console_select(selection, menu_strings, key_num);
if (rc < 0 || key_num >= hash_num)
break;
hash = (UINT8 *) Mok + sig_size * key_num + sizeof(EFI_GUID);
show_sha_digest(Type, hash);
}
for (i = 0; menu_strings[i] != NULL; i++)
FreePool(menu_strings[i]);
FreePool(menu_strings);
}
static void show_mok_info(EFI_GUID Type, void *Mok, UINTN MokSize)
{
EFI_STATUS efi_status;
if (!Mok || MokSize == 0)
return;
if (CompareGuid (&Type, &X509_GUID) == 0) {
UINT8 hash[SHA1_DIGEST_SIZE];
X509 *X509Cert;
efi_status = get_sha1sum(Mok, MokSize, hash);
if (EFI_ERROR(efi_status)) {
console_notify(L"Failed to compute MOK fingerprint");
return;
}
if (X509ConstructCertificate(Mok, MokSize,
(UINT8 **) & X509Cert)
&& X509Cert != NULL) {
show_x509_info(X509Cert, hash);
X509_free(X509Cert);
} else {
console_notify(L"Not a valid X509 certificate");
return;
}
} else if (is_sha2_hash(Type)) {
show_efi_hash(Type, Mok, MokSize);
}
}
static EFI_STATUS list_keys(void *KeyList, UINTN KeyListSize, CHAR16 * title)
{
UINTN MokNum = 0;
MokListNode *keys = NULL;
UINT32 key_num = 0;
CHAR16 **menu_strings;
CHAR16 *selection[] = { title, NULL };
unsigned int i;
if (KeyListSize < (sizeof(EFI_SIGNATURE_LIST) +
sizeof(EFI_SIGNATURE_DATA))) {
console_notify(L"No MOK keys found");
return EFI_NOT_FOUND;
}
MokNum = count_keys(KeyList, KeyListSize);
if (MokNum == 0) {
console_errorbox(L"Invalid key list");
return EFI_ABORTED;
}
keys = build_mok_list(MokNum, KeyList, KeyListSize);
if (!keys) {
console_errorbox(L"Failed to construct key list");
return EFI_ABORTED;
}
menu_strings = AllocateZeroPool(sizeof(CHAR16 *) * (MokNum + 2));
if (!menu_strings)
return EFI_OUT_OF_RESOURCES;
for (i = 0; i < MokNum; i++) {
menu_strings[i] = PoolPrint(L"View key %d", i);
}
menu_strings[i] = StrDuplicate(L"Continue");
menu_strings[i + 1] = NULL;
while (key_num < MokNum) {
int rc;
rc = key_num = console_select(selection, menu_strings, key_num);
if (rc < 0 || key_num >= MokNum)
break;
show_mok_info(keys[key_num].Type, keys[key_num].Mok,
keys[key_num].MokSize);
}
for (i = 0; menu_strings[i] != NULL; i++)
FreePool(menu_strings[i]);
FreePool(menu_strings);
FreePool(keys);
return EFI_SUCCESS;
}
static EFI_STATUS get_line(UINT32 * length, CHAR16 * line, UINT32 line_max,
UINT8 show)
{
EFI_INPUT_KEY key;
EFI_STATUS efi_status;
unsigned int count = 0;
do {
efi_status = console_get_keystroke(&key);
if (EFI_ERROR(efi_status)) {
console_error(L"Failed to read the keystroke",
efi_status);
*length = 0;
return efi_status;
}
if ((count >= line_max &&
key.UnicodeChar != CHAR_BACKSPACE) ||
key.UnicodeChar == CHAR_NULL ||
key.UnicodeChar == CHAR_TAB ||
key.UnicodeChar == CHAR_LINEFEED ||
key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
continue;
}
if (count == 0 && key.UnicodeChar == CHAR_BACKSPACE) {
continue;
} else if (key.UnicodeChar == CHAR_BACKSPACE) {
if (show) {
console_print(L"\b");
}
line[--count] = '\0';
continue;
}
if (show) {
console_print(L"%c", key.UnicodeChar);
}
line[count++] = key.UnicodeChar;
} while (key.UnicodeChar != CHAR_CARRIAGE_RETURN);
console_print(L"\n");
*length = count;
return EFI_SUCCESS;
}
static EFI_STATUS compute_pw_hash(void *Data, UINTN DataSize, UINT8 * password,
UINT32 pw_length, UINT8 * hash)
{
EFI_STATUS efi_status;
unsigned int ctxsize;
void *ctx = NULL;
ctxsize = Sha256GetContextSize();
ctx = AllocatePool(ctxsize);
if (!ctx) {
console_notify(L"Unable to allocate memory for hash context");
return EFI_OUT_OF_RESOURCES;
}
if (!Sha256Init(ctx)) {
console_notify(L"Unable to initialise hash");
efi_status = EFI_OUT_OF_RESOURCES;
goto done;
}
if (Data && DataSize) {
if (!(Sha256Update(ctx, Data, DataSize))) {
console_notify(L"Unable to generate hash");
efi_status = EFI_OUT_OF_RESOURCES;
goto done;
}
}
if (!(Sha256Update(ctx, password, pw_length))) {
console_notify(L"Unable to generate hash");
efi_status = EFI_OUT_OF_RESOURCES;
goto done;
}
if (!(Sha256Final(ctx, hash))) {
console_notify(L"Unable to finalise hash");
efi_status = EFI_OUT_OF_RESOURCES;
goto done;
}
efi_status = EFI_SUCCESS;
done:
return efi_status;
}
static INTN reset_system()
{
RT->ResetSystem(EfiResetWarm, EFI_SUCCESS, 0, NULL);
console_notify(L"Failed to reboot\n");
return -1;
}
static UINT32 get_password(CHAR16 * prompt, CHAR16 * password, UINT32 max)
{
SIMPLE_TEXT_OUTPUT_MODE SavedMode;
CHAR16 *str;
CHAR16 *message[2];
UINTN length;
UINT32 pw_length;
if (!prompt)
prompt = L"Password:";
console_save_and_set_mode(&SavedMode);
str = PoolPrint(L"%s ", prompt);
if (!str) {
console_errorbox(L"Failed to allocate prompt");
return 0;
}
message[0] = str;
message[1] = NULL;
length = StrLen(message[0]);
console_print_box_at(message, -1, -length - 4, -5, length + 4, 3, 0, 1);
get_line(&pw_length, password, max, 0);
console_restore_mode(&SavedMode);
FreePool(str);
return pw_length;
}
static EFI_STATUS match_password(PASSWORD_CRYPT * pw_crypt,
void *Data, UINTN DataSize,
UINT8 * auth, CHAR16 * prompt)
{
EFI_STATUS efi_status;
UINT8 hash[128];
UINT8 *auth_hash;
UINT32 auth_size;
CHAR16 password[PASSWORD_MAX];
UINT32 pw_length;
UINT8 fail_count = 0;
unsigned int i;
if (pw_crypt) {
auth_hash = pw_crypt->hash;
auth_size = get_hash_size(pw_crypt->method);
if (auth_size == 0)
return EFI_INVALID_PARAMETER;
} else if (auth) {
auth_hash = auth;
auth_size = SHA256_DIGEST_SIZE;
} else {
return EFI_INVALID_PARAMETER;
}
while (fail_count < 3) {
pw_length = get_password(prompt, password, PASSWORD_MAX);
if (pw_length < PASSWORD_MIN || pw_length > PASSWORD_MAX) {
console_errorbox(L"Invalid password length");
fail_count++;
continue;
}
/*
* Compute password hash
*/
if (pw_crypt) {
char pw_ascii[PASSWORD_MAX + 1];
for (i = 0; i < pw_length; i++)
pw_ascii[i] = (char)password[i];
pw_ascii[pw_length] = '\0';
efi_status = password_crypt(pw_ascii, pw_length,
pw_crypt, hash);
} else {
/*
* For backward compatibility
*/
efi_status = compute_pw_hash(Data, DataSize,
(UINT8 *) password,
pw_length * sizeof(CHAR16),
hash);
}
if (EFI_ERROR(efi_status)) {
console_errorbox(L"Unable to generate password hash");
fail_count++;
continue;
}
if (CompareMem(auth_hash, hash, auth_size) != 0) {
console_errorbox(L"Password doesn't match");
fail_count++;
continue;
}
break;
}
if (fail_count >= 3)
return EFI_ACCESS_DENIED;
return EFI_SUCCESS;
}
static EFI_STATUS write_db(CHAR16 * db_name, void *MokNew, UINTN MokNewSize)
{
EFI_STATUS efi_status;
UINT32 attributes;
void *old_data = NULL;
void *new_data = NULL;
UINTN old_size;
UINTN new_size;
/* Do not use EFI_VARIABLE_APPEND_WRITE due to faulty firmwares.
* ref: https://github.com/rhboot/shim/issues/55
* https://github.com/rhboot/shim/issues/105 */
efi_status = get_variable_attr(db_name, (UINT8 **)&old_data, &old_size,
SHIM_LOCK_GUID, &attributes);
if (EFI_ERROR(efi_status) && efi_status != EFI_NOT_FOUND) {
return efi_status;
}
/* Check if the old db is compromised or not */
if (attributes & EFI_VARIABLE_RUNTIME_ACCESS) {
FreePool(old_data);
old_data = NULL;
old_size = 0;
}
new_size = old_size + MokNewSize;
new_data = AllocatePool(new_size);
if (new_data == NULL) {
efi_status = EFI_OUT_OF_RESOURCES;
goto out;
}
CopyMem(new_data, old_data, old_size);
CopyMem(new_data + old_size, MokNew, MokNewSize);
efi_status = RT->SetVariable(db_name, &SHIM_LOCK_GUID,
EFI_VARIABLE_NON_VOLATILE |
EFI_VARIABLE_BOOTSERVICE_ACCESS,
new_size, new_data);
out:
if (old_size > 0) {
FreePool(old_data);
}
if (new_data != NULL) {
FreePool(new_data);
}
return efi_status;
}
static EFI_STATUS store_keys(void *MokNew, UINTN MokNewSize, int authenticate,
BOOLEAN MokX)
{
EFI_STATUS efi_status;
CHAR16 *db_name;
CHAR16 *auth_name;
UINT8 auth[PASSWORD_CRYPT_SIZE];
UINTN auth_size = PASSWORD_CRYPT_SIZE;
UINT32 attributes;
if (MokX) {
db_name = L"MokListX";
auth_name = L"MokXAuth";
} else {
db_name = L"MokList";
auth_name = L"MokAuth";
}
if (authenticate) {
efi_status = RT->GetVariable(auth_name, &SHIM_LOCK_GUID,
&attributes, &auth_size, auth);
if (EFI_ERROR(efi_status) ||
(auth_size != SHA256_DIGEST_SIZE &&
auth_size != PASSWORD_CRYPT_SIZE)) {
if (MokX)
console_error(L"Failed to get MokXAuth",
efi_status);
else
console_error(L"Failed to get MokAuth",
efi_status);
return efi_status;
}
if (auth_size == PASSWORD_CRYPT_SIZE) {
efi_status = match_password((PASSWORD_CRYPT *) auth,
NULL, 0, NULL, NULL);
} else {
efi_status = match_password(NULL, MokNew, MokNewSize,
auth, NULL);
}
if (EFI_ERROR(efi_status))
return EFI_ACCESS_DENIED;
}
if (!MokNewSize) {
/* Delete MOK */
efi_status = RT->SetVariable(db_name, &SHIM_LOCK_GUID,
EFI_VARIABLE_NON_VOLATILE |
EFI_VARIABLE_BOOTSERVICE_ACCESS,
0, NULL);
} else {
/* Write new MOK */
efi_status = write_db(db_name, MokNew, MokNewSize);
}
if (EFI_ERROR(efi_status)) {
console_error(L"Failed to set variable", efi_status);
return efi_status;
}
return EFI_SUCCESS;
}
static EFI_STATUS mok_enrollment_prompt(void *MokNew, UINTN MokNewSize,
int auth, BOOLEAN MokX)
{
EFI_STATUS efi_status;
CHAR16 *enroll_p[] = { L"Enroll the key(s)?", NULL };
CHAR16 *title;
if (MokX)
title = L"[Enroll MOKX]";
else
title = L"[Enroll MOK]";
efi_status = list_keys(MokNew, MokNewSize, title);
if (EFI_ERROR(efi_status))
return efi_status;
if (console_yes_no(enroll_p) == 0)
return EFI_ABORTED;
efi_status = store_keys(MokNew, MokNewSize, auth, MokX);
if (EFI_ERROR(efi_status)) {
console_notify(L"Failed to enroll keys\n");
return efi_status;
}
if (auth) {
if (MokX) {
LibDeleteVariable(L"MokXNew", &SHIM_LOCK_GUID);
LibDeleteVariable(L"MokXAuth", &SHIM_LOCK_GUID);