summaryrefslogtreecommitdiffstats
path: root/FmpDevicePkg/FmpDxe/DetectTestKey.c
blob: e697a0848970f34e1a3163fd3df555aaafb121ad (plain)
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
/** @file
  Detects if PcdFmpDevicePkcs7CertBufferXdr contains a test key.

  Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <PiDxe.h>
#include <Library/DebugLib.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/PcdLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseCryptLib.h>

/**
  Check to see if any of the keys in PcdFmpDevicePkcs7CertBufferXdr matches
  the test key.  PcdFmpDeviceTestKeySha256Digest contains the SHA256 hash of
  the test key.  For each key in PcdFmpDevicePkcs7CertBufferXdr, compute the
  SHA256 hash and compare it to PcdFmpDeviceTestKeySha256Digest.  If the
  SHA256 hash matches or there is then error computing the SHA256 hash, then
  set PcdTestKeyUsed to TRUE.  Skip this check if PcdTestKeyUsed is already
  TRUE or PcdFmpDeviceTestKeySha256Digest is not exactly SHA256_DIGEST_SIZE
  bytes.
**/
VOID
DetectTestKey (
  VOID
  )
{
  BOOLEAN  TestKeyUsed;
  UINTN    PublicKeyDataLength;
  UINT8    *PublicKeyDataXdr;
  UINT8    *PublicKeyDataXdrEnd;
  VOID     *HashContext;
  UINT8    Digest[SHA256_DIGEST_SIZE];
  UINTN    TestKeyDigestSize;

  //
  // If PcdFmpDeviceTestKeySha256Digest is not exacty SHA256_DIGEST_SIZE bytes,
  // then skip the test key detection.
  //
  TestKeyDigestSize = PcdGetSize (PcdFmpDeviceTestKeySha256Digest);
  if (TestKeyDigestSize != SHA256_DIGEST_SIZE) {
    return;
  }

  //
  // If PcdTestKeyUsed is already TRUE, then skip test key detection
  //
  TestKeyUsed = PcdGetBool (PcdTestKeyUsed);
  if (TestKeyUsed) {
    return;
  }

  //
  // If PcdFmpDevicePkcs7CertBufferXdr is invalid, then skip test key detection
  //
  PublicKeyDataXdr    = PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr);
  PublicKeyDataXdrEnd = PublicKeyDataXdr + PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr);
  if (PublicKeyDataXdr == NULL || PublicKeyDataXdr == PublicKeyDataXdrEnd) {
    return;
  }

  //
  // Allocate hash context buffer required for SHA 256
  //
  HashContext = AllocatePool (Sha256GetContextSize ());
  if (HashContext == NULL) {
    TestKeyUsed = TRUE;
  }

  //
  // Loop through all keys in PcdFmpDevicePkcs7CertBufferXdr
  //
  while (!TestKeyUsed && PublicKeyDataXdr < PublicKeyDataXdrEnd) {
    if (PublicKeyDataXdr + sizeof (UINT32) > PublicKeyDataXdrEnd) {
      //
      // Key data extends beyond end of PCD
      //
      break;
    }
    //
    // Read key length stored in big endian format
    //
    PublicKeyDataLength = SwapBytes32 (*(UINT32 *)(PublicKeyDataXdr));
    //
    // Point to the start of the key data
    //
    PublicKeyDataXdr += sizeof (UINT32);
    if (PublicKeyDataXdr + PublicKeyDataLength > PublicKeyDataXdrEnd) {
      //
      // Key data extends beyond end of PCD
      //
      break;
    }

    //
    // Hash public key from PcdFmpDevicePkcs7CertBufferXdr using SHA256.
    // If error occurs computing SHA256, then assume test key is in use.
    //
    ZeroMem (Digest, SHA256_DIGEST_SIZE);
    if (!Sha256Init (HashContext)) {
      TestKeyUsed = TRUE;
      break;
    }
    if (!Sha256Update (HashContext, PublicKeyDataXdr, PublicKeyDataLength)) {
      TestKeyUsed = TRUE;
      break;
    }
    if (!Sha256Final (HashContext, Digest)) {
      TestKeyUsed = TRUE;
      break;
    }

    //
    // Check if SHA256 hash of public key matches SHA256 hash of test key
    //
    if (CompareMem (Digest, PcdGetPtr (PcdFmpDeviceTestKeySha256Digest), SHA256_DIGEST_SIZE) == 0) {
      TestKeyUsed = TRUE;
      break;
    }

    //
    // Point to start of next key
    //
    PublicKeyDataXdr += PublicKeyDataLength;
    PublicKeyDataXdr = (UINT8 *)ALIGN_POINTER (PublicKeyDataXdr, sizeof (UINT32));
  }

  //
  // Free hash context buffer required for SHA 256
  //
  if (HashContext != NULL) {
    FreePool (HashContext);
    HashContext = NULL;
  }

  //
  // If test key detected or an error occurred checking for the test key, then
  // set PcdTestKeyUsed to TRUE.
  //
  if (TestKeyUsed) {
    DEBUG ((DEBUG_INFO, "FmpDxe: Test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n"));
    PcdSetBoolS (PcdTestKeyUsed, TRUE);
  } else {
    DEBUG ((DEBUG_INFO, "FmpDxe: No test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n"));
  }
}