|
17 | 17 | #include <Guid/WinCertificate.h> |
18 | 18 |
|
19 | 19 | /** |
20 | | - Determine whether a signed PE/COFF image is authorized to execute by the platform's `db` |
21 | | - signature database. |
22 | | -
|
23 | | - An image can be authorized one of two ways: |
24 | | - 1. It's authenticode image hash is found in the `db` database. |
25 | | - 2. A X.509 certificate in the `db` is a trust anchor for one of the image's signatures, and |
26 | | - that X.509 certificate is not also found in the `dbx`. |
27 | | -
|
28 | | - @param[in] SecDataDir Security data directory in the PE/COFF image described by Cache. |
29 | | - @param[in] Db The `db` signature database. |
30 | | - @param[in] DbSize The size of the `db`. |
31 | | - @param[in] Dbx The `dbx` signature database. |
32 | | - @param[in] DbxSize The size of the `dbx`. |
33 | | - @param[in,out] Cache DIGEST_CACHE pointer bound to the image being searched. The cache may |
34 | | - be updated during the search. |
35 | | - @param[in,out] Action Updated to reflect the outcome of the authorization check. |
36 | | -
|
37 | | - @retval TRUE The image is authorized by `db`. |
38 | | - @retval FALSE The image is not authorized, or an error prevented a |
| 20 | + Extract the DER-encoded PKCS#7 SignedData payload from a single WIN_CERTIFICATE entry. |
| 21 | +
|
| 22 | + Supports `WIN_CERT_TYPE_PKCS_SIGNED_DATA` (a bare PKCS#7 payload following the |
| 23 | + WIN_CERTIFICATE header) and `WIN_CERT_TYPE_EFI_GUID` whose `CertType` is |
| 24 | + `gEfiCertPkcs7Guid`. On success, `AuthData` points into `Cert`'s storage and is |
| 25 | + valid for the lifetime of `Cert`. |
| 26 | +
|
| 27 | + @param[in] Cert The certificate to inspect. |
| 28 | + @param[out] AuthData On success, set to point at the PKCS#7 payload inside Cert. |
| 29 | + @param[out] AuthDataSize On success, set to the PKCS#7 payload length in bytes. |
| 30 | +
|
| 31 | + @retval EFI_SUCCESS AuthData / AuthDataSize were populated. |
| 32 | + @retval EFI_INVALID_PARAMETER A required pointer is NULL. |
| 33 | + @retval EFI_UNSUPPORTED Unsupported WIN_CERTIFICATE type. |
| 34 | + @retval EFI_VOLUME_CORRUPTED dwLength is too small to contain the required header |
| 35 | + for the declared type. |
| 36 | +**/ |
| 37 | +EFI_STATUS |
| 38 | +GetWinCertificatePkcs7AuthData ( |
| 39 | + IN CONST WIN_CERTIFICATE *Cert, |
| 40 | + OUT CONST UINT8 **AuthData, |
| 41 | + OUT UINTN *AuthDataSize |
| 42 | + ); |
| 43 | + |
| 44 | +/** |
| 45 | + Determine whether a single embedded WIN_CERTIFICATE is revoked by `dbx`. |
| 46 | +
|
| 47 | + Runs two checks against the same PKCS#7 SignedData payload: |
| 48 | +
|
| 49 | + 1. For every X.509 trust anchor enrolled in `dbx`, ask `AuthenticodeVerify` |
| 50 | + whether it verifies the signature. Any verifying anchor revokes the |
| 51 | + certificate. |
| 52 | + 2. For every signer reported by `Pkcs7GetSigners`, hash the TBS bytes and |
| 53 | + ask `IsTBSCertHashInDbx` whether the resulting hash is enrolled in `dbx`. |
| 54 | + Any enrolled signer revokes the certificate (the chain is poisoned). |
| 55 | +
|
| 56 | + The caller is responsible for extracting `AuthData` from the WIN_CERTIFICATE |
| 57 | + via `GetWinCertificatePkcs7AuthData` and computing the image's Authenticode |
| 58 | + digest under the algorithm reported by `GetAuthenticodeHashAlgorithm`. |
| 59 | +
|
| 60 | + @param[in] AuthData DER-encoded PKCS#7 SignedData payload. |
| 61 | + @param[in] AuthDataSize Size of AuthData in bytes; must be non-zero. |
| 62 | + @param[in] ImageHash Authenticode digest of the image under the algorithm |
| 63 | + AuthData uses. |
| 64 | + @param[in] ImageHashSize Size of ImageHash in bytes; must be non-zero. |
| 65 | + @param[in] Dbx Raw `dbx` contents, or NULL. |
| 66 | + @param[in] DbxSize Size of Dbx in bytes; 0 when Dbx is NULL. |
| 67 | +
|
| 68 | + @retval TRUE The signature is revoked by `dbx`, or a missing/empty `AuthData` |
| 69 | + or `ImageHash` prevented a safe determination (fail closed). |
| 70 | + @retval FALSE The signature is not revoked by `dbx`, including when `dbx` is |
| 71 | + absent or empty. |
| 72 | +**/ |
| 73 | +BOOLEAN |
| 74 | +IsCertRevoked ( |
| 75 | + IN CONST UINT8 *AuthData, |
| 76 | + IN UINTN AuthDataSize, |
| 77 | + IN CONST UINT8 *ImageHash, |
| 78 | + IN UINTN ImageHashSize, |
| 79 | + IN CONST VOID *Dbx, |
| 80 | + IN UINTN DbxSize |
| 81 | + ); |
| 82 | + |
| 83 | +/** |
| 84 | + Determine whether a single PKCS#7 SignedData payload authorizes the image against `db`. |
| 85 | +
|
| 86 | + Walks each `EFI_SIGNATURE_LIST` in `db` looking for an X.509 trust anchor that verifies the |
| 87 | + signature. A verifying anchor whose TBS hash is enrolled in `dbx` is rejected and iteration |
| 88 | + continues. The first verifying anchor that is not revoked authorizes the image. |
| 89 | +
|
| 90 | + The caller is responsible for extracting `AuthData` from the WIN_CERTIFICATE via |
| 91 | + `GetWinCertificatePkcs7AuthData` and computing the image's Authenticode digest under the |
| 92 | + algorithm reported by `GetAuthenticodeHashAlgorithm`. |
| 93 | +
|
| 94 | + @param[in] AuthData DER-encoded PKCS#7 SignedData payload. |
| 95 | + @param[in] AuthDataSize Size of AuthData in bytes; must be non-zero. |
| 96 | + @param[in] ImageHash Authenticode digest of the image under the algorithm |
| 97 | + AuthData uses. |
| 98 | + @param[in] ImageHashSize Size of ImageHash in bytes; must be non-zero. |
| 99 | + @param[in] Db Raw `db` contents, or NULL. |
| 100 | + @param[in] DbSize Size of Db in bytes; 0 when Db is NULL. |
| 101 | + @param[in] Dbx Raw `dbx` contents, or NULL. |
| 102 | + @param[in] DbxSize Size of Dbx in bytes; 0 when Dbx is NULL. |
| 103 | +
|
| 104 | + @retval TRUE The signature authorizes the image. |
| 105 | + @retval FALSE The signature does not authorize the image, or an error prevented a |
39 | 106 | definitive answer. |
40 | 107 | **/ |
41 | 108 | BOOLEAN |
42 | | -IsSignedImageAuthorized ( |
43 | | - IN CONST EFI_IMAGE_DATA_DIRECTORY *SecDataDir, |
44 | | - IN CONST VOID *Db, |
45 | | - IN UINTN DbSize, |
46 | | - IN CONST VOID *Dbx, |
47 | | - IN UINTN DbxSize, |
48 | | - IN OUT DIGEST_CACHE *Cache, |
49 | | - IN OUT EFI_IMAGE_EXECUTION_ACTION *Action |
| 109 | +IsCertAuthorized ( |
| 110 | + IN CONST UINT8 *AuthData, |
| 111 | + IN UINTN AuthDataSize, |
| 112 | + IN CONST UINT8 *ImageHash, |
| 113 | + IN UINTN ImageHashSize, |
| 114 | + IN CONST VOID *Db, |
| 115 | + IN UINTN DbSize, |
| 116 | + IN CONST VOID *Dbx, |
| 117 | + IN UINTN DbxSize |
50 | 118 | ); |
51 | 119 |
|
52 | 120 | /** |
53 | | - Determine whether a signed PE/COFF image is revoked by the platform's `dbx` signature database. |
| 121 | + Determine whether a single embedded WIN_CERTIFICATE entry authorizes the image. |
| 122 | +
|
| 123 | + Extracts the PKCS#7 SignedData payload from `Cert`, identifies the Authenticode hash |
| 124 | + algorithm declared by the signature, computes (or retrieves from `Cache`) the image |
| 125 | + digest under that algorithm, then runs the certificate through `IsCertRevoked` and |
| 126 | + `IsCertAuthorized` in sequence. Any failure in the prelude, or a hit in `dbx`, causes |
| 127 | + the certificate to be skipped (FALSE). The certificate authorizes only when it is not |
| 128 | + revoked and `IsCertAuthorized` accepts it. |
54 | 129 |
|
55 | | - @param[in] SecDataDir Security data directory describing the embedded WIN_CERTIFICATE table. |
56 | | - @param[in] Dbx The `dbx` signature database. |
57 | | - @param[in] DbxSize The size of the `dbx`. |
58 | | - @param[in,out] Cache DIGEST_CACHE pointer bound to the image being searched. The cache may |
59 | | - be updated during the search. |
60 | | - @param[in,out] Action Updated to reflect the outcome of the revocation check. |
| 130 | + @param[in] Cert The certificate to evaluate. |
| 131 | + @param[in,out] Cache Image digest cache bound to the image buffer; the cache may |
| 132 | + memoize one digest per algorithm across calls. |
| 133 | + @param[in] Db Raw `db` contents, or NULL. |
| 134 | + @param[in] DbSize Size of Db in bytes; 0 when Db is NULL. |
| 135 | + @param[in] Dbx Raw `dbx` contents, or NULL. |
| 136 | + @param[in] DbxSize Size of Dbx in bytes; 0 when Dbx is NULL. |
61 | 137 |
|
62 | | - @retval TRUE The image is revoked by `dbx`. |
63 | | - @retval FALSE The image is not revoked. |
| 138 | + @retval TRUE The certificate is not revoked by `dbx` and authorizes the image via `db`. |
| 139 | + @retval FALSE The certificate is malformed, declares an unsupported hash algorithm, the |
| 140 | + image digest could not be computed, the certificate is revoked, or it |
| 141 | + does not authorize the image. |
64 | 142 | **/ |
65 | 143 | BOOLEAN |
66 | | -IsSignedImageRevoked ( |
67 | | - IN CONST EFI_IMAGE_DATA_DIRECTORY *SecDataDir, |
68 | | - IN CONST VOID *Dbx, |
69 | | - IN UINTN DbxSize, |
70 | | - IN OUT DIGEST_CACHE *Cache, |
71 | | - IN OUT EFI_IMAGE_EXECUTION_ACTION *Action |
| 144 | +IsImageAuthorizedByCert ( |
| 145 | + IN CONST WIN_CERTIFICATE *Cert, |
| 146 | + IN OUT DIGEST_CACHE *Cache, |
| 147 | + IN CONST VOID *Db, |
| 148 | + IN UINTN DbSize, |
| 149 | + IN CONST VOID *Dbx, |
| 150 | + IN UINTN DbxSize |
72 | 151 | ); |
73 | 152 |
|
74 | 153 | #endif // DXE_IMAGE_VERIFICATION_LIB_CERTIFICATE_H_ |
0 commit comments