]>
Commit | Line | Data |
---|---|---|
11eaa7af YZ |
1 | ## @file\r |
2 | # This tool adds EFI_FIRMWARE_IMAGE_AUTHENTICATION for a binary.\r | |
3 | #\r | |
4 | # This tool only support CertType - EFI_CERT_TYPE_PKCS7_GUID\r | |
5 | # {0x4aafd29d, 0x68df, 0x49ee, {0x8a, 0xa9, 0x34, 0x7d, 0x37, 0x56, 0x65, 0xa7}}\r | |
6 | #\r | |
7 | # This tool has been tested with OpenSSL.\r | |
8 | #\r | |
8a0933f4 | 9 | # Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>\r |
2e351cbe | 10 | # SPDX-License-Identifier: BSD-2-Clause-Patent\r |
11eaa7af YZ |
11 | #\r |
12 | \r | |
13 | '''\r | |
14 | Pkcs7Sign\r | |
15 | '''\r | |
1ccc4d89 | 16 | from __future__ import print_function\r |
11eaa7af YZ |
17 | \r |
18 | import os\r | |
19 | import sys\r | |
20 | import argparse\r | |
21 | import subprocess\r | |
22 | import uuid\r | |
23 | import struct\r | |
24 | import collections\r | |
25 | from Common.BuildVersion import gBUILD_VERSION\r | |
26 | \r | |
27 | #\r | |
28 | # Globals for help information\r | |
29 | #\r | |
30 | __prog__ = 'Pkcs7Sign'\r | |
31 | __version__ = '%s Version %s' % (__prog__, '0.9 ' + gBUILD_VERSION)\r | |
32 | __copyright__ = 'Copyright (c) 2016, Intel Corporation. All rights reserved.'\r | |
33 | __usage__ = '%s -e|-d [options] <input_file>' % (__prog__)\r | |
34 | \r | |
35 | #\r | |
36 | # GUID for PKCS7 from UEFI Specification\r | |
37 | #\r | |
38 | WIN_CERT_REVISION = 0x0200\r | |
39 | WIN_CERT_TYPE_EFI_GUID = 0x0EF1\r | |
40 | EFI_CERT_TYPE_PKCS7_GUID = uuid.UUID('{4aafd29d-68df-49ee-8aa9-347d375665a7}')\r | |
41 | \r | |
42 | #\r | |
43 | # typedef struct _WIN_CERTIFICATE {\r | |
44 | # UINT32 dwLength;\r | |
45 | # UINT16 wRevision;\r | |
46 | # UINT16 wCertificateType;\r | |
47 | # //UINT8 bCertificate[ANYSIZE_ARRAY];\r | |
48 | # } WIN_CERTIFICATE;\r | |
49 | #\r | |
50 | # typedef struct _WIN_CERTIFICATE_UEFI_GUID {\r | |
51 | # WIN_CERTIFICATE Hdr;\r | |
52 | # EFI_GUID CertType;\r | |
53 | # //UINT8 CertData[ANYSIZE_ARRAY];\r | |
54 | # } WIN_CERTIFICATE_UEFI_GUID;\r | |
55 | #\r | |
56 | # typedef struct {\r | |
57 | # UINT64 MonotonicCount;\r | |
58 | # WIN_CERTIFICATE_UEFI_GUID AuthInfo;\r | |
59 | # } EFI_FIRMWARE_IMAGE_AUTHENTICATION;\r | |
60 | #\r | |
61 | \r | |
62 | #\r | |
63 | # Filename of test signing private cert that is stored in same directory as this tool\r | |
64 | #\r | |
65 | TEST_SIGNER_PRIVATE_CERT_FILENAME = 'TestCert.pem'\r | |
66 | TEST_OTHER_PUBLIC_CERT_FILENAME = 'TestSub.pub.pem'\r | |
67 | TEST_TRUSTED_PUBLIC_CERT_FILENAME = 'TestRoot.pub.pem'\r | |
68 | \r | |
69 | if __name__ == '__main__':\r | |
70 | #\r | |
71 | # Create command line argument parser object\r | |
72 | #\r | |
56ad03a5 | 73 | parser = argparse.ArgumentParser(prog=__prog__, usage=__usage__, description=__copyright__, conflict_handler='resolve')\r |
11eaa7af YZ |
74 | group = parser.add_mutually_exclusive_group(required=True)\r |
75 | group.add_argument("-e", action="store_true", dest='Encode', help='encode file')\r | |
76 | group.add_argument("-d", action="store_true", dest='Decode', help='decode file')\r | |
56ad03a5 | 77 | group.add_argument("--version", action='version', version=__version__)\r |
11eaa7af YZ |
78 | parser.add_argument("-o", "--output", dest='OutputFile', type=str, metavar='filename', help="specify the output filename", required=True)\r |
79 | parser.add_argument("--signer-private-cert", dest='SignerPrivateCertFile', type=argparse.FileType('rb'), help="specify the signer private cert filename. If not specified, a test signer private cert is used.")\r | |
80 | parser.add_argument("--other-public-cert", dest='OtherPublicCertFile', type=argparse.FileType('rb'), help="specify the other public cert filename. If not specified, a test other public cert is used.")\r | |
81 | parser.add_argument("--trusted-public-cert", dest='TrustedPublicCertFile', type=argparse.FileType('rb'), help="specify the trusted public cert filename. If not specified, a test trusted public cert is used.")\r | |
82 | parser.add_argument("--monotonic-count", dest='MonotonicCountStr', type=str, help="specify the MonotonicCount in FMP capsule. If not specified, 0 is used.")\r | |
83 | parser.add_argument("--signature-size", dest='SignatureSizeStr', type=str, help="specify the signature size for decode process.")\r | |
84 | parser.add_argument("-v", "--verbose", dest='Verbose', action="store_true", help="increase output messages")\r | |
85 | parser.add_argument("-q", "--quiet", dest='Quiet', action="store_true", help="reduce output messages")\r | |
ccaa7754 | 86 | parser.add_argument("--debug", dest='Debug', type=int, metavar='[0-9]', choices=range(0, 10), default=0, help="set debug level")\r |
11eaa7af YZ |
87 | parser.add_argument(metavar="input_file", dest='InputFile', type=argparse.FileType('rb'), help="specify the input filename")\r |
88 | \r | |
89 | #\r | |
90 | # Parse command line arguments\r | |
91 | #\r | |
92 | args = parser.parse_args()\r | |
93 | \r | |
94 | #\r | |
95 | # Generate file path to Open SSL command\r | |
96 | #\r | |
97 | OpenSslCommand = 'openssl'\r | |
98 | try:\r | |
99 | OpenSslPath = os.environ['OPENSSL_PATH']\r | |
100 | OpenSslCommand = os.path.join(OpenSslPath, OpenSslCommand)\r | |
b1051537 YZ |
101 | if ' ' in OpenSslCommand:\r |
102 | OpenSslCommand = '"' + OpenSslCommand + '"'\r | |
11eaa7af YZ |
103 | except:\r |
104 | pass\r | |
105 | \r | |
106 | #\r | |
107 | # Verify that Open SSL command is available\r | |
108 | #\r | |
109 | try:\r | |
8a0933f4 | 110 | Process = subprocess.Popen('%s version' % (OpenSslCommand), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r |
11eaa7af | 111 | except:\r |
72443dd2 | 112 | print('ERROR: Open SSL command not available. Please verify PATH or set OPENSSL_PATH')\r |
11eaa7af YZ |
113 | sys.exit(1)\r |
114 | \r | |
115 | Version = Process.communicate()\r | |
87d2afd0 | 116 | if Process.returncode != 0:\r |
72443dd2 | 117 | print('ERROR: Open SSL command not available. Please verify PATH or set OPENSSL_PATH')\r |
11eaa7af | 118 | sys.exit(Process.returncode)\r |
8ddec24d | 119 | print(Version[0].decode())\r |
11eaa7af YZ |
120 | \r |
121 | #\r | |
122 | # Read input file into a buffer and save input filename\r | |
123 | #\r | |
124 | args.InputFileName = args.InputFile.name\r | |
125 | args.InputFileBuffer = args.InputFile.read()\r | |
126 | args.InputFile.close()\r | |
127 | \r | |
128 | #\r | |
129 | # Save output filename and check if path exists\r | |
130 | #\r | |
131 | OutputDir = os.path.dirname(args.OutputFile)\r | |
132 | if not os.path.exists(OutputDir):\r | |
72443dd2 | 133 | print('ERROR: The output path does not exist: %s' % OutputDir)\r |
11eaa7af YZ |
134 | sys.exit(1)\r |
135 | args.OutputFileName = args.OutputFile\r | |
136 | \r | |
137 | try:\r | |
138 | if args.MonotonicCountStr.upper().startswith('0X'):\r | |
af881abc | 139 | args.MonotonicCountValue = int(args.MonotonicCountStr, 16)\r |
11eaa7af | 140 | else:\r |
af881abc | 141 | args.MonotonicCountValue = int(args.MonotonicCountStr)\r |
11eaa7af | 142 | except:\r |
af881abc | 143 | args.MonotonicCountValue = int(0)\r |
11eaa7af YZ |
144 | \r |
145 | if args.Encode:\r | |
146 | #\r | |
147 | # Save signer private cert filename and close private cert file\r | |
148 | #\r | |
149 | try:\r | |
150 | args.SignerPrivateCertFileName = args.SignerPrivateCertFile.name\r | |
151 | args.SignerPrivateCertFile.close()\r | |
152 | except:\r | |
153 | try:\r | |
154 | #\r | |
155 | # Get path to currently executing script or executable\r | |
156 | #\r | |
157 | if hasattr(sys, 'frozen'):\r | |
158 | Pkcs7ToolPath = sys.executable\r | |
159 | else:\r | |
160 | Pkcs7ToolPath = sys.argv[0]\r | |
161 | if Pkcs7ToolPath.startswith('"'):\r | |
162 | Pkcs7ToolPath = Pkcs7ToolPath[1:]\r | |
163 | if Pkcs7ToolPath.endswith('"'):\r | |
164 | Pkcs7ToolPath = RsaToolPath[:-1]\r | |
165 | args.SignerPrivateCertFileName = os.path.join(os.path.dirname(os.path.realpath(Pkcs7ToolPath)), TEST_SIGNER_PRIVATE_CERT_FILENAME)\r | |
166 | args.SignerPrivateCertFile = open(args.SignerPrivateCertFileName, 'rb')\r | |
167 | args.SignerPrivateCertFile.close()\r | |
168 | except:\r | |
72443dd2 | 169 | print('ERROR: test signer private cert file %s missing' % (args.SignerPrivateCertFileName))\r |
11eaa7af YZ |
170 | sys.exit(1)\r |
171 | \r | |
172 | #\r | |
173 | # Save other public cert filename and close public cert file\r | |
174 | #\r | |
175 | try:\r | |
176 | args.OtherPublicCertFileName = args.OtherPublicCertFile.name\r | |
177 | args.OtherPublicCertFile.close()\r | |
178 | except:\r | |
179 | try:\r | |
180 | #\r | |
181 | # Get path to currently executing script or executable\r | |
182 | #\r | |
183 | if hasattr(sys, 'frozen'):\r | |
184 | Pkcs7ToolPath = sys.executable\r | |
185 | else:\r | |
186 | Pkcs7ToolPath = sys.argv[0]\r | |
187 | if Pkcs7ToolPath.startswith('"'):\r | |
188 | Pkcs7ToolPath = Pkcs7ToolPath[1:]\r | |
189 | if Pkcs7ToolPath.endswith('"'):\r | |
190 | Pkcs7ToolPath = RsaToolPath[:-1]\r | |
191 | args.OtherPublicCertFileName = os.path.join(os.path.dirname(os.path.realpath(Pkcs7ToolPath)), TEST_OTHER_PUBLIC_CERT_FILENAME)\r | |
192 | args.OtherPublicCertFile = open(args.OtherPublicCertFileName, 'rb')\r | |
193 | args.OtherPublicCertFile.close()\r | |
194 | except:\r | |
72443dd2 | 195 | print('ERROR: test other public cert file %s missing' % (args.OtherPublicCertFileName))\r |
11eaa7af YZ |
196 | sys.exit(1)\r |
197 | \r | |
245cda66 YZ |
198 | format = "%dsQ" % len(args.InputFileBuffer)\r |
199 | FullInputFileBuffer = struct.pack(format, args.InputFileBuffer, args.MonotonicCountValue)\r | |
11eaa7af YZ |
200 | \r |
201 | #\r | |
202 | # Sign the input file using the specified private key and capture signature from STDOUT\r | |
203 | #\r | |
a5f26fef | 204 | Process = subprocess.Popen('%s smime -sign -binary -signer "%s" -outform DER -md sha256 -certfile "%s"' % (OpenSslCommand, args.SignerPrivateCertFileName, args.OtherPublicCertFileName), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r |
11eaa7af | 205 | Signature = Process.communicate(input=FullInputFileBuffer)[0]\r |
87d2afd0 | 206 | if Process.returncode != 0:\r |
11eaa7af YZ |
207 | sys.exit(Process.returncode)\r |
208 | \r | |
209 | #\r | |
210 | # Write output file that contains Signature, and Input data\r | |
211 | #\r | |
212 | args.OutputFile = open(args.OutputFileName, 'wb')\r | |
213 | args.OutputFile.write(Signature)\r | |
214 | args.OutputFile.write(args.InputFileBuffer)\r | |
215 | args.OutputFile.close()\r | |
216 | \r | |
217 | if args.Decode:\r | |
218 | #\r | |
219 | # Save trusted public cert filename and close public cert file\r | |
220 | #\r | |
221 | try:\r | |
222 | args.TrustedPublicCertFileName = args.TrustedPublicCertFile.name\r | |
223 | args.TrustedPublicCertFile.close()\r | |
224 | except:\r | |
225 | try:\r | |
226 | #\r | |
227 | # Get path to currently executing script or executable\r | |
228 | #\r | |
229 | if hasattr(sys, 'frozen'):\r | |
230 | Pkcs7ToolPath = sys.executable\r | |
231 | else:\r | |
232 | Pkcs7ToolPath = sys.argv[0]\r | |
233 | if Pkcs7ToolPath.startswith('"'):\r | |
234 | Pkcs7ToolPath = Pkcs7ToolPath[1:]\r | |
235 | if Pkcs7ToolPath.endswith('"'):\r | |
236 | Pkcs7ToolPath = RsaToolPath[:-1]\r | |
237 | args.TrustedPublicCertFileName = os.path.join(os.path.dirname(os.path.realpath(Pkcs7ToolPath)), TEST_TRUSTED_PUBLIC_CERT_FILENAME)\r | |
238 | args.TrustedPublicCertFile = open(args.TrustedPublicCertFileName, 'rb')\r | |
239 | args.TrustedPublicCertFile.close()\r | |
240 | except:\r | |
72443dd2 | 241 | print('ERROR: test trusted public cert file %s missing' % (args.TrustedPublicCertFileName))\r |
11eaa7af YZ |
242 | sys.exit(1)\r |
243 | \r | |
244 | if not args.SignatureSizeStr:\r | |
72443dd2 | 245 | print("ERROR: please use the option --signature-size to specify the size of the signature data!")\r |
11eaa7af YZ |
246 | sys.exit(1)\r |
247 | else:\r | |
248 | if args.SignatureSizeStr.upper().startswith('0X'):\r | |
af881abc | 249 | SignatureSize = int(args.SignatureSizeStr, 16)\r |
11eaa7af | 250 | else:\r |
af881abc | 251 | SignatureSize = int(args.SignatureSizeStr)\r |
11eaa7af | 252 | if SignatureSize < 0:\r |
72443dd2 | 253 | print("ERROR: The value of option --signature-size can't be set to negative value!")\r |
11eaa7af YZ |
254 | sys.exit(1)\r |
255 | elif SignatureSize > len(args.InputFileBuffer):\r | |
72443dd2 | 256 | print("ERROR: The value of option --signature-size is exceed the size of the input file !")\r |
11eaa7af YZ |
257 | sys.exit(1)\r |
258 | \r | |
259 | args.SignatureBuffer = args.InputFileBuffer[0:SignatureSize]\r | |
260 | args.InputFileBuffer = args.InputFileBuffer[SignatureSize:]\r | |
261 | \r | |
245cda66 YZ |
262 | format = "%dsQ" % len(args.InputFileBuffer)\r |
263 | FullInputFileBuffer = struct.pack(format, args.InputFileBuffer, args.MonotonicCountValue)\r | |
11eaa7af YZ |
264 | \r |
265 | #\r | |
266 | # Save output file contents from input file\r | |
267 | #\r | |
268 | open(args.OutputFileName, 'wb').write(FullInputFileBuffer)\r | |
269 | \r | |
270 | #\r | |
271 | # Verify signature\r | |
272 | #\r | |
a5f26fef | 273 | Process = subprocess.Popen('%s smime -verify -inform DER -content %s -CAfile %s' % (OpenSslCommand, args.OutputFileName, args.TrustedPublicCertFileName), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r |
11eaa7af | 274 | Process.communicate(input=args.SignatureBuffer)[0]\r |
87d2afd0 | 275 | if Process.returncode != 0:\r |
72443dd2 | 276 | print('ERROR: Verification failed')\r |
11eaa7af YZ |
277 | os.remove (args.OutputFileName)\r |
278 | sys.exit(Process.returncode)\r | |
279 | \r | |
280 | open(args.OutputFileName, 'wb').write(args.InputFileBuffer)\r |