|
| 1 | +# @file |
| 2 | +# |
| 3 | +# Copyright (c) Microsoft Corporation. |
| 4 | +# SPDX-License-Identifier: BSD-2-Clause-Patent |
| 5 | +## |
| 6 | +"""Strip PKCS#7 ContentInfo wrappers from EFI auth variable signatures. |
| 7 | +
|
| 8 | +Some tooling expects the certificate payload to be raw SignedData instead of a |
| 9 | +ContentInfo wrapper. This script rewrites an authenticated variable payload by |
| 10 | +replacing cert_data with DER-encoded SignedData. |
| 11 | +""" |
| 12 | + |
| 13 | +import argparse |
| 14 | +import logging |
| 15 | +import pathlib |
| 16 | +import sys |
| 17 | + |
| 18 | +from edk2toollib.uefi.authenticated_variables_structure_support import EfiVariableAuthentication2 |
| 19 | +from pyasn1.codec.der.decoder import decode as der_decode |
| 20 | +from pyasn1.codec.der.encoder import encode as der_encode |
| 21 | +from pyasn1_modules import rfc2315 |
| 22 | + |
| 23 | + |
| 24 | +def pkcs7_get_signed_data_structure(signature: bytes) -> bytes: |
| 25 | + """Return DER-encoded SignedData from a DER PKCS#7 payload. |
| 26 | +
|
| 27 | + The input may be either ContentInfo(signedData) or SignedData directly. |
| 28 | + """ |
| 29 | + try: |
| 30 | + content_info, _ = der_decode(signature, asn1Spec=rfc2315.ContentInfo()) |
| 31 | + content_type = content_info.getComponentByName("contentType") |
| 32 | + if content_type != rfc2315.signedData: |
| 33 | + raise ValueError("PKCS#7 payload is not signedData content") |
| 34 | + |
| 35 | + signed_data, _ = der_decode( |
| 36 | + content_info.getComponentByName("content"), |
| 37 | + asn1Spec=rfc2315.SignedData(), |
| 38 | + ) |
| 39 | + logging.info("Found PKCS#7 ContentInfo(signedData); stripping ContentInfo wrapper") |
| 40 | + return der_encode(signed_data) |
| 41 | + except Exception as content_info_error: |
| 42 | + logging.debug("ContentInfo decode failed: %s", content_info_error) |
| 43 | + logging.info("Input does not decode as ContentInfo; trying SignedData") |
| 44 | + |
| 45 | + try: |
| 46 | + signed_data, _ = der_decode(signature, asn1Spec=rfc2315.SignedData()) |
| 47 | + logging.info("Input already decodes as SignedData") |
| 48 | + return der_encode(signed_data) |
| 49 | + except Exception as signed_data_error: |
| 50 | + raise ValueError( |
| 51 | + "Signature is neither ContentInfo(signedData) nor SignedData" |
| 52 | + ) from signed_data_error |
| 53 | + |
| 54 | + |
| 55 | +def strip_content_info(signed_payload: pathlib.Path) -> pathlib.Path: |
| 56 | + """Rewrite signed_payload with cert_data set to raw SignedData. |
| 57 | +
|
| 58 | + Returns the path of the rewritten output file. |
| 59 | + """ |
| 60 | + with open(signed_payload, "rb") as in_file: |
| 61 | + auth_var = EfiVariableAuthentication2(decodefs=in_file) |
| 62 | + |
| 63 | + # cert_data contains the PKCS#7 blob carried inside WIN_CERTIFICATE_UEFI_GUID. |
| 64 | + signed_data = pkcs7_get_signed_data_structure(auth_var.auth_info.cert_data) |
| 65 | + auth_var.auth_info.cert_data = signed_data |
| 66 | + |
| 67 | + out_path = signed_payload.with_name(signed_payload.name + ".stripped") |
| 68 | + with open(out_path, "wb") as out_file: |
| 69 | + out_file.write(auth_var.encode()) |
| 70 | + |
| 71 | + logging.info("Stripped signed payload written to: %s", out_path) |
| 72 | + return out_path |
| 73 | + |
| 74 | + |
| 75 | +def main() -> int: |
| 76 | + """Parse CLI arguments and strip ContentInfo from the provided payload.""" |
| 77 | + parser = argparse.ArgumentParser(description="Strip ContentInfo from signed payload") |
| 78 | + parser.add_argument("signed_payload", type=pathlib.Path, help="Path to signed payload") |
| 79 | + args = parser.parse_args() |
| 80 | + |
| 81 | + try: |
| 82 | + strip_content_info(args.signed_payload) |
| 83 | + except Exception as error: |
| 84 | + logging.error("Failed to strip ContentInfo: %s", error) |
| 85 | + return 1 |
| 86 | + |
| 87 | + return 0 |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") |
| 92 | + sys.exit(main()) |
0 commit comments