File: grub-protect.c

package info (click to toggle)
grub2 2.14-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,540 kB
  • sloc: ansic: 550,889; asm: 68,074; sh: 9,818; cpp: 2,095; makefile: 1,916; python: 1,546; sed: 450; lex: 393; yacc: 268; awk: 85; lisp: 54; perl: 31
file content (1638 lines) | stat: -rw-r--r-- 42,671 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2022 Microsoft Corporation
 *  Copyright (C) 2023 SUSE LLC
 *  Copyright (C) 2024 Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <errno.h>
#include <fcntl.h>
#include <libtasn1.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <grub/emu/hostdisk.h>
#include <grub/emu/misc.h>

#include <grub/util/misc.h>

#include <tss2_buffer.h>
#include <tss2_mu.h>
#include <tcg2.h>
#include <tpm2_args.h>
#include <tpm2.h>

#pragma GCC diagnostic ignored "-Wmissing-prototypes"
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#include <argp.h>
#pragma GCC diagnostic error "-Wmissing-prototypes"
#pragma GCC diagnostic error "-Wmissing-declarations"

#include "progname.h"

/* Unprintable option keys for argp */
typedef enum protect_opt
{
  /* General */
  PROTECT_OPT_ACTION      = 'a',
  PROTECT_OPT_PROTECTOR   = 'p',
  /* TPM2 */
  PROTECT_OPT_TPM2_DEVICE = 0x100,
  PROTECT_OPT_TPM2_PCRS,
  PROTECT_OPT_TPM2_ASYMMETRIC,
  PROTECT_OPT_TPM2_BANK,
  PROTECT_OPT_TPM2_SRK,
  PROTECT_OPT_TPM2_KEYFILE,
  PROTECT_OPT_TPM2_OUTFILE,
  PROTECT_OPT_TPM2_EVICT,
  PROTECT_OPT_TPM2_TPM2KEY,
  PROTECT_OPT_TPM2_NVINDEX,
} protect_opt_t;

/* Option flags to keep track of specified arguments */
typedef enum protect_arg
{
  /* General */
  PROTECT_ARG_ACTION          = 1 << 0,
  PROTECT_ARG_PROTECTOR       = 1 << 1,
  /* TPM2 */
  PROTECT_ARG_TPM2_DEVICE     = 1 << 2,
  PROTECT_ARG_TPM2_PCRS       = 1 << 3,
  PROTECT_ARG_TPM2_ASYMMETRIC = 1 << 4,
  PROTECT_ARG_TPM2_BANK       = 1 << 5,
  PROTECT_ARG_TPM2_SRK        = 1 << 6,
  PROTECT_ARG_TPM2_KEYFILE    = 1 << 7,
  PROTECT_ARG_TPM2_OUTFILE    = 1 << 8,
  PROTECT_ARG_TPM2_EVICT      = 1 << 9,
  PROTECT_ARG_TPM2_TPM2KEY    = 1 << 10,
  PROTECT_ARG_TPM2_NVINDEX    = 1 << 11
} protect_arg_t;

typedef enum protect_protector
{
  PROTECT_TYPE_ERROR,
  PROTECT_TYPE_TPM2
} protect_protector_t;

typedef enum protect_action
{
  PROTECT_ACTION_ERROR,
  PROTECT_ACTION_ADD,
  PROTECT_ACTION_REMOVE
} protect_action_t;

typedef struct protect_args
{
  protect_arg_t args;
  protect_action_t action;
  protect_protector_t protector;

  const char *tpm2_device;
  grub_uint8_t tpm2_pcrs[TPM_MAX_PCRS];
  grub_uint8_t tpm2_pcr_count;
  grub_srk_type_t srk_type;
  TPM_ALG_ID_t tpm2_bank;
  TPM_HANDLE_t tpm2_srk;
  const char *tpm2_keyfile;
  const char *tpm2_outfile;
  bool tpm2_evict;
  bool tpm2_tpm2key;
  TPM_HANDLE_t tpm2_nvindex;
} protect_args_t;

static struct argp_option protect_options[] =
  {
    /* Top-level options */
   {
      .name  = "action",
      .key   = 'a',
      .arg   = "add|remove",
      .flags = 0,
      .doc   =
	N_("Add or remove a key protector to or from a key."),
      .group = 0
    },
    {
      .name  = "protector",
      .key   = 'p',
      .arg   = "tpm2",
      .flags = 0,
      .doc   =
	N_("Set key protector to use (only tpm2 is currently supported)."),
      .group = 0
    },
    /* TPM2 key protector options */
    {
      .name = "tpm2-device",
      .key   = PROTECT_OPT_TPM2_DEVICE,
      .arg   = "FILE",
      .flags = 0,
      .doc   =
	N_("Set the path to the TPM2 device. (default: /dev/tpm0)"),
      .group = 0
    },
    {
      .name = "tpm2-pcrs",
      .key   = PROTECT_OPT_TPM2_PCRS,
      .arg   = "0[,1]...",
      .flags = 0,
      .doc   =
	N_("Set a comma-separated list of PCRs used to authorize key release "
	   "e.g., '7,11'. Please be aware that PCR 0~7 are used by the "
	   "firmware and the measurement result may change after a "
	   "firmware update (for baremetal systems) or a package "
	   "(OVMF/SLOF) update in the VM host. This may lead to "
	   "the failure of key unsealing. (default: 7)"),
      .group = 0
    },
    {
      .name = "tpm2-bank",
      .key  = PROTECT_OPT_TPM2_BANK,
      .arg   = "ALG",
      .flags = 0,
      .doc   =
	N_("Set the bank of PCRs used to authorize key release: "
	   "SHA1, SHA256, SHA384, or SHA512. (default: SHA256)"),
      .group = 0
    },
    {
      .name = "tpm2-keyfile",
      .key   = PROTECT_OPT_TPM2_KEYFILE,
      .arg   = "FILE",
      .flags = 0,
      .doc   =
	N_("Set the path to a file that contains the cleartext key to protect."),
      .group = 0
    },
    {
      .name = "tpm2-outfile",
      .key   = PROTECT_OPT_TPM2_OUTFILE,
      .arg   = "FILE",
      .flags = 0,
      .doc   =
	N_("Set the path to the file that will contain the key after sealing "
	   "(must be accessible to GRUB during boot)."),
      .group = 0
    },
    {
      .name = "tpm2-srk",
      .key   = PROTECT_OPT_TPM2_SRK,
      .arg   = "NUM",
      .flags = 0,
      .doc   =
	N_("Set the SRK handle if the SRK is to be made persistent."),
      .group = 0
    },
    {
      .name = "tpm2-asymmetric",
      .key   = PROTECT_OPT_TPM2_ASYMMETRIC,
      .arg   = "TYPE",
      .flags = 0,
      .doc   =
	N_("Set the type of SRK: RSA (RSA2048) and ECC (ECC_NIST_P256)."
	   "(default: ECC)"),
      .group = 0
    },
    {
      .name = "tpm2-evict",
      .key   = PROTECT_OPT_TPM2_EVICT,
      .arg   = NULL,
      .flags = 0,
      .doc   =
	N_("Evict a previously persisted SRK from the TPM, if any."),
      .group = 0
    },
    {
      .name = "tpm2key",
      .key   = PROTECT_OPT_TPM2_TPM2KEY,
      .arg   = NULL,
      .flags = 0,
      .doc   =
	N_("Use TPM 2.0 Key File format."),
      .group = 0
    },
    {
      .name = "tpm2-nvindex",
      .key   = PROTECT_OPT_TPM2_NVINDEX,
      .arg   = "NUM",
      .flags = 0,
      .doc   =
	N_("Store the sealed key in a persistent or NV index handle."),
      .group = 0
    },
    /* End of list */
    { 0, 0, 0, 0, 0, 0 }
  };

static int protector_tpm2_fd = -1;

static grub_err_t
protect_read_file (const char *filepath, void **buffer, size_t *buffer_size)
{
  grub_err_t err;
  FILE *f;
  long len;
  void *buf;

  f = fopen (filepath, "rb");
  if (f == NULL)
    {
      fprintf (stderr, N_("Could not open file: %s\n"), filepath);
      return GRUB_ERR_FILE_NOT_FOUND;
    }

  if (fseek (f, 0, SEEK_END))
    {
      fprintf (stderr, N_("Could not seek file: %s\n"), filepath);
      err = GRUB_ERR_FILE_READ_ERROR;
      goto exit1;
    }

  len = ftell (f);
  if (len <= 0)
    {
      fprintf (stderr, N_("Could not get file length: %s\n"), filepath);
      err = GRUB_ERR_FILE_READ_ERROR;
      goto exit1;
    }

  rewind (f);

  buf = grub_malloc (len);
  if (buf == NULL)
    {
      fprintf (stderr, N_("Could not allocate memory for file: %s\n"), filepath);
      err = GRUB_ERR_OUT_OF_MEMORY;
      goto exit1;
    }

  if (fread (buf, len, 1, f) != 1)
    {
      fprintf (stderr, N_("Could not read file: %s\n"), filepath);
      err = GRUB_ERR_FILE_READ_ERROR;
      goto exit2;
    }

  *buffer = buf;
  *buffer_size = len;

  buf = NULL;
  err = GRUB_ERR_NONE;

 exit2:
  grub_free (buf);

 exit1:
  fclose (f);

  return err;
}

static grub_err_t
protect_write_file (const char *filepath, void *buffer, size_t buffer_size)
{
  grub_err_t err;
  FILE *f;

  f = fopen (filepath, "wb");
  if (f == NULL)
    return GRUB_ERR_FILE_NOT_FOUND;

  if (fwrite (buffer, buffer_size, 1, f) != 1)
  {
    err = GRUB_ERR_WRITE_ERROR;
    goto exit;
  }

  err = GRUB_ERR_NONE;

 exit:
  fclose (f);

  return err;
}

grub_err_t
grub_tcg2_get_max_output_size (grub_size_t *size)
{
  if (size == NULL)
    return GRUB_ERR_BAD_ARGUMENT;

  *size = GRUB_TPM2_BUFFER_CAPACITY;

  return GRUB_ERR_NONE;
}

grub_err_t
grub_tcg2_submit_command (grub_size_t input_size, grub_uint8_t *input,
			  grub_size_t output_size, grub_uint8_t *output)
{
  if (write (protector_tpm2_fd, input, input_size) != input_size)
    {
      fprintf (stderr, N_("Could not send TPM command.\n"));
      return GRUB_ERR_BAD_DEVICE;
    }

  if (read (protector_tpm2_fd, output, output_size) < sizeof (TPM_RESPONSE_HEADER_t))
    {
      fprintf (stderr, N_("Could not get TPM response.\n"));
      return GRUB_ERR_BAD_DEVICE;
    }

  return GRUB_ERR_NONE;
}

static grub_err_t
protect_tpm2_open_device (const char *dev_node)
{
  if (protector_tpm2_fd != -1)
    return GRUB_ERR_NONE;

  protector_tpm2_fd = open (dev_node, O_RDWR);
  if (protector_tpm2_fd == -1)
    {
      fprintf (stderr, N_("Could not open TPM device (%s).\n"), strerror (errno));
      return GRUB_ERR_FILE_NOT_FOUND;
    }

  return GRUB_ERR_NONE;
}

static grub_err_t
protect_tpm2_close_device (void)
{
  int err;

  if (protector_tpm2_fd == -1)
    return GRUB_ERR_NONE;

  err = close (protector_tpm2_fd);
  if (err != GRUB_ERR_NONE)
  {
    fprintf (stderr, N_("Could not close TPM device (%s).\n"), strerror (errno));
    return GRUB_ERR_IO;
  }

  protector_tpm2_fd = -1;
  return GRUB_ERR_NONE;
}

static grub_err_t
protect_tpm2_get_policy_digest (protect_args_t *args, TPM2B_DIGEST_t *digest)
{
  TPM_RC_t rc;
  TPML_PCR_SELECTION_t pcr_sel = {
    .count = 1,
    .pcrSelections = {
      {
	.hash = args->tpm2_bank,
	.sizeOfSelect = 3,
	.pcrSelect = {0}
      },
    }
  };
  TPML_PCR_SELECTION_t pcr_sel_out = {0};
  TPML_DIGEST_t pcr_values = {0};
  TPM2B_DIGEST_t pcr_digest = {0};
  grub_size_t pcr_digest_len;
  TPM2B_MAX_BUFFER_t pcr_concat = {0};
  grub_size_t pcr_concat_len;
  grub_uint8_t *pcr_cursor;
  TPM2B_NONCE_t nonce = {0};
  TPM2B_ENCRYPTED_SECRET_t salt = {0};
  TPMT_SYM_DEF_t symmetric = {0};
  TPMI_SH_AUTH_SESSION_t session = 0;
  TPM2B_DIGEST_t policy_digest = {0};
  grub_uint8_t i;
  grub_err_t err;

  /* PCR Read */
  for (i = 0; i < args->tpm2_pcr_count; i++)
    TPMS_PCR_SELECTION_SelectPCR (&pcr_sel.pcrSelections[0], args->tpm2_pcrs[i]);

  rc = grub_tpm2_pcr_read (NULL, &pcr_sel, NULL, &pcr_sel_out, &pcr_values, NULL);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Failed to read PCRs (TPM2_PCR_Read: 0x%x).\n", rc);
      return GRUB_ERR_BAD_DEVICE;
    }

  if ((pcr_sel_out.count != pcr_sel.count) ||
       (pcr_sel.pcrSelections[0].sizeOfSelect !=
	pcr_sel_out.pcrSelections[0].sizeOfSelect))
    {
      fprintf (stderr, N_("Could not read all the specified PCRs.\n"));
      return GRUB_ERR_BAD_DEVICE;
    }

  /* Compute PCR Digest */
  switch (args->tpm2_bank)
    {
    case TPM_ALG_SHA1:
      pcr_digest_len = TPM_SHA1_DIGEST_SIZE;
      break;
    case TPM_ALG_SHA256:
      pcr_digest_len = TPM_SHA256_DIGEST_SIZE;
      break;
    case TPM_ALG_SHA384:
      pcr_digest_len = TPM_SHA384_DIGEST_SIZE;
      break;
    case TPM_ALG_SHA512:
      pcr_digest_len = TPM_SHA512_DIGEST_SIZE;
      break;
    default:
      return GRUB_ERR_BAD_ARGUMENT;
    }

  pcr_concat_len = pcr_digest_len * args->tpm2_pcr_count;
  if (pcr_concat_len > TPM_MAX_DIGEST_BUFFER)
    {
      fprintf (stderr, N_("PCR concatenation buffer not big enough.\n"));
      return GRUB_ERR_OUT_OF_RANGE;
    }

  pcr_cursor = pcr_concat.buffer;
  for (i = 0; i < args->tpm2_pcr_count; i++)
    {
      if (pcr_values.digests[i].size != pcr_digest_len)
	{
	  fprintf (stderr,
		   N_("Bad PCR value size: expected %llu bytes but got %u bytes.\n"),
		   (long long unsigned int)pcr_digest_len, pcr_values.digests[i].size);
	  return GRUB_ERR_BAD_ARGUMENT;
	}

      grub_memcpy (pcr_cursor, pcr_values.digests[i].buffer, pcr_digest_len);
      pcr_cursor += pcr_digest_len;
    }
  pcr_concat.size = pcr_concat_len;

  rc = grub_tpm2_hash (NULL, &pcr_concat, TPM_ALG_SHA256, TPM_RH_NULL, &pcr_digest, NULL, NULL);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Failed to generate PCR digest (TPM2_Hash: 0x%x)\n", rc);
      return GRUB_ERR_BAD_DEVICE;
    }

  /* Start Trial Session */
  nonce.size = TPM_SHA256_DIGEST_SIZE;
  symmetric.algorithm = TPM_ALG_NULL;

  rc = grub_tpm2_startauthsession (TPM_RH_NULL, TPM_RH_NULL, 0, &nonce, &salt,
				   TPM_SE_TRIAL, &symmetric, TPM_ALG_SHA256,
				   &session, NULL, 0);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Failed to start trial policy session (TPM2_StartAuthSession: 0x%x).\n", rc);
      return GRUB_ERR_BAD_DEVICE;
    }

  /* PCR Policy */
  rc = grub_tpm2_policypcr (session, NULL, &pcr_digest, &pcr_sel, NULL);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Failed to submit PCR policy (TPM2_PolicyPCR: 0x%x).\n", rc);
      err = GRUB_ERR_BAD_DEVICE;
      goto error;
    }

  /* Retrieve Policy Digest */
  rc = grub_tpm2_policygetdigest (session, NULL, &policy_digest, NULL);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Failed to get policy digest (TPM2_PolicyGetDigest: 0x%x).\n", rc);
      err = GRUB_ERR_BAD_DEVICE;
      goto error;
    }

  /* Epilogue */
  *digest = policy_digest;
  err = GRUB_ERR_NONE;

 error:
  grub_tpm2_flushcontext (session);

  return err;
}

static grub_err_t
protect_tpm2_get_srk (protect_args_t *args, TPM_HANDLE_t *srk)
{
  TPM_RC_t rc;
  TPM2B_PUBLIC_t public;
  TPMS_AUTH_COMMAND_t authCommand = {0};
  TPM2B_SENSITIVE_CREATE_t inSensitive = {0};
  TPM2B_PUBLIC_t inPublic = {0};
  TPM2B_DATA_t outsideInfo = {0};
  TPML_PCR_SELECTION_t creationPcr = {0};
  TPM2B_PUBLIC_t outPublic = {0};
  TPM2B_CREATION_DATA_t creationData = {0};
  TPM2B_DIGEST_t creationHash = {0};
  TPMT_TK_CREATION_t creationTicket = {0};
  TPM2B_NAME_t srkName = {0};
  TPM_HANDLE_t srkHandle;

  if (args->tpm2_srk != 0)
    {
      /* Find SRK */
      rc = grub_tpm2_readpublic (args->tpm2_srk, NULL, &public);
      if (rc == TPM_RC_SUCCESS)
	{
	  printf ("Read SRK from 0x%x\n", args->tpm2_srk);
	  *srk = args->tpm2_srk;
	  return GRUB_ERR_NONE;
	}

      /* The handle exists but its public area could not be read. */
      if ((rc & ~TPM_RC_N_MASK) != TPM_RC_HANDLE)
	{
	  fprintf (stderr, "Failed to retrieve SRK from 0x%x (TPM2_ReadPublic: 0x%x).\n", args->tpm2_srk, rc);
	  return GRUB_ERR_BAD_DEVICE;
	}
    }

  /* Create SRK */
  authCommand.sessionHandle = TPM_RS_PW;
  inPublic.publicArea.type = args->srk_type.type;
  inPublic.publicArea.nameAlg = TPM_ALG_SHA256;
  inPublic.publicArea.objectAttributes.restricted = 1;
  inPublic.publicArea.objectAttributes.userWithAuth = 1;
  inPublic.publicArea.objectAttributes.decrypt = 1;
  inPublic.publicArea.objectAttributes.fixedTPM = 1;
  inPublic.publicArea.objectAttributes.fixedParent = 1;
  inPublic.publicArea.objectAttributes.sensitiveDataOrigin = 1;
  inPublic.publicArea.objectAttributes.noDA = 1;

  switch (args->srk_type.type)
    {
    case TPM_ALG_RSA:
      inPublic.publicArea.parameters.rsaDetail.symmetric.algorithm = TPM_ALG_AES;
      inPublic.publicArea.parameters.rsaDetail.symmetric.keyBits.aes = 128;
      inPublic.publicArea.parameters.rsaDetail.symmetric.mode.aes = TPM_ALG_CFB;
      inPublic.publicArea.parameters.rsaDetail.scheme.scheme = TPM_ALG_NULL;
      inPublic.publicArea.parameters.rsaDetail.keyBits = args->srk_type.detail.rsa_bits;
      inPublic.publicArea.parameters.rsaDetail.exponent = 0;
      break;

    case TPM_ALG_ECC:
      inPublic.publicArea.parameters.eccDetail.symmetric.algorithm = TPM_ALG_AES;
      inPublic.publicArea.parameters.eccDetail.symmetric.keyBits.aes = 128;
      inPublic.publicArea.parameters.eccDetail.symmetric.mode.aes = TPM_ALG_CFB;
      inPublic.publicArea.parameters.eccDetail.scheme.scheme = TPM_ALG_NULL;
      inPublic.publicArea.parameters.eccDetail.curveID = args->srk_type.detail.ecc_curve;
      inPublic.publicArea.parameters.eccDetail.kdf.scheme = TPM_ALG_NULL;
      break;

    default:
      return GRUB_ERR_BAD_ARGUMENT;
    }

  rc = grub_tpm2_createprimary (TPM_RH_OWNER, &authCommand, &inSensitive, &inPublic,
			        &outsideInfo, &creationPcr, &srkHandle, &outPublic,
			        &creationData, &creationHash, &creationTicket,
			        &srkName, NULL);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Failed to create SRK (TPM2_CreatePrimary: 0x%x).\n", rc);
      return GRUB_ERR_BAD_DEVICE;
    }

  /* Persist SRK */
  if (args->tpm2_srk != 0)
    {
      rc = grub_tpm2_evictcontrol (TPM_RH_OWNER, srkHandle, &authCommand, args->tpm2_srk, NULL);
      if (rc == TPM_RC_SUCCESS)
	{
	  grub_tpm2_flushcontext (srkHandle);
	  srkHandle = args->tpm2_srk;
	}
      else
	fprintf (stderr,
		 "Warning: Failed to persist SRK (0x%x) (TPM2_EvictControl: 0x%x).\n"
		 "Continuing anyway...\n", args->tpm2_srk, rc);
    }

  /* Epilogue */
  *srk = srkHandle;

  return GRUB_ERR_NONE;
}

static grub_err_t
protect_tpm2_seal (TPM2B_DIGEST_t *policyDigest, TPM_HANDLE_t srk,
		   grub_uint8_t *clearText, grub_size_t clearTextLength,
		   tpm2_sealed_key_t *sealed_key)
{
  TPM_RC_t rc;
  TPMS_AUTH_COMMAND_t authCommand = {0};
  TPM2B_SENSITIVE_CREATE_t inSensitive = {0};
  TPM2B_PUBLIC_t inPublic  = {0};
  TPM2B_DATA_t outsideInfo = {0};
  TPML_PCR_SELECTION_t pcr_sel = {0};
  TPM2B_PRIVATE_t outPrivate = {0};
  TPM2B_PUBLIC_t outPublic = {0};

  /* Seal Data */
  authCommand.sessionHandle = TPM_RS_PW;

  inSensitive.sensitive.data.size = clearTextLength;
  memcpy(inSensitive.sensitive.data.buffer, clearText, clearTextLength);

  inPublic.publicArea.type = TPM_ALG_KEYEDHASH;
  inPublic.publicArea.nameAlg = TPM_ALG_SHA256;
  inPublic.publicArea.parameters.keyedHashDetail.scheme.scheme = TPM_ALG_NULL;
  inPublic.publicArea.authPolicy = *policyDigest;

  rc = grub_tpm2_create (srk, &authCommand, &inSensitive, &inPublic, &outsideInfo,
			 &pcr_sel, &outPrivate, &outPublic, NULL, NULL, NULL, NULL);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Failed to seal key (TPM2_Create: 0x%x).\n", rc);
      return GRUB_ERR_BAD_DEVICE;
    }

  /* Epilogue */
  sealed_key->public = outPublic;
  sealed_key->private = outPrivate;

  return GRUB_ERR_NONE;
}

extern asn1_static_node tpm2key_asn1_tab[];

/* id-sealedkey OID defined in TPM 2.0 Key Files Spec */
#define TPM2KEY_SEALED_KEY_OID "2.23.133.10.1.5"

static grub_err_t
protect_tpm2_export_tpm2key (const protect_args_t *args, tpm2_sealed_key_t *sealed_key,
			     void **der_buf, int *der_buf_size)
{
  const char *sealed_key_oid = TPM2KEY_SEALED_KEY_OID;
  asn1_node asn1_def = NULL;
  asn1_node tpm2key = NULL;
  grub_uint32_t parent;
  grub_uint32_t cmd_code;
  struct grub_tpm2_buffer pol_buf;
  TPML_PCR_SELECTION_t pcr_sel = {
    .count = 1,
    .pcrSelections = {
      {
	.hash = args->tpm2_bank,
	.sizeOfSelect = 3,
	.pcrSelect = {0}
      },
    }
  };
  struct grub_tpm2_buffer pub_buf;
  struct grub_tpm2_buffer priv_buf;
  int i;
  int ret;
  grub_err_t err = GRUB_ERR_NONE;

  if (der_buf == NULL)
    return GRUB_ERR_BAD_ARGUMENT;

  for (i = 0; i < args->tpm2_pcr_count; i++)
    TPMS_PCR_SELECTION_SelectPCR (&pcr_sel.pcrSelections[0], args->tpm2_pcrs[i]);

  /*
   * Prepare the parameters for TPM_CC_PolicyPCR:
   * empty pcrDigest and the user selected PCRs
   */
  grub_tpm2_buffer_init (&pol_buf);
  grub_tpm2_buffer_pack_u16 (&pol_buf, 0);
  grub_Tss2_MU_TPML_PCR_SELECTION_Marshal (&pol_buf, &pcr_sel);

  grub_tpm2_buffer_init (&pub_buf);
  grub_Tss2_MU_TPM2B_PUBLIC_Marshal (&pub_buf, &sealed_key->public);
  grub_tpm2_buffer_init (&priv_buf);
  grub_Tss2_MU_TPM2B_Marshal (&priv_buf, sealed_key->private.size,
			      sealed_key->private.buffer);
  if (pub_buf.error != 0 || priv_buf.error != 0)
    return GRUB_ERR_BAD_ARGUMENT;

  ret = asn1_array2tree (tpm2key_asn1_tab, &asn1_def, NULL);
  if (ret != ASN1_SUCCESS)
    return GRUB_ERR_BAD_ARGUMENT;

  ret = asn1_create_element (asn1_def, "TPM2KEY.TPMKey" , &tpm2key);
  if (ret != ASN1_SUCCESS)
    return GRUB_ERR_BAD_ARGUMENT;

  /* Set 'type' to "sealed key" */
  ret = asn1_write_value (tpm2key, "type", sealed_key_oid, 1);
  if (ret != ASN1_SUCCESS)
    {
      fprintf (stderr, "Failed to set 'type': 0x%u\n", ret);
      err = GRUB_ERR_BAD_ARGUMENT;
      goto error;
    }

  /* Set 'emptyAuth' to TRUE */
  ret = asn1_write_value (tpm2key, "emptyAuth", "TRUE", 1);
  if (ret != ASN1_SUCCESS)
    {
      fprintf (stderr, "Failed to set 'emptyAuth': 0x%x\n", ret);
      err = GRUB_ERR_BAD_ARGUMENT;
      goto error;
    }

  /* Set 'policy' */
  ret = asn1_write_value (tpm2key, "policy", "NEW", 1);
  if (ret != ASN1_SUCCESS)
    {
      fprintf (stderr, "Failed to set 'policy': 0x%x\n", ret);
      err = GRUB_ERR_BAD_ARGUMENT;
      goto error;
    }
  cmd_code = grub_cpu_to_be32 (TPM_CC_PolicyPCR);
  ret = asn1_write_value (tpm2key, "policy.?LAST.CommandCode", &cmd_code,
			  sizeof (cmd_code));
  if (ret != ASN1_SUCCESS)
    {
      fprintf (stderr, "Failed to set 'policy CommandCode': 0x%x\n", ret);
      err = GRUB_ERR_BAD_ARGUMENT;
      goto error;
    }
  ret = asn1_write_value (tpm2key, "policy.?LAST.CommandPolicy", &pol_buf.data,
			  pol_buf.size);
  if (ret != ASN1_SUCCESS)
    {
      fprintf (stderr, "Failed to set 'policy CommandPolicy': 0x%x\n", ret);
      err = GRUB_ERR_BAD_ARGUMENT;
      goto error;
    }

  /* Remove 'secret' */
  ret = asn1_write_value (tpm2key, "secret", NULL, 0);
  if (ret != ASN1_SUCCESS)
    {
      fprintf (stderr, "Failed to remove 'secret': 0x%x\n", ret);
      err = GRUB_ERR_BAD_ARGUMENT;
      goto error;
    }

  /* Remove 'authPolicy' */
  ret = asn1_write_value (tpm2key, "authPolicy", NULL, 0);
  if (ret != ASN1_SUCCESS)
    {
      fprintf (stderr, "Failed to remove 'authPolicy': 0x%x\n", ret);
      err = GRUB_ERR_BAD_ARGUMENT;
      goto error;
    }

  /* Remove 'description' */
  ret = asn1_write_value (tpm2key, "description", NULL, 0);
  if (ret != ASN1_SUCCESS)
    {
      fprintf (stderr, "Failed to remove 'description': 0x%x\n", ret);
      err = GRUB_ERR_BAD_ARGUMENT;
      goto error;
    }

  /*
   *  Use the SRK handle as the parent handle if specified
   *  Otherwise, Use TPM_RH_OWNER as the default parent handle
  */
  if (args->tpm2_srk != 0)
    parent = grub_cpu_to_be32 (args->tpm2_srk);
  else
    parent = grub_cpu_to_be32 (TPM_RH_OWNER);
  ret = asn1_write_value (tpm2key, "parent", &parent, sizeof (parent));
  if (ret != ASN1_SUCCESS)
    {
      fprintf (stderr, "Failed to set 'parent': 0x%x\n", ret);
      err = GRUB_ERR_BAD_ARGUMENT;
      goto error;
    }

  /*
   * Set 'rsaParent' to TRUE if the RSA SRK is specified and the SRK
   * handle is not persistent. Otherwise, remove 'rsaParent'.
   */
  if (args->tpm2_srk == 0 && args->srk_type.type == TPM_ALG_RSA)
    ret = asn1_write_value (tpm2key, "rsaParent", "TRUE", 1);
  else
    ret = asn1_write_value (tpm2key, "rsaParent", NULL, 0);

  if (ret != ASN1_SUCCESS)
    {
      fprintf (stderr, "Failed to set 'rsaParent': 0x%x\n", ret);
      err = GRUB_ERR_BAD_ARGUMENT;
      goto error;
    }

  /* Set the pubkey */
  ret = asn1_write_value (tpm2key, "pubkey", pub_buf.data, pub_buf.size);
  if (ret != ASN1_SUCCESS)
    {
      fprintf (stderr, "Failed to set 'pubkey': 0x%x\n", ret);
      err = GRUB_ERR_BAD_ARGUMENT;
      goto error;
    }

  /* Set the privkey */
  ret = asn1_write_value (tpm2key, "privkey", priv_buf.data, priv_buf.size);
  if (ret != ASN1_SUCCESS)
    {
      fprintf (stderr, "Failed to set 'privkey': 0x%x\n", ret);
      err = GRUB_ERR_BAD_ARGUMENT;
      goto error;
    }

  /* Create the DER binary */
  *der_buf_size = 0;
  ret = asn1_der_coding (tpm2key, "", NULL, der_buf_size, NULL);
  if (ret != ASN1_MEM_ERROR)
    {
      fprintf (stderr, "Failed to get DER size: 0x%x\n", ret);
      err = GRUB_ERR_BAD_ARGUMENT;
      goto error;
    }

  *der_buf = grub_malloc (*der_buf_size);
  if (*der_buf == NULL)
    {
      fprintf (stderr, "Failed to allocate memory for DER encoding\n");
      err = GRUB_ERR_OUT_OF_MEMORY;
      goto error;
    }

  ret = asn1_der_coding (tpm2key, "", *der_buf, der_buf_size, NULL);
  if (ret != ASN1_SUCCESS)
    {
      fprintf (stderr, "DER coding error: 0x%x\n", ret);
      err = GRUB_ERR_BAD_ARGUMENT;
      goto error;
    }

 error:
  if (tpm2key)
    asn1_delete_structure (&tpm2key);

  return err;
}

static grub_err_t
protect_tpm2_export_raw (tpm2_sealed_key_t *sealed_key, void **out_buf, int *out_buf_size)
{
  struct grub_tpm2_buffer buf;

  grub_tpm2_buffer_init (&buf);
  grub_Tss2_MU_TPM2B_PUBLIC_Marshal (&buf, &sealed_key->public);
  grub_Tss2_MU_TPM2B_Marshal (&buf, sealed_key->private.size,
			      sealed_key->private.buffer);
  if (buf.error != 0)
    return GRUB_ERR_BAD_ARGUMENT;

  *out_buf_size = buf.size;
  *out_buf = grub_malloc (buf.size);

  if (*out_buf == NULL)
    {
      fprintf (stderr, N_("Could not allocate memory for the raw format key.\n"));
      return GRUB_ERR_OUT_OF_MEMORY;
    }

  grub_memcpy (*out_buf, buf.data, buf.size);

  return GRUB_ERR_NONE;
}

static grub_err_t
protect_tpm2_export_persistent (protect_args_t *args,
				TPM_HANDLE_t srk_handle,
				tpm2_sealed_key_t *sealed_key)
{
  TPMS_AUTH_COMMAND_t authCmd = {0};
  TPM2B_NAME_t name = {0};
  TPM_HANDLE_t sealed_handle;
  TPM_RC_t rc;
  grub_err_t err = GRUB_ERR_NONE;

  /* Load the sealed key and associate it with the SRK */
  authCmd.sessionHandle = TPM_RS_PW;
  rc = grub_tpm2_load (srk_handle, &authCmd, &sealed_key->private, &sealed_key->public,
		       &sealed_handle, &name, NULL);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Failed to load sealed key (TPM2_Load: %x).\n", rc);
      return GRUB_ERR_BAD_DEVICE;
    }

  /* Make the sealed key object persistent */
  authCmd.sessionHandle = TPM_RS_PW;
  rc = grub_tpm2_evictcontrol (TPM_RH_OWNER, sealed_handle, &authCmd, args->tpm2_nvindex, NULL);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Failed to make sealed key persistent with handle 0x%x (TPM2_EvictControl: 0x%x).\n", args->tpm2_nvindex, rc);
      err = GRUB_ERR_BAD_DEVICE;
      goto exit;
    }

 exit:
  grub_tpm2_flushcontext (sealed_handle);

  return err;
}

static grub_err_t
protect_tpm2_export_nvindex (protect_args_t *args, void *data, int data_size)
{
  TPMS_AUTH_COMMAND_t authCmd = {0};
  TPM2B_NV_PUBLIC_t pub_info = {0};
  TPM2B_MAX_NV_BUFFER_t nv_data = {0};
  TPM_RC_t rc;

  if (data_size > TPM_MAX_NV_BUFFER_SIZE || data_size < 0)
    {
      fprintf (stderr, N_("Invalid tpm2key size for TPM NV buffer\n"));
      return GRUB_ERR_OUT_OF_RANGE;
    }

  pub_info.nvPublic.nvIndex = args->tpm2_nvindex;
  pub_info.nvPublic.nameAlg = TPM_ALG_SHA256;
  pub_info.nvPublic.attributes = TPMA_NV_OWNERWRITE | TPMA_NV_OWNERREAD;
  pub_info.nvPublic.dataSize = (grub_uint16_t) data_size;

  authCmd.sessionHandle = TPM_RS_PW;
  rc = grub_tpm2_nv_definespace (TPM_RH_OWNER, &authCmd, NULL, &pub_info);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Failed to define NV space for 0x%x (TPM2_NV_DefineSpace: 0x%x)\n", args->tpm2_nvindex, rc);
      return GRUB_ERR_BAD_DEVICE;
    }

  authCmd.sessionHandle = TPM_RS_PW;
  grub_memcpy (nv_data.buffer, data, data_size);
  nv_data.size = (grub_uint16_t) data_size;

  rc = grub_tpm2_nv_write (TPM_RH_OWNER, args->tpm2_nvindex, &authCmd, &nv_data, 0);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Failed to write data into 0x%x (TPM2_NV_Write: 0x%x)\n", args->tpm2_nvindex, rc);
      return GRUB_ERR_BAD_DEVICE;
    }

  return GRUB_ERR_NONE;
}

static grub_err_t
protect_tpm2_add (protect_args_t *args)
{
  grub_err_t err;
  grub_uint8_t *key = NULL;
  grub_size_t key_size;
  TPM_HANDLE_t srk;
  TPM2B_DIGEST_t policy_digest;
  void *out_buf = NULL;
  int out_buf_size;
  tpm2_sealed_key_t sealed_key;

  err = protect_tpm2_open_device (args->tpm2_device);
  if (err != GRUB_ERR_NONE)
    return err;

  err = protect_read_file (args->tpm2_keyfile, (void **)&key, &key_size);
  if (err != GRUB_ERR_NONE)
    goto exit1;

  if (key_size > TPM_MAX_SYM_DATA)
    {
      fprintf (stderr, N_("Input key size larger than %u bytes.\n"), TPM_MAX_SYM_DATA);
      err = GRUB_ERR_OUT_OF_RANGE;
      goto exit2;
    }

  err = protect_tpm2_get_srk (args, &srk);
  if (err != GRUB_ERR_NONE)
    goto exit2;

  err = protect_tpm2_get_policy_digest (args, &policy_digest);
  if (err != GRUB_ERR_NONE)
    goto exit3;

  err = protect_tpm2_seal (&policy_digest, srk, key, key_size, &sealed_key);
  if (err != GRUB_ERR_NONE)
    goto exit3;

  if (args->tpm2_tpm2key == true)
    {
      err = protect_tpm2_export_tpm2key (args, &sealed_key, &out_buf, &out_buf_size);
      if (err != GRUB_ERR_NONE)
	{
	  fprintf (stderr, N_("Could not export to TPM 2.0 Key File format\n"));
	  goto exit3;
	}
    }
  else
    {
      err = protect_tpm2_export_raw (&sealed_key, &out_buf, &out_buf_size);
      if (err != GRUB_ERR_NONE)
	{
	  fprintf (stderr, N_("Could not export to the raw format\n"));
	  goto exit3;
	}
    }

  if (args->tpm2_outfile != NULL)
    {
      err = protect_write_file (args->tpm2_outfile, out_buf, out_buf_size);
      if (err != GRUB_ERR_NONE)
	{
	  fprintf (stderr, N_("Could not write key file (%s).\n"), strerror (errno));
	  goto exit3;
	}
    }

  if (TPM_HT_IS_NVINDEX (args->tpm2_nvindex) == true)
    {
      err = protect_tpm2_export_nvindex (args, out_buf, out_buf_size);
      if (err != GRUB_ERR_NONE)
	goto exit3;
    }
  else if (TPM_HT_IS_PERSISTENT (args->tpm2_nvindex) == true)
    {
      err = protect_tpm2_export_persistent (args, srk, &sealed_key);
      if (err != GRUB_ERR_NONE)
	goto exit3;
    }

 exit3:
  grub_tpm2_flushcontext (srk);
  grub_free (out_buf);

 exit2:
  grub_free (key);

 exit1:
  protect_tpm2_close_device ();

  return err;
}

static grub_err_t
protect_tpm2_evict (TPM_HANDLE_t handle)
{
  TPM_RC_t rc;
  TPM2B_PUBLIC_t public;
  TPMS_AUTH_COMMAND_t authCmd = {0};

  /* Find the persistent handle */
  rc = grub_tpm2_readpublic (handle, NULL, &public);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Handle 0x%x not found.\n", handle);
      return GRUB_ERR_BAD_ARGUMENT;
    }

  /* Evict the persistent handle */
  authCmd.sessionHandle = TPM_RS_PW;
  rc = grub_tpm2_evictcontrol (TPM_RH_OWNER, handle, &authCmd, handle, NULL);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Failed to evict handle 0x%x (TPM2_EvictControl: 0x%x).\n", handle, rc);
      return GRUB_ERR_BAD_DEVICE;
    }

  return GRUB_ERR_NONE;
}

static grub_err_t
protect_tpm2_nv_undefine (TPM_HANDLE_t handle)
{
  TPM_RC_t rc;
  TPM2B_NV_PUBLIC_t nv_public;
  TPMS_AUTH_COMMAND_t authCmd = {0};
  TPM2B_NAME_t nv_name;

  /* Find the nvindex handle */
  rc = grub_tpm2_nv_readpublic (handle, NULL, &nv_public, &nv_name);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Handle 0x%x not found.\n", handle);
      return GRUB_ERR_BAD_ARGUMENT;
    }

  /* Undefine the nvindex handle */
  authCmd.sessionHandle = TPM_RS_PW;
  rc = grub_tpm2_nv_undefinespace (TPM_RH_OWNER, handle, &authCmd);
  if (rc != TPM_RC_SUCCESS)
    {
      fprintf (stderr, "Failed to undefine handle 0x%x (TPM2_NV_UndefineSpace: 0x%x).\n", handle, rc);
      return GRUB_ERR_BAD_DEVICE;
    }

  return GRUB_ERR_NONE;
}

static grub_err_t
protect_tpm2_remove (protect_args_t *args)
{
  grub_err_t err;

  if (args->tpm2_evict == false)
    {
      printf ("--tpm2-evict not specified, nothing to do.\n");
      return GRUB_ERR_NONE;
    }

  err = protect_tpm2_open_device (args->tpm2_device);
  if (err != GRUB_ERR_NONE)
    return err;

  if (args->tpm2_srk != 0)
    {
      err = protect_tpm2_evict (args->tpm2_srk);
      if (err != GRUB_ERR_NONE)
	goto exit;
    }

  if (args->tpm2_nvindex != 0)
    {
      if (TPM_HT_IS_PERSISTENT (args->tpm2_nvindex) == true)
	{
	  err = protect_tpm2_evict (args->tpm2_nvindex);
	  if (err != GRUB_ERR_NONE)
	    goto exit;
	}
      else if (TPM_HT_IS_NVINDEX (args->tpm2_nvindex) == true)
	{
	  err = protect_tpm2_nv_undefine (args->tpm2_nvindex);
	  if (err != GRUB_ERR_NONE)
	    goto exit;
	}
      else
	{
	  fprintf (stderr, "Unsupported handle 0x%x\n", args->tpm2_nvindex);
	  err = GRUB_ERR_BAD_ARGUMENT;
	  goto exit;
	}
    }

  err = GRUB_ERR_NONE;

 exit:
  protect_tpm2_close_device ();

  return err;
}

static grub_err_t
protect_tpm2_run (protect_args_t *args)
{
  switch (args->action)
    {
    case PROTECT_ACTION_ADD:
      return protect_tpm2_add (args);

    case PROTECT_ACTION_REMOVE:
      return protect_tpm2_remove (args);

    default:
      return GRUB_ERR_BAD_ARGUMENT;
    }
}

static grub_err_t
protect_tpm2_args_verify (protect_args_t *args)
{
  if (args->tpm2_device == NULL)
    args->tpm2_device = "/dev/tpm0";

  switch (args->action)
    {
    case PROTECT_ACTION_ADD:
      if (args->args & PROTECT_ARG_TPM2_EVICT)
	{
	  fprintf (stderr, N_("--tpm2-evict is invalid when --action is 'add'.\n"));
	  return GRUB_ERR_BAD_ARGUMENT;
	}

      if (args->tpm2_keyfile == NULL)
	{
	  fprintf (stderr, N_("--tpm2-keyfile must be specified.\n"));
	  return GRUB_ERR_BAD_ARGUMENT;
	}

      if (args->tpm2_outfile == NULL && args->tpm2_nvindex == 0)
	{
	  fprintf (stderr, N_("--tpm2-outfile or --tpm2-nvindex must be specified.\n"));
	  return GRUB_ERR_BAD_ARGUMENT;
	}

      if (args->tpm2_nvindex != 0)
	{
	  if (args->tpm2_tpm2key == true && TPM_HT_IS_PERSISTENT (args->tpm2_nvindex) == true)
	    {
	      fprintf (stderr, N_("Persistent handle does not support TPM 2.0 Key File format.\n"));
	      return GRUB_ERR_BAD_ARGUMENT;
	    }

	  if (TPM_HT_IS_PERSISTENT (args->tpm2_nvindex) == false && TPM_HT_IS_NVINDEX (args->tpm2_nvindex) == false)
	    {
	      fprintf (stderr, N_("--tpm2-nvindex must be a persistent or NV index handle.\n"));
	      return GRUB_ERR_BAD_ARGUMENT;
	    }

	  if (args->tpm2_nvindex == args->tpm2_srk)
	    {
	      fprintf (stderr, N_("--tpm2-nvindex and --tpm2-srk must be different.\n"));
	      return GRUB_ERR_BAD_ARGUMENT;
	    }
	}

      if (args->tpm2_srk != 0 && TPM_HT_IS_PERSISTENT(args->tpm2_srk) == false)
	{
	  fprintf (stderr, N_("--tpm2-srk must be a persistent handle, e.g. 0x81000000.\n"));
	  return GRUB_ERR_BAD_ARGUMENT;
	}

      if (args->tpm2_pcr_count == 0)
	{
	  args->tpm2_pcrs[0] = 7;
	  args->tpm2_pcr_count = 1;
	}

      if (args->srk_type.type == TPM_ALG_ERROR)
	{
	  args->srk_type.type = TPM_ALG_ECC;
	  args->srk_type.detail.ecc_curve = TPM_ECC_NIST_P256;
	}

      if (args->tpm2_bank == TPM_ALG_ERROR)
	args->tpm2_bank = TPM_ALG_SHA256;

      break;

    case PROTECT_ACTION_REMOVE:
      if (args->args & PROTECT_ARG_TPM2_ASYMMETRIC)
	{
	  fprintf (stderr, N_("--tpm2-asymmetric is invalid when --action is 'remove'.\n"));
	  return GRUB_ERR_BAD_ARGUMENT;
	}

      if (args->args & PROTECT_ARG_TPM2_BANK)
	{
	  fprintf (stderr, N_("--tpm2-bank is invalid when --action is 'remove'.\n"));
	  return GRUB_ERR_BAD_ARGUMENT;
	}

      if (args->args & PROTECT_ARG_TPM2_KEYFILE)
	{
	  fprintf (stderr, N_("--tpm2-keyfile is invalid when --action is 'remove'.\n"));
	  return GRUB_ERR_BAD_ARGUMENT;
	}

      if (args->args & PROTECT_ARG_TPM2_OUTFILE)
	{
	  fprintf (stderr, N_("--tpm2-outfile is invalid when --action is 'remove'.\n"));
	  return GRUB_ERR_BAD_ARGUMENT;
	}

      if (args->args & PROTECT_ARG_TPM2_PCRS)
	{
	  fprintf (stderr, N_("--tpm2-pcrs is invalid when --action is 'remove'.\n"));
	  return GRUB_ERR_BAD_ARGUMENT;
	}

      if (args->tpm2_srk == 0 && args->tpm2_nvindex == 0)
	{
	  fprintf (stderr, N_("missing --tpm2-srk or --tpm2-nvindex for --action 'remove'.\n"));
	  return GRUB_ERR_BAD_ARGUMENT;
	}

      break;

    default:
      fprintf (stderr, N_("The TPM2 key protector only supports the following actions: add, remove.\n"));
      return GRUB_ERR_BAD_ARGUMENT;
    }

  return GRUB_ERR_NONE;
}

static error_t
protect_argp_parser (int key, char *arg, struct argp_state *state)
{
  grub_err_t err;
  protect_args_t *args = state->input;

  switch (key)
    {
    case PROTECT_OPT_ACTION:
      if (args->args & PROTECT_ARG_ACTION)
	{
	  fprintf (stderr, N_("--action|-a can only be specified once.\n"));
	  return EINVAL;
	}

      if (grub_strcmp (arg, "add") == 0)
	args->action = PROTECT_ACTION_ADD;
      else if (grub_strcmp (arg, "remove") == 0)
	args->action = PROTECT_ACTION_REMOVE;
      else
	{
	  fprintf (stderr, N_("'%s' is not a valid action.\n"), arg);
	  return EINVAL;
	}

      args->args |= PROTECT_ARG_ACTION;
      break;

    case PROTECT_OPT_PROTECTOR:
      if (args->args & PROTECT_ARG_PROTECTOR)
	{
	  fprintf (stderr, N_("--protector|-p can only be specified once.\n"));
	  return EINVAL;
	}

      if (grub_strcmp (arg, "tpm2") == 0)
	args->protector = PROTECT_TYPE_TPM2;
      else
	{
	  fprintf (stderr, N_("'%s' is not a valid protector.\n"), arg);
	  return EINVAL;
	}

      args->args |= PROTECT_ARG_PROTECTOR;
      break;

    case PROTECT_OPT_TPM2_DEVICE:
      if (args->args & PROTECT_ARG_TPM2_DEVICE)
	{
	  fprintf (stderr, N_("--tpm2-device can only be specified once.\n"));
	  return EINVAL;
	}

      args->tpm2_device = xstrdup (arg);
      args->args |= PROTECT_ARG_TPM2_DEVICE;
      break;

    case PROTECT_OPT_TPM2_PCRS:
      if (args->args & PROTECT_ARG_TPM2_PCRS)
	{
	  fprintf (stderr, N_("--tpm2-pcrs can only be specified once.\n"));
	  return EINVAL;
	}

      err = grub_tpm2_protector_parse_pcrs (arg, args->tpm2_pcrs,
					    &args->tpm2_pcr_count);
      if (err != GRUB_ERR_NONE)
	{
	  if (grub_errno != GRUB_ERR_NONE)
	    grub_print_error ();
	  return EINVAL;
	}

      args->args |= PROTECT_ARG_TPM2_PCRS;
      break;

    case PROTECT_OPT_TPM2_SRK:
      if (args->args & PROTECT_ARG_TPM2_SRK)
	{
	  fprintf (stderr, N_("--tpm2-srk can only be specified once.\n"));
	  return EINVAL;
	}

      err = grub_tpm2_protector_parse_tpm_handle (arg, &args->tpm2_srk);
      if (err != GRUB_ERR_NONE)
	{
	  if (grub_errno != GRUB_ERR_NONE)
	    grub_print_error ();
	  return EINVAL;
	}

      args->args |= PROTECT_ARG_TPM2_SRK;
      break;

    case PROTECT_OPT_TPM2_ASYMMETRIC:
      if (args->args & PROTECT_ARG_TPM2_ASYMMETRIC)
	{
	  fprintf (stderr, N_("--tpm2-asymmetric can only be specified once.\n"));
	  return EINVAL;
	}

      err = grub_tpm2_protector_parse_asymmetric (arg, &args->srk_type);
      if (err != GRUB_ERR_NONE)
	{
	  if (grub_errno != GRUB_ERR_NONE)
	    grub_print_error ();
	  return EINVAL;
	}

      args->args |= PROTECT_ARG_TPM2_ASYMMETRIC;
      break;

    case PROTECT_OPT_TPM2_BANK:
      if (args->args & PROTECT_ARG_TPM2_BANK)
	{
	  fprintf (stderr, N_("--tpm2-bank can only be specified once.\n"));
	  return EINVAL;
	}

      err = grub_tpm2_protector_parse_bank (arg, &args->tpm2_bank);
      if (err != GRUB_ERR_NONE)
	{
	  if (grub_errno != GRUB_ERR_NONE)
	    grub_print_error ();
	  return EINVAL;
	}

      args->args |= PROTECT_ARG_TPM2_BANK;
      break;

    case PROTECT_OPT_TPM2_KEYFILE:
      if (args->args & PROTECT_ARG_TPM2_KEYFILE)
	{
	  fprintf (stderr, N_("--tpm2-keyfile can only be specified once.\n"));
	  return EINVAL;
	}

      args->tpm2_keyfile = xstrdup(arg);
      args->args |= PROTECT_ARG_TPM2_KEYFILE;
      break;

    case PROTECT_OPT_TPM2_OUTFILE:
      if (args->args & PROTECT_ARG_TPM2_OUTFILE)
	{
	  fprintf (stderr, N_("--tpm2-outfile can only be specified once.\n"));
	  return EINVAL;
	}

      args->tpm2_outfile = xstrdup(arg);
      args->args |= PROTECT_ARG_TPM2_OUTFILE;
      break;

    case PROTECT_OPT_TPM2_EVICT:
      if (args->args & PROTECT_ARG_TPM2_EVICT)
	{
	  fprintf (stderr, N_("--tpm2-evict can only be specified once.\n"));
	  return EINVAL;
	}

      args->tpm2_evict = true;
      args->args |= PROTECT_ARG_TPM2_EVICT;
      break;

    case PROTECT_OPT_TPM2_TPM2KEY:
      if (args->args & PROTECT_ARG_TPM2_TPM2KEY)
	{
	  fprintf (stderr, N_("--tpm2-tpm2key can only be specified once.\n"));
	  return EINVAL;
	}

      args->tpm2_tpm2key = true;
      args->args |= PROTECT_ARG_TPM2_TPM2KEY;
      break;

    case PROTECT_OPT_TPM2_NVINDEX:
      if (args->args & PROTECT_ARG_TPM2_NVINDEX)
	{
	  fprintf (stderr, N_("--tpm2-nvindex can only be specified once.\n"));
	  return EINVAL;
	}

      err = grub_tpm2_protector_parse_tpm_handle (arg, &args->tpm2_nvindex);
      if (err != GRUB_ERR_NONE)
	{
	  if (grub_errno != GRUB_ERR_NONE)
	    grub_print_error ();
	  return EINVAL;
	}

      args->args |= PROTECT_ARG_TPM2_NVINDEX;
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }

  return 0;
}

static grub_err_t
protect_args_verify (protect_args_t *args)
{
  if (args->action == PROTECT_ACTION_ERROR)
    {
      fprintf (stderr, N_("--action is mandatory.\n"));
      return GRUB_ERR_BAD_ARGUMENT;
    }

  /*
   * At the moment, the only configurable key protector is the TPM2 one, so it
   * is the only key protector supported by this tool.
   */
  if (args->protector != PROTECT_TYPE_TPM2)
    {
      fprintf (stderr, N_("--protector is mandatory and only 'tpm2' is currently supported.\n"));
      return GRUB_ERR_BAD_ARGUMENT;
    }

  switch (args->protector)
    {
    case PROTECT_TYPE_TPM2:
      return protect_tpm2_args_verify (args);
    default:
      return GRUB_ERR_BAD_ARGUMENT;
    }

  return GRUB_ERR_NONE;
}

static grub_err_t
protect_dispatch (protect_args_t *args)
{
  switch (args->protector)
    {
    case PROTECT_TYPE_TPM2:
      return protect_tpm2_run (args);
    default:
      return GRUB_ERR_BAD_ARGUMENT;
    }
}

static void
protect_init (int *argc, char **argv[])
{
  grub_util_host_init (argc, argv);

  grub_util_biosdisk_init (NULL);

  grub_init_all ();

  grub_lvm_fini ();
  grub_mdraid09_fini ();
  grub_mdraid1x_fini ();
  grub_diskfilter_fini ();
  grub_diskfilter_init ();
  grub_mdraid09_init ();
  grub_mdraid1x_init ();
  grub_lvm_init ();
}

static void
protect_fini (void)
{
  grub_fini_all ();
  grub_util_biosdisk_fini ();
}

static struct argp protect_argp =
{
  .options     = protect_options,
  .parser      = protect_argp_parser,
  .args_doc    = NULL,
  .doc         =
    N_("Protect a cleartext key using a GRUB key protector that can retrieve "
       "the key during boot to unlock fully-encrypted disks automatically."),
  .children    = NULL,
  .help_filter = NULL,
  .argp_domain = NULL
};

int
main (int argc, char *argv[])
{
  grub_err_t err;
  protect_args_t args = {0};

  if (argp_parse (&protect_argp, argc, argv, 0, 0, &args) != 0)
    {
      fprintf (stderr, N_("Could not parse arguments.\n"));
      return EXIT_FAILURE;
    }

  protect_init (&argc, &argv);

  err = protect_args_verify (&args);
  if (err != GRUB_ERR_NONE)
    goto exit;

  err = protect_dispatch (&args);

 exit:
  protect_fini ();

  if (err != GRUB_ERR_NONE)
    return EXIT_FAILURE;

  return EXIT_SUCCESS;
}